vector_sparse.hpp 80 KB

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