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