iterator.hpp 49 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436
  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