You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

969 lines
37 KiB

  1. /* -*- c++ -*- (enables emacs c++ mode) */
  2. /*===========================================================================
  3. Copyright (C) 2002-2012 Yves Renard
  4. This file is a part of GETFEM++
  5. Getfem++ is free software; you can redistribute it and/or modify it
  6. under the terms of the GNU Lesser General Public License as published
  7. by the Free Software Foundation; either version 3 of the License, or
  8. (at your option) any later version along with the GCC Runtime Library
  9. Exception either version 3.1 or (at your option) any later version.
  10. This program is distributed in the hope that it will be useful, but
  11. WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  12. or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
  13. License and GCC Runtime Library Exception for more details.
  14. You should have received a copy of the GNU Lesser General Public License
  15. along with this program; if not, write to the Free Software Foundation,
  16. Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
  17. As a special exception, you may use this file as it is a part of a free
  18. software library without restriction. Specifically, if other files
  19. instantiate templates or use macros or inline functions from this file,
  20. or you compile this file and link it with other files to produce an
  21. executable, this file does not by itself cause the resulting executable
  22. to be covered by the GNU Lesser General Public License. This exception
  23. does not however invalidate any other reasons why the executable file
  24. might be covered by the GNU Lesser General Public License.
  25. ===========================================================================*/
  26. /**@file gmm_vector.h
  27. @author Yves Renard <Yves.Renard@insa-lyon.fr>
  28. @date October 13, 2002.
  29. @brief Declaration of the vector types (gmm::rsvector, gmm::wsvector,
  30. gmm::slvector ,..)
  31. */
  32. #ifndef GMM_VECTOR_H__
  33. #define GMM_VECTOR_H__
  34. #include <map>
  35. #include "gmm_interface.h"
  36. namespace gmm {
  37. /*************************************************************************/
  38. /* */
  39. /* Class ref_elt_vector: reference on a vector component. */
  40. /* */
  41. /*************************************************************************/
  42. template<typename T, typename V> class ref_elt_vector {
  43. V *pm;
  44. size_type l;
  45. public :
  46. operator T() const { return pm->r(l); }
  47. ref_elt_vector(V *p, size_type ll) : pm(p), l(ll) {}
  48. inline ref_elt_vector &operator =(T v)
  49. { (*pm).w(l,v); return *this; }
  50. inline bool operator ==(T v) const { return ((*pm).r(l) == v); }
  51. inline bool operator !=(T v) const { return ((*pm).r(l) != v); }
  52. inline ref_elt_vector &operator +=(T v)
  53. { (*pm).w(l,(*pm).r(l) + v); return *this; }
  54. inline ref_elt_vector &operator -=(T v)
  55. { (*pm).w(l,(*pm).r(l) - v); return *this; }
  56. inline ref_elt_vector &operator /=(T v)
  57. { (*pm).w(l,(*pm).r(l) / v); return *this; }
  58. inline ref_elt_vector &operator *=(T v)
  59. { (*pm).w(l,(*pm).r(l) * v); return *this; }
  60. inline ref_elt_vector &operator =(const ref_elt_vector &re)
  61. { *this = T(re); return *this; }
  62. T operator +() { return T(*this); } // necessary for unknow reason
  63. T operator -() { return -T(*this); } // necessary for unknow reason
  64. T operator +(T v) { return T(*this)+ v; } // necessary for unknow reason
  65. T operator -(T v) { return T(*this)- v; } // necessary for unknow reason
  66. T operator *(T v) { return T(*this)* v; } // necessary for unknow reason
  67. T operator /(T v) { return T(*this)/ v; } // necessary for unknow reason
  68. };
  69. template<typename T, typename V> inline
  70. bool operator ==(T v, const ref_elt_vector<T, V> &re) { return (v==T(re)); }
  71. template<typename T, typename V> inline
  72. bool operator !=(T v, const ref_elt_vector<T, V> &re) { return (v!=T(re)); }
  73. template<typename T, typename V> inline
  74. T &operator +=(T &v, const ref_elt_vector<T, V> &re)
  75. { v += T(re); return v; }
  76. template<typename T, typename V> inline
  77. T &operator -=(T &v, const ref_elt_vector<T, V> &re)
  78. { v -= T(re); return v; }
  79. template<typename T, typename V> inline
  80. T &operator *=(T &v, const ref_elt_vector<T, V> &re)
  81. { v *= T(re); return v; }
  82. template<typename T, typename V> inline
  83. T &operator /=(T &v, const ref_elt_vector<T, V> &re)
  84. { v /= T(re); return v; }
  85. template<typename T, typename V> inline
  86. T operator +(const ref_elt_vector<T, V> &re) { return T(re); }
  87. template<typename T, typename V> inline
  88. T operator -(const ref_elt_vector<T, V> &re) { return -T(re); }
  89. template<typename T, typename V> inline
  90. T operator +(const ref_elt_vector<T, V> &re, T v) { return T(re)+ v; }
  91. template<typename T, typename V> inline
  92. T operator +(T v, const ref_elt_vector<T, V> &re) { return v+ T(re); }
  93. template<typename T, typename V> inline
  94. T operator -(const ref_elt_vector<T, V> &re, T v) { return T(re)- v; }
  95. template<typename T, typename V> inline
  96. T operator -(T v, const ref_elt_vector<T, V> &re) { return v- T(re); }
  97. template<typename T, typename V> inline
  98. T operator *(const ref_elt_vector<T, V> &re, T v) { return T(re)* v; }
  99. template<typename T, typename V> inline
  100. T operator *(T v, const ref_elt_vector<T, V> &re) { return v* T(re); }
  101. template<typename T, typename V> inline
  102. T operator /(const ref_elt_vector<T, V> &re, T v) { return T(re)/ v; }
  103. template<typename T, typename V> inline
  104. T operator /(T v, const ref_elt_vector<T, V> &re) { return v/ T(re); }
  105. template<typename T, typename V> inline
  106. typename number_traits<T>::magnitude_type
  107. abs(const ref_elt_vector<T, V> &re) { return gmm::abs(T(re)); }
  108. template<typename T, typename V> inline
  109. T sqr(const ref_elt_vector<T, V> &re) { return gmm::sqr(T(re)); }
  110. template<typename T, typename V> inline
  111. typename number_traits<T>::magnitude_type
  112. abs_sqr(const ref_elt_vector<T, V> &re) { return gmm::abs_sqr(T(re)); }
  113. template<typename T, typename V> inline
  114. T conj(const ref_elt_vector<T, V> &re) { return gmm::conj(T(re)); }
  115. template<typename T, typename V> std::ostream &operator <<
  116. (std::ostream &o, const ref_elt_vector<T, V> &re) { o << T(re); return o; }
  117. template<typename T, typename V> inline
  118. typename number_traits<T>::magnitude_type
  119. real(const ref_elt_vector<T, V> &re) { return gmm::real(T(re)); }
  120. template<typename T, typename V> inline
  121. typename number_traits<T>::magnitude_type
  122. imag(const ref_elt_vector<T, V> &re) { return gmm::imag(T(re)); }
  123. /*************************************************************************/
  124. /* */
  125. /* Class wsvector: sparse vector optimized for random write operations. */
  126. /* */
  127. /*************************************************************************/
  128. template<typename T> struct wsvector_iterator
  129. : public std::map<size_type, T>::iterator {
  130. typedef typename std::map<size_type, T>::iterator base_it_type;
  131. typedef T value_type;
  132. typedef value_type* pointer;
  133. typedef value_type& reference;
  134. // typedef size_t size_type;
  135. typedef ptrdiff_t difference_type;
  136. typedef std::bidirectional_iterator_tag iterator_category;
  137. reference operator *() const { return (base_it_type::operator*()).second; }
  138. pointer operator->() const { return &(operator*()); }
  139. size_type index(void) const { return (base_it_type::operator*()).first; }
  140. wsvector_iterator(void) {}
  141. wsvector_iterator(const base_it_type &it) : base_it_type(it) {}
  142. };
  143. template<typename T> struct wsvector_const_iterator
  144. : public std::map<size_type, T>::const_iterator {
  145. typedef typename std::map<size_type, T>::const_iterator base_it_type;
  146. typedef T value_type;
  147. typedef const value_type* pointer;
  148. typedef const value_type& reference;
  149. // typedef size_t size_type;
  150. typedef ptrdiff_t difference_type;
  151. typedef std::bidirectional_iterator_tag iterator_category;
  152. reference operator *() const { return (base_it_type::operator*()).second; }
  153. pointer operator->() const { return &(operator*()); }
  154. size_type index(void) const { return (base_it_type::operator*()).first; }
  155. wsvector_const_iterator(void) {}
  156. wsvector_const_iterator(const wsvector_iterator<T> &it)
  157. : base_it_type(it) {}
  158. wsvector_const_iterator(const base_it_type &it) : base_it_type(it) {}
  159. };
  160. /**
  161. sparse vector built upon std::map.
  162. Read and write access are quite fast (log n)
  163. */
  164. template<typename T> class wsvector : public std::map<size_type, T> {
  165. public:
  166. typedef typename std::map<int, T>::size_type size_type;
  167. typedef std::map<size_type, T> base_type;
  168. typedef typename base_type::iterator iterator;
  169. typedef typename base_type::const_iterator const_iterator;
  170. protected:
  171. size_type nbl;
  172. public:
  173. void clean(double eps);
  174. void resize(size_type);
  175. inline ref_elt_vector<T, wsvector<T> > operator [](size_type c)
  176. { return ref_elt_vector<T, wsvector<T> >(this, c); }
  177. inline void w(size_type c, const T &e) {
  178. GMM_ASSERT2(c < nbl, "out of range");
  179. if (e == T(0)) { base_type::erase(c); }
  180. else base_type::operator [](c) = e;
  181. }
  182. inline T r(size_type c) const {
  183. GMM_ASSERT2(c < nbl, "out of range");
  184. const_iterator it = this->lower_bound(c);
  185. if (it != this->end() && c == it->first) return it->second;
  186. else return T(0);
  187. }
  188. inline T operator [](size_type c) const { return r(c); }
  189. size_type nb_stored(void) const { return base_type::size(); }
  190. size_type size(void) const { return nbl; }
  191. void swap(wsvector<T> &v)
  192. { std::swap(nbl, v.nbl); std::map<size_type, T>::swap(v); }
  193. /* Constructeurs */
  194. void init(size_type l) { nbl = l; this->clear(); }
  195. explicit wsvector(size_type l){ init(l); }
  196. wsvector(void) { init(0); }
  197. };
  198. template<typename T> void wsvector<T>::clean(double eps) {
  199. iterator it = this->begin(), itf = it, ite = this->end();
  200. while (it != ite) {
  201. ++itf; if (gmm::abs(it->second) <= eps) erase(it); it = itf;
  202. }
  203. }
  204. template<typename T> void wsvector<T>::resize(size_type n) {
  205. if (n < nbl) {
  206. iterator it = this->begin(), itf = it, ite = this->end();
  207. while (it != ite) { ++itf; if (it->first >= n) this->erase(it); it=itf; }
  208. }
  209. nbl = n;
  210. }
  211. template <typename T> struct linalg_traits<wsvector<T> > {
  212. typedef wsvector<T> this_type;
  213. typedef this_type origin_type;
  214. typedef linalg_false is_reference;
  215. typedef abstract_vector linalg_type;
  216. typedef T value_type;
  217. typedef ref_elt_vector<T, wsvector<T> > reference;
  218. typedef wsvector_iterator<T> iterator;
  219. typedef wsvector_const_iterator<T> const_iterator;
  220. typedef abstract_sparse storage_type;
  221. typedef linalg_true index_sorted;
  222. static size_type size(const this_type &v) { return v.size(); }
  223. static iterator begin(this_type &v) { return v.begin(); }
  224. static const_iterator begin(const this_type &v) { return v.begin(); }
  225. static iterator end(this_type &v) { return v.end(); }
  226. static const_iterator end(const this_type &v) { return v.end(); }
  227. static origin_type* origin(this_type &v) { return &v; }
  228. static const origin_type* origin(const this_type &v) { return &v; }
  229. static void clear(origin_type* o, const iterator &, const iterator &)
  230. { o->clear(); }
  231. static void do_clear(this_type &v) { v.clear(); }
  232. static value_type access(const origin_type *o, const const_iterator &,
  233. const const_iterator &, size_type i)
  234. { return (*o)[i]; }
  235. static reference access(origin_type *o, const iterator &, const iterator &,
  236. size_type i)
  237. { return (*o)[i]; }
  238. static void resize(this_type &v, size_type n) { v.resize(n); }
  239. };
  240. template<typename T> std::ostream &operator <<
  241. (std::ostream &o, const wsvector<T>& v) { gmm::write(o,v); return o; }
  242. /******* Optimized BLAS for wsvector<T> **********************************/
  243. template <typename T> inline void copy(const wsvector<T> &v1,
  244. wsvector<T> &v2) {
  245. GMM_ASSERT2(vect_size(v1) == vect_size(v2), "dimensions mismatch");
  246. v2 = v1;
  247. }
  248. template <typename T> inline
  249. void copy(const wsvector<T> &v1, const simple_vector_ref<wsvector<T> *> &v2){
  250. simple_vector_ref<wsvector<T> *>
  251. *svr = const_cast<simple_vector_ref<wsvector<T> *> *>(&v2);
  252. wsvector<T>
  253. *pv = const_cast<wsvector<T> *>(v2.origin);
  254. GMM_ASSERT2(vect_size(v1) == vect_size(v2), "dimensions mismatch");
  255. *pv = v1; svr->begin_ = vect_begin(*pv); svr->end_ = vect_end(*pv);
  256. }
  257. template <typename T> inline
  258. void copy(const simple_vector_ref<const wsvector<T> *> &v1,
  259. wsvector<T> &v2)
  260. { copy(*(v1.origin), v2); }
  261. template <typename T> inline
  262. void copy(const simple_vector_ref<wsvector<T> *> &v1, wsvector<T> &v2)
  263. { copy(*(v1.origin), v2); }
  264. template <typename T> inline void clean(wsvector<T> &v, double eps) {
  265. typedef typename number_traits<T>::magnitude_type R;
  266. typename wsvector<T>::iterator it = v.begin(), ite = v.end(), itc;
  267. while (it != ite)
  268. if (gmm::abs((*it).second) <= R(eps))
  269. { itc=it; ++it; v.erase(itc); } else ++it;
  270. }
  271. template <typename T>
  272. inline void clean(const simple_vector_ref<wsvector<T> *> &l, double eps) {
  273. simple_vector_ref<wsvector<T> *>
  274. *svr = const_cast<simple_vector_ref<wsvector<T> *> *>(&l);
  275. wsvector<T>
  276. *pv = const_cast<wsvector<T> *>((l.origin));
  277. clean(*pv, eps);
  278. svr->begin_ = vect_begin(*pv); svr->end_ = vect_end(*pv);
  279. }
  280. template <typename T>
  281. inline size_type nnz(const wsvector<T>& l) { return l.nb_stored(); }
  282. /*************************************************************************/
  283. /* */
  284. /* rsvector: sparse vector optimized for linear algebra operations. */
  285. /* */
  286. /*************************************************************************/
  287. template<typename T> struct elt_rsvector_ {
  288. size_type c; T e;
  289. /* e is initialized by default to avoid some false warnings of valgrind..
  290. (from http://valgrind.org/docs/manual/mc-manual.html:
  291. When memory is read into the CPU's floating point registers, the
  292. relevant V bits are read from memory and they are immediately
  293. checked. If any are invalid, an uninitialised value error is
  294. emitted. This precludes using the floating-point registers to copy
  295. possibly-uninitialised memory, but simplifies Valgrind in that it
  296. does not have to track the validity status of the floating-point
  297. registers.
  298. */
  299. elt_rsvector_(void) : e(0) {}
  300. elt_rsvector_(size_type cc) : c(cc), e(0) {}
  301. elt_rsvector_(size_type cc, const T &ee) : c(cc), e(ee) {}
  302. bool operator < (const elt_rsvector_ &a) const { return c < a.c; }
  303. bool operator == (const elt_rsvector_ &a) const { return c == a.c; }
  304. bool operator != (const elt_rsvector_ &a) const { return c != a.c; }
  305. };
  306. template<typename T> struct rsvector_iterator {
  307. typedef typename std::vector<elt_rsvector_<T> >::iterator IT;
  308. typedef T value_type;
  309. typedef value_type* pointer;
  310. typedef value_type& reference;
  311. typedef size_t size_type;
  312. typedef ptrdiff_t difference_type;
  313. typedef std::bidirectional_iterator_tag iterator_category;
  314. typedef rsvector_iterator<T> iterator;
  315. IT it;
  316. reference operator *() const { return it->e; }
  317. pointer operator->() const { return &(operator*()); }
  318. iterator &operator ++() { ++it; return *this; }
  319. iterator operator ++(int) { iterator tmp = *this; ++(*this); return tmp; }
  320. iterator &operator --() { --it; return *this; }
  321. iterator operator --(int) { iterator tmp = *this; --(*this); return tmp; }
  322. bool operator ==(const iterator &i) const { return it == i.it; }
  323. bool operator !=(const iterator &i) const { return !(i == *this); }
  324. size_type index(void) const { return it->c; }
  325. rsvector_iterator(void) {}
  326. rsvector_iterator(const IT &i) : it(i) {}
  327. };
  328. template<typename T> struct rsvector_const_iterator {
  329. typedef typename std::vector<elt_rsvector_<T> >::const_iterator IT;
  330. typedef T value_type;
  331. typedef const value_type* pointer;
  332. typedef const value_type& reference;
  333. typedef size_t size_type;
  334. typedef ptrdiff_t difference_type;
  335. typedef std::forward_iterator_tag iterator_category;
  336. typedef rsvector_const_iterator<T> iterator;
  337. IT it;
  338. reference operator *() const { return it->e; }
  339. pointer operator->() const { return &(operator*()); }
  340. size_type index(void) const { return it->c; }
  341. iterator &operator ++() { ++it; return *this; }
  342. iterator operator ++(int) { iterator tmp = *this; ++(*this); return tmp; }
  343. iterator &operator --() { --it; return *this; }
  344. iterator operator --(int) { iterator tmp = *this; --(*this); return tmp; }
  345. bool operator ==(const iterator &i) const { return it == i.it; }
  346. bool operator !=(const iterator &i) const { return !(i == *this); }
  347. rsvector_const_iterator(void) {}
  348. rsvector_const_iterator(const rsvector_iterator<T> &i) : it(i.it) {}
  349. rsvector_const_iterator(const IT &i) : it(i) {}
  350. };
  351. /**
  352. sparse vector built upon std::vector. Read access is fast,
  353. but insertion is O(n)
  354. */
  355. template<typename T> class rsvector : public std::vector<elt_rsvector_<T> > {
  356. public:
  357. typedef std::vector<elt_rsvector_<T> > base_type_;
  358. typedef typename base_type_::iterator iterator;
  359. typedef typename base_type_::const_iterator const_iterator;
  360. typedef typename base_type_::size_type size_type;
  361. typedef T value_type;
  362. protected:
  363. size_type nbl; /* size of the vector. */
  364. public:
  365. void sup(size_type j);
  366. void base_resize(size_type n) { base_type_::resize(n); }
  367. void resize(size_type);
  368. ref_elt_vector<T, rsvector<T> > operator [](size_type c)
  369. { return ref_elt_vector<T, rsvector<T> >(this, c); }
  370. void w(size_type c, const T &e);
  371. T r(size_type c) const;
  372. void swap_indices(size_type i, size_type j);
  373. inline T operator [](size_type c) const { return r(c); }
  374. size_type nb_stored(void) const { return base_type_::size(); }
  375. size_type size(void) const { return nbl; }
  376. void clear(void) { base_type_::resize(0); }
  377. void swap(rsvector<T> &v)
  378. { std::swap(nbl, v.nbl); std::vector<elt_rsvector_<T> >::swap(v); }
  379. /* Constructeurs */
  380. explicit rsvector(size_type l) : nbl(l) { }
  381. rsvector(void) : nbl(0) { }
  382. };
  383. template <typename T>
  384. void rsvector<T>::swap_indices(size_type i, size_type j) {
  385. if (i > j) std::swap(i, j);
  386. if (i != j) {
  387. int situation = 0;
  388. elt_rsvector_<T> ei(i), ej(j), a;
  389. iterator it, ite, iti, itj;
  390. iti = std::lower_bound(this->begin(), this->end(), ei);
  391. if (iti != this->end() && iti->c == i) situation += 1;
  392. itj = std::lower_bound(this->begin(), this->end(), ej);
  393. if (itj != this->end() && itj->c == j) situation += 2;
  394. switch (situation) {
  395. case 1 : a = *iti; a.c = j; it = iti; ++it; ite = this->end();
  396. for (; it != ite && it->c <= j; ++it, ++iti) *iti = *it;
  397. *iti = a;
  398. break;
  399. case 2 : a = *itj; a.c = i; it = itj; ite = this->begin();
  400. if (it != ite) {
  401. --it;
  402. while (it->c >= i) { *itj = *it; --itj; if (it==ite) break; --it; }
  403. }
  404. *itj = a;
  405. break;
  406. case 3 : std::swap(iti->e, itj->e);
  407. break;
  408. }
  409. }
  410. }
  411. template <typename T> void rsvector<T>::sup(size_type j) {
  412. if (nb_stored() != 0) {
  413. elt_rsvector_<T> ev(j);
  414. iterator it = std::lower_bound(this->begin(), this->end(), ev);
  415. if (it != this->end() && it->c == j) {
  416. for (iterator ite = this->end() - 1; it != ite; ++it) *it = *(it+1);
  417. base_type_::resize(nb_stored()-1);
  418. }
  419. }
  420. }
  421. template<typename T> void rsvector<T>::resize(size_type n) {
  422. if (n < nbl) {
  423. for (size_type i = 0; i < nb_stored(); ++i)
  424. if (base_type_::operator[](i).c >= n) { base_resize(i); break; }
  425. }
  426. nbl = n;
  427. }
  428. template <typename T> void rsvector<T>::w(size_type c, const T &e) {
  429. GMM_ASSERT2(c < nbl, "out of range");
  430. if (e == T(0)) sup(c);
  431. else {
  432. elt_rsvector_<T> ev(c, e);
  433. if (nb_stored() == 0) {
  434. base_type_::resize(1,ev);
  435. }
  436. else {
  437. iterator it = std::lower_bound(this->begin(), this->end(), ev);
  438. if (it != this->end() && it->c == c) it->e = e;
  439. else {
  440. size_type ind = it - this->begin();
  441. base_type_::resize(nb_stored()+1, ev);
  442. if (ind != nb_stored() - 1) {
  443. it = this->begin() + ind;
  444. for (iterator ite = this->end() - 1; ite != it; --ite)
  445. *ite = *(ite-1);
  446. *it = ev;
  447. }
  448. }
  449. }
  450. }
  451. }
  452. template <typename T> T rsvector<T>::r(size_type c) const {
  453. GMM_ASSERT2(c < nbl, "out of range");
  454. if (nb_stored() != 0) {
  455. elt_rsvector_<T> ev(c);
  456. const_iterator it = std::lower_bound(this->begin(), this->end(), ev);
  457. if (it != this->end() && it->c == c) return it->e;
  458. }
  459. return T(0);
  460. }
  461. template <typename T> struct linalg_traits<rsvector<T> > {
  462. typedef rsvector<T> this_type;
  463. typedef this_type origin_type;
  464. typedef linalg_false is_reference;
  465. typedef abstract_vector linalg_type;
  466. typedef T value_type;
  467. typedef ref_elt_vector<T, rsvector<T> > reference;
  468. typedef rsvector_iterator<T> iterator;
  469. typedef rsvector_const_iterator<T> const_iterator;
  470. typedef abstract_sparse storage_type;
  471. typedef linalg_true index_sorted;
  472. static size_type size(const this_type &v) { return v.size(); }
  473. static iterator begin(this_type &v) { return iterator(v.begin()); }
  474. static const_iterator begin(const this_type &v)
  475. { return const_iterator(v.begin()); }
  476. static iterator end(this_type &v) { return iterator(v.end()); }
  477. static const_iterator end(const this_type &v)
  478. { return const_iterator(v.end()); }
  479. static origin_type* origin(this_type &v) { return &v; }
  480. static const origin_type* origin(const this_type &v) { return &v; }
  481. static void clear(origin_type* o, const iterator &, const iterator &)
  482. { o->clear(); }
  483. static void do_clear(this_type &v) { v.clear(); }
  484. static value_type access(const origin_type *o, const const_iterator &,
  485. const const_iterator &, size_type i)
  486. { return (*o)[i]; }
  487. static reference access(origin_type *o, const iterator &, const iterator &,
  488. size_type i)
  489. { return (*o)[i]; }
  490. static void resize(this_type &v, size_type n) { v.resize(n); }
  491. };
  492. template<typename T> std::ostream &operator <<
  493. (std::ostream &o, const rsvector<T>& v) { gmm::write(o,v); return o; }
  494. /******* Optimized operations for rsvector<T> ****************************/
  495. template <typename T> inline void copy(const rsvector<T> &v1,
  496. rsvector<T> &v2) {
  497. GMM_ASSERT2(vect_size(v1) == vect_size(v2), "dimensions mismatch");
  498. v2 = v1;
  499. }
  500. template <typename T> inline
  501. void copy(const rsvector<T> &v1, const simple_vector_ref<rsvector<T> *> &v2){
  502. simple_vector_ref<rsvector<T> *>
  503. *svr = const_cast<simple_vector_ref<rsvector<T> *> *>(&v2);
  504. rsvector<T>
  505. *pv = const_cast<rsvector<T> *>((v2.origin));
  506. GMM_ASSERT2(vect_size(v1) == vect_size(v2), "dimensions mismatch");
  507. *pv = v1; svr->begin_ = vect_begin(*pv); svr->end_ = vect_end(*pv);
  508. }
  509. template <typename T> inline
  510. void copy(const simple_vector_ref<const rsvector<T> *> &v1,
  511. rsvector<T> &v2)
  512. { copy(*(v1.origin), v2); }
  513. template <typename T> inline
  514. void copy(const simple_vector_ref<rsvector<T> *> &v1, rsvector<T> &v2)
  515. { copy(*(v1.origin), v2); }
  516. template <typename V, typename T> inline void add(const V &v1,
  517. rsvector<T> &v2) {
  518. if ((const void *)(&v1) != (const void *)(&v2)) {
  519. GMM_ASSERT2(vect_size(v1) == vect_size(v2), "dimensions mismatch");
  520. add_rsvector(v1, v2, typename linalg_traits<V>::storage_type());
  521. }
  522. }
  523. template <typename V, typename T>
  524. inline void add_rsvector(const V &v1, rsvector<T> &v2, abstract_dense)
  525. { add(v1, v2, abstract_dense(), abstract_sparse()); }
  526. template <typename V, typename T>
  527. inline void add_rsvector(const V &v1, rsvector<T> &v2, abstract_skyline)
  528. { add(v1, v2, abstract_skyline(), abstract_sparse()); }
  529. template <typename V, typename T>
  530. void add_rsvector(const V &v1, rsvector<T> &v2, abstract_sparse) {
  531. add_rsvector(v1, v2, typename linalg_traits<V>::index_sorted());
  532. }
  533. template <typename V, typename T>
  534. void add_rsvector(const V &v1, rsvector<T> &v2, linalg_false) {
  535. add(v1, v2, abstract_sparse(), abstract_sparse());
  536. }
  537. template <typename V, typename T>
  538. void add_rsvector(const V &v1, rsvector<T> &v2, linalg_true) {
  539. typename linalg_traits<V>::const_iterator it1 = vect_const_begin(v1),
  540. ite1 = vect_const_end(v1);
  541. typename rsvector<T>::iterator it2 = v2.begin(), ite2 = v2.end(), it3;
  542. size_type nbc = 0, old_nbc = v2.nb_stored();
  543. for (; it1 != ite1 && it2 != ite2 ; ++nbc)
  544. if (it1.index() == it2->c) { ++it1; ++it2; }
  545. else if (it1.index() < it2->c) ++it1; else ++it2;
  546. for (; it1 != ite1; ++it1) ++nbc;
  547. for (; it2 != ite2; ++it2) ++nbc;
  548. v2.base_resize(nbc);
  549. it3 = v2.begin() + old_nbc;
  550. it2 = v2.end(); ite2 = v2.begin();
  551. it1 = vect_end(v1); ite1 = vect_const_begin(v1);
  552. while (it1 != ite1 && it3 != ite2) {
  553. --it3; --it1; --it2;
  554. if (it3->c > it1.index()) { *it2 = *it3; ++it1; }
  555. else if (it3->c == it1.index()) { *it2=*it3; it2->e+=*it1; }
  556. else { it2->c = it1.index(); it2->e = *it1; ++it3; }
  557. }
  558. while (it1 != ite1) { --it1; --it2; it2->c = it1.index(); it2->e = *it1; }
  559. }
  560. template <typename V, typename T> void copy(const V &v1, rsvector<T> &v2) {
  561. if ((const void *)(&v1) != (const void *)(&v2)) {
  562. GMM_ASSERT2(vect_size(v1) == vect_size(v2), "dimensions mismatch");
  563. if (same_origin(v1, v2))
  564. GMM_WARNING2("a conflict is possible in vector copy\n");
  565. copy_rsvector(v1, v2, typename linalg_traits<V>::storage_type());
  566. }
  567. }
  568. template <typename V, typename T>
  569. void copy_rsvector(const V &v1, rsvector<T> &v2, abstract_dense)
  570. { copy_vect(v1, v2, abstract_dense(), abstract_sparse()); }
  571. template <typename V, typename T>
  572. void copy_rsvector(const V &v1, rsvector<T> &v2, abstract_skyline)
  573. { copy_vect(v1, v2, abstract_skyline(), abstract_sparse()); }
  574. template <typename V, typename T>
  575. void copy_rsvector(const V &v1, rsvector<T> &v2, abstract_sparse) {
  576. copy_rsvector(v1, v2, typename linalg_traits<V>::index_sorted());
  577. }
  578. template <typename V, typename T2>
  579. void copy_rsvector(const V &v1, rsvector<T2> &v2, linalg_true) {
  580. typedef typename linalg_traits<V>::value_type T1;
  581. typename linalg_traits<V>::const_iterator it = vect_const_begin(v1),
  582. ite = vect_const_end(v1);
  583. v2.base_resize(nnz(v1));
  584. typename rsvector<T2>::iterator it2 = v2.begin();
  585. size_type nn = 0;
  586. for (; it != ite; ++it)
  587. if ((*it) != T1(0)) { it2->c = it.index(); it2->e = *it; ++it2; ++nn; }
  588. v2.base_resize(nn);
  589. }
  590. template <typename V, typename T2>
  591. void copy_rsvector(const V &v1, rsvector<T2> &v2, linalg_false) {
  592. typedef typename linalg_traits<V>::value_type T1;
  593. typename linalg_traits<V>::const_iterator it = vect_const_begin(v1),
  594. ite = vect_const_end(v1);
  595. v2.base_resize(nnz(v1));
  596. typename rsvector<T2>::iterator it2 = v2.begin();
  597. size_type nn = 0;
  598. for (; it != ite; ++it)
  599. if ((*it) != T1(0)) { it2->c = it.index(); it2->e = *it; ++it2; ++nn; }
  600. v2.base_resize(nn);
  601. std::sort(v2.begin(), v2.end());
  602. }
  603. template <typename T> inline void clean(rsvector<T> &v, double eps) {
  604. typedef typename number_traits<T>::magnitude_type R;
  605. typename rsvector<T>::iterator it = v.begin(), ite = v.end();
  606. for (; it != ite; ++it) if (gmm::abs((*it).e) <= eps) break;
  607. if (it != ite) {
  608. typename rsvector<T>::iterator itc = it;
  609. size_type erased = 1;
  610. for (++it; it != ite; ++it)
  611. { *itc = *it; if (gmm::abs((*it).e) <= R(eps)) ++erased; else ++itc; }
  612. v.base_resize(v.nb_stored() - erased);
  613. }
  614. }
  615. template <typename T>
  616. inline void clean(const simple_vector_ref<rsvector<T> *> &l, double eps) {
  617. simple_vector_ref<rsvector<T> *>
  618. *svr = const_cast<simple_vector_ref<rsvector<T> *> *>(&l);
  619. rsvector<T>
  620. *pv = const_cast<rsvector<T> *>((l.origin));
  621. clean(*pv, eps);
  622. svr->begin_ = vect_begin(*pv); svr->end_ = vect_end(*pv);
  623. }
  624. template <typename T>
  625. inline size_type nnz(const rsvector<T>& l) { return l.nb_stored(); }
  626. /*************************************************************************/
  627. /* */
  628. /* Class slvector: 'sky-line' vector. */
  629. /* */
  630. /*************************************************************************/
  631. template<typename T> struct slvector_iterator {
  632. typedef T value_type;
  633. typedef T *pointer;
  634. typedef T &reference;
  635. typedef ptrdiff_t difference_type;
  636. typedef std::random_access_iterator_tag iterator_category;
  637. typedef size_t size_type;
  638. typedef slvector_iterator<T> iterator;
  639. typedef typename std::vector<T>::iterator base_iterator;
  640. base_iterator it;
  641. size_type shift;
  642. iterator &operator ++()
  643. { ++it; ++shift; return *this; }
  644. iterator &operator --()
  645. { --it; --shift; return *this; }
  646. iterator operator ++(int)
  647. { iterator tmp = *this; ++(*(this)); return tmp; }
  648. iterator operator --(int)
  649. { iterator tmp = *this; --(*(this)); return tmp; }
  650. iterator &operator +=(difference_type i)
  651. { it += i; shift += i; return *this; }
  652. iterator &operator -=(difference_type i)
  653. { it -= i; shift -= i; return *this; }
  654. iterator operator +(difference_type i) const
  655. { iterator tmp = *this; return (tmp += i); }
  656. iterator operator -(difference_type i) const
  657. { iterator tmp = *this; return (tmp -= i); }
  658. difference_type operator -(const iterator &i) const
  659. { return it - i.it; }
  660. reference operator *() const
  661. { return *it; }
  662. reference operator [](int ii)
  663. { return *(it + ii); }
  664. bool operator ==(const iterator &i) const
  665. { return it == i.it; }
  666. bool operator !=(const iterator &i) const
  667. { return !(i == *this); }
  668. bool operator < (const iterator &i) const
  669. { return it < i.it; }
  670. size_type index(void) const { return shift; }
  671. slvector_iterator(void) {}
  672. slvector_iterator(const base_iterator &iter, size_type s)
  673. : it(iter), shift(s) {}
  674. };
  675. template<typename T> struct slvector_const_iterator {
  676. typedef T value_type;
  677. typedef const T *pointer;
  678. typedef value_type reference;
  679. typedef ptrdiff_t difference_type;
  680. typedef std::random_access_iterator_tag iterator_category;
  681. typedef size_t size_type;
  682. typedef slvector_const_iterator<T> iterator;
  683. typedef typename std::vector<T>::const_iterator base_iterator;
  684. base_iterator it;
  685. size_type shift;
  686. iterator &operator ++()
  687. { ++it; ++shift; return *this; }
  688. iterator &operator --()
  689. { --it; --shift; return *this; }
  690. iterator operator ++(int)
  691. { iterator tmp = *this; ++(*(this)); return tmp; }
  692. iterator operator --(int)
  693. { iterator tmp = *this; --(*(this)); return tmp; }
  694. iterator &operator +=(difference_type i)
  695. { it += i; shift += i; return *this; }
  696. iterator &operator -=(difference_type i)
  697. { it -= i; shift -= i; return *this; }
  698. iterator operator +(difference_type i) const
  699. { iterator tmp = *this; return (tmp += i); }
  700. iterator operator -(difference_type i) const
  701. { iterator tmp = *this; return (tmp -= i); }
  702. difference_type operator -(const iterator &i) const
  703. { return it - i.it; }
  704. value_type operator *() const
  705. { return *it; }
  706. value_type operator [](int ii)
  707. { return *(it + ii); }
  708. bool operator ==(const iterator &i) const
  709. { return it == i.it; }
  710. bool operator !=(const iterator &i) const
  711. { return !(i == *this); }
  712. bool operator < (const iterator &i) const
  713. { return it < i.it; }
  714. size_type index(void) const { return shift; }
  715. slvector_const_iterator(void) {}
  716. slvector_const_iterator(const slvector_iterator<T>& iter)
  717. : it(iter.it), shift(iter.shift) {}
  718. slvector_const_iterator(const base_iterator &iter, size_type s)
  719. : it(iter), shift(s) {}
  720. };
  721. /** skyline vector.
  722. */
  723. template <typename T> class slvector {
  724. public :
  725. typedef slvector_iterator<T> iterators;
  726. typedef slvector_const_iterator<T> const_iterators;
  727. typedef typename std::vector<T>::size_type size_type;
  728. typedef T value_type;
  729. protected :
  730. std::vector<T> data;
  731. size_type shift;
  732. size_type size_;
  733. public :
  734. size_type size(void) const { return size_; }
  735. size_type first(void) const { return shift; }
  736. size_type last(void) const { return shift + data.size(); }
  737. ref_elt_vector<T, slvector<T> > operator [](size_type c)
  738. { return ref_elt_vector<T, slvector<T> >(this, c); }
  739. typename std::vector<T>::iterator data_begin(void) { return data.begin(); }
  740. typename std::vector<T>::iterator data_end(void) { return data.end(); }
  741. typename std::vector<T>::const_iterator data_begin(void) const
  742. { return data.begin(); }
  743. typename std::vector<T>::const_iterator data_end(void) const
  744. { return data.end(); }
  745. void w(size_type c, const T &e);
  746. T r(size_type c) const {
  747. GMM_ASSERT2(c < size_, "out of range");
  748. if (c < shift || c >= shift + data.size()) return T(0);
  749. return data[c - shift];
  750. }
  751. inline T operator [](size_type c) const { return r(c); }
  752. void resize(size_type);
  753. void clear(void) { data.resize(0); shift = 0; }
  754. void swap(slvector<T> &v) {
  755. std::swap(data, v.data);
  756. std::swap(shift, v.shift);
  757. std::swap(size_, v.size_);
  758. }
  759. slvector(void) : data(0), shift(0), size_(0) {}
  760. explicit slvector(size_type l) : data(0), shift(0), size_(l) {}
  761. slvector(size_type l, size_type d, size_type s)
  762. : data(d), shift(s), size_(l) {}
  763. };
  764. template<typename T> void slvector<T>::resize(size_type n) {
  765. if (n < last()) {
  766. if (shift >= n) clear(); else { data.resize(n-shift); }
  767. }
  768. size_ = n;
  769. }
  770. template<typename T> void slvector<T>::w(size_type c, const T &e) {
  771. GMM_ASSERT2(c < size_, "out of range");
  772. size_type s = data.size();
  773. if (!s) { data.resize(1); shift = c; }
  774. else if (c < shift) {
  775. data.resize(s + shift - c);
  776. typename std::vector<T>::iterator it = data.begin(),it2=data.end()-1;
  777. typename std::vector<T>::iterator it3 = it2 - shift + c;
  778. for (; it3 >= it; --it3, --it2) *it2 = *it3;
  779. std::fill(it, it + shift - c, T(0));
  780. shift = c;
  781. }
  782. else if (c >= shift + s) {
  783. data.resize(c - shift + 1);
  784. std::fill(data.begin() + s, data.end(), T(0));
  785. }
  786. data[c - shift] = e;
  787. }
  788. template <typename T> struct linalg_traits<slvector<T> > {
  789. typedef slvector<T> this_type;
  790. typedef this_type origin_type;
  791. typedef linalg_false is_reference;
  792. typedef abstract_vector linalg_type;
  793. typedef T value_type;
  794. typedef ref_elt_vector<T, slvector<T> > reference;
  795. typedef slvector_iterator<T> iterator;
  796. typedef slvector_const_iterator<T> const_iterator;
  797. typedef abstract_skyline storage_type;
  798. typedef linalg_true index_sorted;
  799. static size_type size(const this_type &v) { return v.size(); }
  800. static iterator begin(this_type &v)
  801. { return iterator(v.data_begin(), v.first()); }
  802. static const_iterator begin(const this_type &v)
  803. { return const_iterator(v.data_begin(), v.first()); }
  804. static iterator end(this_type &v)
  805. { return iterator(v.data_end(), v.last()); }
  806. static const_iterator end(const this_type &v)
  807. { return const_iterator(v.data_end(), v.last()); }
  808. static origin_type* origin(this_type &v) { return &v; }
  809. static const origin_type* origin(const this_type &v) { return &v; }
  810. static void clear(origin_type* o, const iterator &, const iterator &)
  811. { o->clear(); }
  812. static void do_clear(this_type &v) { v.clear(); }
  813. static value_type access(const origin_type *o, const const_iterator &,
  814. const const_iterator &, size_type i)
  815. { return (*o)[i]; }
  816. static reference access(origin_type *o, const iterator &, const iterator &,
  817. size_type i)
  818. { return (*o)[i]; }
  819. static void resize(this_type &v, size_type n) { v.resize(n); }
  820. };
  821. template<typename T> std::ostream &operator <<
  822. (std::ostream &o, const slvector<T>& v) { gmm::write(o,v); return o; }
  823. template <typename T>
  824. inline size_type nnz(const slvector<T>& l) { return l.last() - l.first(); }
  825. }
  826. namespace std {
  827. template <typename T> void swap(gmm::wsvector<T> &v, gmm::wsvector<T> &w)
  828. { v.swap(w);}
  829. template <typename T> void swap(gmm::rsvector<T> &v, gmm::rsvector<T> &w)
  830. { v.swap(w);}
  831. template <typename T> void swap(gmm::slvector<T> &v, gmm::slvector<T> &w)
  832. { v.swap(w);}
  833. }
  834. #endif /* GMM_VECTOR_H__ */