vector_sparse.hpp 82 KB

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