vector_sparse.hpp 81 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216
  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 cbegin () const {
  599. return begin ();
  600. }
  601. BOOST_UBLAS_INLINE
  602. const_iterator end () const {
  603. return const_iterator (*this, data ().end ());
  604. }
  605. BOOST_UBLAS_INLINE
  606. const_iterator cend () const {
  607. return end ();
  608. }
  609. class iterator:
  610. public container_reference<mapped_vector>,
  611. public bidirectional_iterator_base<sparse_bidirectional_iterator_tag,
  612. iterator, value_type> {
  613. public:
  614. typedef typename mapped_vector::value_type value_type;
  615. typedef typename mapped_vector::difference_type difference_type;
  616. typedef typename mapped_vector::true_reference reference;
  617. typedef typename mapped_vector::pointer pointer;
  618. // Construction and destruction
  619. BOOST_UBLAS_INLINE
  620. iterator ():
  621. container_reference<self_type> (), it_ () {}
  622. BOOST_UBLAS_INLINE
  623. iterator (self_type &v, const subiterator_type &it):
  624. container_reference<self_type> (v), it_ (it) {}
  625. // Arithmetic
  626. BOOST_UBLAS_INLINE
  627. iterator &operator ++ () {
  628. ++ it_;
  629. return *this;
  630. }
  631. BOOST_UBLAS_INLINE
  632. iterator &operator -- () {
  633. -- it_;
  634. return *this;
  635. }
  636. // Dereference
  637. BOOST_UBLAS_INLINE
  638. reference operator * () const {
  639. BOOST_UBLAS_CHECK (index () < (*this) ().size (), bad_index ());
  640. return (*it_).second;
  641. }
  642. // Index
  643. BOOST_UBLAS_INLINE
  644. size_type index () const {
  645. BOOST_UBLAS_CHECK (*this != (*this) ().end (), bad_index ());
  646. BOOST_UBLAS_CHECK ((*it_).first < (*this) ().size (), bad_index ());
  647. return (*it_).first;
  648. }
  649. // Assignment
  650. BOOST_UBLAS_INLINE
  651. iterator &operator = (const iterator &it) {
  652. container_reference<self_type>::assign (&it ());
  653. it_ = it.it_;
  654. return *this;
  655. }
  656. // Comparison
  657. BOOST_UBLAS_INLINE
  658. bool operator == (const iterator &it) const {
  659. BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ());
  660. return it_ == it.it_;
  661. }
  662. private:
  663. subiterator_type it_;
  664. friend class const_iterator;
  665. };
  666. BOOST_UBLAS_INLINE
  667. iterator begin () {
  668. return iterator (*this, data ().begin ());
  669. }
  670. BOOST_UBLAS_INLINE
  671. iterator end () {
  672. return iterator (*this, data ().end ());
  673. }
  674. // Reverse iterator
  675. typedef reverse_iterator_base<const_iterator> const_reverse_iterator;
  676. typedef reverse_iterator_base<iterator> reverse_iterator;
  677. BOOST_UBLAS_INLINE
  678. const_reverse_iterator rbegin () const {
  679. return const_reverse_iterator (end ());
  680. }
  681. BOOST_UBLAS_INLINE
  682. const_reverse_iterator crbegin () const {
  683. return rbegin ();
  684. }
  685. BOOST_UBLAS_INLINE
  686. const_reverse_iterator rend () const {
  687. return const_reverse_iterator (begin ());
  688. }
  689. BOOST_UBLAS_INLINE
  690. const_reverse_iterator crend () const {
  691. return rend ();
  692. }
  693. BOOST_UBLAS_INLINE
  694. reverse_iterator rbegin () {
  695. return reverse_iterator (end ());
  696. }
  697. BOOST_UBLAS_INLINE
  698. reverse_iterator rend () {
  699. return reverse_iterator (begin ());
  700. }
  701. // Serialization
  702. template<class Archive>
  703. void serialize(Archive & ar, const unsigned int /* file_version */){
  704. serialization::collection_size_type s (size_);
  705. ar & serialization::make_nvp("size",s);
  706. if (Archive::is_loading::value) {
  707. size_ = s;
  708. }
  709. ar & serialization::make_nvp("data", data_);
  710. }
  711. private:
  712. size_type size_;
  713. array_type data_;
  714. static const value_type zero_;
  715. };
  716. template<class T, class A>
  717. const typename mapped_vector<T, A>::value_type mapped_vector<T, A>::zero_ = value_type/*zero*/();
  718. // Thanks to Kresimir Fresl for extending this to cover different index bases.
  719. /** \brief Compressed array based sparse vector
  720. *
  721. * a sparse vector of values of type T of variable size. The non zero values are stored as
  722. * two seperate arrays: an index array and a value array. The index array is always sorted
  723. * and there is at most one entry for each index. Inserting an element can be time consuming.
  724. * If the vector contains a few zero entries, then it is better to have a normal vector.
  725. * If the vector has a very high dimension with a few non-zero values, then this vector is
  726. * very memory efficient (at the cost of a few more computations).
  727. *
  728. * For a \f$n\f$-dimensional compressed vector and \f$0 \leq i < n\f$ the non-zero elements
  729. * \f$v_i\f$ are mapped to consecutive elements of the index and value container, i.e. for
  730. * 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$.
  731. *
  732. * Supported parameters for the adapted array (indices and values) are \c unbounded_array<> ,
  733. * \c bounded_array<> and \c std::vector<>.
  734. *
  735. * \tparam T the type of object stored in the vector (like double, float, complex, etc...)
  736. * \tparam IB the index base of the compressed vector. Default is 0. Other supported value is 1
  737. * \tparam IA the type of adapted array for indices. Default is \c unbounded_array<std::size_t>
  738. * \tparam TA the type of adapted array for values. Default is unbounded_array<T>
  739. */
  740. template<class T, std::size_t IB, class IA, class TA>
  741. class compressed_vector:
  742. public vector_container<compressed_vector<T, IB, IA, TA> > {
  743. typedef T &true_reference;
  744. typedef T *pointer;
  745. typedef const T *const_pointer;
  746. typedef compressed_vector<T, IB, IA, TA> self_type;
  747. public:
  748. #ifdef BOOST_UBLAS_ENABLE_PROXY_SHORTCUTS
  749. using vector_container<self_type>::operator ();
  750. #endif
  751. // ISSUE require type consistency check
  752. // is_convertable (IA::size_type, TA::size_type)
  753. typedef typename IA::value_type size_type;
  754. typedef typename IA::difference_type difference_type;
  755. typedef T value_type;
  756. typedef const T &const_reference;
  757. #ifndef BOOST_UBLAS_STRICT_VECTOR_SPARSE
  758. typedef T &reference;
  759. #else
  760. typedef sparse_vector_element<self_type> reference;
  761. #endif
  762. typedef IA index_array_type;
  763. typedef TA value_array_type;
  764. typedef const vector_reference<const self_type> const_closure_type;
  765. typedef vector_reference<self_type> closure_type;
  766. typedef self_type vector_temporary_type;
  767. typedef sparse_tag storage_category;
  768. // Construction and destruction
  769. BOOST_UBLAS_INLINE
  770. compressed_vector ():
  771. vector_container<self_type> (),
  772. size_ (0), capacity_ (restrict_capacity (0)), filled_ (0),
  773. index_data_ (capacity_), value_data_ (capacity_) {
  774. storage_invariants ();
  775. }
  776. explicit BOOST_UBLAS_INLINE
  777. compressed_vector (size_type size, size_type non_zeros = 0):
  778. vector_container<self_type> (),
  779. size_ (size), capacity_ (restrict_capacity (non_zeros)), filled_ (0),
  780. index_data_ (capacity_), value_data_ (capacity_) {
  781. storage_invariants ();
  782. }
  783. BOOST_UBLAS_INLINE
  784. compressed_vector (const compressed_vector &v):
  785. vector_container<self_type> (),
  786. size_ (v.size_), capacity_ (v.capacity_), filled_ (v.filled_),
  787. index_data_ (v.index_data_), value_data_ (v.value_data_) {
  788. storage_invariants ();
  789. }
  790. template<class AE>
  791. BOOST_UBLAS_INLINE
  792. compressed_vector (const vector_expression<AE> &ae, size_type non_zeros = 0):
  793. vector_container<self_type> (),
  794. size_ (ae ().size ()), capacity_ (restrict_capacity (non_zeros)), filled_ (0),
  795. index_data_ (capacity_), value_data_ (capacity_) {
  796. storage_invariants ();
  797. vector_assign<scalar_assign> (*this, ae);
  798. }
  799. // Accessors
  800. BOOST_UBLAS_INLINE
  801. size_type size () const {
  802. return size_;
  803. }
  804. BOOST_UBLAS_INLINE
  805. size_type nnz_capacity () const {
  806. return capacity_;
  807. }
  808. BOOST_UBLAS_INLINE
  809. size_type nnz () const {
  810. return filled_;
  811. }
  812. // Storage accessors
  813. BOOST_UBLAS_INLINE
  814. static size_type index_base () {
  815. return IB;
  816. }
  817. BOOST_UBLAS_INLINE
  818. typename index_array_type::size_type filled () const {
  819. return filled_;
  820. }
  821. BOOST_UBLAS_INLINE
  822. const index_array_type &index_data () const {
  823. return index_data_;
  824. }
  825. BOOST_UBLAS_INLINE
  826. const value_array_type &value_data () const {
  827. return value_data_;
  828. }
  829. BOOST_UBLAS_INLINE
  830. void set_filled (const typename index_array_type::size_type & filled) {
  831. filled_ = filled;
  832. storage_invariants ();
  833. }
  834. BOOST_UBLAS_INLINE
  835. index_array_type &index_data () {
  836. return index_data_;
  837. }
  838. BOOST_UBLAS_INLINE
  839. value_array_type &value_data () {
  840. return value_data_;
  841. }
  842. // Resizing
  843. private:
  844. BOOST_UBLAS_INLINE
  845. size_type restrict_capacity (size_type non_zeros) const {
  846. non_zeros = (std::max) (non_zeros, size_type (1));
  847. non_zeros = (std::min) (non_zeros, size_);
  848. return non_zeros;
  849. }
  850. public:
  851. BOOST_UBLAS_INLINE
  852. void resize (size_type size, bool preserve = true) {
  853. size_ = size;
  854. capacity_ = restrict_capacity (capacity_);
  855. if (preserve) {
  856. index_data_. resize (capacity_, size_type ());
  857. value_data_. resize (capacity_, value_type ());
  858. filled_ = (std::min) (capacity_, filled_);
  859. while ((filled_ > 0) && (zero_based(index_data_[filled_ - 1]) >= size)) {
  860. --filled_;
  861. }
  862. }
  863. else {
  864. index_data_. resize (capacity_);
  865. value_data_. resize (capacity_);
  866. filled_ = 0;
  867. }
  868. storage_invariants ();
  869. }
  870. // Reserving
  871. BOOST_UBLAS_INLINE
  872. void reserve (size_type non_zeros, bool preserve = true) {
  873. capacity_ = restrict_capacity (non_zeros);
  874. if (preserve) {
  875. index_data_. resize (capacity_, size_type ());
  876. value_data_. resize (capacity_, value_type ());
  877. filled_ = (std::min) (capacity_, filled_);
  878. }
  879. else {
  880. index_data_. resize (capacity_);
  881. value_data_. resize (capacity_);
  882. filled_ = 0;
  883. }
  884. storage_invariants ();
  885. }
  886. // Element support
  887. BOOST_UBLAS_INLINE
  888. pointer find_element (size_type i) {
  889. return const_cast<pointer> (const_cast<const self_type&>(*this).find_element (i));
  890. }
  891. BOOST_UBLAS_INLINE
  892. const_pointer find_element (size_type i) const {
  893. const_subiterator_type it (detail::lower_bound (index_data_.begin (), index_data_.begin () + filled_, k_based (i), std::less<size_type> ()));
  894. if (it == index_data_.begin () + filled_ || *it != k_based (i))
  895. return 0;
  896. return &value_data_ [it - index_data_.begin ()];
  897. }
  898. // Element access
  899. BOOST_UBLAS_INLINE
  900. const_reference operator () (size_type i) const {
  901. BOOST_UBLAS_CHECK (i < size_, bad_index ());
  902. const_subiterator_type it (detail::lower_bound (index_data_.begin (), index_data_.begin () + filled_, k_based (i), std::less<size_type> ()));
  903. if (it == index_data_.begin () + filled_ || *it != k_based (i))
  904. return zero_;
  905. return value_data_ [it - index_data_.begin ()];
  906. }
  907. BOOST_UBLAS_INLINE
  908. true_reference ref (size_type i) {
  909. BOOST_UBLAS_CHECK (i < size_, bad_index ());
  910. subiterator_type it (detail::lower_bound (index_data_.begin (), index_data_.begin () + filled_, k_based (i), std::less<size_type> ()));
  911. if (it == index_data_.begin () + filled_ || *it != k_based (i))
  912. return insert_element (i, value_type/*zero*/());
  913. else
  914. return value_data_ [it - index_data_.begin ()];
  915. }
  916. BOOST_UBLAS_INLINE
  917. reference operator () (size_type i) {
  918. #ifndef BOOST_UBLAS_STRICT_VECTOR_SPARSE
  919. return ref (i) ;
  920. #else
  921. BOOST_UBLAS_CHECK (i < size_, bad_index ());
  922. return reference (*this, i);
  923. #endif
  924. }
  925. BOOST_UBLAS_INLINE
  926. const_reference operator [] (size_type i) const {
  927. return (*this) (i);
  928. }
  929. BOOST_UBLAS_INLINE
  930. reference operator [] (size_type i) {
  931. return (*this) (i);
  932. }
  933. // Element assignment
  934. BOOST_UBLAS_INLINE
  935. true_reference insert_element (size_type i, const_reference t) {
  936. BOOST_UBLAS_CHECK (!find_element (i), bad_index ()); // duplicate element
  937. if (filled_ >= capacity_)
  938. reserve (2 * capacity_, true);
  939. subiterator_type it (detail::lower_bound (index_data_.begin (), index_data_.begin () + filled_, k_based (i), std::less<size_type> ()));
  940. // ISSUE max_capacity limit due to difference_type
  941. typename std::iterator_traits<subiterator_type>::difference_type n = it - index_data_.begin ();
  942. BOOST_UBLAS_CHECK (filled_ == 0 || filled_ == typename index_array_type::size_type (n) || *it != k_based (i), internal_logic ()); // duplicate found by lower_bound
  943. ++ filled_;
  944. it = index_data_.begin () + n;
  945. std::copy_backward (it, index_data_.begin () + filled_ - 1, index_data_.begin () + filled_);
  946. *it = k_based (i);
  947. typename value_array_type::iterator itt (value_data_.begin () + n);
  948. std::copy_backward (itt, value_data_.begin () + filled_ - 1, value_data_.begin () + filled_);
  949. *itt = t;
  950. storage_invariants ();
  951. return *itt;
  952. }
  953. BOOST_UBLAS_INLINE
  954. void erase_element (size_type i) {
  955. subiterator_type it (detail::lower_bound (index_data_.begin (), index_data_.begin () + filled_, k_based (i), std::less<size_type> ()));
  956. typename std::iterator_traits<subiterator_type>::difference_type n = it - index_data_.begin ();
  957. if (filled_ > typename index_array_type::size_type (n) && *it == k_based (i)) {
  958. std::copy (it + 1, index_data_.begin () + filled_, it);
  959. typename value_array_type::iterator itt (value_data_.begin () + n);
  960. std::copy (itt + 1, value_data_.begin () + filled_, itt);
  961. -- filled_;
  962. }
  963. storage_invariants ();
  964. }
  965. // Zeroing
  966. BOOST_UBLAS_INLINE
  967. void clear () {
  968. filled_ = 0;
  969. storage_invariants ();
  970. }
  971. // Assignment
  972. BOOST_UBLAS_INLINE
  973. compressed_vector &operator = (const compressed_vector &v) {
  974. if (this != &v) {
  975. size_ = v.size_;
  976. capacity_ = v.capacity_;
  977. filled_ = v.filled_;
  978. index_data_ = v.index_data_;
  979. value_data_ = v.value_data_;
  980. }
  981. storage_invariants ();
  982. return *this;
  983. }
  984. template<class C> // Container assignment without temporary
  985. BOOST_UBLAS_INLINE
  986. compressed_vector &operator = (const vector_container<C> &v) {
  987. resize (v ().size (), false);
  988. assign (v);
  989. return *this;
  990. }
  991. BOOST_UBLAS_INLINE
  992. compressed_vector &assign_temporary (compressed_vector &v) {
  993. swap (v);
  994. return *this;
  995. }
  996. template<class AE>
  997. BOOST_UBLAS_INLINE
  998. compressed_vector &operator = (const vector_expression<AE> &ae) {
  999. self_type temporary (ae, capacity_);
  1000. return assign_temporary (temporary);
  1001. }
  1002. template<class AE>
  1003. BOOST_UBLAS_INLINE
  1004. compressed_vector &assign (const vector_expression<AE> &ae) {
  1005. vector_assign<scalar_assign> (*this, ae);
  1006. return *this;
  1007. }
  1008. // Computed assignment
  1009. template<class AE>
  1010. BOOST_UBLAS_INLINE
  1011. compressed_vector &operator += (const vector_expression<AE> &ae) {
  1012. self_type temporary (*this + ae, capacity_);
  1013. return assign_temporary (temporary);
  1014. }
  1015. template<class C> // Container assignment without temporary
  1016. BOOST_UBLAS_INLINE
  1017. compressed_vector &operator += (const vector_container<C> &v) {
  1018. plus_assign (v);
  1019. return *this;
  1020. }
  1021. template<class AE>
  1022. BOOST_UBLAS_INLINE
  1023. compressed_vector &plus_assign (const vector_expression<AE> &ae) {
  1024. vector_assign<scalar_plus_assign> (*this, ae);
  1025. return *this;
  1026. }
  1027. template<class AE>
  1028. BOOST_UBLAS_INLINE
  1029. compressed_vector &operator -= (const vector_expression<AE> &ae) {
  1030. self_type temporary (*this - ae, capacity_);
  1031. return assign_temporary (temporary);
  1032. }
  1033. template<class C> // Container assignment without temporary
  1034. BOOST_UBLAS_INLINE
  1035. compressed_vector &operator -= (const vector_container<C> &v) {
  1036. minus_assign (v);
  1037. return *this;
  1038. }
  1039. template<class AE>
  1040. BOOST_UBLAS_INLINE
  1041. compressed_vector &minus_assign (const vector_expression<AE> &ae) {
  1042. vector_assign<scalar_minus_assign> (*this, ae);
  1043. return *this;
  1044. }
  1045. template<class AT>
  1046. BOOST_UBLAS_INLINE
  1047. compressed_vector &operator *= (const AT &at) {
  1048. vector_assign_scalar<scalar_multiplies_assign> (*this, at);
  1049. return *this;
  1050. }
  1051. template<class AT>
  1052. BOOST_UBLAS_INLINE
  1053. compressed_vector &operator /= (const AT &at) {
  1054. vector_assign_scalar<scalar_divides_assign> (*this, at);
  1055. return *this;
  1056. }
  1057. // Swapping
  1058. BOOST_UBLAS_INLINE
  1059. void swap (compressed_vector &v) {
  1060. if (this != &v) {
  1061. std::swap (size_, v.size_);
  1062. std::swap (capacity_, v.capacity_);
  1063. std::swap (filled_, v.filled_);
  1064. index_data_.swap (v.index_data_);
  1065. value_data_.swap (v.value_data_);
  1066. }
  1067. storage_invariants ();
  1068. }
  1069. BOOST_UBLAS_INLINE
  1070. friend void swap (compressed_vector &v1, compressed_vector &v2) {
  1071. v1.swap (v2);
  1072. }
  1073. // Back element insertion and erasure
  1074. BOOST_UBLAS_INLINE
  1075. void push_back (size_type i, const_reference t) {
  1076. BOOST_UBLAS_CHECK (filled_ == 0 || index_data_ [filled_ - 1] < k_based (i), external_logic ());
  1077. if (filled_ >= capacity_)
  1078. reserve (2 * capacity_, true);
  1079. BOOST_UBLAS_CHECK (filled_ < capacity_, internal_logic ());
  1080. index_data_ [filled_] = k_based (i);
  1081. value_data_ [filled_] = t;
  1082. ++ filled_;
  1083. storage_invariants ();
  1084. }
  1085. BOOST_UBLAS_INLINE
  1086. void pop_back () {
  1087. BOOST_UBLAS_CHECK (filled_ > 0, external_logic ());
  1088. -- filled_;
  1089. storage_invariants ();
  1090. }
  1091. // Iterator types
  1092. private:
  1093. // Use index array iterator
  1094. typedef typename IA::const_iterator const_subiterator_type;
  1095. typedef typename IA::iterator subiterator_type;
  1096. BOOST_UBLAS_INLINE
  1097. true_reference at_element (size_type i) {
  1098. BOOST_UBLAS_CHECK (i < size_, bad_index ());
  1099. subiterator_type it (detail::lower_bound (index_data_.begin (), index_data_.begin () + filled_, k_based (i), std::less<size_type> ()));
  1100. BOOST_UBLAS_CHECK (it != index_data_.begin () + filled_ && *it == k_based (i), bad_index ());
  1101. return value_data_ [it - index_data_.begin ()];
  1102. }
  1103. public:
  1104. class const_iterator;
  1105. class iterator;
  1106. // Element lookup
  1107. // BOOST_UBLAS_INLINE This function seems to be big. So we do not let the compiler inline it.
  1108. const_iterator find (size_type i) const {
  1109. return const_iterator (*this, detail::lower_bound (index_data_.begin (), index_data_.begin () + filled_, k_based (i), std::less<size_type> ()));
  1110. }
  1111. // BOOST_UBLAS_INLINE This function seems to be big. So we do not let the compiler inline it.
  1112. iterator find (size_type i) {
  1113. return iterator (*this, detail::lower_bound (index_data_.begin (), index_data_.begin () + filled_, k_based (i), std::less<size_type> ()));
  1114. }
  1115. class const_iterator:
  1116. public container_const_reference<compressed_vector>,
  1117. public bidirectional_iterator_base<sparse_bidirectional_iterator_tag,
  1118. const_iterator, value_type> {
  1119. public:
  1120. typedef typename compressed_vector::value_type value_type;
  1121. typedef typename compressed_vector::difference_type difference_type;
  1122. typedef typename compressed_vector::const_reference reference;
  1123. typedef const typename compressed_vector::pointer pointer;
  1124. // Construction and destruction
  1125. BOOST_UBLAS_INLINE
  1126. const_iterator ():
  1127. container_const_reference<self_type> (), it_ () {}
  1128. BOOST_UBLAS_INLINE
  1129. const_iterator (const self_type &v, const const_subiterator_type &it):
  1130. container_const_reference<self_type> (v), it_ (it) {}
  1131. BOOST_UBLAS_INLINE
  1132. const_iterator (const typename self_type::iterator &it): // ISSUE self_type:: stops VC8 using std::iterator here
  1133. container_const_reference<self_type> (it ()), it_ (it.it_) {}
  1134. // Arithmetic
  1135. BOOST_UBLAS_INLINE
  1136. const_iterator &operator ++ () {
  1137. ++ it_;
  1138. return *this;
  1139. }
  1140. BOOST_UBLAS_INLINE
  1141. const_iterator &operator -- () {
  1142. -- it_;
  1143. return *this;
  1144. }
  1145. // Dereference
  1146. BOOST_UBLAS_INLINE
  1147. const_reference operator * () const {
  1148. BOOST_UBLAS_CHECK (index () < (*this) ().size (), bad_index ());
  1149. return (*this) ().value_data_ [it_ - (*this) ().index_data_.begin ()];
  1150. }
  1151. // Index
  1152. BOOST_UBLAS_INLINE
  1153. size_type index () const {
  1154. BOOST_UBLAS_CHECK (*this != (*this) ().end (), bad_index ());
  1155. BOOST_UBLAS_CHECK ((*this) ().zero_based (*it_) < (*this) ().size (), bad_index ());
  1156. return (*this) ().zero_based (*it_);
  1157. }
  1158. // Assignment
  1159. BOOST_UBLAS_INLINE
  1160. const_iterator &operator = (const const_iterator &it) {
  1161. container_const_reference<self_type>::assign (&it ());
  1162. it_ = it.it_;
  1163. return *this;
  1164. }
  1165. // Comparison
  1166. BOOST_UBLAS_INLINE
  1167. bool operator == (const const_iterator &it) const {
  1168. BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ());
  1169. return it_ == it.it_;
  1170. }
  1171. private:
  1172. const_subiterator_type it_;
  1173. };
  1174. BOOST_UBLAS_INLINE
  1175. const_iterator begin () const {
  1176. return find (0);
  1177. }
  1178. BOOST_UBLAS_INLINE
  1179. const_iterator cbegin () const {
  1180. return begin ();
  1181. }
  1182. BOOST_UBLAS_INLINE
  1183. const_iterator end () const {
  1184. return find (size_);
  1185. }
  1186. BOOST_UBLAS_INLINE
  1187. const_iterator cend () const {
  1188. return end ();
  1189. }
  1190. class iterator:
  1191. public container_reference<compressed_vector>,
  1192. public bidirectional_iterator_base<sparse_bidirectional_iterator_tag,
  1193. iterator, value_type> {
  1194. public:
  1195. typedef typename compressed_vector::value_type value_type;
  1196. typedef typename compressed_vector::difference_type difference_type;
  1197. typedef typename compressed_vector::true_reference reference;
  1198. typedef typename compressed_vector::pointer pointer;
  1199. // Construction and destruction
  1200. BOOST_UBLAS_INLINE
  1201. iterator ():
  1202. container_reference<self_type> (), it_ () {}
  1203. BOOST_UBLAS_INLINE
  1204. iterator (self_type &v, const subiterator_type &it):
  1205. container_reference<self_type> (v), it_ (it) {}
  1206. // Arithmetic
  1207. BOOST_UBLAS_INLINE
  1208. iterator &operator ++ () {
  1209. ++ it_;
  1210. return *this;
  1211. }
  1212. BOOST_UBLAS_INLINE
  1213. iterator &operator -- () {
  1214. -- it_;
  1215. return *this;
  1216. }
  1217. // Dereference
  1218. BOOST_UBLAS_INLINE
  1219. reference operator * () const {
  1220. BOOST_UBLAS_CHECK (index () < (*this) ().size (), bad_index ());
  1221. return (*this) ().value_data_ [it_ - (*this) ().index_data_.begin ()];
  1222. }
  1223. // Index
  1224. BOOST_UBLAS_INLINE
  1225. size_type index () const {
  1226. BOOST_UBLAS_CHECK (*this != (*this) ().end (), bad_index ());
  1227. BOOST_UBLAS_CHECK ((*this) ().zero_based (*it_) < (*this) ().size (), bad_index ());
  1228. return (*this) ().zero_based (*it_);
  1229. }
  1230. // Assignment
  1231. BOOST_UBLAS_INLINE
  1232. iterator &operator = (const iterator &it) {
  1233. container_reference<self_type>::assign (&it ());
  1234. it_ = it.it_;
  1235. return *this;
  1236. }
  1237. // Comparison
  1238. BOOST_UBLAS_INLINE
  1239. bool operator == (const iterator &it) const {
  1240. BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ());
  1241. return it_ == it.it_;
  1242. }
  1243. private:
  1244. subiterator_type it_;
  1245. friend class const_iterator;
  1246. };
  1247. BOOST_UBLAS_INLINE
  1248. iterator begin () {
  1249. return find (0);
  1250. }
  1251. BOOST_UBLAS_INLINE
  1252. iterator end () {
  1253. return find (size_);
  1254. }
  1255. // Reverse iterator
  1256. typedef reverse_iterator_base<const_iterator> const_reverse_iterator;
  1257. typedef reverse_iterator_base<iterator> reverse_iterator;
  1258. BOOST_UBLAS_INLINE
  1259. const_reverse_iterator rbegin () const {
  1260. return const_reverse_iterator (end ());
  1261. }
  1262. BOOST_UBLAS_INLINE
  1263. const_reverse_iterator crbegin () const {
  1264. return rbegin ();
  1265. }
  1266. BOOST_UBLAS_INLINE
  1267. const_reverse_iterator rend () const {
  1268. return const_reverse_iterator (begin ());
  1269. }
  1270. BOOST_UBLAS_INLINE
  1271. const_reverse_iterator crend () const {
  1272. return rend ();
  1273. }
  1274. BOOST_UBLAS_INLINE
  1275. reverse_iterator rbegin () {
  1276. return reverse_iterator (end ());
  1277. }
  1278. BOOST_UBLAS_INLINE
  1279. reverse_iterator rend () {
  1280. return reverse_iterator (begin ());
  1281. }
  1282. // Serialization
  1283. template<class Archive>
  1284. void serialize(Archive & ar, const unsigned int /* file_version */){
  1285. serialization::collection_size_type s (size_);
  1286. ar & serialization::make_nvp("size",s);
  1287. if (Archive::is_loading::value) {
  1288. size_ = s;
  1289. }
  1290. // ISSUE: filled may be much less than capacity
  1291. // ISSUE: index_data_ and value_data_ are undefined between filled and capacity (trouble with 'nan'-values)
  1292. ar & serialization::make_nvp("capacity", capacity_);
  1293. ar & serialization::make_nvp("filled", filled_);
  1294. ar & serialization::make_nvp("index_data", index_data_);
  1295. ar & serialization::make_nvp("value_data", value_data_);
  1296. storage_invariants();
  1297. }
  1298. private:
  1299. void storage_invariants () const
  1300. {
  1301. BOOST_UBLAS_CHECK (capacity_ == index_data_.size (), internal_logic ());
  1302. BOOST_UBLAS_CHECK (capacity_ == value_data_.size (), internal_logic ());
  1303. BOOST_UBLAS_CHECK (filled_ <= capacity_, internal_logic ());
  1304. BOOST_UBLAS_CHECK ((0 == filled_) || (zero_based(index_data_[filled_ - 1]) < size_), internal_logic ());
  1305. }
  1306. size_type size_;
  1307. typename index_array_type::size_type capacity_;
  1308. typename index_array_type::size_type filled_;
  1309. index_array_type index_data_;
  1310. value_array_type value_data_;
  1311. static const value_type zero_;
  1312. BOOST_UBLAS_INLINE
  1313. static size_type zero_based (size_type k_based_index) {
  1314. return k_based_index - IB;
  1315. }
  1316. BOOST_UBLAS_INLINE
  1317. static size_type k_based (size_type zero_based_index) {
  1318. return zero_based_index + IB;
  1319. }
  1320. friend class iterator;
  1321. friend class const_iterator;
  1322. };
  1323. template<class T, std::size_t IB, class IA, class TA>
  1324. const typename compressed_vector<T, IB, IA, TA>::value_type compressed_vector<T, IB, IA, TA>::zero_ = value_type/*zero*/();
  1325. // Thanks to Kresimir Fresl for extending this to cover different index bases.
  1326. /** \brief Coordimate array based sparse vector
  1327. *
  1328. * a sparse vector of values of type \c T of variable size. The non zero values are stored
  1329. * as two seperate arrays: an index array and a value array. The arrays may be out of order
  1330. * with multiple entries for each vector element. If there are multiple values for the same
  1331. * index the sum of these values is the real value. It is way more efficient for inserting values
  1332. * than a \c compressed_vector but less memory efficient. Also linearly parsing a vector can
  1333. * be longer in specific cases than a \c compressed_vector.
  1334. *
  1335. * For a n-dimensional sorted coordinate vector and \f$ 0 \leq i < n\f$ the non-zero elements
  1336. * \f$v_i\f$ are mapped to consecutive elements of the index and value container, i.e. for
  1337. * 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$.
  1338. *
  1339. * Supported parameters for the adapted array (indices and values) are \c unbounded_array<> ,
  1340. * \c bounded_array<> and \c std::vector<>.
  1341. *
  1342. * \tparam T the type of object stored in the vector (like double, float, complex, etc...)
  1343. * \tparam IB the index base of the compressed vector. Default is 0. Other supported value is 1
  1344. * \tparam IA the type of adapted array for indices. Default is \c unbounded_array<std::size_t>
  1345. * \tparam TA the type of adapted array for values. Default is unbounded_array<T>
  1346. */
  1347. template<class T, std::size_t IB, class IA, class TA>
  1348. class coordinate_vector:
  1349. public vector_container<coordinate_vector<T, IB, IA, TA> > {
  1350. typedef T &true_reference;
  1351. typedef T *pointer;
  1352. typedef const T *const_pointer;
  1353. typedef coordinate_vector<T, IB, IA, TA> self_type;
  1354. public:
  1355. #ifdef BOOST_UBLAS_ENABLE_PROXY_SHORTCUTS
  1356. using vector_container<self_type>::operator ();
  1357. #endif
  1358. // ISSUE require type consistency check
  1359. // is_convertable (IA::size_type, TA::size_type)
  1360. typedef typename IA::value_type size_type;
  1361. typedef typename IA::difference_type difference_type;
  1362. typedef T value_type;
  1363. typedef const T &const_reference;
  1364. #ifndef BOOST_UBLAS_STRICT_VECTOR_SPARSE
  1365. typedef T &reference;
  1366. #else
  1367. typedef sparse_vector_element<self_type> reference;
  1368. #endif
  1369. typedef IA index_array_type;
  1370. typedef TA value_array_type;
  1371. typedef const vector_reference<const self_type> const_closure_type;
  1372. typedef vector_reference<self_type> closure_type;
  1373. typedef self_type vector_temporary_type;
  1374. typedef sparse_tag storage_category;
  1375. // Construction and destruction
  1376. BOOST_UBLAS_INLINE
  1377. coordinate_vector ():
  1378. vector_container<self_type> (),
  1379. size_ (0), capacity_ (restrict_capacity (0)),
  1380. filled_ (0), sorted_filled_ (filled_), sorted_ (true),
  1381. index_data_ (capacity_), value_data_ (capacity_) {
  1382. storage_invariants ();
  1383. }
  1384. explicit BOOST_UBLAS_INLINE
  1385. coordinate_vector (size_type size, size_type non_zeros = 0):
  1386. vector_container<self_type> (),
  1387. size_ (size), capacity_ (restrict_capacity (non_zeros)),
  1388. filled_ (0), sorted_filled_ (filled_), sorted_ (true),
  1389. index_data_ (capacity_), value_data_ (capacity_) {
  1390. storage_invariants ();
  1391. }
  1392. BOOST_UBLAS_INLINE
  1393. coordinate_vector (const coordinate_vector &v):
  1394. vector_container<self_type> (),
  1395. size_ (v.size_), capacity_ (v.capacity_),
  1396. filled_ (v.filled_), sorted_filled_ (v.sorted_filled_), sorted_ (v.sorted_),
  1397. index_data_ (v.index_data_), value_data_ (v.value_data_) {
  1398. storage_invariants ();
  1399. }
  1400. template<class AE>
  1401. BOOST_UBLAS_INLINE
  1402. coordinate_vector (const vector_expression<AE> &ae, size_type non_zeros = 0):
  1403. vector_container<self_type> (),
  1404. size_ (ae ().size ()), capacity_ (restrict_capacity (non_zeros)),
  1405. filled_ (0), sorted_filled_ (filled_), sorted_ (true),
  1406. index_data_ (capacity_), value_data_ (capacity_) {
  1407. storage_invariants ();
  1408. vector_assign<scalar_assign> (*this, ae);
  1409. }
  1410. // Accessors
  1411. BOOST_UBLAS_INLINE
  1412. size_type size () const {
  1413. return size_;
  1414. }
  1415. BOOST_UBLAS_INLINE
  1416. size_type nnz_capacity () const {
  1417. return capacity_;
  1418. }
  1419. BOOST_UBLAS_INLINE
  1420. size_type nnz () const {
  1421. return filled_;
  1422. }
  1423. // Storage accessors
  1424. BOOST_UBLAS_INLINE
  1425. static size_type index_base () {
  1426. return IB;
  1427. }
  1428. BOOST_UBLAS_INLINE
  1429. typename index_array_type::size_type filled () const {
  1430. return filled_;
  1431. }
  1432. BOOST_UBLAS_INLINE
  1433. const index_array_type &index_data () const {
  1434. return index_data_;
  1435. }
  1436. BOOST_UBLAS_INLINE
  1437. const value_array_type &value_data () const {
  1438. return value_data_;
  1439. }
  1440. BOOST_UBLAS_INLINE
  1441. void set_filled (const typename index_array_type::size_type &sorted, const typename index_array_type::size_type &filled) {
  1442. sorted_filled_ = sorted;
  1443. filled_ = filled;
  1444. storage_invariants ();
  1445. }
  1446. BOOST_UBLAS_INLINE
  1447. index_array_type &index_data () {
  1448. return index_data_;
  1449. }
  1450. BOOST_UBLAS_INLINE
  1451. value_array_type &value_data () {
  1452. return value_data_;
  1453. }
  1454. // Resizing
  1455. private:
  1456. BOOST_UBLAS_INLINE
  1457. size_type restrict_capacity (size_type non_zeros) const {
  1458. // minimum non_zeros
  1459. non_zeros = (std::max) (non_zeros, size_type (1));
  1460. // ISSUE no maximum as coordinate may contain inserted duplicates
  1461. return non_zeros;
  1462. }
  1463. public:
  1464. BOOST_UBLAS_INLINE
  1465. void resize (size_type size, bool preserve = true) {
  1466. if (preserve)
  1467. sort (); // remove duplicate elements.
  1468. size_ = size;
  1469. capacity_ = restrict_capacity (capacity_);
  1470. if (preserve) {
  1471. index_data_. resize (capacity_, size_type ());
  1472. value_data_. resize (capacity_, value_type ());
  1473. filled_ = (std::min) (capacity_, filled_);
  1474. while ((filled_ > 0) && (zero_based(index_data_[filled_ - 1]) >= size)) {
  1475. --filled_;
  1476. }
  1477. }
  1478. else {
  1479. index_data_. resize (capacity_);
  1480. value_data_. resize (capacity_);
  1481. filled_ = 0;
  1482. }
  1483. sorted_filled_ = filled_;
  1484. storage_invariants ();
  1485. }
  1486. // Reserving
  1487. BOOST_UBLAS_INLINE
  1488. void reserve (size_type non_zeros, bool preserve = true) {
  1489. if (preserve)
  1490. sort (); // remove duplicate elements.
  1491. capacity_ = restrict_capacity (non_zeros);
  1492. if (preserve) {
  1493. index_data_. resize (capacity_, size_type ());
  1494. value_data_. resize (capacity_, value_type ());
  1495. filled_ = (std::min) (capacity_, filled_);
  1496. }
  1497. else {
  1498. index_data_. resize (capacity_);
  1499. value_data_. resize (capacity_);
  1500. filled_ = 0;
  1501. }
  1502. sorted_filled_ = filled_;
  1503. storage_invariants ();
  1504. }
  1505. // Element support
  1506. BOOST_UBLAS_INLINE
  1507. pointer find_element (size_type i) {
  1508. return const_cast<pointer> (const_cast<const self_type&>(*this).find_element (i));
  1509. }
  1510. BOOST_UBLAS_INLINE
  1511. const_pointer find_element (size_type i) const {
  1512. sort ();
  1513. const_subiterator_type it (detail::lower_bound (index_data_.begin (), index_data_.begin () + filled_, k_based (i), std::less<size_type> ()));
  1514. if (it == index_data_.begin () + filled_ || *it != k_based (i))
  1515. return 0;
  1516. return &value_data_ [it - index_data_.begin ()];
  1517. }
  1518. // Element access
  1519. BOOST_UBLAS_INLINE
  1520. const_reference operator () (size_type i) const {
  1521. BOOST_UBLAS_CHECK (i < size_, bad_index ());
  1522. sort ();
  1523. const_subiterator_type it (detail::lower_bound (index_data_.begin (), index_data_.begin () + filled_, k_based (i), std::less<size_type> ()));
  1524. if (it == index_data_.begin () + filled_ || *it != k_based (i))
  1525. return zero_;
  1526. return value_data_ [it - index_data_.begin ()];
  1527. }
  1528. BOOST_UBLAS_INLINE
  1529. true_reference ref (size_type i) {
  1530. BOOST_UBLAS_CHECK (i < size_, bad_index ());
  1531. sort ();
  1532. subiterator_type it (detail::lower_bound (index_data_.begin (), index_data_.begin () + filled_, k_based (i), std::less<size_type> ()));
  1533. if (it == index_data_.begin () + filled_ || *it != k_based (i))
  1534. return insert_element (i, value_type/*zero*/());
  1535. else
  1536. return value_data_ [it - index_data_.begin ()];
  1537. }
  1538. BOOST_UBLAS_INLINE
  1539. reference operator () (size_type i) {
  1540. #ifndef BOOST_UBLAS_STRICT_VECTOR_SPARSE
  1541. return ref (i);
  1542. #else
  1543. BOOST_UBLAS_CHECK (i < size_, bad_index ());
  1544. return reference (*this, i);
  1545. #endif
  1546. }
  1547. BOOST_UBLAS_INLINE
  1548. const_reference operator [] (size_type i) const {
  1549. return (*this) (i);
  1550. }
  1551. BOOST_UBLAS_INLINE
  1552. reference operator [] (size_type i) {
  1553. return (*this) (i);
  1554. }
  1555. // Element assignment
  1556. BOOST_UBLAS_INLINE
  1557. void append_element (size_type i, const_reference t) {
  1558. if (filled_ >= capacity_)
  1559. reserve (2 * filled_, true);
  1560. BOOST_UBLAS_CHECK (filled_ < capacity_, internal_logic ());
  1561. index_data_ [filled_] = k_based (i);
  1562. value_data_ [filled_] = t;
  1563. ++ filled_;
  1564. sorted_ = false;
  1565. storage_invariants ();
  1566. }
  1567. BOOST_UBLAS_INLINE
  1568. true_reference insert_element (size_type i, const_reference t) {
  1569. BOOST_UBLAS_CHECK (!find_element (i), bad_index ()); // duplicate element
  1570. append_element (i, t);
  1571. return value_data_ [filled_ - 1];
  1572. }
  1573. BOOST_UBLAS_INLINE
  1574. void erase_element (size_type i) {
  1575. sort ();
  1576. subiterator_type it (detail::lower_bound (index_data_.begin (), index_data_.begin () + filled_, k_based (i), std::less<size_type> ()));
  1577. typename std::iterator_traits<subiterator_type>::difference_type n = it - index_data_.begin ();
  1578. if (filled_ > typename index_array_type::size_type (n) && *it == k_based (i)) {
  1579. std::copy (it + 1, index_data_.begin () + filled_, it);
  1580. typename value_array_type::iterator itt (value_data_.begin () + n);
  1581. std::copy (itt + 1, value_data_.begin () + filled_, itt);
  1582. -- filled_;
  1583. sorted_filled_ = filled_;
  1584. }
  1585. storage_invariants ();
  1586. }
  1587. // Zeroing
  1588. BOOST_UBLAS_INLINE
  1589. void clear () {
  1590. filled_ = 0;
  1591. sorted_filled_ = filled_;
  1592. sorted_ = true;
  1593. storage_invariants ();
  1594. }
  1595. // Assignment
  1596. BOOST_UBLAS_INLINE
  1597. coordinate_vector &operator = (const coordinate_vector &v) {
  1598. if (this != &v) {
  1599. size_ = v.size_;
  1600. capacity_ = v.capacity_;
  1601. filled_ = v.filled_;
  1602. sorted_filled_ = v.sorted_filled_;
  1603. sorted_ = v.sorted_;
  1604. index_data_ = v.index_data_;
  1605. value_data_ = v.value_data_;
  1606. }
  1607. storage_invariants ();
  1608. return *this;
  1609. }
  1610. template<class C> // Container assignment without temporary
  1611. BOOST_UBLAS_INLINE
  1612. coordinate_vector &operator = (const vector_container<C> &v) {
  1613. resize (v ().size (), false);
  1614. assign (v);
  1615. return *this;
  1616. }
  1617. BOOST_UBLAS_INLINE
  1618. coordinate_vector &assign_temporary (coordinate_vector &v) {
  1619. swap (v);
  1620. return *this;
  1621. }
  1622. template<class AE>
  1623. BOOST_UBLAS_INLINE
  1624. coordinate_vector &operator = (const vector_expression<AE> &ae) {
  1625. self_type temporary (ae, capacity_);
  1626. return assign_temporary (temporary);
  1627. }
  1628. template<class AE>
  1629. BOOST_UBLAS_INLINE
  1630. coordinate_vector &assign (const vector_expression<AE> &ae) {
  1631. vector_assign<scalar_assign> (*this, ae);
  1632. return *this;
  1633. }
  1634. // Computed assignment
  1635. template<class AE>
  1636. BOOST_UBLAS_INLINE
  1637. coordinate_vector &operator += (const vector_expression<AE> &ae) {
  1638. self_type temporary (*this + ae, capacity_);
  1639. return assign_temporary (temporary);
  1640. }
  1641. template<class C> // Container assignment without temporary
  1642. BOOST_UBLAS_INLINE
  1643. coordinate_vector &operator += (const vector_container<C> &v) {
  1644. plus_assign (v);
  1645. return *this;
  1646. }
  1647. template<class AE>
  1648. BOOST_UBLAS_INLINE
  1649. coordinate_vector &plus_assign (const vector_expression<AE> &ae) {
  1650. vector_assign<scalar_plus_assign> (*this, ae);
  1651. return *this;
  1652. }
  1653. template<class AE>
  1654. BOOST_UBLAS_INLINE
  1655. coordinate_vector &operator -= (const vector_expression<AE> &ae) {
  1656. self_type temporary (*this - ae, capacity_);
  1657. return assign_temporary (temporary);
  1658. }
  1659. template<class C> // Container assignment without temporary
  1660. BOOST_UBLAS_INLINE
  1661. coordinate_vector &operator -= (const vector_container<C> &v) {
  1662. minus_assign (v);
  1663. return *this;
  1664. }
  1665. template<class AE>
  1666. BOOST_UBLAS_INLINE
  1667. coordinate_vector &minus_assign (const vector_expression<AE> &ae) {
  1668. vector_assign<scalar_minus_assign> (*this, ae);
  1669. return *this;
  1670. }
  1671. template<class AT>
  1672. BOOST_UBLAS_INLINE
  1673. coordinate_vector &operator *= (const AT &at) {
  1674. vector_assign_scalar<scalar_multiplies_assign> (*this, at);
  1675. return *this;
  1676. }
  1677. template<class AT>
  1678. BOOST_UBLAS_INLINE
  1679. coordinate_vector &operator /= (const AT &at) {
  1680. vector_assign_scalar<scalar_divides_assign> (*this, at);
  1681. return *this;
  1682. }
  1683. // Swapping
  1684. BOOST_UBLAS_INLINE
  1685. void swap (coordinate_vector &v) {
  1686. if (this != &v) {
  1687. std::swap (size_, v.size_);
  1688. std::swap (capacity_, v.capacity_);
  1689. std::swap (filled_, v.filled_);
  1690. std::swap (sorted_filled_, v.sorted_filled_);
  1691. std::swap (sorted_, v.sorted_);
  1692. index_data_.swap (v.index_data_);
  1693. value_data_.swap (v.value_data_);
  1694. }
  1695. storage_invariants ();
  1696. }
  1697. BOOST_UBLAS_INLINE
  1698. friend void swap (coordinate_vector &v1, coordinate_vector &v2) {
  1699. v1.swap (v2);
  1700. }
  1701. // replacement if STL lower bound algorithm for use of inplace_merge
  1702. size_type lower_bound (size_type beg, size_type end, size_type target) const {
  1703. while (end > beg) {
  1704. size_type mid = (beg + end) / 2;
  1705. if (index_data_[mid] < index_data_[target]) {
  1706. beg = mid + 1;
  1707. } else {
  1708. end = mid;
  1709. }
  1710. }
  1711. return beg;
  1712. }
  1713. // specialized replacement of STL inplace_merge to avoid compilation
  1714. // problems with respect to the array_triple iterator
  1715. void inplace_merge (size_type beg, size_type mid, size_type end) const {
  1716. size_type len_lef = mid - beg;
  1717. size_type len_rig = end - mid;
  1718. if (len_lef == 1 && len_rig == 1) {
  1719. if (index_data_[mid] < index_data_[beg]) {
  1720. std::swap(index_data_[beg], index_data_[mid]);
  1721. std::swap(value_data_[beg], value_data_[mid]);
  1722. }
  1723. } else if (len_lef > 0 && len_rig > 0) {
  1724. size_type lef_mid, rig_mid;
  1725. if (len_lef >= len_rig) {
  1726. lef_mid = (beg + mid) / 2;
  1727. rig_mid = lower_bound(mid, end, lef_mid);
  1728. } else {
  1729. rig_mid = (mid + end) / 2;
  1730. lef_mid = lower_bound(beg, mid, rig_mid);
  1731. }
  1732. std::rotate(&index_data_[0] + lef_mid, &index_data_[0] + mid, &index_data_[0] + rig_mid);
  1733. std::rotate(&value_data_[0] + lef_mid, &value_data_[0] + mid, &value_data_[0] + rig_mid);
  1734. size_type new_mid = lef_mid + rig_mid - mid;
  1735. inplace_merge(beg, lef_mid, new_mid);
  1736. inplace_merge(new_mid, rig_mid, end);
  1737. }
  1738. }
  1739. // Sorting and summation of duplicates
  1740. BOOST_UBLAS_INLINE
  1741. void sort () const {
  1742. if (! sorted_ && filled_ > 0) {
  1743. typedef index_pair_array<index_array_type, value_array_type> array_pair;
  1744. array_pair ipa (filled_, index_data_, value_data_);
  1745. #ifndef BOOST_UBLAS_COO_ALWAYS_DO_FULL_SORT
  1746. const typename array_pair::iterator iunsorted = ipa.begin () + sorted_filled_;
  1747. // sort new elements and merge
  1748. std::sort (iunsorted, ipa.end ());
  1749. inplace_merge(0, sorted_filled_, filled_);
  1750. #else
  1751. const typename array_pair::iterator iunsorted = ipa.begin ();
  1752. std::sort (iunsorted, ipa.end ());
  1753. #endif
  1754. // sum duplicates with += and remove
  1755. size_type filled = 0;
  1756. for (size_type i = 1; i < filled_; ++ i) {
  1757. if (index_data_ [filled] != index_data_ [i]) {
  1758. ++ filled;
  1759. if (filled != i) {
  1760. index_data_ [filled] = index_data_ [i];
  1761. value_data_ [filled] = value_data_ [i];
  1762. }
  1763. } else {
  1764. value_data_ [filled] += value_data_ [i];
  1765. }
  1766. }
  1767. filled_ = filled + 1;
  1768. sorted_filled_ = filled_;
  1769. sorted_ = true;
  1770. storage_invariants ();
  1771. }
  1772. }
  1773. // Back element insertion and erasure
  1774. BOOST_UBLAS_INLINE
  1775. void push_back (size_type i, const_reference t) {
  1776. // must maintain sort order
  1777. BOOST_UBLAS_CHECK (sorted_ && (filled_ == 0 || index_data_ [filled_ - 1] < k_based (i)), external_logic ());
  1778. if (filled_ >= capacity_)
  1779. reserve (2 * filled_, true);
  1780. BOOST_UBLAS_CHECK (filled_ < capacity_, internal_logic ());
  1781. index_data_ [filled_] = k_based (i);
  1782. value_data_ [filled_] = t;
  1783. ++ filled_;
  1784. sorted_filled_ = filled_;
  1785. storage_invariants ();
  1786. }
  1787. BOOST_UBLAS_INLINE
  1788. void pop_back () {
  1789. // ISSUE invariants could be simpilfied if sorted required as precondition
  1790. BOOST_UBLAS_CHECK (filled_ > 0, external_logic ());
  1791. -- filled_;
  1792. sorted_filled_ = (std::min) (sorted_filled_, filled_);
  1793. sorted_ = sorted_filled_ = filled_;
  1794. storage_invariants ();
  1795. }
  1796. // Iterator types
  1797. private:
  1798. // Use index array iterator
  1799. typedef typename IA::const_iterator const_subiterator_type;
  1800. typedef typename IA::iterator subiterator_type;
  1801. BOOST_UBLAS_INLINE
  1802. true_reference at_element (size_type i) {
  1803. BOOST_UBLAS_CHECK (i < size_, bad_index ());
  1804. sort ();
  1805. subiterator_type it (detail::lower_bound (index_data_.begin (), index_data_.begin () + filled_, k_based (i), std::less<size_type> ()));
  1806. BOOST_UBLAS_CHECK (it != index_data_.begin () + filled_ && *it == k_based (i), bad_index ());
  1807. return value_data_ [it - index_data_.begin ()];
  1808. }
  1809. public:
  1810. class const_iterator;
  1811. class iterator;
  1812. // Element lookup
  1813. // BOOST_UBLAS_INLINE This function seems to be big. So we do not let the compiler inline it.
  1814. const_iterator find (size_type i) const {
  1815. sort ();
  1816. return const_iterator (*this, detail::lower_bound (index_data_.begin (), index_data_.begin () + filled_, k_based (i), std::less<size_type> ()));
  1817. }
  1818. // BOOST_UBLAS_INLINE This function seems to be big. So we do not let the compiler inline it.
  1819. iterator find (size_type i) {
  1820. sort ();
  1821. return iterator (*this, detail::lower_bound (index_data_.begin (), index_data_.begin () + filled_, k_based (i), std::less<size_type> ()));
  1822. }
  1823. class const_iterator:
  1824. public container_const_reference<coordinate_vector>,
  1825. public bidirectional_iterator_base<sparse_bidirectional_iterator_tag,
  1826. const_iterator, value_type> {
  1827. public:
  1828. typedef typename coordinate_vector::value_type value_type;
  1829. typedef typename coordinate_vector::difference_type difference_type;
  1830. typedef typename coordinate_vector::const_reference reference;
  1831. typedef const typename coordinate_vector::pointer pointer;
  1832. // Construction and destruction
  1833. BOOST_UBLAS_INLINE
  1834. const_iterator ():
  1835. container_const_reference<self_type> (), it_ () {}
  1836. BOOST_UBLAS_INLINE
  1837. const_iterator (const self_type &v, const const_subiterator_type &it):
  1838. container_const_reference<self_type> (v), it_ (it) {}
  1839. BOOST_UBLAS_INLINE
  1840. const_iterator (const typename self_type::iterator &it): // ISSUE self_type:: stops VC8 using std::iterator here
  1841. container_const_reference<self_type> (it ()), it_ (it.it_) {}
  1842. // Arithmetic
  1843. BOOST_UBLAS_INLINE
  1844. const_iterator &operator ++ () {
  1845. ++ it_;
  1846. return *this;
  1847. }
  1848. BOOST_UBLAS_INLINE
  1849. const_iterator &operator -- () {
  1850. -- it_;
  1851. return *this;
  1852. }
  1853. // Dereference
  1854. BOOST_UBLAS_INLINE
  1855. const_reference operator * () const {
  1856. BOOST_UBLAS_CHECK (index () < (*this) ().size (), bad_index ());
  1857. return (*this) ().value_data_ [it_ - (*this) ().index_data_.begin ()];
  1858. }
  1859. // Index
  1860. BOOST_UBLAS_INLINE
  1861. size_type index () const {
  1862. BOOST_UBLAS_CHECK (*this != (*this) ().end (), bad_index ());
  1863. BOOST_UBLAS_CHECK ((*this) ().zero_based (*it_) < (*this) ().size (), bad_index ());
  1864. return (*this) ().zero_based (*it_);
  1865. }
  1866. // Assignment
  1867. BOOST_UBLAS_INLINE
  1868. const_iterator &operator = (const const_iterator &it) {
  1869. container_const_reference<self_type>::assign (&it ());
  1870. it_ = it.it_;
  1871. return *this;
  1872. }
  1873. // Comparison
  1874. BOOST_UBLAS_INLINE
  1875. bool operator == (const const_iterator &it) const {
  1876. BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ());
  1877. return it_ == it.it_;
  1878. }
  1879. private:
  1880. const_subiterator_type it_;
  1881. };
  1882. BOOST_UBLAS_INLINE
  1883. const_iterator begin () const {
  1884. return find (0);
  1885. }
  1886. BOOST_UBLAS_INLINE
  1887. const_iterator cbegin () const {
  1888. return begin();
  1889. }
  1890. BOOST_UBLAS_INLINE
  1891. const_iterator end () const {
  1892. return find (size_);
  1893. }
  1894. BOOST_UBLAS_INLINE
  1895. const_iterator cend () const {
  1896. return end();
  1897. }
  1898. class iterator:
  1899. public container_reference<coordinate_vector>,
  1900. public bidirectional_iterator_base<sparse_bidirectional_iterator_tag,
  1901. iterator, value_type> {
  1902. public:
  1903. typedef typename coordinate_vector::value_type value_type;
  1904. typedef typename coordinate_vector::difference_type difference_type;
  1905. typedef typename coordinate_vector::true_reference reference;
  1906. typedef typename coordinate_vector::pointer pointer;
  1907. // Construction and destruction
  1908. BOOST_UBLAS_INLINE
  1909. iterator ():
  1910. container_reference<self_type> (), it_ () {}
  1911. BOOST_UBLAS_INLINE
  1912. iterator (self_type &v, const subiterator_type &it):
  1913. container_reference<self_type> (v), it_ (it) {}
  1914. // Arithmetic
  1915. BOOST_UBLAS_INLINE
  1916. iterator &operator ++ () {
  1917. ++ it_;
  1918. return *this;
  1919. }
  1920. BOOST_UBLAS_INLINE
  1921. iterator &operator -- () {
  1922. -- it_;
  1923. return *this;
  1924. }
  1925. // Dereference
  1926. BOOST_UBLAS_INLINE
  1927. reference operator * () const {
  1928. BOOST_UBLAS_CHECK (index () < (*this) ().size (), bad_index ());
  1929. return (*this) ().value_data_ [it_ - (*this) ().index_data_.begin ()];
  1930. }
  1931. // Index
  1932. BOOST_UBLAS_INLINE
  1933. size_type index () const {
  1934. BOOST_UBLAS_CHECK (*this != (*this) ().end (), bad_index ());
  1935. BOOST_UBLAS_CHECK ((*this) ().zero_based (*it_) < (*this) ().size (), bad_index ());
  1936. return (*this) ().zero_based (*it_);
  1937. }
  1938. // Assignment
  1939. BOOST_UBLAS_INLINE
  1940. iterator &operator = (const iterator &it) {
  1941. container_reference<self_type>::assign (&it ());
  1942. it_ = it.it_;
  1943. return *this;
  1944. }
  1945. // Comparison
  1946. BOOST_UBLAS_INLINE
  1947. bool operator == (const iterator &it) const {
  1948. BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ());
  1949. return it_ == it.it_;
  1950. }
  1951. private:
  1952. subiterator_type it_;
  1953. friend class const_iterator;
  1954. };
  1955. BOOST_UBLAS_INLINE
  1956. iterator begin () {
  1957. return find (0);
  1958. }
  1959. BOOST_UBLAS_INLINE
  1960. iterator end () {
  1961. return find (size_);
  1962. }
  1963. // Reverse iterator
  1964. typedef reverse_iterator_base<const_iterator> const_reverse_iterator;
  1965. typedef reverse_iterator_base<iterator> reverse_iterator;
  1966. BOOST_UBLAS_INLINE
  1967. const_reverse_iterator rbegin () const {
  1968. return const_reverse_iterator (end ());
  1969. }
  1970. BOOST_UBLAS_INLINE
  1971. const_reverse_iterator crbegin () const {
  1972. return rbegin ();
  1973. }
  1974. BOOST_UBLAS_INLINE
  1975. const_reverse_iterator rend () const {
  1976. return const_reverse_iterator (begin ());
  1977. }
  1978. BOOST_UBLAS_INLINE
  1979. const_reverse_iterator crend () const {
  1980. return rend ();
  1981. }
  1982. BOOST_UBLAS_INLINE
  1983. reverse_iterator rbegin () {
  1984. return reverse_iterator (end ());
  1985. }
  1986. BOOST_UBLAS_INLINE
  1987. reverse_iterator rend () {
  1988. return reverse_iterator (begin ());
  1989. }
  1990. // Serialization
  1991. template<class Archive>
  1992. void serialize(Archive & ar, const unsigned int /* file_version */){
  1993. serialization::collection_size_type s (size_);
  1994. ar & serialization::make_nvp("size",s);
  1995. if (Archive::is_loading::value) {
  1996. size_ = s;
  1997. }
  1998. // ISSUE: filled may be much less than capacity
  1999. // ISSUE: index_data_ and value_data_ are undefined between filled and capacity (trouble with 'nan'-values)
  2000. ar & serialization::make_nvp("capacity", capacity_);
  2001. ar & serialization::make_nvp("filled", filled_);
  2002. ar & serialization::make_nvp("sorted_filled", sorted_filled_);
  2003. ar & serialization::make_nvp("sorted", sorted_);
  2004. ar & serialization::make_nvp("index_data", index_data_);
  2005. ar & serialization::make_nvp("value_data", value_data_);
  2006. storage_invariants();
  2007. }
  2008. private:
  2009. void storage_invariants () const
  2010. {
  2011. BOOST_UBLAS_CHECK (capacity_ == index_data_.size (), internal_logic ());
  2012. BOOST_UBLAS_CHECK (capacity_ == value_data_.size (), internal_logic ());
  2013. BOOST_UBLAS_CHECK (filled_ <= capacity_, internal_logic ());
  2014. BOOST_UBLAS_CHECK (sorted_filled_ <= filled_, internal_logic ());
  2015. BOOST_UBLAS_CHECK (sorted_ == (sorted_filled_ == filled_), internal_logic ());
  2016. BOOST_UBLAS_CHECK ((0 == filled_) || (zero_based(index_data_[filled_ - 1]) < size_), internal_logic ());
  2017. }
  2018. size_type size_;
  2019. size_type capacity_;
  2020. mutable typename index_array_type::size_type filled_;
  2021. mutable typename index_array_type::size_type sorted_filled_;
  2022. mutable bool sorted_;
  2023. mutable index_array_type index_data_;
  2024. mutable value_array_type value_data_;
  2025. static const value_type zero_;
  2026. BOOST_UBLAS_INLINE
  2027. static size_type zero_based (size_type k_based_index) {
  2028. return k_based_index - IB;
  2029. }
  2030. BOOST_UBLAS_INLINE
  2031. static size_type k_based (size_type zero_based_index) {
  2032. return zero_based_index + IB;
  2033. }
  2034. friend class iterator;
  2035. friend class const_iterator;
  2036. };
  2037. template<class T, std::size_t IB, class IA, class TA>
  2038. const typename coordinate_vector<T, IB, IA, TA>::value_type coordinate_vector<T, IB, IA, TA>::zero_ = value_type/*zero*/();
  2039. }}}
  2040. #endif