storage.hpp 66 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130
  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