| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247 |
- //
- // Copyright (c) 2000-2002
- // Joerg Walter, Mathias Koch
- //
- // Distributed under the Boost Software License, Version 1.0. (See
- // accompanying file LICENSE_1_0.txt or copy at
- // http://www.boost.org/LICENSE_1_0.txt)
- //
- // The authors gratefully acknowledge the support of
- // GeNeSys mbH & Co. KG in producing this work.
- //
- #ifndef _BOOST_UBLAS_VECTOR_SPARSE_
- #define _BOOST_UBLAS_VECTOR_SPARSE_
- #include <boost/config.hpp>
- // In debug mode, MSCV enables iterator debugging, which additional checks are
- // executed for consistency. So, when two iterators are compared, it is tested
- // that they point to elements of the same container. If the check fails, then
- // the program is aborted.
- //
- // When matrices MVOV are traversed by column and then by row, the previous
- // check fails.
- //
- // MVOV::iterator2 iter2 = mvov.begin2();
- // for (; iter2 != mvov.end() ; iter2++) {
- // MVOV::iterator1 iter1 = iter2.begin();
- // .....
- // }
- //
- // These additional checks in iterators are disabled in this file, but their
- // status are restored at the end of file.
- // https://msdn.microsoft.com/en-us/library/hh697468.aspx
- #ifdef BOOST_MSVC
- #define _BACKUP_ITERATOR_DEBUG_LEVEL _ITERATOR_DEBUG_LEVEL
- #undef _ITERATOR_DEBUG_LEVEL
- #define _ITERATOR_DEBUG_LEVEL 0
- #endif
- #include <boost/numeric/ublas/storage_sparse.hpp>
- #include <boost/numeric/ublas/vector_expression.hpp>
- #include <boost/numeric/ublas/detail/vector_assign.hpp>
- #if BOOST_UBLAS_TYPE_CHECK
- #include <boost/numeric/ublas/vector.hpp>
- #endif
- // Iterators based on ideas of Jeremy Siek
- namespace boost { namespace numeric { namespace ublas {
- #ifdef BOOST_UBLAS_STRICT_VECTOR_SPARSE
- template<class V>
- class sparse_vector_element:
- public container_reference<V> {
- public:
- typedef V vector_type;
- typedef typename V::size_type size_type;
- typedef typename V::value_type value_type;
- typedef const value_type &const_reference;
- typedef value_type *pointer;
- private:
- // Proxied element operations
- void get_d () const {
- pointer p = (*this) ().find_element (i_);
- if (p)
- d_ = *p;
- else
- d_ = value_type/*zero*/();
- }
- void set (const value_type &s) const {
- pointer p = (*this) ().find_element (i_);
- if (!p)
- (*this) ().insert_element (i_, s);
- else
- *p = s;
- }
- public:
- // Construction and destruction
- sparse_vector_element (vector_type &v, size_type i):
- container_reference<vector_type> (v), i_ (i) {
- }
- BOOST_UBLAS_INLINE
- sparse_vector_element (const sparse_vector_element &p):
- container_reference<vector_type> (p), i_ (p.i_) {}
- BOOST_UBLAS_INLINE
- ~sparse_vector_element () {
- }
- // Assignment
- BOOST_UBLAS_INLINE
- sparse_vector_element &operator = (const sparse_vector_element &p) {
- // Overide the implict copy assignment
- p.get_d ();
- set (p.d_);
- return *this;
- }
- template<class D>
- BOOST_UBLAS_INLINE
- sparse_vector_element &operator = (const D &d) {
- set (d);
- return *this;
- }
- template<class D>
- BOOST_UBLAS_INLINE
- sparse_vector_element &operator += (const D &d) {
- get_d ();
- d_ += d;
- set (d_);
- return *this;
- }
- template<class D>
- BOOST_UBLAS_INLINE
- sparse_vector_element &operator -= (const D &d) {
- get_d ();
- d_ -= d;
- set (d_);
- return *this;
- }
- template<class D>
- BOOST_UBLAS_INLINE
- sparse_vector_element &operator *= (const D &d) {
- get_d ();
- d_ *= d;
- set (d_);
- return *this;
- }
- template<class D>
- BOOST_UBLAS_INLINE
- sparse_vector_element &operator /= (const D &d) {
- get_d ();
- d_ /= d;
- set (d_);
- return *this;
- }
- // Comparison
- template<class D>
- BOOST_UBLAS_INLINE
- bool operator == (const D &d) const {
- get_d ();
- return d_ == d;
- }
- template<class D>
- BOOST_UBLAS_INLINE
- bool operator != (const D &d) const {
- get_d ();
- return d_ != d;
- }
- // Conversion - weak link in proxy as d_ is not a perfect alias for the element
- BOOST_UBLAS_INLINE
- operator const_reference () const {
- get_d ();
- return d_;
- }
- // Conversion to reference - may be invalidated
- BOOST_UBLAS_INLINE
- value_type& ref () const {
- const pointer p = (*this) ().find_element (i_);
- if (!p)
- return (*this) ().insert_element (i_, value_type/*zero*/());
- else
- return *p;
- }
- private:
- size_type i_;
- mutable value_type d_;
- };
- /*
- * Generalise explicit reference access
- */
- namespace detail {
- template <class R>
- struct element_reference {
- typedef R& reference;
- static reference get_reference (reference r)
- {
- return r;
- }
- };
- template <class V>
- struct element_reference<sparse_vector_element<V> > {
- typedef typename V::value_type& reference;
- static reference get_reference (const sparse_vector_element<V>& sve)
- {
- return sve.ref ();
- }
- };
- }
- template <class VER>
- typename detail::element_reference<VER>::reference ref (VER& ver) {
- return detail::element_reference<VER>::get_reference (ver);
- }
- template <class VER>
- typename detail::element_reference<VER>::reference ref (const VER& ver) {
- return detail::element_reference<VER>::get_reference (ver);
- }
- template<class V>
- struct type_traits<sparse_vector_element<V> > {
- typedef typename V::value_type element_type;
- typedef type_traits<sparse_vector_element<V> > self_type;
- typedef typename type_traits<element_type>::value_type value_type;
- typedef typename type_traits<element_type>::const_reference const_reference;
- typedef sparse_vector_element<V> reference;
- typedef typename type_traits<element_type>::real_type real_type;
- typedef typename type_traits<element_type>::precision_type precision_type;
- static const unsigned plus_complexity = type_traits<element_type>::plus_complexity;
- static const unsigned multiplies_complexity = type_traits<element_type>::multiplies_complexity;
- static
- BOOST_UBLAS_INLINE
- real_type real (const_reference t) {
- return type_traits<element_type>::real (t);
- }
- static
- BOOST_UBLAS_INLINE
- real_type imag (const_reference t) {
- return type_traits<element_type>::imag (t);
- }
- static
- BOOST_UBLAS_INLINE
- value_type conj (const_reference t) {
- return type_traits<element_type>::conj (t);
- }
- static
- BOOST_UBLAS_INLINE
- real_type type_abs (const_reference t) {
- return type_traits<element_type>::type_abs (t);
- }
- static
- BOOST_UBLAS_INLINE
- value_type type_sqrt (const_reference t) {
- return type_traits<element_type>::type_sqrt (t);
- }
- static
- BOOST_UBLAS_INLINE
- real_type norm_1 (const_reference t) {
- return type_traits<element_type>::norm_1 (t);
- }
- static
- BOOST_UBLAS_INLINE
- real_type norm_2 (const_reference t) {
- return type_traits<element_type>::norm_2 (t);
- }
- static
- BOOST_UBLAS_INLINE
- real_type norm_inf (const_reference t) {
- return type_traits<element_type>::norm_inf (t);
- }
- static
- BOOST_UBLAS_INLINE
- bool equals (const_reference t1, const_reference t2) {
- return type_traits<element_type>::equals (t1, t2);
- }
- };
- template<class V1, class T2>
- struct promote_traits<sparse_vector_element<V1>, T2> {
- typedef typename promote_traits<typename sparse_vector_element<V1>::value_type, T2>::promote_type promote_type;
- };
- template<class T1, class V2>
- struct promote_traits<T1, sparse_vector_element<V2> > {
- typedef typename promote_traits<T1, typename sparse_vector_element<V2>::value_type>::promote_type promote_type;
- };
- template<class V1, class V2>
- struct promote_traits<sparse_vector_element<V1>, sparse_vector_element<V2> > {
- typedef typename promote_traits<typename sparse_vector_element<V1>::value_type,
- typename sparse_vector_element<V2>::value_type>::promote_type promote_type;
- };
- #endif
- /** \brief Index map based sparse vector
- *
- * A sparse vector of values of type T of variable size. The sparse storage type A can be
- * \c std::map<size_t, T> or \c map_array<size_t, T>. This means that only non-zero elements
- * are effectively stored.
- *
- * For a \f$n\f$-dimensional sparse vector, and 0 <= i < n the non-zero elements \f$v_i\f$
- * are mapped to consecutive elements of the associative container, i.e. for elements
- * \f$k = v_{i_1}\f$ and \f$k + 1 = v_{i_2}\f$ of the container, holds \f$i_1 < i_2\f$.
- *
- * Supported parameters for the adapted array are \c map_array<std::size_t, T> and
- * \c map_std<std::size_t, T>. The latter is equivalent to \c std::map<std::size_t, T>.
- *
- * \tparam T the type of object stored in the vector (like double, float, complex, etc...)
- * \tparam A the type of Storage array
- */
- template<class T, class A>
- class mapped_vector:
- public vector_container<mapped_vector<T, A> > {
- typedef T &true_reference;
- typedef T *pointer;
- typedef const T *const_pointer;
- typedef mapped_vector<T, A> self_type;
- public:
- #ifdef BOOST_UBLAS_ENABLE_PROXY_SHORTCUTS
- using vector_container<self_type>::operator ();
- #endif
- typedef typename A::size_type size_type;
- typedef typename A::difference_type difference_type;
- typedef T value_type;
- typedef A array_type;
- typedef const value_type &const_reference;
- #ifndef BOOST_UBLAS_STRICT_VECTOR_SPARSE
- typedef typename detail::map_traits<A,T>::reference reference;
- #else
- typedef sparse_vector_element<self_type> reference;
- #endif
- typedef const vector_reference<const self_type> const_closure_type;
- typedef vector_reference<self_type> closure_type;
- typedef self_type vector_temporary_type;
- typedef sparse_tag storage_category;
- // Construction and destruction
- BOOST_UBLAS_INLINE
- mapped_vector ():
- vector_container<self_type> (),
- size_ (0), data_ () {}
- BOOST_UBLAS_INLINE
- mapped_vector (size_type size, size_type non_zeros = 0):
- vector_container<self_type> (),
- size_ (size), data_ () {
- detail::map_reserve (data(), restrict_capacity (non_zeros));
- }
- BOOST_UBLAS_INLINE
- mapped_vector (const mapped_vector &v):
- vector_container<self_type> (),
- size_ (v.size_), data_ (v.data_) {}
- template<class AE>
- BOOST_UBLAS_INLINE
- mapped_vector (const vector_expression<AE> &ae, size_type non_zeros = 0):
- vector_container<self_type> (),
- size_ (ae ().size ()), data_ () {
- detail::map_reserve (data(), restrict_capacity (non_zeros));
- vector_assign<scalar_assign> (*this, ae);
- }
- // Accessors
- BOOST_UBLAS_INLINE
- size_type size () const {
- return size_;
- }
- BOOST_UBLAS_INLINE
- size_type nnz_capacity () const {
- return detail::map_capacity (data ());
- }
- BOOST_UBLAS_INLINE
- size_type nnz () const {
- return data (). size ();
- }
- // Storage accessors
- BOOST_UBLAS_INLINE
- const array_type &data () const {
- return data_;
- }
- BOOST_UBLAS_INLINE
- array_type &data () {
- return data_;
- }
- // Resizing
- private:
- BOOST_UBLAS_INLINE
- size_type restrict_capacity (size_type non_zeros) const {
- non_zeros = (std::min) (non_zeros, size_);
- return non_zeros;
- }
- public:
- BOOST_UBLAS_INLINE
- void resize (size_type size, bool preserve = true) {
- size_ = size;
- if (preserve) {
- data ().erase (data ().lower_bound(size_), data ().end());
- }
- else {
- data ().clear ();
- }
- }
- // Reserving
- BOOST_UBLAS_INLINE
- void reserve (size_type non_zeros, bool /*preserve*/ = true) {
- detail::map_reserve (data (), restrict_capacity (non_zeros));
- }
- // Element support
- BOOST_UBLAS_INLINE
- pointer find_element (size_type i) {
- return const_cast<pointer> (const_cast<const self_type&>(*this).find_element (i));
- }
- BOOST_UBLAS_INLINE
- const_pointer find_element (size_type i) const {
- const_subiterator_type it (data ().find (i));
- if (it == data ().end ())
- return 0;
- BOOST_UBLAS_CHECK ((*it).first == i, internal_logic ()); // broken map
- return &(*it).second;
- }
- // Element access
- BOOST_UBLAS_INLINE
- const_reference operator () (size_type i) const {
- BOOST_UBLAS_CHECK (i < size_, bad_index ());
- const_subiterator_type it (data ().find (i));
- if (it == data ().end ())
- return zero_;
- BOOST_UBLAS_CHECK ((*it).first == i, internal_logic ()); // broken map
- return (*it).second;
- }
- BOOST_UBLAS_INLINE
- true_reference ref (size_type i) {
- BOOST_UBLAS_CHECK (i < size_, bad_index ());
- std::pair<subiterator_type, bool> ii (data ().insert (typename array_type::value_type (i, value_type/*zero*/())));
- BOOST_UBLAS_CHECK ((ii.first)->first == i, internal_logic ()); // broken map
- return (ii.first)->second;
- }
- BOOST_UBLAS_INLINE
- reference operator () (size_type i) {
- #ifndef BOOST_UBLAS_STRICT_VECTOR_SPARSE
- return ref (i);
- #else
- BOOST_UBLAS_CHECK (i < size_, bad_index ());
- return reference (*this, i);
- #endif
- }
- BOOST_UBLAS_INLINE
- const_reference operator [] (size_type i) const {
- return (*this) (i);
- }
- BOOST_UBLAS_INLINE
- reference operator [] (size_type i) {
- return (*this) (i);
- }
- // Element assignment
- BOOST_UBLAS_INLINE
- true_reference insert_element (size_type i, const_reference t) {
- std::pair<subiterator_type, bool> ii = data ().insert (typename array_type::value_type (i, t));
- BOOST_UBLAS_CHECK (ii.second, bad_index ()); // duplicate element
- BOOST_UBLAS_CHECK ((ii.first)->first == i, internal_logic ()); // broken map
- if (!ii.second) // existing element
- (ii.first)->second = t;
- return (ii.first)->second;
- }
- BOOST_UBLAS_INLINE
- void erase_element (size_type i) {
- subiterator_type it = data ().find (i);
- if (it == data ().end ())
- return;
- data ().erase (it);
- }
- // Zeroing
- BOOST_UBLAS_INLINE
- void clear () {
- data ().clear ();
- }
- // Assignment
- BOOST_UBLAS_INLINE
- mapped_vector &operator = (const mapped_vector &v) {
- if (this != &v) {
- size_ = v.size_;
- data () = v.data ();
- }
- return *this;
- }
- template<class C> // Container assignment without temporary
- BOOST_UBLAS_INLINE
- mapped_vector &operator = (const vector_container<C> &v) {
- resize (v ().size (), false);
- assign (v);
- return *this;
- }
- BOOST_UBLAS_INLINE
- mapped_vector &assign_temporary (mapped_vector &v) {
- swap (v);
- return *this;
- }
- template<class AE>
- BOOST_UBLAS_INLINE
- mapped_vector &operator = (const vector_expression<AE> &ae) {
- self_type temporary (ae, detail::map_capacity (data()));
- return assign_temporary (temporary);
- }
- template<class AE>
- BOOST_UBLAS_INLINE
- mapped_vector &assign (const vector_expression<AE> &ae) {
- vector_assign<scalar_assign> (*this, ae);
- return *this;
- }
- // Computed assignment
- template<class AE>
- BOOST_UBLAS_INLINE
- mapped_vector &operator += (const vector_expression<AE> &ae) {
- self_type temporary (*this + ae, detail::map_capacity (data()));
- return assign_temporary (temporary);
- }
- template<class C> // Container assignment without temporary
- BOOST_UBLAS_INLINE
- mapped_vector &operator += (const vector_container<C> &v) {
- plus_assign (v);
- return *this;
- }
- template<class AE>
- BOOST_UBLAS_INLINE
- mapped_vector &plus_assign (const vector_expression<AE> &ae) {
- vector_assign<scalar_plus_assign> (*this, ae);
- return *this;
- }
- template<class AE>
- BOOST_UBLAS_INLINE
- mapped_vector &operator -= (const vector_expression<AE> &ae) {
- self_type temporary (*this - ae, detail::map_capacity (data()));
- return assign_temporary (temporary);
- }
- template<class C> // Container assignment without temporary
- BOOST_UBLAS_INLINE
- mapped_vector &operator -= (const vector_container<C> &v) {
- minus_assign (v);
- return *this;
- }
- template<class AE>
- BOOST_UBLAS_INLINE
- mapped_vector &minus_assign (const vector_expression<AE> &ae) {
- vector_assign<scalar_minus_assign> (*this, ae);
- return *this;
- }
- template<class AT>
- BOOST_UBLAS_INLINE
- mapped_vector &operator *= (const AT &at) {
- vector_assign_scalar<scalar_multiplies_assign> (*this, at);
- return *this;
- }
- template<class AT>
- BOOST_UBLAS_INLINE
- mapped_vector &operator /= (const AT &at) {
- vector_assign_scalar<scalar_divides_assign> (*this, at);
- return *this;
- }
- // Swapping
- BOOST_UBLAS_INLINE
- void swap (mapped_vector &v) {
- if (this != &v) {
- std::swap (size_, v.size_);
- data ().swap (v.data ());
- }
- }
- BOOST_UBLAS_INLINE
- friend void swap (mapped_vector &v1, mapped_vector &v2) {
- v1.swap (v2);
- }
- // Iterator types
- private:
- // Use storage iterator
- typedef typename A::const_iterator const_subiterator_type;
- typedef typename A::iterator subiterator_type;
- BOOST_UBLAS_INLINE
- true_reference at_element (size_type i) {
- BOOST_UBLAS_CHECK (i < size_, bad_index ());
- subiterator_type it (data ().find (i));
- BOOST_UBLAS_CHECK (it != data ().end(), bad_index ());
- BOOST_UBLAS_CHECK ((*it).first == i, internal_logic ()); // broken map
- return it->second;
- }
- public:
- class const_iterator;
- class iterator;
- // Element lookup
- // BOOST_UBLAS_INLINE This function seems to be big. So we do not let the compiler inline it.
- const_iterator find (size_type i) const {
- return const_iterator (*this, data ().lower_bound (i));
- }
- // BOOST_UBLAS_INLINE This function seems to be big. So we do not let the compiler inline it.
- iterator find (size_type i) {
- return iterator (*this, data ().lower_bound (i));
- }
- class const_iterator:
- public container_const_reference<mapped_vector>,
- public bidirectional_iterator_base<sparse_bidirectional_iterator_tag,
- const_iterator, value_type> {
- public:
- typedef typename mapped_vector::value_type value_type;
- typedef typename mapped_vector::difference_type difference_type;
- typedef typename mapped_vector::const_reference reference;
- typedef const typename mapped_vector::pointer pointer;
- // Construction and destruction
- BOOST_UBLAS_INLINE
- const_iterator ():
- container_const_reference<self_type> (), it_ () {}
- BOOST_UBLAS_INLINE
- const_iterator (const self_type &v, const const_subiterator_type &it):
- container_const_reference<self_type> (v), it_ (it) {}
- BOOST_UBLAS_INLINE
- const_iterator (const typename self_type::iterator &it): // ISSUE self_type:: stops VC8 using std::iterator here
- container_const_reference<self_type> (it ()), it_ (it.it_) {}
- // Arithmetic
- BOOST_UBLAS_INLINE
- const_iterator &operator ++ () {
- ++ it_;
- return *this;
- }
- BOOST_UBLAS_INLINE
- const_iterator &operator -- () {
- -- it_;
- return *this;
- }
- // Dereference
- BOOST_UBLAS_INLINE
- const_reference operator * () const {
- BOOST_UBLAS_CHECK (index () < (*this) ().size (), bad_index ());
- return (*it_).second;
- }
- // Index
- BOOST_UBLAS_INLINE
- size_type index () const {
- BOOST_UBLAS_CHECK (*this != (*this) ().end (), bad_index ());
- BOOST_UBLAS_CHECK ((*it_).first < (*this) ().size (), bad_index ());
- return (*it_).first;
- }
- // Assignment
- BOOST_UBLAS_INLINE
- const_iterator &operator = (const const_iterator &it) {
- container_const_reference<self_type>::assign (&it ());
- it_ = it.it_;
- return *this;
- }
- // Comparison
- BOOST_UBLAS_INLINE
- bool operator == (const const_iterator &it) const {
- BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ());
- return it_ == it.it_;
- }
- private:
- const_subiterator_type it_;
- };
- BOOST_UBLAS_INLINE
- const_iterator begin () const {
- return const_iterator (*this, data ().begin ());
- }
- BOOST_UBLAS_INLINE
- const_iterator cbegin () const {
- return begin ();
- }
- BOOST_UBLAS_INLINE
- const_iterator end () const {
- return const_iterator (*this, data ().end ());
- }
- BOOST_UBLAS_INLINE
- const_iterator cend () const {
- return end ();
- }
- class iterator:
- public container_reference<mapped_vector>,
- public bidirectional_iterator_base<sparse_bidirectional_iterator_tag,
- iterator, value_type> {
- public:
- typedef typename mapped_vector::value_type value_type;
- typedef typename mapped_vector::difference_type difference_type;
- typedef typename mapped_vector::true_reference reference;
- typedef typename mapped_vector::pointer pointer;
- // Construction and destruction
- BOOST_UBLAS_INLINE
- iterator ():
- container_reference<self_type> (), it_ () {}
- BOOST_UBLAS_INLINE
- iterator (self_type &v, const subiterator_type &it):
- container_reference<self_type> (v), it_ (it) {}
- // Arithmetic
- BOOST_UBLAS_INLINE
- iterator &operator ++ () {
- ++ it_;
- return *this;
- }
- BOOST_UBLAS_INLINE
- iterator &operator -- () {
- -- it_;
- return *this;
- }
- // Dereference
- BOOST_UBLAS_INLINE
- reference operator * () const {
- BOOST_UBLAS_CHECK (index () < (*this) ().size (), bad_index ());
- return (*it_).second;
- }
- // Index
- BOOST_UBLAS_INLINE
- size_type index () const {
- BOOST_UBLAS_CHECK (*this != (*this) ().end (), bad_index ());
- BOOST_UBLAS_CHECK ((*it_).first < (*this) ().size (), bad_index ());
- return (*it_).first;
- }
- // Assignment
- BOOST_UBLAS_INLINE
- iterator &operator = (const iterator &it) {
- container_reference<self_type>::assign (&it ());
- it_ = it.it_;
- return *this;
- }
- // Comparison
- BOOST_UBLAS_INLINE
- bool operator == (const iterator &it) const {
- BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ());
- return it_ == it.it_;
- }
- private:
- subiterator_type it_;
- friend class const_iterator;
- };
- BOOST_UBLAS_INLINE
- iterator begin () {
- return iterator (*this, data ().begin ());
- }
- BOOST_UBLAS_INLINE
- iterator end () {
- return iterator (*this, data ().end ());
- }
- // Reverse iterator
- typedef reverse_iterator_base<const_iterator> const_reverse_iterator;
- typedef reverse_iterator_base<iterator> reverse_iterator;
- BOOST_UBLAS_INLINE
- const_reverse_iterator rbegin () const {
- return const_reverse_iterator (end ());
- }
- BOOST_UBLAS_INLINE
- const_reverse_iterator crbegin () const {
- return rbegin ();
- }
- BOOST_UBLAS_INLINE
- const_reverse_iterator rend () const {
- return const_reverse_iterator (begin ());
- }
- BOOST_UBLAS_INLINE
- const_reverse_iterator crend () const {
- return rend ();
- }
- BOOST_UBLAS_INLINE
- reverse_iterator rbegin () {
- return reverse_iterator (end ());
- }
- BOOST_UBLAS_INLINE
- reverse_iterator rend () {
- return reverse_iterator (begin ());
- }
- // Serialization
- template<class Archive>
- void serialize(Archive & ar, const unsigned int /* file_version */){
- serialization::collection_size_type s (size_);
- ar & serialization::make_nvp("size",s);
- if (Archive::is_loading::value) {
- size_ = s;
- }
- ar & serialization::make_nvp("data", data_);
- }
- private:
- size_type size_;
- array_type data_;
- static const value_type zero_;
- };
- template<class T, class A>
- const typename mapped_vector<T, A>::value_type mapped_vector<T, A>::zero_ = value_type/*zero*/();
- // Thanks to Kresimir Fresl for extending this to cover different index bases.
-
- /** \brief Compressed array based sparse vector
- *
- * a sparse vector of values of type T of variable size. The non zero values are stored as
- * two seperate arrays: an index array and a value array. The index array is always sorted
- * and there is at most one entry for each index. Inserting an element can be time consuming.
- * If the vector contains a few zero entries, then it is better to have a normal vector.
- * If the vector has a very high dimension with a few non-zero values, then this vector is
- * very memory efficient (at the cost of a few more computations).
- *
- * For a \f$n\f$-dimensional compressed vector and \f$0 \leq i < n\f$ the non-zero elements
- * \f$v_i\f$ are mapped to consecutive elements of the index and value container, i.e. for
- * 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$.
- *
- * Supported parameters for the adapted array (indices and values) are \c unbounded_array<> ,
- * \c bounded_array<> and \c std::vector<>.
- *
- * \tparam T the type of object stored in the vector (like double, float, complex, etc...)
- * \tparam IB the index base of the compressed vector. Default is 0. Other supported value is 1
- * \tparam IA the type of adapted array for indices. Default is \c unbounded_array<std::size_t>
- * \tparam TA the type of adapted array for values. Default is unbounded_array<T>
- */
- template<class T, std::size_t IB, class IA, class TA>
- class compressed_vector:
- public vector_container<compressed_vector<T, IB, IA, TA> > {
- typedef T &true_reference;
- typedef T *pointer;
- typedef const T *const_pointer;
- typedef compressed_vector<T, IB, IA, TA> self_type;
- public:
- #ifdef BOOST_UBLAS_ENABLE_PROXY_SHORTCUTS
- using vector_container<self_type>::operator ();
- #endif
- // ISSUE require type consistency check
- // is_convertable (IA::size_type, TA::size_type)
- typedef typename IA::value_type size_type;
- typedef typename IA::difference_type difference_type;
- typedef T value_type;
- typedef const T &const_reference;
- #ifndef BOOST_UBLAS_STRICT_VECTOR_SPARSE
- typedef T &reference;
- #else
- typedef sparse_vector_element<self_type> reference;
- #endif
- typedef IA index_array_type;
- typedef TA value_array_type;
- typedef const vector_reference<const self_type> const_closure_type;
- typedef vector_reference<self_type> closure_type;
- typedef self_type vector_temporary_type;
- typedef sparse_tag storage_category;
- // Construction and destruction
- BOOST_UBLAS_INLINE
- compressed_vector ():
- vector_container<self_type> (),
- size_ (0), capacity_ (restrict_capacity (0)), filled_ (0),
- index_data_ (capacity_), value_data_ (capacity_) {
- storage_invariants ();
- }
- explicit BOOST_UBLAS_INLINE
- compressed_vector (size_type size, size_type non_zeros = 0):
- vector_container<self_type> (),
- size_ (size), capacity_ (restrict_capacity (non_zeros)), filled_ (0),
- index_data_ (capacity_), value_data_ (capacity_) {
- storage_invariants ();
- }
- BOOST_UBLAS_INLINE
- compressed_vector (const compressed_vector &v):
- vector_container<self_type> (),
- size_ (v.size_), capacity_ (v.capacity_), filled_ (v.filled_),
- index_data_ (v.index_data_), value_data_ (v.value_data_) {
- storage_invariants ();
- }
- template<class AE>
- BOOST_UBLAS_INLINE
- compressed_vector (const vector_expression<AE> &ae, size_type non_zeros = 0):
- vector_container<self_type> (),
- size_ (ae ().size ()), capacity_ (restrict_capacity (non_zeros)), filled_ (0),
- index_data_ (capacity_), value_data_ (capacity_) {
- storage_invariants ();
- vector_assign<scalar_assign> (*this, ae);
- }
- // Accessors
- BOOST_UBLAS_INLINE
- size_type size () const {
- return size_;
- }
- BOOST_UBLAS_INLINE
- size_type nnz_capacity () const {
- return capacity_;
- }
- BOOST_UBLAS_INLINE
- size_type nnz () const {
- return filled_;
- }
- // Storage accessors
- BOOST_UBLAS_INLINE
- static size_type index_base () {
- return IB;
- }
- BOOST_UBLAS_INLINE
- typename index_array_type::size_type filled () const {
- return filled_;
- }
- BOOST_UBLAS_INLINE
- const index_array_type &index_data () const {
- return index_data_;
- }
- BOOST_UBLAS_INLINE
- const value_array_type &value_data () const {
- return value_data_;
- }
- BOOST_UBLAS_INLINE
- void set_filled (const typename index_array_type::size_type & filled) {
- filled_ = filled;
- storage_invariants ();
- }
- BOOST_UBLAS_INLINE
- index_array_type &index_data () {
- return index_data_;
- }
- BOOST_UBLAS_INLINE
- value_array_type &value_data () {
- return value_data_;
- }
- // Resizing
- private:
- BOOST_UBLAS_INLINE
- size_type restrict_capacity (size_type non_zeros) const {
- non_zeros = (std::max) (non_zeros, size_type (1));
- non_zeros = (std::min) (non_zeros, size_);
- return non_zeros;
- }
- public:
- BOOST_UBLAS_INLINE
- void resize (size_type size, bool preserve = true) {
- size_ = size;
- capacity_ = restrict_capacity (capacity_);
- if (preserve) {
- index_data_. resize (capacity_, size_type ());
- value_data_. resize (capacity_, value_type ());
- filled_ = (std::min) (capacity_, filled_);
- while ((filled_ > 0) && (zero_based(index_data_[filled_ - 1]) >= size)) {
- --filled_;
- }
- }
- else {
- index_data_. resize (capacity_);
- value_data_. resize (capacity_);
- filled_ = 0;
- }
- storage_invariants ();
- }
- // Reserving
- BOOST_UBLAS_INLINE
- void reserve (size_type non_zeros, bool preserve = true) {
- capacity_ = restrict_capacity (non_zeros);
- if (preserve) {
- index_data_. resize (capacity_, size_type ());
- value_data_. resize (capacity_, value_type ());
- filled_ = (std::min) (capacity_, filled_);
- }
- else {
- index_data_. resize (capacity_);
- value_data_. resize (capacity_);
- filled_ = 0;
- }
- storage_invariants ();
- }
- // Element support
- BOOST_UBLAS_INLINE
- pointer find_element (size_type i) {
- return const_cast<pointer> (const_cast<const self_type&>(*this).find_element (i));
- }
- BOOST_UBLAS_INLINE
- const_pointer find_element (size_type i) const {
- const_subiterator_type it (detail::lower_bound (index_data_.begin (), index_data_.begin () + filled_, k_based (i), std::less<size_type> ()));
- if (it == index_data_.begin () + filled_ || *it != k_based (i))
- return 0;
- return &value_data_ [it - index_data_.begin ()];
- }
- // Element access
- BOOST_UBLAS_INLINE
- const_reference operator () (size_type i) const {
- BOOST_UBLAS_CHECK (i < size_, bad_index ());
- const_subiterator_type it (detail::lower_bound (index_data_.begin (), index_data_.begin () + filled_, k_based (i), std::less<size_type> ()));
- if (it == index_data_.begin () + filled_ || *it != k_based (i))
- return zero_;
- return value_data_ [it - index_data_.begin ()];
- }
- BOOST_UBLAS_INLINE
- true_reference ref (size_type i) {
- BOOST_UBLAS_CHECK (i < size_, bad_index ());
- subiterator_type it (detail::lower_bound (index_data_.begin (), index_data_.begin () + filled_, k_based (i), std::less<size_type> ()));
- if (it == index_data_.begin () + filled_ || *it != k_based (i))
- return insert_element (i, value_type/*zero*/());
- else
- return value_data_ [it - index_data_.begin ()];
- }
- BOOST_UBLAS_INLINE
- reference operator () (size_type i) {
- #ifndef BOOST_UBLAS_STRICT_VECTOR_SPARSE
- return ref (i) ;
- #else
- BOOST_UBLAS_CHECK (i < size_, bad_index ());
- return reference (*this, i);
- #endif
- }
- BOOST_UBLAS_INLINE
- const_reference operator [] (size_type i) const {
- return (*this) (i);
- }
- BOOST_UBLAS_INLINE
- reference operator [] (size_type i) {
- return (*this) (i);
- }
- // Element assignment
- BOOST_UBLAS_INLINE
- true_reference insert_element (size_type i, const_reference t) {
- BOOST_UBLAS_CHECK (!find_element (i), bad_index ()); // duplicate element
- if (filled_ >= capacity_)
- reserve (2 * capacity_, true);
- subiterator_type it (detail::lower_bound (index_data_.begin (), index_data_.begin () + filled_, k_based (i), std::less<size_type> ()));
- // ISSUE max_capacity limit due to difference_type
- typename std::iterator_traits<subiterator_type>::difference_type n = it - index_data_.begin ();
- BOOST_UBLAS_CHECK (filled_ == 0 || filled_ == typename index_array_type::size_type (n) || *it != k_based (i), internal_logic ()); // duplicate found by lower_bound
- ++ filled_;
- it = index_data_.begin () + n;
- std::copy_backward (it, index_data_.begin () + filled_ - 1, index_data_.begin () + filled_);
- *it = k_based (i);
- typename value_array_type::iterator itt (value_data_.begin () + n);
- std::copy_backward (itt, value_data_.begin () + filled_ - 1, value_data_.begin () + filled_);
- *itt = t;
- storage_invariants ();
- return *itt;
- }
- BOOST_UBLAS_INLINE
- void erase_element (size_type i) {
- subiterator_type it (detail::lower_bound (index_data_.begin (), index_data_.begin () + filled_, k_based (i), std::less<size_type> ()));
- typename std::iterator_traits<subiterator_type>::difference_type n = it - index_data_.begin ();
- if (filled_ > typename index_array_type::size_type (n) && *it == k_based (i)) {
- std::copy (it + 1, index_data_.begin () + filled_, it);
- typename value_array_type::iterator itt (value_data_.begin () + n);
- std::copy (itt + 1, value_data_.begin () + filled_, itt);
- -- filled_;
- }
- storage_invariants ();
- }
- // Zeroing
- BOOST_UBLAS_INLINE
- void clear () {
- filled_ = 0;
- storage_invariants ();
- }
- // Assignment
- BOOST_UBLAS_INLINE
- compressed_vector &operator = (const compressed_vector &v) {
- if (this != &v) {
- size_ = v.size_;
- capacity_ = v.capacity_;
- filled_ = v.filled_;
- index_data_ = v.index_data_;
- value_data_ = v.value_data_;
- }
- storage_invariants ();
- return *this;
- }
- template<class C> // Container assignment without temporary
- BOOST_UBLAS_INLINE
- compressed_vector &operator = (const vector_container<C> &v) {
- resize (v ().size (), false);
- assign (v);
- return *this;
- }
- BOOST_UBLAS_INLINE
- compressed_vector &assign_temporary (compressed_vector &v) {
- swap (v);
- return *this;
- }
- template<class AE>
- BOOST_UBLAS_INLINE
- compressed_vector &operator = (const vector_expression<AE> &ae) {
- self_type temporary (ae, capacity_);
- return assign_temporary (temporary);
- }
- template<class AE>
- BOOST_UBLAS_INLINE
- compressed_vector &assign (const vector_expression<AE> &ae) {
- vector_assign<scalar_assign> (*this, ae);
- return *this;
- }
- // Computed assignment
- template<class AE>
- BOOST_UBLAS_INLINE
- compressed_vector &operator += (const vector_expression<AE> &ae) {
- self_type temporary (*this + ae, capacity_);
- return assign_temporary (temporary);
- }
- template<class C> // Container assignment without temporary
- BOOST_UBLAS_INLINE
- compressed_vector &operator += (const vector_container<C> &v) {
- plus_assign (v);
- return *this;
- }
- template<class AE>
- BOOST_UBLAS_INLINE
- compressed_vector &plus_assign (const vector_expression<AE> &ae) {
- vector_assign<scalar_plus_assign> (*this, ae);
- return *this;
- }
- template<class AE>
- BOOST_UBLAS_INLINE
- compressed_vector &operator -= (const vector_expression<AE> &ae) {
- self_type temporary (*this - ae, capacity_);
- return assign_temporary (temporary);
- }
- template<class C> // Container assignment without temporary
- BOOST_UBLAS_INLINE
- compressed_vector &operator -= (const vector_container<C> &v) {
- minus_assign (v);
- return *this;
- }
- template<class AE>
- BOOST_UBLAS_INLINE
- compressed_vector &minus_assign (const vector_expression<AE> &ae) {
- vector_assign<scalar_minus_assign> (*this, ae);
- return *this;
- }
- template<class AT>
- BOOST_UBLAS_INLINE
- compressed_vector &operator *= (const AT &at) {
- vector_assign_scalar<scalar_multiplies_assign> (*this, at);
- return *this;
- }
- template<class AT>
- BOOST_UBLAS_INLINE
- compressed_vector &operator /= (const AT &at) {
- vector_assign_scalar<scalar_divides_assign> (*this, at);
- return *this;
- }
- // Swapping
- BOOST_UBLAS_INLINE
- void swap (compressed_vector &v) {
- if (this != &v) {
- std::swap (size_, v.size_);
- std::swap (capacity_, v.capacity_);
- std::swap (filled_, v.filled_);
- index_data_.swap (v.index_data_);
- value_data_.swap (v.value_data_);
- }
- storage_invariants ();
- }
- BOOST_UBLAS_INLINE
- friend void swap (compressed_vector &v1, compressed_vector &v2) {
- v1.swap (v2);
- }
- // Back element insertion and erasure
- BOOST_UBLAS_INLINE
- void push_back (size_type i, const_reference t) {
- BOOST_UBLAS_CHECK (filled_ == 0 || index_data_ [filled_ - 1] < k_based (i), external_logic ());
- if (filled_ >= capacity_)
- reserve (2 * capacity_, true);
- BOOST_UBLAS_CHECK (filled_ < capacity_, internal_logic ());
- index_data_ [filled_] = k_based (i);
- value_data_ [filled_] = t;
- ++ filled_;
- storage_invariants ();
- }
- BOOST_UBLAS_INLINE
- void pop_back () {
- BOOST_UBLAS_CHECK (filled_ > 0, external_logic ());
- -- filled_;
- storage_invariants ();
- }
- // Iterator types
- private:
- // Use index array iterator
- typedef typename IA::const_iterator const_subiterator_type;
- typedef typename IA::iterator subiterator_type;
- BOOST_UBLAS_INLINE
- true_reference at_element (size_type i) {
- BOOST_UBLAS_CHECK (i < size_, bad_index ());
- subiterator_type it (detail::lower_bound (index_data_.begin (), index_data_.begin () + filled_, k_based (i), std::less<size_type> ()));
- BOOST_UBLAS_CHECK (it != index_data_.begin () + filled_ && *it == k_based (i), bad_index ());
- return value_data_ [it - index_data_.begin ()];
- }
- public:
- class const_iterator;
- class iterator;
- // Element lookup
- // BOOST_UBLAS_INLINE This function seems to be big. So we do not let the compiler inline it.
- const_iterator find (size_type i) const {
- return const_iterator (*this, detail::lower_bound (index_data_.begin (), index_data_.begin () + filled_, k_based (i), std::less<size_type> ()));
- }
- // BOOST_UBLAS_INLINE This function seems to be big. So we do not let the compiler inline it.
- iterator find (size_type i) {
- return iterator (*this, detail::lower_bound (index_data_.begin (), index_data_.begin () + filled_, k_based (i), std::less<size_type> ()));
- }
- class const_iterator:
- public container_const_reference<compressed_vector>,
- public bidirectional_iterator_base<sparse_bidirectional_iterator_tag,
- const_iterator, value_type> {
- public:
- typedef typename compressed_vector::value_type value_type;
- typedef typename compressed_vector::difference_type difference_type;
- typedef typename compressed_vector::const_reference reference;
- typedef const typename compressed_vector::pointer pointer;
- // Construction and destruction
- BOOST_UBLAS_INLINE
- const_iterator ():
- container_const_reference<self_type> (), it_ () {}
- BOOST_UBLAS_INLINE
- const_iterator (const self_type &v, const const_subiterator_type &it):
- container_const_reference<self_type> (v), it_ (it) {}
- BOOST_UBLAS_INLINE
- const_iterator (const typename self_type::iterator &it): // ISSUE self_type:: stops VC8 using std::iterator here
- container_const_reference<self_type> (it ()), it_ (it.it_) {}
- // Arithmetic
- BOOST_UBLAS_INLINE
- const_iterator &operator ++ () {
- ++ it_;
- return *this;
- }
- BOOST_UBLAS_INLINE
- const_iterator &operator -- () {
- -- it_;
- return *this;
- }
- // Dereference
- BOOST_UBLAS_INLINE
- const_reference operator * () const {
- BOOST_UBLAS_CHECK (index () < (*this) ().size (), bad_index ());
- return (*this) ().value_data_ [it_ - (*this) ().index_data_.begin ()];
- }
- // Index
- BOOST_UBLAS_INLINE
- size_type index () const {
- BOOST_UBLAS_CHECK (*this != (*this) ().end (), bad_index ());
- BOOST_UBLAS_CHECK ((*this) ().zero_based (*it_) < (*this) ().size (), bad_index ());
- return (*this) ().zero_based (*it_);
- }
- // Assignment
- BOOST_UBLAS_INLINE
- const_iterator &operator = (const const_iterator &it) {
- container_const_reference<self_type>::assign (&it ());
- it_ = it.it_;
- return *this;
- }
- // Comparison
- BOOST_UBLAS_INLINE
- bool operator == (const const_iterator &it) const {
- BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ());
- return it_ == it.it_;
- }
- private:
- const_subiterator_type it_;
- };
- BOOST_UBLAS_INLINE
- const_iterator begin () const {
- return find (0);
- }
- BOOST_UBLAS_INLINE
- const_iterator cbegin () const {
- return begin ();
- }
- BOOST_UBLAS_INLINE
- const_iterator end () const {
- return find (size_);
- }
- BOOST_UBLAS_INLINE
- const_iterator cend () const {
- return end ();
- }
- class iterator:
- public container_reference<compressed_vector>,
- public bidirectional_iterator_base<sparse_bidirectional_iterator_tag,
- iterator, value_type> {
- public:
- typedef typename compressed_vector::value_type value_type;
- typedef typename compressed_vector::difference_type difference_type;
- typedef typename compressed_vector::true_reference reference;
- typedef typename compressed_vector::pointer pointer;
- // Construction and destruction
- BOOST_UBLAS_INLINE
- iterator ():
- container_reference<self_type> (), it_ () {}
- BOOST_UBLAS_INLINE
- iterator (self_type &v, const subiterator_type &it):
- container_reference<self_type> (v), it_ (it) {}
- // Arithmetic
- BOOST_UBLAS_INLINE
- iterator &operator ++ () {
- ++ it_;
- return *this;
- }
- BOOST_UBLAS_INLINE
- iterator &operator -- () {
- -- it_;
- return *this;
- }
- // Dereference
- BOOST_UBLAS_INLINE
- reference operator * () const {
- BOOST_UBLAS_CHECK (index () < (*this) ().size (), bad_index ());
- return (*this) ().value_data_ [it_ - (*this) ().index_data_.begin ()];
- }
- // Index
- BOOST_UBLAS_INLINE
- size_type index () const {
- BOOST_UBLAS_CHECK (*this != (*this) ().end (), bad_index ());
- BOOST_UBLAS_CHECK ((*this) ().zero_based (*it_) < (*this) ().size (), bad_index ());
- return (*this) ().zero_based (*it_);
- }
- // Assignment
- BOOST_UBLAS_INLINE
- iterator &operator = (const iterator &it) {
- container_reference<self_type>::assign (&it ());
- it_ = it.it_;
- return *this;
- }
- // Comparison
- BOOST_UBLAS_INLINE
- bool operator == (const iterator &it) const {
- BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ());
- return it_ == it.it_;
- }
- private:
- subiterator_type it_;
- friend class const_iterator;
- };
- BOOST_UBLAS_INLINE
- iterator begin () {
- return find (0);
- }
- BOOST_UBLAS_INLINE
- iterator end () {
- return find (size_);
- }
- // Reverse iterator
- typedef reverse_iterator_base<const_iterator> const_reverse_iterator;
- typedef reverse_iterator_base<iterator> reverse_iterator;
- BOOST_UBLAS_INLINE
- const_reverse_iterator rbegin () const {
- return const_reverse_iterator (end ());
- }
- BOOST_UBLAS_INLINE
- const_reverse_iterator crbegin () const {
- return rbegin ();
- }
- BOOST_UBLAS_INLINE
- const_reverse_iterator rend () const {
- return const_reverse_iterator (begin ());
- }
- BOOST_UBLAS_INLINE
- const_reverse_iterator crend () const {
- return rend ();
- }
- BOOST_UBLAS_INLINE
- reverse_iterator rbegin () {
- return reverse_iterator (end ());
- }
- BOOST_UBLAS_INLINE
- reverse_iterator rend () {
- return reverse_iterator (begin ());
- }
- // Serialization
- template<class Archive>
- void serialize(Archive & ar, const unsigned int /* file_version */){
- serialization::collection_size_type s (size_);
- ar & serialization::make_nvp("size",s);
- if (Archive::is_loading::value) {
- size_ = s;
- }
- // ISSUE: filled may be much less than capacity
- // ISSUE: index_data_ and value_data_ are undefined between filled and capacity (trouble with 'nan'-values)
- ar & serialization::make_nvp("capacity", capacity_);
- ar & serialization::make_nvp("filled", filled_);
- ar & serialization::make_nvp("index_data", index_data_);
- ar & serialization::make_nvp("value_data", value_data_);
- storage_invariants();
- }
- private:
- void storage_invariants () const
- {
- BOOST_UBLAS_CHECK (capacity_ == index_data_.size (), internal_logic ());
- BOOST_UBLAS_CHECK (capacity_ == value_data_.size (), internal_logic ());
- BOOST_UBLAS_CHECK (filled_ <= capacity_, internal_logic ());
- BOOST_UBLAS_CHECK ((0 == filled_) || (zero_based(index_data_[filled_ - 1]) < size_), internal_logic ());
- }
- size_type size_;
- typename index_array_type::size_type capacity_;
- typename index_array_type::size_type filled_;
- index_array_type index_data_;
- value_array_type value_data_;
- static const value_type zero_;
- BOOST_UBLAS_INLINE
- static size_type zero_based (size_type k_based_index) {
- return k_based_index - IB;
- }
- BOOST_UBLAS_INLINE
- static size_type k_based (size_type zero_based_index) {
- return zero_based_index + IB;
- }
- friend class iterator;
- friend class const_iterator;
- };
- template<class T, std::size_t IB, class IA, class TA>
- const typename compressed_vector<T, IB, IA, TA>::value_type compressed_vector<T, IB, IA, TA>::zero_ = value_type/*zero*/();
- // Thanks to Kresimir Fresl for extending this to cover different index bases.
- /** \brief Coordimate array based sparse vector
- *
- * a sparse vector of values of type \c T of variable size. The non zero values are stored
- * as two seperate arrays: an index array and a value array. The arrays may be out of order
- * with multiple entries for each vector element. If there are multiple values for the same
- * index the sum of these values is the real value. It is way more efficient for inserting values
- * than a \c compressed_vector but less memory efficient. Also linearly parsing a vector can
- * be longer in specific cases than a \c compressed_vector.
- *
- * For a n-dimensional sorted coordinate vector and \f$ 0 \leq i < n\f$ the non-zero elements
- * \f$v_i\f$ are mapped to consecutive elements of the index and value container, i.e. for
- * 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$.
- *
- * Supported parameters for the adapted array (indices and values) are \c unbounded_array<> ,
- * \c bounded_array<> and \c std::vector<>.
- *
- * \tparam T the type of object stored in the vector (like double, float, complex, etc...)
- * \tparam IB the index base of the compressed vector. Default is 0. Other supported value is 1
- * \tparam IA the type of adapted array for indices. Default is \c unbounded_array<std::size_t>
- * \tparam TA the type of adapted array for values. Default is unbounded_array<T>
- */
- template<class T, std::size_t IB, class IA, class TA>
- class coordinate_vector:
- public vector_container<coordinate_vector<T, IB, IA, TA> > {
- typedef T &true_reference;
- typedef T *pointer;
- typedef const T *const_pointer;
- typedef coordinate_vector<T, IB, IA, TA> self_type;
- public:
- #ifdef BOOST_UBLAS_ENABLE_PROXY_SHORTCUTS
- using vector_container<self_type>::operator ();
- #endif
- // ISSUE require type consistency check
- // is_convertable (IA::size_type, TA::size_type)
- typedef typename IA::value_type size_type;
- typedef typename IA::difference_type difference_type;
- typedef T value_type;
- typedef const T &const_reference;
- #ifndef BOOST_UBLAS_STRICT_VECTOR_SPARSE
- typedef T &reference;
- #else
- typedef sparse_vector_element<self_type> reference;
- #endif
- typedef IA index_array_type;
- typedef TA value_array_type;
- typedef const vector_reference<const self_type> const_closure_type;
- typedef vector_reference<self_type> closure_type;
- typedef self_type vector_temporary_type;
- typedef sparse_tag storage_category;
- // Construction and destruction
- BOOST_UBLAS_INLINE
- coordinate_vector ():
- vector_container<self_type> (),
- size_ (0), capacity_ (restrict_capacity (0)),
- filled_ (0), sorted_filled_ (filled_), sorted_ (true),
- index_data_ (capacity_), value_data_ (capacity_) {
- storage_invariants ();
- }
- explicit BOOST_UBLAS_INLINE
- coordinate_vector (size_type size, size_type non_zeros = 0):
- vector_container<self_type> (),
- size_ (size), capacity_ (restrict_capacity (non_zeros)),
- filled_ (0), sorted_filled_ (filled_), sorted_ (true),
- index_data_ (capacity_), value_data_ (capacity_) {
- storage_invariants ();
- }
- BOOST_UBLAS_INLINE
- coordinate_vector (const coordinate_vector &v):
- vector_container<self_type> (),
- size_ (v.size_), capacity_ (v.capacity_),
- filled_ (v.filled_), sorted_filled_ (v.sorted_filled_), sorted_ (v.sorted_),
- index_data_ (v.index_data_), value_data_ (v.value_data_) {
- storage_invariants ();
- }
- template<class AE>
- BOOST_UBLAS_INLINE
- coordinate_vector (const vector_expression<AE> &ae, size_type non_zeros = 0):
- vector_container<self_type> (),
- size_ (ae ().size ()), capacity_ (restrict_capacity (non_zeros)),
- filled_ (0), sorted_filled_ (filled_), sorted_ (true),
- index_data_ (capacity_), value_data_ (capacity_) {
- storage_invariants ();
- vector_assign<scalar_assign> (*this, ae);
- }
- // Accessors
- BOOST_UBLAS_INLINE
- size_type size () const {
- return size_;
- }
- BOOST_UBLAS_INLINE
- size_type nnz_capacity () const {
- return capacity_;
- }
- BOOST_UBLAS_INLINE
- size_type nnz () const {
- return filled_;
- }
- // Storage accessors
- BOOST_UBLAS_INLINE
- static size_type index_base () {
- return IB;
- }
- BOOST_UBLAS_INLINE
- typename index_array_type::size_type filled () const {
- return filled_;
- }
- BOOST_UBLAS_INLINE
- const index_array_type &index_data () const {
- return index_data_;
- }
- BOOST_UBLAS_INLINE
- const value_array_type &value_data () const {
- return value_data_;
- }
- BOOST_UBLAS_INLINE
- void set_filled (const typename index_array_type::size_type &sorted, const typename index_array_type::size_type &filled) {
- sorted_filled_ = sorted;
- filled_ = filled;
- storage_invariants ();
- }
- BOOST_UBLAS_INLINE
- index_array_type &index_data () {
- return index_data_;
- }
- BOOST_UBLAS_INLINE
- value_array_type &value_data () {
- return value_data_;
- }
- // Resizing
- private:
- BOOST_UBLAS_INLINE
- size_type restrict_capacity (size_type non_zeros) const {
- // minimum non_zeros
- non_zeros = (std::max) (non_zeros, size_type (1));
- // ISSUE no maximum as coordinate may contain inserted duplicates
- return non_zeros;
- }
- public:
- BOOST_UBLAS_INLINE
- void resize (size_type size, bool preserve = true) {
- if (preserve)
- sort (); // remove duplicate elements.
- size_ = size;
- capacity_ = restrict_capacity (capacity_);
- if (preserve) {
- index_data_. resize (capacity_, size_type ());
- value_data_. resize (capacity_, value_type ());
- filled_ = (std::min) (capacity_, filled_);
- while ((filled_ > 0) && (zero_based(index_data_[filled_ - 1]) >= size)) {
- --filled_;
- }
- }
- else {
- index_data_. resize (capacity_);
- value_data_. resize (capacity_);
- filled_ = 0;
- }
- sorted_filled_ = filled_;
- storage_invariants ();
- }
- // Reserving
- BOOST_UBLAS_INLINE
- void reserve (size_type non_zeros, bool preserve = true) {
- if (preserve)
- sort (); // remove duplicate elements.
- capacity_ = restrict_capacity (non_zeros);
- if (preserve) {
- index_data_. resize (capacity_, size_type ());
- value_data_. resize (capacity_, value_type ());
- filled_ = (std::min) (capacity_, filled_);
- }
- else {
- index_data_. resize (capacity_);
- value_data_. resize (capacity_);
- filled_ = 0;
- }
- sorted_filled_ = filled_;
- storage_invariants ();
- }
- // Element support
- BOOST_UBLAS_INLINE
- pointer find_element (size_type i) {
- return const_cast<pointer> (const_cast<const self_type&>(*this).find_element (i));
- }
- BOOST_UBLAS_INLINE
- const_pointer find_element (size_type i) const {
- sort ();
- const_subiterator_type it (detail::lower_bound (index_data_.begin (), index_data_.begin () + filled_, k_based (i), std::less<size_type> ()));
- if (it == index_data_.begin () + filled_ || *it != k_based (i))
- return 0;
- return &value_data_ [it - index_data_.begin ()];
- }
- // Element access
- BOOST_UBLAS_INLINE
- const_reference operator () (size_type i) const {
- BOOST_UBLAS_CHECK (i < size_, bad_index ());
- sort ();
- const_subiterator_type it (detail::lower_bound (index_data_.begin (), index_data_.begin () + filled_, k_based (i), std::less<size_type> ()));
- if (it == index_data_.begin () + filled_ || *it != k_based (i))
- return zero_;
- return value_data_ [it - index_data_.begin ()];
- }
- BOOST_UBLAS_INLINE
- true_reference ref (size_type i) {
- BOOST_UBLAS_CHECK (i < size_, bad_index ());
- sort ();
- subiterator_type it (detail::lower_bound (index_data_.begin (), index_data_.begin () + filled_, k_based (i), std::less<size_type> ()));
- if (it == index_data_.begin () + filled_ || *it != k_based (i))
- return insert_element (i, value_type/*zero*/());
- else
- return value_data_ [it - index_data_.begin ()];
- }
- BOOST_UBLAS_INLINE
- reference operator () (size_type i) {
- #ifndef BOOST_UBLAS_STRICT_VECTOR_SPARSE
- return ref (i);
- #else
- BOOST_UBLAS_CHECK (i < size_, bad_index ());
- return reference (*this, i);
- #endif
- }
- BOOST_UBLAS_INLINE
- const_reference operator [] (size_type i) const {
- return (*this) (i);
- }
- BOOST_UBLAS_INLINE
- reference operator [] (size_type i) {
- return (*this) (i);
- }
- // Element assignment
- BOOST_UBLAS_INLINE
- void append_element (size_type i, const_reference t) {
- if (filled_ >= capacity_)
- reserve (2 * filled_, true);
- BOOST_UBLAS_CHECK (filled_ < capacity_, internal_logic ());
- index_data_ [filled_] = k_based (i);
- value_data_ [filled_] = t;
- ++ filled_;
- sorted_ = false;
- storage_invariants ();
- }
- BOOST_UBLAS_INLINE
- true_reference insert_element (size_type i, const_reference t) {
- BOOST_UBLAS_CHECK (!find_element (i), bad_index ()); // duplicate element
- append_element (i, t);
- return value_data_ [filled_ - 1];
- }
- BOOST_UBLAS_INLINE
- void erase_element (size_type i) {
- sort ();
- subiterator_type it (detail::lower_bound (index_data_.begin (), index_data_.begin () + filled_, k_based (i), std::less<size_type> ()));
- typename std::iterator_traits<subiterator_type>::difference_type n = it - index_data_.begin ();
- if (filled_ > typename index_array_type::size_type (n) && *it == k_based (i)) {
- std::copy (it + 1, index_data_.begin () + filled_, it);
- typename value_array_type::iterator itt (value_data_.begin () + n);
- std::copy (itt + 1, value_data_.begin () + filled_, itt);
- -- filled_;
- sorted_filled_ = filled_;
- }
- storage_invariants ();
- }
- // Zeroing
- BOOST_UBLAS_INLINE
- void clear () {
- filled_ = 0;
- sorted_filled_ = filled_;
- sorted_ = true;
- storage_invariants ();
- }
- // Assignment
- BOOST_UBLAS_INLINE
- coordinate_vector &operator = (const coordinate_vector &v) {
- if (this != &v) {
- size_ = v.size_;
- capacity_ = v.capacity_;
- filled_ = v.filled_;
- sorted_filled_ = v.sorted_filled_;
- sorted_ = v.sorted_;
- index_data_ = v.index_data_;
- value_data_ = v.value_data_;
- }
- storage_invariants ();
- return *this;
- }
- template<class C> // Container assignment without temporary
- BOOST_UBLAS_INLINE
- coordinate_vector &operator = (const vector_container<C> &v) {
- resize (v ().size (), false);
- assign (v);
- return *this;
- }
- BOOST_UBLAS_INLINE
- coordinate_vector &assign_temporary (coordinate_vector &v) {
- swap (v);
- return *this;
- }
- template<class AE>
- BOOST_UBLAS_INLINE
- coordinate_vector &operator = (const vector_expression<AE> &ae) {
- self_type temporary (ae, capacity_);
- return assign_temporary (temporary);
- }
- template<class AE>
- BOOST_UBLAS_INLINE
- coordinate_vector &assign (const vector_expression<AE> &ae) {
- vector_assign<scalar_assign> (*this, ae);
- return *this;
- }
- // Computed assignment
- template<class AE>
- BOOST_UBLAS_INLINE
- coordinate_vector &operator += (const vector_expression<AE> &ae) {
- self_type temporary (*this + ae, capacity_);
- return assign_temporary (temporary);
- }
- template<class C> // Container assignment without temporary
- BOOST_UBLAS_INLINE
- coordinate_vector &operator += (const vector_container<C> &v) {
- plus_assign (v);
- return *this;
- }
- template<class AE>
- BOOST_UBLAS_INLINE
- coordinate_vector &plus_assign (const vector_expression<AE> &ae) {
- vector_assign<scalar_plus_assign> (*this, ae);
- return *this;
- }
- template<class AE>
- BOOST_UBLAS_INLINE
- coordinate_vector &operator -= (const vector_expression<AE> &ae) {
- self_type temporary (*this - ae, capacity_);
- return assign_temporary (temporary);
- }
- template<class C> // Container assignment without temporary
- BOOST_UBLAS_INLINE
- coordinate_vector &operator -= (const vector_container<C> &v) {
- minus_assign (v);
- return *this;
- }
- template<class AE>
- BOOST_UBLAS_INLINE
- coordinate_vector &minus_assign (const vector_expression<AE> &ae) {
- vector_assign<scalar_minus_assign> (*this, ae);
- return *this;
- }
- template<class AT>
- BOOST_UBLAS_INLINE
- coordinate_vector &operator *= (const AT &at) {
- vector_assign_scalar<scalar_multiplies_assign> (*this, at);
- return *this;
- }
- template<class AT>
- BOOST_UBLAS_INLINE
- coordinate_vector &operator /= (const AT &at) {
- vector_assign_scalar<scalar_divides_assign> (*this, at);
- return *this;
- }
- // Swapping
- BOOST_UBLAS_INLINE
- void swap (coordinate_vector &v) {
- if (this != &v) {
- std::swap (size_, v.size_);
- std::swap (capacity_, v.capacity_);
- std::swap (filled_, v.filled_);
- std::swap (sorted_filled_, v.sorted_filled_);
- std::swap (sorted_, v.sorted_);
- index_data_.swap (v.index_data_);
- value_data_.swap (v.value_data_);
- }
- storage_invariants ();
- }
- BOOST_UBLAS_INLINE
- friend void swap (coordinate_vector &v1, coordinate_vector &v2) {
- v1.swap (v2);
- }
- // replacement if STL lower bound algorithm for use of inplace_merge
- size_type lower_bound (size_type beg, size_type end, size_type target) const {
- while (end > beg) {
- size_type mid = (beg + end) / 2;
- if (index_data_[mid] < index_data_[target]) {
- beg = mid + 1;
- } else {
- end = mid;
- }
- }
- return beg;
- }
- // specialized replacement of STL inplace_merge to avoid compilation
- // problems with respect to the array_triple iterator
- void inplace_merge (size_type beg, size_type mid, size_type end) const {
- size_type len_lef = mid - beg;
- size_type len_rig = end - mid;
- if (len_lef == 1 && len_rig == 1) {
- if (index_data_[mid] < index_data_[beg]) {
- std::swap(index_data_[beg], index_data_[mid]);
- std::swap(value_data_[beg], value_data_[mid]);
- }
- } else if (len_lef > 0 && len_rig > 0) {
- size_type lef_mid, rig_mid;
- if (len_lef >= len_rig) {
- lef_mid = (beg + mid) / 2;
- rig_mid = lower_bound(mid, end, lef_mid);
- } else {
- rig_mid = (mid + end) / 2;
- lef_mid = lower_bound(beg, mid, rig_mid);
- }
- std::rotate(&index_data_[0] + lef_mid, &index_data_[0] + mid, &index_data_[0] + rig_mid);
- std::rotate(&value_data_[0] + lef_mid, &value_data_[0] + mid, &value_data_[0] + rig_mid);
- size_type new_mid = lef_mid + rig_mid - mid;
- inplace_merge(beg, lef_mid, new_mid);
- inplace_merge(new_mid, rig_mid, end);
- }
- }
- // Sorting and summation of duplicates
- BOOST_UBLAS_INLINE
- void sort () const {
- if (! sorted_ && filled_ > 0) {
- typedef index_pair_array<index_array_type, value_array_type> array_pair;
- array_pair ipa (filled_, index_data_, value_data_);
- #ifndef BOOST_UBLAS_COO_ALWAYS_DO_FULL_SORT
- const typename array_pair::iterator iunsorted = ipa.begin () + sorted_filled_;
- // sort new elements and merge
- std::sort (iunsorted, ipa.end ());
- inplace_merge(0, sorted_filled_, filled_);
- #else
- const typename array_pair::iterator iunsorted = ipa.begin ();
- std::sort (iunsorted, ipa.end ());
- #endif
- // sum duplicates with += and remove
- size_type filled = 0;
- for (size_type i = 1; i < filled_; ++ i) {
- if (index_data_ [filled] != index_data_ [i]) {
- ++ filled;
- if (filled != i) {
- index_data_ [filled] = index_data_ [i];
- value_data_ [filled] = value_data_ [i];
- }
- } else {
- value_data_ [filled] += value_data_ [i];
- }
- }
- filled_ = filled + 1;
- sorted_filled_ = filled_;
- sorted_ = true;
- storage_invariants ();
- }
- }
- // Back element insertion and erasure
- BOOST_UBLAS_INLINE
- void push_back (size_type i, const_reference t) {
- // must maintain sort order
- BOOST_UBLAS_CHECK (sorted_ && (filled_ == 0 || index_data_ [filled_ - 1] < k_based (i)), external_logic ());
- if (filled_ >= capacity_)
- reserve (2 * filled_, true);
- BOOST_UBLAS_CHECK (filled_ < capacity_, internal_logic ());
- index_data_ [filled_] = k_based (i);
- value_data_ [filled_] = t;
- ++ filled_;
- sorted_filled_ = filled_;
- storage_invariants ();
- }
- BOOST_UBLAS_INLINE
- void pop_back () {
- // ISSUE invariants could be simpilfied if sorted required as precondition
- BOOST_UBLAS_CHECK (filled_ > 0, external_logic ());
- -- filled_;
- sorted_filled_ = (std::min) (sorted_filled_, filled_);
- sorted_ = sorted_filled_ = filled_;
- storage_invariants ();
- }
- // Iterator types
- private:
- // Use index array iterator
- typedef typename IA::const_iterator const_subiterator_type;
- typedef typename IA::iterator subiterator_type;
- BOOST_UBLAS_INLINE
- true_reference at_element (size_type i) {
- BOOST_UBLAS_CHECK (i < size_, bad_index ());
- sort ();
- subiterator_type it (detail::lower_bound (index_data_.begin (), index_data_.begin () + filled_, k_based (i), std::less<size_type> ()));
- BOOST_UBLAS_CHECK (it != index_data_.begin () + filled_ && *it == k_based (i), bad_index ());
- return value_data_ [it - index_data_.begin ()];
- }
- public:
- class const_iterator;
- class iterator;
- // Element lookup
- // BOOST_UBLAS_INLINE This function seems to be big. So we do not let the compiler inline it.
- const_iterator find (size_type i) const {
- sort ();
- return const_iterator (*this, detail::lower_bound (index_data_.begin (), index_data_.begin () + filled_, k_based (i), std::less<size_type> ()));
- }
- // BOOST_UBLAS_INLINE This function seems to be big. So we do not let the compiler inline it.
- iterator find (size_type i) {
- sort ();
- return iterator (*this, detail::lower_bound (index_data_.begin (), index_data_.begin () + filled_, k_based (i), std::less<size_type> ()));
- }
- class const_iterator:
- public container_const_reference<coordinate_vector>,
- public bidirectional_iterator_base<sparse_bidirectional_iterator_tag,
- const_iterator, value_type> {
- public:
- typedef typename coordinate_vector::value_type value_type;
- typedef typename coordinate_vector::difference_type difference_type;
- typedef typename coordinate_vector::const_reference reference;
- typedef const typename coordinate_vector::pointer pointer;
- // Construction and destruction
- BOOST_UBLAS_INLINE
- const_iterator ():
- container_const_reference<self_type> (), it_ () {}
- BOOST_UBLAS_INLINE
- const_iterator (const self_type &v, const const_subiterator_type &it):
- container_const_reference<self_type> (v), it_ (it) {}
- BOOST_UBLAS_INLINE
- const_iterator (const typename self_type::iterator &it): // ISSUE self_type:: stops VC8 using std::iterator here
- container_const_reference<self_type> (it ()), it_ (it.it_) {}
- // Arithmetic
- BOOST_UBLAS_INLINE
- const_iterator &operator ++ () {
- ++ it_;
- return *this;
- }
- BOOST_UBLAS_INLINE
- const_iterator &operator -- () {
- -- it_;
- return *this;
- }
- // Dereference
- BOOST_UBLAS_INLINE
- const_reference operator * () const {
- BOOST_UBLAS_CHECK (index () < (*this) ().size (), bad_index ());
- return (*this) ().value_data_ [it_ - (*this) ().index_data_.begin ()];
- }
- // Index
- BOOST_UBLAS_INLINE
- size_type index () const {
- BOOST_UBLAS_CHECK (*this != (*this) ().end (), bad_index ());
- BOOST_UBLAS_CHECK ((*this) ().zero_based (*it_) < (*this) ().size (), bad_index ());
- return (*this) ().zero_based (*it_);
- }
- // Assignment
- BOOST_UBLAS_INLINE
- const_iterator &operator = (const const_iterator &it) {
- container_const_reference<self_type>::assign (&it ());
- it_ = it.it_;
- return *this;
- }
- // Comparison
- BOOST_UBLAS_INLINE
- bool operator == (const const_iterator &it) const {
- BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ());
- return it_ == it.it_;
- }
- private:
- const_subiterator_type it_;
- };
- BOOST_UBLAS_INLINE
- const_iterator begin () const {
- return find (0);
- }
- BOOST_UBLAS_INLINE
- const_iterator cbegin () const {
- return begin();
- }
- BOOST_UBLAS_INLINE
- const_iterator end () const {
- return find (size_);
- }
- BOOST_UBLAS_INLINE
- const_iterator cend () const {
- return end();
- }
- class iterator:
- public container_reference<coordinate_vector>,
- public bidirectional_iterator_base<sparse_bidirectional_iterator_tag,
- iterator, value_type> {
- public:
- typedef typename coordinate_vector::value_type value_type;
- typedef typename coordinate_vector::difference_type difference_type;
- typedef typename coordinate_vector::true_reference reference;
- typedef typename coordinate_vector::pointer pointer;
- // Construction and destruction
- BOOST_UBLAS_INLINE
- iterator ():
- container_reference<self_type> (), it_ () {}
- BOOST_UBLAS_INLINE
- iterator (self_type &v, const subiterator_type &it):
- container_reference<self_type> (v), it_ (it) {}
- // Arithmetic
- BOOST_UBLAS_INLINE
- iterator &operator ++ () {
- ++ it_;
- return *this;
- }
- BOOST_UBLAS_INLINE
- iterator &operator -- () {
- -- it_;
- return *this;
- }
- // Dereference
- BOOST_UBLAS_INLINE
- reference operator * () const {
- BOOST_UBLAS_CHECK (index () < (*this) ().size (), bad_index ());
- return (*this) ().value_data_ [it_ - (*this) ().index_data_.begin ()];
- }
- // Index
- BOOST_UBLAS_INLINE
- size_type index () const {
- BOOST_UBLAS_CHECK (*this != (*this) ().end (), bad_index ());
- BOOST_UBLAS_CHECK ((*this) ().zero_based (*it_) < (*this) ().size (), bad_index ());
- return (*this) ().zero_based (*it_);
- }
- // Assignment
- BOOST_UBLAS_INLINE
- iterator &operator = (const iterator &it) {
- container_reference<self_type>::assign (&it ());
- it_ = it.it_;
- return *this;
- }
- // Comparison
- BOOST_UBLAS_INLINE
- bool operator == (const iterator &it) const {
- BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ());
- return it_ == it.it_;
- }
- private:
- subiterator_type it_;
- friend class const_iterator;
- };
- BOOST_UBLAS_INLINE
- iterator begin () {
- return find (0);
- }
- BOOST_UBLAS_INLINE
- iterator end () {
- return find (size_);
- }
- // Reverse iterator
- typedef reverse_iterator_base<const_iterator> const_reverse_iterator;
- typedef reverse_iterator_base<iterator> reverse_iterator;
- BOOST_UBLAS_INLINE
- const_reverse_iterator rbegin () const {
- return const_reverse_iterator (end ());
- }
- BOOST_UBLAS_INLINE
- const_reverse_iterator crbegin () const {
- return rbegin ();
- }
- BOOST_UBLAS_INLINE
- const_reverse_iterator rend () const {
- return const_reverse_iterator (begin ());
- }
- BOOST_UBLAS_INLINE
- const_reverse_iterator crend () const {
- return rend ();
- }
- BOOST_UBLAS_INLINE
- reverse_iterator rbegin () {
- return reverse_iterator (end ());
- }
- BOOST_UBLAS_INLINE
- reverse_iterator rend () {
- return reverse_iterator (begin ());
- }
- // Serialization
- template<class Archive>
- void serialize(Archive & ar, const unsigned int /* file_version */){
- serialization::collection_size_type s (size_);
- ar & serialization::make_nvp("size",s);
- if (Archive::is_loading::value) {
- size_ = s;
- }
- // ISSUE: filled may be much less than capacity
- // ISSUE: index_data_ and value_data_ are undefined between filled and capacity (trouble with 'nan'-values)
- ar & serialization::make_nvp("capacity", capacity_);
- ar & serialization::make_nvp("filled", filled_);
- ar & serialization::make_nvp("sorted_filled", sorted_filled_);
- ar & serialization::make_nvp("sorted", sorted_);
- ar & serialization::make_nvp("index_data", index_data_);
- ar & serialization::make_nvp("value_data", value_data_);
- storage_invariants();
- }
- private:
- void storage_invariants () const
- {
- BOOST_UBLAS_CHECK (capacity_ == index_data_.size (), internal_logic ());
- BOOST_UBLAS_CHECK (capacity_ == value_data_.size (), internal_logic ());
- BOOST_UBLAS_CHECK (filled_ <= capacity_, internal_logic ());
- BOOST_UBLAS_CHECK (sorted_filled_ <= filled_, internal_logic ());
- BOOST_UBLAS_CHECK (sorted_ == (sorted_filled_ == filled_), internal_logic ());
- BOOST_UBLAS_CHECK ((0 == filled_) || (zero_based(index_data_[filled_ - 1]) < size_), internal_logic ());
- }
- size_type size_;
- size_type capacity_;
- mutable typename index_array_type::size_type filled_;
- mutable typename index_array_type::size_type sorted_filled_;
- mutable bool sorted_;
- mutable index_array_type index_data_;
- mutable value_array_type value_data_;
- static const value_type zero_;
- BOOST_UBLAS_INLINE
- static size_type zero_based (size_type k_based_index) {
- return k_based_index - IB;
- }
- BOOST_UBLAS_INLINE
- static size_type k_based (size_type zero_based_index) {
- return zero_based_index + IB;
- }
- friend class iterator;
- friend class const_iterator;
- };
- template<class T, std::size_t IB, class IA, class TA>
- const typename coordinate_vector<T, IB, IA, TA>::value_type coordinate_vector<T, IB, IA, TA>::zero_ = value_type/*zero*/();
- }}}
- #ifdef BOOST_MSVC
- #undef _ITERATOR_DEBUG_LEVEL
- #define _ITERATOR_DEBUG_LEVEL _BACKUP_ITERATOR_DEBUG_LEVEL
- #undef _BACKUP_ITERATOR_DEBUG_LEVEL
- #endif
- #endif
|