storage.hpp 65 KB

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