storage.hpp 66 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131
  1. //
  2. // Copyright (c) 2000-2002
  3. // Joerg Walter, Mathias Koch
  4. //
  5. // Distributed under the Boost Software License, Version 1.0. (See
  6. // accompanying file LICENSE_1_0.txt or copy at
  7. // http://www.boost.org/LICENSE_1_0.txt)
  8. //
  9. // The authors gratefully acknowledge the support of
  10. // GeNeSys mbH & Co. KG in producing this work.
  11. //
  12. #ifndef BOOST_UBLAS_STORAGE_H
  13. #define BOOST_UBLAS_STORAGE_H
  14. #include <algorithm>
  15. #ifdef BOOST_UBLAS_SHALLOW_ARRAY_ADAPTOR
  16. #include <boost/shared_array.hpp>
  17. #endif
  18. #include <boost/serialization/array.hpp>
  19. #include <boost/serialization/collection_size_type.hpp>
  20. #include <boost/serialization/nvp.hpp>
  21. #include <boost/numeric/ublas/exception.hpp>
  22. #include <boost/numeric/ublas/traits.hpp>
  23. #include <boost/numeric/ublas/detail/iterator.hpp>
  24. namespace boost { namespace numeric { namespace ublas {
  25. // Base class for Storage Arrays - see the Barton Nackman trick
  26. template<class E>
  27. class storage_array:
  28. private nonassignable {
  29. };
  30. // Unbounded array - with allocator
  31. template<class T, class ALLOC>
  32. class unbounded_array:
  33. public storage_array<unbounded_array<T, ALLOC> > {
  34. typedef unbounded_array<T, ALLOC> self_type;
  35. public:
  36. typedef ALLOC allocator_type;
  37. typedef typename ALLOC::size_type size_type;
  38. typedef typename ALLOC::difference_type difference_type;
  39. typedef T value_type;
  40. typedef const T &const_reference;
  41. typedef T &reference;
  42. typedef const T *const_pointer;
  43. typedef T *pointer;
  44. typedef const_pointer const_iterator;
  45. typedef pointer iterator;
  46. // Construction and destruction
  47. explicit BOOST_UBLAS_INLINE
  48. unbounded_array (const ALLOC &a = ALLOC()):
  49. alloc_ (a), size_ (0) {
  50. data_ = 0;
  51. }
  52. explicit BOOST_UBLAS_INLINE
  53. unbounded_array (size_type size, const ALLOC &a = ALLOC()):
  54. alloc_(a), size_ (size) {
  55. if (size_) {
  56. data_ = alloc_.allocate (size_);
  57. //Disabled warning C4127 because the conditional expression is constant
  58. #ifdef _MSC_VER
  59. #pragma warning(push)
  60. #pragma warning(disable: 4127)
  61. #endif
  62. if (! detail::has_trivial_constructor<T>::value) {
  63. #ifdef _MSC_VER
  64. #pragma warning(pop)
  65. #endif
  66. for (pointer d = data_; d != data_ + size_; ++d)
  67. alloc_.construct(d, value_type());
  68. }
  69. }
  70. else
  71. data_ = 0;
  72. }
  73. // No value initialised, but still be default constructed
  74. BOOST_UBLAS_INLINE
  75. unbounded_array (size_type size, const value_type &init, const ALLOC &a = ALLOC()):
  76. alloc_ (a), size_ (size) {
  77. if (size_) {
  78. data_ = alloc_.allocate (size_);
  79. std::uninitialized_fill (begin(), end(), init);
  80. }
  81. else
  82. data_ = 0;
  83. }
  84. BOOST_UBLAS_INLINE
  85. unbounded_array (const unbounded_array &c):
  86. storage_array<unbounded_array<T, ALLOC> >(),
  87. alloc_ (c.alloc_), size_ (c.size_) {
  88. if (size_) {
  89. data_ = alloc_.allocate (size_);
  90. std::uninitialized_copy (c.begin(), c.end(), begin());
  91. }
  92. else
  93. data_ = 0;
  94. }
  95. #ifdef BOOST_UBLAS_CPP_GE_2011
  96. BOOST_UBLAS_INLINE
  97. unbounded_array (unbounded_array &&c) :
  98. storage_array<unbounded_array<T, ALLOC> >(),
  99. alloc_ (std::move(c.alloc_)), size_ (c.size_), data_(c.data_)
  100. {
  101. c.size_ = 0;
  102. c.data_ = nullptr;
  103. }
  104. #endif
  105. BOOST_UBLAS_INLINE
  106. ~unbounded_array () {
  107. if (size_) {
  108. //Disabled warning C4127 because the conditional expression is constant
  109. #ifdef _MSC_VER
  110. #pragma warning(push)
  111. #pragma warning(disable: 4127)
  112. #endif
  113. if (! detail::has_trivial_destructor<T>::value) {
  114. #ifdef _MSC_VER
  115. #pragma warning(pop)
  116. #endif
  117. // std::_Destroy (begin(), end(), alloc_);
  118. const iterator i_end = end();
  119. for (iterator i = begin (); i != i_end; ++i) {
  120. iterator_destroy (i);
  121. }
  122. }
  123. alloc_.deallocate (data_, size_);
  124. }
  125. }
  126. // Resizing
  127. private:
  128. BOOST_UBLAS_INLINE
  129. void resize_internal (const size_type size, const value_type init, const bool preserve) {
  130. if (size != size_) {
  131. pointer p_data = data_;
  132. if (size) {
  133. data_ = alloc_.allocate (size);
  134. if (preserve) {
  135. pointer si = p_data;
  136. pointer di = data_;
  137. if (size < size_) {
  138. for (; di != data_ + size; ++di) {
  139. alloc_.construct (di, *si);
  140. ++si;
  141. }
  142. }
  143. else {
  144. for (; si != p_data + size_; ++si) {
  145. alloc_.construct (di, *si);
  146. ++di;
  147. }
  148. for (; di != data_ + size; ++di) {
  149. alloc_.construct (di, init);
  150. }
  151. }
  152. }
  153. else {
  154. //Disabled warning C4127 because the conditional expression is constant
  155. #ifdef _MSC_VER
  156. #pragma warning(push)
  157. #pragma warning(disable: 4127)
  158. #endif
  159. if (! detail::has_trivial_constructor<T>::value) {
  160. #ifdef _MSC_VER
  161. #pragma warning(pop)
  162. #endif
  163. for (pointer di = data_; di != data_ + size; ++di)
  164. alloc_.construct (di, value_type());
  165. }
  166. }
  167. }
  168. if (size_) {
  169. //Disabled warning C4127 because the conditional expression is constant
  170. #ifdef _MSC_VER
  171. #pragma warning(push)
  172. #pragma warning(disable: 4127)
  173. #endif
  174. if (! detail::has_trivial_destructor<T>::value) {
  175. #ifdef _MSC_VER
  176. #pragma warning(pop)
  177. #endif
  178. for (pointer si = p_data; si != p_data + size_; ++si)
  179. alloc_.destroy (si);
  180. }
  181. alloc_.deallocate (p_data, size_);
  182. }
  183. if (!size)
  184. data_ = 0;
  185. size_ = size;
  186. }
  187. }
  188. public:
  189. BOOST_UBLAS_INLINE
  190. void resize (size_type size) {
  191. resize_internal (size, value_type (), false);
  192. }
  193. BOOST_UBLAS_INLINE
  194. void resize (size_type size, value_type init) {
  195. resize_internal (size, init, true);
  196. }
  197. // Random Access Container
  198. BOOST_UBLAS_INLINE
  199. size_type max_size () const {
  200. return ALLOC ().max_size();
  201. }
  202. BOOST_UBLAS_INLINE
  203. bool empty () const {
  204. return size_ == 0;
  205. }
  206. BOOST_UBLAS_INLINE
  207. size_type size () const {
  208. return size_;
  209. }
  210. // Element access
  211. BOOST_UBLAS_INLINE
  212. const_reference operator [] (size_type i) const {
  213. BOOST_UBLAS_CHECK (i < size_, bad_index ());
  214. return data_ [i];
  215. }
  216. BOOST_UBLAS_INLINE
  217. reference operator [] (size_type i) {
  218. BOOST_UBLAS_CHECK (i < size_, bad_index ());
  219. return data_ [i];
  220. }
  221. // Assignment
  222. BOOST_UBLAS_INLINE
  223. unbounded_array &operator = (const unbounded_array &a) {
  224. if (this != &a) {
  225. resize (a.size_);
  226. std::copy (a.data_, a.data_ + a.size_, data_);
  227. }
  228. return *this;
  229. }
  230. BOOST_UBLAS_INLINE
  231. unbounded_array &assign_temporary (unbounded_array &a) {
  232. swap (a);
  233. return *this;
  234. }
  235. // Swapping
  236. BOOST_UBLAS_INLINE
  237. void swap (unbounded_array &a) {
  238. if (this != &a) {
  239. std::swap (size_, a.size_);
  240. std::swap (data_, a.data_);
  241. }
  242. }
  243. BOOST_UBLAS_INLINE
  244. friend void swap (unbounded_array &a1, unbounded_array &a2) {
  245. a1.swap (a2);
  246. }
  247. BOOST_UBLAS_INLINE
  248. const_iterator begin () const {
  249. return data_;
  250. }
  251. BOOST_UBLAS_INLINE
  252. const_iterator cbegin () const {
  253. return begin ();
  254. }
  255. BOOST_UBLAS_INLINE
  256. const_iterator end () const {
  257. return data_ + size_;
  258. }
  259. BOOST_UBLAS_INLINE
  260. const_iterator cend () const {
  261. return end ();
  262. }
  263. BOOST_UBLAS_INLINE
  264. iterator begin () {
  265. return data_;
  266. }
  267. BOOST_UBLAS_INLINE
  268. iterator end () {
  269. return data_ + size_;
  270. }
  271. // Reverse iterators
  272. typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
  273. typedef std::reverse_iterator<iterator> reverse_iterator;
  274. BOOST_UBLAS_INLINE
  275. const_reverse_iterator rbegin () const {
  276. return const_reverse_iterator (end ());
  277. }
  278. BOOST_UBLAS_INLINE
  279. const_reverse_iterator crbegin () const {
  280. return rbegin ();
  281. }
  282. BOOST_UBLAS_INLINE
  283. const_reverse_iterator rend () const {
  284. return const_reverse_iterator (begin ());
  285. }
  286. BOOST_UBLAS_INLINE
  287. const_reverse_iterator crend () const {
  288. return rend ();
  289. }
  290. BOOST_UBLAS_INLINE
  291. reverse_iterator rbegin () {
  292. return reverse_iterator (end ());
  293. }
  294. BOOST_UBLAS_INLINE
  295. reverse_iterator rend () {
  296. return reverse_iterator (begin ());
  297. }
  298. // Allocator
  299. allocator_type get_allocator () {
  300. return alloc_;
  301. }
  302. private:
  303. friend class boost::serialization::access;
  304. // Serialization
  305. template<class Archive>
  306. void serialize(Archive & ar, const unsigned int /*version*/)
  307. {
  308. serialization::collection_size_type s(size_);
  309. ar & serialization::make_nvp("size",s);
  310. if ( Archive::is_loading::value ) {
  311. resize(s);
  312. }
  313. ar & serialization::make_array(data_, s);
  314. }
  315. private:
  316. // Handle explict destroy on a (possibly indexed) iterator
  317. BOOST_UBLAS_INLINE
  318. static void iterator_destroy (iterator &i) {
  319. (void)(i);
  320. (&(*i)) -> ~value_type ();
  321. }
  322. ALLOC alloc_;
  323. size_type size_;
  324. pointer data_;
  325. };
  326. // Bounded array - with allocator for size_type and difference_type
  327. template<class T, std::size_t N, class ALLOC>
  328. class bounded_array:
  329. public storage_array<bounded_array<T, N, ALLOC> > {
  330. typedef bounded_array<T, N, ALLOC> self_type;
  331. public:
  332. // No allocator_type as ALLOC is not used for allocation
  333. typedef typename ALLOC::size_type size_type;
  334. typedef typename ALLOC::difference_type difference_type;
  335. typedef T value_type;
  336. typedef const T &const_reference;
  337. typedef T &reference;
  338. typedef const T *const_pointer;
  339. typedef T *pointer;
  340. typedef const_pointer const_iterator;
  341. typedef pointer iterator;
  342. // Construction and destruction
  343. BOOST_UBLAS_INLINE
  344. bounded_array ():
  345. size_ (0) /*, data_ ()*/ { // size 0 - use bounded_vector to default construct with size N
  346. }
  347. explicit BOOST_UBLAS_INLINE
  348. bounded_array (size_type size):
  349. size_ (size) /*, data_ ()*/ {
  350. BOOST_UBLAS_CHECK (size_ <= N, bad_size ());
  351. // data_ (an array) elements are already default constructed
  352. }
  353. BOOST_UBLAS_INLINE
  354. bounded_array (size_type size, const value_type &init):
  355. size_ (size) /*, data_ ()*/ {
  356. BOOST_UBLAS_CHECK (size_ <= N, bad_size ());
  357. // ISSUE elements should be value constructed here, but we must fill instead as already default constructed
  358. std::fill (begin(), end(), init) ;
  359. }
  360. BOOST_UBLAS_INLINE
  361. bounded_array (const bounded_array &c):
  362. size_ (c.size_) {
  363. // ISSUE elements should be copy constructed here, but we must copy instead as already default constructed
  364. std::copy (c.begin(), c.end(), begin());
  365. }
  366. // Resizing
  367. BOOST_UBLAS_INLINE
  368. void resize (size_type size) {
  369. BOOST_UBLAS_CHECK (size <= N, bad_size ());
  370. size_ = size;
  371. }
  372. BOOST_UBLAS_INLINE
  373. void resize (size_type size, value_type init) {
  374. BOOST_UBLAS_CHECK (size <= N, bad_size ());
  375. if (size > size_)
  376. std::fill (data_ + size_, data_ + size, init);
  377. size_ = size;
  378. }
  379. // Random Access Container
  380. BOOST_UBLAS_INLINE
  381. size_type max_size () const {
  382. return N;
  383. }
  384. BOOST_UBLAS_INLINE
  385. bool empty () const {
  386. return size_ == 0;
  387. }
  388. BOOST_UBLAS_INLINE
  389. size_type size () const {
  390. return size_;
  391. }
  392. // Element access
  393. BOOST_UBLAS_INLINE
  394. const_reference operator [] (size_type i) const {
  395. BOOST_UBLAS_CHECK (i < size_, bad_index ());
  396. return data_ [i];
  397. }
  398. BOOST_UBLAS_INLINE
  399. reference operator [] (size_type i) {
  400. BOOST_UBLAS_CHECK (i < size_, bad_index ());
  401. return data_ [i];
  402. }
  403. // Assignment
  404. BOOST_UBLAS_INLINE
  405. bounded_array &operator = (const bounded_array &a) {
  406. if (this != &a) {
  407. resize (a.size_);
  408. std::copy (a.data_, a.data_ + a.size_, data_);
  409. }
  410. return *this;
  411. }
  412. BOOST_UBLAS_INLINE
  413. bounded_array &assign_temporary (bounded_array &a) {
  414. *this = a;
  415. return *this;
  416. }
  417. // Swapping
  418. BOOST_UBLAS_INLINE
  419. void swap (bounded_array &a) {
  420. if (this != &a) {
  421. std::swap (size_, a.size_);
  422. std::swap_ranges (data_, data_ + (std::max) (size_, a.size_), a.data_);
  423. }
  424. }
  425. BOOST_UBLAS_INLINE
  426. friend void swap (bounded_array &a1, bounded_array &a2) {
  427. a1.swap (a2);
  428. }
  429. BOOST_UBLAS_INLINE
  430. const_iterator begin () const {
  431. return data_;
  432. }
  433. BOOST_UBLAS_INLINE
  434. const_iterator cbegin () const {
  435. return begin ();
  436. }
  437. BOOST_UBLAS_INLINE
  438. const_iterator end () const {
  439. return data_ + size_;
  440. }
  441. BOOST_UBLAS_INLINE
  442. const_iterator cend () const {
  443. return end ();
  444. }
  445. BOOST_UBLAS_INLINE
  446. iterator begin () {
  447. return data_;
  448. }
  449. BOOST_UBLAS_INLINE
  450. iterator end () {
  451. return data_ + size_;
  452. }
  453. // Reverse iterators
  454. typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
  455. typedef std::reverse_iterator<iterator> reverse_iterator;
  456. BOOST_UBLAS_INLINE
  457. const_reverse_iterator rbegin () const {
  458. return const_reverse_iterator (end ());
  459. }
  460. BOOST_UBLAS_INLINE
  461. const_reverse_iterator crbegin () const {
  462. return rbegin ();
  463. }
  464. BOOST_UBLAS_INLINE
  465. const_reverse_iterator rend () const {
  466. return const_reverse_iterator (begin ());
  467. }
  468. BOOST_UBLAS_INLINE
  469. const_reverse_iterator crend () const {
  470. return rend ();
  471. }
  472. BOOST_UBLAS_INLINE
  473. reverse_iterator rbegin () {
  474. return reverse_iterator (end ());
  475. }
  476. BOOST_UBLAS_INLINE
  477. reverse_iterator rend () {
  478. return reverse_iterator (begin ());
  479. }
  480. private:
  481. // Serialization
  482. friend class boost::serialization::access;
  483. template<class Archive>
  484. void serialize(Archive & ar, const unsigned int /*version*/)
  485. {
  486. serialization::collection_size_type s(size_);
  487. ar & serialization::make_nvp("size", s);
  488. if ( Archive::is_loading::value ) {
  489. if (s > N) bad_size("too large size in bounded_array::load()\n").raise();
  490. resize(s);
  491. }
  492. ar & serialization::make_array(data_, s);
  493. }
  494. private:
  495. size_type size_;
  496. // MSVC does not like arrays of size 0 in base classes. Hence, this conditionally changes the size to 1
  497. #ifdef _MSC_VER
  498. BOOST_UBLAS_BOUNDED_ARRAY_ALIGN value_type data_ [(N>0)?N:1];
  499. #else
  500. BOOST_UBLAS_BOUNDED_ARRAY_ALIGN value_type data_ [N];
  501. #endif
  502. };
  503. // Array adaptor with normal deep copy semantics of elements
  504. template<class T>
  505. class array_adaptor:
  506. public storage_array<array_adaptor<T> > {
  507. typedef array_adaptor<T> self_type;
  508. public:
  509. typedef std::size_t size_type;
  510. typedef std::ptrdiff_t difference_type;
  511. typedef T value_type;
  512. typedef const T &const_reference;
  513. typedef T &reference;
  514. typedef const T *const_pointer;
  515. typedef T *pointer;
  516. // Construction and destruction
  517. BOOST_UBLAS_INLINE
  518. array_adaptor ():
  519. size_ (0), own_ (true), data_ (new value_type [0]) {
  520. }
  521. explicit BOOST_UBLAS_INLINE
  522. array_adaptor (size_type size):
  523. size_ (size), own_ (true), data_ (new value_type [size]) {
  524. }
  525. BOOST_UBLAS_INLINE
  526. array_adaptor (size_type size, const value_type &init):
  527. size_ (size), own_ (true), data_ (new value_type [size]) {
  528. std::fill (data_, data_ + size_, init);
  529. }
  530. BOOST_UBLAS_INLINE
  531. array_adaptor (size_type size, pointer data):
  532. size_ (size), own_ (false), data_ (data) {}
  533. template <size_t N>
  534. BOOST_UBLAS_INLINE array_adaptor (T (&data)[N]):
  535. size_ (N), own_ (false), data_ (data) {}
  536. BOOST_UBLAS_INLINE
  537. array_adaptor (const array_adaptor &a):
  538. storage_array<self_type> (),
  539. size_ (a.size_), own_ (true), data_ (new value_type [a.size_]) {
  540. *this = a;
  541. }
  542. BOOST_UBLAS_INLINE
  543. ~array_adaptor () {
  544. if (own_) {
  545. delete [] data_;
  546. }
  547. }
  548. // Resizing
  549. private:
  550. BOOST_UBLAS_INLINE
  551. void resize_internal (size_type size, value_type init, bool preserve = true) {
  552. if (size != size_) {
  553. pointer data = new value_type [size];
  554. if (preserve) {
  555. std::copy (data_, data_ + (std::min) (size, size_), data);
  556. std::fill (data + (std::min) (size, size_), data + size, init);
  557. }
  558. if (own_)
  559. delete [] data_;
  560. size_ = size;
  561. own_ = true;
  562. data_ = data;
  563. }
  564. }
  565. BOOST_UBLAS_INLINE
  566. void resize_internal (size_type size, pointer data, value_type init, bool preserve = true) {
  567. if (data != data_) {
  568. if (preserve) {
  569. std::copy (data_, data_ + (std::min) (size, size_), data);
  570. std::fill (data + (std::min) (size, size_), data + size, init);
  571. }
  572. if (own_)
  573. delete [] data_;
  574. own_ = false;
  575. data_ = data;
  576. }
  577. else {
  578. std::fill (data + (std::min) (size, size_), data + size, init);
  579. }
  580. size_ = size;
  581. }
  582. public:
  583. BOOST_UBLAS_INLINE
  584. void resize (size_type size) {
  585. resize_internal (size, value_type (), false);
  586. }
  587. BOOST_UBLAS_INLINE
  588. void resize (size_type size, value_type init) {
  589. resize_internal (size, init, true);
  590. }
  591. BOOST_UBLAS_INLINE
  592. void resize (size_type size, pointer data) {
  593. resize_internal (size, data, value_type (), false);
  594. }
  595. BOOST_UBLAS_INLINE
  596. void resize (size_type size, pointer data, value_type init) {
  597. resize_internal (size, data, init, true);
  598. }
  599. template <size_t N>
  600. BOOST_UBLAS_INLINE void resize (T (&data)[N]) {
  601. resize_internal (N, data, value_type (), false);
  602. }
  603. template <size_t N>
  604. BOOST_UBLAS_INLINE void resize (T (&data)[N], value_type init) {
  605. resize_internal (N, data, init, true);
  606. }
  607. BOOST_UBLAS_INLINE
  608. size_type size () const {
  609. return size_;
  610. }
  611. // Element access
  612. BOOST_UBLAS_INLINE
  613. const_reference operator [] (size_type i) const {
  614. BOOST_UBLAS_CHECK (i < size_, bad_index ());
  615. return data_ [i];
  616. }
  617. BOOST_UBLAS_INLINE
  618. reference operator [] (size_type i) {
  619. BOOST_UBLAS_CHECK (i < size_, bad_index ());
  620. return data_ [i];
  621. }
  622. // Assignment
  623. BOOST_UBLAS_INLINE
  624. array_adaptor &operator = (const array_adaptor &a) {
  625. if (this != &a) {
  626. resize (a.size_);
  627. std::copy (a.data_, a.data_ + a.size_, data_);
  628. }
  629. return *this;
  630. }
  631. BOOST_UBLAS_INLINE
  632. array_adaptor &assign_temporary (array_adaptor &a) {
  633. if (own_ && a.own_)
  634. swap (a);
  635. else
  636. *this = a;
  637. return *this;
  638. }
  639. // Swapping
  640. BOOST_UBLAS_INLINE
  641. void swap (array_adaptor &a) {
  642. if (this != &a) {
  643. std::swap (size_, a.size_);
  644. std::swap (own_, a.own_);
  645. std::swap (data_, a.data_);
  646. }
  647. }
  648. BOOST_UBLAS_INLINE
  649. friend void swap (array_adaptor &a1, array_adaptor &a2) {
  650. a1.swap (a2);
  651. }
  652. // Iterators simply are pointers.
  653. typedef const_pointer const_iterator;
  654. BOOST_UBLAS_INLINE
  655. const_iterator begin () const {
  656. return data_;
  657. }
  658. BOOST_UBLAS_INLINE
  659. const_iterator cbegin () const {
  660. return begin ();
  661. }
  662. BOOST_UBLAS_INLINE
  663. const_iterator end () const {
  664. return data_ + size_;
  665. }
  666. BOOST_UBLAS_INLINE
  667. const_iterator cend () const {
  668. return end ();
  669. }
  670. typedef pointer iterator;
  671. BOOST_UBLAS_INLINE
  672. iterator begin () {
  673. return data_;
  674. }
  675. BOOST_UBLAS_INLINE
  676. iterator end () {
  677. return data_ + size_;
  678. }
  679. // Reverse iterators
  680. typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
  681. typedef std::reverse_iterator<iterator> reverse_iterator;
  682. BOOST_UBLAS_INLINE
  683. const_reverse_iterator rbegin () const {
  684. return const_reverse_iterator (end ());
  685. }
  686. BOOST_UBLAS_INLINE
  687. const_reverse_iterator crbegin () const {
  688. return rbegin ();
  689. }
  690. BOOST_UBLAS_INLINE
  691. const_reverse_iterator rend () const {
  692. return const_reverse_iterator (begin ());
  693. }
  694. BOOST_UBLAS_INLINE
  695. const_reverse_iterator crend () const {
  696. return rend ();
  697. }
  698. BOOST_UBLAS_INLINE
  699. reverse_iterator rbegin () {
  700. return reverse_iterator (end ());
  701. }
  702. BOOST_UBLAS_INLINE
  703. reverse_iterator rend () {
  704. return reverse_iterator (begin ());
  705. }
  706. private:
  707. size_type size_;
  708. bool own_;
  709. pointer data_;
  710. };
  711. #ifdef BOOST_UBLAS_SHALLOW_ARRAY_ADAPTOR
  712. // Array adaptor with shallow (reference) copy semantics of elements.
  713. // shared_array is used to maintain reference counts.
  714. // This class breaks the normal copy semantics for a storage container and is very dangerous!
  715. template<class T>
  716. class shallow_array_adaptor:
  717. public storage_array<shallow_array_adaptor<T> > {
  718. typedef shallow_array_adaptor<T> self_type;
  719. template<class TT>
  720. struct leaker {
  721. typedef void result_type;
  722. typedef TT *argument_type;
  723. BOOST_UBLAS_INLINE
  724. result_type operator () (argument_type /* x */) {}
  725. };
  726. public:
  727. typedef std::size_t size_type;
  728. typedef std::ptrdiff_t difference_type;
  729. typedef T value_type;
  730. typedef const T &const_reference;
  731. typedef T &reference;
  732. typedef const T *const_pointer;
  733. typedef T *pointer;
  734. // Construction and destruction
  735. BOOST_UBLAS_INLINE
  736. shallow_array_adaptor ():
  737. size_ (0), own_ (true), data_ (new value_type [0]) {
  738. }
  739. explicit BOOST_UBLAS_INLINE
  740. shallow_array_adaptor (size_type size):
  741. size_ (size), own_ (true), data_ (new value_type [size]) {
  742. }
  743. BOOST_UBLAS_INLINE
  744. shallow_array_adaptor (size_type size, const value_type &init):
  745. size_ (size), own_ (true), data_ (new value_type [size]) {
  746. std::fill (data_.get (), data_.get () + size_, init);
  747. }
  748. BOOST_UBLAS_INLINE
  749. shallow_array_adaptor (size_type size, pointer data):
  750. size_ (size), own_ (false), data_ (data, leaker<value_type> ()) {}
  751. template <size_t N>
  752. BOOST_UBLAS_INLINE
  753. shallow_array_adaptor (T (&data)[N]):
  754. size_ (N), own_ (false), data_ (data, leaker<value_type> ()) {}
  755. BOOST_UBLAS_INLINE
  756. shallow_array_adaptor (const shallow_array_adaptor &a):
  757. storage_array<self_type> (),
  758. size_ (a.size_), own_ (a.own_), data_ (a.data_) {}
  759. BOOST_UBLAS_INLINE
  760. ~shallow_array_adaptor () {
  761. }
  762. // Resizing
  763. private:
  764. BOOST_UBLAS_INLINE
  765. void resize_internal (size_type size, value_type init, bool preserve = true) {
  766. if (size != size_) {
  767. shared_array<value_type> data (new value_type [size]);
  768. if (preserve) {
  769. std::copy (data_.get (), data_.get () + (std::min) (size, size_), data.get ());
  770. std::fill (data.get () + (std::min) (size, size_), data.get () + size, init);
  771. }
  772. size_ = size;
  773. own_ = true;
  774. data_ = data;
  775. }
  776. }
  777. BOOST_UBLAS_INLINE
  778. void resize_internal (size_type size, pointer data, value_type init, bool preserve = true) {
  779. if (preserve) {
  780. std::copy (data_.get (), data_.get () + (std::min) (size, size_), data);
  781. std::fill (data + (std::min) (size, size_), data + size, init);
  782. }
  783. size_ = size;
  784. own_ = false;
  785. data_.reset(data, leaker<value_type> ());
  786. }
  787. public:
  788. BOOST_UBLAS_INLINE
  789. void resize (size_type size) {
  790. resize_internal (size, value_type (), false);
  791. }
  792. BOOST_UBLAS_INLINE
  793. void resize (size_type size, value_type init) {
  794. resize_internal (size, init, true);
  795. }
  796. BOOST_UBLAS_INLINE
  797. void resize (size_type size, pointer data) {
  798. resize_internal (size, data, value_type (), false);
  799. }
  800. BOOST_UBLAS_INLINE
  801. void resize (size_type size, pointer data, value_type init) {
  802. resize_internal (size, data, init, true);
  803. }
  804. template <size_t N>
  805. BOOST_UBLAS_INLINE
  806. void resize (T (&data)[N]) {
  807. resize_internal (N, data, value_type (), false);
  808. }
  809. template <size_t N>
  810. BOOST_UBLAS_INLINE
  811. void resize (T (&data)[N], value_type init) {
  812. resize_internal (N, data, init, true);
  813. }
  814. BOOST_UBLAS_INLINE
  815. size_type size () const {
  816. return size_;
  817. }
  818. // Element access
  819. BOOST_UBLAS_INLINE
  820. const_reference operator [] (size_type i) const {
  821. BOOST_UBLAS_CHECK (i < size_, bad_index ());
  822. return data_ [i];
  823. }
  824. BOOST_UBLAS_INLINE
  825. reference operator [] (size_type i) {
  826. BOOST_UBLAS_CHECK (i < size_, bad_index ());
  827. return data_ [i];
  828. }
  829. // Assignment
  830. BOOST_UBLAS_INLINE
  831. shallow_array_adaptor &operator = (const shallow_array_adaptor &a) {
  832. if (this != &a) {
  833. resize (a.size_);
  834. std::copy (a.data_.get (), a.data_.get () + a.size_, data_.get ());
  835. }
  836. return *this;
  837. }
  838. BOOST_UBLAS_INLINE
  839. shallow_array_adaptor &assign_temporary (shallow_array_adaptor &a) {
  840. if (own_ && a.own_)
  841. swap (a);
  842. else
  843. *this = a;
  844. return *this;
  845. }
  846. // Swapping
  847. BOOST_UBLAS_INLINE
  848. void swap (shallow_array_adaptor &a) {
  849. if (this != &a) {
  850. std::swap (size_, a.size_);
  851. std::swap (own_, a.own_);
  852. std::swap (data_, a.data_);
  853. }
  854. }
  855. BOOST_UBLAS_INLINE
  856. friend void swap (shallow_array_adaptor &a1, shallow_array_adaptor &a2) {
  857. a1.swap (a2);
  858. }
  859. // Iterators simply are pointers.
  860. typedef const_pointer const_iterator;
  861. BOOST_UBLAS_INLINE
  862. const_iterator begin () const {
  863. return data_.get ();
  864. }
  865. BOOST_UBLAS_INLINE
  866. const_iterator cbegin () const {
  867. return begin ();
  868. }
  869. BOOST_UBLAS_INLINE
  870. const_iterator end () const {
  871. return data_.get () + size_;
  872. }
  873. BOOST_UBLAS_INLINE
  874. const_iterator cend () const {
  875. return end ();
  876. }
  877. typedef pointer iterator;
  878. BOOST_UBLAS_INLINE
  879. iterator begin () {
  880. return data_.get ();
  881. }
  882. BOOST_UBLAS_INLINE
  883. iterator end () {
  884. return data_.get () + size_;
  885. }
  886. // Reverse iterators
  887. typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
  888. typedef std::reverse_iterator<iterator> reverse_iterator;
  889. BOOST_UBLAS_INLINE
  890. const_reverse_iterator rbegin () const {
  891. return const_reverse_iterator (end ());
  892. }
  893. BOOST_UBLAS_INLINE
  894. const_reverse_iterator crbegin () const {
  895. return rbegin ();
  896. }
  897. BOOST_UBLAS_INLINE
  898. const_reverse_iterator rend () const {
  899. return const_reverse_iterator (begin ());
  900. }
  901. BOOST_UBLAS_INLINE
  902. const_reverse_iterator crend () const {
  903. return rend ();
  904. }
  905. BOOST_UBLAS_INLINE
  906. reverse_iterator rbegin () {
  907. return reverse_iterator (end ());
  908. }
  909. BOOST_UBLAS_INLINE
  910. reverse_iterator rend () {
  911. return reverse_iterator (begin ());
  912. }
  913. private:
  914. size_type size_;
  915. bool own_;
  916. shared_array<value_type> data_;
  917. };
  918. #endif
  919. // Range class
  920. template <class Z, class D>
  921. class basic_range {
  922. typedef basic_range<Z, D> self_type;
  923. public:
  924. typedef Z size_type;
  925. typedef D difference_type;
  926. typedef size_type value_type;
  927. typedef value_type const_reference;
  928. typedef const_reference reference;
  929. typedef const value_type *const_pointer;
  930. typedef value_type *pointer;
  931. // Construction and destruction
  932. BOOST_UBLAS_INLINE
  933. basic_range ():
  934. start_ (0), size_ (0) {}
  935. BOOST_UBLAS_INLINE
  936. basic_range (size_type start, size_type stop):
  937. start_ (start), size_ (stop - start) {
  938. BOOST_UBLAS_CHECK (start_ <= stop, bad_index ());
  939. }
  940. BOOST_UBLAS_INLINE
  941. size_type start () const {
  942. return start_;
  943. }
  944. BOOST_UBLAS_INLINE
  945. size_type size () const {
  946. return size_;
  947. }
  948. // Random Access Container
  949. BOOST_UBLAS_INLINE
  950. size_type max_size () const {
  951. return size_;
  952. }
  953. BOOST_UBLAS_INLINE
  954. bool empty () const {
  955. return size_ == 0;
  956. }
  957. // Element access
  958. BOOST_UBLAS_INLINE
  959. const_reference operator () (size_type i) const {
  960. BOOST_UBLAS_CHECK (i < size_, bad_index ());
  961. return start_ + i;
  962. }
  963. // Composition
  964. BOOST_UBLAS_INLINE
  965. basic_range compose (const basic_range &r) const {
  966. return basic_range (start_ + r.start_, start_ + r.start_ + r.size_);
  967. }
  968. // Comparison
  969. BOOST_UBLAS_INLINE
  970. bool operator == (const basic_range &r) const {
  971. return start_ == r.start_ && size_ == r.size_;
  972. }
  973. BOOST_UBLAS_INLINE
  974. bool operator != (const basic_range &r) const {
  975. return ! (*this == r);
  976. }
  977. // Iterator types
  978. private:
  979. // Use and index
  980. typedef size_type const_subiterator_type;
  981. public:
  982. #ifdef BOOST_UBLAS_USE_INDEXED_ITERATOR
  983. typedef indexed_const_iterator<self_type, std::random_access_iterator_tag> const_iterator;
  984. #else
  985. class const_iterator:
  986. public container_const_reference<basic_range>,
  987. public random_access_iterator_base<std::random_access_iterator_tag,
  988. const_iterator, value_type> {
  989. public:
  990. typedef typename basic_range::value_type value_type;
  991. typedef typename basic_range::difference_type difference_type;
  992. typedef typename basic_range::const_reference reference;
  993. typedef typename basic_range::const_pointer pointer;
  994. // Construction and destruction
  995. BOOST_UBLAS_INLINE
  996. const_iterator ():
  997. container_const_reference<basic_range> (), it_ () {}
  998. BOOST_UBLAS_INLINE
  999. const_iterator (const basic_range &r, const const_subiterator_type &it):
  1000. container_const_reference<basic_range> (r), it_ (it) {}
  1001. // Arithmetic
  1002. BOOST_UBLAS_INLINE
  1003. const_iterator &operator ++ () {
  1004. ++ it_;
  1005. return *this;
  1006. }
  1007. BOOST_UBLAS_INLINE
  1008. const_iterator &operator -- () {
  1009. BOOST_UBLAS_CHECK (it_ > 0, bad_index ());
  1010. -- it_;
  1011. return *this;
  1012. }
  1013. BOOST_UBLAS_INLINE
  1014. const_iterator &operator += (difference_type n) {
  1015. BOOST_UBLAS_CHECK (n >= 0 || it_ >= size_type(-n), bad_index ());
  1016. it_ += n;
  1017. return *this;
  1018. }
  1019. BOOST_UBLAS_INLINE
  1020. const_iterator &operator -= (difference_type n) {
  1021. BOOST_UBLAS_CHECK (n <= 0 || it_ >= size_type(n), bad_index ());
  1022. it_ -= n;
  1023. return *this;
  1024. }
  1025. BOOST_UBLAS_INLINE
  1026. difference_type operator - (const const_iterator &it) const {
  1027. return it_ - it.it_;
  1028. }
  1029. // Dereference
  1030. BOOST_UBLAS_INLINE
  1031. const_reference operator * () const {
  1032. BOOST_UBLAS_CHECK ((*this) ().start () <= it_, bad_index ());
  1033. BOOST_UBLAS_CHECK (it_ < (*this) ().start () + (*this) ().size (), bad_index ());
  1034. return it_;
  1035. }
  1036. BOOST_UBLAS_INLINE
  1037. const_reference operator [] (difference_type n) const {
  1038. return *(*this + n);
  1039. }
  1040. // Index
  1041. BOOST_UBLAS_INLINE
  1042. size_type index () const {
  1043. BOOST_UBLAS_CHECK ((*this) ().start () <= it_, bad_index ());
  1044. BOOST_UBLAS_CHECK (it_ < (*this) ().start () + (*this) ().size (), bad_index ());
  1045. return it_ - (*this) ().start ();
  1046. }
  1047. // Assignment
  1048. BOOST_UBLAS_INLINE
  1049. const_iterator &operator = (const const_iterator &it) {
  1050. // Comeau recommends...
  1051. this->assign (&it ());
  1052. it_ = it.it_;
  1053. return *this;
  1054. }
  1055. // Comparison
  1056. BOOST_UBLAS_INLINE
  1057. bool operator == (const const_iterator &it) const {
  1058. BOOST_UBLAS_CHECK ((*this) () == it (), external_logic ());
  1059. return it_ == it.it_;
  1060. }
  1061. BOOST_UBLAS_INLINE
  1062. bool operator < (const const_iterator &it) const {
  1063. BOOST_UBLAS_CHECK ((*this) () == it (), external_logic ());
  1064. return it_ < it.it_;
  1065. }
  1066. private:
  1067. const_subiterator_type it_;
  1068. };
  1069. #endif
  1070. BOOST_UBLAS_INLINE
  1071. const_iterator begin () const {
  1072. return const_iterator (*this, start_);
  1073. }
  1074. BOOST_UBLAS_INLINE
  1075. const_iterator cbegin () const {
  1076. return begin ();
  1077. }
  1078. BOOST_UBLAS_INLINE
  1079. const_iterator end () const {
  1080. return const_iterator (*this, start_ + size_);
  1081. }
  1082. BOOST_UBLAS_INLINE
  1083. const_iterator cend () const {
  1084. return end ();
  1085. }
  1086. // Reverse iterator
  1087. typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
  1088. BOOST_UBLAS_INLINE
  1089. const_reverse_iterator rbegin () const {
  1090. return const_reverse_iterator (end ());
  1091. }
  1092. BOOST_UBLAS_INLINE
  1093. const_reverse_iterator crbegin () const {
  1094. return rbegin ();
  1095. }
  1096. BOOST_UBLAS_INLINE
  1097. const_reverse_iterator rend () const {
  1098. return const_reverse_iterator (begin ());
  1099. }
  1100. BOOST_UBLAS_INLINE
  1101. const_reverse_iterator crend () const {
  1102. return rend ();
  1103. }
  1104. BOOST_UBLAS_INLINE
  1105. basic_range preprocess (size_type size) const {
  1106. if (this != &all_)
  1107. return *this;
  1108. return basic_range (0, size);
  1109. }
  1110. static
  1111. BOOST_UBLAS_INLINE
  1112. const basic_range &all () {
  1113. return all_;
  1114. }
  1115. private:
  1116. size_type start_;
  1117. size_type size_;
  1118. static const basic_range all_;
  1119. };
  1120. template <class Z, class D>
  1121. const basic_range<Z,D> basic_range<Z,D>::all_ (0, size_type (-1));
  1122. // Slice class
  1123. template <class Z, class D>
  1124. class basic_slice {
  1125. typedef basic_slice<Z, D> self_type;
  1126. public:
  1127. typedef Z size_type;
  1128. typedef D difference_type;
  1129. typedef size_type value_type;
  1130. typedef value_type const_reference;
  1131. typedef const_reference reference;
  1132. typedef const value_type *const_pointer;
  1133. typedef value_type *pointer;
  1134. // Construction and destruction
  1135. BOOST_UBLAS_INLINE
  1136. basic_slice ():
  1137. start_ (0), stride_ (0), size_ (0) {}
  1138. BOOST_UBLAS_INLINE
  1139. basic_slice (size_type start, difference_type stride, size_type size):
  1140. start_ (start), stride_ (stride), size_ (size) {}
  1141. BOOST_UBLAS_INLINE
  1142. size_type start () const {
  1143. return start_;
  1144. }
  1145. BOOST_UBLAS_INLINE
  1146. difference_type stride () const {
  1147. return stride_;
  1148. }
  1149. BOOST_UBLAS_INLINE
  1150. size_type size () const {
  1151. return size_;
  1152. }
  1153. // Random Access Container
  1154. BOOST_UBLAS_INLINE
  1155. size_type max_size () const {
  1156. return size_;
  1157. }
  1158. BOOST_UBLAS_INLINE
  1159. bool empty () const {
  1160. return size_ == 0;
  1161. }
  1162. // Element access
  1163. BOOST_UBLAS_INLINE
  1164. const_reference operator () (size_type i) const {
  1165. BOOST_UBLAS_CHECK (i < size_, bad_index ());
  1166. BOOST_UBLAS_CHECK (stride_ >= 0 || start_ >= i * -stride_, bad_index ());
  1167. return start_ + i * stride_;
  1168. }
  1169. // Composition
  1170. BOOST_UBLAS_INLINE
  1171. basic_slice compose (const basic_range<size_type, difference_type> &r) const {
  1172. BOOST_UBLAS_CHECK (stride_ >=0 || start_ >= -stride_ * r.start(), bad_index ());
  1173. return basic_slice (start_ + stride_ * r.start (), stride_, r.size ());
  1174. }
  1175. BOOST_UBLAS_INLINE
  1176. basic_slice compose (const basic_slice &s) const {
  1177. BOOST_UBLAS_CHECK (stride_ >=0 || start_ >= -stride_ * s.start_, bad_index ());
  1178. return basic_slice (start_ + stride_ * s.start_, stride_ * s.stride_, s.size_);
  1179. }
  1180. // Comparison
  1181. BOOST_UBLAS_INLINE
  1182. bool operator == (const basic_slice &s) const {
  1183. return start_ == s.start_ && stride_ == s.stride_ && size_ == s.size_;
  1184. }
  1185. BOOST_UBLAS_INLINE
  1186. bool operator != (const basic_slice &s) const {
  1187. return ! (*this == s);
  1188. }
  1189. // Iterator types
  1190. private:
  1191. // Use and index
  1192. typedef size_type const_subiterator_type;
  1193. public:
  1194. #ifdef BOOST_UBLAS_USE_INDEXED_ITERATOR
  1195. typedef indexed_const_iterator<self_type, std::random_access_iterator_tag> const_iterator;
  1196. #else
  1197. class const_iterator:
  1198. public container_const_reference<basic_slice>,
  1199. public random_access_iterator_base<std::random_access_iterator_tag,
  1200. const_iterator, value_type> {
  1201. public:
  1202. typedef typename basic_slice::value_type value_type;
  1203. typedef typename basic_slice::difference_type difference_type;
  1204. typedef typename basic_slice::const_reference reference;
  1205. typedef typename basic_slice::const_pointer pointer;
  1206. // Construction and destruction
  1207. BOOST_UBLAS_INLINE
  1208. const_iterator ():
  1209. container_const_reference<basic_slice> (), it_ () {}
  1210. BOOST_UBLAS_INLINE
  1211. const_iterator (const basic_slice &s, const const_subiterator_type &it):
  1212. container_const_reference<basic_slice> (s), it_ (it) {}
  1213. // Arithmetic
  1214. BOOST_UBLAS_INLINE
  1215. const_iterator &operator ++ () {
  1216. ++it_;
  1217. return *this;
  1218. }
  1219. BOOST_UBLAS_INLINE
  1220. const_iterator &operator -- () {
  1221. BOOST_UBLAS_CHECK (it_ > 0, bad_index ());
  1222. --it_;
  1223. return *this;
  1224. }
  1225. BOOST_UBLAS_INLINE
  1226. const_iterator &operator += (difference_type n) {
  1227. BOOST_UBLAS_CHECK (n >= 0 || it_ >= size_type(-n), bad_index ());
  1228. it_ += n;
  1229. return *this;
  1230. }
  1231. BOOST_UBLAS_INLINE
  1232. const_iterator &operator -= (difference_type n) {
  1233. BOOST_UBLAS_CHECK (n <= 0 || it_ >= size_type(n), bad_index ());
  1234. it_ -= n;
  1235. return *this;
  1236. }
  1237. BOOST_UBLAS_INLINE
  1238. difference_type operator - (const const_iterator &it) const {
  1239. return it_ - it.it_;
  1240. }
  1241. // Dereference
  1242. BOOST_UBLAS_INLINE
  1243. const_reference operator * () const {
  1244. BOOST_UBLAS_CHECK (it_ < (*this) ().size (), bad_index ());
  1245. return (*this) ().start () + it_* (*this) ().stride ();
  1246. }
  1247. BOOST_UBLAS_INLINE
  1248. const_reference operator [] (difference_type n) const {
  1249. return *(*this + n);
  1250. }
  1251. // Index
  1252. BOOST_UBLAS_INLINE
  1253. size_type index () const {
  1254. BOOST_UBLAS_CHECK (it_ < (*this) ().size (), bad_index ());
  1255. return it_;
  1256. }
  1257. // Assignment
  1258. BOOST_UBLAS_INLINE
  1259. const_iterator &operator = (const const_iterator &it) {
  1260. // Comeau recommends...
  1261. this->assign (&it ());
  1262. it_ = it.it_;
  1263. return *this;
  1264. }
  1265. // Comparison
  1266. BOOST_UBLAS_INLINE
  1267. bool operator == (const const_iterator &it) const {
  1268. BOOST_UBLAS_CHECK ((*this) () == it (), external_logic ());
  1269. return it_ == it.it_;
  1270. }
  1271. BOOST_UBLAS_INLINE
  1272. bool operator < (const const_iterator &it) const {
  1273. BOOST_UBLAS_CHECK ((*this) () == it (), external_logic ());
  1274. return it_ < it.it_;
  1275. }
  1276. private:
  1277. const_subiterator_type it_;
  1278. };
  1279. #endif
  1280. BOOST_UBLAS_INLINE
  1281. const_iterator begin () const {
  1282. return const_iterator (*this, 0);
  1283. }
  1284. BOOST_UBLAS_INLINE
  1285. const_iterator cbegin () const {
  1286. return begin ();
  1287. }
  1288. BOOST_UBLAS_INLINE
  1289. const_iterator end () const {
  1290. return const_iterator (*this, size_);
  1291. }
  1292. BOOST_UBLAS_INLINE
  1293. const_iterator cend () const {
  1294. return end ();
  1295. }
  1296. // Reverse iterator
  1297. typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
  1298. BOOST_UBLAS_INLINE
  1299. const_reverse_iterator rbegin () const {
  1300. return const_reverse_iterator (end ());
  1301. }
  1302. BOOST_UBLAS_INLINE
  1303. const_reverse_iterator crbegin () const {
  1304. return rbegin ();
  1305. }
  1306. BOOST_UBLAS_INLINE
  1307. const_reverse_iterator rend () const {
  1308. return const_reverse_iterator (begin ());
  1309. }
  1310. BOOST_UBLAS_INLINE
  1311. const_reverse_iterator crend () const {
  1312. return rend ();
  1313. }
  1314. BOOST_UBLAS_INLINE
  1315. basic_slice preprocess (size_type size) const {
  1316. if (this != &all_)
  1317. return *this;
  1318. return basic_slice (0, 1, size);
  1319. }
  1320. static
  1321. BOOST_UBLAS_INLINE
  1322. const basic_slice &all () {
  1323. return all_;
  1324. }
  1325. private:
  1326. size_type start_;
  1327. difference_type stride_;
  1328. size_type size_;
  1329. static const basic_slice all_;
  1330. };
  1331. template <class Z, class D>
  1332. const basic_slice<Z,D> basic_slice<Z,D>::all_ (0, 1, size_type (-1));
  1333. // Indirect array class
  1334. template<class A>
  1335. class indirect_array {
  1336. typedef indirect_array<A> self_type;
  1337. public:
  1338. typedef A array_type;
  1339. typedef const A const_array_type;
  1340. typedef typename A::size_type size_type;
  1341. typedef typename A::difference_type difference_type;
  1342. typedef typename A::value_type value_type;
  1343. typedef typename A::const_reference const_reference;
  1344. typedef typename A::reference reference;
  1345. typedef typename A::const_pointer const_pointer;
  1346. typedef typename A::pointer pointer;
  1347. // Construction and destruction
  1348. BOOST_UBLAS_INLINE
  1349. indirect_array ():
  1350. size_ (), data_ () {}
  1351. explicit BOOST_UBLAS_INLINE
  1352. indirect_array (size_type size):
  1353. size_ (size), data_ (size) {}
  1354. BOOST_UBLAS_INLINE
  1355. indirect_array (size_type size, const array_type &data):
  1356. size_ (size), data_ (data) {}
  1357. BOOST_UBLAS_INLINE
  1358. indirect_array (pointer start, pointer stop):
  1359. size_ (stop - start), data_ (stop - start) {
  1360. std::copy (start, stop, data_.begin ());
  1361. }
  1362. BOOST_UBLAS_INLINE
  1363. size_type size () const {
  1364. return size_;
  1365. }
  1366. BOOST_UBLAS_INLINE
  1367. const_array_type data () const {
  1368. return data_;
  1369. }
  1370. BOOST_UBLAS_INLINE
  1371. array_type data () {
  1372. return data_;
  1373. }
  1374. // Random Access Container
  1375. BOOST_UBLAS_INLINE
  1376. size_type max_size () const {
  1377. return size_;
  1378. }
  1379. BOOST_UBLAS_INLINE
  1380. bool empty () const {
  1381. return data_.size () == 0;
  1382. }
  1383. // Element access
  1384. BOOST_UBLAS_INLINE
  1385. const_reference operator () (size_type i) const {
  1386. BOOST_UBLAS_CHECK (i < size_, bad_index ());
  1387. return data_ [i];
  1388. }
  1389. BOOST_UBLAS_INLINE
  1390. reference operator () (size_type i) {
  1391. BOOST_UBLAS_CHECK (i < size_, bad_index ());
  1392. return data_ [i];
  1393. }
  1394. BOOST_UBLAS_INLINE
  1395. const_reference operator [] (size_type i) const {
  1396. return (*this) (i);
  1397. }
  1398. BOOST_UBLAS_INLINE
  1399. reference operator [] (size_type i) {
  1400. return (*this) (i);
  1401. }
  1402. // Composition
  1403. BOOST_UBLAS_INLINE
  1404. indirect_array compose (const basic_range<size_type, difference_type> &r) const {
  1405. BOOST_UBLAS_CHECK (r.start () + r.size () <= size_, bad_size ());
  1406. array_type data (r.size ());
  1407. for (size_type i = 0; i < r.size (); ++ i)
  1408. data [i] = data_ [r.start () + i];
  1409. return indirect_array (r.size (), data);
  1410. }
  1411. BOOST_UBLAS_INLINE
  1412. indirect_array compose (const basic_slice<size_type, difference_type> &s) const {
  1413. BOOST_UBLAS_CHECK (s.start () + s.stride () * (s.size () - (s.size () > 0)) <= size (), bad_size ());
  1414. array_type data (s.size ());
  1415. for (size_type i = 0; i < s.size (); ++ i)
  1416. data [i] = data_ [s.start () + s.stride () * i];
  1417. return indirect_array (s.size (), data);
  1418. }
  1419. BOOST_UBLAS_INLINE
  1420. indirect_array compose (const indirect_array &ia) const {
  1421. array_type data (ia.size_);
  1422. for (size_type i = 0; i < ia.size_; ++ i) {
  1423. BOOST_UBLAS_CHECK (ia.data_ [i] <= size_, bad_size ());
  1424. data [i] = data_ [ia.data_ [i]];
  1425. }
  1426. return indirect_array (ia.size_, data);
  1427. }
  1428. // Comparison
  1429. template<class OA>
  1430. BOOST_UBLAS_INLINE
  1431. bool operator == (const indirect_array<OA> &ia) const {
  1432. if (size_ != ia.size_)
  1433. return false;
  1434. for (size_type i = 0; i < BOOST_UBLAS_SAME (size_, ia.size_); ++ i)
  1435. if (data_ [i] != ia.data_ [i])
  1436. return false;
  1437. return true;
  1438. }
  1439. template<class OA>
  1440. BOOST_UBLAS_INLINE
  1441. bool operator != (const indirect_array<OA> &ia) const {
  1442. return ! (*this == ia);
  1443. }
  1444. // Iterator types
  1445. private:
  1446. // Use a index difference
  1447. typedef difference_type const_subiterator_type;
  1448. public:
  1449. #ifdef BOOST_UBLAS_USE_INDEXED_ITERATOR
  1450. typedef indexed_const_iterator<indirect_array, std::random_access_iterator_tag> const_iterator;
  1451. #else
  1452. class const_iterator:
  1453. public container_const_reference<indirect_array>,
  1454. public random_access_iterator_base<std::random_access_iterator_tag,
  1455. const_iterator, value_type> {
  1456. public:
  1457. typedef typename indirect_array::value_type value_type;
  1458. typedef typename indirect_array::difference_type difference_type;
  1459. typedef typename indirect_array::const_reference reference;
  1460. typedef typename indirect_array::const_pointer pointer;
  1461. // Construction and destruction
  1462. BOOST_UBLAS_INLINE
  1463. const_iterator ():
  1464. container_const_reference<indirect_array> (), it_ () {}
  1465. BOOST_UBLAS_INLINE
  1466. const_iterator (const indirect_array &ia, const const_subiterator_type &it):
  1467. container_const_reference<indirect_array> (ia), it_ (it) {}
  1468. // Arithmetic
  1469. BOOST_UBLAS_INLINE
  1470. const_iterator &operator ++ () {
  1471. ++ it_;
  1472. return *this;
  1473. }
  1474. BOOST_UBLAS_INLINE
  1475. const_iterator &operator -- () {
  1476. -- it_;
  1477. return *this;
  1478. }
  1479. BOOST_UBLAS_INLINE
  1480. const_iterator &operator += (difference_type n) {
  1481. it_ += n;
  1482. return *this;
  1483. }
  1484. BOOST_UBLAS_INLINE
  1485. const_iterator &operator -= (difference_type n) {
  1486. it_ -= n;
  1487. return *this;
  1488. }
  1489. BOOST_UBLAS_INLINE
  1490. difference_type operator - (const const_iterator &it) const {
  1491. return it_ - it.it_;
  1492. }
  1493. // Dereference
  1494. BOOST_UBLAS_INLINE
  1495. const_reference operator * () const {
  1496. return (*this) () (it_);
  1497. }
  1498. BOOST_UBLAS_INLINE
  1499. const_reference operator [] (difference_type n) const {
  1500. return *(*this + n);
  1501. }
  1502. // Index
  1503. BOOST_UBLAS_INLINE
  1504. size_type index () const {
  1505. return it_;
  1506. }
  1507. // Assignment
  1508. BOOST_UBLAS_INLINE
  1509. const_iterator &operator = (const const_iterator &it) {
  1510. // Comeau recommends...
  1511. this->assign (&it ());
  1512. it_ = it.it_;
  1513. return *this;
  1514. }
  1515. // Comparison
  1516. BOOST_UBLAS_INLINE
  1517. bool operator == (const const_iterator &it) const {
  1518. BOOST_UBLAS_CHECK ((*this) () == it (), external_logic ());
  1519. return it_ == it.it_;
  1520. }
  1521. BOOST_UBLAS_INLINE
  1522. bool operator < (const const_iterator &it) const {
  1523. BOOST_UBLAS_CHECK ((*this) () == it (), external_logic ());
  1524. return it_ < it.it_;
  1525. }
  1526. private:
  1527. const_subiterator_type it_;
  1528. };
  1529. #endif
  1530. BOOST_UBLAS_INLINE
  1531. const_iterator begin () const {
  1532. return const_iterator (*this, 0);
  1533. }
  1534. BOOST_UBLAS_INLINE
  1535. const_iterator cbegin () const {
  1536. return begin ();
  1537. }
  1538. BOOST_UBLAS_INLINE
  1539. const_iterator end () const {
  1540. return const_iterator (*this, size_);
  1541. }
  1542. BOOST_UBLAS_INLINE
  1543. const_iterator cend () const {
  1544. return end ();
  1545. }
  1546. // Reverse iterator
  1547. typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
  1548. BOOST_UBLAS_INLINE
  1549. const_reverse_iterator rbegin () const {
  1550. return const_reverse_iterator (end ());
  1551. }
  1552. BOOST_UBLAS_INLINE
  1553. const_reverse_iterator crbegin () const {
  1554. return rbegin ();
  1555. }
  1556. BOOST_UBLAS_INLINE
  1557. const_reverse_iterator rend () const {
  1558. return const_reverse_iterator (begin ());
  1559. }
  1560. BOOST_UBLAS_INLINE
  1561. const_reverse_iterator crend () const {
  1562. return rend ();
  1563. }
  1564. BOOST_UBLAS_INLINE
  1565. indirect_array preprocess (size_type size) const {
  1566. if (this != &all_)
  1567. return *this;
  1568. indirect_array ia (size);
  1569. for (size_type i = 0; i < size; ++ i)
  1570. ia (i) = i;
  1571. return ia;
  1572. }
  1573. static
  1574. BOOST_UBLAS_INLINE
  1575. const indirect_array &all () {
  1576. return all_;
  1577. }
  1578. private:
  1579. size_type size_;
  1580. array_type data_;
  1581. static const indirect_array all_;
  1582. };
  1583. template<class A>
  1584. const indirect_array<A> indirect_array<A>::all_;
  1585. // Gunter Winkler contributed the classes index_pair, index_pair_array,
  1586. // index_triple and index_triple_array to enable inplace sort of parallel arrays.
  1587. template <class V>
  1588. class index_pair :
  1589. public container_reference<V> {
  1590. typedef index_pair<V> self_type;
  1591. public:
  1592. typedef typename V::size_type size_type;
  1593. BOOST_UBLAS_INLINE
  1594. index_pair(V& v, size_type i) :
  1595. container_reference<V>(v), i_(i),
  1596. v1_(v.data1_[i]), v2_(v.data2_[i]),
  1597. dirty_(false), is_copy_(false) {}
  1598. BOOST_UBLAS_INLINE
  1599. index_pair(const self_type& rhs) :
  1600. container_reference<V>(rhs()), i_(0),
  1601. v1_(rhs.v1_), v2_(rhs.v2_),
  1602. dirty_(false), is_copy_(true) {}
  1603. BOOST_UBLAS_INLINE
  1604. ~index_pair() {
  1605. if (dirty_ && (!is_copy_) ) {
  1606. (*this)().data1_[i_] = v1_;
  1607. (*this)().data2_[i_] = v2_;
  1608. }
  1609. }
  1610. BOOST_UBLAS_INLINE
  1611. self_type& operator=(const self_type& rhs) {
  1612. v1_ = rhs.v1_;
  1613. v2_ = rhs.v2_;
  1614. dirty_ = true;
  1615. return *this;
  1616. }
  1617. BOOST_UBLAS_INLINE
  1618. void swap(self_type& rhs) {
  1619. self_type tmp(rhs);
  1620. rhs = *this;
  1621. *this = tmp;
  1622. }
  1623. BOOST_UBLAS_INLINE
  1624. friend void swap(self_type& lhs, self_type& rhs) {
  1625. lhs.swap(rhs);
  1626. }
  1627. friend void swap(self_type lhs, self_type rhs) { // For gcc 4.8 and c++11
  1628. lhs.swap(rhs);
  1629. }
  1630. BOOST_UBLAS_INLINE
  1631. bool equal(const self_type& rhs) const {
  1632. return (v1_ == rhs.v1_);
  1633. }
  1634. BOOST_UBLAS_INLINE
  1635. bool less(const self_type& rhs) const {
  1636. return (v1_ < rhs.v1_);
  1637. }
  1638. BOOST_UBLAS_INLINE
  1639. friend bool operator == (const self_type& lhs, const self_type& rhs) {
  1640. return lhs.equal(rhs);
  1641. }
  1642. BOOST_UBLAS_INLINE
  1643. friend bool operator != (const self_type& lhs, const self_type& rhs) {
  1644. return !lhs.equal(rhs);
  1645. }
  1646. BOOST_UBLAS_INLINE
  1647. friend bool operator < (const self_type& lhs, const self_type& rhs) {
  1648. return lhs.less(rhs);
  1649. }
  1650. BOOST_UBLAS_INLINE
  1651. friend bool operator >= (const self_type& lhs, const self_type& rhs) {
  1652. return !lhs.less(rhs);
  1653. }
  1654. BOOST_UBLAS_INLINE
  1655. friend bool operator > (const self_type& lhs, const self_type& rhs) {
  1656. return rhs.less(lhs);
  1657. }
  1658. BOOST_UBLAS_INLINE
  1659. friend bool operator <= (const self_type& lhs, const self_type& rhs) {
  1660. return !rhs.less(lhs);
  1661. }
  1662. private:
  1663. size_type i_;
  1664. typename V::value1_type v1_;
  1665. typename V::value2_type v2_;
  1666. bool dirty_;
  1667. bool is_copy_;
  1668. };
  1669. template <class V1, class V2>
  1670. class index_pair_array:
  1671. private boost::noncopyable {
  1672. typedef index_pair_array<V1, V2> self_type;
  1673. public:
  1674. typedef typename V1::value_type value1_type;
  1675. typedef typename V2::value_type value2_type;
  1676. typedef typename V1::size_type size_type;
  1677. typedef typename V1::difference_type difference_type;
  1678. typedef index_pair<self_type> value_type;
  1679. // There is nothing that can be referenced directly. Always return a copy of the index_pair
  1680. typedef value_type reference;
  1681. typedef const value_type const_reference;
  1682. BOOST_UBLAS_INLINE
  1683. index_pair_array(size_type size, V1& data1, V2& data2) :
  1684. size_(size),data1_(data1),data2_(data2) {}
  1685. BOOST_UBLAS_INLINE
  1686. size_type size() const {
  1687. return size_;
  1688. }
  1689. BOOST_UBLAS_INLINE
  1690. const_reference operator () (size_type i) const {
  1691. return value_type((*this), i);
  1692. }
  1693. BOOST_UBLAS_INLINE
  1694. reference operator () (size_type i) {
  1695. return value_type((*this), i);
  1696. }
  1697. typedef indexed_iterator<self_type, std::random_access_iterator_tag> iterator;
  1698. typedef indexed_const_iterator<self_type, std::random_access_iterator_tag> const_iterator;
  1699. BOOST_UBLAS_INLINE
  1700. iterator begin() {
  1701. return iterator( (*this), 0);
  1702. }
  1703. BOOST_UBLAS_INLINE
  1704. iterator end() {
  1705. return iterator( (*this), size());
  1706. }
  1707. BOOST_UBLAS_INLINE
  1708. const_iterator begin() const {
  1709. return const_iterator( (*this), 0);
  1710. }
  1711. BOOST_UBLAS_INLINE
  1712. const_iterator cbegin () const {
  1713. return begin ();
  1714. }
  1715. BOOST_UBLAS_INLINE
  1716. const_iterator end() const {
  1717. return const_iterator( (*this), size());
  1718. }
  1719. BOOST_UBLAS_INLINE
  1720. const_iterator cend () const {
  1721. return end ();
  1722. }
  1723. // unnecessary function:
  1724. BOOST_UBLAS_INLINE
  1725. bool equal(size_type i1, size_type i2) const {
  1726. return data1_[i1] == data1_[i2];
  1727. }
  1728. BOOST_UBLAS_INLINE
  1729. bool less(size_type i1, size_type i2) const {
  1730. return data1_[i1] < data1_[i2];
  1731. }
  1732. // gives a large speedup
  1733. BOOST_UBLAS_INLINE
  1734. friend void iter_swap(const iterator& lhs, const iterator& rhs) {
  1735. const size_type i1 = lhs.index();
  1736. const size_type i2 = rhs.index();
  1737. std::swap(lhs().data1_[i1], rhs().data1_[i2]);
  1738. std::swap(lhs().data2_[i1], rhs().data2_[i2]);
  1739. }
  1740. private:
  1741. size_type size_;
  1742. V1& data1_;
  1743. V2& data2_;
  1744. // friend class value_type;
  1745. friend class index_pair<self_type>;
  1746. };
  1747. template <class M>
  1748. class index_triple :
  1749. public container_reference<M> {
  1750. typedef index_triple<M> self_type;
  1751. public:
  1752. typedef typename M::size_type size_type;
  1753. BOOST_UBLAS_INLINE
  1754. index_triple(M& m, size_type i) :
  1755. container_reference<M>(m), i_(i),
  1756. v1_(m.data1_[i]), v2_(m.data2_[i]), v3_(m.data3_[i]),
  1757. dirty_(false), is_copy_(false) {}
  1758. BOOST_UBLAS_INLINE
  1759. index_triple(const self_type& rhs) :
  1760. container_reference<M>(rhs()), i_(0),
  1761. v1_(rhs.v1_), v2_(rhs.v2_), v3_(rhs.v3_),
  1762. dirty_(false), is_copy_(true) {}
  1763. BOOST_UBLAS_INLINE
  1764. ~index_triple() {
  1765. if (dirty_ && (!is_copy_) ) {
  1766. (*this)().data1_[i_] = v1_;
  1767. (*this)().data2_[i_] = v2_;
  1768. (*this)().data3_[i_] = v3_;
  1769. }
  1770. }
  1771. BOOST_UBLAS_INLINE
  1772. self_type& operator=(const self_type& rhs) {
  1773. v1_ = rhs.v1_;
  1774. v2_ = rhs.v2_;
  1775. v3_ = rhs.v3_;
  1776. dirty_ = true;
  1777. return *this;
  1778. }
  1779. BOOST_UBLAS_INLINE
  1780. void swap(self_type& rhs) {
  1781. self_type tmp(rhs);
  1782. rhs = *this;
  1783. *this = tmp;
  1784. }
  1785. BOOST_UBLAS_INLINE
  1786. friend void swap(self_type& lhs, self_type& rhs) {
  1787. lhs.swap(rhs);
  1788. }
  1789. friend void swap(self_type lhs, self_type rhs) { // For gcc 4.8 and c++11
  1790. lhs.swap(rhs);
  1791. }
  1792. BOOST_UBLAS_INLINE
  1793. bool equal(const self_type& rhs) const {
  1794. return ((v1_ == rhs.v1_) && (v2_ == rhs.v2_));
  1795. }
  1796. BOOST_UBLAS_INLINE
  1797. bool less(const self_type& rhs) const {
  1798. return ((v1_ < rhs.v1_) ||
  1799. (v1_ == rhs.v1_ && v2_ < rhs.v2_));
  1800. }
  1801. BOOST_UBLAS_INLINE
  1802. friend bool operator == (const self_type& lhs, const self_type& rhs) {
  1803. return lhs.equal(rhs);
  1804. }
  1805. BOOST_UBLAS_INLINE
  1806. friend bool operator != (const self_type& lhs, const self_type& rhs) {
  1807. return !lhs.equal(rhs);
  1808. }
  1809. BOOST_UBLAS_INLINE
  1810. friend bool operator < (const self_type& lhs, const self_type& rhs) {
  1811. return lhs.less(rhs);
  1812. }
  1813. BOOST_UBLAS_INLINE
  1814. friend bool operator >= (const self_type& lhs, const self_type& rhs) {
  1815. return !lhs.less(rhs);
  1816. }
  1817. BOOST_UBLAS_INLINE
  1818. friend bool operator > (const self_type& lhs, const self_type& rhs) {
  1819. return rhs.less(lhs);
  1820. }
  1821. BOOST_UBLAS_INLINE
  1822. friend bool operator <= (const self_type& lhs, const self_type& rhs) {
  1823. return !rhs.less(lhs);
  1824. }
  1825. private:
  1826. size_type i_;
  1827. typename M::value1_type v1_;
  1828. typename M::value2_type v2_;
  1829. typename M::value3_type v3_;
  1830. bool dirty_;
  1831. bool is_copy_;
  1832. };
  1833. template <class V1, class V2, class V3>
  1834. class index_triple_array:
  1835. private boost::noncopyable {
  1836. typedef index_triple_array<V1, V2, V3> self_type;
  1837. public:
  1838. typedef typename V1::value_type value1_type;
  1839. typedef typename V2::value_type value2_type;
  1840. typedef typename V3::value_type value3_type;
  1841. typedef typename V1::size_type size_type;
  1842. typedef typename V1::difference_type difference_type;
  1843. typedef index_triple<self_type> value_type;
  1844. // There is nothing that can be referenced directly. Always return a copy of the index_triple
  1845. typedef value_type reference;
  1846. typedef const value_type const_reference;
  1847. BOOST_UBLAS_INLINE
  1848. index_triple_array(size_type size, V1& data1, V2& data2, V3& data3) :
  1849. size_(size),data1_(data1),data2_(data2),data3_(data3) {}
  1850. BOOST_UBLAS_INLINE
  1851. size_type size() const {
  1852. return size_;
  1853. }
  1854. BOOST_UBLAS_INLINE
  1855. const_reference operator () (size_type i) const {
  1856. return value_type((*this), i);
  1857. }
  1858. BOOST_UBLAS_INLINE
  1859. reference operator () (size_type i) {
  1860. return value_type((*this), i);
  1861. }
  1862. typedef indexed_iterator<self_type, std::random_access_iterator_tag> iterator;
  1863. typedef indexed_const_iterator<self_type, std::random_access_iterator_tag> const_iterator;
  1864. BOOST_UBLAS_INLINE
  1865. iterator begin() {
  1866. return iterator( (*this), 0);
  1867. }
  1868. BOOST_UBLAS_INLINE
  1869. iterator end() {
  1870. return iterator( (*this), size());
  1871. }
  1872. BOOST_UBLAS_INLINE
  1873. const_iterator begin() const {
  1874. return const_iterator( (*this), 0);
  1875. }
  1876. BOOST_UBLAS_INLINE
  1877. const_iterator cbegin () const {
  1878. return begin ();
  1879. }
  1880. BOOST_UBLAS_INLINE
  1881. const_iterator end() const {
  1882. return const_iterator( (*this), size());
  1883. }
  1884. BOOST_UBLAS_INLINE
  1885. const_iterator cend () const {
  1886. return end ();
  1887. }
  1888. // unnecessary function:
  1889. BOOST_UBLAS_INLINE
  1890. bool equal(size_type i1, size_type i2) const {
  1891. return ((data1_[i1] == data1_[i2]) && (data2_[i1] == data2_[i2]));
  1892. }
  1893. BOOST_UBLAS_INLINE
  1894. bool less(size_type i1, size_type i2) const {
  1895. return ((data1_[i1] < data1_[i2]) ||
  1896. (data1_[i1] == data1_[i2] && data2_[i1] < data2_[i2]));
  1897. }
  1898. // gives a large speedup
  1899. BOOST_UBLAS_INLINE
  1900. friend void iter_swap(const iterator& lhs, const iterator& rhs) {
  1901. const size_type i1 = lhs.index();
  1902. const size_type i2 = rhs.index();
  1903. std::swap(lhs().data1_[i1], rhs().data1_[i2]);
  1904. std::swap(lhs().data2_[i1], rhs().data2_[i2]);
  1905. std::swap(lhs().data3_[i1], rhs().data3_[i2]);
  1906. }
  1907. private:
  1908. size_type size_;
  1909. V1& data1_;
  1910. V2& data2_;
  1911. V3& data3_;
  1912. // friend class value_type;
  1913. friend class index_triple<self_type>;
  1914. };
  1915. }}}
  1916. #endif