storage.hpp 65 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087
  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