symmetric.hpp 81 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134
  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_SYMMETRIC_
  13. #define _BOOST_UBLAS_SYMMETRIC_
  14. #include <boost/numeric/ublas/matrix.hpp>
  15. #include <boost/numeric/ublas/triangular.hpp>
  16. #include <boost/numeric/ublas/detail/temporary.hpp>
  17. // Iterators based on ideas of Jeremy Siek
  18. // Symmetric matrices are square. Thanks to Peter Schmitteckert for spotting this.
  19. namespace boost { namespace numeric { namespace ublas {
  20. template<class M>
  21. bool is_symmetric (const M &m) {
  22. typedef typename M::size_type size_type;
  23. if (m.size1 () != m.size2 ())
  24. return false;
  25. size_type size = BOOST_UBLAS_SAME (m.size1 (), m.size2 ());
  26. for (size_type i = 0; i < size; ++ i) {
  27. for (size_type j = i; j < size; ++ j) {
  28. if (m (i, j) != m (j, i))
  29. return false;
  30. }
  31. }
  32. return true;
  33. }
  34. // Array based symmetric matrix class
  35. template<class T, class TRI, class L, class A>
  36. class symmetric_matrix:
  37. public matrix_container<symmetric_matrix<T, TRI, L, A> > {
  38. typedef T *pointer;
  39. typedef TRI triangular_type;
  40. typedef L layout_type;
  41. typedef symmetric_matrix<T, TRI, L, A> self_type;
  42. public:
  43. #ifdef BOOST_UBLAS_ENABLE_PROXY_SHORTCUTS
  44. using matrix_container<self_type>::operator ();
  45. #endif
  46. typedef typename A::size_type size_type;
  47. typedef typename A::difference_type difference_type;
  48. typedef T value_type;
  49. typedef const T &const_reference;
  50. typedef T &reference;
  51. typedef A array_type;
  52. typedef const matrix_reference<const self_type> const_closure_type;
  53. typedef matrix_reference<self_type> closure_type;
  54. typedef vector<T, A> vector_temporary_type;
  55. typedef matrix<T, L, A> matrix_temporary_type; // general sub-matrix
  56. typedef packed_tag storage_category;
  57. typedef typename L::orientation_category orientation_category;
  58. // Construction and destruction
  59. BOOST_UBLAS_INLINE
  60. symmetric_matrix ():
  61. matrix_container<self_type> (),
  62. size_ (0), data_ (0) {}
  63. BOOST_UBLAS_INLINE
  64. symmetric_matrix (size_type size):
  65. matrix_container<self_type> (),
  66. size_ (BOOST_UBLAS_SAME (size, size)), data_ (triangular_type::packed_size (layout_type (), size, size)) {
  67. }
  68. BOOST_UBLAS_INLINE
  69. symmetric_matrix (size_type size1, size_type size2):
  70. matrix_container<self_type> (),
  71. size_ (BOOST_UBLAS_SAME (size1, size2)), data_ (triangular_type::packed_size (layout_type (), size1, size2)) {
  72. }
  73. BOOST_UBLAS_INLINE
  74. symmetric_matrix (size_type size, const array_type &data):
  75. matrix_container<self_type> (),
  76. size_ (size), data_ (data) {}
  77. BOOST_UBLAS_INLINE
  78. symmetric_matrix (const symmetric_matrix &m):
  79. matrix_container<self_type> (),
  80. size_ (m.size_), data_ (m.data_) {}
  81. template<class AE>
  82. BOOST_UBLAS_INLINE
  83. symmetric_matrix (const matrix_expression<AE> &ae):
  84. matrix_container<self_type> (),
  85. size_ (BOOST_UBLAS_SAME (ae ().size1 (), ae ().size2 ())),
  86. data_ (triangular_type::packed_size (layout_type (), size_, size_)) {
  87. matrix_assign<scalar_assign> (*this, ae);
  88. }
  89. // Accessors
  90. BOOST_UBLAS_INLINE
  91. size_type size1 () const {
  92. return size_;
  93. }
  94. BOOST_UBLAS_INLINE
  95. size_type size2 () const {
  96. return size_;
  97. }
  98. // Storage accessors
  99. BOOST_UBLAS_INLINE
  100. const array_type &data () const {
  101. return data_;
  102. }
  103. BOOST_UBLAS_INLINE
  104. array_type &data () {
  105. return data_;
  106. }
  107. // Resizing
  108. BOOST_UBLAS_INLINE
  109. void resize (size_type size, bool preserve = true) {
  110. if (preserve) {
  111. self_type temporary (size, size);
  112. detail::matrix_resize_preserve<layout_type, triangular_type> (*this, temporary);
  113. }
  114. else {
  115. data ().resize (triangular_type::packed_size (layout_type (), size, size));
  116. size_ = size;
  117. }
  118. }
  119. BOOST_UBLAS_INLINE
  120. void resize (size_type size1, size_type size2, bool preserve = true) {
  121. resize (BOOST_UBLAS_SAME (size1, size2), preserve);
  122. }
  123. BOOST_UBLAS_INLINE
  124. void resize_packed_preserve (size_type size) {
  125. size_ = BOOST_UBLAS_SAME (size, size);
  126. data ().resize (triangular_type::packed_size (layout_type (), size_, size_), value_type ());
  127. }
  128. // Element access
  129. BOOST_UBLAS_INLINE
  130. const_reference operator () (size_type i, size_type j) const {
  131. BOOST_UBLAS_CHECK (i < size_, bad_index ());
  132. BOOST_UBLAS_CHECK (j < size_, bad_index ());
  133. if (triangular_type::other (i, j))
  134. return data () [triangular_type::element (layout_type (), i, size_, j, size_)];
  135. else
  136. return data () [triangular_type::element (layout_type (), j, size_, i, size_)];
  137. }
  138. BOOST_UBLAS_INLINE
  139. reference at_element (size_type i, size_type j) {
  140. BOOST_UBLAS_CHECK (i < size_, bad_index ());
  141. BOOST_UBLAS_CHECK (j < size_, bad_index ());
  142. return data () [triangular_type::element (layout_type (), i, size_, j, size_)];
  143. }
  144. BOOST_UBLAS_INLINE
  145. reference operator () (size_type i, size_type j) {
  146. BOOST_UBLAS_CHECK (i < size_, bad_index ());
  147. BOOST_UBLAS_CHECK (j < size_, bad_index ());
  148. if (triangular_type::other (i, j))
  149. return data () [triangular_type::element (layout_type (), i, size_, j, size_)];
  150. else
  151. return data () [triangular_type::element (layout_type (), j, size_, i, size_)];
  152. }
  153. // Element assignment
  154. BOOST_UBLAS_INLINE
  155. reference insert_element (size_type i, size_type j, const_reference t) {
  156. return (operator () (i, j) = t);
  157. }
  158. BOOST_UBLAS_INLINE
  159. void erase_element (size_type i, size_type j) {
  160. operator () (i, j) = value_type/*zero*/();
  161. }
  162. // Zeroing
  163. BOOST_UBLAS_INLINE
  164. void clear () {
  165. // data ().clear ();
  166. std::fill (data ().begin (), data ().end (), value_type/*zero*/());
  167. }
  168. // Assignment
  169. BOOST_UBLAS_INLINE
  170. symmetric_matrix &operator = (const symmetric_matrix &m) {
  171. size_ = m.size_;
  172. data () = m.data ();
  173. return *this;
  174. }
  175. BOOST_UBLAS_INLINE
  176. symmetric_matrix &assign_temporary (symmetric_matrix &m) {
  177. swap (m);
  178. return *this;
  179. }
  180. template<class AE>
  181. BOOST_UBLAS_INLINE
  182. symmetric_matrix &operator = (const matrix_expression<AE> &ae) {
  183. self_type temporary (ae);
  184. return assign_temporary (temporary);
  185. }
  186. template<class AE>
  187. BOOST_UBLAS_INLINE
  188. symmetric_matrix &assign (const matrix_expression<AE> &ae) {
  189. matrix_assign<scalar_assign> (*this, ae);
  190. return *this;
  191. }
  192. template<class AE>
  193. BOOST_UBLAS_INLINE
  194. symmetric_matrix& operator += (const matrix_expression<AE> &ae) {
  195. self_type temporary (*this + ae);
  196. return assign_temporary (temporary);
  197. }
  198. template<class AE>
  199. BOOST_UBLAS_INLINE
  200. symmetric_matrix &plus_assign (const matrix_expression<AE> &ae) {
  201. matrix_assign<scalar_plus_assign> (*this, ae);
  202. return *this;
  203. }
  204. template<class AE>
  205. BOOST_UBLAS_INLINE
  206. symmetric_matrix& operator -= (const matrix_expression<AE> &ae) {
  207. self_type temporary (*this - ae);
  208. return assign_temporary (temporary);
  209. }
  210. template<class AE>
  211. BOOST_UBLAS_INLINE
  212. symmetric_matrix &minus_assign (const matrix_expression<AE> &ae) {
  213. matrix_assign<scalar_minus_assign> (*this, ae);
  214. return *this;
  215. }
  216. template<class AT>
  217. BOOST_UBLAS_INLINE
  218. symmetric_matrix& operator *= (const AT &at) {
  219. matrix_assign_scalar<scalar_multiplies_assign> (*this, at);
  220. return *this;
  221. }
  222. template<class AT>
  223. BOOST_UBLAS_INLINE
  224. symmetric_matrix& operator /= (const AT &at) {
  225. matrix_assign_scalar<scalar_divides_assign> (*this, at);
  226. return *this;
  227. }
  228. // Swapping
  229. BOOST_UBLAS_INLINE
  230. void swap (symmetric_matrix &m) {
  231. if (this != &m) {
  232. std::swap (size_, m.size_);
  233. data ().swap (m.data ());
  234. }
  235. }
  236. BOOST_UBLAS_INLINE
  237. friend void swap (symmetric_matrix &m1, symmetric_matrix &m2) {
  238. m1.swap (m2);
  239. }
  240. // Iterator types
  241. #ifdef BOOST_UBLAS_USE_INDEXED_ITERATOR
  242. typedef indexed_iterator1<self_type, packed_random_access_iterator_tag> iterator1;
  243. typedef indexed_iterator2<self_type, packed_random_access_iterator_tag> iterator2;
  244. typedef indexed_const_iterator1<self_type, dense_random_access_iterator_tag> const_iterator1;
  245. typedef indexed_const_iterator2<self_type, dense_random_access_iterator_tag> const_iterator2;
  246. #else
  247. class const_iterator1;
  248. class iterator1;
  249. class const_iterator2;
  250. class iterator2;
  251. #endif
  252. typedef reverse_iterator_base1<const_iterator1> const_reverse_iterator1;
  253. typedef reverse_iterator_base1<iterator1> reverse_iterator1;
  254. typedef reverse_iterator_base2<const_iterator2> const_reverse_iterator2;
  255. typedef reverse_iterator_base2<iterator2> reverse_iterator2;
  256. // Element lookup
  257. BOOST_UBLAS_INLINE
  258. const_iterator1 find1 (int /* rank */, size_type i, size_type j) const {
  259. return const_iterator1 (*this, i, j);
  260. }
  261. BOOST_UBLAS_INLINE
  262. iterator1 find1 (int rank, size_type i, size_type j) {
  263. if (rank == 1)
  264. i = triangular_type::mutable_restrict1 (i, j, size1(), size2());
  265. if (rank == 0)
  266. i = triangular_type::global_mutable_restrict1 (i, size1(), j, size2());
  267. return iterator1 (*this, i, j);
  268. }
  269. BOOST_UBLAS_INLINE
  270. const_iterator2 find2 (int /* rank */, size_type i, size_type j) const {
  271. return const_iterator2 (*this, i, j);
  272. }
  273. BOOST_UBLAS_INLINE
  274. iterator2 find2 (int rank, size_type i, size_type j) {
  275. if (rank == 1)
  276. j = triangular_type::mutable_restrict2 (i, j, size1(), size2());
  277. if (rank == 0)
  278. j = triangular_type::global_mutable_restrict2 (i, size1(), j, size2());
  279. return iterator2 (*this, i, j);
  280. }
  281. // Iterators simply are indices.
  282. #ifndef BOOST_UBLAS_USE_INDEXED_ITERATOR
  283. class const_iterator1:
  284. public container_const_reference<symmetric_matrix>,
  285. public random_access_iterator_base<dense_random_access_iterator_tag,
  286. const_iterator1, value_type> {
  287. public:
  288. typedef typename symmetric_matrix::value_type value_type;
  289. typedef typename symmetric_matrix::difference_type difference_type;
  290. typedef typename symmetric_matrix::const_reference reference;
  291. typedef const typename symmetric_matrix::pointer pointer;
  292. typedef const_iterator2 dual_iterator_type;
  293. typedef const_reverse_iterator2 dual_reverse_iterator_type;
  294. // Construction and destruction
  295. BOOST_UBLAS_INLINE
  296. const_iterator1 ():
  297. container_const_reference<self_type> (), it1_ (), it2_ () {}
  298. BOOST_UBLAS_INLINE
  299. const_iterator1 (const self_type &m, size_type it1, size_type it2):
  300. container_const_reference<self_type> (m), it1_ (it1), it2_ (it2) {}
  301. BOOST_UBLAS_INLINE
  302. const_iterator1 (const iterator1 &it):
  303. container_const_reference<self_type> (it ()), it1_ (it.it1_), it2_ (it.it2_) {}
  304. // Arithmetic
  305. BOOST_UBLAS_INLINE
  306. const_iterator1 &operator ++ () {
  307. ++ it1_;
  308. return *this;
  309. }
  310. BOOST_UBLAS_INLINE
  311. const_iterator1 &operator -- () {
  312. -- it1_;
  313. return *this;
  314. }
  315. BOOST_UBLAS_INLINE
  316. const_iterator1 &operator += (difference_type n) {
  317. it1_ += n;
  318. return *this;
  319. }
  320. BOOST_UBLAS_INLINE
  321. const_iterator1 &operator -= (difference_type n) {
  322. it1_ -= n;
  323. return *this;
  324. }
  325. BOOST_UBLAS_INLINE
  326. difference_type operator - (const const_iterator1 &it) const {
  327. BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ());
  328. BOOST_UBLAS_CHECK (it2_ == it.it2_, external_logic ());
  329. return it1_ - it.it1_;
  330. }
  331. // Dereference
  332. BOOST_UBLAS_INLINE
  333. const_reference operator * () const {
  334. return (*this) () (it1_, it2_);
  335. }
  336. BOOST_UBLAS_INLINE
  337. const_reference operator [] (difference_type n) const {
  338. return *(*this + n);
  339. }
  340. #ifndef BOOST_UBLAS_NO_NESTED_CLASS_RELATION
  341. BOOST_UBLAS_INLINE
  342. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  343. typename self_type::
  344. #endif
  345. const_iterator2 begin () const {
  346. return (*this) ().find2 (1, it1_, 0);
  347. }
  348. BOOST_UBLAS_INLINE
  349. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  350. typename self_type::
  351. #endif
  352. const_iterator2 end () const {
  353. return (*this) ().find2 (1, it1_, (*this) ().size2 ());
  354. }
  355. BOOST_UBLAS_INLINE
  356. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  357. typename self_type::
  358. #endif
  359. const_reverse_iterator2 rbegin () const {
  360. return const_reverse_iterator2 (end ());
  361. }
  362. BOOST_UBLAS_INLINE
  363. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  364. typename self_type::
  365. #endif
  366. const_reverse_iterator2 rend () const {
  367. return const_reverse_iterator2 (begin ());
  368. }
  369. #endif
  370. // Indices
  371. BOOST_UBLAS_INLINE
  372. size_type index1 () const {
  373. return it1_;
  374. }
  375. BOOST_UBLAS_INLINE
  376. size_type index2 () const {
  377. return it2_;
  378. }
  379. // Assignment
  380. BOOST_UBLAS_INLINE
  381. const_iterator1 &operator = (const const_iterator1 &it) {
  382. container_const_reference<self_type>::assign (&it ());
  383. it1_ = it.it1_;
  384. it2_ = it.it2_;
  385. return *this;
  386. }
  387. // Comparison
  388. BOOST_UBLAS_INLINE
  389. bool operator == (const const_iterator1 &it) const {
  390. BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ());
  391. BOOST_UBLAS_CHECK (it2_ == it.it2_, external_logic ());
  392. return it1_ == it.it1_;
  393. }
  394. BOOST_UBLAS_INLINE
  395. bool operator < (const const_iterator1 &it) const {
  396. BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ());
  397. BOOST_UBLAS_CHECK (it2_ == it.it2_, external_logic ());
  398. return it1_ < it.it1_;
  399. }
  400. private:
  401. size_type it1_;
  402. size_type it2_;
  403. };
  404. #endif
  405. BOOST_UBLAS_INLINE
  406. const_iterator1 begin1 () const {
  407. return find1 (0, 0, 0);
  408. }
  409. BOOST_UBLAS_INLINE
  410. const_iterator1 end1 () const {
  411. return find1 (0, size_, 0);
  412. }
  413. #ifndef BOOST_UBLAS_USE_INDEXED_ITERATOR
  414. class iterator1:
  415. public container_reference<symmetric_matrix>,
  416. public random_access_iterator_base<packed_random_access_iterator_tag,
  417. iterator1, value_type> {
  418. public:
  419. typedef typename symmetric_matrix::value_type value_type;
  420. typedef typename symmetric_matrix::difference_type difference_type;
  421. typedef typename symmetric_matrix::reference reference;
  422. typedef typename symmetric_matrix::pointer pointer;
  423. typedef iterator2 dual_iterator_type;
  424. typedef reverse_iterator2 dual_reverse_iterator_type;
  425. // Construction and destruction
  426. BOOST_UBLAS_INLINE
  427. iterator1 ():
  428. container_reference<self_type> (), it1_ (), it2_ () {}
  429. BOOST_UBLAS_INLINE
  430. iterator1 (self_type &m, size_type it1, size_type it2):
  431. container_reference<self_type> (m), it1_ (it1), it2_ (it2) {}
  432. // Arithmetic
  433. BOOST_UBLAS_INLINE
  434. iterator1 &operator ++ () {
  435. ++ it1_;
  436. return *this;
  437. }
  438. BOOST_UBLAS_INLINE
  439. iterator1 &operator -- () {
  440. -- it1_;
  441. return *this;
  442. }
  443. BOOST_UBLAS_INLINE
  444. iterator1 &operator += (difference_type n) {
  445. it1_ += n;
  446. return *this;
  447. }
  448. BOOST_UBLAS_INLINE
  449. iterator1 &operator -= (difference_type n) {
  450. it1_ -= n;
  451. return *this;
  452. }
  453. BOOST_UBLAS_INLINE
  454. difference_type operator - (const iterator1 &it) const {
  455. BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ());
  456. BOOST_UBLAS_CHECK (it2_ == it.it2_, external_logic ());
  457. return it1_ - it.it1_;
  458. }
  459. // Dereference
  460. BOOST_UBLAS_INLINE
  461. reference operator * () const {
  462. return (*this) () (it1_, it2_);
  463. }
  464. BOOST_UBLAS_INLINE
  465. reference operator [] (difference_type n) const {
  466. return *(*this + n);
  467. }
  468. #ifndef BOOST_UBLAS_NO_NESTED_CLASS_RELATION
  469. BOOST_UBLAS_INLINE
  470. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  471. typename self_type::
  472. #endif
  473. iterator2 begin () const {
  474. return (*this) ().find2 (1, it1_, 0);
  475. }
  476. BOOST_UBLAS_INLINE
  477. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  478. typename self_type::
  479. #endif
  480. iterator2 end () const {
  481. return (*this) ().find2 (1, it1_, (*this) ().size2 ());
  482. }
  483. BOOST_UBLAS_INLINE
  484. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  485. typename self_type::
  486. #endif
  487. reverse_iterator2 rbegin () const {
  488. return reverse_iterator2 (end ());
  489. }
  490. BOOST_UBLAS_INLINE
  491. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  492. typename self_type::
  493. #endif
  494. reverse_iterator2 rend () const {
  495. return reverse_iterator2 (begin ());
  496. }
  497. #endif
  498. // Indices
  499. BOOST_UBLAS_INLINE
  500. size_type index1 () const {
  501. return it1_;
  502. }
  503. BOOST_UBLAS_INLINE
  504. size_type index2 () const {
  505. return it2_;
  506. }
  507. // Assignment
  508. BOOST_UBLAS_INLINE
  509. iterator1 &operator = (const iterator1 &it) {
  510. container_reference<self_type>::assign (&it ());
  511. it1_ = it.it1_;
  512. it2_ = it.it2_;
  513. return *this;
  514. }
  515. // Comparison
  516. BOOST_UBLAS_INLINE
  517. bool operator == (const iterator1 &it) const {
  518. BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ());
  519. BOOST_UBLAS_CHECK (it2_ == it.it2_, external_logic ());
  520. return it1_ == it.it1_;
  521. }
  522. BOOST_UBLAS_INLINE
  523. bool operator < (const iterator1 &it) const {
  524. BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ());
  525. BOOST_UBLAS_CHECK (it2_ == it.it2_, external_logic ());
  526. return it1_ < it.it1_;
  527. }
  528. private:
  529. size_type it1_;
  530. size_type it2_;
  531. friend class const_iterator1;
  532. };
  533. #endif
  534. BOOST_UBLAS_INLINE
  535. iterator1 begin1 () {
  536. return find1 (0, 0, 0);
  537. }
  538. BOOST_UBLAS_INLINE
  539. iterator1 end1 () {
  540. return find1 (0, size_, 0);
  541. }
  542. #ifndef BOOST_UBLAS_USE_INDEXED_ITERATOR
  543. class const_iterator2:
  544. public container_const_reference<symmetric_matrix>,
  545. public random_access_iterator_base<dense_random_access_iterator_tag,
  546. const_iterator2, value_type> {
  547. public:
  548. typedef typename symmetric_matrix::value_type value_type;
  549. typedef typename symmetric_matrix::difference_type difference_type;
  550. typedef typename symmetric_matrix::const_reference reference;
  551. typedef const typename symmetric_matrix::pointer pointer;
  552. typedef const_iterator1 dual_iterator_type;
  553. typedef const_reverse_iterator1 dual_reverse_iterator_type;
  554. // Construction and destruction
  555. BOOST_UBLAS_INLINE
  556. const_iterator2 ():
  557. container_const_reference<self_type> (), it1_ (), it2_ () {}
  558. BOOST_UBLAS_INLINE
  559. const_iterator2 (const self_type &m, size_type it1, size_type it2):
  560. container_const_reference<self_type> (m), it1_ (it1), it2_ (it2) {}
  561. BOOST_UBLAS_INLINE
  562. const_iterator2 (const iterator2 &it):
  563. container_const_reference<self_type> (it ()), it1_ (it.it1_), it2_ (it.it2_) {}
  564. // Arithmetic
  565. BOOST_UBLAS_INLINE
  566. const_iterator2 &operator ++ () {
  567. ++ it2_;
  568. return *this;
  569. }
  570. BOOST_UBLAS_INLINE
  571. const_iterator2 &operator -- () {
  572. -- it2_;
  573. return *this;
  574. }
  575. BOOST_UBLAS_INLINE
  576. const_iterator2 &operator += (difference_type n) {
  577. it2_ += n;
  578. return *this;
  579. }
  580. BOOST_UBLAS_INLINE
  581. const_iterator2 &operator -= (difference_type n) {
  582. it2_ -= n;
  583. return *this;
  584. }
  585. BOOST_UBLAS_INLINE
  586. difference_type operator - (const const_iterator2 &it) const {
  587. BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ());
  588. BOOST_UBLAS_CHECK (it1_ == it.it1_, external_logic ());
  589. return it2_ - it.it2_;
  590. }
  591. // Dereference
  592. BOOST_UBLAS_INLINE
  593. const_reference operator * () const {
  594. return (*this) () (it1_, it2_);
  595. }
  596. BOOST_UBLAS_INLINE
  597. const_reference operator [] (difference_type n) const {
  598. return *(*this + n);
  599. }
  600. #ifndef BOOST_UBLAS_NO_NESTED_CLASS_RELATION
  601. BOOST_UBLAS_INLINE
  602. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  603. typename self_type::
  604. #endif
  605. const_iterator1 begin () const {
  606. return (*this) ().find1 (1, 0, it2_);
  607. }
  608. BOOST_UBLAS_INLINE
  609. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  610. typename self_type::
  611. #endif
  612. const_iterator1 end () const {
  613. return (*this) ().find1 (1, (*this) ().size1 (), it2_);
  614. }
  615. BOOST_UBLAS_INLINE
  616. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  617. typename self_type::
  618. #endif
  619. const_reverse_iterator1 rbegin () const {
  620. return const_reverse_iterator1 (end ());
  621. }
  622. BOOST_UBLAS_INLINE
  623. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  624. typename self_type::
  625. #endif
  626. const_reverse_iterator1 rend () const {
  627. return const_reverse_iterator1 (begin ());
  628. }
  629. #endif
  630. // Indices
  631. BOOST_UBLAS_INLINE
  632. size_type index1 () const {
  633. return it1_;
  634. }
  635. BOOST_UBLAS_INLINE
  636. size_type index2 () const {
  637. return it2_;
  638. }
  639. // Assignment
  640. BOOST_UBLAS_INLINE
  641. const_iterator2 &operator = (const const_iterator2 &it) {
  642. container_const_reference<self_type>::assign (&it ());
  643. it1_ = it.it1_;
  644. it2_ = it.it2_;
  645. return *this;
  646. }
  647. // Comparison
  648. BOOST_UBLAS_INLINE
  649. bool operator == (const const_iterator2 &it) const {
  650. BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ());
  651. BOOST_UBLAS_CHECK (it1_ == it.it1_, external_logic ());
  652. return it2_ == it.it2_;
  653. }
  654. BOOST_UBLAS_INLINE
  655. bool operator < (const const_iterator2 &it) const {
  656. BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ());
  657. BOOST_UBLAS_CHECK (it1_ == it.it1_, external_logic ());
  658. return it2_ < it.it2_;
  659. }
  660. private:
  661. size_type it1_;
  662. size_type it2_;
  663. };
  664. #endif
  665. BOOST_UBLAS_INLINE
  666. const_iterator2 begin2 () const {
  667. return find2 (0, 0, 0);
  668. }
  669. BOOST_UBLAS_INLINE
  670. const_iterator2 end2 () const {
  671. return find2 (0, 0, size_);
  672. }
  673. #ifndef BOOST_UBLAS_USE_INDEXED_ITERATOR
  674. class iterator2:
  675. public container_reference<symmetric_matrix>,
  676. public random_access_iterator_base<packed_random_access_iterator_tag,
  677. iterator2, value_type> {
  678. public:
  679. typedef typename symmetric_matrix::value_type value_type;
  680. typedef typename symmetric_matrix::difference_type difference_type;
  681. typedef typename symmetric_matrix::reference reference;
  682. typedef typename symmetric_matrix::pointer pointer;
  683. typedef iterator1 dual_iterator_type;
  684. typedef reverse_iterator1 dual_reverse_iterator_type;
  685. // Construction and destruction
  686. BOOST_UBLAS_INLINE
  687. iterator2 ():
  688. container_reference<self_type> (), it1_ (), it2_ () {}
  689. BOOST_UBLAS_INLINE
  690. iterator2 (self_type &m, size_type it1, size_type it2):
  691. container_reference<self_type> (m), it1_ (it1), it2_ (it2) {}
  692. // Arithmetic
  693. BOOST_UBLAS_INLINE
  694. iterator2 &operator ++ () {
  695. ++ it2_;
  696. return *this;
  697. }
  698. BOOST_UBLAS_INLINE
  699. iterator2 &operator -- () {
  700. -- it2_;
  701. return *this;
  702. }
  703. BOOST_UBLAS_INLINE
  704. iterator2 &operator += (difference_type n) {
  705. it2_ += n;
  706. return *this;
  707. }
  708. BOOST_UBLAS_INLINE
  709. iterator2 &operator -= (difference_type n) {
  710. it2_ -= n;
  711. return *this;
  712. }
  713. BOOST_UBLAS_INLINE
  714. difference_type operator - (const iterator2 &it) const {
  715. BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ());
  716. BOOST_UBLAS_CHECK (it1_ == it.it1_, external_logic ());
  717. return it2_ - it.it2_;
  718. }
  719. // Dereference
  720. BOOST_UBLAS_INLINE
  721. reference operator * () const {
  722. return (*this) () (it1_, it2_);
  723. }
  724. BOOST_UBLAS_INLINE
  725. reference operator [] (difference_type n) const {
  726. return *(*this + n);
  727. }
  728. #ifndef BOOST_UBLAS_NO_NESTED_CLASS_RELATION
  729. BOOST_UBLAS_INLINE
  730. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  731. typename self_type::
  732. #endif
  733. iterator1 begin () const {
  734. return (*this) ().find1 (1, 0, it2_);
  735. }
  736. BOOST_UBLAS_INLINE
  737. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  738. typename self_type::
  739. #endif
  740. iterator1 end () const {
  741. return (*this) ().find1 (1, (*this) ().size1 (), it2_);
  742. }
  743. BOOST_UBLAS_INLINE
  744. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  745. typename self_type::
  746. #endif
  747. reverse_iterator1 rbegin () const {
  748. return reverse_iterator1 (end ());
  749. }
  750. BOOST_UBLAS_INLINE
  751. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  752. typename self_type::
  753. #endif
  754. reverse_iterator1 rend () const {
  755. return reverse_iterator1 (begin ());
  756. }
  757. #endif
  758. // Indices
  759. BOOST_UBLAS_INLINE
  760. size_type index1 () const {
  761. return it1_;
  762. }
  763. BOOST_UBLAS_INLINE
  764. size_type index2 () const {
  765. return it2_;
  766. }
  767. // Assignment
  768. BOOST_UBLAS_INLINE
  769. iterator2 &operator = (const iterator2 &it) {
  770. container_reference<self_type>::assign (&it ());
  771. it1_ = it.it1_;
  772. it2_ = it.it2_;
  773. return *this;
  774. }
  775. // Comparison
  776. BOOST_UBLAS_INLINE
  777. bool operator == (const iterator2 &it) const {
  778. BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ());
  779. BOOST_UBLAS_CHECK (it1_ == it.it1_, external_logic ());
  780. return it2_ == it.it2_;
  781. }
  782. BOOST_UBLAS_INLINE
  783. bool operator < (const iterator2 &it) const {
  784. BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ());
  785. BOOST_UBLAS_CHECK (it1_ == it.it1_, external_logic ());
  786. return it2_ < it.it2_;
  787. }
  788. private:
  789. size_type it1_;
  790. size_type it2_;
  791. friend class const_iterator2;
  792. };
  793. #endif
  794. BOOST_UBLAS_INLINE
  795. iterator2 begin2 () {
  796. return find2 (0, 0, 0);
  797. }
  798. BOOST_UBLAS_INLINE
  799. iterator2 end2 () {
  800. return find2 (0, 0, size_);
  801. }
  802. // Reverse iterators
  803. BOOST_UBLAS_INLINE
  804. const_reverse_iterator1 rbegin1 () const {
  805. return const_reverse_iterator1 (end1 ());
  806. }
  807. BOOST_UBLAS_INLINE
  808. const_reverse_iterator1 rend1 () const {
  809. return const_reverse_iterator1 (begin1 ());
  810. }
  811. BOOST_UBLAS_INLINE
  812. reverse_iterator1 rbegin1 () {
  813. return reverse_iterator1 (end1 ());
  814. }
  815. BOOST_UBLAS_INLINE
  816. reverse_iterator1 rend1 () {
  817. return reverse_iterator1 (begin1 ());
  818. }
  819. BOOST_UBLAS_INLINE
  820. const_reverse_iterator2 rbegin2 () const {
  821. return const_reverse_iterator2 (end2 ());
  822. }
  823. BOOST_UBLAS_INLINE
  824. const_reverse_iterator2 rend2 () const {
  825. return const_reverse_iterator2 (begin2 ());
  826. }
  827. BOOST_UBLAS_INLINE
  828. reverse_iterator2 rbegin2 () {
  829. return reverse_iterator2 (end2 ());
  830. }
  831. BOOST_UBLAS_INLINE
  832. reverse_iterator2 rend2 () {
  833. return reverse_iterator2 (begin2 ());
  834. }
  835. private:
  836. size_type size_;
  837. array_type data_;
  838. };
  839. // Symmetric matrix adaptor class
  840. template<class M, class TRI>
  841. class symmetric_adaptor:
  842. public matrix_expression<symmetric_adaptor<M, TRI> > {
  843. typedef symmetric_adaptor<M, TRI> self_type;
  844. public:
  845. #ifdef BOOST_UBLAS_ENABLE_PROXY_SHORTCUTS
  846. using matrix_expression<self_type>::operator ();
  847. #endif
  848. typedef const M const_matrix_type;
  849. typedef M matrix_type;
  850. typedef TRI triangular_type;
  851. typedef typename M::size_type size_type;
  852. typedef typename M::difference_type difference_type;
  853. typedef typename M::value_type value_type;
  854. typedef typename M::const_reference const_reference;
  855. typedef typename boost::mpl::if_<boost::is_const<M>,
  856. typename M::const_reference,
  857. typename M::reference>::type reference;
  858. typedef typename boost::mpl::if_<boost::is_const<M>,
  859. typename M::const_closure_type,
  860. typename M::closure_type>::type matrix_closure_type;
  861. typedef const self_type const_closure_type;
  862. typedef self_type closure_type;
  863. // Replaced by _temporary_traits to avoid type requirements on M
  864. //typedef typename M::vector_temporary_type vector_temporary_type;
  865. //typedef typename M::matrix_temporary_type matrix_temporary_type;
  866. typedef typename storage_restrict_traits<typename M::storage_category,
  867. packed_proxy_tag>::storage_category storage_category;
  868. typedef typename M::orientation_category orientation_category;
  869. // Construction and destruction
  870. BOOST_UBLAS_INLINE
  871. symmetric_adaptor (matrix_type &data):
  872. matrix_expression<self_type> (),
  873. data_ (data) {
  874. BOOST_UBLAS_CHECK (data_.size1 () == data_.size2 (), bad_size ());
  875. }
  876. BOOST_UBLAS_INLINE
  877. symmetric_adaptor (const symmetric_adaptor &m):
  878. matrix_expression<self_type> (),
  879. data_ (m.data_) {
  880. BOOST_UBLAS_CHECK (data_.size1 () == data_.size2 (), bad_size ());
  881. }
  882. // Accessors
  883. BOOST_UBLAS_INLINE
  884. size_type size1 () const {
  885. return data_.size1 ();
  886. }
  887. BOOST_UBLAS_INLINE
  888. size_type size2 () const {
  889. return data_.size2 ();
  890. }
  891. // Storage accessors
  892. BOOST_UBLAS_INLINE
  893. const matrix_closure_type &data () const {
  894. return data_;
  895. }
  896. BOOST_UBLAS_INLINE
  897. matrix_closure_type &data () {
  898. return data_;
  899. }
  900. // Element access
  901. #ifndef BOOST_UBLAS_PROXY_CONST_MEMBER
  902. BOOST_UBLAS_INLINE
  903. const_reference operator () (size_type i, size_type j) const {
  904. BOOST_UBLAS_CHECK (i < size1 (), bad_index ());
  905. BOOST_UBLAS_CHECK (j < size2 (), bad_index ());
  906. if (triangular_type::other (i, j))
  907. return data () (i, j);
  908. else
  909. return data () (j, i);
  910. }
  911. BOOST_UBLAS_INLINE
  912. reference operator () (size_type i, size_type j) {
  913. BOOST_UBLAS_CHECK (i < size1 (), bad_index ());
  914. BOOST_UBLAS_CHECK (j < size2 (), bad_index ());
  915. if (triangular_type::other (i, j))
  916. return data () (i, j);
  917. else
  918. return data () (j, i);
  919. }
  920. #else
  921. BOOST_UBLAS_INLINE
  922. reference operator () (size_type i, size_type j) const {
  923. BOOST_UBLAS_CHECK (i < size1 (), bad_index ());
  924. BOOST_UBLAS_CHECK (j < size2 (), bad_index ());
  925. if (triangular_type::other (i, j))
  926. return data () (i, j);
  927. else
  928. return data () (j, i);
  929. }
  930. #endif
  931. // Assignment
  932. BOOST_UBLAS_INLINE
  933. symmetric_adaptor &operator = (const symmetric_adaptor &m) {
  934. matrix_assign<scalar_assign, triangular_type> (*this, m);
  935. return *this;
  936. }
  937. BOOST_UBLAS_INLINE
  938. symmetric_adaptor &assign_temporary (symmetric_adaptor &m) {
  939. *this = m;
  940. return *this;
  941. }
  942. template<class AE>
  943. BOOST_UBLAS_INLINE
  944. symmetric_adaptor &operator = (const matrix_expression<AE> &ae) {
  945. matrix_assign<scalar_assign, triangular_type> (*this, matrix<value_type> (ae));
  946. return *this;
  947. }
  948. template<class AE>
  949. BOOST_UBLAS_INLINE
  950. symmetric_adaptor &assign (const matrix_expression<AE> &ae) {
  951. matrix_assign<scalar_assign, triangular_type> (*this, ae);
  952. return *this;
  953. }
  954. template<class AE>
  955. BOOST_UBLAS_INLINE
  956. symmetric_adaptor& operator += (const matrix_expression<AE> &ae) {
  957. matrix_assign<scalar_assign, triangular_type> (*this, matrix<value_type> (*this + ae));
  958. return *this;
  959. }
  960. template<class AE>
  961. BOOST_UBLAS_INLINE
  962. symmetric_adaptor &plus_assign (const matrix_expression<AE> &ae) {
  963. matrix_assign<scalar_plus_assign, triangular_type> (*this, ae);
  964. return *this;
  965. }
  966. template<class AE>
  967. BOOST_UBLAS_INLINE
  968. symmetric_adaptor& operator -= (const matrix_expression<AE> &ae) {
  969. matrix_assign<scalar_assign, triangular_type> (*this, matrix<value_type> (*this - ae));
  970. return *this;
  971. }
  972. template<class AE>
  973. BOOST_UBLAS_INLINE
  974. symmetric_adaptor &minus_assign (const matrix_expression<AE> &ae) {
  975. matrix_assign<scalar_minus_assign, triangular_type> (*this, ae);
  976. return *this;
  977. }
  978. template<class AT>
  979. BOOST_UBLAS_INLINE
  980. symmetric_adaptor& operator *= (const AT &at) {
  981. matrix_assign_scalar<scalar_multiplies_assign> (*this, at);
  982. return *this;
  983. }
  984. template<class AT>
  985. BOOST_UBLAS_INLINE
  986. symmetric_adaptor& operator /= (const AT &at) {
  987. matrix_assign_scalar<scalar_divides_assign> (*this, at);
  988. return *this;
  989. }
  990. // Closure comparison
  991. BOOST_UBLAS_INLINE
  992. bool same_closure (const symmetric_adaptor &sa) const {
  993. return (*this).data ().same_closure (sa.data ());
  994. }
  995. // Swapping
  996. BOOST_UBLAS_INLINE
  997. void swap (symmetric_adaptor &m) {
  998. if (this != &m)
  999. matrix_swap<scalar_swap, triangular_type> (*this, m);
  1000. }
  1001. BOOST_UBLAS_INLINE
  1002. friend void swap (symmetric_adaptor &m1, symmetric_adaptor &m2) {
  1003. m1.swap (m2);
  1004. }
  1005. // Iterator types
  1006. private:
  1007. // Use matrix iterator
  1008. typedef typename M::const_iterator1 const_subiterator1_type;
  1009. typedef typename boost::mpl::if_<boost::is_const<M>,
  1010. typename M::const_iterator1,
  1011. typename M::iterator1>::type subiterator1_type;
  1012. typedef typename M::const_iterator2 const_subiterator2_type;
  1013. typedef typename boost::mpl::if_<boost::is_const<M>,
  1014. typename M::const_iterator2,
  1015. typename M::iterator2>::type subiterator2_type;
  1016. public:
  1017. #ifdef BOOST_UBLAS_USE_INDEXED_ITERATOR
  1018. typedef indexed_iterator1<self_type, packed_random_access_iterator_tag> iterator1;
  1019. typedef indexed_iterator2<self_type, packed_random_access_iterator_tag> iterator2;
  1020. typedef indexed_const_iterator1<self_type, dense_random_access_iterator_tag> const_iterator1;
  1021. typedef indexed_const_iterator2<self_type, dense_random_access_iterator_tag> const_iterator2;
  1022. #else
  1023. class const_iterator1;
  1024. class iterator1;
  1025. class const_iterator2;
  1026. class iterator2;
  1027. #endif
  1028. typedef reverse_iterator_base1<const_iterator1> const_reverse_iterator1;
  1029. typedef reverse_iterator_base1<iterator1> reverse_iterator1;
  1030. typedef reverse_iterator_base2<const_iterator2> const_reverse_iterator2;
  1031. typedef reverse_iterator_base2<iterator2> reverse_iterator2;
  1032. // Element lookup
  1033. BOOST_UBLAS_INLINE
  1034. const_iterator1 find1 (int rank, size_type i, size_type j) const {
  1035. if (triangular_type::other (i, j)) {
  1036. if (triangular_type::other (size1 (), j)) {
  1037. return const_iterator1 (*this, 0, 0,
  1038. data ().find1 (rank, i, j), data ().find1 (rank, size1 (), j),
  1039. data ().find2 (rank, size2 (), size1 ()), data ().find2 (rank, size2 (), size1 ()));
  1040. } else {
  1041. return const_iterator1 (*this, 0, 1,
  1042. data ().find1 (rank, i, j), data ().find1 (rank, j, j),
  1043. data ().find2 (rank, j, j), data ().find2 (rank, j, size1 ()));
  1044. }
  1045. } else {
  1046. if (triangular_type::other (size1 (), j)) {
  1047. return const_iterator1 (*this, 1, 0,
  1048. data ().find1 (rank, j, j), data ().find1 (rank, size1 (), j),
  1049. data ().find2 (rank, j, i), data ().find2 (rank, j, j));
  1050. } else {
  1051. return const_iterator1 (*this, 1, 1,
  1052. data ().find1 (rank, size1 (), size2 ()), data ().find1 (rank, size1 (), size2 ()),
  1053. data ().find2 (rank, j, i), data ().find2 (rank, j, size1 ()));
  1054. }
  1055. }
  1056. }
  1057. BOOST_UBLAS_INLINE
  1058. iterator1 find1 (int rank, size_type i, size_type j) {
  1059. if (rank == 1)
  1060. i = triangular_type::mutable_restrict1 (i, j, size1(), size2());
  1061. return iterator1 (*this, data ().find1 (rank, i, j));
  1062. }
  1063. BOOST_UBLAS_INLINE
  1064. const_iterator2 find2 (int rank, size_type i, size_type j) const {
  1065. if (triangular_type::other (i, j)) {
  1066. if (triangular_type::other (i, size2 ())) {
  1067. return const_iterator2 (*this, 1, 1,
  1068. data ().find1 (rank, size2 (), size1 ()), data ().find1 (rank, size2 (), size1 ()),
  1069. data ().find2 (rank, i, j), data ().find2 (rank, i, size2 ()));
  1070. } else {
  1071. return const_iterator2 (*this, 1, 0,
  1072. data ().find1 (rank, i, i), data ().find1 (rank, size2 (), i),
  1073. data ().find2 (rank, i, j), data ().find2 (rank, i, i));
  1074. }
  1075. } else {
  1076. if (triangular_type::other (i, size2 ())) {
  1077. return const_iterator2 (*this, 0, 1,
  1078. data ().find1 (rank, j, i), data ().find1 (rank, i, i),
  1079. data ().find2 (rank, i, i), data ().find2 (rank, i, size2 ()));
  1080. } else {
  1081. return const_iterator2 (*this, 0, 0,
  1082. data ().find1 (rank, j, i), data ().find1 (rank, size2 (), i),
  1083. data ().find2 (rank, size1 (), size2 ()), data ().find2 (rank, size2 (), size2 ()));
  1084. }
  1085. }
  1086. }
  1087. BOOST_UBLAS_INLINE
  1088. iterator2 find2 (int rank, size_type i, size_type j) {
  1089. if (rank == 1)
  1090. j = triangular_type::mutable_restrict2 (i, j, size1(), size2());
  1091. return iterator2 (*this, data ().find2 (rank, i, j));
  1092. }
  1093. // Iterators simply are indices.
  1094. #ifndef BOOST_UBLAS_USE_INDEXED_ITERATOR
  1095. class const_iterator1:
  1096. public container_const_reference<symmetric_adaptor>,
  1097. public random_access_iterator_base<typename iterator_restrict_traits<
  1098. typename const_subiterator1_type::iterator_category, dense_random_access_iterator_tag>::iterator_category,
  1099. const_iterator1, value_type> {
  1100. public:
  1101. typedef typename const_subiterator1_type::value_type value_type;
  1102. typedef typename const_subiterator1_type::difference_type difference_type;
  1103. typedef typename const_subiterator1_type::reference reference;
  1104. typedef typename const_subiterator1_type::pointer pointer;
  1105. typedef const_iterator2 dual_iterator_type;
  1106. typedef const_reverse_iterator2 dual_reverse_iterator_type;
  1107. // Construction and destruction
  1108. BOOST_UBLAS_INLINE
  1109. const_iterator1 ():
  1110. container_const_reference<self_type> (),
  1111. begin_ (-1), end_ (-1), current_ (-1),
  1112. it1_begin_ (), it1_end_ (), it1_ (),
  1113. it2_begin_ (), it2_end_ (), it2_ () {}
  1114. BOOST_UBLAS_INLINE
  1115. const_iterator1 (const self_type &m, int begin, int end,
  1116. const const_subiterator1_type &it1_begin, const const_subiterator1_type &it1_end,
  1117. const const_subiterator2_type &it2_begin, const const_subiterator2_type &it2_end):
  1118. container_const_reference<self_type> (m),
  1119. begin_ (begin), end_ (end), current_ (begin),
  1120. it1_begin_ (it1_begin), it1_end_ (it1_end), it1_ (it1_begin_),
  1121. it2_begin_ (it2_begin), it2_end_ (it2_end), it2_ (it2_begin_) {
  1122. if (current_ == 0 && it1_ == it1_end_)
  1123. current_ = 1;
  1124. if (current_ == 1 && it2_ == it2_end_)
  1125. current_ = 0;
  1126. if ((current_ == 0 && it1_ == it1_end_) ||
  1127. (current_ == 1 && it2_ == it2_end_))
  1128. current_ = end_;
  1129. BOOST_UBLAS_CHECK (current_ == end_ ||
  1130. (current_ == 0 && it1_ != it1_end_) ||
  1131. (current_ == 1 && it2_ != it2_end_), internal_logic ());
  1132. }
  1133. // FIXME cannot compile
  1134. // iterator1 does not have these members!
  1135. BOOST_UBLAS_INLINE
  1136. const_iterator1 (const iterator1 &it):
  1137. container_const_reference<self_type> (it ()),
  1138. begin_ (it.begin_), end_ (it.end_), current_ (it.current_),
  1139. it1_begin_ (it.it1_begin_), it1_end_ (it.it1_end_), it1_ (it.it1_),
  1140. it2_begin_ (it.it2_begin_), it2_end_ (it.it2_end_), it2_ (it.it2_) {
  1141. BOOST_UBLAS_CHECK (current_ == end_ ||
  1142. (current_ == 0 && it1_ != it1_end_) ||
  1143. (current_ == 1 && it2_ != it2_end_), internal_logic ());
  1144. }
  1145. // Arithmetic
  1146. BOOST_UBLAS_INLINE
  1147. const_iterator1 &operator ++ () {
  1148. BOOST_UBLAS_CHECK (current_ == 0 || current_ == 1, internal_logic ());
  1149. if (current_ == 0) {
  1150. BOOST_UBLAS_CHECK (it1_ != it1_end_, internal_logic ());
  1151. ++ it1_;
  1152. if (it1_ == it1_end_ && end_ == 1) {
  1153. it2_ = it2_begin_;
  1154. current_ = 1;
  1155. }
  1156. } else /* if (current_ == 1) */ {
  1157. BOOST_UBLAS_CHECK (it2_ != it2_end_, internal_logic ());
  1158. ++ it2_;
  1159. if (it2_ == it2_end_ && end_ == 0) {
  1160. it1_ = it1_begin_;
  1161. current_ = 0;
  1162. }
  1163. }
  1164. return *this;
  1165. }
  1166. BOOST_UBLAS_INLINE
  1167. const_iterator1 &operator -- () {
  1168. BOOST_UBLAS_CHECK (current_ == 0 || current_ == 1, internal_logic ());
  1169. if (current_ == 0) {
  1170. if (it1_ == it1_begin_ && begin_ == 1) {
  1171. it2_ = it2_end_;
  1172. BOOST_UBLAS_CHECK (it2_ != it2_begin_, internal_logic ());
  1173. -- it2_;
  1174. current_ = 1;
  1175. } else {
  1176. -- it1_;
  1177. }
  1178. } else /* if (current_ == 1) */ {
  1179. if (it2_ == it2_begin_ && begin_ == 0) {
  1180. it1_ = it1_end_;
  1181. BOOST_UBLAS_CHECK (it1_ != it1_begin_, internal_logic ());
  1182. -- it1_;
  1183. current_ = 0;
  1184. } else {
  1185. -- it2_;
  1186. }
  1187. }
  1188. return *this;
  1189. }
  1190. BOOST_UBLAS_INLINE
  1191. const_iterator1 &operator += (difference_type n) {
  1192. BOOST_UBLAS_CHECK (current_ == 0 || current_ == 1, internal_logic ());
  1193. if (current_ == 0) {
  1194. size_type d = (std::min) (n, it1_end_ - it1_);
  1195. it1_ += d;
  1196. n -= d;
  1197. if (n > 0 || (end_ == 1 && it1_ == it1_end_)) {
  1198. BOOST_UBLAS_CHECK (end_ == 1, external_logic ());
  1199. d = (std::min) (n, it2_end_ - it2_begin_);
  1200. it2_ = it2_begin_ + d;
  1201. n -= d;
  1202. current_ = 1;
  1203. }
  1204. } else /* if (current_ == 1) */ {
  1205. size_type d = (std::min) (n, it2_end_ - it2_);
  1206. it2_ += d;
  1207. n -= d;
  1208. if (n > 0 || (end_ == 0 && it2_ == it2_end_)) {
  1209. BOOST_UBLAS_CHECK (end_ == 0, external_logic ());
  1210. d = (std::min) (n, it1_end_ - it1_begin_);
  1211. it1_ = it1_begin_ + d;
  1212. n -= d;
  1213. current_ = 0;
  1214. }
  1215. }
  1216. BOOST_UBLAS_CHECK (n == 0, external_logic ());
  1217. return *this;
  1218. }
  1219. BOOST_UBLAS_INLINE
  1220. const_iterator1 &operator -= (difference_type n) {
  1221. BOOST_UBLAS_CHECK (current_ == 0 || current_ == 1, internal_logic ());
  1222. if (current_ == 0) {
  1223. size_type d = (std::min) (n, it1_ - it1_begin_);
  1224. it1_ -= d;
  1225. n -= d;
  1226. if (n > 0) {
  1227. BOOST_UBLAS_CHECK (end_ == 1, external_logic ());
  1228. d = (std::min) (n, it2_end_ - it2_begin_);
  1229. it2_ = it2_end_ - d;
  1230. n -= d;
  1231. current_ = 1;
  1232. }
  1233. } else /* if (current_ == 1) */ {
  1234. size_type d = (std::min) (n, it2_ - it2_begin_);
  1235. it2_ -= d;
  1236. n -= d;
  1237. if (n > 0) {
  1238. BOOST_UBLAS_CHECK (end_ == 0, external_logic ());
  1239. d = (std::min) (n, it1_end_ - it1_begin_);
  1240. it1_ = it1_end_ - d;
  1241. n -= d;
  1242. current_ = 0;
  1243. }
  1244. }
  1245. BOOST_UBLAS_CHECK (n == 0, external_logic ());
  1246. return *this;
  1247. }
  1248. BOOST_UBLAS_INLINE
  1249. difference_type operator - (const const_iterator1 &it) const {
  1250. BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ());
  1251. BOOST_UBLAS_CHECK (current_ == 0 || current_ == 1, internal_logic ());
  1252. BOOST_UBLAS_CHECK (it.current_ == 0 || it.current_ == 1, internal_logic ());
  1253. BOOST_UBLAS_CHECK (/* begin_ == it.begin_ && */ end_ == it.end_, internal_logic ());
  1254. if (current_ == 0 && it.current_ == 0) {
  1255. return it1_ - it.it1_;
  1256. } else if (current_ == 0 && it.current_ == 1) {
  1257. if (end_ == 1 && it.end_ == 1) {
  1258. return (it1_ - it.it1_end_) + (it.it2_begin_ - it.it2_);
  1259. } else /* if (end_ == 0 && it.end_ == 0) */ {
  1260. return (it1_ - it.it1_begin_) + (it.it2_end_ - it.it2_);
  1261. }
  1262. } else if (current_ == 1 && it.current_ == 0) {
  1263. if (end_ == 1 && it.end_ == 1) {
  1264. return (it2_ - it.it2_begin_) + (it.it1_end_ - it.it1_);
  1265. } else /* if (end_ == 0 && it.end_ == 0) */ {
  1266. return (it2_ - it.it2_end_) + (it.it1_begin_ - it.it1_);
  1267. }
  1268. }
  1269. /* current_ == 1 && it.current_ == 1 */ {
  1270. return it2_ - it.it2_;
  1271. }
  1272. }
  1273. // Dereference
  1274. BOOST_UBLAS_INLINE
  1275. const_reference operator * () const {
  1276. BOOST_UBLAS_CHECK (current_ == 0 || current_ == 1, internal_logic ());
  1277. if (current_ == 0) {
  1278. BOOST_UBLAS_CHECK (it1_ != it1_end_, internal_logic ());
  1279. return *it1_;
  1280. } else /* if (current_ == 1) */ {
  1281. BOOST_UBLAS_CHECK (it2_ != it2_end_, internal_logic ());
  1282. return *it2_;
  1283. }
  1284. }
  1285. BOOST_UBLAS_INLINE
  1286. const_reference operator [] (difference_type n) const {
  1287. return *(*this + n);
  1288. }
  1289. #ifndef BOOST_UBLAS_NO_NESTED_CLASS_RELATION
  1290. BOOST_UBLAS_INLINE
  1291. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  1292. typename self_type::
  1293. #endif
  1294. const_iterator2 begin () const {
  1295. return (*this) ().find2 (1, index1 (), 0);
  1296. }
  1297. BOOST_UBLAS_INLINE
  1298. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  1299. typename self_type::
  1300. #endif
  1301. const_iterator2 end () const {
  1302. return (*this) ().find2 (1, index1 (), (*this) ().size2 ());
  1303. }
  1304. BOOST_UBLAS_INLINE
  1305. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  1306. typename self_type::
  1307. #endif
  1308. const_reverse_iterator2 rbegin () const {
  1309. return const_reverse_iterator2 (end ());
  1310. }
  1311. BOOST_UBLAS_INLINE
  1312. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  1313. typename self_type::
  1314. #endif
  1315. const_reverse_iterator2 rend () const {
  1316. return const_reverse_iterator2 (begin ());
  1317. }
  1318. #endif
  1319. // Indices
  1320. BOOST_UBLAS_INLINE
  1321. size_type index1 () const {
  1322. BOOST_UBLAS_CHECK (current_ == 0 || current_ == 1, internal_logic ());
  1323. if (current_ == 0) {
  1324. BOOST_UBLAS_CHECK (it1_ != it1_end_, internal_logic ());
  1325. return it1_.index1 ();
  1326. } else /* if (current_ == 1) */ {
  1327. BOOST_UBLAS_CHECK (it2_ != it2_end_, internal_logic ());
  1328. return it2_.index2 ();
  1329. }
  1330. }
  1331. BOOST_UBLAS_INLINE
  1332. size_type index2 () const {
  1333. BOOST_UBLAS_CHECK (current_ == 0 || current_ == 1, internal_logic ());
  1334. if (current_ == 0) {
  1335. BOOST_UBLAS_CHECK (it1_ != it1_end_, internal_logic ());
  1336. return it1_.index2 ();
  1337. } else /* if (current_ == 1) */ {
  1338. BOOST_UBLAS_CHECK (it2_ != it2_end_, internal_logic ());
  1339. return it2_.index1 ();
  1340. }
  1341. }
  1342. // Assignment
  1343. BOOST_UBLAS_INLINE
  1344. const_iterator1 &operator = (const const_iterator1 &it) {
  1345. container_const_reference<self_type>::assign (&it ());
  1346. begin_ = it.begin_;
  1347. end_ = it.end_;
  1348. current_ = it.current_;
  1349. it1_begin_ = it.it1_begin_;
  1350. it1_end_ = it.it1_end_;
  1351. it1_ = it.it1_;
  1352. it2_begin_ = it.it2_begin_;
  1353. it2_end_ = it.it2_end_;
  1354. it2_ = it.it2_;
  1355. return *this;
  1356. }
  1357. // Comparison
  1358. BOOST_UBLAS_INLINE
  1359. bool operator == (const const_iterator1 &it) const {
  1360. BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ());
  1361. BOOST_UBLAS_CHECK (current_ == 0 || current_ == 1, internal_logic ());
  1362. BOOST_UBLAS_CHECK (it.current_ == 0 || it.current_ == 1, internal_logic ());
  1363. BOOST_UBLAS_CHECK (/* begin_ == it.begin_ && */ end_ == it.end_, internal_logic ());
  1364. return (current_ == 0 && it.current_ == 0 && it1_ == it.it1_) ||
  1365. (current_ == 1 && it.current_ == 1 && it2_ == it.it2_);
  1366. }
  1367. BOOST_UBLAS_INLINE
  1368. bool operator < (const const_iterator1 &it) const {
  1369. BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ());
  1370. return it - *this > 0;
  1371. }
  1372. private:
  1373. int begin_;
  1374. int end_;
  1375. int current_;
  1376. const_subiterator1_type it1_begin_;
  1377. const_subiterator1_type it1_end_;
  1378. const_subiterator1_type it1_;
  1379. const_subiterator2_type it2_begin_;
  1380. const_subiterator2_type it2_end_;
  1381. const_subiterator2_type it2_;
  1382. };
  1383. #endif
  1384. BOOST_UBLAS_INLINE
  1385. const_iterator1 begin1 () const {
  1386. return find1 (0, 0, 0);
  1387. }
  1388. BOOST_UBLAS_INLINE
  1389. const_iterator1 end1 () const {
  1390. return find1 (0, size1 (), 0);
  1391. }
  1392. #ifndef BOOST_UBLAS_USE_INDEXED_ITERATOR
  1393. class iterator1:
  1394. public container_reference<symmetric_adaptor>,
  1395. public random_access_iterator_base<typename iterator_restrict_traits<
  1396. typename subiterator1_type::iterator_category, packed_random_access_iterator_tag>::iterator_category,
  1397. iterator1, value_type> {
  1398. public:
  1399. typedef typename subiterator1_type::value_type value_type;
  1400. typedef typename subiterator1_type::difference_type difference_type;
  1401. typedef typename subiterator1_type::reference reference;
  1402. typedef typename subiterator1_type::pointer pointer;
  1403. typedef iterator2 dual_iterator_type;
  1404. typedef reverse_iterator2 dual_reverse_iterator_type;
  1405. // Construction and destruction
  1406. BOOST_UBLAS_INLINE
  1407. iterator1 ():
  1408. container_reference<self_type> (), it1_ () {}
  1409. BOOST_UBLAS_INLINE
  1410. iterator1 (self_type &m, const subiterator1_type &it1):
  1411. container_reference<self_type> (m), it1_ (it1) {}
  1412. // Arithmetic
  1413. BOOST_UBLAS_INLINE
  1414. iterator1 &operator ++ () {
  1415. ++ it1_;
  1416. return *this;
  1417. }
  1418. BOOST_UBLAS_INLINE
  1419. iterator1 &operator -- () {
  1420. -- it1_;
  1421. return *this;
  1422. }
  1423. BOOST_UBLAS_INLINE
  1424. iterator1 &operator += (difference_type n) {
  1425. it1_ += n;
  1426. return *this;
  1427. }
  1428. BOOST_UBLAS_INLINE
  1429. iterator1 &operator -= (difference_type n) {
  1430. it1_ -= n;
  1431. return *this;
  1432. }
  1433. BOOST_UBLAS_INLINE
  1434. difference_type operator - (const iterator1 &it) const {
  1435. BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ());
  1436. return it1_ - it.it1_;
  1437. }
  1438. // Dereference
  1439. BOOST_UBLAS_INLINE
  1440. reference operator * () const {
  1441. return *it1_;
  1442. }
  1443. BOOST_UBLAS_INLINE
  1444. reference operator [] (difference_type n) const {
  1445. return *(*this + n);
  1446. }
  1447. #ifndef BOOST_UBLAS_NO_NESTED_CLASS_RELATION
  1448. BOOST_UBLAS_INLINE
  1449. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  1450. typename self_type::
  1451. #endif
  1452. iterator2 begin () const {
  1453. return (*this) ().find2 (1, index1 (), 0);
  1454. }
  1455. BOOST_UBLAS_INLINE
  1456. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  1457. typename self_type::
  1458. #endif
  1459. iterator2 end () const {
  1460. return (*this) ().find2 (1, index1 (), (*this) ().size2 ());
  1461. }
  1462. BOOST_UBLAS_INLINE
  1463. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  1464. typename self_type::
  1465. #endif
  1466. reverse_iterator2 rbegin () const {
  1467. return reverse_iterator2 (end ());
  1468. }
  1469. BOOST_UBLAS_INLINE
  1470. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  1471. typename self_type::
  1472. #endif
  1473. reverse_iterator2 rend () const {
  1474. return reverse_iterator2 (begin ());
  1475. }
  1476. #endif
  1477. // Indices
  1478. BOOST_UBLAS_INLINE
  1479. size_type index1 () const {
  1480. return it1_.index1 ();
  1481. }
  1482. BOOST_UBLAS_INLINE
  1483. size_type index2 () const {
  1484. return it1_.index2 ();
  1485. }
  1486. // Assignment
  1487. BOOST_UBLAS_INLINE
  1488. iterator1 &operator = (const iterator1 &it) {
  1489. container_reference<self_type>::assign (&it ());
  1490. it1_ = it.it1_;
  1491. return *this;
  1492. }
  1493. // Comparison
  1494. BOOST_UBLAS_INLINE
  1495. bool operator == (const iterator1 &it) const {
  1496. BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ());
  1497. return it1_ == it.it1_;
  1498. }
  1499. BOOST_UBLAS_INLINE
  1500. bool operator < (const iterator1 &it) const {
  1501. BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ());
  1502. return it1_ < it.it1_;
  1503. }
  1504. private:
  1505. subiterator1_type it1_;
  1506. friend class const_iterator1;
  1507. };
  1508. #endif
  1509. BOOST_UBLAS_INLINE
  1510. iterator1 begin1 () {
  1511. return find1 (0, 0, 0);
  1512. }
  1513. BOOST_UBLAS_INLINE
  1514. iterator1 end1 () {
  1515. return find1 (0, size1 (), 0);
  1516. }
  1517. #ifndef BOOST_UBLAS_USE_INDEXED_ITERATOR
  1518. class const_iterator2:
  1519. public container_const_reference<symmetric_adaptor>,
  1520. public random_access_iterator_base<typename iterator_restrict_traits<
  1521. typename const_subiterator2_type::iterator_category, dense_random_access_iterator_tag>::iterator_category,
  1522. const_iterator2, value_type> {
  1523. public:
  1524. typedef typename const_subiterator2_type::value_type value_type;
  1525. typedef typename const_subiterator2_type::difference_type difference_type;
  1526. typedef typename const_subiterator2_type::reference reference;
  1527. typedef typename const_subiterator2_type::pointer pointer;
  1528. typedef const_iterator1 dual_iterator_type;
  1529. typedef const_reverse_iterator1 dual_reverse_iterator_type;
  1530. // Construction and destruction
  1531. BOOST_UBLAS_INLINE
  1532. const_iterator2 ():
  1533. container_const_reference<self_type> (),
  1534. begin_ (-1), end_ (-1), current_ (-1),
  1535. it1_begin_ (), it1_end_ (), it1_ (),
  1536. it2_begin_ (), it2_end_ (), it2_ () {}
  1537. BOOST_UBLAS_INLINE
  1538. const_iterator2 (const self_type &m, int begin, int end,
  1539. const const_subiterator1_type &it1_begin, const const_subiterator1_type &it1_end,
  1540. const const_subiterator2_type &it2_begin, const const_subiterator2_type &it2_end):
  1541. container_const_reference<self_type> (m),
  1542. begin_ (begin), end_ (end), current_ (begin),
  1543. it1_begin_ (it1_begin), it1_end_ (it1_end), it1_ (it1_begin_),
  1544. it2_begin_ (it2_begin), it2_end_ (it2_end), it2_ (it2_begin_) {
  1545. if (current_ == 0 && it1_ == it1_end_)
  1546. current_ = 1;
  1547. if (current_ == 1 && it2_ == it2_end_)
  1548. current_ = 0;
  1549. if ((current_ == 0 && it1_ == it1_end_) ||
  1550. (current_ == 1 && it2_ == it2_end_))
  1551. current_ = end_;
  1552. BOOST_UBLAS_CHECK (current_ == end_ ||
  1553. (current_ == 0 && it1_ != it1_end_) ||
  1554. (current_ == 1 && it2_ != it2_end_), internal_logic ());
  1555. }
  1556. // FIXME cannot compiler
  1557. // iterator2 does not have these members!
  1558. BOOST_UBLAS_INLINE
  1559. const_iterator2 (const iterator2 &it):
  1560. container_const_reference<self_type> (it ()),
  1561. begin_ (it.begin_), end_ (it.end_), current_ (it.current_),
  1562. it1_begin_ (it.it1_begin_), it1_end_ (it.it1_end_), it1_ (it.it1_),
  1563. it2_begin_ (it.it2_begin_), it2_end_ (it.it2_end_), it2_ (it.it2_) {
  1564. BOOST_UBLAS_CHECK (current_ == end_ ||
  1565. (current_ == 0 && it1_ != it1_end_) ||
  1566. (current_ == 1 && it2_ != it2_end_), internal_logic ());
  1567. }
  1568. // Arithmetic
  1569. BOOST_UBLAS_INLINE
  1570. const_iterator2 &operator ++ () {
  1571. BOOST_UBLAS_CHECK (current_ == 0 || current_ == 1, internal_logic ());
  1572. if (current_ == 0) {
  1573. BOOST_UBLAS_CHECK (it1_ != it1_end_, internal_logic ());
  1574. ++ it1_;
  1575. if (it1_ == it1_end_ && end_ == 1) {
  1576. it2_ = it2_begin_;
  1577. current_ = 1;
  1578. }
  1579. } else /* if (current_ == 1) */ {
  1580. BOOST_UBLAS_CHECK (it2_ != it2_end_, internal_logic ());
  1581. ++ it2_;
  1582. if (it2_ == it2_end_ && end_ == 0) {
  1583. it1_ = it1_begin_;
  1584. current_ = 0;
  1585. }
  1586. }
  1587. return *this;
  1588. }
  1589. BOOST_UBLAS_INLINE
  1590. const_iterator2 &operator -- () {
  1591. BOOST_UBLAS_CHECK (current_ == 0 || current_ == 1, internal_logic ());
  1592. if (current_ == 0) {
  1593. if (it1_ == it1_begin_ && begin_ == 1) {
  1594. it2_ = it2_end_;
  1595. BOOST_UBLAS_CHECK (it2_ != it2_begin_, internal_logic ());
  1596. -- it2_;
  1597. current_ = 1;
  1598. } else {
  1599. -- it1_;
  1600. }
  1601. } else /* if (current_ == 1) */ {
  1602. if (it2_ == it2_begin_ && begin_ == 0) {
  1603. it1_ = it1_end_;
  1604. BOOST_UBLAS_CHECK (it1_ != it1_begin_, internal_logic ());
  1605. -- it1_;
  1606. current_ = 0;
  1607. } else {
  1608. -- it2_;
  1609. }
  1610. }
  1611. return *this;
  1612. }
  1613. BOOST_UBLAS_INLINE
  1614. const_iterator2 &operator += (difference_type n) {
  1615. BOOST_UBLAS_CHECK (current_ == 0 || current_ == 1, internal_logic ());
  1616. if (current_ == 0) {
  1617. size_type d = (std::min) (n, it1_end_ - it1_);
  1618. it1_ += d;
  1619. n -= d;
  1620. if (n > 0 || (end_ == 1 && it1_ == it1_end_)) {
  1621. BOOST_UBLAS_CHECK (end_ == 1, external_logic ());
  1622. d = (std::min) (n, it2_end_ - it2_begin_);
  1623. it2_ = it2_begin_ + d;
  1624. n -= d;
  1625. current_ = 1;
  1626. }
  1627. } else /* if (current_ == 1) */ {
  1628. size_type d = (std::min) (n, it2_end_ - it2_);
  1629. it2_ += d;
  1630. n -= d;
  1631. if (n > 0 || (end_ == 0 && it2_ == it2_end_)) {
  1632. BOOST_UBLAS_CHECK (end_ == 0, external_logic ());
  1633. d = (std::min) (n, it1_end_ - it1_begin_);
  1634. it1_ = it1_begin_ + d;
  1635. n -= d;
  1636. current_ = 0;
  1637. }
  1638. }
  1639. BOOST_UBLAS_CHECK (n == 0, external_logic ());
  1640. return *this;
  1641. }
  1642. BOOST_UBLAS_INLINE
  1643. const_iterator2 &operator -= (difference_type n) {
  1644. BOOST_UBLAS_CHECK (current_ == 0 || current_ == 1, internal_logic ());
  1645. if (current_ == 0) {
  1646. size_type d = (std::min) (n, it1_ - it1_begin_);
  1647. it1_ -= d;
  1648. n -= d;
  1649. if (n > 0) {
  1650. BOOST_UBLAS_CHECK (end_ == 1, external_logic ());
  1651. d = (std::min) (n, it2_end_ - it2_begin_);
  1652. it2_ = it2_end_ - d;
  1653. n -= d;
  1654. current_ = 1;
  1655. }
  1656. } else /* if (current_ == 1) */ {
  1657. size_type d = (std::min) (n, it2_ - it2_begin_);
  1658. it2_ -= d;
  1659. n -= d;
  1660. if (n > 0) {
  1661. BOOST_UBLAS_CHECK (end_ == 0, external_logic ());
  1662. d = (std::min) (n, it1_end_ - it1_begin_);
  1663. it1_ = it1_end_ - d;
  1664. n -= d;
  1665. current_ = 0;
  1666. }
  1667. }
  1668. BOOST_UBLAS_CHECK (n == 0, external_logic ());
  1669. return *this;
  1670. }
  1671. BOOST_UBLAS_INLINE
  1672. difference_type operator - (const const_iterator2 &it) const {
  1673. BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ());
  1674. BOOST_UBLAS_CHECK (current_ == 0 || current_ == 1, internal_logic ());
  1675. BOOST_UBLAS_CHECK (it.current_ == 0 || it.current_ == 1, internal_logic ());
  1676. BOOST_UBLAS_CHECK (/* begin_ == it.begin_ && */ end_ == it.end_, internal_logic ());
  1677. if (current_ == 0 && it.current_ == 0) {
  1678. return it1_ - it.it1_;
  1679. } else if (current_ == 0 && it.current_ == 1) {
  1680. if (end_ == 1 && it.end_ == 1) {
  1681. return (it1_ - it.it1_end_) + (it.it2_begin_ - it.it2_);
  1682. } else /* if (end_ == 0 && it.end_ == 0) */ {
  1683. return (it1_ - it.it1_begin_) + (it.it2_end_ - it.it2_);
  1684. }
  1685. } else if (current_ == 1 && it.current_ == 0) {
  1686. if (end_ == 1 && it.end_ == 1) {
  1687. return (it2_ - it.it2_begin_) + (it.it1_end_ - it.it1_);
  1688. } else /* if (end_ == 0 && it.end_ == 0) */ {
  1689. return (it2_ - it.it2_end_) + (it.it1_begin_ - it.it1_);
  1690. }
  1691. }
  1692. /* current_ == 1 && it.current_ == 1 */ {
  1693. return it2_ - it.it2_;
  1694. }
  1695. }
  1696. // Dereference
  1697. BOOST_UBLAS_INLINE
  1698. const_reference operator * () const {
  1699. BOOST_UBLAS_CHECK (current_ == 0 || current_ == 1, internal_logic ());
  1700. if (current_ == 0) {
  1701. BOOST_UBLAS_CHECK (it1_ != it1_end_, internal_logic ());
  1702. return *it1_;
  1703. } else /* if (current_ == 1) */ {
  1704. BOOST_UBLAS_CHECK (it2_ != it2_end_, internal_logic ());
  1705. return *it2_;
  1706. }
  1707. }
  1708. BOOST_UBLAS_INLINE
  1709. const_reference operator [] (difference_type n) const {
  1710. return *(*this + n);
  1711. }
  1712. #ifndef BOOST_UBLAS_NO_NESTED_CLASS_RELATION
  1713. BOOST_UBLAS_INLINE
  1714. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  1715. typename self_type::
  1716. #endif
  1717. const_iterator1 begin () const {
  1718. return (*this) ().find1 (1, 0, index2 ());
  1719. }
  1720. BOOST_UBLAS_INLINE
  1721. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  1722. typename self_type::
  1723. #endif
  1724. const_iterator1 end () const {
  1725. return (*this) ().find1 (1, (*this) ().size1 (), index2 ());
  1726. }
  1727. BOOST_UBLAS_INLINE
  1728. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  1729. typename self_type::
  1730. #endif
  1731. const_reverse_iterator1 rbegin () const {
  1732. return const_reverse_iterator1 (end ());
  1733. }
  1734. BOOST_UBLAS_INLINE
  1735. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  1736. typename self_type::
  1737. #endif
  1738. const_reverse_iterator1 rend () const {
  1739. return const_reverse_iterator1 (begin ());
  1740. }
  1741. #endif
  1742. // Indices
  1743. BOOST_UBLAS_INLINE
  1744. size_type index1 () const {
  1745. BOOST_UBLAS_CHECK (current_ == 0 || current_ == 1, internal_logic ());
  1746. if (current_ == 0) {
  1747. BOOST_UBLAS_CHECK (it1_ != it1_end_, internal_logic ());
  1748. return it1_.index2 ();
  1749. } else /* if (current_ == 1) */ {
  1750. BOOST_UBLAS_CHECK (it2_ != it2_end_, internal_logic ());
  1751. return it2_.index1 ();
  1752. }
  1753. }
  1754. BOOST_UBLAS_INLINE
  1755. size_type index2 () const {
  1756. BOOST_UBLAS_CHECK (current_ == 0 || current_ == 1, internal_logic ());
  1757. if (current_ == 0) {
  1758. BOOST_UBLAS_CHECK (it1_ != it1_end_, internal_logic ());
  1759. return it1_.index1 ();
  1760. } else /* if (current_ == 1) */ {
  1761. BOOST_UBLAS_CHECK (it2_ != it2_end_, internal_logic ());
  1762. return it2_.index2 ();
  1763. }
  1764. }
  1765. // Assignment
  1766. BOOST_UBLAS_INLINE
  1767. const_iterator2 &operator = (const const_iterator2 &it) {
  1768. container_const_reference<self_type>::assign (&it ());
  1769. begin_ = it.begin_;
  1770. end_ = it.end_;
  1771. current_ = it.current_;
  1772. it1_begin_ = it.it1_begin_;
  1773. it1_end_ = it.it1_end_;
  1774. it1_ = it.it1_;
  1775. it2_begin_ = it.it2_begin_;
  1776. it2_end_ = it.it2_end_;
  1777. it2_ = it.it2_;
  1778. return *this;
  1779. }
  1780. // Comparison
  1781. BOOST_UBLAS_INLINE
  1782. bool operator == (const const_iterator2 &it) const {
  1783. BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ());
  1784. BOOST_UBLAS_CHECK (current_ == 0 || current_ == 1, internal_logic ());
  1785. BOOST_UBLAS_CHECK (it.current_ == 0 || it.current_ == 1, internal_logic ());
  1786. BOOST_UBLAS_CHECK (/* begin_ == it.begin_ && */ end_ == it.end_, internal_logic ());
  1787. return (current_ == 0 && it.current_ == 0 && it1_ == it.it1_) ||
  1788. (current_ == 1 && it.current_ == 1 && it2_ == it.it2_);
  1789. }
  1790. BOOST_UBLAS_INLINE
  1791. bool operator < (const const_iterator2 &it) const {
  1792. BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ());
  1793. return it - *this > 0;
  1794. }
  1795. private:
  1796. int begin_;
  1797. int end_;
  1798. int current_;
  1799. const_subiterator1_type it1_begin_;
  1800. const_subiterator1_type it1_end_;
  1801. const_subiterator1_type it1_;
  1802. const_subiterator2_type it2_begin_;
  1803. const_subiterator2_type it2_end_;
  1804. const_subiterator2_type it2_;
  1805. };
  1806. #endif
  1807. BOOST_UBLAS_INLINE
  1808. const_iterator2 begin2 () const {
  1809. return find2 (0, 0, 0);
  1810. }
  1811. BOOST_UBLAS_INLINE
  1812. const_iterator2 end2 () const {
  1813. return find2 (0, 0, size2 ());
  1814. }
  1815. #ifndef BOOST_UBLAS_USE_INDEXED_ITERATOR
  1816. class iterator2:
  1817. public container_reference<symmetric_adaptor>,
  1818. public random_access_iterator_base<typename iterator_restrict_traits<
  1819. typename subiterator2_type::iterator_category, packed_random_access_iterator_tag>::iterator_category,
  1820. iterator2, value_type> {
  1821. public:
  1822. typedef typename subiterator2_type::value_type value_type;
  1823. typedef typename subiterator2_type::difference_type difference_type;
  1824. typedef typename subiterator2_type::reference reference;
  1825. typedef typename subiterator2_type::pointer pointer;
  1826. typedef iterator1 dual_iterator_type;
  1827. typedef reverse_iterator1 dual_reverse_iterator_type;
  1828. // Construction and destruction
  1829. BOOST_UBLAS_INLINE
  1830. iterator2 ():
  1831. container_reference<self_type> (), it2_ () {}
  1832. BOOST_UBLAS_INLINE
  1833. iterator2 (self_type &m, const subiterator2_type &it2):
  1834. container_reference<self_type> (m), it2_ (it2) {}
  1835. // Arithmetic
  1836. BOOST_UBLAS_INLINE
  1837. iterator2 &operator ++ () {
  1838. ++ it2_;
  1839. return *this;
  1840. }
  1841. BOOST_UBLAS_INLINE
  1842. iterator2 &operator -- () {
  1843. -- it2_;
  1844. return *this;
  1845. }
  1846. BOOST_UBLAS_INLINE
  1847. iterator2 &operator += (difference_type n) {
  1848. it2_ += n;
  1849. return *this;
  1850. }
  1851. BOOST_UBLAS_INLINE
  1852. iterator2 &operator -= (difference_type n) {
  1853. it2_ -= n;
  1854. return *this;
  1855. }
  1856. BOOST_UBLAS_INLINE
  1857. difference_type operator - (const iterator2 &it) const {
  1858. BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ());
  1859. return it2_ - it.it2_;
  1860. }
  1861. // Dereference
  1862. BOOST_UBLAS_INLINE
  1863. reference operator * () const {
  1864. return *it2_;
  1865. }
  1866. BOOST_UBLAS_INLINE
  1867. reference operator [] (difference_type n) const {
  1868. return *(*this + n);
  1869. }
  1870. #ifndef BOOST_UBLAS_NO_NESTED_CLASS_RELATION
  1871. BOOST_UBLAS_INLINE
  1872. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  1873. typename self_type::
  1874. #endif
  1875. iterator1 begin () const {
  1876. return (*this) ().find1 (1, 0, index2 ());
  1877. }
  1878. BOOST_UBLAS_INLINE
  1879. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  1880. typename self_type::
  1881. #endif
  1882. iterator1 end () const {
  1883. return (*this) ().find1 (1, (*this) ().size1 (), index2 ());
  1884. }
  1885. BOOST_UBLAS_INLINE
  1886. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  1887. typename self_type::
  1888. #endif
  1889. reverse_iterator1 rbegin () const {
  1890. return reverse_iterator1 (end ());
  1891. }
  1892. BOOST_UBLAS_INLINE
  1893. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  1894. typename self_type::
  1895. #endif
  1896. reverse_iterator1 rend () const {
  1897. return reverse_iterator1 (begin ());
  1898. }
  1899. #endif
  1900. // Indices
  1901. BOOST_UBLAS_INLINE
  1902. size_type index1 () const {
  1903. return it2_.index1 ();
  1904. }
  1905. BOOST_UBLAS_INLINE
  1906. size_type index2 () const {
  1907. return it2_.index2 ();
  1908. }
  1909. // Assignment
  1910. BOOST_UBLAS_INLINE
  1911. iterator2 &operator = (const iterator2 &it) {
  1912. container_reference<self_type>::assign (&it ());
  1913. it2_ = it.it2_;
  1914. return *this;
  1915. }
  1916. // Comparison
  1917. BOOST_UBLAS_INLINE
  1918. bool operator == (const iterator2 &it) const {
  1919. BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ());
  1920. return it2_ == it.it2_;
  1921. }
  1922. BOOST_UBLAS_INLINE
  1923. bool operator < (const iterator2 &it) const {
  1924. BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ());
  1925. return it2_ < it.it2_;
  1926. }
  1927. private:
  1928. subiterator2_type it2_;
  1929. friend class const_iterator2;
  1930. };
  1931. #endif
  1932. BOOST_UBLAS_INLINE
  1933. iterator2 begin2 () {
  1934. return find2 (0, 0, 0);
  1935. }
  1936. BOOST_UBLAS_INLINE
  1937. iterator2 end2 () {
  1938. return find2 (0, 0, size2 ());
  1939. }
  1940. // Reverse iterators
  1941. BOOST_UBLAS_INLINE
  1942. const_reverse_iterator1 rbegin1 () const {
  1943. return const_reverse_iterator1 (end1 ());
  1944. }
  1945. BOOST_UBLAS_INLINE
  1946. const_reverse_iterator1 rend1 () const {
  1947. return const_reverse_iterator1 (begin1 ());
  1948. }
  1949. BOOST_UBLAS_INLINE
  1950. reverse_iterator1 rbegin1 () {
  1951. return reverse_iterator1 (end1 ());
  1952. }
  1953. BOOST_UBLAS_INLINE
  1954. reverse_iterator1 rend1 () {
  1955. return reverse_iterator1 (begin1 ());
  1956. }
  1957. BOOST_UBLAS_INLINE
  1958. const_reverse_iterator2 rbegin2 () const {
  1959. return const_reverse_iterator2 (end2 ());
  1960. }
  1961. BOOST_UBLAS_INLINE
  1962. const_reverse_iterator2 rend2 () const {
  1963. return const_reverse_iterator2 (begin2 ());
  1964. }
  1965. BOOST_UBLAS_INLINE
  1966. reverse_iterator2 rbegin2 () {
  1967. return reverse_iterator2 (end2 ());
  1968. }
  1969. BOOST_UBLAS_INLINE
  1970. reverse_iterator2 rend2 () {
  1971. return reverse_iterator2 (begin2 ());
  1972. }
  1973. private:
  1974. matrix_closure_type data_;
  1975. };
  1976. // Specialization for temporary_traits
  1977. template <class M, class TRI>
  1978. struct vector_temporary_traits< symmetric_adaptor<M, TRI> >
  1979. : vector_temporary_traits< M > {} ;
  1980. template <class M, class TRI>
  1981. struct vector_temporary_traits< const symmetric_adaptor<M, TRI> >
  1982. : vector_temporary_traits< M > {} ;
  1983. template <class M, class TRI>
  1984. struct matrix_temporary_traits< symmetric_adaptor<M, TRI> >
  1985. : matrix_temporary_traits< M > {} ;
  1986. template <class M, class TRI>
  1987. struct matrix_temporary_traits< const symmetric_adaptor<M, TRI> >
  1988. : matrix_temporary_traits< M > {} ;
  1989. }}}
  1990. #endif