iterator.hpp 49 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437
  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_ITERATOR_
  13. #define _BOOST_UBLAS_ITERATOR_
  14. #include <boost/numeric/ublas/exception.hpp>
  15. #include <iterator>
  16. namespace boost { namespace numeric { namespace ublas {
  17. /** \brief Base class of all proxy classes that contain
  18. * a (redirectable) reference to an immutable object.
  19. *
  20. * \param C the type of the container referred to
  21. */
  22. template<class C>
  23. class container_const_reference:
  24. private nonassignable {
  25. public:
  26. typedef C container_type;
  27. BOOST_UBLAS_INLINE
  28. container_const_reference ():
  29. c_ (0) {}
  30. BOOST_UBLAS_INLINE
  31. container_const_reference (const container_type &c):
  32. c_ (&c) {}
  33. BOOST_UBLAS_INLINE
  34. const container_type &operator () () const {
  35. return *c_;
  36. }
  37. BOOST_UBLAS_INLINE
  38. container_const_reference &assign (const container_type *c) {
  39. c_ = c;
  40. return *this;
  41. }
  42. // Closure comparison
  43. BOOST_UBLAS_INLINE
  44. bool same_closure (const container_const_reference &cr) const {
  45. return c_ == cr.c_;
  46. }
  47. private:
  48. const container_type *c_;
  49. };
  50. /** \brief Base class of all proxy classes that contain
  51. * a (redirectable) reference to a mutable object.
  52. *
  53. * \param C the type of the container referred to
  54. */
  55. template<class C>
  56. class container_reference:
  57. private nonassignable {
  58. public:
  59. typedef C container_type;
  60. BOOST_UBLAS_INLINE
  61. container_reference ():
  62. c_ (0) {}
  63. BOOST_UBLAS_INLINE
  64. container_reference (container_type &c):
  65. c_ (&c) {}
  66. BOOST_UBLAS_INLINE
  67. container_type &operator () () const {
  68. return *c_;
  69. }
  70. BOOST_UBLAS_INLINE
  71. container_reference &assign (container_type *c) {
  72. c_ = c;
  73. return *this;
  74. }
  75. // Closure comparison
  76. BOOST_UBLAS_INLINE
  77. bool same_closure (const container_reference &cr) const {
  78. return c_ == cr.c_;
  79. }
  80. private:
  81. container_type *c_;
  82. };
  83. /** \brief Base class of all forward iterators.
  84. *
  85. * \param IC the iterator category
  86. * \param I the derived iterator type
  87. * \param T the value type
  88. *
  89. * The forward iterator can only proceed in one direction
  90. * via the post increment operator.
  91. */
  92. template<class IC, class I, class T>
  93. struct forward_iterator_base:
  94. public std::iterator<IC, T> {
  95. typedef I derived_iterator_type;
  96. typedef T derived_value_type;
  97. // Arithmetic
  98. BOOST_UBLAS_INLINE
  99. derived_iterator_type operator ++ (int) {
  100. derived_iterator_type &d (*static_cast<const derived_iterator_type *> (this));
  101. derived_iterator_type tmp (d);
  102. ++ d;
  103. return tmp;
  104. }
  105. BOOST_UBLAS_INLINE
  106. friend derived_iterator_type operator ++ (derived_iterator_type &d, int) {
  107. derived_iterator_type tmp (d);
  108. ++ d;
  109. return tmp;
  110. }
  111. // Comparison
  112. BOOST_UBLAS_INLINE
  113. bool operator != (const derived_iterator_type &it) const {
  114. const derived_iterator_type *d = static_cast<const derived_iterator_type *> (this);
  115. return ! (*d == it);
  116. }
  117. };
  118. /** \brief Base class of all bidirectional iterators.
  119. *
  120. * \param IC the iterator category
  121. * \param I the derived iterator type
  122. * \param T the value type
  123. *
  124. * The bidirectional iterator can proceed in both directions
  125. * via the post increment and post decrement operator.
  126. */
  127. template<class IC, class I, class T>
  128. struct bidirectional_iterator_base:
  129. public std::iterator<IC, T> {
  130. typedef I derived_iterator_type;
  131. typedef T derived_value_type;
  132. // Arithmetic
  133. BOOST_UBLAS_INLINE
  134. derived_iterator_type operator ++ (int) {
  135. derived_iterator_type &d (*static_cast<const derived_iterator_type *> (this));
  136. derived_iterator_type tmp (d);
  137. ++ d;
  138. return tmp;
  139. }
  140. BOOST_UBLAS_INLINE
  141. friend derived_iterator_type operator ++ (derived_iterator_type &d, int) {
  142. derived_iterator_type tmp (d);
  143. ++ d;
  144. return tmp;
  145. }
  146. BOOST_UBLAS_INLINE
  147. derived_iterator_type operator -- (int) {
  148. derived_iterator_type &d (*static_cast<const derived_iterator_type *> (this));
  149. derived_iterator_type tmp (d);
  150. -- d;
  151. return tmp;
  152. }
  153. BOOST_UBLAS_INLINE
  154. friend derived_iterator_type operator -- (derived_iterator_type &d, int) {
  155. derived_iterator_type tmp (d);
  156. -- d;
  157. return tmp;
  158. }
  159. // Comparison
  160. BOOST_UBLAS_INLINE
  161. bool operator != (const derived_iterator_type &it) const {
  162. const derived_iterator_type *d = static_cast<const derived_iterator_type *> (this);
  163. return ! (*d == it);
  164. }
  165. };
  166. /** \brief Base class of all random access iterators.
  167. *
  168. * \param IC the iterator category
  169. * \param I the derived iterator type
  170. * \param T the value type
  171. * \param D the difference type, default: std::ptrdiff_t
  172. *
  173. * The random access iterator can proceed in both directions
  174. * via the post increment/decrement operator or in larger steps
  175. * via the +, - and +=, -= operators. The random access iterator
  176. * is LessThan Comparable.
  177. */
  178. template<class IC, class I, class T, class D = std::ptrdiff_t>
  179. // ISSUE the default for D seems rather dangerous as it can easily be (silently) incorrect
  180. struct random_access_iterator_base:
  181. public std::iterator<IC, T> {
  182. typedef I derived_iterator_type;
  183. typedef T derived_value_type;
  184. typedef D derived_difference_type;
  185. /* FIXME Need to explicitly pass derived_reference_type as otherwise I undefined type or forward declared
  186. typedef typename derived_iterator_type::reference derived_reference_type;
  187. // Indexed element
  188. BOOST_UBLAS_INLINE
  189. derived_reference_type operator [] (derived_difference_type n) {
  190. return *(*this + n);
  191. }
  192. */
  193. // Arithmetic
  194. BOOST_UBLAS_INLINE
  195. derived_iterator_type operator ++ (int) {
  196. derived_iterator_type &d (*static_cast<derived_iterator_type *> (this));
  197. derived_iterator_type tmp (d);
  198. ++ d;
  199. return tmp;
  200. }
  201. BOOST_UBLAS_INLINE
  202. friend derived_iterator_type operator ++ (derived_iterator_type &d, int) {
  203. derived_iterator_type tmp (d);
  204. ++ d;
  205. return tmp;
  206. }
  207. BOOST_UBLAS_INLINE
  208. derived_iterator_type operator -- (int) {
  209. derived_iterator_type &d (*static_cast<derived_iterator_type *> (this));
  210. derived_iterator_type tmp (d);
  211. -- d;
  212. return tmp;
  213. }
  214. BOOST_UBLAS_INLINE
  215. friend derived_iterator_type operator -- (derived_iterator_type &d, int) {
  216. derived_iterator_type tmp (d);
  217. -- d;
  218. return tmp;
  219. }
  220. BOOST_UBLAS_INLINE
  221. derived_iterator_type operator + (derived_difference_type n) const {
  222. derived_iterator_type tmp (*static_cast<const derived_iterator_type *> (this));
  223. return tmp += n;
  224. }
  225. BOOST_UBLAS_INLINE
  226. friend derived_iterator_type operator + (const derived_iterator_type &d, derived_difference_type n) {
  227. derived_iterator_type tmp (d);
  228. return tmp += n;
  229. }
  230. BOOST_UBLAS_INLINE
  231. friend derived_iterator_type operator + (derived_difference_type n, const derived_iterator_type &d) {
  232. derived_iterator_type tmp (d);
  233. return tmp += n;
  234. }
  235. BOOST_UBLAS_INLINE
  236. derived_iterator_type operator - (derived_difference_type n) const {
  237. derived_iterator_type tmp (*static_cast<const derived_iterator_type *> (this));
  238. return tmp -= n;
  239. }
  240. BOOST_UBLAS_INLINE
  241. friend derived_iterator_type operator - (const derived_iterator_type &d, derived_difference_type n) {
  242. derived_iterator_type tmp (d);
  243. return tmp -= n;
  244. }
  245. // Comparison
  246. BOOST_UBLAS_INLINE
  247. bool operator != (const derived_iterator_type &it) const {
  248. const derived_iterator_type *d = static_cast<const derived_iterator_type *> (this);
  249. return ! (*d == it);
  250. }
  251. BOOST_UBLAS_INLINE
  252. bool operator <= (const derived_iterator_type &it) const {
  253. const derived_iterator_type *d = static_cast<const derived_iterator_type *> (this);
  254. return ! (it < *d);
  255. }
  256. BOOST_UBLAS_INLINE
  257. bool operator >= (const derived_iterator_type &it) const {
  258. const derived_iterator_type *d = static_cast<const derived_iterator_type *> (this);
  259. return ! (*d < it);
  260. }
  261. BOOST_UBLAS_INLINE
  262. bool operator > (const derived_iterator_type &it) const {
  263. const derived_iterator_type *d = static_cast<const derived_iterator_type *> (this);
  264. return it < *d;
  265. }
  266. };
  267. /** \brief Base class of all reverse iterators. (non-MSVC version)
  268. *
  269. * \param I the derived iterator type
  270. * \param T the value type
  271. * \param R the reference type
  272. *
  273. * The reverse iterator implements a bidirectional iterator
  274. * reversing the elements of the underlying iterator. It
  275. * implements most operators of a random access iterator.
  276. *
  277. * uBLAS extension: it.index()
  278. */
  279. // Renamed this class from reverse_iterator to get
  280. // typedef reverse_iterator<...> reverse_iterator
  281. // working. Thanks to Gabriel Dos Reis for explaining this.
  282. template <class I>
  283. class reverse_iterator_base:
  284. public std::reverse_iterator<I> {
  285. public:
  286. typedef typename I::container_type container_type;
  287. typedef typename container_type::size_type size_type;
  288. typedef typename I::difference_type difference_type;
  289. typedef I iterator_type;
  290. // Construction and destruction
  291. BOOST_UBLAS_INLINE
  292. reverse_iterator_base ():
  293. std::reverse_iterator<iterator_type> () {}
  294. BOOST_UBLAS_INLINE
  295. reverse_iterator_base (const iterator_type &it):
  296. std::reverse_iterator<iterator_type> (it) {}
  297. // Arithmetic
  298. BOOST_UBLAS_INLINE
  299. reverse_iterator_base &operator ++ () {
  300. return *this = -- this->base ();
  301. }
  302. BOOST_UBLAS_INLINE
  303. reverse_iterator_base operator ++ (int) {
  304. reverse_iterator_base tmp (*this);
  305. *this = -- this->base ();
  306. return tmp;
  307. }
  308. BOOST_UBLAS_INLINE
  309. reverse_iterator_base &operator -- () {
  310. return *this = ++ this->base ();
  311. }
  312. BOOST_UBLAS_INLINE
  313. reverse_iterator_base operator -- (int) {
  314. reverse_iterator_base tmp (*this);
  315. *this = ++ this->base ();
  316. return tmp;
  317. }
  318. BOOST_UBLAS_INLINE
  319. reverse_iterator_base &operator += (difference_type n) {
  320. return *this = this->base () - n;
  321. }
  322. BOOST_UBLAS_INLINE
  323. reverse_iterator_base &operator -= (difference_type n) {
  324. return *this = this->base () + n;
  325. }
  326. BOOST_UBLAS_INLINE
  327. friend reverse_iterator_base operator + (const reverse_iterator_base &it, difference_type n) {
  328. reverse_iterator_base tmp (it);
  329. return tmp += n;
  330. }
  331. BOOST_UBLAS_INLINE
  332. friend reverse_iterator_base operator + (difference_type n, const reverse_iterator_base &it) {
  333. reverse_iterator_base tmp (it);
  334. return tmp += n;
  335. }
  336. BOOST_UBLAS_INLINE
  337. friend reverse_iterator_base operator - (const reverse_iterator_base &it, difference_type n) {
  338. reverse_iterator_base tmp (it);
  339. return tmp -= n;
  340. }
  341. BOOST_UBLAS_INLINE
  342. friend difference_type operator - (const reverse_iterator_base &it1, const reverse_iterator_base &it2) {
  343. return it2.base () - it1.base ();
  344. }
  345. BOOST_UBLAS_INLINE
  346. const container_type &operator () () const {
  347. return this->base () ();
  348. }
  349. BOOST_UBLAS_INLINE
  350. size_type index () const {
  351. iterator_type tmp (this->base ());
  352. return (-- tmp).index ();
  353. }
  354. };
  355. /** \brief 1st base class of all matrix reverse iterators. (non-MSVC version)
  356. *
  357. * \param I the derived iterator type
  358. *
  359. * The reverse iterator implements a bidirectional iterator
  360. * reversing the elements of the underlying iterator. It
  361. * implements most operators of a random access iterator.
  362. *
  363. * uBLAS extension: it.index1(), it.index2() and access to
  364. * the dual iterator via begin(), end(), rbegin(), rend()
  365. */
  366. // Renamed this class from reverse_iterator1 to get
  367. // typedef reverse_iterator1<...> reverse_iterator1
  368. // working. Thanks to Gabriel Dos Reis for explaining this.
  369. template <class I>
  370. class reverse_iterator_base1:
  371. public std::reverse_iterator<I> {
  372. public:
  373. typedef typename I::container_type container_type;
  374. typedef typename container_type::size_type size_type;
  375. typedef typename I::difference_type difference_type;
  376. typedef I iterator_type;
  377. typedef typename I::dual_iterator_type dual_iterator_type;
  378. typedef typename I::dual_reverse_iterator_type dual_reverse_iterator_type;
  379. // Construction and destruction
  380. BOOST_UBLAS_INLINE
  381. reverse_iterator_base1 ():
  382. std::reverse_iterator<iterator_type> () {}
  383. BOOST_UBLAS_INLINE
  384. reverse_iterator_base1 (const iterator_type &it):
  385. std::reverse_iterator<iterator_type> (it) {}
  386. // Arithmetic
  387. BOOST_UBLAS_INLINE
  388. reverse_iterator_base1 &operator ++ () {
  389. return *this = -- this->base ();
  390. }
  391. BOOST_UBLAS_INLINE
  392. reverse_iterator_base1 operator ++ (int) {
  393. reverse_iterator_base1 tmp (*this);
  394. *this = -- this->base ();
  395. return tmp;
  396. }
  397. BOOST_UBLAS_INLINE
  398. reverse_iterator_base1 &operator -- () {
  399. return *this = ++ this->base ();
  400. }
  401. BOOST_UBLAS_INLINE
  402. reverse_iterator_base1 operator -- (int) {
  403. reverse_iterator_base1 tmp (*this);
  404. *this = ++ this->base ();
  405. return tmp;
  406. }
  407. BOOST_UBLAS_INLINE
  408. reverse_iterator_base1 &operator += (difference_type n) {
  409. return *this = this->base () - n;
  410. }
  411. BOOST_UBLAS_INLINE
  412. reverse_iterator_base1 &operator -= (difference_type n) {
  413. return *this = this->base () + n;
  414. }
  415. BOOST_UBLAS_INLINE
  416. friend reverse_iterator_base1 operator + (const reverse_iterator_base1 &it, difference_type n) {
  417. reverse_iterator_base1 tmp (it);
  418. return tmp += n;
  419. }
  420. BOOST_UBLAS_INLINE
  421. friend reverse_iterator_base1 operator + (difference_type n, const reverse_iterator_base1 &it) {
  422. reverse_iterator_base1 tmp (it);
  423. return tmp += n;
  424. }
  425. BOOST_UBLAS_INLINE
  426. friend reverse_iterator_base1 operator - (const reverse_iterator_base1 &it, difference_type n) {
  427. reverse_iterator_base1 tmp (it);
  428. return tmp -= n;
  429. }
  430. BOOST_UBLAS_INLINE
  431. friend difference_type operator - (const reverse_iterator_base1 &it1, const reverse_iterator_base1 &it2) {
  432. return it2.base () - it1.base ();
  433. }
  434. BOOST_UBLAS_INLINE
  435. const container_type &operator () () const {
  436. return this->base () ();
  437. }
  438. BOOST_UBLAS_INLINE
  439. size_type index1 () const {
  440. iterator_type tmp (this->base ());
  441. return (-- tmp).index1 ();
  442. }
  443. BOOST_UBLAS_INLINE
  444. size_type index2 () const {
  445. iterator_type tmp (this->base ());
  446. return (-- tmp).index2 ();
  447. }
  448. BOOST_UBLAS_INLINE
  449. dual_iterator_type begin () const {
  450. iterator_type tmp (this->base ());
  451. return (-- tmp).begin ();
  452. }
  453. BOOST_UBLAS_INLINE
  454. dual_iterator_type end () const {
  455. iterator_type tmp (this->base ());
  456. return (-- tmp).end ();
  457. }
  458. BOOST_UBLAS_INLINE
  459. dual_reverse_iterator_type rbegin () const {
  460. return dual_reverse_iterator_type (end ());
  461. }
  462. BOOST_UBLAS_INLINE
  463. dual_reverse_iterator_type rend () const {
  464. return dual_reverse_iterator_type (begin ());
  465. }
  466. };
  467. /** \brief 2nd base class of all matrix reverse iterators. (non-MSVC version)
  468. *
  469. * \param I the derived iterator type
  470. *
  471. * The reverse iterator implements a bidirectional iterator
  472. * reversing the elements of the underlying iterator. It
  473. * implements most operators of a random access iterator.
  474. *
  475. * uBLAS extension: it.index1(), it.index2() and access to
  476. * the dual iterator via begin(), end(), rbegin(), rend()
  477. *
  478. * Note: this type is _identical_ to reverse_iterator_base1
  479. */
  480. // Renamed this class from reverse_iterator2 to get
  481. // typedef reverse_iterator2<...> reverse_iterator2
  482. // working. Thanks to Gabriel Dos Reis for explaining this.
  483. template <class I>
  484. class reverse_iterator_base2:
  485. public std::reverse_iterator<I> {
  486. public:
  487. typedef typename I::container_type container_type;
  488. typedef typename container_type::size_type size_type;
  489. typedef typename I::difference_type difference_type;
  490. typedef I iterator_type;
  491. typedef typename I::dual_iterator_type dual_iterator_type;
  492. typedef typename I::dual_reverse_iterator_type dual_reverse_iterator_type;
  493. // Construction and destruction
  494. BOOST_UBLAS_INLINE
  495. reverse_iterator_base2 ():
  496. std::reverse_iterator<iterator_type> () {}
  497. BOOST_UBLAS_INLINE
  498. reverse_iterator_base2 (const iterator_type &it):
  499. std::reverse_iterator<iterator_type> (it) {}
  500. // Arithmetic
  501. BOOST_UBLAS_INLINE
  502. reverse_iterator_base2 &operator ++ () {
  503. return *this = -- this->base ();
  504. }
  505. BOOST_UBLAS_INLINE
  506. reverse_iterator_base2 operator ++ (int) {
  507. reverse_iterator_base2 tmp (*this);
  508. *this = -- this->base ();
  509. return tmp;
  510. }
  511. BOOST_UBLAS_INLINE
  512. reverse_iterator_base2 &operator -- () {
  513. return *this = ++ this->base ();
  514. }
  515. BOOST_UBLAS_INLINE
  516. reverse_iterator_base2 operator -- (int) {
  517. reverse_iterator_base2 tmp (*this);
  518. *this = ++ this->base ();
  519. return tmp;
  520. }
  521. BOOST_UBLAS_INLINE
  522. reverse_iterator_base2 &operator += (difference_type n) {
  523. return *this = this->base () - n;
  524. }
  525. BOOST_UBLAS_INLINE
  526. reverse_iterator_base2 &operator -= (difference_type n) {
  527. return *this = this->base () + n;
  528. }
  529. BOOST_UBLAS_INLINE
  530. friend reverse_iterator_base2 operator + (const reverse_iterator_base2 &it, difference_type n) {
  531. reverse_iterator_base2 tmp (it);
  532. return tmp += n;
  533. }
  534. BOOST_UBLAS_INLINE
  535. friend reverse_iterator_base2 operator + (difference_type n, const reverse_iterator_base2 &it) {
  536. reverse_iterator_base2 tmp (it);
  537. return tmp += n;
  538. }
  539. BOOST_UBLAS_INLINE
  540. friend reverse_iterator_base2 operator - (const reverse_iterator_base2 &it, difference_type n) {
  541. reverse_iterator_base2 tmp (it);
  542. return tmp -= n;
  543. }
  544. BOOST_UBLAS_INLINE
  545. friend difference_type operator - (const reverse_iterator_base2 &it1, const reverse_iterator_base2 &it2) {
  546. return it2.base () - it1.base ();
  547. }
  548. BOOST_UBLAS_INLINE
  549. const container_type &operator () () const {
  550. return this->base () ();
  551. }
  552. BOOST_UBLAS_INLINE
  553. size_type index1 () const {
  554. iterator_type tmp (this->base ());
  555. return (-- tmp).index1 ();
  556. }
  557. BOOST_UBLAS_INLINE
  558. size_type index2 () const {
  559. iterator_type tmp (this->base ());
  560. return (-- tmp).index2 ();
  561. }
  562. BOOST_UBLAS_INLINE
  563. dual_iterator_type begin () const {
  564. iterator_type tmp (this->base ());
  565. return (-- tmp).begin ();
  566. }
  567. BOOST_UBLAS_INLINE
  568. dual_iterator_type end () const {
  569. iterator_type tmp (this->base ());
  570. return (-- tmp).end ();
  571. }
  572. BOOST_UBLAS_INLINE
  573. dual_reverse_iterator_type rbegin () const {
  574. return dual_reverse_iterator_type (end ());
  575. }
  576. BOOST_UBLAS_INLINE
  577. dual_reverse_iterator_type rend () const {
  578. return dual_reverse_iterator_type (begin ());
  579. }
  580. };
  581. /** \brief A class implementing an indexed random access iterator.
  582. *
  583. * \param C the (mutable) container type
  584. * \param IC the iterator category
  585. *
  586. * This class implements a random access iterator. The current
  587. * position is stored as the unsigned integer it_ and the
  588. * values are accessed via operator()(it_) of the container.
  589. *
  590. * uBLAS extension: index()
  591. */
  592. template<class C, class IC>
  593. class indexed_iterator:
  594. public container_reference<C>,
  595. public random_access_iterator_base<IC,
  596. indexed_iterator<C, IC>,
  597. typename C::value_type,
  598. typename C::difference_type> {
  599. public:
  600. typedef C container_type;
  601. typedef IC iterator_category;
  602. typedef typename container_type::size_type size_type;
  603. typedef typename container_type::difference_type difference_type;
  604. typedef typename container_type::value_type value_type;
  605. typedef typename container_type::reference reference;
  606. // Construction and destruction
  607. BOOST_UBLAS_INLINE
  608. indexed_iterator ():
  609. container_reference<container_type> (), it_ () {}
  610. BOOST_UBLAS_INLINE
  611. indexed_iterator (container_type &c, size_type it):
  612. container_reference<container_type> (c), it_ (it) {}
  613. // Arithmetic
  614. BOOST_UBLAS_INLINE
  615. indexed_iterator &operator ++ () {
  616. ++ it_;
  617. return *this;
  618. }
  619. BOOST_UBLAS_INLINE
  620. indexed_iterator &operator -- () {
  621. -- it_;
  622. return *this;
  623. }
  624. BOOST_UBLAS_INLINE
  625. indexed_iterator &operator += (difference_type n) {
  626. it_ += n;
  627. return *this;
  628. }
  629. BOOST_UBLAS_INLINE
  630. indexed_iterator &operator -= (difference_type n) {
  631. it_ -= n;
  632. return *this;
  633. }
  634. BOOST_UBLAS_INLINE
  635. difference_type operator - (const indexed_iterator &it) const {
  636. BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ());
  637. return it_ - it.it_;
  638. }
  639. // Dereference
  640. BOOST_UBLAS_INLINE
  641. reference operator * () const {
  642. BOOST_UBLAS_CHECK (index () < (*this) ().size (), bad_index ());
  643. return (*this) () (it_);
  644. }
  645. BOOST_UBLAS_INLINE
  646. reference operator [] (difference_type n) const {
  647. return *((*this) + n);
  648. }
  649. // Index
  650. BOOST_UBLAS_INLINE
  651. size_type index () const {
  652. return it_;
  653. }
  654. // Assignment
  655. BOOST_UBLAS_INLINE
  656. indexed_iterator &operator = (const indexed_iterator &it) {
  657. // FIX: ICC needs full qualification?!
  658. // assign (&it ());
  659. container_reference<C>::assign (&it ());
  660. it_ = it.it_;
  661. return *this;
  662. }
  663. // Comparison
  664. BOOST_UBLAS_INLINE
  665. bool operator == (const indexed_iterator &it) const {
  666. BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ());
  667. return it_ == it.it_;
  668. }
  669. BOOST_UBLAS_INLINE
  670. bool operator < (const indexed_iterator &it) const {
  671. BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ());
  672. return it_ < it.it_;
  673. }
  674. private:
  675. size_type it_;
  676. };
  677. /** \brief A class implementing an indexed random access iterator.
  678. *
  679. * \param C the (immutable) container type
  680. * \param IC the iterator category
  681. *
  682. * This class implements a random access iterator. The current
  683. * position is stored as the unsigned integer \c it_ and the
  684. * values are accessed via \c operator()(it_) of the container.
  685. *
  686. * uBLAS extension: \c index()
  687. *
  688. * Note: there is an automatic conversion from
  689. * \c indexed_iterator to \c indexed_const_iterator
  690. */
  691. template<class C, class IC>
  692. class indexed_const_iterator:
  693. public container_const_reference<C>,
  694. public random_access_iterator_base<IC,
  695. indexed_const_iterator<C, IC>,
  696. typename C::value_type,
  697. typename C::difference_type> {
  698. public:
  699. typedef C container_type;
  700. typedef IC iterator_category;
  701. typedef typename container_type::size_type size_type;
  702. typedef typename container_type::difference_type difference_type;
  703. typedef typename container_type::value_type value_type;
  704. typedef typename container_type::const_reference reference;
  705. typedef indexed_iterator<container_type, iterator_category> iterator_type;
  706. // Construction and destruction
  707. BOOST_UBLAS_INLINE
  708. indexed_const_iterator ():
  709. container_const_reference<container_type> (), it_ () {}
  710. BOOST_UBLAS_INLINE
  711. indexed_const_iterator (const container_type &c, size_type it):
  712. container_const_reference<container_type> (c), it_ (it) {}
  713. BOOST_UBLAS_INLINE
  714. indexed_const_iterator (const iterator_type &it):
  715. container_const_reference<container_type> (it ()), it_ (it.index ()) {}
  716. // Arithmetic
  717. BOOST_UBLAS_INLINE
  718. indexed_const_iterator &operator ++ () {
  719. ++ it_;
  720. return *this;
  721. }
  722. BOOST_UBLAS_INLINE
  723. indexed_const_iterator &operator -- () {
  724. -- it_;
  725. return *this;
  726. }
  727. BOOST_UBLAS_INLINE
  728. indexed_const_iterator &operator += (difference_type n) {
  729. it_ += n;
  730. return *this;
  731. }
  732. BOOST_UBLAS_INLINE
  733. indexed_const_iterator &operator -= (difference_type n) {
  734. it_ -= n;
  735. return *this;
  736. }
  737. BOOST_UBLAS_INLINE
  738. difference_type operator - (const indexed_const_iterator &it) const {
  739. BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ());
  740. return it_ - it.it_;
  741. }
  742. // Dereference
  743. BOOST_UBLAS_INLINE
  744. reference operator * () const {
  745. BOOST_UBLAS_CHECK (index () < (*this) ().size (), bad_index ());
  746. return (*this) () (it_);
  747. }
  748. BOOST_UBLAS_INLINE
  749. reference operator [] (difference_type n) const {
  750. return *((*this) + n);
  751. }
  752. // Index
  753. BOOST_UBLAS_INLINE
  754. size_type index () const {
  755. return it_;
  756. }
  757. // Assignment
  758. BOOST_UBLAS_INLINE
  759. indexed_const_iterator &operator = (const indexed_const_iterator &it) {
  760. // FIX: ICC needs full qualification?!
  761. // assign (&it ());
  762. container_const_reference<C>::assign (&it ());
  763. it_ = it.it_;
  764. return *this;
  765. }
  766. // Comparison
  767. BOOST_UBLAS_INLINE
  768. bool operator == (const indexed_const_iterator &it) const {
  769. BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ());
  770. return it_ == it.it_;
  771. }
  772. BOOST_UBLAS_INLINE
  773. bool operator < (const indexed_const_iterator &it) const {
  774. BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ());
  775. return it_ < it.it_;
  776. }
  777. private:
  778. size_type it_;
  779. friend class indexed_iterator<container_type, iterator_category>;
  780. };
  781. template<class C, class IC>
  782. class indexed_iterator2;
  783. /** \brief A class implementing an indexed random access iterator
  784. * of a matrix.
  785. *
  786. * \param C the (mutable) container type
  787. * \param IC the iterator category
  788. *
  789. * This class implements a random access iterator. The current
  790. * position is stored as two unsigned integers \c it1_ and \c it2_
  791. * and the values are accessed via \c operator()(it1_, it2_) of the
  792. * container. The iterator changes the first index.
  793. *
  794. * uBLAS extension: \c index1(), \c index2() and access to the
  795. * dual iterator via \c begin(), \c end(), \c rbegin() and \c rend()
  796. *
  797. * Note: The container has to support the \code find2(rank, i, j) \endcode
  798. * method
  799. */
  800. template<class C, class IC>
  801. class indexed_iterator1:
  802. public container_reference<C>,
  803. public random_access_iterator_base<IC,
  804. indexed_iterator1<C, IC>,
  805. typename C::value_type,
  806. typename C::difference_type> {
  807. public:
  808. typedef C container_type;
  809. typedef IC iterator_category;
  810. typedef typename container_type::size_type size_type;
  811. typedef typename container_type::difference_type difference_type;
  812. typedef typename container_type::value_type value_type;
  813. typedef typename container_type::reference reference;
  814. typedef indexed_iterator2<container_type, iterator_category> dual_iterator_type;
  815. typedef reverse_iterator_base2<dual_iterator_type> dual_reverse_iterator_type;
  816. // Construction and destruction
  817. BOOST_UBLAS_INLINE
  818. indexed_iterator1 ():
  819. container_reference<container_type> (), it1_ (), it2_ () {}
  820. BOOST_UBLAS_INLINE
  821. indexed_iterator1 (container_type &c, size_type it1, size_type it2):
  822. container_reference<container_type> (c), it1_ (it1), it2_ (it2) {}
  823. // Arithmetic
  824. BOOST_UBLAS_INLINE
  825. indexed_iterator1 &operator ++ () {
  826. ++ it1_;
  827. return *this;
  828. }
  829. BOOST_UBLAS_INLINE
  830. indexed_iterator1 &operator -- () {
  831. -- it1_;
  832. return *this;
  833. }
  834. BOOST_UBLAS_INLINE
  835. indexed_iterator1 &operator += (difference_type n) {
  836. it1_ += n;
  837. return *this;
  838. }
  839. BOOST_UBLAS_INLINE
  840. indexed_iterator1 &operator -= (difference_type n) {
  841. it1_ -= n;
  842. return *this;
  843. }
  844. BOOST_UBLAS_INLINE
  845. difference_type operator - (const indexed_iterator1 &it) const {
  846. BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ());
  847. BOOST_UBLAS_CHECK (it2_ == it.it2_, external_logic ());
  848. return it1_ - it.it1_;
  849. }
  850. // Dereference
  851. BOOST_UBLAS_INLINE
  852. reference operator * () const {
  853. BOOST_UBLAS_CHECK (index1 () < (*this) ().size1 (), bad_index ());
  854. BOOST_UBLAS_CHECK (index2 () < (*this) ().size2 (), bad_index ());
  855. return (*this) () (it1_, it2_);
  856. }
  857. BOOST_UBLAS_INLINE
  858. reference operator [] (difference_type n) const {
  859. return *((*this) + n);
  860. }
  861. // Index
  862. BOOST_UBLAS_INLINE
  863. size_type index1 () const {
  864. return it1_;
  865. }
  866. BOOST_UBLAS_INLINE
  867. size_type index2 () const {
  868. return it2_;
  869. }
  870. BOOST_UBLAS_INLINE
  871. dual_iterator_type begin () const {
  872. return (*this) ().find2 (1, index1 (), 0);
  873. }
  874. BOOST_UBLAS_INLINE
  875. dual_iterator_type end () const {
  876. return (*this) ().find2 (1, index1 (), (*this) ().size2 ());
  877. }
  878. BOOST_UBLAS_INLINE
  879. dual_reverse_iterator_type rbegin () const {
  880. return dual_reverse_iterator_type (end ());
  881. }
  882. BOOST_UBLAS_INLINE
  883. dual_reverse_iterator_type rend () const {
  884. return dual_reverse_iterator_type (begin ());
  885. }
  886. // Assignment
  887. BOOST_UBLAS_INLINE
  888. indexed_iterator1 &operator = (const indexed_iterator1 &it) {
  889. // FIX: ICC needs full qualification?!
  890. // assign (&it ());
  891. container_reference<C>::assign (&it ());
  892. it1_ = it.it1_;
  893. it2_ = it.it2_;
  894. return *this;
  895. }
  896. // Comparison
  897. BOOST_UBLAS_INLINE
  898. bool operator == (const indexed_iterator1 &it) const {
  899. BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ());
  900. BOOST_UBLAS_CHECK (it2_ == it.it2_, external_logic ());
  901. return it1_ == it.it1_;
  902. }
  903. BOOST_UBLAS_INLINE
  904. bool operator < (const indexed_iterator1 &it) const {
  905. BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ());
  906. BOOST_UBLAS_CHECK (it2_ == it.it2_, external_logic ());
  907. return it1_ < it.it1_;
  908. }
  909. private:
  910. size_type it1_;
  911. size_type it2_;
  912. };
  913. template<class C, class IC>
  914. class indexed_const_iterator2;
  915. /** \brief A class implementing an indexed random access iterator
  916. * of a matrix.
  917. *
  918. * \param C the (immutable) container type
  919. * \param IC the iterator category
  920. *
  921. * This class implements a random access iterator. The current
  922. * position is stored as two unsigned integers \c it1_ and \c it2_
  923. * and the values are accessed via \c operator()(it1_, it2_) of the
  924. * container. The iterator changes the first index.
  925. *
  926. * uBLAS extension: \c index1(), \c index2() and access to the
  927. * dual iterator via \c begin(), \c end(), \c rbegin() and \c rend()
  928. *
  929. * Note 1: The container has to support the find2(rank, i, j) method
  930. *
  931. * Note 2: there is an automatic conversion from
  932. * \c indexed_iterator1 to \c indexed_const_iterator1
  933. */
  934. template<class C, class IC>
  935. class indexed_const_iterator1:
  936. public container_const_reference<C>,
  937. public random_access_iterator_base<IC,
  938. indexed_const_iterator1<C, IC>,
  939. typename C::value_type,
  940. typename C::difference_type> {
  941. public:
  942. typedef C container_type;
  943. typedef IC iterator_category;
  944. typedef typename container_type::size_type size_type;
  945. typedef typename container_type::difference_type difference_type;
  946. typedef typename container_type::value_type value_type;
  947. typedef typename container_type::const_reference reference;
  948. typedef indexed_iterator1<container_type, iterator_category> iterator_type;
  949. typedef indexed_const_iterator2<container_type, iterator_category> dual_iterator_type;
  950. typedef reverse_iterator_base2<dual_iterator_type> dual_reverse_iterator_type;
  951. // Construction and destruction
  952. BOOST_UBLAS_INLINE
  953. indexed_const_iterator1 ():
  954. container_const_reference<container_type> (), it1_ (), it2_ () {}
  955. BOOST_UBLAS_INLINE
  956. indexed_const_iterator1 (const container_type &c, size_type it1, size_type it2):
  957. container_const_reference<container_type> (c), it1_ (it1), it2_ (it2) {}
  958. BOOST_UBLAS_INLINE
  959. indexed_const_iterator1 (const iterator_type &it):
  960. container_const_reference<container_type> (it ()), it1_ (it.index1 ()), it2_ (it.index2 ()) {}
  961. // Arithmetic
  962. BOOST_UBLAS_INLINE
  963. indexed_const_iterator1 &operator ++ () {
  964. ++ it1_;
  965. return *this;
  966. }
  967. BOOST_UBLAS_INLINE
  968. indexed_const_iterator1 &operator -- () {
  969. -- it1_;
  970. return *this;
  971. }
  972. BOOST_UBLAS_INLINE
  973. indexed_const_iterator1 &operator += (difference_type n) {
  974. it1_ += n;
  975. return *this;
  976. }
  977. BOOST_UBLAS_INLINE
  978. indexed_const_iterator1 &operator -= (difference_type n) {
  979. it1_ -= n;
  980. return *this;
  981. }
  982. BOOST_UBLAS_INLINE
  983. difference_type operator - (const indexed_const_iterator1 &it) const {
  984. BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ());
  985. BOOST_UBLAS_CHECK (it2_ == it.it2_, external_logic ());
  986. return it1_ - it.it1_;
  987. }
  988. // Dereference
  989. BOOST_UBLAS_INLINE
  990. reference operator * () const {
  991. BOOST_UBLAS_CHECK (index1 () < (*this) ().size1 (), bad_index ());
  992. BOOST_UBLAS_CHECK (index2 () < (*this) ().size2 (), bad_index ());
  993. return (*this) () (it1_, it2_);
  994. }
  995. BOOST_UBLAS_INLINE
  996. reference operator [] (difference_type n) const {
  997. return *((*this) + n);
  998. }
  999. // Index
  1000. BOOST_UBLAS_INLINE
  1001. size_type index1 () const {
  1002. return it1_;
  1003. }
  1004. BOOST_UBLAS_INLINE
  1005. size_type index2 () const {
  1006. return it2_;
  1007. }
  1008. BOOST_UBLAS_INLINE
  1009. dual_iterator_type begin () const {
  1010. return (*this) ().find2 (1, index1 (), 0);
  1011. }
  1012. BOOST_UBLAS_INLINE
  1013. dual_iterator_type end () const {
  1014. return (*this) ().find2 (1, index1 (), (*this) ().size2 ());
  1015. }
  1016. BOOST_UBLAS_INLINE
  1017. dual_reverse_iterator_type rbegin () const {
  1018. return dual_reverse_iterator_type (end ());
  1019. }
  1020. BOOST_UBLAS_INLINE
  1021. dual_reverse_iterator_type rend () const {
  1022. return dual_reverse_iterator_type (begin ());
  1023. }
  1024. // Assignment
  1025. BOOST_UBLAS_INLINE
  1026. indexed_const_iterator1 &operator = (const indexed_const_iterator1 &it) {
  1027. // FIX: ICC needs full qualification?!
  1028. // assign (&it ());
  1029. container_const_reference<C>::assign (&it ());
  1030. it1_ = it.it1_;
  1031. it2_ = it.it2_;
  1032. return *this;
  1033. }
  1034. // Comparison
  1035. BOOST_UBLAS_INLINE
  1036. bool operator == (const indexed_const_iterator1 &it) const {
  1037. BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ());
  1038. BOOST_UBLAS_CHECK (it2_ == it.it2_, external_logic ());
  1039. return it1_ == it.it1_;
  1040. }
  1041. BOOST_UBLAS_INLINE
  1042. bool operator < (const indexed_const_iterator1 &it) const {
  1043. BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ());
  1044. BOOST_UBLAS_CHECK (it2_ == it.it2_, external_logic ());
  1045. return it1_ < it.it1_;
  1046. }
  1047. private:
  1048. size_type it1_;
  1049. size_type it2_;
  1050. friend class indexed_iterator1<container_type, iterator_category>;
  1051. };
  1052. /** \brief A class implementing an indexed random access iterator
  1053. * of a matrix.
  1054. *
  1055. * \param C the (mutable) container type
  1056. * \param IC the iterator category
  1057. *
  1058. * This class implements a random access iterator. The current
  1059. * position is stored as two unsigned integers \c it1_ and \c it2_
  1060. * and the values are accessed via \c operator()(it1_, it2_) of the
  1061. * container. The iterator changes the second index.
  1062. *
  1063. * uBLAS extension: \c index1(), \c index2() and access to the
  1064. * dual iterator via \c begin(), \c end(), \c rbegin() and \c rend()
  1065. *
  1066. * Note: The container has to support the find1(rank, i, j) method
  1067. */
  1068. template<class C, class IC>
  1069. class indexed_iterator2:
  1070. public container_reference<C>,
  1071. public random_access_iterator_base<IC,
  1072. indexed_iterator2<C, IC>,
  1073. typename C::value_type,
  1074. typename C::difference_type> {
  1075. public:
  1076. typedef C container_type;
  1077. typedef IC iterator_category;
  1078. typedef typename container_type::size_type size_type;
  1079. typedef typename container_type::difference_type difference_type;
  1080. typedef typename container_type::value_type value_type;
  1081. typedef typename container_type::reference reference;
  1082. typedef indexed_iterator1<container_type, iterator_category> dual_iterator_type;
  1083. typedef reverse_iterator_base1<dual_iterator_type> dual_reverse_iterator_type;
  1084. // Construction and destruction
  1085. BOOST_UBLAS_INLINE
  1086. indexed_iterator2 ():
  1087. container_reference<container_type> (), it1_ (), it2_ () {}
  1088. BOOST_UBLAS_INLINE
  1089. indexed_iterator2 (container_type &c, size_type it1, size_type it2):
  1090. container_reference<container_type> (c), it1_ (it1), it2_ (it2) {}
  1091. // Arithmetic
  1092. BOOST_UBLAS_INLINE
  1093. indexed_iterator2 &operator ++ () {
  1094. ++ it2_;
  1095. return *this;
  1096. }
  1097. BOOST_UBLAS_INLINE
  1098. indexed_iterator2 &operator -- () {
  1099. -- it2_;
  1100. return *this;
  1101. }
  1102. BOOST_UBLAS_INLINE
  1103. indexed_iterator2 &operator += (difference_type n) {
  1104. it2_ += n;
  1105. return *this;
  1106. }
  1107. BOOST_UBLAS_INLINE
  1108. indexed_iterator2 &operator -= (difference_type n) {
  1109. it2_ -= n;
  1110. return *this;
  1111. }
  1112. BOOST_UBLAS_INLINE
  1113. difference_type operator - (const indexed_iterator2 &it) const {
  1114. BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ());
  1115. BOOST_UBLAS_CHECK (it1_ == it.it1_, external_logic ());
  1116. return it2_ - it.it2_;
  1117. }
  1118. // Dereference
  1119. BOOST_UBLAS_INLINE
  1120. reference operator * () const {
  1121. BOOST_UBLAS_CHECK (index1 () < (*this) ().size1 (), bad_index ());
  1122. BOOST_UBLAS_CHECK (index2 () < (*this) ().size2 (), bad_index ());
  1123. return (*this) () (it1_, it2_);
  1124. }
  1125. BOOST_UBLAS_INLINE
  1126. reference operator [] (difference_type n) const {
  1127. return *((*this) + n);
  1128. }
  1129. // Index
  1130. BOOST_UBLAS_INLINE
  1131. size_type index1 () const {
  1132. return it1_;
  1133. }
  1134. BOOST_UBLAS_INLINE
  1135. size_type index2 () const {
  1136. return it2_;
  1137. }
  1138. BOOST_UBLAS_INLINE
  1139. dual_iterator_type begin () const {
  1140. return (*this) ().find1 (1, 0, index2 ());
  1141. }
  1142. BOOST_UBLAS_INLINE
  1143. dual_iterator_type end () const {
  1144. return (*this) ().find1 (1, (*this) ().size1 (), index2 ());
  1145. }
  1146. BOOST_UBLAS_INLINE
  1147. dual_reverse_iterator_type rbegin () const {
  1148. return dual_reverse_iterator_type (end ());
  1149. }
  1150. BOOST_UBLAS_INLINE
  1151. dual_reverse_iterator_type rend () const {
  1152. return dual_reverse_iterator_type (begin ());
  1153. }
  1154. // Assignment
  1155. BOOST_UBLAS_INLINE
  1156. indexed_iterator2 &operator = (const indexed_iterator2 &it) {
  1157. // FIX: ICC needs full qualification?!
  1158. // assign (&it ());
  1159. container_reference<C>::assign (&it ());
  1160. it1_ = it.it1_;
  1161. it2_ = it.it2_;
  1162. return *this;
  1163. }
  1164. // Comparison
  1165. BOOST_UBLAS_INLINE
  1166. bool operator == (const indexed_iterator2 &it) const {
  1167. BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ());
  1168. BOOST_UBLAS_CHECK (it1_ == it.it1_, external_logic ());
  1169. return it2_ == it.it2_;
  1170. }
  1171. BOOST_UBLAS_INLINE
  1172. bool operator < (const indexed_iterator2 &it) const {
  1173. BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ());
  1174. BOOST_UBLAS_CHECK (it1_ == it.it1_, external_logic ());
  1175. return it2_ < it.it2_;
  1176. }
  1177. private:
  1178. size_type it1_;
  1179. size_type it2_;
  1180. };
  1181. /** \brief A class implementing an indexed random access iterator
  1182. * of a matrix.
  1183. *
  1184. * \param C the (immutable) container type
  1185. * \param IC the iterator category
  1186. *
  1187. * This class implements a random access iterator. The current
  1188. * position is stored as two unsigned integers \c it1_ and \c it2_
  1189. * and the values are accessed via \c operator()(it1_, it2_) of the
  1190. * container. The iterator changes the second index.
  1191. *
  1192. * uBLAS extension: \c index1(), \c index2() and access to the
  1193. * dual iterator via \c begin(), \c end(), \c rbegin() and \c rend()
  1194. *
  1195. * Note 1: The container has to support the \c find2(rank, i, j) method
  1196. *
  1197. * Note 2: there is an automatic conversion from
  1198. * \c indexed_iterator2 to \c indexed_const_iterator2
  1199. */
  1200. template<class C, class IC>
  1201. class indexed_const_iterator2:
  1202. public container_const_reference<C>,
  1203. public random_access_iterator_base<IC,
  1204. indexed_const_iterator2<C, IC>,
  1205. typename C::value_type,
  1206. typename C::difference_type> {
  1207. public:
  1208. typedef C container_type;
  1209. typedef IC iterator_category;
  1210. typedef typename container_type::size_type size_type;
  1211. typedef typename container_type::difference_type difference_type;
  1212. typedef typename container_type::value_type value_type;
  1213. typedef typename container_type::const_reference reference;
  1214. typedef indexed_iterator2<container_type, iterator_category> iterator_type;
  1215. typedef indexed_const_iterator1<container_type, iterator_category> dual_iterator_type;
  1216. typedef reverse_iterator_base1<dual_iterator_type> dual_reverse_iterator_type;
  1217. // Construction and destruction
  1218. BOOST_UBLAS_INLINE
  1219. indexed_const_iterator2 ():
  1220. container_const_reference<container_type> (), it1_ (), it2_ () {}
  1221. BOOST_UBLAS_INLINE
  1222. indexed_const_iterator2 (const container_type &c, size_type it1, size_type it2):
  1223. container_const_reference<container_type> (c), it1_ (it1), it2_ (it2) {}
  1224. BOOST_UBLAS_INLINE
  1225. indexed_const_iterator2 (const iterator_type &it):
  1226. container_const_reference<container_type> (it ()), it1_ (it.index1 ()), it2_ (it.index2 ()) {}
  1227. // Arithmetic
  1228. BOOST_UBLAS_INLINE
  1229. indexed_const_iterator2 &operator ++ () {
  1230. ++ it2_;
  1231. return *this;
  1232. }
  1233. BOOST_UBLAS_INLINE
  1234. indexed_const_iterator2 &operator -- () {
  1235. -- it2_;
  1236. return *this;
  1237. }
  1238. BOOST_UBLAS_INLINE
  1239. indexed_const_iterator2 &operator += (difference_type n) {
  1240. it2_ += n;
  1241. return *this;
  1242. }
  1243. BOOST_UBLAS_INLINE
  1244. indexed_const_iterator2 &operator -= (difference_type n) {
  1245. it2_ -= n;
  1246. return *this;
  1247. }
  1248. BOOST_UBLAS_INLINE
  1249. difference_type operator - (const indexed_const_iterator2 &it) const {
  1250. BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ());
  1251. BOOST_UBLAS_CHECK (it1_ == it.it1_, external_logic ());
  1252. return it2_ - it.it2_;
  1253. }
  1254. // Dereference
  1255. BOOST_UBLAS_INLINE
  1256. reference operator * () const {
  1257. BOOST_UBLAS_CHECK (index1 () < (*this) ().size1 (), bad_index ());
  1258. BOOST_UBLAS_CHECK (index2 () < (*this) ().size2 (), bad_index ());
  1259. return (*this) () (it1_, it2_);
  1260. }
  1261. BOOST_UBLAS_INLINE
  1262. reference operator [] (difference_type n) const {
  1263. return *((*this) + n);
  1264. }
  1265. // Index
  1266. BOOST_UBLAS_INLINE
  1267. size_type index1 () const {
  1268. return it1_;
  1269. }
  1270. BOOST_UBLAS_INLINE
  1271. size_type index2 () const {
  1272. return it2_;
  1273. }
  1274. BOOST_UBLAS_INLINE
  1275. dual_iterator_type begin () const {
  1276. return (*this) ().find1 (1, 0, index2 ());
  1277. }
  1278. BOOST_UBLAS_INLINE
  1279. dual_iterator_type end () const {
  1280. return (*this) ().find1 (1, (*this) ().size1 (), index2 ());
  1281. }
  1282. BOOST_UBLAS_INLINE
  1283. dual_reverse_iterator_type rbegin () const {
  1284. return dual_reverse_iterator_type (end ());
  1285. }
  1286. BOOST_UBLAS_INLINE
  1287. dual_reverse_iterator_type rend () const {
  1288. return dual_reverse_iterator_type (begin ());
  1289. }
  1290. // Assignment
  1291. BOOST_UBLAS_INLINE
  1292. indexed_const_iterator2 &operator = (const indexed_const_iterator2 &it) {
  1293. // FIX: ICC needs full qualification?!
  1294. // assign (&it ());
  1295. container_const_reference<C>::assign (&it ());
  1296. it1_ = it.it1_;
  1297. it2_ = it.it2_;
  1298. return *this;
  1299. }
  1300. // Comparison
  1301. BOOST_UBLAS_INLINE
  1302. bool operator == (const indexed_const_iterator2 &it) const {
  1303. BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ());
  1304. BOOST_UBLAS_CHECK (it1_ == it.it1_, external_logic ());
  1305. return it2_ == it.it2_;
  1306. }
  1307. BOOST_UBLAS_INLINE
  1308. bool operator < (const indexed_const_iterator2 &it) const {
  1309. BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ());
  1310. BOOST_UBLAS_CHECK (it1_ == it.it1_, external_logic ());
  1311. return it2_ < it.it2_;
  1312. }
  1313. private:
  1314. size_type it1_;
  1315. size_type it2_;
  1316. friend class indexed_iterator2<container_type, iterator_category>;
  1317. };
  1318. }}}
  1319. #endif