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.

1571 lines
58 KiB

  1. /* -*- c++ -*- (enables emacs c++ mode) */
  2. /*===========================================================================
  3. Copyright (C) 2002-2017 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 bool operator ==(T v) const { return ((*pm).r(l) == v); }
  49. inline bool operator !=(T v) const { return ((*pm).r(l) != v); }
  50. inline bool operator ==(std::complex<T> v) const
  51. { return ((*pm).r(l) == v); }
  52. inline bool operator !=(std::complex<T> v) const
  53. { return ((*pm).r(l) != v); }
  54. inline ref_elt_vector &operator +=(T v)
  55. { (*pm).wa(l, v); return *this; }
  56. inline ref_elt_vector &operator -=(T v)
  57. { (*pm).wa(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 *=(T v)
  61. { (*pm).w(l,(*pm).r(l) * v); return *this; }
  62. inline ref_elt_vector &operator =(const ref_elt_vector &re)
  63. { *this = T(re); return *this; }
  64. inline ref_elt_vector &operator =(T v)
  65. { (*pm).w(l,v); return *this; }
  66. T operator +() { return T(*this); }
  67. T operator -() { return -T(*this); }
  68. T operator +(T v) { return T(*this)+ v; }
  69. T operator -(T v) { return T(*this)- v; }
  70. T operator *(T v) { return T(*this)* v; }
  71. T operator /(T v) { return T(*this)/ v; }
  72. std::complex<T> operator +(std::complex<T> v) { return T(*this)+ v; }
  73. std::complex<T> operator -(std::complex<T> v) { return T(*this)- v; }
  74. std::complex<T> operator *(std::complex<T> v) { return T(*this)* v; }
  75. std::complex<T> operator /(std::complex<T> v) { return T(*this)/ v; }
  76. };
  77. template<typename T, typename V> class ref_elt_vector<std::complex<T>,V> {
  78. V *pm;
  79. size_type l;
  80. public :
  81. operator std::complex<T>() const { return pm->r(l); }
  82. ref_elt_vector(V *p, size_type ll) : pm(p), l(ll) {}
  83. inline bool operator ==(std::complex<T> v) const
  84. { return ((*pm).r(l) == v); }
  85. inline bool operator !=(std::complex<T> v) const
  86. { return ((*pm).r(l) != v); }
  87. inline bool operator ==(T v) const { return ((*pm).r(l) == v); }
  88. inline bool operator !=(T v) const { return ((*pm).r(l) != v); }
  89. inline ref_elt_vector &operator +=(std::complex<T> v)
  90. { (*pm).w(l,(*pm).r(l) + v); return *this; }
  91. inline ref_elt_vector &operator -=(std::complex<T> v)
  92. { (*pm).w(l,(*pm).r(l) - v); return *this; }
  93. inline ref_elt_vector &operator /=(std::complex<T> v)
  94. { (*pm).w(l,(*pm).r(l) / v); return *this; }
  95. inline ref_elt_vector &operator *=(std::complex<T> v)
  96. { (*pm).w(l,(*pm).r(l) * v); return *this; }
  97. inline ref_elt_vector &operator =(const ref_elt_vector &re)
  98. { *this = T(re); return *this; }
  99. inline ref_elt_vector &operator =(std::complex<T> v)
  100. { (*pm).w(l,v); return *this; }
  101. inline ref_elt_vector &operator =(T v)
  102. { (*pm).w(l,std::complex<T>(v)); return *this; }
  103. inline ref_elt_vector &operator +=(T v)
  104. { (*pm).w(l,(*pm).r(l) + v); return *this; }
  105. inline ref_elt_vector &operator -=(T v)
  106. { (*pm).w(l,(*pm).r(l) - v); return *this; }
  107. inline ref_elt_vector &operator /=(T v)
  108. { (*pm).w(l,(*pm).r(l) / v); return *this; }
  109. inline ref_elt_vector &operator *=(T v)
  110. { (*pm).w(l,(*pm).r(l) * v); return *this; }
  111. std::complex<T> operator +() { return std::complex<T>(*this); }
  112. std::complex<T> operator -() { return -std::complex<T>(*this); }
  113. std::complex<T> operator +(T v) { return std::complex<T>(*this)+ v; }
  114. std::complex<T> operator -(T v) { return std::complex<T>(*this)- v; }
  115. std::complex<T> operator *(T v) { return std::complex<T>(*this)* v; }
  116. std::complex<T> operator /(T v) { return std::complex<T>(*this)/ v; }
  117. std::complex<T> operator +(std::complex<T> v)
  118. { return std::complex<T>(*this)+ v; }
  119. std::complex<T> operator -(std::complex<T> v)
  120. { return std::complex<T>(*this)- v; }
  121. std::complex<T> operator *(std::complex<T> v)
  122. { return std::complex<T>(*this)* v; }
  123. std::complex<T> operator /(std::complex<T> v)
  124. { return std::complex<T>(*this)/ v; }
  125. };
  126. template<typename T, typename V> inline
  127. bool operator ==(T v, const ref_elt_vector<T, V> &re) { return (v==T(re)); }
  128. template<typename T, typename V> inline
  129. bool operator !=(T v, const ref_elt_vector<T, V> &re) { return (v!=T(re)); }
  130. template<typename T, typename V> inline
  131. T &operator +=(T &v, const ref_elt_vector<T, V> &re)
  132. { v += T(re); return v; }
  133. template<typename T, typename V> inline
  134. T &operator -=(T &v, const ref_elt_vector<T, V> &re)
  135. { v -= T(re); return v; }
  136. template<typename T, typename V> inline
  137. T &operator *=(T &v, const ref_elt_vector<T, V> &re)
  138. { v *= T(re); return v; }
  139. template<typename T, typename V> inline
  140. T &operator /=(T &v, const ref_elt_vector<T, V> &re)
  141. { v /= T(re); return v; }
  142. template<typename T, typename V> inline
  143. T operator +(T v, const ref_elt_vector<T, V> &re) { return v+ T(re); }
  144. template<typename T, typename V> inline
  145. T operator -(T v, const ref_elt_vector<T, V> &re) { return v- T(re); }
  146. template<typename T, typename V> inline
  147. T operator *(T v, const ref_elt_vector<T, V> &re) { return v* T(re); }
  148. template<typename T, typename V> inline
  149. T operator /(T v, const ref_elt_vector<T, V> &re) { return v/ T(re); }
  150. template<typename T, typename V> inline
  151. std::complex<T> operator +(std::complex<T> v, const ref_elt_vector<T, V> &re)
  152. { return v+ T(re); }
  153. template<typename T, typename V> inline
  154. std::complex<T> operator -(std::complex<T> v, const ref_elt_vector<T, V> &re)
  155. { return v- T(re); }
  156. template<typename T, typename V> inline
  157. std::complex<T> operator *(std::complex<T> v, const ref_elt_vector<T, V> &re)
  158. { return v* T(re); }
  159. template<typename T, typename V> inline
  160. std::complex<T> operator /(std::complex<T> v, const ref_elt_vector<T, V> &re)
  161. { return v/ T(re); }
  162. template<typename T, typename V> inline
  163. std::complex<T> operator +(T v, const ref_elt_vector<std::complex<T>, V> &re)
  164. { return v+ std::complex<T>(re); }
  165. template<typename T, typename V> inline
  166. std::complex<T> operator -(T v, const ref_elt_vector<std::complex<T>, V> &re)
  167. { return v- std::complex<T>(re); }
  168. template<typename T, typename V> inline
  169. std::complex<T> operator *(T v, const ref_elt_vector<std::complex<T>, V> &re)
  170. { return v* std::complex<T>(re); }
  171. template<typename T, typename V> inline
  172. std::complex<T> operator /(T v, const ref_elt_vector<std::complex<T>, V> &re)
  173. { return v/ std::complex<T>(re); }
  174. template<typename T, typename V> inline
  175. typename number_traits<T>::magnitude_type
  176. abs(const ref_elt_vector<T, V> &re) { return gmm::abs(T(re)); }
  177. template<typename T, typename V> inline
  178. T sqr(const ref_elt_vector<T, V> &re) { return gmm::sqr(T(re)); }
  179. template<typename T, typename V> inline
  180. typename number_traits<T>::magnitude_type
  181. abs_sqr(const ref_elt_vector<T, V> &re) { return gmm::abs_sqr(T(re)); }
  182. template<typename T, typename V> inline
  183. T conj(const ref_elt_vector<T, V> &re) { return gmm::conj(T(re)); }
  184. template<typename T, typename V> std::ostream &operator <<
  185. (std::ostream &o, const ref_elt_vector<T, V> &re) { o << T(re); return o; }
  186. template<typename T, typename V> inline
  187. typename number_traits<T>::magnitude_type
  188. real(const ref_elt_vector<T, V> &re) { return gmm::real(T(re)); }
  189. template<typename T, typename V> inline
  190. typename number_traits<T>::magnitude_type
  191. imag(const ref_elt_vector<T, V> &re) { return gmm::imag(T(re)); }
  192. /*************************************************************************/
  193. /* */
  194. /* Class dsvector: sparse vector optimized for random write operations */
  195. /* with constant complexity for read and write operations. */
  196. /* Based on distribution sort principle. */
  197. /* Cheap for densely populated vectors. */
  198. /* */
  199. /*************************************************************************/
  200. template<typename T> class dsvector;
  201. template<typename T> struct dsvector_iterator {
  202. size_type i; // Current index.
  203. T* p; // Pointer to the current position.
  204. dsvector<T> *v; // Pointer to the vector.
  205. typedef T value_type;
  206. typedef value_type* pointer;
  207. typedef const value_type* const_pointer;
  208. typedef value_type& reference;
  209. // typedef size_t size_type;
  210. typedef ptrdiff_t difference_type;
  211. typedef std::bidirectional_iterator_tag iterator_category;
  212. typedef dsvector_iterator<T> iterator;
  213. reference operator *() const { return *p; }
  214. pointer operator->() const { return &(operator*()); }
  215. iterator &operator ++() {
  216. for (size_type k = (i & 15); k < 15; ++k)
  217. { ++p; ++i; if (*p != T(0)) return *this; }
  218. v->next_pos(*(const_cast<const_pointer *>(&(p))), i);
  219. return *this;
  220. }
  221. iterator operator ++(int) { iterator tmp = *this; ++(*this); return tmp; }
  222. iterator &operator --() {
  223. for (size_type k = (i & 15); k > 0; --k)
  224. { --p; --i; if (*p != T(0)) return *this; }
  225. v->previous_pos(p, i);
  226. return *this;
  227. }
  228. iterator operator --(int) { iterator tmp = *this; --(*this); return tmp; }
  229. bool operator ==(const iterator &it) const
  230. { return (i == it.i && p == it.p && v == it.v); }
  231. bool operator !=(const iterator &it) const
  232. { return !(it == *this); }
  233. size_type index(void) const { return i; }
  234. dsvector_iterator(void) : i(size_type(-1)), p(0), v(0) {}
  235. dsvector_iterator(dsvector<T> &w) : i(size_type(-1)), p(0), v(&w) {};
  236. };
  237. template<typename T> struct dsvector_const_iterator {
  238. size_type i; // Current index.
  239. const T* p; // Pointer to the current position.
  240. const dsvector<T> *v; // Pointer to the vector.
  241. typedef T value_type;
  242. typedef const value_type* pointer;
  243. typedef const value_type& reference;
  244. // typedef size_t size_type;
  245. typedef ptrdiff_t difference_type;
  246. typedef std::bidirectional_iterator_tag iterator_category;
  247. typedef dsvector_const_iterator<T> iterator;
  248. reference operator *() const { return *p; }
  249. pointer operator->() const { return &(operator*()); }
  250. iterator &operator ++() {
  251. for (size_type k = (i & 15); k < 15; ++k)
  252. { ++p; ++i; if (*p != T(0)) return *this; }
  253. v->next_pos(p, i);
  254. return *this;
  255. }
  256. iterator operator ++(int) { iterator tmp = *this; ++(*this); return tmp; }
  257. iterator &operator --() {
  258. for (size_type k = (i & 15); k > 0; --k)
  259. { --p; --i; if (*p != T(0)) return *this; }
  260. v->previous_pos(p, i);
  261. return *this;
  262. }
  263. iterator operator --(int) { iterator tmp = *this; --(*this); return tmp; }
  264. bool operator ==(const iterator &it) const
  265. { return (i == it.i && p == it.p && v == it.v); }
  266. bool operator !=(const iterator &it) const
  267. { return !(it == *this); }
  268. size_type index(void) const { return i; }
  269. dsvector_const_iterator(void) : i(size_type(-1)), p(0) {}
  270. dsvector_const_iterator(const dsvector_iterator<T> &it)
  271. : i(it.i), p(it.p), v(it.v) {}
  272. dsvector_const_iterator(const dsvector<T> &w)
  273. : i(size_type(-1)), p(0), v(&w) {};
  274. };
  275. /**
  276. Sparse vector built on distribution sort principle.
  277. Read and write access have a constant complexity depending only on the
  278. vector size.
  279. */
  280. template<typename T> class dsvector {
  281. typedef dsvector_iterator<T> iterator;
  282. typedef dsvector_const_iterator<T> const_iterator;
  283. typedef dsvector<T> this_type;
  284. typedef T * pointer;
  285. typedef const T * const_pointer;
  286. typedef void * void_pointer;
  287. typedef const void * const_void_pointer;
  288. protected:
  289. size_type n; // Potential vector size
  290. size_type depth; // Number of row of pointer arrays
  291. size_type mask; // Mask for the first pointer array
  292. size_type shift; // Shift for the first pointer array
  293. void_pointer root_ptr; // Root pointer
  294. const T *read_access(size_type i) const {
  295. GMM_ASSERT1(i < n, "index out of range");
  296. size_type my_mask = mask, my_shift = shift;
  297. void_pointer p = root_ptr;
  298. if (!p) return 0;
  299. for (size_type k = 0; k < depth; ++k) {
  300. p = ((void **)(p))[(i & my_mask) >> my_shift];
  301. if (!p) return 0;
  302. my_mask = (my_mask >> 4);
  303. my_shift -= 4;
  304. }
  305. GMM_ASSERT1(my_shift == 0, "internal error");
  306. GMM_ASSERT1(my_mask == 15, "internal error");
  307. return &(((const T *)(p))[i & 15]);
  308. }
  309. T *write_access(size_type i) {
  310. GMM_ASSERT1(i < n, "index " << i << " out of range (size " << n << ")");
  311. size_type my_mask = mask, my_shift = shift;
  312. if (!root_ptr) {
  313. if (depth) {
  314. root_ptr = new void_pointer[16];
  315. std::memset(root_ptr, 0, 16*sizeof(void_pointer));
  316. } else {
  317. root_ptr = new T[16];
  318. for (size_type l = 0; l < 16; ++l) ((T *)(root_ptr))[l] = T(0);
  319. }
  320. }
  321. void_pointer p = root_ptr;
  322. for (size_type k = 0; k < depth; ++k) {
  323. size_type j = (i & my_mask) >> my_shift;
  324. void_pointer q = ((void_pointer *)(p))[j];
  325. if (!q) {
  326. if (k+1 != depth) {
  327. q = new void_pointer[16];
  328. std::memset(q, 0, 16*sizeof(void_pointer));
  329. } else {
  330. q = new T[16];
  331. for (size_type l = 0; l < 16; ++l) ((T *)(q))[l] = T(0);
  332. }
  333. ((void_pointer *)(p))[j] = q;
  334. }
  335. p = q;
  336. my_mask = (my_mask >> 4);
  337. my_shift -= 4;
  338. }
  339. GMM_ASSERT1(my_shift == 0, "internal error");
  340. GMM_ASSERT1(my_mask == 15, "internal error " << my_mask);
  341. return &(((T *)(p))[i & 15]);
  342. }
  343. void init(size_type n_) {
  344. n = n_; depth = 0; shift = 0; mask = 1; if (n_) --n_;
  345. while (n_) { n_ /= 16; ++depth; shift += 4; mask *= 16; }
  346. mask--; if (shift) shift -= 4; if (depth) --depth;
  347. root_ptr = 0;
  348. }
  349. void rec_del(void_pointer p, size_type my_depth) {
  350. if (my_depth) {
  351. for (size_type k = 0; k < 16; ++k)
  352. if (((void_pointer *)(p))[k])
  353. rec_del(((void_pointer *)(p))[k], my_depth-1);
  354. delete[] ((void_pointer *)(p));
  355. } else {
  356. delete[] ((T *)(p));
  357. }
  358. }
  359. void rec_clean(void_pointer p, size_type my_depth, double eps) {
  360. if (my_depth) {
  361. for (size_type k = 0; k < 16; ++k)
  362. if (((void_pointer *)(p))[k])
  363. rec_clean(((void_pointer *)(p))[k], my_depth-1, eps);
  364. } else {
  365. for (size_type k = 0; k < 16; ++k)
  366. if (gmm::abs(((T *)(p))[k]) <= eps) ((T *)(p))[k] = T(0);
  367. }
  368. }
  369. void rec_clean_i(void_pointer p, size_type my_depth, size_type my_mask,
  370. size_type i, size_type base) {
  371. if (my_depth) {
  372. my_mask = (my_mask >> 4);
  373. for (size_type k = 0; k < 16; ++k)
  374. if (((void_pointer *)(p))[k] && (base + (k+1)*(mask+1)) >= i)
  375. rec_clean_i(((void_pointer *)(p))[k], my_depth-1, my_mask,
  376. i, base + k*(my_mask+1));
  377. } else {
  378. for (size_type k = 0; k < 16; ++k)
  379. if (base+k > i) ((T *)(p))[k] = T(0);
  380. }
  381. }
  382. size_type rec_nnz(void_pointer p, size_type my_depth) const {
  383. size_type nn = 0;
  384. if (my_depth) {
  385. for (size_type k = 0; k < 16; ++k)
  386. if (((void_pointer *)(p))[k])
  387. nn += rec_nnz(((void_pointer *)(p))[k], my_depth-1);
  388. } else {
  389. for (size_type k = 0; k < 16; ++k)
  390. if (((const T *)(p))[k] != T(0)) nn++;
  391. }
  392. return nn;
  393. }
  394. void copy_rec(void_pointer &p, const_void_pointer q, size_type my_depth) {
  395. if (my_depth) {
  396. p = new void_pointer[16];
  397. std::memset(p, 0, 16*sizeof(void_pointer));
  398. for (size_type l = 0; l < 16; ++l)
  399. if (((const const_void_pointer *)(q))[l])
  400. copy_rec(((void_pointer *)(p))[l],
  401. ((const const_void_pointer *)(q))[l], my_depth-1);
  402. } else {
  403. p = new T[16];
  404. for (size_type l = 0; l < 16; ++l) ((T *)(p))[l] = ((const T *)(q))[l];
  405. }
  406. }
  407. void copy(const dsvector<T> &v) {
  408. if (root_ptr) rec_del(root_ptr, depth);
  409. root_ptr = 0;
  410. mask = v.mask; depth = v.depth; n = v.n; shift = v.shift;
  411. if (v.root_ptr) copy_rec(root_ptr, v.root_ptr, depth);
  412. }
  413. void next_pos_rec(void_pointer p, size_type my_depth, size_type my_mask,
  414. const_pointer &pp, size_type &i, size_type base) const {
  415. size_type ii = i;
  416. if (my_depth) {
  417. my_mask = (my_mask >> 4);
  418. for (size_type k = 0; k < 16; ++k)
  419. if (((void_pointer *)(p))[k] && (base + (k+1)*(my_mask+1)) >= i) {
  420. next_pos_rec(((void_pointer *)(p))[k], my_depth-1, my_mask,
  421. pp, i, base + k*(my_mask+1));
  422. if (i != size_type(-1)) return; else i = ii;
  423. }
  424. i = size_type(-1); pp = 0;
  425. } else {
  426. for (size_type k = 0; k < 16; ++k)
  427. if (base+k > i && ((const_pointer)(p))[k] != T(0))
  428. { i = base+k; pp = &(((const_pointer)(p))[k]); return; }
  429. i = size_type(-1); pp = 0;
  430. }
  431. }
  432. void previous_pos_rec(void_pointer p, size_type my_depth, size_type my_mask,
  433. const_pointer &pp, size_type &i,
  434. size_type base) const {
  435. size_type ii = i;
  436. if (my_depth) {
  437. my_mask = (my_mask >> 4);
  438. for (size_type k = 15; k != size_type(-1); --k)
  439. if (((void_pointer *)(p))[k] && ((base + k*(my_mask+1)) < i)) {
  440. previous_pos_rec(((void_pointer *)(p))[k], my_depth-1,
  441. my_mask, pp, i, base + k*(my_mask+1));
  442. if (i != size_type(-1)) return; else i = ii;
  443. }
  444. i = size_type(-1); pp = 0;
  445. } else {
  446. for (size_type k = 15; k != size_type(-1); --k)
  447. if (base+k < i && ((const_pointer)(p))[k] != T(0))
  448. { i = base+k; pp = &(((const_pointer)(p))[k]); return; }
  449. i = size_type(-1); pp = 0;
  450. }
  451. }
  452. public:
  453. void clean(double eps) { if (root_ptr) rec_clean(root_ptr, depth); }
  454. void resize(size_type n_) {
  455. if (n_ != n) {
  456. n = n_;
  457. if (n_ < n) { // Depth unchanged (a choice)
  458. if (root_ptr) rec_clean_i(root_ptr, depth, mask, n_, 0);
  459. } else {
  460. // may change the depth (add some levels)
  461. size_type my_depth = 0, my_shift = 0, my_mask = 1; if (n_) --n_;
  462. while (n_) { n_ /= 16; ++my_depth; my_shift += 4; my_mask *= 16; }
  463. my_mask--; if (my_shift) my_shift -= 4; if (my_depth) --my_depth;
  464. if (my_depth > depth || depth == 0) {
  465. if (root_ptr) {
  466. for (size_type k = depth; k < my_depth; ++k) {
  467. void_pointer *q = new void_pointer [16];
  468. std::memset(q, 0, 16*sizeof(void_pointer));
  469. q[0] = root_ptr; root_ptr = q;
  470. }
  471. }
  472. mask = my_mask; depth = my_depth; shift = my_shift;
  473. }
  474. }
  475. }
  476. }
  477. void clear(void) { if (root_ptr) rec_del(root_ptr, depth); root_ptr = 0; }
  478. void next_pos(const_pointer &pp, size_type &i) const {
  479. if (!root_ptr || i >= n) { pp = 0, i = size_type(-1); return; }
  480. next_pos_rec(root_ptr, depth, mask, pp, i, 0);
  481. }
  482. void previous_pos(const_pointer &pp, size_type &i) const {
  483. if (!root_ptr) { pp = 0, i = size_type(-1); return; }
  484. if (i == size_type(-1)) { i = n; }
  485. previous_pos_rec(root_ptr, depth, mask, pp, i, 0);
  486. }
  487. iterator begin(void) {
  488. iterator it(*this);
  489. if (n && root_ptr) {
  490. it.i = 0; it.p = const_cast<T *>(read_access(0));
  491. if (!(it.p) || *(it.p) == T(0))
  492. next_pos(*(const_cast<const_pointer *>(&(it.p))), it.i);
  493. }
  494. return it;
  495. }
  496. iterator end(void) { return iterator(*this); }
  497. const_iterator begin(void) const {
  498. const_iterator it(*this);
  499. if (n && root_ptr) {
  500. it.i = 0; it.p = read_access(0);
  501. if (!(it.p) || *(it.p) == T(0)) next_pos(it.p, it.i);
  502. }
  503. return it;
  504. }
  505. const_iterator end(void) const { return const_iterator(*this); }
  506. inline ref_elt_vector<T, dsvector<T> > operator [](size_type c)
  507. { return ref_elt_vector<T, dsvector<T> >(this, c); }
  508. inline void w(size_type c, const T &e) {
  509. if (e == T(0)) { if (read_access(c)) *(write_access(c)) = e; }
  510. else *(write_access(c)) = e;
  511. }
  512. inline void wa(size_type c, const T &e)
  513. { if (e != T(0)) { *(write_access(c)) += e; } }
  514. inline T r(size_type c) const
  515. { const T *p = read_access(c); if (p) return *p; else return T(0); }
  516. inline T operator [](size_type c) const { return r(c); }
  517. size_type nnz(void) const
  518. { if (root_ptr) return rec_nnz(root_ptr, depth); else return 0; }
  519. size_type size(void) const { return n; }
  520. void swap(dsvector<T> &v) {
  521. std::swap(n, v.n); std::swap(root_ptr, v.root_ptr);
  522. std::swap(depth, v.depth); std::swap(shift, v.shift);
  523. std::swap(mask, v.mask);
  524. }
  525. /* Constructors */
  526. dsvector(const dsvector<T> &v) { init(0); copy(v); }
  527. dsvector<T> &operator =(const dsvector<T> &v) { copy(v); return *this; }
  528. explicit dsvector(size_type l){ init(l); }
  529. dsvector(void) { init(0); }
  530. ~dsvector() { if (root_ptr) rec_del(root_ptr, depth); root_ptr = 0; }
  531. };
  532. template <typename T> struct linalg_traits<dsvector<T>> {
  533. typedef dsvector<T> this_type;
  534. typedef this_type origin_type;
  535. typedef linalg_false is_reference;
  536. typedef abstract_vector linalg_type;
  537. typedef T value_type;
  538. typedef ref_elt_vector<T, dsvector<T> > reference;
  539. typedef dsvector_iterator<T> iterator;
  540. typedef dsvector_const_iterator<T> const_iterator;
  541. typedef abstract_sparse storage_type;
  542. typedef linalg_true index_sorted;
  543. static size_type size(const this_type &v) { return v.size(); }
  544. static iterator begin(this_type &v) { return v.begin(); }
  545. static const_iterator begin(const this_type &v) { return v.begin(); }
  546. static iterator end(this_type &v) { return v.end(); }
  547. static const_iterator end(const this_type &v) { return v.end(); }
  548. static origin_type* origin(this_type &v) { return &v; }
  549. static const origin_type* origin(const this_type &v) { return &v; }
  550. static void clear(origin_type* o, const iterator &, const iterator &)
  551. { o->clear(); }
  552. static void do_clear(this_type &v) { v.clear(); }
  553. static value_type access(const origin_type *o, const const_iterator &,
  554. const const_iterator &, size_type i)
  555. { return (*o)[i]; }
  556. static reference access(origin_type *o, const iterator &, const iterator &,
  557. size_type i)
  558. { return (*o)[i]; }
  559. static void resize(this_type &v, size_type n) { v.resize(n); }
  560. };
  561. template<typename T> std::ostream &operator <<
  562. (std::ostream &o, const dsvector<T>& v) { gmm::write(o,v); return o; }
  563. /******* Optimized operations for dsvector<T> ****************************/
  564. template <typename T> inline void copy(const dsvector<T> &v1,
  565. dsvector<T> &v2) {
  566. GMM_ASSERT2(v1.size() == v2.size(), "dimensions mismatch");
  567. v2 = v1;
  568. }
  569. template <typename T> inline void copy(const dsvector<T> &v1,
  570. const dsvector<T> &v2) {
  571. GMM_ASSERT2(v1.size() == v2.size(), "dimensions mismatch");
  572. v2 = const_cast<dsvector<T> &>(v1);
  573. }
  574. template <typename T> inline
  575. void copy(const dsvector<T> &v1, const simple_vector_ref<dsvector<T> *> &v2){
  576. simple_vector_ref<dsvector<T> *>
  577. *svr = const_cast<simple_vector_ref<dsvector<T> *> *>(&v2);
  578. dsvector<T>
  579. *pv = const_cast<dsvector<T> *>((v2.origin));
  580. GMM_ASSERT2(vect_size(v1) == vect_size(v2), "dimensions mismatch");
  581. *pv = v1; svr->begin_ = vect_begin(*pv); svr->end_ = vect_end(*pv);
  582. }
  583. template <typename T> inline
  584. void copy(const simple_vector_ref<const dsvector<T> *> &v1,
  585. dsvector<T> &v2)
  586. { copy(*(v1.origin), v2); }
  587. template <typename T> inline
  588. void copy(const simple_vector_ref<dsvector<T> *> &v1, dsvector<T> &v2)
  589. { copy(*(v1.origin), v2); }
  590. template <typename T> inline
  591. void copy(const simple_vector_ref<dsvector<T> *> &v1,
  592. const simple_vector_ref<dsvector<T> *> &v2)
  593. { copy(*(v1.origin), v2); }
  594. template <typename T> inline
  595. void copy(const simple_vector_ref<const dsvector<T> *> &v1,
  596. const simple_vector_ref<dsvector<T> *> &v2)
  597. { copy(*(v1.origin), v2); }
  598. template <typename T>
  599. inline size_type nnz(const dsvector<T>& l) { return l.nnz(); }
  600. /*************************************************************************/
  601. /* */
  602. /* Class wsvector: sparse vector optimized for random write operations, */
  603. /* with log(n) complexity for read and write operations. */
  604. /* Based on std::map */
  605. /* */
  606. /*************************************************************************/
  607. template<typename T> struct wsvector_iterator
  608. : public std::map<size_type, T>::iterator {
  609. typedef typename std::map<size_type, T>::iterator base_it_type;
  610. typedef T value_type;
  611. typedef value_type* pointer;
  612. typedef value_type& reference;
  613. // typedef size_t size_type;
  614. typedef ptrdiff_t difference_type;
  615. typedef std::bidirectional_iterator_tag iterator_category;
  616. reference operator *() const { return (base_it_type::operator*()).second; }
  617. pointer operator->() const { return &(operator*()); }
  618. size_type index(void) const { return (base_it_type::operator*()).first; }
  619. wsvector_iterator(void) {}
  620. wsvector_iterator(const base_it_type &it) : base_it_type(it) {}
  621. };
  622. template<typename T> struct wsvector_const_iterator
  623. : public std::map<size_type, T>::const_iterator {
  624. typedef typename std::map<size_type, T>::const_iterator base_it_type;
  625. typedef T value_type;
  626. typedef const value_type* pointer;
  627. typedef const value_type& reference;
  628. // typedef size_t size_type;
  629. typedef ptrdiff_t difference_type;
  630. typedef std::bidirectional_iterator_tag iterator_category;
  631. reference operator *() const { return (base_it_type::operator*()).second; }
  632. pointer operator->() const { return &(operator*()); }
  633. size_type index(void) const { return (base_it_type::operator*()).first; }
  634. wsvector_const_iterator(void) {}
  635. wsvector_const_iterator(const wsvector_iterator<T> &it)
  636. : base_it_type(it) {}
  637. wsvector_const_iterator(const base_it_type &it) : base_it_type(it) {}
  638. };
  639. /**
  640. sparse vector built upon std::map.
  641. Read and write access are quite fast (log n)
  642. */
  643. template<typename T> class wsvector : public std::map<size_type, T> {
  644. public:
  645. typedef typename std::map<int, T>::size_type size_type;
  646. typedef std::map<size_type, T> base_type;
  647. typedef typename base_type::iterator iterator;
  648. typedef typename base_type::const_iterator const_iterator;
  649. protected:
  650. size_type nbl;
  651. public:
  652. void clean(double eps);
  653. void resize(size_type);
  654. inline ref_elt_vector<T, wsvector<T> > operator [](size_type c)
  655. { return ref_elt_vector<T, wsvector<T> >(this, c); }
  656. inline void w(size_type c, const T &e) {
  657. GMM_ASSERT2(c < nbl, "out of range");
  658. if (e == T(0)) { this->erase(c); }
  659. else base_type::operator [](c) = e;
  660. }
  661. inline void wa(size_type c, const T &e) {
  662. GMM_ASSERT2(c < nbl, "out of range");
  663. if (e != T(0)) {
  664. iterator it = this->lower_bound(c);
  665. if (it != this->end() && it->first == c) it->second += e;
  666. else base_type::operator [](c) = e;
  667. }
  668. }
  669. inline T r(size_type c) const {
  670. GMM_ASSERT2(c < nbl, "out of range");
  671. const_iterator it = this->lower_bound(c);
  672. if (it != this->end() && c == it->first) return it->second;
  673. else return T(0);
  674. }
  675. inline T operator [](size_type c) const { return r(c); }
  676. size_type nb_stored(void) const { return base_type::size(); }
  677. size_type size(void) const { return nbl; }
  678. void swap(wsvector<T> &v)
  679. { std::swap(nbl, v.nbl); std::map<size_type, T>::swap(v); }
  680. /* Constructors */
  681. void init(size_type l) { nbl = l; this->clear(); }
  682. explicit wsvector(size_type l){ init(l); }
  683. wsvector(void) { init(0); }
  684. };
  685. template<typename T> void wsvector<T>::clean(double eps) {
  686. iterator it = this->begin(), itf = it, ite = this->end();
  687. while (it != ite) {
  688. ++itf; if (gmm::abs(it->second) <= eps) this->erase(it); it = itf;
  689. }
  690. }
  691. template<typename T> void wsvector<T>::resize(size_type n) {
  692. if (n < nbl) {
  693. iterator it = this->begin(), itf = it, ite = this->end();
  694. while (it != ite) { ++itf; if (it->first >= n) this->erase(it); it=itf; }
  695. }
  696. nbl = n;
  697. }
  698. template <typename T> struct linalg_traits<wsvector<T> > {
  699. typedef wsvector<T> this_type;
  700. typedef this_type origin_type;
  701. typedef linalg_false is_reference;
  702. typedef abstract_vector linalg_type;
  703. typedef T value_type;
  704. typedef ref_elt_vector<T, wsvector<T> > reference;
  705. typedef wsvector_iterator<T> iterator;
  706. typedef wsvector_const_iterator<T> const_iterator;
  707. typedef abstract_sparse storage_type;
  708. typedef linalg_true index_sorted;
  709. static size_type size(const this_type &v) { return v.size(); }
  710. static iterator begin(this_type &v) { return v.begin(); }
  711. static const_iterator begin(const this_type &v) { return v.begin(); }
  712. static iterator end(this_type &v) { return v.end(); }
  713. static const_iterator end(const this_type &v) { return v.end(); }
  714. static origin_type* origin(this_type &v) { return &v; }
  715. static const origin_type* origin(const this_type &v) { return &v; }
  716. static void clear(origin_type* o, const iterator &, const iterator &)
  717. { o->clear(); }
  718. static void do_clear(this_type &v) { v.clear(); }
  719. static value_type access(const origin_type *o, const const_iterator &,
  720. const const_iterator &, size_type i)
  721. { return (*o)[i]; }
  722. static reference access(origin_type *o, const iterator &, const iterator &,
  723. size_type i)
  724. { return (*o)[i]; }
  725. static void resize(this_type &v, size_type n) { v.resize(n); }
  726. };
  727. template<typename T> std::ostream &operator <<
  728. (std::ostream &o, const wsvector<T>& v) { gmm::write(o,v); return o; }
  729. /******* Optimized BLAS for wsvector<T> **********************************/
  730. template <typename T> inline void copy(const wsvector<T> &v1,
  731. wsvector<T> &v2) {
  732. GMM_ASSERT2(vect_size(v1) == vect_size(v2), "dimensions mismatch");
  733. v2 = v1;
  734. }
  735. template <typename T> inline
  736. void copy(const wsvector<T> &v1, const simple_vector_ref<wsvector<T> *> &v2){
  737. simple_vector_ref<wsvector<T> *>
  738. *svr = const_cast<simple_vector_ref<wsvector<T> *> *>(&v2);
  739. wsvector<T>
  740. *pv = const_cast<wsvector<T> *>(v2.origin);
  741. GMM_ASSERT2(vect_size(v1) == vect_size(v2), "dimensions mismatch");
  742. *pv = v1; svr->begin_ = vect_begin(*pv); svr->end_ = vect_end(*pv);
  743. }
  744. template <typename T> inline
  745. void copy(const simple_vector_ref<const wsvector<T> *> &v1,
  746. wsvector<T> &v2)
  747. { copy(*(v1.origin), v2); }
  748. template <typename T> inline
  749. void copy(const simple_vector_ref<wsvector<T> *> &v1, wsvector<T> &v2)
  750. { copy(*(v1.origin), v2); }
  751. template <typename T> inline void clean(wsvector<T> &v, double eps) {
  752. typedef typename number_traits<T>::magnitude_type R;
  753. typename wsvector<T>::iterator it = v.begin(), ite = v.end(), itc;
  754. while (it != ite)
  755. if (gmm::abs((*it).second) <= R(eps))
  756. { itc=it; ++it; v.erase(itc); } else ++it;
  757. }
  758. template <typename T>
  759. inline void clean(const simple_vector_ref<wsvector<T> *> &l, double eps) {
  760. simple_vector_ref<wsvector<T> *>
  761. *svr = const_cast<simple_vector_ref<wsvector<T> *> *>(&l);
  762. wsvector<T>
  763. *pv = const_cast<wsvector<T> *>((l.origin));
  764. clean(*pv, eps);
  765. svr->begin_ = vect_begin(*pv); svr->end_ = vect_end(*pv);
  766. }
  767. template <typename T>
  768. inline size_type nnz(const wsvector<T>& l) { return l.nb_stored(); }
  769. /*************************************************************************/
  770. /* */
  771. /* rsvector: sparse vector optimized for linear algebra operations. */
  772. /* */
  773. /*************************************************************************/
  774. template<typename T> struct elt_rsvector_ {
  775. size_type c; T e;
  776. /* e is initialized by default to avoid some false warnings of valgrind.
  777. (from http://valgrind.org/docs/manual/mc-manual.html:
  778. When memory is read into the CPU's floating point registers, the
  779. relevant V bits are read from memory and they are immediately
  780. checked. If any are invalid, an uninitialised value error is
  781. emitted. This precludes using the floating-point registers to copy
  782. possibly-uninitialised memory, but simplifies Valgrind in that it
  783. does not have to track the validity status of the floating-point
  784. registers.
  785. */
  786. elt_rsvector_(void) : e(0) {}
  787. elt_rsvector_(size_type cc) : c(cc), e(0) {}
  788. elt_rsvector_(size_type cc, const T &ee) : c(cc), e(ee) {}
  789. bool operator < (const elt_rsvector_ &a) const { return c < a.c; }
  790. bool operator == (const elt_rsvector_ &a) const { return c == a.c; }
  791. bool operator != (const elt_rsvector_ &a) const { return c != a.c; }
  792. };
  793. template<typename T> struct rsvector_iterator {
  794. typedef typename std::vector<elt_rsvector_<T> >::iterator IT;
  795. typedef T value_type;
  796. typedef value_type* pointer;
  797. typedef value_type& reference;
  798. typedef size_t size_type;
  799. typedef ptrdiff_t difference_type;
  800. typedef std::bidirectional_iterator_tag iterator_category;
  801. typedef rsvector_iterator<T> iterator;
  802. IT it;
  803. reference operator *() const { return it->e; }
  804. pointer operator->() const { return &(operator*()); }
  805. iterator &operator ++() { ++it; return *this; }
  806. iterator operator ++(int) { iterator tmp = *this; ++(*this); return tmp; }
  807. iterator &operator --() { --it; return *this; }
  808. iterator operator --(int) { iterator tmp = *this; --(*this); return tmp; }
  809. bool operator ==(const iterator &i) const { return it == i.it; }
  810. bool operator !=(const iterator &i) const { return !(i == *this); }
  811. size_type index(void) const { return it->c; }
  812. rsvector_iterator(void) {}
  813. rsvector_iterator(const IT &i) : it(i) {}
  814. };
  815. template<typename T> struct rsvector_const_iterator {
  816. typedef typename std::vector<elt_rsvector_<T> >::const_iterator IT;
  817. typedef T value_type;
  818. typedef const value_type* pointer;
  819. typedef const value_type& reference;
  820. typedef size_t size_type;
  821. typedef ptrdiff_t difference_type;
  822. typedef std::forward_iterator_tag iterator_category;
  823. typedef rsvector_const_iterator<T> iterator;
  824. IT it;
  825. reference operator *() const { return it->e; }
  826. pointer operator->() const { return &(operator*()); }
  827. size_type index(void) const { return it->c; }
  828. iterator &operator ++() { ++it; return *this; }
  829. iterator operator ++(int) { iterator tmp = *this; ++(*this); return tmp; }
  830. iterator &operator --() { --it; return *this; }
  831. iterator operator --(int) { iterator tmp = *this; --(*this); return tmp; }
  832. bool operator ==(const iterator &i) const { return it == i.it; }
  833. bool operator !=(const iterator &i) const { return !(i == *this); }
  834. rsvector_const_iterator(void) {}
  835. rsvector_const_iterator(const rsvector_iterator<T> &i) : it(i.it) {}
  836. rsvector_const_iterator(const IT &i) : it(i) {}
  837. };
  838. /**
  839. sparse vector built upon std::vector. Read access is fast,
  840. but insertion is O(n)
  841. */
  842. template<typename T> class rsvector : public std::vector<elt_rsvector_<T> > {
  843. public:
  844. typedef std::vector<elt_rsvector_<T> > base_type_;
  845. typedef typename base_type_::iterator iterator;
  846. typedef typename base_type_::const_iterator const_iterator;
  847. typedef typename base_type_::size_type size_type;
  848. typedef T value_type;
  849. protected:
  850. size_type nbl; /* size of the vector. */
  851. public:
  852. void sup(size_type j);
  853. void base_resize(size_type n) { base_type_::resize(n); }
  854. void resize(size_type);
  855. ref_elt_vector<T, rsvector<T> > operator [](size_type c)
  856. { return ref_elt_vector<T, rsvector<T> >(this, c); }
  857. void w(size_type c, const T &e);
  858. void wa(size_type c, const T &e);
  859. T r(size_type c) const;
  860. void swap_indices(size_type i, size_type j);
  861. inline T operator [](size_type c) const { return r(c); }
  862. size_type nb_stored(void) const { return base_type_::size(); }
  863. size_type size(void) const { return nbl; }
  864. void clear(void) { base_type_::resize(0); }
  865. void swap(rsvector<T> &v)
  866. { std::swap(nbl, v.nbl); std::vector<elt_rsvector_<T> >::swap(v); }
  867. /* Constructeurs */
  868. explicit rsvector(size_type l) : nbl(l) { }
  869. rsvector(void) : nbl(0) { }
  870. };
  871. template <typename T>
  872. void rsvector<T>::swap_indices(size_type i, size_type j) {
  873. if (i > j) std::swap(i, j);
  874. if (i != j) {
  875. int situation = 0;
  876. elt_rsvector_<T> ei(i), ej(j), a;
  877. iterator it, ite, iti, itj;
  878. iti = std::lower_bound(this->begin(), this->end(), ei);
  879. if (iti != this->end() && iti->c == i) situation += 1;
  880. itj = std::lower_bound(this->begin(), this->end(), ej);
  881. if (itj != this->end() && itj->c == j) situation += 2;
  882. switch (situation) {
  883. case 1 : a = *iti; a.c = j; it = iti; ++it; ite = this->end();
  884. for (; it != ite && it->c <= j; ++it, ++iti) *iti = *it;
  885. *iti = a;
  886. break;
  887. case 2 : a = *itj; a.c = i; it = itj; ite = this->begin();
  888. if (it != ite) {
  889. --it;
  890. while (it->c >= i) { *itj = *it; --itj; if (it==ite) break; --it; }
  891. }
  892. *itj = a;
  893. break;
  894. case 3 : std::swap(iti->e, itj->e);
  895. break;
  896. }
  897. }
  898. }
  899. template <typename T> void rsvector<T>::sup(size_type j) {
  900. if (nb_stored() != 0) {
  901. elt_rsvector_<T> ev(j);
  902. iterator it = std::lower_bound(this->begin(), this->end(), ev);
  903. if (it != this->end() && it->c == j) {
  904. for (iterator ite = this->end() - 1; it != ite; ++it) *it = *(it+1);
  905. base_resize(nb_stored()-1);
  906. }
  907. }
  908. }
  909. template<typename T> void rsvector<T>::resize(size_type n) {
  910. if (n < nbl) {
  911. for (size_type i = 0; i < nb_stored(); ++i)
  912. if (base_type_::operator[](i).c >= n) { base_resize(i); break; }
  913. }
  914. nbl = n;
  915. }
  916. template <typename T> void rsvector<T>::w(size_type c, const T &e) {
  917. GMM_ASSERT2(c < nbl, "out of range");
  918. if (e == T(0)) sup(c);
  919. else {
  920. elt_rsvector_<T> ev(c, e);
  921. if (nb_stored() == 0) {
  922. base_type_::push_back(ev);
  923. }
  924. else {
  925. iterator it = std::lower_bound(this->begin(), this->end(), ev);
  926. if (it != this->end() && it->c == c) it->e = e;
  927. else {
  928. size_type ind = it - this->begin(), nb = this->nb_stored();
  929. if (nb - ind > 1100)
  930. GMM_WARNING2("Inefficient addition of element in rsvector with "
  931. << this->nb_stored() - ind << " non-zero entries");
  932. base_type_::push_back(ev);
  933. if (ind != nb) {
  934. it = this->begin() + ind;
  935. iterator ite = this->end(); --ite; iterator itee = ite;
  936. for (; ite != it; --ite) { --itee; *ite = *itee; }
  937. *it = ev;
  938. }
  939. }
  940. }
  941. }
  942. }
  943. template <typename T> void rsvector<T>::wa(size_type c, const T &e) {
  944. GMM_ASSERT2(c < nbl, "out of range");
  945. if (e != T(0)) {
  946. elt_rsvector_<T> ev(c, e);
  947. if (nb_stored() == 0) {
  948. base_type_::push_back(ev);
  949. }
  950. else {
  951. iterator it = std::lower_bound(this->begin(), this->end(), ev);
  952. if (it != this->end() && it->c == c) it->e += e;
  953. else {
  954. size_type ind = it - this->begin(), nb = this->nb_stored();
  955. if (nb - ind > 1100)
  956. GMM_WARNING2("Inefficient addition of element in rsvector with "
  957. << this->nb_stored() - ind << " non-zero entries");
  958. base_type_::push_back(ev);
  959. if (ind != nb) {
  960. it = this->begin() + ind;
  961. iterator ite = this->end(); --ite; iterator itee = ite;
  962. for (; ite != it; --ite) { --itee; *ite = *itee; }
  963. *it = ev;
  964. }
  965. }
  966. }
  967. }
  968. }
  969. template <typename T> T rsvector<T>::r(size_type c) const {
  970. GMM_ASSERT2(c < nbl, "out of range. Index " << c
  971. << " for a length of " << nbl);
  972. if (nb_stored() != 0) {
  973. elt_rsvector_<T> ev(c);
  974. const_iterator it = std::lower_bound(this->begin(), this->end(), ev);
  975. if (it != this->end() && it->c == c) return it->e;
  976. }
  977. return T(0);
  978. }
  979. template <typename T> struct linalg_traits<rsvector<T> > {
  980. typedef rsvector<T> this_type;
  981. typedef this_type origin_type;
  982. typedef linalg_false is_reference;
  983. typedef abstract_vector linalg_type;
  984. typedef T value_type;
  985. typedef ref_elt_vector<T, rsvector<T> > reference;
  986. typedef rsvector_iterator<T> iterator;
  987. typedef rsvector_const_iterator<T> const_iterator;
  988. typedef abstract_sparse storage_type;
  989. typedef linalg_true index_sorted;
  990. static size_type size(const this_type &v) { return v.size(); }
  991. static iterator begin(this_type &v) { return iterator(v.begin()); }
  992. static const_iterator begin(const this_type &v)
  993. { return const_iterator(v.begin()); }
  994. static iterator end(this_type &v) { return iterator(v.end()); }
  995. static const_iterator end(const this_type &v)
  996. { return const_iterator(v.end()); }
  997. static origin_type* origin(this_type &v) { return &v; }
  998. static const origin_type* origin(const this_type &v) { return &v; }
  999. static void clear(origin_type* o, const iterator &, const iterator &)
  1000. { o->clear(); }
  1001. static void do_clear(this_type &v) { v.clear(); }
  1002. static value_type access(const origin_type *o, const const_iterator &,
  1003. const const_iterator &, size_type i)
  1004. { return (*o)[i]; }
  1005. static reference access(origin_type *o, const iterator &, const iterator &,
  1006. size_type i)
  1007. { return (*o)[i]; }
  1008. static void resize(this_type &v, size_type n) { v.resize(n); }
  1009. };
  1010. template<typename T> std::ostream &operator <<
  1011. (std::ostream &o, const rsvector<T>& v) { gmm::write(o,v); return o; }
  1012. /******* Optimized operations for rsvector<T> ****************************/
  1013. template <typename T> inline void copy(const rsvector<T> &v1,
  1014. rsvector<T> &v2) {
  1015. GMM_ASSERT2(vect_size(v1) == vect_size(v2), "dimensions mismatch");
  1016. v2 = v1;
  1017. }
  1018. template <typename T> inline
  1019. void copy(const rsvector<T> &v1, const simple_vector_ref<rsvector<T> *> &v2){
  1020. simple_vector_ref<rsvector<T> *>
  1021. *svr = const_cast<simple_vector_ref<rsvector<T> *> *>(&v2);
  1022. rsvector<T>
  1023. *pv = const_cast<rsvector<T> *>((v2.origin));
  1024. GMM_ASSERT2(vect_size(v1) == vect_size(v2), "dimensions mismatch");
  1025. *pv = v1; svr->begin_ = vect_begin(*pv); svr->end_ = vect_end(*pv);
  1026. }
  1027. template <typename T> inline
  1028. void copy(const simple_vector_ref<const rsvector<T> *> &v1,
  1029. rsvector<T> &v2)
  1030. { copy(*(v1.origin), v2); }
  1031. template <typename T> inline
  1032. void copy(const simple_vector_ref<rsvector<T> *> &v1, rsvector<T> &v2)
  1033. { copy(*(v1.origin), v2); }
  1034. template <typename V, typename T> inline void add(const V &v1,
  1035. rsvector<T> &v2) {
  1036. if ((const void *)(&v1) != (const void *)(&v2)) {
  1037. GMM_ASSERT2(vect_size(v1) == vect_size(v2), "dimensions mismatch");
  1038. add_rsvector(v1, v2, typename linalg_traits<V>::storage_type());
  1039. }
  1040. }
  1041. template <typename V, typename T>
  1042. inline void add_rsvector(const V &v1, rsvector<T> &v2, abstract_dense)
  1043. { add(v1, v2, abstract_dense(), abstract_sparse()); }
  1044. template <typename V, typename T>
  1045. inline void add_rsvector(const V &v1, rsvector<T> &v2, abstract_skyline)
  1046. { add(v1, v2, abstract_skyline(), abstract_sparse()); }
  1047. template <typename V, typename T>
  1048. void add_rsvector(const V &v1, rsvector<T> &v2, abstract_sparse) {
  1049. add_rsvector(v1, v2, typename linalg_traits<V>::index_sorted());
  1050. }
  1051. template <typename V, typename T>
  1052. void add_rsvector(const V &v1, rsvector<T> &v2, linalg_false) {
  1053. add(v1, v2, abstract_sparse(), abstract_sparse());
  1054. }
  1055. template <typename V, typename T>
  1056. void add_rsvector(const V &v1, rsvector<T> &v2, linalg_true) {
  1057. typename linalg_traits<V>::const_iterator it1 = vect_const_begin(v1),
  1058. ite1 = vect_const_end(v1);
  1059. typename rsvector<T>::iterator it2 = v2.begin(), ite2 = v2.end(), it3;
  1060. size_type nbc = 0, old_nbc = v2.nb_stored();
  1061. for (; it1 != ite1 && it2 != ite2 ; ++nbc)
  1062. if (it1.index() == it2->c) { ++it1; ++it2; }
  1063. else if (it1.index() < it2->c) ++it1; else ++it2;
  1064. for (; it1 != ite1; ++it1) ++nbc;
  1065. for (; it2 != ite2; ++it2) ++nbc;
  1066. v2.base_resize(nbc);
  1067. it3 = v2.begin() + old_nbc;
  1068. it2 = v2.end(); ite2 = v2.begin();
  1069. it1 = vect_end(v1); ite1 = vect_const_begin(v1);
  1070. while (it1 != ite1 && it3 != ite2) {
  1071. --it3; --it1; --it2;
  1072. if (it3->c > it1.index()) { *it2 = *it3; ++it1; }
  1073. else if (it3->c == it1.index()) { *it2=*it3; it2->e+=*it1; }
  1074. else { it2->c = it1.index(); it2->e = *it1; ++it3; }
  1075. }
  1076. while (it1 != ite1) { --it1; --it2; it2->c = it1.index(); it2->e = *it1; }
  1077. }
  1078. template <typename V, typename T> void copy(const V &v1, rsvector<T> &v2) {
  1079. if ((const void *)(&v1) != (const void *)(&v2)) {
  1080. GMM_ASSERT2(vect_size(v1) == vect_size(v2), "dimensions mismatch");
  1081. if (same_origin(v1, v2))
  1082. GMM_WARNING2("a conflict is possible in vector copy\n");
  1083. copy_rsvector(v1, v2, typename linalg_traits<V>::storage_type());
  1084. }
  1085. }
  1086. template <typename V, typename T>
  1087. void copy_rsvector(const V &v1, rsvector<T> &v2, abstract_dense)
  1088. { copy_vect(v1, v2, abstract_dense(), abstract_sparse()); }
  1089. template <typename V, typename T>
  1090. void copy_rsvector(const V &v1, rsvector<T> &v2, abstract_skyline)
  1091. { copy_vect(v1, v2, abstract_skyline(), abstract_sparse()); }
  1092. template <typename V, typename T>
  1093. void copy_rsvector(const V &v1, rsvector<T> &v2, abstract_sparse) {
  1094. copy_rsvector(v1, v2, typename linalg_traits<V>::index_sorted());
  1095. }
  1096. template <typename V, typename T2>
  1097. void copy_rsvector(const V &v1, rsvector<T2> &v2, linalg_true) {
  1098. typedef typename linalg_traits<V>::value_type T1;
  1099. typename linalg_traits<V>::const_iterator it = vect_const_begin(v1),
  1100. ite = vect_const_end(v1);
  1101. v2.base_resize(nnz(v1));
  1102. typename rsvector<T2>::iterator it2 = v2.begin();
  1103. size_type nn = 0;
  1104. for (; it != ite; ++it)
  1105. if ((*it) != T1(0)) { it2->c = it.index(); it2->e = *it; ++it2; ++nn; }
  1106. v2.base_resize(nn);
  1107. }
  1108. template <typename V, typename T2>
  1109. void copy_rsvector(const V &v1, rsvector<T2> &v2, linalg_false) {
  1110. typedef typename linalg_traits<V>::value_type T1;
  1111. typename linalg_traits<V>::const_iterator it = vect_const_begin(v1),
  1112. ite = vect_const_end(v1);
  1113. v2.base_resize(nnz(v1));
  1114. typename rsvector<T2>::iterator it2 = v2.begin();
  1115. size_type nn = 0;
  1116. for (; it != ite; ++it)
  1117. if ((*it) != T1(0)) { it2->c = it.index(); it2->e = *it; ++it2; ++nn; }
  1118. v2.base_resize(nn);
  1119. std::sort(v2.begin(), v2.end());
  1120. }
  1121. template <typename T> inline void clean(rsvector<T> &v, double eps) {
  1122. typedef typename number_traits<T>::magnitude_type R;
  1123. typename rsvector<T>::iterator it = v.begin(), ite = v.end();
  1124. for (; it != ite; ++it) if (gmm::abs((*it).e) <= eps) break;
  1125. if (it != ite) {
  1126. typename rsvector<T>::iterator itc = it;
  1127. size_type erased = 1;
  1128. for (++it; it != ite; ++it)
  1129. { *itc = *it; if (gmm::abs((*it).e) <= R(eps)) ++erased; else ++itc; }
  1130. v.base_resize(v.nb_stored() - erased);
  1131. }
  1132. }
  1133. template <typename T>
  1134. inline void clean(const simple_vector_ref<rsvector<T> *> &l, double eps) {
  1135. simple_vector_ref<rsvector<T> *>
  1136. *svr = const_cast<simple_vector_ref<rsvector<T> *> *>(&l);
  1137. rsvector<T>
  1138. *pv = const_cast<rsvector<T> *>((l.origin));
  1139. clean(*pv, eps);
  1140. svr->begin_ = vect_begin(*pv); svr->end_ = vect_end(*pv);
  1141. }
  1142. template <typename T>
  1143. inline size_type nnz(const rsvector<T>& l) { return l.nb_stored(); }
  1144. /*************************************************************************/
  1145. /* */
  1146. /* Class slvector: 'sky-line' vector. */
  1147. /* */
  1148. /*************************************************************************/
  1149. template<typename T> struct slvector_iterator {
  1150. typedef T value_type;
  1151. typedef T *pointer;
  1152. typedef T &reference;
  1153. typedef ptrdiff_t difference_type;
  1154. typedef std::random_access_iterator_tag iterator_category;
  1155. typedef size_t size_type;
  1156. typedef slvector_iterator<T> iterator;
  1157. typedef typename std::vector<T>::iterator base_iterator;
  1158. base_iterator it;
  1159. size_type shift;
  1160. iterator &operator ++()
  1161. { ++it; ++shift; return *this; }
  1162. iterator &operator --()
  1163. { --it; --shift; return *this; }
  1164. iterator operator ++(int)
  1165. { iterator tmp = *this; ++(*(this)); return tmp; }
  1166. iterator operator --(int)
  1167. { iterator tmp = *this; --(*(this)); return tmp; }
  1168. iterator &operator +=(difference_type i)
  1169. { it += i; shift += i; return *this; }
  1170. iterator &operator -=(difference_type i)
  1171. { it -= i; shift -= i; return *this; }
  1172. iterator operator +(difference_type i) const
  1173. { iterator tmp = *this; return (tmp += i); }
  1174. iterator operator -(difference_type i) const
  1175. { iterator tmp = *this; return (tmp -= i); }
  1176. difference_type operator -(const iterator &i) const
  1177. { return it - i.it; }
  1178. reference operator *() const
  1179. { return *it; }
  1180. reference operator [](int ii)
  1181. { return *(it + ii); }
  1182. bool operator ==(const iterator &i) const
  1183. { return it == i.it; }
  1184. bool operator !=(const iterator &i) const
  1185. { return !(i == *this); }
  1186. bool operator < (const iterator &i) const
  1187. { return it < i.it; }
  1188. size_type index(void) const { return shift; }
  1189. slvector_iterator(void) {}
  1190. slvector_iterator(const base_iterator &iter, size_type s)
  1191. : it(iter), shift(s) {}
  1192. };
  1193. template<typename T> struct slvector_const_iterator {
  1194. typedef T value_type;
  1195. typedef const T *pointer;
  1196. typedef value_type reference;
  1197. typedef ptrdiff_t difference_type;
  1198. typedef std::random_access_iterator_tag iterator_category;
  1199. typedef size_t size_type;
  1200. typedef slvector_const_iterator<T> iterator;
  1201. typedef typename std::vector<T>::const_iterator base_iterator;
  1202. base_iterator it;
  1203. size_type shift;
  1204. iterator &operator ++()
  1205. { ++it; ++shift; return *this; }
  1206. iterator &operator --()
  1207. { --it; --shift; return *this; }
  1208. iterator operator ++(int)
  1209. { iterator tmp = *this; ++(*(this)); return tmp; }
  1210. iterator operator --(int)
  1211. { iterator tmp = *this; --(*(this)); return tmp; }
  1212. iterator &operator +=(difference_type i)
  1213. { it += i; shift += i; return *this; }
  1214. iterator &operator -=(difference_type i)
  1215. { it -= i; shift -= i; return *this; }
  1216. iterator operator +(difference_type i) const
  1217. { iterator tmp = *this; return (tmp += i); }
  1218. iterator operator -(difference_type i) const
  1219. { iterator tmp = *this; return (tmp -= i); }
  1220. difference_type operator -(const iterator &i) const
  1221. { return it - i.it; }
  1222. value_type operator *() const
  1223. { return *it; }
  1224. value_type operator [](int ii)
  1225. { return *(it + ii); }
  1226. bool operator ==(const iterator &i) const
  1227. { return it == i.it; }
  1228. bool operator !=(const iterator &i) const
  1229. { return !(i == *this); }
  1230. bool operator < (const iterator &i) const
  1231. { return it < i.it; }
  1232. size_type index(void) const { return shift; }
  1233. slvector_const_iterator(void) {}
  1234. slvector_const_iterator(const slvector_iterator<T>& iter)
  1235. : it(iter.it), shift(iter.shift) {}
  1236. slvector_const_iterator(const base_iterator &iter, size_type s)
  1237. : it(iter), shift(s) {}
  1238. };
  1239. /** skyline vector.
  1240. */
  1241. template <typename T> class slvector {
  1242. public :
  1243. typedef slvector_iterator<T> iterators;
  1244. typedef slvector_const_iterator<T> const_iterators;
  1245. typedef typename std::vector<T>::size_type size_type;
  1246. typedef T value_type;
  1247. protected :
  1248. std::vector<T> data;
  1249. size_type shift;
  1250. size_type size_;
  1251. public :
  1252. size_type size(void) const { return size_; }
  1253. size_type first(void) const { return shift; }
  1254. size_type last(void) const { return shift + data.size(); }
  1255. ref_elt_vector<T, slvector<T> > operator [](size_type c)
  1256. { return ref_elt_vector<T, slvector<T> >(this, c); }
  1257. typename std::vector<T>::iterator data_begin(void) { return data.begin(); }
  1258. typename std::vector<T>::iterator data_end(void) { return data.end(); }
  1259. typename std::vector<T>::const_iterator data_begin(void) const
  1260. { return data.begin(); }
  1261. typename std::vector<T>::const_iterator data_end(void) const
  1262. { return data.end(); }
  1263. void w(size_type c, const T &e);
  1264. void wa(size_type c, const T &e);
  1265. T r(size_type c) const {
  1266. GMM_ASSERT2(c < size_, "out of range");
  1267. if (c < shift || c >= shift + data.size()) return T(0);
  1268. return data[c - shift];
  1269. }
  1270. inline T operator [](size_type c) const { return r(c); }
  1271. void resize(size_type);
  1272. void clear(void) { data.resize(0); shift = 0; }
  1273. void swap(slvector<T> &v) {
  1274. std::swap(data, v.data);
  1275. std::swap(shift, v.shift);
  1276. std::swap(size_, v.size_);
  1277. }
  1278. slvector(void) : data(0), shift(0), size_(0) {}
  1279. explicit slvector(size_type l) : data(0), shift(0), size_(l) {}
  1280. slvector(size_type l, size_type d, size_type s)
  1281. : data(d), shift(s), size_(l) {}
  1282. };
  1283. template<typename T> void slvector<T>::resize(size_type n) {
  1284. if (n < last()) {
  1285. if (shift >= n) clear(); else { data.resize(n-shift); }
  1286. }
  1287. size_ = n;
  1288. }
  1289. template<typename T> void slvector<T>::w(size_type c, const T &e) {
  1290. GMM_ASSERT2(c < size_, "out of range");
  1291. size_type s = data.size();
  1292. if (!s) { data.resize(1); shift = c; }
  1293. else if (c < shift) {
  1294. data.resize(s + shift - c);
  1295. typename std::vector<T>::iterator it = data.begin(),it2=data.end()-1;
  1296. typename std::vector<T>::iterator it3 = it2 - shift + c;
  1297. for (; it3 >= it; --it3, --it2) *it2 = *it3;
  1298. std::fill(it, it + shift - c, T(0));
  1299. shift = c;
  1300. }
  1301. else if (c >= shift + s) {
  1302. data.resize(c - shift + 1, T(0));
  1303. // std::fill(data.begin() + s, data.end(), T(0));
  1304. }
  1305. data[c - shift] = e;
  1306. }
  1307. template<typename T> void slvector<T>::wa(size_type c, const T &e) {
  1308. GMM_ASSERT2(c < size_, "out of range");
  1309. size_type s = data.size();
  1310. if (!s) { data.resize(1, e); shift = c; return; }
  1311. else if (c < shift) {
  1312. data.resize(s + shift - c);
  1313. typename std::vector<T>::iterator it = data.begin(),it2=data.end()-1;
  1314. typename std::vector<T>::iterator it3 = it2 - shift + c;
  1315. for (; it3 >= it; --it3, --it2) *it2 = *it3;
  1316. std::fill(it, it + shift - c, T(0));
  1317. shift = c;
  1318. data[c - shift] = e;
  1319. return;
  1320. }
  1321. else if (c >= shift + s) {
  1322. data.resize(c - shift + 1, T(0));
  1323. data[c - shift] = e;
  1324. return;
  1325. // std::fill(data.begin() + s, data.end(), T(0));
  1326. }
  1327. data[c - shift] += e;
  1328. }
  1329. template <typename T> struct linalg_traits<slvector<T> > {
  1330. typedef slvector<T> this_type;
  1331. typedef this_type origin_type;
  1332. typedef linalg_false is_reference;
  1333. typedef abstract_vector linalg_type;
  1334. typedef T value_type;
  1335. typedef ref_elt_vector<T, slvector<T> > reference;
  1336. typedef slvector_iterator<T> iterator;
  1337. typedef slvector_const_iterator<T> const_iterator;
  1338. typedef abstract_skyline storage_type;
  1339. typedef linalg_true index_sorted;
  1340. static size_type size(const this_type &v) { return v.size(); }
  1341. static iterator begin(this_type &v)
  1342. { return iterator(v.data_begin(), v.first()); }
  1343. static const_iterator begin(const this_type &v)
  1344. { return const_iterator(v.data_begin(), v.first()); }
  1345. static iterator end(this_type &v)
  1346. { return iterator(v.data_end(), v.last()); }
  1347. static const_iterator end(const this_type &v)
  1348. { return const_iterator(v.data_end(), v.last()); }
  1349. static origin_type* origin(this_type &v) { return &v; }
  1350. static const origin_type* origin(const this_type &v) { return &v; }
  1351. static void clear(origin_type* o, const iterator &, const iterator &)
  1352. { o->clear(); }
  1353. static void do_clear(this_type &v) { v.clear(); }
  1354. static value_type access(const origin_type *o, const const_iterator &,
  1355. const const_iterator &, size_type i)
  1356. { return (*o)[i]; }
  1357. static reference access(origin_type *o, const iterator &, const iterator &,
  1358. size_type i)
  1359. { return (*o)[i]; }
  1360. static void resize(this_type &v, size_type n) { v.resize(n); }
  1361. };
  1362. template<typename T> std::ostream &operator <<
  1363. (std::ostream &o, const slvector<T>& v) { gmm::write(o,v); return o; }
  1364. template <typename T>
  1365. inline size_type nnz(const slvector<T>& l) { return l.last() - l.first(); }
  1366. }
  1367. namespace std {
  1368. template <typename T> void swap(gmm::wsvector<T> &v, gmm::wsvector<T> &w)
  1369. { v.swap(w);}
  1370. template <typename T> void swap(gmm::rsvector<T> &v, gmm::rsvector<T> &w)
  1371. { v.swap(w);}
  1372. template <typename T> void swap(gmm::slvector<T> &v, gmm::slvector<T> &w)
  1373. { v.swap(w);}
  1374. }
  1375. #endif /* GMM_VECTOR_H__ */