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.

972 lines
37 KiB

  1. /* -*- c++ -*- (enables emacs c++ mode) */
  2. /*===========================================================================
  3. Copyright (C) 2002-2015 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)) { this->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) this->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. if (this->nb_stored() - ind > 800)
  442. GMM_WARNING2("Inefficient addition of element in rsvector with "
  443. << this->nb_stored() - ind << " non-zero entries");
  444. base_type_::resize(nb_stored()+1, ev);
  445. if (ind != this->nb_stored() - 1) {
  446. it = this->begin() + ind;
  447. for (iterator ite = this->end() - 1; ite != it; --ite)
  448. *ite = *(ite-1);
  449. *it = ev;
  450. }
  451. }
  452. }
  453. }
  454. }
  455. template <typename T> T rsvector<T>::r(size_type c) const {
  456. GMM_ASSERT2(c < nbl, "out of range. Index " << c << " for a length of " << nbl);
  457. if (nb_stored() != 0) {
  458. elt_rsvector_<T> ev(c);
  459. const_iterator it = std::lower_bound(this->begin(), this->end(), ev);
  460. if (it != this->end() && it->c == c) return it->e;
  461. }
  462. return T(0);
  463. }
  464. template <typename T> struct linalg_traits<rsvector<T> > {
  465. typedef rsvector<T> this_type;
  466. typedef this_type origin_type;
  467. typedef linalg_false is_reference;
  468. typedef abstract_vector linalg_type;
  469. typedef T value_type;
  470. typedef ref_elt_vector<T, rsvector<T> > reference;
  471. typedef rsvector_iterator<T> iterator;
  472. typedef rsvector_const_iterator<T> const_iterator;
  473. typedef abstract_sparse storage_type;
  474. typedef linalg_true index_sorted;
  475. static size_type size(const this_type &v) { return v.size(); }
  476. static iterator begin(this_type &v) { return iterator(v.begin()); }
  477. static const_iterator begin(const this_type &v)
  478. { return const_iterator(v.begin()); }
  479. static iterator end(this_type &v) { return iterator(v.end()); }
  480. static const_iterator end(const this_type &v)
  481. { return const_iterator(v.end()); }
  482. static origin_type* origin(this_type &v) { return &v; }
  483. static const origin_type* origin(const this_type &v) { return &v; }
  484. static void clear(origin_type* o, const iterator &, const iterator &)
  485. { o->clear(); }
  486. static void do_clear(this_type &v) { v.clear(); }
  487. static value_type access(const origin_type *o, const const_iterator &,
  488. const const_iterator &, size_type i)
  489. { return (*o)[i]; }
  490. static reference access(origin_type *o, const iterator &, const iterator &,
  491. size_type i)
  492. { return (*o)[i]; }
  493. static void resize(this_type &v, size_type n) { v.resize(n); }
  494. };
  495. template<typename T> std::ostream &operator <<
  496. (std::ostream &o, const rsvector<T>& v) { gmm::write(o,v); return o; }
  497. /******* Optimized operations for rsvector<T> ****************************/
  498. template <typename T> inline void copy(const rsvector<T> &v1,
  499. rsvector<T> &v2) {
  500. GMM_ASSERT2(vect_size(v1) == vect_size(v2), "dimensions mismatch");
  501. v2 = v1;
  502. }
  503. template <typename T> inline
  504. void copy(const rsvector<T> &v1, const simple_vector_ref<rsvector<T> *> &v2){
  505. simple_vector_ref<rsvector<T> *>
  506. *svr = const_cast<simple_vector_ref<rsvector<T> *> *>(&v2);
  507. rsvector<T>
  508. *pv = const_cast<rsvector<T> *>((v2.origin));
  509. GMM_ASSERT2(vect_size(v1) == vect_size(v2), "dimensions mismatch");
  510. *pv = v1; svr->begin_ = vect_begin(*pv); svr->end_ = vect_end(*pv);
  511. }
  512. template <typename T> inline
  513. void copy(const simple_vector_ref<const rsvector<T> *> &v1,
  514. rsvector<T> &v2)
  515. { copy(*(v1.origin), v2); }
  516. template <typename T> inline
  517. void copy(const simple_vector_ref<rsvector<T> *> &v1, rsvector<T> &v2)
  518. { copy(*(v1.origin), v2); }
  519. template <typename V, typename T> inline void add(const V &v1,
  520. rsvector<T> &v2) {
  521. if ((const void *)(&v1) != (const void *)(&v2)) {
  522. GMM_ASSERT2(vect_size(v1) == vect_size(v2), "dimensions mismatch");
  523. add_rsvector(v1, v2, typename linalg_traits<V>::storage_type());
  524. }
  525. }
  526. template <typename V, typename T>
  527. inline void add_rsvector(const V &v1, rsvector<T> &v2, abstract_dense)
  528. { add(v1, v2, abstract_dense(), abstract_sparse()); }
  529. template <typename V, typename T>
  530. inline void add_rsvector(const V &v1, rsvector<T> &v2, abstract_skyline)
  531. { add(v1, v2, abstract_skyline(), abstract_sparse()); }
  532. template <typename V, typename T>
  533. void add_rsvector(const V &v1, rsvector<T> &v2, abstract_sparse) {
  534. add_rsvector(v1, v2, typename linalg_traits<V>::index_sorted());
  535. }
  536. template <typename V, typename T>
  537. void add_rsvector(const V &v1, rsvector<T> &v2, linalg_false) {
  538. add(v1, v2, abstract_sparse(), abstract_sparse());
  539. }
  540. template <typename V, typename T>
  541. void add_rsvector(const V &v1, rsvector<T> &v2, linalg_true) {
  542. typename linalg_traits<V>::const_iterator it1 = vect_const_begin(v1),
  543. ite1 = vect_const_end(v1);
  544. typename rsvector<T>::iterator it2 = v2.begin(), ite2 = v2.end(), it3;
  545. size_type nbc = 0, old_nbc = v2.nb_stored();
  546. for (; it1 != ite1 && it2 != ite2 ; ++nbc)
  547. if (it1.index() == it2->c) { ++it1; ++it2; }
  548. else if (it1.index() < it2->c) ++it1; else ++it2;
  549. for (; it1 != ite1; ++it1) ++nbc;
  550. for (; it2 != ite2; ++it2) ++nbc;
  551. v2.base_resize(nbc);
  552. it3 = v2.begin() + old_nbc;
  553. it2 = v2.end(); ite2 = v2.begin();
  554. it1 = vect_end(v1); ite1 = vect_const_begin(v1);
  555. while (it1 != ite1 && it3 != ite2) {
  556. --it3; --it1; --it2;
  557. if (it3->c > it1.index()) { *it2 = *it3; ++it1; }
  558. else if (it3->c == it1.index()) { *it2=*it3; it2->e+=*it1; }
  559. else { it2->c = it1.index(); it2->e = *it1; ++it3; }
  560. }
  561. while (it1 != ite1) { --it1; --it2; it2->c = it1.index(); it2->e = *it1; }
  562. }
  563. template <typename V, typename T> void copy(const V &v1, rsvector<T> &v2) {
  564. if ((const void *)(&v1) != (const void *)(&v2)) {
  565. GMM_ASSERT2(vect_size(v1) == vect_size(v2), "dimensions mismatch");
  566. if (same_origin(v1, v2))
  567. GMM_WARNING2("a conflict is possible in vector copy\n");
  568. copy_rsvector(v1, v2, typename linalg_traits<V>::storage_type());
  569. }
  570. }
  571. template <typename V, typename T>
  572. void copy_rsvector(const V &v1, rsvector<T> &v2, abstract_dense)
  573. { copy_vect(v1, v2, abstract_dense(), abstract_sparse()); }
  574. template <typename V, typename T>
  575. void copy_rsvector(const V &v1, rsvector<T> &v2, abstract_skyline)
  576. { copy_vect(v1, v2, abstract_skyline(), abstract_sparse()); }
  577. template <typename V, typename T>
  578. void copy_rsvector(const V &v1, rsvector<T> &v2, abstract_sparse) {
  579. copy_rsvector(v1, v2, typename linalg_traits<V>::index_sorted());
  580. }
  581. template <typename V, typename T2>
  582. void copy_rsvector(const V &v1, rsvector<T2> &v2, linalg_true) {
  583. typedef typename linalg_traits<V>::value_type T1;
  584. typename linalg_traits<V>::const_iterator it = vect_const_begin(v1),
  585. ite = vect_const_end(v1);
  586. v2.base_resize(nnz(v1));
  587. typename rsvector<T2>::iterator it2 = v2.begin();
  588. size_type nn = 0;
  589. for (; it != ite; ++it)
  590. if ((*it) != T1(0)) { it2->c = it.index(); it2->e = *it; ++it2; ++nn; }
  591. v2.base_resize(nn);
  592. }
  593. template <typename V, typename T2>
  594. void copy_rsvector(const V &v1, rsvector<T2> &v2, linalg_false) {
  595. typedef typename linalg_traits<V>::value_type T1;
  596. typename linalg_traits<V>::const_iterator it = vect_const_begin(v1),
  597. ite = vect_const_end(v1);
  598. v2.base_resize(nnz(v1));
  599. typename rsvector<T2>::iterator it2 = v2.begin();
  600. size_type nn = 0;
  601. for (; it != ite; ++it)
  602. if ((*it) != T1(0)) { it2->c = it.index(); it2->e = *it; ++it2; ++nn; }
  603. v2.base_resize(nn);
  604. std::sort(v2.begin(), v2.end());
  605. }
  606. template <typename T> inline void clean(rsvector<T> &v, double eps) {
  607. typedef typename number_traits<T>::magnitude_type R;
  608. typename rsvector<T>::iterator it = v.begin(), ite = v.end();
  609. for (; it != ite; ++it) if (gmm::abs((*it).e) <= eps) break;
  610. if (it != ite) {
  611. typename rsvector<T>::iterator itc = it;
  612. size_type erased = 1;
  613. for (++it; it != ite; ++it)
  614. { *itc = *it; if (gmm::abs((*it).e) <= R(eps)) ++erased; else ++itc; }
  615. v.base_resize(v.nb_stored() - erased);
  616. }
  617. }
  618. template <typename T>
  619. inline void clean(const simple_vector_ref<rsvector<T> *> &l, double eps) {
  620. simple_vector_ref<rsvector<T> *>
  621. *svr = const_cast<simple_vector_ref<rsvector<T> *> *>(&l);
  622. rsvector<T>
  623. *pv = const_cast<rsvector<T> *>((l.origin));
  624. clean(*pv, eps);
  625. svr->begin_ = vect_begin(*pv); svr->end_ = vect_end(*pv);
  626. }
  627. template <typename T>
  628. inline size_type nnz(const rsvector<T>& l) { return l.nb_stored(); }
  629. /*************************************************************************/
  630. /* */
  631. /* Class slvector: 'sky-line' vector. */
  632. /* */
  633. /*************************************************************************/
  634. template<typename T> struct slvector_iterator {
  635. typedef T value_type;
  636. typedef T *pointer;
  637. typedef T &reference;
  638. typedef ptrdiff_t difference_type;
  639. typedef std::random_access_iterator_tag iterator_category;
  640. typedef size_t size_type;
  641. typedef slvector_iterator<T> iterator;
  642. typedef typename std::vector<T>::iterator base_iterator;
  643. base_iterator it;
  644. size_type shift;
  645. iterator &operator ++()
  646. { ++it; ++shift; return *this; }
  647. iterator &operator --()
  648. { --it; --shift; return *this; }
  649. iterator operator ++(int)
  650. { iterator tmp = *this; ++(*(this)); return tmp; }
  651. iterator operator --(int)
  652. { iterator tmp = *this; --(*(this)); return tmp; }
  653. iterator &operator +=(difference_type i)
  654. { it += i; shift += i; return *this; }
  655. iterator &operator -=(difference_type i)
  656. { it -= i; shift -= i; return *this; }
  657. iterator operator +(difference_type i) const
  658. { iterator tmp = *this; return (tmp += i); }
  659. iterator operator -(difference_type i) const
  660. { iterator tmp = *this; return (tmp -= i); }
  661. difference_type operator -(const iterator &i) const
  662. { return it - i.it; }
  663. reference operator *() const
  664. { return *it; }
  665. reference operator [](int ii)
  666. { return *(it + ii); }
  667. bool operator ==(const iterator &i) const
  668. { return it == i.it; }
  669. bool operator !=(const iterator &i) const
  670. { return !(i == *this); }
  671. bool operator < (const iterator &i) const
  672. { return it < i.it; }
  673. size_type index(void) const { return shift; }
  674. slvector_iterator(void) {}
  675. slvector_iterator(const base_iterator &iter, size_type s)
  676. : it(iter), shift(s) {}
  677. };
  678. template<typename T> struct slvector_const_iterator {
  679. typedef T value_type;
  680. typedef const T *pointer;
  681. typedef value_type reference;
  682. typedef ptrdiff_t difference_type;
  683. typedef std::random_access_iterator_tag iterator_category;
  684. typedef size_t size_type;
  685. typedef slvector_const_iterator<T> iterator;
  686. typedef typename std::vector<T>::const_iterator base_iterator;
  687. base_iterator it;
  688. size_type shift;
  689. iterator &operator ++()
  690. { ++it; ++shift; return *this; }
  691. iterator &operator --()
  692. { --it; --shift; return *this; }
  693. iterator operator ++(int)
  694. { iterator tmp = *this; ++(*(this)); return tmp; }
  695. iterator operator --(int)
  696. { iterator tmp = *this; --(*(this)); return tmp; }
  697. iterator &operator +=(difference_type i)
  698. { it += i; shift += i; return *this; }
  699. iterator &operator -=(difference_type i)
  700. { it -= i; shift -= i; return *this; }
  701. iterator operator +(difference_type i) const
  702. { iterator tmp = *this; return (tmp += i); }
  703. iterator operator -(difference_type i) const
  704. { iterator tmp = *this; return (tmp -= i); }
  705. difference_type operator -(const iterator &i) const
  706. { return it - i.it; }
  707. value_type operator *() const
  708. { return *it; }
  709. value_type operator [](int ii)
  710. { return *(it + ii); }
  711. bool operator ==(const iterator &i) const
  712. { return it == i.it; }
  713. bool operator !=(const iterator &i) const
  714. { return !(i == *this); }
  715. bool operator < (const iterator &i) const
  716. { return it < i.it; }
  717. size_type index(void) const { return shift; }
  718. slvector_const_iterator(void) {}
  719. slvector_const_iterator(const slvector_iterator<T>& iter)
  720. : it(iter.it), shift(iter.shift) {}
  721. slvector_const_iterator(const base_iterator &iter, size_type s)
  722. : it(iter), shift(s) {}
  723. };
  724. /** skyline vector.
  725. */
  726. template <typename T> class slvector {
  727. public :
  728. typedef slvector_iterator<T> iterators;
  729. typedef slvector_const_iterator<T> const_iterators;
  730. typedef typename std::vector<T>::size_type size_type;
  731. typedef T value_type;
  732. protected :
  733. std::vector<T> data;
  734. size_type shift;
  735. size_type size_;
  736. public :
  737. size_type size(void) const { return size_; }
  738. size_type first(void) const { return shift; }
  739. size_type last(void) const { return shift + data.size(); }
  740. ref_elt_vector<T, slvector<T> > operator [](size_type c)
  741. { return ref_elt_vector<T, slvector<T> >(this, c); }
  742. typename std::vector<T>::iterator data_begin(void) { return data.begin(); }
  743. typename std::vector<T>::iterator data_end(void) { return data.end(); }
  744. typename std::vector<T>::const_iterator data_begin(void) const
  745. { return data.begin(); }
  746. typename std::vector<T>::const_iterator data_end(void) const
  747. { return data.end(); }
  748. void w(size_type c, const T &e);
  749. T r(size_type c) const {
  750. GMM_ASSERT2(c < size_, "out of range");
  751. if (c < shift || c >= shift + data.size()) return T(0);
  752. return data[c - shift];
  753. }
  754. inline T operator [](size_type c) const { return r(c); }
  755. void resize(size_type);
  756. void clear(void) { data.resize(0); shift = 0; }
  757. void swap(slvector<T> &v) {
  758. std::swap(data, v.data);
  759. std::swap(shift, v.shift);
  760. std::swap(size_, v.size_);
  761. }
  762. slvector(void) : data(0), shift(0), size_(0) {}
  763. explicit slvector(size_type l) : data(0), shift(0), size_(l) {}
  764. slvector(size_type l, size_type d, size_type s)
  765. : data(d), shift(s), size_(l) {}
  766. };
  767. template<typename T> void slvector<T>::resize(size_type n) {
  768. if (n < last()) {
  769. if (shift >= n) clear(); else { data.resize(n-shift); }
  770. }
  771. size_ = n;
  772. }
  773. template<typename T> void slvector<T>::w(size_type c, const T &e) {
  774. GMM_ASSERT2(c < size_, "out of range");
  775. size_type s = data.size();
  776. if (!s) { data.resize(1); shift = c; }
  777. else if (c < shift) {
  778. data.resize(s + shift - c);
  779. typename std::vector<T>::iterator it = data.begin(),it2=data.end()-1;
  780. typename std::vector<T>::iterator it3 = it2 - shift + c;
  781. for (; it3 >= it; --it3, --it2) *it2 = *it3;
  782. std::fill(it, it + shift - c, T(0));
  783. shift = c;
  784. }
  785. else if (c >= shift + s) {
  786. data.resize(c - shift + 1);
  787. std::fill(data.begin() + s, data.end(), T(0));
  788. }
  789. data[c - shift] = e;
  790. }
  791. template <typename T> struct linalg_traits<slvector<T> > {
  792. typedef slvector<T> this_type;
  793. typedef this_type origin_type;
  794. typedef linalg_false is_reference;
  795. typedef abstract_vector linalg_type;
  796. typedef T value_type;
  797. typedef ref_elt_vector<T, slvector<T> > reference;
  798. typedef slvector_iterator<T> iterator;
  799. typedef slvector_const_iterator<T> const_iterator;
  800. typedef abstract_skyline storage_type;
  801. typedef linalg_true index_sorted;
  802. static size_type size(const this_type &v) { return v.size(); }
  803. static iterator begin(this_type &v)
  804. { return iterator(v.data_begin(), v.first()); }
  805. static const_iterator begin(const this_type &v)
  806. { return const_iterator(v.data_begin(), v.first()); }
  807. static iterator end(this_type &v)
  808. { return iterator(v.data_end(), v.last()); }
  809. static const_iterator end(const this_type &v)
  810. { return const_iterator(v.data_end(), v.last()); }
  811. static origin_type* origin(this_type &v) { return &v; }
  812. static const origin_type* origin(const this_type &v) { return &v; }
  813. static void clear(origin_type* o, const iterator &, const iterator &)
  814. { o->clear(); }
  815. static void do_clear(this_type &v) { v.clear(); }
  816. static value_type access(const origin_type *o, const const_iterator &,
  817. const const_iterator &, size_type i)
  818. { return (*o)[i]; }
  819. static reference access(origin_type *o, const iterator &, const iterator &,
  820. size_type i)
  821. { return (*o)[i]; }
  822. static void resize(this_type &v, size_type n) { v.resize(n); }
  823. };
  824. template<typename T> std::ostream &operator <<
  825. (std::ostream &o, const slvector<T>& v) { gmm::write(o,v); return o; }
  826. template <typename T>
  827. inline size_type nnz(const slvector<T>& l) { return l.last() - l.first(); }
  828. }
  829. namespace std {
  830. template <typename T> void swap(gmm::wsvector<T> &v, gmm::wsvector<T> &w)
  831. { v.swap(w);}
  832. template <typename T> void swap(gmm::rsvector<T> &v, gmm::rsvector<T> &w)
  833. { v.swap(w);}
  834. template <typename T> void swap(gmm::slvector<T> &v, gmm::slvector<T> &w)
  835. { v.swap(w);}
  836. }
  837. #endif /* GMM_VECTOR_H__ */