vector_proxy.hpp 62 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647
  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_PROXY_
  13. #define _BOOST_UBLAS_VECTOR_PROXY_
  14. #include <boost/numeric/ublas/vector_expression.hpp>
  15. #include <boost/numeric/ublas/detail/vector_assign.hpp>
  16. #include <boost/numeric/ublas/detail/temporary.hpp>
  17. // Iterators based on ideas of Jeremy Siek
  18. namespace boost { namespace numeric { namespace ublas {
  19. /** \brief A vector referencing a continuous subvector of elements of vector \c v containing all elements specified by \c range.
  20. *
  21. * A vector range can be used as a normal vector in any expression.
  22. * If the specified range falls outside that of the index range of the vector, then
  23. * the \c vector_range is not a well formed \i Vector \i Expression and access to an
  24. * element outside of index range of the vector is \b undefined.
  25. *
  26. * \tparam V the type of vector referenced (for example \c vector<double>)
  27. */
  28. template<class V>
  29. class vector_range:
  30. public vector_expression<vector_range<V> > {
  31. typedef vector_range<V> self_type;
  32. public:
  33. #ifdef BOOST_UBLAS_ENABLE_PROXY_SHORTCUTS
  34. using vector_expression<self_type>::operator ();
  35. #endif
  36. typedef const V const_vector_type;
  37. typedef V vector_type;
  38. typedef typename V::size_type size_type;
  39. typedef typename V::difference_type difference_type;
  40. typedef typename V::value_type value_type;
  41. typedef typename V::const_reference const_reference;
  42. typedef typename boost::mpl::if_<boost::is_const<V>,
  43. typename V::const_reference,
  44. typename V::reference>::type reference;
  45. typedef typename boost::mpl::if_<boost::is_const<V>,
  46. typename V::const_closure_type,
  47. typename V::closure_type>::type vector_closure_type;
  48. typedef basic_range<size_type, difference_type> range_type;
  49. typedef const self_type const_closure_type;
  50. typedef self_type closure_type;
  51. typedef typename storage_restrict_traits<typename V::storage_category,
  52. dense_proxy_tag>::storage_category storage_category;
  53. // Construction and destruction
  54. BOOST_UBLAS_INLINE
  55. vector_range (vector_type &data, const range_type &r):
  56. data_ (data), r_ (r.preprocess (data.size ())) {
  57. // Early checking of preconditions here.
  58. // BOOST_UBLAS_CHECK (r_.start () <= data_.size () &&
  59. // r_.start () + r_.size () <= data_.size (), bad_index ());
  60. }
  61. BOOST_UBLAS_INLINE
  62. vector_range (const vector_closure_type &data, const range_type &r, bool):
  63. data_ (data), r_ (r.preprocess (data.size ())) {
  64. // Early checking of preconditions here.
  65. // BOOST_UBLAS_CHECK (r_.start () <= data_.size () &&
  66. // r_.start () + r_.size () <= data_.size (), bad_index ());
  67. }
  68. // Accessors
  69. BOOST_UBLAS_INLINE
  70. size_type start () const {
  71. return r_.start ();
  72. }
  73. BOOST_UBLAS_INLINE
  74. size_type size () const {
  75. return r_.size ();
  76. }
  77. // Storage accessors
  78. BOOST_UBLAS_INLINE
  79. const vector_closure_type &data () const {
  80. return data_;
  81. }
  82. BOOST_UBLAS_INLINE
  83. vector_closure_type &data () {
  84. return data_;
  85. }
  86. // Element access
  87. #ifndef BOOST_UBLAS_PROXY_CONST_MEMBER
  88. BOOST_UBLAS_INLINE
  89. const_reference operator () (size_type i) const {
  90. return data_ (r_ (i));
  91. }
  92. BOOST_UBLAS_INLINE
  93. reference operator () (size_type i) {
  94. return data_ (r_ (i));
  95. }
  96. BOOST_UBLAS_INLINE
  97. const_reference operator [] (size_type i) const {
  98. return (*this) (i);
  99. }
  100. BOOST_UBLAS_INLINE
  101. reference operator [] (size_type i) {
  102. return (*this) (i);
  103. }
  104. #else
  105. BOOST_UBLAS_INLINE
  106. reference operator () (size_type i) const {
  107. return data_ (r_ (i));
  108. }
  109. BOOST_UBLAS_INLINE
  110. reference operator [] (size_type i) const {
  111. return (*this) (i);
  112. }
  113. #endif
  114. // ISSUE can this be done in free project function?
  115. // Although a const function can create a non-const proxy to a non-const object
  116. // Critical is that vector_type and data_ (vector_closure_type) are const correct
  117. BOOST_UBLAS_INLINE
  118. vector_range<vector_type> project (const range_type &r) const {
  119. return vector_range<vector_type> (data_, r_.compose (r.preprocess (data_.size ())), false);
  120. }
  121. // Assignment
  122. BOOST_UBLAS_INLINE
  123. vector_range &operator = (const vector_range &vr) {
  124. // ISSUE need a temporary, proxy can be overlaping alias
  125. vector_assign<scalar_assign> (*this, typename vector_temporary_traits<V>::type (vr));
  126. return *this;
  127. }
  128. BOOST_UBLAS_INLINE
  129. vector_range &assign_temporary (vector_range &vr) {
  130. // assign elements, proxied container remains the same
  131. vector_assign<scalar_assign> (*this, vr);
  132. return *this;
  133. }
  134. template<class AE>
  135. BOOST_UBLAS_INLINE
  136. vector_range &operator = (const vector_expression<AE> &ae) {
  137. vector_assign<scalar_assign> (*this, typename vector_temporary_traits<V>::type (ae));
  138. return *this;
  139. }
  140. template<class AE>
  141. BOOST_UBLAS_INLINE
  142. vector_range &assign (const vector_expression<AE> &ae) {
  143. vector_assign<scalar_assign> (*this, ae);
  144. return *this;
  145. }
  146. template<class AE>
  147. BOOST_UBLAS_INLINE
  148. vector_range &operator += (const vector_expression<AE> &ae) {
  149. vector_assign<scalar_assign> (*this, typename vector_temporary_traits<V>::type (*this + ae));
  150. return *this;
  151. }
  152. template<class AE>
  153. BOOST_UBLAS_INLINE
  154. vector_range &plus_assign (const vector_expression<AE> &ae) {
  155. vector_assign<scalar_plus_assign> (*this, ae);
  156. return *this;
  157. }
  158. template<class AE>
  159. BOOST_UBLAS_INLINE
  160. vector_range &operator -= (const vector_expression<AE> &ae) {
  161. vector_assign<scalar_assign> (*this, typename vector_temporary_traits<V>::type (*this - ae));
  162. return *this;
  163. }
  164. template<class AE>
  165. BOOST_UBLAS_INLINE
  166. vector_range &minus_assign (const vector_expression<AE> &ae) {
  167. vector_assign<scalar_minus_assign> (*this, ae);
  168. return *this;
  169. }
  170. template<class AT>
  171. BOOST_UBLAS_INLINE
  172. vector_range &operator *= (const AT &at) {
  173. vector_assign_scalar<scalar_multiplies_assign> (*this, at);
  174. return *this;
  175. }
  176. template<class AT>
  177. BOOST_UBLAS_INLINE
  178. vector_range &operator /= (const AT &at) {
  179. vector_assign_scalar<scalar_divides_assign> (*this, at);
  180. return *this;
  181. }
  182. // Closure comparison
  183. BOOST_UBLAS_INLINE
  184. bool same_closure (const vector_range &vr) const {
  185. return (*this).data_.same_closure (vr.data_);
  186. }
  187. // Comparison
  188. BOOST_UBLAS_INLINE
  189. bool operator == (const vector_range &vr) const {
  190. return (*this).data_ == vr.data_ && r_ == vr.r_;
  191. }
  192. // Swapping
  193. BOOST_UBLAS_INLINE
  194. void swap (vector_range vr) {
  195. if (this != &vr) {
  196. BOOST_UBLAS_CHECK (size () == vr.size (), bad_size ());
  197. // Sparse ranges may be nonconformant now.
  198. // std::swap_ranges (begin (), end (), vr.begin ());
  199. vector_swap<scalar_swap> (*this, vr);
  200. }
  201. }
  202. BOOST_UBLAS_INLINE
  203. friend void swap (vector_range vr1, vector_range vr2) {
  204. vr1.swap (vr2);
  205. }
  206. // Iterator types
  207. private:
  208. typedef typename V::const_iterator const_subiterator_type;
  209. typedef typename boost::mpl::if_<boost::is_const<V>,
  210. typename V::const_iterator,
  211. typename V::iterator>::type subiterator_type;
  212. public:
  213. #ifdef BOOST_UBLAS_USE_INDEXED_ITERATOR
  214. typedef indexed_iterator<vector_range<vector_type>,
  215. typename subiterator_type::iterator_category> iterator;
  216. typedef indexed_const_iterator<vector_range<vector_type>,
  217. typename const_subiterator_type::iterator_category> const_iterator;
  218. #else
  219. class const_iterator;
  220. class iterator;
  221. #endif
  222. // Element lookup
  223. BOOST_UBLAS_INLINE
  224. const_iterator find (size_type i) const {
  225. const_subiterator_type it (data_.find (start () + i));
  226. #ifdef BOOST_UBLAS_USE_INDEXED_ITERATOR
  227. return const_iterator (*this, it.index ());
  228. #else
  229. return const_iterator (*this, it);
  230. #endif
  231. }
  232. BOOST_UBLAS_INLINE
  233. iterator find (size_type i) {
  234. subiterator_type it (data_.find (start () + i));
  235. #ifdef BOOST_UBLAS_USE_INDEXED_ITERATOR
  236. return iterator (*this, it.index ());
  237. #else
  238. return iterator (*this, it);
  239. #endif
  240. }
  241. #ifndef BOOST_UBLAS_USE_INDEXED_ITERATOR
  242. class const_iterator:
  243. public container_const_reference<vector_range>,
  244. public iterator_base_traits<typename const_subiterator_type::iterator_category>::template
  245. iterator_base<const_iterator, value_type>::type {
  246. public:
  247. typedef typename const_subiterator_type::difference_type difference_type;
  248. typedef typename const_subiterator_type::value_type value_type;
  249. typedef typename const_subiterator_type::reference reference;
  250. typedef typename const_subiterator_type::pointer pointer;
  251. // Construction and destruction
  252. BOOST_UBLAS_INLINE
  253. const_iterator ():
  254. container_const_reference<self_type> (), it_ () {}
  255. BOOST_UBLAS_INLINE
  256. const_iterator (const self_type &vr, const const_subiterator_type &it):
  257. container_const_reference<self_type> (vr), it_ (it) {}
  258. BOOST_UBLAS_INLINE
  259. const_iterator (const typename self_type::iterator &it): // ISSUE self_type:: stops VC8 using std::iterator here
  260. container_const_reference<self_type> (it ()), it_ (it.it_) {}
  261. // Arithmetic
  262. BOOST_UBLAS_INLINE
  263. const_iterator &operator ++ () {
  264. ++ it_;
  265. return *this;
  266. }
  267. BOOST_UBLAS_INLINE
  268. const_iterator &operator -- () {
  269. -- it_;
  270. return *this;
  271. }
  272. BOOST_UBLAS_INLINE
  273. const_iterator &operator += (difference_type n) {
  274. it_ += n;
  275. return *this;
  276. }
  277. BOOST_UBLAS_INLINE
  278. const_iterator &operator -= (difference_type n) {
  279. it_ -= n;
  280. return *this;
  281. }
  282. BOOST_UBLAS_INLINE
  283. difference_type operator - (const const_iterator &it) const {
  284. BOOST_UBLAS_CHECK ((*this) ().same_closure (it ()), external_logic ());
  285. return it_ - it.it_;
  286. }
  287. // Dereference
  288. BOOST_UBLAS_INLINE
  289. const_reference operator * () const {
  290. BOOST_UBLAS_CHECK (index () < (*this) ().size (), bad_index ());
  291. return *it_;
  292. }
  293. BOOST_UBLAS_INLINE
  294. const_reference operator [] (difference_type n) const {
  295. return *(*this + n);
  296. }
  297. // Index
  298. BOOST_UBLAS_INLINE
  299. size_type index () const {
  300. return it_.index () - (*this) ().start ();
  301. }
  302. // Assignment
  303. BOOST_UBLAS_INLINE
  304. const_iterator &operator = (const const_iterator &it) {
  305. container_const_reference<self_type>::assign (&it ());
  306. it_ = it.it_;
  307. return *this;
  308. }
  309. // Comparison
  310. BOOST_UBLAS_INLINE
  311. bool operator == (const const_iterator &it) const {
  312. BOOST_UBLAS_CHECK ((*this) ().same_closure (it ()), external_logic ());
  313. return it_ == it.it_;
  314. }
  315. BOOST_UBLAS_INLINE
  316. bool operator < (const const_iterator &it) const {
  317. BOOST_UBLAS_CHECK ((*this) ().same_closure (it ()), external_logic ());
  318. return it_ < it.it_;
  319. }
  320. private:
  321. const_subiterator_type it_;
  322. };
  323. #endif
  324. BOOST_UBLAS_INLINE
  325. const_iterator begin () const {
  326. return find (0);
  327. }
  328. BOOST_UBLAS_INLINE
  329. const_iterator end () const {
  330. return find (size ());
  331. }
  332. #ifndef BOOST_UBLAS_USE_INDEXED_ITERATOR
  333. class iterator:
  334. public container_reference<vector_range>,
  335. public iterator_base_traits<typename subiterator_type::iterator_category>::template
  336. iterator_base<iterator, value_type>::type {
  337. public:
  338. typedef typename subiterator_type::difference_type difference_type;
  339. typedef typename subiterator_type::value_type value_type;
  340. typedef typename subiterator_type::reference reference;
  341. typedef typename subiterator_type::pointer pointer;
  342. // Construction and destruction
  343. BOOST_UBLAS_INLINE
  344. iterator ():
  345. container_reference<self_type> (), it_ () {}
  346. BOOST_UBLAS_INLINE
  347. iterator (self_type &vr, const subiterator_type &it):
  348. container_reference<self_type> (vr), it_ (it) {}
  349. // Arithmetic
  350. BOOST_UBLAS_INLINE
  351. iterator &operator ++ () {
  352. ++ it_;
  353. return *this;
  354. }
  355. BOOST_UBLAS_INLINE
  356. iterator &operator -- () {
  357. -- it_;
  358. return *this;
  359. }
  360. BOOST_UBLAS_INLINE
  361. iterator &operator += (difference_type n) {
  362. it_ += n;
  363. return *this;
  364. }
  365. BOOST_UBLAS_INLINE
  366. iterator &operator -= (difference_type n) {
  367. it_ -= n;
  368. return *this;
  369. }
  370. BOOST_UBLAS_INLINE
  371. difference_type operator - (const iterator &it) const {
  372. BOOST_UBLAS_CHECK ((*this) ().same_closure (it ()), external_logic ());
  373. return it_ - it.it_;
  374. }
  375. // Dereference
  376. BOOST_UBLAS_INLINE
  377. reference operator * () const {
  378. BOOST_UBLAS_CHECK (index () < (*this) ().size (), bad_index ());
  379. return *it_;
  380. }
  381. BOOST_UBLAS_INLINE
  382. reference operator [] (difference_type n) const {
  383. return *(*this + n);
  384. }
  385. // Index
  386. BOOST_UBLAS_INLINE
  387. size_type index () const {
  388. return it_.index () - (*this) ().start ();
  389. }
  390. // Assignment
  391. BOOST_UBLAS_INLINE
  392. iterator &operator = (const iterator &it) {
  393. container_reference<self_type>::assign (&it ());
  394. it_ = it.it_;
  395. return *this;
  396. }
  397. // Comparison
  398. BOOST_UBLAS_INLINE
  399. bool operator == (const iterator &it) const {
  400. BOOST_UBLAS_CHECK ((*this) ().same_closure (it ()), external_logic ());
  401. return it_ == it.it_;
  402. }
  403. BOOST_UBLAS_INLINE
  404. bool operator < (const iterator &it) const {
  405. BOOST_UBLAS_CHECK ((*this) ().same_closure (it ()), external_logic ());
  406. return it_ < it.it_;
  407. }
  408. private:
  409. subiterator_type it_;
  410. friend class const_iterator;
  411. };
  412. #endif
  413. BOOST_UBLAS_INLINE
  414. iterator begin () {
  415. return find (0);
  416. }
  417. BOOST_UBLAS_INLINE
  418. iterator end () {
  419. return find (size ());
  420. }
  421. // Reverse iterator
  422. typedef reverse_iterator_base<const_iterator> const_reverse_iterator;
  423. typedef reverse_iterator_base<iterator> reverse_iterator;
  424. BOOST_UBLAS_INLINE
  425. const_reverse_iterator rbegin () const {
  426. return const_reverse_iterator (end ());
  427. }
  428. BOOST_UBLAS_INLINE
  429. const_reverse_iterator rend () const {
  430. return const_reverse_iterator (begin ());
  431. }
  432. BOOST_UBLAS_INLINE
  433. reverse_iterator rbegin () {
  434. return reverse_iterator (end ());
  435. }
  436. BOOST_UBLAS_INLINE
  437. reverse_iterator rend () {
  438. return reverse_iterator (begin ());
  439. }
  440. private:
  441. vector_closure_type data_;
  442. range_type r_;
  443. };
  444. // ------------------
  445. // Simple Projections
  446. // ------------------
  447. /** \brief Return a \c vector_range on a specified vector, a start and stop index.
  448. * Return a \c vector_range on a specified vector, a start and stop index. The resulting \c vector_range can be manipulated like a normal vector.
  449. * If the specified range falls outside that of of the index range of the vector, then the resulting \c vector_range is not a well formed
  450. * Vector Expression and access to an element outside of index range of the vector is \b undefined.
  451. */
  452. template<class V>
  453. BOOST_UBLAS_INLINE
  454. vector_range<V> subrange (V &data, typename V::size_type start, typename V::size_type stop) {
  455. typedef basic_range<typename V::size_type, typename V::difference_type> range_type;
  456. return vector_range<V> (data, range_type (start, stop));
  457. }
  458. /** \brief Return a \c const \c vector_range on a specified vector, a start and stop index.
  459. * Return a \c const \c vector_range on a specified vector, a start and stop index. The resulting \c const \c vector_range can be manipulated like a normal vector.
  460. *If the specified range falls outside that of of the index range of the vector, then the resulting \c vector_range is not a well formed
  461. * Vector Expression and access to an element outside of index range of the vector is \b undefined.
  462. */
  463. template<class V>
  464. BOOST_UBLAS_INLINE
  465. vector_range<const V> subrange (const V &data, typename V::size_type start, typename V::size_type stop) {
  466. typedef basic_range<typename V::size_type, typename V::difference_type> range_type;
  467. return vector_range<const V> (data, range_type (start, stop));
  468. }
  469. // -------------------
  470. // Generic Projections
  471. // -------------------
  472. /** \brief Return a \c const \c vector_range on a specified vector and \c range
  473. * Return a \c const \c vector_range on a specified vector and \c range. The resulting \c vector_range can be manipulated like a normal vector.
  474. * If the specified range falls outside that of of the index range of the vector, then the resulting \c vector_range is not a well formed
  475. * Vector Expression and access to an element outside of index range of the vector is \b undefined.
  476. */
  477. template<class V>
  478. BOOST_UBLAS_INLINE
  479. vector_range<V> project (V &data, typename vector_range<V>::range_type const &r) {
  480. return vector_range<V> (data, r);
  481. }
  482. /** \brief Return a \c vector_range on a specified vector and \c range
  483. * Return a \c vector_range on a specified vector and \c range. The resulting \c vector_range can be manipulated like a normal vector.
  484. * If the specified range falls outside that of of the index range of the vector, then the resulting \c vector_range is not a well formed
  485. * Vector Expression and access to an element outside of index range of the vector is \b undefined.
  486. */
  487. template<class V>
  488. BOOST_UBLAS_INLINE
  489. const vector_range<const V> project (const V &data, typename vector_range<V>::range_type const &r) {
  490. // ISSUE was: return vector_range<V> (const_cast<V &> (data), r);
  491. return vector_range<const V> (data, r);
  492. }
  493. /** \brief Return a \c const \c vector_range on a specified vector and const \c range
  494. * Return a \c const \c vector_range on a specified vector and const \c range. The resulting \c vector_range can be manipulated like a normal vector.
  495. * If the specified range falls outside that of of the index range of the vector, then the resulting \c vector_range is not a well formed
  496. * Vector Expression and access to an element outside of index range of the vector is \b undefined.
  497. */
  498. template<class V>
  499. BOOST_UBLAS_INLINE
  500. vector_range<V> project (vector_range<V> &data, const typename vector_range<V>::range_type &r) {
  501. return data.project (r);
  502. }
  503. /** \brief Return a \c vector_range on a specified vector and const \c range
  504. * Return a \c vector_range on a specified vector and const \c range. The resulting \c vector_range can be manipulated like a normal vector.
  505. * If the specified range falls outside that of of the index range of the vector, then the resulting \c vector_range is not a well formed
  506. * Vector Expression and access to an element outside of index range of the vector is \b undefined.
  507. */
  508. template<class V>
  509. BOOST_UBLAS_INLINE
  510. const vector_range<V> project (const vector_range<V> &data, const typename vector_range<V>::range_type &r) {
  511. return data.project (r);
  512. }
  513. // Specialization of temporary_traits
  514. template <class V>
  515. struct vector_temporary_traits< vector_range<V> >
  516. : vector_temporary_traits< V > {} ;
  517. template <class V>
  518. struct vector_temporary_traits< const vector_range<V> >
  519. : vector_temporary_traits< V > {} ;
  520. /** \brief A vector referencing a non continuous subvector of elements of vector v containing all elements specified by \c slice.
  521. *
  522. * A vector slice can be used as a normal vector in any expression.
  523. * If the specified slice falls outside that of the index slice of the vector, then
  524. * the \c vector_slice is not a well formed \i Vector \i Expression and access to an
  525. * element outside of index slice of the vector is \b undefined.
  526. *
  527. * A slice is a generalization of a range. In a range going from \f$a\f$ to \f$b\f$,
  528. * all elements belong to the range. In a slice, a \i \f$step\f$ can be specified meaning to
  529. * take one element over \f$step\f$ in the range specified from \f$a\f$ to \f$b\f$.
  530. * Obviously, a slice with a \f$step\f$ of 1 is equivalent to a range.
  531. *
  532. * \tparam V the type of vector referenced (for example \c vector<double>)
  533. */
  534. template<class V>
  535. class vector_slice:
  536. public vector_expression<vector_slice<V> > {
  537. typedef vector_slice<V> self_type;
  538. public:
  539. #ifdef BOOST_UBLAS_ENABLE_PROXY_SHORTCUTS
  540. using vector_expression<self_type>::operator ();
  541. #endif
  542. typedef const V const_vector_type;
  543. typedef V vector_type;
  544. typedef typename V::size_type size_type;
  545. typedef typename V::difference_type difference_type;
  546. typedef typename V::value_type value_type;
  547. typedef typename V::const_reference const_reference;
  548. typedef typename boost::mpl::if_<boost::is_const<V>,
  549. typename V::const_reference,
  550. typename V::reference>::type reference;
  551. typedef typename boost::mpl::if_<boost::is_const<V>,
  552. typename V::const_closure_type,
  553. typename V::closure_type>::type vector_closure_type;
  554. typedef basic_range<size_type, difference_type> range_type;
  555. typedef basic_slice<size_type, difference_type> slice_type;
  556. typedef const self_type const_closure_type;
  557. typedef self_type closure_type;
  558. typedef typename storage_restrict_traits<typename V::storage_category,
  559. dense_proxy_tag>::storage_category storage_category;
  560. // Construction and destruction
  561. BOOST_UBLAS_INLINE
  562. vector_slice (vector_type &data, const slice_type &s):
  563. data_ (data), s_ (s.preprocess (data.size ())) {
  564. // Early checking of preconditions here.
  565. // BOOST_UBLAS_CHECK (s_.start () <= data_.size () &&
  566. // s_.start () + s_.stride () * (s_.size () - (s_.size () > 0)) <= data_.size (), bad_index ());
  567. }
  568. BOOST_UBLAS_INLINE
  569. vector_slice (const vector_closure_type &data, const slice_type &s, int):
  570. data_ (data), s_ (s.preprocess (data.size ())) {
  571. // Early checking of preconditions here.
  572. // BOOST_UBLAS_CHECK (s_.start () <= data_.size () &&
  573. // s_.start () + s_.stride () * (s_.size () - (s_.size () > 0)) <= data_.size (), bad_index ());
  574. }
  575. // Accessors
  576. BOOST_UBLAS_INLINE
  577. size_type start () const {
  578. return s_.start ();
  579. }
  580. BOOST_UBLAS_INLINE
  581. difference_type stride () const {
  582. return s_.stride ();
  583. }
  584. BOOST_UBLAS_INLINE
  585. size_type size () const {
  586. return s_.size ();
  587. }
  588. // Storage accessors
  589. BOOST_UBLAS_INLINE
  590. const vector_closure_type &data () const {
  591. return data_;
  592. }
  593. BOOST_UBLAS_INLINE
  594. vector_closure_type &data () {
  595. return data_;
  596. }
  597. // Element access
  598. #ifndef BOOST_UBLAS_PROXY_CONST_MEMBER
  599. BOOST_UBLAS_INLINE
  600. const_reference operator () (size_type i) const {
  601. return data_ (s_ (i));
  602. }
  603. BOOST_UBLAS_INLINE
  604. reference operator () (size_type i) {
  605. return data_ (s_ (i));
  606. }
  607. BOOST_UBLAS_INLINE
  608. const_reference operator [] (size_type i) const {
  609. return (*this) (i);
  610. }
  611. BOOST_UBLAS_INLINE
  612. reference operator [] (size_type i) {
  613. return (*this) (i);
  614. }
  615. #else
  616. BOOST_UBLAS_INLINE
  617. reference operator () (size_type i) const {
  618. return data_ (s_ (i));
  619. }
  620. BOOST_UBLAS_INLINE
  621. reference operator [] (size_type i) const {
  622. return (*this) (i);
  623. }
  624. #endif
  625. // ISSUE can this be done in free project function?
  626. // Although a const function can create a non-const proxy to a non-const object
  627. // Critical is that vector_type and data_ (vector_closure_type) are const correct
  628. BOOST_UBLAS_INLINE
  629. vector_slice<vector_type> project (const range_type &r) const {
  630. return vector_slice<vector_type> (data_, s_.compose (r.preprocess (data_.size ())), false);
  631. }
  632. BOOST_UBLAS_INLINE
  633. vector_slice<vector_type> project (const slice_type &s) const {
  634. return vector_slice<vector_type> (data_, s_.compose (s.preprocess (data_.size ())), false);
  635. }
  636. // Assignment
  637. BOOST_UBLAS_INLINE
  638. vector_slice &operator = (const vector_slice &vs) {
  639. // ISSUE need a temporary, proxy can be overlaping alias
  640. vector_assign<scalar_assign> (*this, typename vector_temporary_traits<V>::type (vs));
  641. return *this;
  642. }
  643. BOOST_UBLAS_INLINE
  644. vector_slice &assign_temporary (vector_slice &vs) {
  645. // assign elements, proxied container remains the same
  646. vector_assign<scalar_assign> (*this, vs);
  647. return *this;
  648. }
  649. template<class AE>
  650. BOOST_UBLAS_INLINE
  651. vector_slice &operator = (const vector_expression<AE> &ae) {
  652. vector_assign<scalar_assign> (*this, typename vector_temporary_traits<V>::type (ae));
  653. return *this;
  654. }
  655. template<class AE>
  656. BOOST_UBLAS_INLINE
  657. vector_slice &assign (const vector_expression<AE> &ae) {
  658. vector_assign<scalar_assign> (*this, ae);
  659. return *this;
  660. }
  661. template<class AE>
  662. BOOST_UBLAS_INLINE
  663. vector_slice &operator += (const vector_expression<AE> &ae) {
  664. vector_assign<scalar_assign> (*this, typename vector_temporary_traits<V>::type (*this + ae));
  665. return *this;
  666. }
  667. template<class AE>
  668. BOOST_UBLAS_INLINE
  669. vector_slice &plus_assign (const vector_expression<AE> &ae) {
  670. vector_assign<scalar_plus_assign> (*this, ae);
  671. return *this;
  672. }
  673. template<class AE>
  674. BOOST_UBLAS_INLINE
  675. vector_slice &operator -= (const vector_expression<AE> &ae) {
  676. vector_assign<scalar_assign> (*this, typename vector_temporary_traits<V>::type (*this - ae));
  677. return *this;
  678. }
  679. template<class AE>
  680. BOOST_UBLAS_INLINE
  681. vector_slice &minus_assign (const vector_expression<AE> &ae) {
  682. vector_assign<scalar_minus_assign> (*this, ae);
  683. return *this;
  684. }
  685. template<class AT>
  686. BOOST_UBLAS_INLINE
  687. vector_slice &operator *= (const AT &at) {
  688. vector_assign_scalar<scalar_multiplies_assign> (*this, at);
  689. return *this;
  690. }
  691. template<class AT>
  692. BOOST_UBLAS_INLINE
  693. vector_slice &operator /= (const AT &at) {
  694. vector_assign_scalar<scalar_divides_assign> (*this, at);
  695. return *this;
  696. }
  697. // Closure comparison
  698. BOOST_UBLAS_INLINE
  699. bool same_closure (const vector_slice &vr) const {
  700. return (*this).data_.same_closure (vr.data_);
  701. }
  702. // Comparison
  703. BOOST_UBLAS_INLINE
  704. bool operator == (const vector_slice &vs) const {
  705. return (*this).data_ == vs.data_ && s_ == vs.s_;
  706. }
  707. // Swapping
  708. BOOST_UBLAS_INLINE
  709. void swap (vector_slice vs) {
  710. if (this != &vs) {
  711. BOOST_UBLAS_CHECK (size () == vs.size (), bad_size ());
  712. // Sparse ranges may be nonconformant now.
  713. // std::swap_ranges (begin (), end (), vs.begin ());
  714. vector_swap<scalar_swap> (*this, vs);
  715. }
  716. }
  717. BOOST_UBLAS_INLINE
  718. friend void swap (vector_slice vs1, vector_slice vs2) {
  719. vs1.swap (vs2);
  720. }
  721. // Iterator types
  722. private:
  723. // Use slice as an index - FIXME this fails for packed assignment
  724. typedef typename slice_type::const_iterator const_subiterator_type;
  725. typedef typename slice_type::const_iterator subiterator_type;
  726. public:
  727. #ifdef BOOST_UBLAS_USE_INDEXED_ITERATOR
  728. typedef indexed_iterator<vector_slice<vector_type>,
  729. typename vector_type::iterator::iterator_category> iterator;
  730. typedef indexed_const_iterator<vector_slice<vector_type>,
  731. typename vector_type::const_iterator::iterator_category> const_iterator;
  732. #else
  733. class const_iterator;
  734. class iterator;
  735. #endif
  736. // Element lookup
  737. BOOST_UBLAS_INLINE
  738. const_iterator find (size_type i) const {
  739. #ifdef BOOST_UBLAS_USE_INDEXED_ITERATOR
  740. return const_iterator (*this, i);
  741. #else
  742. return const_iterator (*this, s_.begin () + i);
  743. #endif
  744. }
  745. BOOST_UBLAS_INLINE
  746. iterator find (size_type i) {
  747. #ifdef BOOST_UBLAS_USE_INDEXED_ITERATOR
  748. return iterator (*this, i);
  749. #else
  750. return iterator (*this, s_.begin () + i);
  751. #endif
  752. }
  753. #ifndef BOOST_UBLAS_USE_INDEXED_ITERATOR
  754. class const_iterator:
  755. public container_const_reference<vector_slice>,
  756. public iterator_base_traits<typename V::const_iterator::iterator_category>::template
  757. iterator_base<const_iterator, value_type>::type {
  758. public:
  759. typedef typename V::const_iterator::difference_type difference_type;
  760. typedef typename V::const_iterator::value_type value_type;
  761. typedef typename V::const_reference reference; //FIXME due to indexing access
  762. typedef typename V::const_iterator::pointer pointer;
  763. // Construction and destruction
  764. BOOST_UBLAS_INLINE
  765. const_iterator ():
  766. container_const_reference<self_type> (), it_ () {}
  767. BOOST_UBLAS_INLINE
  768. const_iterator (const self_type &vs, const const_subiterator_type &it):
  769. container_const_reference<self_type> (vs), it_ (it) {}
  770. BOOST_UBLAS_INLINE
  771. const_iterator (const typename self_type::iterator &it): // ISSUE self_type:: stops VC8 using std::iterator here
  772. container_const_reference<self_type> (it ()), it_ (it.it_) {}
  773. // Arithmetic
  774. BOOST_UBLAS_INLINE
  775. const_iterator &operator ++ () {
  776. ++ it_;
  777. return *this;
  778. }
  779. BOOST_UBLAS_INLINE
  780. const_iterator &operator -- () {
  781. -- it_;
  782. return *this;
  783. }
  784. BOOST_UBLAS_INLINE
  785. const_iterator &operator += (difference_type n) {
  786. it_ += n;
  787. return *this;
  788. }
  789. BOOST_UBLAS_INLINE
  790. const_iterator &operator -= (difference_type n) {
  791. it_ -= n;
  792. return *this;
  793. }
  794. BOOST_UBLAS_INLINE
  795. difference_type operator - (const const_iterator &it) const {
  796. BOOST_UBLAS_CHECK ((*this) ().same_closure (it ()), external_logic ());
  797. return it_ - it.it_;
  798. }
  799. // Dereference
  800. BOOST_UBLAS_INLINE
  801. const_reference operator * () const {
  802. // FIXME replace find with at_element
  803. BOOST_UBLAS_CHECK (index () < (*this) ().size (), bad_index ());
  804. return (*this) ().data_ (*it_);
  805. }
  806. BOOST_UBLAS_INLINE
  807. const_reference operator [] (difference_type n) const {
  808. return *(*this + n);
  809. }
  810. // Index
  811. BOOST_UBLAS_INLINE
  812. size_type index () const {
  813. return it_.index ();
  814. }
  815. // Assignment
  816. BOOST_UBLAS_INLINE
  817. const_iterator &operator = (const const_iterator &it) {
  818. container_const_reference<self_type>::assign (&it ());
  819. it_ = it.it_;
  820. return *this;
  821. }
  822. // Comparison
  823. BOOST_UBLAS_INLINE
  824. bool operator == (const const_iterator &it) const {
  825. BOOST_UBLAS_CHECK ((*this) ().same_closure (it ()), external_logic ());
  826. return it_ == it.it_;
  827. }
  828. BOOST_UBLAS_INLINE
  829. bool operator < (const const_iterator &it) const {
  830. BOOST_UBLAS_CHECK ((*this) ().same_closure (it ()), external_logic ());
  831. return it_ < it.it_;
  832. }
  833. private:
  834. const_subiterator_type it_;
  835. };
  836. #endif
  837. BOOST_UBLAS_INLINE
  838. const_iterator begin () const {
  839. return find (0);
  840. }
  841. BOOST_UBLAS_INLINE
  842. const_iterator end () const {
  843. return find (size ());
  844. }
  845. #ifndef BOOST_UBLAS_USE_INDEXED_ITERATOR
  846. class iterator:
  847. public container_reference<vector_slice>,
  848. public iterator_base_traits<typename V::iterator::iterator_category>::template
  849. iterator_base<iterator, value_type>::type {
  850. public:
  851. typedef typename V::iterator::difference_type difference_type;
  852. typedef typename V::iterator::value_type value_type;
  853. typedef typename V::reference reference; //FIXME due to indexing access
  854. typedef typename V::iterator::pointer pointer;
  855. // Construction and destruction
  856. BOOST_UBLAS_INLINE
  857. iterator ():
  858. container_reference<self_type> (), it_ () {}
  859. BOOST_UBLAS_INLINE
  860. iterator (self_type &vs, const subiterator_type &it):
  861. container_reference<self_type> (vs), it_ (it) {}
  862. // Arithmetic
  863. BOOST_UBLAS_INLINE
  864. iterator &operator ++ () {
  865. ++ it_;
  866. return *this;
  867. }
  868. BOOST_UBLAS_INLINE
  869. iterator &operator -- () {
  870. -- it_;
  871. return *this;
  872. }
  873. BOOST_UBLAS_INLINE
  874. iterator &operator += (difference_type n) {
  875. it_ += n;
  876. return *this;
  877. }
  878. BOOST_UBLAS_INLINE
  879. iterator &operator -= (difference_type n) {
  880. it_ -= n;
  881. return *this;
  882. }
  883. BOOST_UBLAS_INLINE
  884. difference_type operator - (const iterator &it) const {
  885. BOOST_UBLAS_CHECK ((*this) ().same_closure (it ()), external_logic ());
  886. return it_ - it.it_;
  887. }
  888. // Dereference
  889. BOOST_UBLAS_INLINE
  890. reference operator * () const {
  891. // FIXME replace find with at_element
  892. BOOST_UBLAS_CHECK (index () < (*this) ().size (), bad_index ());
  893. return (*this) ().data_ (*it_);
  894. }
  895. BOOST_UBLAS_INLINE
  896. reference operator [] (difference_type n) const {
  897. return *(*this + n);
  898. }
  899. // Index
  900. BOOST_UBLAS_INLINE
  901. size_type index () const {
  902. return it_.index ();
  903. }
  904. // Assignment
  905. BOOST_UBLAS_INLINE
  906. iterator &operator = (const iterator &it) {
  907. container_reference<self_type>::assign (&it ());
  908. it_ = it.it_;
  909. return *this;
  910. }
  911. // Comparison
  912. BOOST_UBLAS_INLINE
  913. bool operator == (const iterator &it) const {
  914. BOOST_UBLAS_CHECK ((*this) ().same_closure (it ()), external_logic ());
  915. return it_ == it.it_;
  916. }
  917. BOOST_UBLAS_INLINE
  918. bool operator < (const iterator &it) const {
  919. BOOST_UBLAS_CHECK ((*this) ().same_closure (it ()), external_logic ());
  920. return it_ < it.it_;
  921. }
  922. private:
  923. subiterator_type it_;
  924. friend class const_iterator;
  925. };
  926. #endif
  927. BOOST_UBLAS_INLINE
  928. iterator begin () {
  929. return find (0);
  930. }
  931. BOOST_UBLAS_INLINE
  932. iterator end () {
  933. return find (size ());
  934. }
  935. // Reverse iterator
  936. typedef reverse_iterator_base<const_iterator> const_reverse_iterator;
  937. typedef reverse_iterator_base<iterator> reverse_iterator;
  938. BOOST_UBLAS_INLINE
  939. const_reverse_iterator rbegin () const {
  940. return const_reverse_iterator (end ());
  941. }
  942. BOOST_UBLAS_INLINE
  943. const_reverse_iterator rend () const {
  944. return const_reverse_iterator (begin ());
  945. }
  946. BOOST_UBLAS_INLINE
  947. reverse_iterator rbegin () {
  948. return reverse_iterator (end ());
  949. }
  950. BOOST_UBLAS_INLINE
  951. reverse_iterator rend () {
  952. return reverse_iterator (begin ());
  953. }
  954. private:
  955. vector_closure_type data_;
  956. slice_type s_;
  957. };
  958. // Simple Projections
  959. template<class V>
  960. BOOST_UBLAS_INLINE
  961. vector_slice<V> subslice (V &data, typename V::size_type start, typename V::difference_type stride, typename V::size_type size) {
  962. typedef basic_slice<typename V::size_type, typename V::difference_type> slice_type;
  963. return vector_slice<V> (data, slice_type (start, stride, size));
  964. }
  965. template<class V>
  966. BOOST_UBLAS_INLINE
  967. vector_slice<const V> subslice (const V &data, typename V::size_type start, typename V::difference_type stride, typename V::size_type size) {
  968. typedef basic_slice<typename V::size_type, typename V::difference_type> slice_type;
  969. return vector_slice<const V> (data, slice_type (start, stride, size));
  970. }
  971. // Generic Projections
  972. template<class V>
  973. BOOST_UBLAS_INLINE
  974. vector_slice<V> project (V &data, const typename vector_slice<V>::slice_type &s) {
  975. return vector_slice<V> (data, s);
  976. }
  977. template<class V>
  978. BOOST_UBLAS_INLINE
  979. const vector_slice<const V> project (const V &data, const typename vector_slice<V>::slice_type &s) {
  980. // ISSUE was: return vector_slice<V> (const_cast<V &> (data), s);
  981. return vector_slice<const V> (data, s);
  982. }
  983. template<class V>
  984. BOOST_UBLAS_INLINE
  985. vector_slice<V> project (vector_slice<V> &data, const typename vector_slice<V>::slice_type &s) {
  986. return data.project (s);
  987. }
  988. template<class V>
  989. BOOST_UBLAS_INLINE
  990. const vector_slice<V> project (const vector_slice<V> &data, const typename vector_slice<V>::slice_type &s) {
  991. return data.project (s);
  992. }
  993. // ISSUE in the following two functions it would be logical to use vector_slice<V>::range_type but this confuses VC7.1 and 8.0
  994. template<class V>
  995. BOOST_UBLAS_INLINE
  996. vector_slice<V> project (vector_slice<V> &data, const typename vector_range<V>::range_type &r) {
  997. return data.project (r);
  998. }
  999. template<class V>
  1000. BOOST_UBLAS_INLINE
  1001. const vector_slice<V> project (const vector_slice<V> &data, const typename vector_range<V>::range_type &r) {
  1002. return data.project (r);
  1003. }
  1004. // Specialization of temporary_traits
  1005. template <class V>
  1006. struct vector_temporary_traits< vector_slice<V> >
  1007. : vector_temporary_traits< V > {} ;
  1008. template <class V>
  1009. struct vector_temporary_traits< const vector_slice<V> >
  1010. : vector_temporary_traits< V > {} ;
  1011. // Vector based indirection class
  1012. // Contributed by Toon Knapen.
  1013. // Extended and optimized by Kresimir Fresl.
  1014. /** \brief A vector referencing a non continuous subvector of elements given another vector of indices.
  1015. *
  1016. * It is the most general version of any subvectors because it uses another vector of indices to reference
  1017. * the subvector.
  1018. *
  1019. * The vector of indices can be of any type with the restriction that its elements must be
  1020. * type-compatible with the size_type \c of the container. In practice, the following are good candidates:
  1021. * - \c boost::numeric::ublas::indirect_array<A> where \c A can be \c int, \c size_t, \c long, etc...
  1022. * - \c std::vector<A> where \c A can \c int, \c size_t, \c long, etc...
  1023. * - \c boost::numeric::ublas::vector<int> can work too (\c int can be replaced by another integer type)
  1024. * - etc...
  1025. *
  1026. * An indirect vector can be used as a normal vector in any expression. If the specified indirect vector
  1027. * falls outside that of the indices of the vector, then the \c vector_indirect is not a well formed
  1028. * \i Vector \i Expression and access to an element outside of indices of the vector is \b undefined.
  1029. *
  1030. * \tparam V the type of vector referenced (for example \c vector<double>)
  1031. * \tparam IA the type of index vector. Default is \c ublas::indirect_array<>
  1032. */
  1033. template<class V, class IA>
  1034. class vector_indirect:
  1035. public vector_expression<vector_indirect<V, IA> > {
  1036. typedef vector_indirect<V, IA> self_type;
  1037. public:
  1038. #ifdef BOOST_UBLAS_ENABLE_PROXY_SHORTCUTS
  1039. using vector_expression<self_type>::operator ();
  1040. #endif
  1041. typedef const V const_vector_type;
  1042. typedef V vector_type;
  1043. typedef const IA const_indirect_array_type;
  1044. typedef IA indirect_array_type;
  1045. typedef typename V::size_type size_type;
  1046. typedef typename V::difference_type difference_type;
  1047. typedef typename V::value_type value_type;
  1048. typedef typename V::const_reference const_reference;
  1049. typedef typename boost::mpl::if_<boost::is_const<V>,
  1050. typename V::const_reference,
  1051. typename V::reference>::type reference;
  1052. typedef typename boost::mpl::if_<boost::is_const<V>,
  1053. typename V::const_closure_type,
  1054. typename V::closure_type>::type vector_closure_type;
  1055. typedef basic_range<size_type, difference_type> range_type;
  1056. typedef basic_slice<size_type, difference_type> slice_type;
  1057. typedef const self_type const_closure_type;
  1058. typedef self_type closure_type;
  1059. typedef typename storage_restrict_traits<typename V::storage_category,
  1060. dense_proxy_tag>::storage_category storage_category;
  1061. // Construction and destruction
  1062. BOOST_UBLAS_INLINE
  1063. vector_indirect (vector_type &data, size_type size):
  1064. data_ (data), ia_ (size) {}
  1065. BOOST_UBLAS_INLINE
  1066. vector_indirect (vector_type &data, const indirect_array_type &ia):
  1067. data_ (data), ia_ (ia.preprocess (data.size ())) {}
  1068. BOOST_UBLAS_INLINE
  1069. vector_indirect (const vector_closure_type &data, const indirect_array_type &ia, int):
  1070. data_ (data), ia_ (ia.preprocess (data.size ())) {}
  1071. // Accessors
  1072. BOOST_UBLAS_INLINE
  1073. size_type size () const {
  1074. return ia_.size ();
  1075. }
  1076. BOOST_UBLAS_INLINE
  1077. const_indirect_array_type &indirect () const {
  1078. return ia_;
  1079. }
  1080. BOOST_UBLAS_INLINE
  1081. indirect_array_type &indirect () {
  1082. return ia_;
  1083. }
  1084. // Storage accessors
  1085. BOOST_UBLAS_INLINE
  1086. const vector_closure_type &data () const {
  1087. return data_;
  1088. }
  1089. BOOST_UBLAS_INLINE
  1090. vector_closure_type &data () {
  1091. return data_;
  1092. }
  1093. // Element access
  1094. #ifndef BOOST_UBLAS_PROXY_CONST_MEMBER
  1095. BOOST_UBLAS_INLINE
  1096. const_reference operator () (size_type i) const {
  1097. return data_ (ia_ (i));
  1098. }
  1099. BOOST_UBLAS_INLINE
  1100. reference operator () (size_type i) {
  1101. return data_ (ia_ (i));
  1102. }
  1103. BOOST_UBLAS_INLINE
  1104. const_reference operator [] (size_type i) const {
  1105. return (*this) (i);
  1106. }
  1107. BOOST_UBLAS_INLINE
  1108. reference operator [] (size_type i) {
  1109. return (*this) (i);
  1110. }
  1111. #else
  1112. BOOST_UBLAS_INLINE
  1113. reference operator () (size_type i) const {
  1114. return data_ (ia_ (i));
  1115. }
  1116. BOOST_UBLAS_INLINE
  1117. reference operator [] (size_type i) const {
  1118. return (*this) (i);
  1119. }
  1120. #endif
  1121. // ISSUE can this be done in free project function?
  1122. // Although a const function can create a non-const proxy to a non-const object
  1123. // Critical is that vector_type and data_ (vector_closure_type) are const correct
  1124. BOOST_UBLAS_INLINE
  1125. vector_indirect<vector_type, indirect_array_type> project (const range_type &r) const {
  1126. return vector_indirect<vector_type, indirect_array_type> (data_, ia_.compose (r.preprocess (data_.size ())), 0);
  1127. }
  1128. BOOST_UBLAS_INLINE
  1129. vector_indirect<vector_type, indirect_array_type> project (const slice_type &s) const {
  1130. return vector_indirect<vector_type, indirect_array_type> (data_, ia_.compose (s.preprocess (data_.size ())), 0);
  1131. }
  1132. BOOST_UBLAS_INLINE
  1133. vector_indirect<vector_type, indirect_array_type> project (const indirect_array_type &ia) const {
  1134. return vector_indirect<vector_type, indirect_array_type> (data_, ia_.compose (ia.preprocess (data_.size ())), 0);
  1135. }
  1136. // Assignment
  1137. BOOST_UBLAS_INLINE
  1138. vector_indirect &operator = (const vector_indirect &vi) {
  1139. // ISSUE need a temporary, proxy can be overlaping alias
  1140. vector_assign<scalar_assign> (*this, typename vector_temporary_traits<V>::type (vi));
  1141. return *this;
  1142. }
  1143. BOOST_UBLAS_INLINE
  1144. vector_indirect &assign_temporary (vector_indirect &vi) {
  1145. // assign elements, proxied container remains the same
  1146. vector_assign<scalar_assign> (*this, vi);
  1147. return *this;
  1148. }
  1149. template<class AE>
  1150. BOOST_UBLAS_INLINE
  1151. vector_indirect &operator = (const vector_expression<AE> &ae) {
  1152. vector_assign<scalar_assign> (*this, typename vector_temporary_traits<V>::type (ae));
  1153. return *this;
  1154. }
  1155. template<class AE>
  1156. BOOST_UBLAS_INLINE
  1157. vector_indirect &assign (const vector_expression<AE> &ae) {
  1158. vector_assign<scalar_assign> (*this, ae);
  1159. return *this;
  1160. }
  1161. template<class AE>
  1162. BOOST_UBLAS_INLINE
  1163. vector_indirect &operator += (const vector_expression<AE> &ae) {
  1164. vector_assign<scalar_assign> (*this, typename vector_temporary_traits<V>::type (*this + ae));
  1165. return *this;
  1166. }
  1167. template<class AE>
  1168. BOOST_UBLAS_INLINE
  1169. vector_indirect &plus_assign (const vector_expression<AE> &ae) {
  1170. vector_assign<scalar_plus_assign> (*this, ae);
  1171. return *this;
  1172. }
  1173. template<class AE>
  1174. BOOST_UBLAS_INLINE
  1175. vector_indirect &operator -= (const vector_expression<AE> &ae) {
  1176. vector_assign<scalar_assign> (*this, typename vector_temporary_traits<V>::type (*this - ae));
  1177. return *this;
  1178. }
  1179. template<class AE>
  1180. BOOST_UBLAS_INLINE
  1181. vector_indirect &minus_assign (const vector_expression<AE> &ae) {
  1182. vector_assign<scalar_minus_assign> (*this, ae);
  1183. return *this;
  1184. }
  1185. template<class AT>
  1186. BOOST_UBLAS_INLINE
  1187. vector_indirect &operator *= (const AT &at) {
  1188. vector_assign_scalar<scalar_multiplies_assign> (*this, at);
  1189. return *this;
  1190. }
  1191. template<class AT>
  1192. BOOST_UBLAS_INLINE
  1193. vector_indirect &operator /= (const AT &at) {
  1194. vector_assign_scalar<scalar_divides_assign> (*this, at);
  1195. return *this;
  1196. }
  1197. // Closure comparison
  1198. BOOST_UBLAS_INLINE
  1199. bool same_closure (const vector_indirect &vr) const {
  1200. return true;
  1201. }
  1202. // Comparison
  1203. BOOST_UBLAS_INLINE
  1204. bool operator == (const vector_indirect &vi) const {
  1205. return (*this).data_ == vi.data_ && ia_ == vi.ia_;
  1206. }
  1207. // Swapping
  1208. BOOST_UBLAS_INLINE
  1209. void swap (vector_indirect vi) {
  1210. if (this != &vi) {
  1211. BOOST_UBLAS_CHECK (size () == vi.size (), bad_size ());
  1212. // Sparse ranges may be nonconformant now.
  1213. // std::swap_ranges (begin (), end (), vi.begin ());
  1214. vector_swap<scalar_swap> (*this, vi);
  1215. }
  1216. }
  1217. BOOST_UBLAS_INLINE
  1218. friend void swap (vector_indirect vi1, vector_indirect vi2) {
  1219. vi1.swap (vi2);
  1220. }
  1221. // Iterator types
  1222. private:
  1223. // Use indirect array as an index - FIXME this fails for packed assignment
  1224. typedef typename IA::const_iterator const_subiterator_type;
  1225. typedef typename IA::const_iterator subiterator_type;
  1226. public:
  1227. #ifdef BOOST_UBLAS_USE_INDEXED_ITERATOR
  1228. typedef indexed_iterator<vector_indirect<vector_type, indirect_array_type>,
  1229. typename vector_type::iterator::iterator_category> iterator;
  1230. typedef indexed_const_iterator<vector_indirect<vector_type, indirect_array_type>,
  1231. typename vector_type::const_iterator::iterator_category> const_iterator;
  1232. #else
  1233. class const_iterator;
  1234. class iterator;
  1235. #endif
  1236. // Element lookup
  1237. BOOST_UBLAS_INLINE
  1238. const_iterator find (size_type i) const {
  1239. #ifdef BOOST_UBLAS_USE_INDEXED_ITERATOR
  1240. return const_iterator (*this, i);
  1241. #else
  1242. return const_iterator (*this, ia_.begin () + i);
  1243. #endif
  1244. }
  1245. BOOST_UBLAS_INLINE
  1246. iterator find (size_type i) {
  1247. #ifdef BOOST_UBLAS_USE_INDEXED_ITERATOR
  1248. return iterator (*this, i);
  1249. #else
  1250. return iterator (*this, ia_.begin () + i);
  1251. #endif
  1252. }
  1253. // Iterators simply are indices.
  1254. #ifndef BOOST_UBLAS_USE_INDEXED_ITERATOR
  1255. class const_iterator:
  1256. public container_const_reference<vector_indirect>,
  1257. public iterator_base_traits<typename V::const_iterator::iterator_category>::template
  1258. iterator_base<const_iterator, value_type>::type {
  1259. public:
  1260. typedef typename V::const_iterator::difference_type difference_type;
  1261. typedef typename V::const_iterator::value_type value_type;
  1262. typedef typename V::const_reference reference; //FIXME due to indexing access
  1263. typedef typename V::const_iterator::pointer pointer;
  1264. // Construction and destruction
  1265. BOOST_UBLAS_INLINE
  1266. const_iterator ():
  1267. container_const_reference<self_type> (), it_ () {}
  1268. BOOST_UBLAS_INLINE
  1269. const_iterator (const self_type &vi, const const_subiterator_type &it):
  1270. container_const_reference<self_type> (vi), it_ (it) {}
  1271. BOOST_UBLAS_INLINE
  1272. const_iterator (const typename self_type::iterator &it): // ISSUE self_type:: stops VC8 using std::iterator here
  1273. container_const_reference<self_type> (it ()), it_ (it.it_) {}
  1274. // Arithmetic
  1275. BOOST_UBLAS_INLINE
  1276. const_iterator &operator ++ () {
  1277. ++ it_;
  1278. return *this;
  1279. }
  1280. BOOST_UBLAS_INLINE
  1281. const_iterator &operator -- () {
  1282. -- it_;
  1283. return *this;
  1284. }
  1285. BOOST_UBLAS_INLINE
  1286. const_iterator &operator += (difference_type n) {
  1287. it_ += n;
  1288. return *this;
  1289. }
  1290. BOOST_UBLAS_INLINE
  1291. const_iterator &operator -= (difference_type n) {
  1292. it_ -= n;
  1293. return *this;
  1294. }
  1295. BOOST_UBLAS_INLINE
  1296. difference_type operator - (const const_iterator &it) const {
  1297. BOOST_UBLAS_CHECK ((*this) ().same_closure (it ()), external_logic ());
  1298. return it_ - it.it_;
  1299. }
  1300. // Dereference
  1301. BOOST_UBLAS_INLINE
  1302. const_reference operator * () const {
  1303. // FIXME replace find with at_element
  1304. BOOST_UBLAS_CHECK (index () < (*this) ().size (), bad_index ());
  1305. return (*this) ().data_ (*it_);
  1306. }
  1307. BOOST_UBLAS_INLINE
  1308. const_reference operator [] (difference_type n) const {
  1309. return *(*this + n);
  1310. }
  1311. // Index
  1312. BOOST_UBLAS_INLINE
  1313. size_type index () const {
  1314. return it_.index ();
  1315. }
  1316. // Assignment
  1317. BOOST_UBLAS_INLINE
  1318. const_iterator &operator = (const const_iterator &it) {
  1319. container_const_reference<self_type>::assign (&it ());
  1320. it_ = it.it_;
  1321. return *this;
  1322. }
  1323. // Comparison
  1324. BOOST_UBLAS_INLINE
  1325. bool operator == (const const_iterator &it) const {
  1326. BOOST_UBLAS_CHECK ((*this) ().same_closure (it ()), external_logic ());
  1327. return it_ == it.it_;
  1328. }
  1329. BOOST_UBLAS_INLINE
  1330. bool operator < (const const_iterator &it) const {
  1331. BOOST_UBLAS_CHECK ((*this) ().same_closure (it ()), external_logic ());
  1332. return it_ < it.it_;
  1333. }
  1334. private:
  1335. const_subiterator_type it_;
  1336. };
  1337. #endif
  1338. BOOST_UBLAS_INLINE
  1339. const_iterator begin () const {
  1340. return find (0);
  1341. }
  1342. BOOST_UBLAS_INLINE
  1343. const_iterator end () const {
  1344. return find (size ());
  1345. }
  1346. #ifndef BOOST_UBLAS_USE_INDEXED_ITERATOR
  1347. class iterator:
  1348. public container_reference<vector_indirect>,
  1349. public iterator_base_traits<typename V::iterator::iterator_category>::template
  1350. iterator_base<iterator, value_type>::type {
  1351. public:
  1352. typedef typename V::iterator::difference_type difference_type;
  1353. typedef typename V::iterator::value_type value_type;
  1354. typedef typename V::reference reference; //FIXME due to indexing access
  1355. typedef typename V::iterator::pointer pointer;
  1356. // Construction and destruction
  1357. BOOST_UBLAS_INLINE
  1358. iterator ():
  1359. container_reference<self_type> (), it_ () {}
  1360. BOOST_UBLAS_INLINE
  1361. iterator (self_type &vi, const subiterator_type &it):
  1362. container_reference<self_type> (vi), it_ (it) {}
  1363. // Arithmetic
  1364. BOOST_UBLAS_INLINE
  1365. iterator &operator ++ () {
  1366. ++ it_;
  1367. return *this;
  1368. }
  1369. BOOST_UBLAS_INLINE
  1370. iterator &operator -- () {
  1371. -- it_;
  1372. return *this;
  1373. }
  1374. BOOST_UBLAS_INLINE
  1375. iterator &operator += (difference_type n) {
  1376. it_ += n;
  1377. return *this;
  1378. }
  1379. BOOST_UBLAS_INLINE
  1380. iterator &operator -= (difference_type n) {
  1381. it_ -= n;
  1382. return *this;
  1383. }
  1384. BOOST_UBLAS_INLINE
  1385. difference_type operator - (const iterator &it) const {
  1386. BOOST_UBLAS_CHECK ((*this) ().same_closure (it ()), external_logic ());
  1387. return it_ - it.it_;
  1388. }
  1389. // Dereference
  1390. BOOST_UBLAS_INLINE
  1391. reference operator * () const {
  1392. // FIXME replace find with at_element
  1393. BOOST_UBLAS_CHECK (index () < (*this) ().size (), bad_index ());
  1394. return (*this) ().data_ (*it_);
  1395. }
  1396. BOOST_UBLAS_INLINE
  1397. reference operator [] (difference_type n) const {
  1398. return *(*this + n);
  1399. }
  1400. // Index
  1401. BOOST_UBLAS_INLINE
  1402. size_type index () const {
  1403. return it_.index ();
  1404. }
  1405. // Assignment
  1406. BOOST_UBLAS_INLINE
  1407. iterator &operator = (const iterator &it) {
  1408. container_reference<self_type>::assign (&it ());
  1409. it_ = it.it_;
  1410. return *this;
  1411. }
  1412. // Comparison
  1413. BOOST_UBLAS_INLINE
  1414. bool operator == (const iterator &it) const {
  1415. BOOST_UBLAS_CHECK ((*this) ().same_closure (it ()), external_logic ());
  1416. return it_ == it.it_;
  1417. }
  1418. BOOST_UBLAS_INLINE
  1419. bool operator < (const iterator &it) const {
  1420. BOOST_UBLAS_CHECK ((*this) ().same_closure (it ()), external_logic ());
  1421. return it_ < it.it_;
  1422. }
  1423. private:
  1424. subiterator_type it_;
  1425. friend class const_iterator;
  1426. };
  1427. #endif
  1428. BOOST_UBLAS_INLINE
  1429. iterator begin () {
  1430. return find (0);
  1431. }
  1432. BOOST_UBLAS_INLINE
  1433. iterator end () {
  1434. return find (size ());
  1435. }
  1436. // Reverse iterator
  1437. typedef reverse_iterator_base<const_iterator> const_reverse_iterator;
  1438. typedef reverse_iterator_base<iterator> reverse_iterator;
  1439. BOOST_UBLAS_INLINE
  1440. const_reverse_iterator rbegin () const {
  1441. return const_reverse_iterator (end ());
  1442. }
  1443. BOOST_UBLAS_INLINE
  1444. const_reverse_iterator rend () const {
  1445. return const_reverse_iterator (begin ());
  1446. }
  1447. BOOST_UBLAS_INLINE
  1448. reverse_iterator rbegin () {
  1449. return reverse_iterator (end ());
  1450. }
  1451. BOOST_UBLAS_INLINE
  1452. reverse_iterator rend () {
  1453. return reverse_iterator (begin ());
  1454. }
  1455. private:
  1456. vector_closure_type data_;
  1457. indirect_array_type ia_;
  1458. };
  1459. // Projections
  1460. template<class V, class A>
  1461. BOOST_UBLAS_INLINE
  1462. vector_indirect<V, indirect_array<A> > project (V &data, const indirect_array<A> &ia) {
  1463. return vector_indirect<V, indirect_array<A> > (data, ia);
  1464. }
  1465. template<class V, class A>
  1466. BOOST_UBLAS_INLINE
  1467. const vector_indirect<const V, indirect_array<A> > project (const V &data, const indirect_array<A> &ia) {
  1468. // ISSUE was: return vector_indirect<V, indirect_array<A> > (const_cast<V &> (data), ia)
  1469. return vector_indirect<const V, indirect_array<A> > (data, ia);
  1470. }
  1471. template<class V, class IA>
  1472. BOOST_UBLAS_INLINE
  1473. vector_indirect<V, IA> project (vector_indirect<V, IA> &data, const typename vector_indirect<V, IA>::range_type &r) {
  1474. return data.project (r);
  1475. }
  1476. template<class V, class IA>
  1477. BOOST_UBLAS_INLINE
  1478. const vector_indirect<V, IA> project (const vector_indirect<V, IA> &data, const typename vector_indirect<V, IA>::range_type &r) {
  1479. return data.project (r);
  1480. }
  1481. template<class V, class IA>
  1482. BOOST_UBLAS_INLINE
  1483. vector_indirect<V, IA> project (vector_indirect<V, IA> &data, const typename vector_indirect<V, IA>::slice_type &s) {
  1484. return data.project (s);
  1485. }
  1486. template<class V, class IA>
  1487. BOOST_UBLAS_INLINE
  1488. const vector_indirect<V, IA> project (const vector_indirect<V, IA> &data, const typename vector_indirect<V, IA>::slice_type &s) {
  1489. return data.project (s);
  1490. }
  1491. template<class V, class A>
  1492. BOOST_UBLAS_INLINE
  1493. vector_indirect<V, indirect_array<A> > project (vector_indirect<V, indirect_array<A> > &data, const indirect_array<A> &ia) {
  1494. return data.project (ia);
  1495. }
  1496. template<class V, class A>
  1497. BOOST_UBLAS_INLINE
  1498. const vector_indirect<V, indirect_array<A> > project (const vector_indirect<V, indirect_array<A> > &data, const indirect_array<A> &ia) {
  1499. return data.project (ia);
  1500. }
  1501. // Specialization of temporary_traits
  1502. template <class V>
  1503. struct vector_temporary_traits< vector_indirect<V> >
  1504. : vector_temporary_traits< V > {} ;
  1505. template <class V>
  1506. struct vector_temporary_traits< const vector_indirect<V> >
  1507. : vector_temporary_traits< V > {} ;
  1508. }}}
  1509. #endif