storage.hpp 66 KB

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