37 #ifndef GMM_VECTOR_H__ 38 #define GMM_VECTOR_H__ 52 template<
typename T,
typename V>
class ref_elt_vector {
59 operator T()
const {
return pm->r(l); }
60 ref_elt_vector(V *p,
size_type ll) : pm(p), l(ll) {}
61 inline bool operator ==(T v)
const {
return ((*pm).r(l) == v); }
62 inline bool operator !=(T v)
const {
return ((*pm).r(l) != v); }
63 inline bool operator ==(std::complex<T> v)
const 64 {
return ((*pm).r(l) == v); }
65 inline bool operator !=(std::complex<T> v)
const 66 {
return ((*pm).r(l) != v); }
67 inline ref_elt_vector &operator +=(T v)
68 { (*pm).wa(l, v);
return *
this; }
69 inline ref_elt_vector &operator -=(T v)
70 { (*pm).wa(l, -v);
return *
this; }
71 inline ref_elt_vector &operator /=(T v)
72 { (*pm).w(l,(*pm).r(l) / v);
return *
this; }
73 inline ref_elt_vector &operator *=(T v)
74 { (*pm).w(l,(*pm).r(l) * v);
return *
this; }
75 inline ref_elt_vector &operator =(
const ref_elt_vector &re)
76 { *
this = T(re);
return *
this; }
77 inline ref_elt_vector &operator =(T v)
78 { (*pm).w(l,v);
return *
this; }
79 T operator +() {
return T(*
this); }
80 T operator -() {
return -T(*
this); }
81 T operator +(T v) {
return T(*
this)+ v; }
82 T operator -(T v) {
return T(*
this)- v; }
83 T operator *(T v) {
return T(*
this)* v; }
84 T operator /(T v) {
return T(*
this)/ v; }
85 std::complex<T> operator +(std::complex<T> v) {
return T(*
this)+ v; }
86 std::complex<T> operator -(std::complex<T> v) {
return T(*
this)- v; }
87 std::complex<T> operator *(std::complex<T> v) {
return T(*
this)* v; }
88 std::complex<T> operator /(std::complex<T> v) {
return T(*
this)/ v; }
91 template<
typename T,
typename V>
class ref_elt_vector<
std::complex<T>,V> {
98 operator std::complex<T>()
const {
return pm->r(l); }
99 ref_elt_vector(V *p,
size_type ll) : pm(p), l(ll) {}
100 inline bool operator ==(std::complex<T> v)
const 101 {
return ((*pm).r(l) == v); }
102 inline bool operator !=(std::complex<T> v)
const 103 {
return ((*pm).r(l) != v); }
104 inline bool operator ==(T v)
const {
return ((*pm).r(l) == v); }
105 inline bool operator !=(T v)
const {
return ((*pm).r(l) != v); }
106 inline ref_elt_vector &operator +=(std::complex<T> v)
107 { (*pm).w(l,(*pm).r(l) + v);
return *
this; }
108 inline ref_elt_vector &operator -=(std::complex<T> v)
109 { (*pm).w(l,(*pm).r(l) - v);
return *
this; }
110 inline ref_elt_vector &operator /=(std::complex<T> v)
111 { (*pm).w(l,(*pm).r(l) / v);
return *
this; }
112 inline ref_elt_vector &operator *=(std::complex<T> v)
113 { (*pm).w(l,(*pm).r(l) * v);
return *
this; }
114 inline ref_elt_vector &operator =(
const ref_elt_vector &re)
115 { *
this = T(re);
return *
this; }
116 inline ref_elt_vector &operator =(std::complex<T> v)
117 { (*pm).w(l,v);
return *
this; }
118 inline ref_elt_vector &operator =(T v)
119 { (*pm).w(l,std::complex<T>(v));
return *
this; }
120 inline ref_elt_vector &operator +=(T v)
121 { (*pm).w(l,(*pm).r(l) + v);
return *
this; }
122 inline ref_elt_vector &operator -=(T v)
123 { (*pm).w(l,(*pm).r(l) - v);
return *
this; }
124 inline ref_elt_vector &operator /=(T v)
125 { (*pm).w(l,(*pm).r(l) / v);
return *
this; }
126 inline ref_elt_vector &operator *=(T v)
127 { (*pm).w(l,(*pm).r(l) * v);
return *
this; }
128 std::complex<T> operator +() {
return std::complex<T>(*this); }
129 std::complex<T> operator -() {
return -std::complex<T>(*this); }
130 std::complex<T> operator +(T v) {
return std::complex<T>(*this)+ v; }
131 std::complex<T> operator -(T v) {
return std::complex<T>(*this)- v; }
132 std::complex<T> operator *(T v) {
return std::complex<T>(*this)* v; }
133 std::complex<T> operator /(T v) {
return std::complex<T>(*this)/ v; }
134 std::complex<T> operator +(std::complex<T> v)
135 {
return std::complex<T>(*this)+ v; }
136 std::complex<T> operator -(std::complex<T> v)
137 {
return std::complex<T>(*this)- v; }
138 std::complex<T> operator *(std::complex<T> v)
139 {
return std::complex<T>(*this)* v; }
140 std::complex<T> operator /(std::complex<T> v)
141 {
return std::complex<T>(*this)/ v; }
145 template<
typename T,
typename V>
inline 146 bool operator ==(T v,
const ref_elt_vector<T, V> &re) {
return (v==T(re)); }
147 template<
typename T,
typename V>
inline 148 bool operator !=(T v,
const ref_elt_vector<T, V> &re) {
return (v!=T(re)); }
149 template<
typename T,
typename V>
inline 150 T &operator +=(T &v,
const ref_elt_vector<T, V> &re)
151 { v += T(re);
return v; }
152 template<
typename T,
typename V>
inline 153 T &operator -=(T &v,
const ref_elt_vector<T, V> &re)
154 { v -= T(re);
return v; }
155 template<
typename T,
typename V>
inline 156 T &operator *=(T &v,
const ref_elt_vector<T, V> &re)
157 { v *= T(re);
return v; }
158 template<
typename T,
typename V>
inline 159 T &operator /=(T &v,
const ref_elt_vector<T, V> &re)
160 { v /= T(re);
return v; }
161 template<
typename T,
typename V>
inline 162 T operator +(T v,
const ref_elt_vector<T, V> &re) {
return v+ T(re); }
163 template<
typename T,
typename V>
inline 164 T operator -(T v,
const ref_elt_vector<T, V> &re) {
return v- T(re); }
165 template<
typename T,
typename V>
inline 166 T operator *(T v,
const ref_elt_vector<T, V> &re) {
return v* T(re); }
167 template<
typename T,
typename V>
inline 168 T operator /(T v,
const ref_elt_vector<T, V> &re) {
return v/ T(re); }
169 template<
typename T,
typename V>
inline 170 std::complex<T> operator +(std::complex<T> v,
const ref_elt_vector<T, V> &re)
172 template<
typename T,
typename V>
inline 173 std::complex<T> operator -(std::complex<T> v,
const ref_elt_vector<T, V> &re)
175 template<
typename T,
typename V>
inline 176 std::complex<T> operator *(std::complex<T> v,
const ref_elt_vector<T, V> &re)
178 template<
typename T,
typename V>
inline 179 std::complex<T> operator /(std::complex<T> v,
const ref_elt_vector<T, V> &re)
181 template<
typename T,
typename V>
inline 182 std::complex<T> operator +(T v,
const ref_elt_vector<std::complex<T>, V> &re)
183 {
return v+ std::complex<T>(re); }
184 template<
typename T,
typename V>
inline 185 std::complex<T> operator -(T v,
const ref_elt_vector<std::complex<T>, V> &re)
186 {
return v- std::complex<T>(re); }
187 template<
typename T,
typename V>
inline 188 std::complex<T> operator *(T v,
const ref_elt_vector<std::complex<T>, V> &re)
189 {
return v* std::complex<T>(re); }
190 template<
typename T,
typename V>
inline 191 std::complex<T> operator /(T v,
const ref_elt_vector<std::complex<T>, V> &re)
192 {
return v/ std::complex<T>(re); }
193 template<
typename T,
typename V>
inline 194 typename number_traits<T>::magnitude_type
195 abs(
const ref_elt_vector<T, V> &re) {
return gmm::abs(T(re)); }
196 template<
typename T,
typename V>
inline 197 T sqr(
const ref_elt_vector<T, V> &re) {
return gmm::sqr(T(re)); }
198 template<
typename T,
typename V>
inline 199 typename number_traits<T>::magnitude_type
200 abs_sqr(
const ref_elt_vector<T, V> &re) {
return gmm::abs_sqr(T(re)); }
201 template<
typename T,
typename V>
inline 202 T conj(
const ref_elt_vector<T, V> &re) {
return gmm::conj(T(re)); }
203 template<
typename T,
typename V> std::ostream &
operator <<
204 (std::ostream &o,
const ref_elt_vector<T, V> &re) { o << T(re);
return o; }
205 template<
typename T,
typename V>
inline 206 typename number_traits<T>::magnitude_type
207 real(
const ref_elt_vector<T, V> &re) {
return gmm::real(T(re)); }
208 template<
typename T,
typename V>
inline 209 typename number_traits<T>::magnitude_type
210 imag(
const ref_elt_vector<T, V> &re) {
return gmm::imag(T(re)); }
221 template<
typename T>
class dsvector;
223 template<
typename T>
struct dsvector_iterator {
228 typedef T value_type;
229 typedef value_type* pointer;
230 typedef const value_type* const_pointer;
231 typedef value_type& reference;
233 typedef ptrdiff_t difference_type;
234 typedef std::bidirectional_iterator_tag iterator_category;
235 typedef dsvector_iterator<T> iterator;
237 reference operator *()
const {
return *p; }
238 pointer operator->()
const {
return &(operator*()); }
240 iterator &operator ++() {
241 for (
size_type k = (i & 15); k < 15; ++k)
242 { ++p; ++i;
if (*p != T(0))
return *
this; }
243 v->next_pos(*(const_cast<const_pointer *>(&(p))), i);
246 iterator operator ++(
int) { iterator tmp = *
this; ++(*this);
return tmp; }
247 iterator &operator --() {
249 { --p; --i;
if (*p != T(0))
return *
this; }
250 v->previous_pos(p, i);
253 iterator operator --(
int) { iterator tmp = *
this; --(*this);
return tmp; }
255 bool operator ==(
const iterator &it)
const 256 {
return (i == it.i && p == it.p && v == it.v); }
257 bool operator !=(
const iterator &it)
const 258 {
return !(it == *
this); }
260 size_type index(
void)
const {
return i; }
262 dsvector_iterator(
void) : i(
size_type(-1)), p(0), v(0) {}
263 dsvector_iterator(dsvector<T> &w) : i(
size_type(-1)), p(0), v(&w) {};
267 template<
typename T>
struct dsvector_const_iterator {
270 const dsvector<T> *v;
272 typedef T value_type;
273 typedef const value_type* pointer;
274 typedef const value_type& reference;
276 typedef ptrdiff_t difference_type;
277 typedef std::bidirectional_iterator_tag iterator_category;
278 typedef dsvector_const_iterator<T> iterator;
280 reference operator *()
const {
return *p; }
281 pointer operator->()
const {
return &(operator*()); }
282 iterator &operator ++() {
283 for (
size_type k = (i & 15); k < 15; ++k)
284 { ++p; ++i;
if (*p != T(0))
return *
this; }
288 iterator operator ++(
int) { iterator tmp = *
this; ++(*this);
return tmp; }
289 iterator &operator --() {
291 { --p; --i;
if (*p != T(0))
return *
this; }
292 v->previous_pos(p, i);
295 iterator operator --(
int) { iterator tmp = *
this; --(*this);
return tmp; }
297 bool operator ==(
const iterator &it)
const 298 {
return (i == it.i && p == it.p && v == it.v); }
299 bool operator !=(
const iterator &it)
const 300 {
return !(it == *
this); }
302 size_type index(
void)
const {
return i; }
304 dsvector_const_iterator(
void) : i(
size_type(-1)), p(0) {}
305 dsvector_const_iterator(
const dsvector_iterator<T> &it)
306 : i(it.i), p(it.p), v(it.v) {}
307 dsvector_const_iterator(
const dsvector<T> &w)
317 template<
typename T>
class dsvector {
319 typedef dsvector_iterator<T> iterator;
320 typedef dsvector_const_iterator<T> const_iterator;
321 typedef dsvector<T> this_type;
323 typedef const T * const_pointer;
324 typedef void * void_pointer;
325 typedef const void * const_void_pointer;
332 void_pointer root_ptr;
334 const T *read_access(
size_type i)
const {
335 GMM_ASSERT1(i < n,
"index out of range");
336 size_type my_mask = mask, my_shift = shift;
337 void_pointer p = root_ptr;
340 p = ((
void **)(p))[(i & my_mask) >> my_shift];
342 my_mask = (my_mask >> 4);
345 GMM_ASSERT1(my_shift == 0,
"internal error");
346 GMM_ASSERT1(my_mask == 15,
"internal error");
347 return &(((
const T *)(p))[i & 15]);
351 GMM_ASSERT1(i < n,
"index " << i <<
" out of range (size " << n <<
")");
352 size_type my_mask = mask, my_shift = shift;
355 root_ptr =
new void_pointer[16];
356 std::memset(root_ptr, 0, 16*
sizeof(void_pointer));
358 root_ptr =
new T[16];
359 for (
size_type l = 0; l < 16; ++l) ((T *)(root_ptr))[l] = T(0);
363 void_pointer p = root_ptr;
366 void_pointer q = ((void_pointer *)(p))[j];
369 q =
new void_pointer[16];
370 std::memset(q, 0, 16*
sizeof(void_pointer));
373 for (
size_type l = 0; l < 16; ++l) ((T *)(q))[l] = T(0);
375 ((void_pointer *)(p))[j] = q;
378 my_mask = (my_mask >> 4);
381 GMM_ASSERT1(my_shift == 0,
"internal error");
382 GMM_ASSERT1(my_mask == 15,
"internal error " << my_mask);
383 return &(((T *)(p))[i & 15]);
387 n = n_; depth = 0; shift = 0; mask = 1;
if (n_) --n_;
388 while (n_) { n_ /= 16; ++depth; shift += 4; mask *= 16; }
389 mask--;
if (shift) shift -= 4;
if (depth) --depth;
393 void rec_del(void_pointer p,
size_type my_depth) {
396 if (((void_pointer *)(p))[k])
397 rec_del(((void_pointer *)(p))[k], my_depth-1);
398 delete[] ((void_pointer *)(p));
404 void rec_clean(void_pointer p,
size_type my_depth,
double eps) {
407 if (((void_pointer *)(p))[k])
408 rec_clean(((void_pointer *)(p))[k], my_depth-1, eps);
411 if (gmm::abs(((T *)(p))[k]) <= eps) ((T *)(p))[k] = T(0);
418 my_mask = (my_mask >> 4);
420 if (((void_pointer *)(p))[k] && (base + (k+1)*(mask+1)) >= i)
421 rec_clean_i(((void_pointer *)(p))[k], my_depth-1, my_mask,
422 i, base + k*(my_mask+1));
425 if (base+k > i) ((T *)(p))[k] = T(0);
434 if (((void_pointer *)(p))[k])
435 nn += rec_nnz(((void_pointer *)(p))[k], my_depth-1);
438 if (((
const T *)(p))[k] != T(0)) nn++;
443 void copy_rec(void_pointer &p, const_void_pointer q,
size_type my_depth) {
445 p =
new void_pointer[16];
446 std::memset(p, 0, 16*
sizeof(void_pointer));
448 if (((
const const_void_pointer *)(q))[l])
449 copy_rec(((void_pointer *)(p))[l],
450 ((
const const_void_pointer *)(q))[l], my_depth-1);
453 for (
size_type l = 0; l < 16; ++l) ((T *)(p))[l] = ((
const T *)(q))[l];
457 void copy(
const dsvector<T> &v) {
458 if (root_ptr) rec_del(root_ptr, depth);
460 mask = v.mask; depth = v.depth; n = v.n; shift = v.shift;
461 if (v.root_ptr) copy_rec(root_ptr, v.root_ptr, depth);
468 my_mask = (my_mask >> 4);
470 if (((void_pointer *)(p))[k] && (base + (k+1)*(my_mask+1)) >= i) {
471 next_pos_rec(((void_pointer *)(p))[k], my_depth-1, my_mask,
472 pp, i, base + k*(my_mask+1));
473 if (i !=
size_type(-1))
return;
else i = ii;
478 if (base+k > i && ((const_pointer)(p))[k] != T(0))
479 { i = base+k; pp = &(((const_pointer)(p))[k]);
return; }
489 my_mask = (my_mask >> 4);
491 if (((void_pointer *)(p))[k] && ((base + k*(my_mask+1)) < i)) {
492 previous_pos_rec(((void_pointer *)(p))[k], my_depth-1,
493 my_mask, pp, i, base + k*(my_mask+1));
494 if (i !=
size_type(-1))
return;
else i = ii;
499 if (base+k < i && ((const_pointer)(p))[k] != T(0))
500 { i = base+k; pp = &(((const_pointer)(p))[k]);
return; }
507 void clean(
double eps) {
if (root_ptr) rec_clean(root_ptr, depth); }
512 if (root_ptr) rec_clean_i(root_ptr, depth, mask, n_, 0);
515 size_type my_depth = 0, my_shift = 0, my_mask = 1;
if (n_) --n_;
516 while (n_) { n_ /= 16; ++my_depth; my_shift += 4; my_mask *= 16; }
517 my_mask--;
if (my_shift) my_shift -= 4;
if (my_depth) --my_depth;
518 if (my_depth > depth || depth == 0) {
520 for (
size_type k = depth; k < my_depth; ++k) {
521 void_pointer *q =
new void_pointer [16];
522 std::memset(q, 0, 16*
sizeof(void_pointer));
523 q[0] = root_ptr; root_ptr = q;
526 mask = my_mask; depth = my_depth; shift = my_shift;
532 void clear(
void) {
if (root_ptr) rec_del(root_ptr, depth); root_ptr = 0; }
534 void next_pos(const_pointer &pp,
size_type &i)
const {
535 if (!root_ptr || i >= n) { pp = 0, i =
size_type(-1);
return; }
536 next_pos_rec(root_ptr, depth, mask, pp, i, 0);
539 void previous_pos(const_pointer &pp,
size_type &i)
const {
540 if (!root_ptr) { pp = 0, i =
size_type(-1);
return; }
542 previous_pos_rec(root_ptr, depth, mask, pp, i, 0);
545 iterator begin(
void) {
548 it.i = 0; it.p =
const_cast<T *
>(read_access(0));
549 if (!(it.p) || *(it.p) == T(0))
550 next_pos(*(const_cast<const_pointer *>(&(it.p))), it.i);
555 iterator end(
void) {
return iterator(*
this); }
557 const_iterator begin(
void)
const {
558 const_iterator it(*
this);
560 it.i = 0; it.p = read_access(0);
561 if (!(it.p) || *(it.p) == T(0)) next_pos(it.p, it.i);
566 const_iterator end(
void)
const {
return const_iterator(*
this); }
568 inline ref_elt_vector<T, dsvector<T> > operator [](
size_type c)
569 {
return ref_elt_vector<T, dsvector<T> >(
this, c); }
572 if (e == T(0)) {
if (read_access(c)) *(write_access(c)) = e; }
573 else *(write_access(c)) = e;
577 {
if (e != T(0)) { *(write_access(c)) += e; } }
580 {
const T *p = read_access(c);
if (p)
return *p;
else return T(0); }
582 inline T operator [](
size_type c)
const {
return r(c); }
585 {
if (root_ptr)
return rec_nnz(root_ptr, depth);
else return 0; }
588 void swap(dsvector<T> &v) {
589 std::swap(n, v.n); std::swap(root_ptr, v.root_ptr);
590 std::swap(depth, v.depth); std::swap(shift, v.shift);
591 std::swap(mask, v.mask);
595 dsvector(
const dsvector<T> &v) { init(0);
copy(v); }
596 dsvector<T> &operator =(
const dsvector<T> &v) {
copy(v);
return *
this; }
597 explicit dsvector(
size_type l){ init(l); }
598 dsvector(
void) { init(0); }
599 ~dsvector() {
if (root_ptr) rec_del(root_ptr, depth); root_ptr = 0; }
602 template <
typename T>
struct linalg_traits<dsvector<T>> {
603 typedef dsvector<T> this_type;
604 typedef this_type origin_type;
605 typedef linalg_false is_reference;
606 typedef abstract_vector linalg_type;
607 typedef T value_type;
608 typedef ref_elt_vector<T, dsvector<T> > reference;
609 typedef dsvector_iterator<T> iterator;
610 typedef dsvector_const_iterator<T> const_iterator;
611 typedef abstract_sparse storage_type;
612 typedef linalg_true index_sorted;
613 static size_type size(
const this_type &v) {
return v.size(); }
614 static iterator begin(this_type &v) {
return v.begin(); }
615 static const_iterator begin(
const this_type &v) {
return v.begin(); }
616 static iterator end(this_type &v) {
return v.end(); }
617 static const_iterator end(
const this_type &v) {
return v.end(); }
618 static origin_type* origin(this_type &v) {
return &v; }
619 static const origin_type* origin(
const this_type &v) {
return &v; }
620 static void clear(origin_type* o,
const iterator &,
const iterator &)
622 static void do_clear(this_type &v) { v.clear(); }
623 static value_type access(
const origin_type *o,
const const_iterator &,
626 static reference access(origin_type *o,
const iterator &,
const iterator &,
632 template<
typename T> std::ostream &
operator <<
633 (std::ostream &o,
const dsvector<T>& v) { gmm::write(o,v);
return o; }
637 template <
typename T>
inline void copy(
const dsvector<T> &v1,
639 GMM_ASSERT2(v1.size() == v2.size(),
"dimensions mismatch");
642 template <
typename T>
inline void copy(
const dsvector<T> &v1,
643 const dsvector<T> &v2) {
644 GMM_ASSERT2(v1.size() == v2.size(),
"dimensions mismatch");
645 v2 =
const_cast<dsvector<T> &
>(v1);
647 template <
typename T>
inline 648 void copy(
const dsvector<T> &v1,
const simple_vector_ref<dsvector<T> *> &v2){
649 simple_vector_ref<dsvector<T> *>
650 *svr =
const_cast<simple_vector_ref<dsvector<T> *
> *>(&v2);
652 *pv =
const_cast<dsvector<T> *
>((v2.origin));
653 GMM_ASSERT2(vect_size(v1) == vect_size(v2),
"dimensions mismatch");
654 *pv = v1; svr->begin_ = vect_begin(*pv); svr->end_ = vect_end(*pv);
656 template <
typename T>
inline 657 void copy(
const simple_vector_ref<
const dsvector<T> *> &v1,
659 {
copy(*(v1.origin), v2); }
660 template <
typename T>
inline 661 void copy(
const simple_vector_ref<dsvector<T> *> &v1, dsvector<T> &v2)
662 {
copy(*(v1.origin), v2); }
663 template <
typename T>
inline 664 void copy(
const simple_vector_ref<dsvector<T> *> &v1,
665 const simple_vector_ref<dsvector<T> *> &v2)
666 {
copy(*(v1.origin), v2); }
667 template <
typename T>
inline 668 void copy(
const simple_vector_ref<
const dsvector<T> *> &v1,
669 const simple_vector_ref<dsvector<T> *> &v2)
670 {
copy(*(v1.origin), v2); }
672 template <
typename T>
673 inline size_type nnz(
const dsvector<T>& l) {
return l.nnz(); }
683 template<
typename T>
struct wsvector_iterator
684 :
public std::map<size_type, T>::iterator {
685 typedef typename std::map<size_type, T>::iterator base_it_type;
686 typedef T value_type;
687 typedef value_type* pointer;
688 typedef value_type& reference;
690 typedef ptrdiff_t difference_type;
691 typedef std::bidirectional_iterator_tag iterator_category;
693 reference operator *()
const {
return (base_it_type::operator*()).second; }
694 pointer operator->()
const {
return &(operator*()); }
695 size_type index(
void)
const {
return (base_it_type::operator*()).first; }
697 wsvector_iterator(
void) {}
698 wsvector_iterator(
const base_it_type &it) : base_it_type(it) {}
701 template<
typename T>
struct wsvector_const_iterator
702 :
public std::map<size_type, T>::const_iterator {
703 typedef typename std::map<size_type, T>::const_iterator base_it_type;
704 typedef T value_type;
705 typedef const value_type* pointer;
706 typedef const value_type& reference;
708 typedef ptrdiff_t difference_type;
709 typedef std::bidirectional_iterator_tag iterator_category;
711 reference operator *()
const {
return (base_it_type::operator*()).second; }
712 pointer operator->()
const {
return &(operator*()); }
713 size_type index(
void)
const {
return (base_it_type::operator*()).first; }
715 wsvector_const_iterator(
void) {}
716 wsvector_const_iterator(
const wsvector_iterator<T> &it)
717 : base_it_type(it) {}
718 wsvector_const_iterator(
const base_it_type &it) : base_it_type(it) {}
726 template<
typename T>
class wsvector :
public std::map<size_type, T> {
730 typedef std::map<size_type, T> base_type;
731 typedef typename base_type::iterator iterator;
732 typedef typename base_type::const_iterator const_iterator;
738 void clean(
double eps);
741 inline ref_elt_vector<T, wsvector<T> > operator [](size_type c)
742 {
return ref_elt_vector<T, wsvector<T> >(
this, c); }
744 inline void w(size_type c,
const T &e) {
745 GMM_ASSERT2(c < nbl,
"out of range");
746 if (e == T(0)) { this->erase(c); }
747 else base_type::operator [](c) = e;
750 inline void wa(size_type c,
const T &e) {
751 GMM_ASSERT2(c < nbl,
"out of range");
753 iterator it = this->lower_bound(c);
754 if (it != this->end() && it->first == c) it->second += e;
755 else base_type::operator [](c) = e;
759 inline T r(size_type c)
const {
760 GMM_ASSERT2(c < nbl,
"out of range");
761 const_iterator it = this->lower_bound(c);
762 if (it != this->end() && c == it->first)
return it->second;
766 inline T operator [](size_type c)
const {
return r(c); }
768 size_type nb_stored(
void)
const {
return base_type::size(); }
769 size_type size(
void)
const {
return nbl; }
771 void swap(wsvector<T> &v)
772 { std::swap(nbl, v.nbl); std::map<size_type, T>::swap(v); }
776 void init(size_type l) { nbl = l; this->
clear(); }
777 explicit wsvector(size_type l){ init(l); }
778 wsvector(
void) { init(0); }
781 template<
typename T>
void wsvector<T>::clean(
double eps) {
782 iterator it = this->begin(), itf = it, ite = this->end();
784 ++itf;
if (gmm::abs(it->second) <= eps) this->erase(it); it = itf;
788 template<
typename T>
void wsvector<T>::resize(size_type n) {
790 iterator it = this->begin(), itf = it, ite = this->end();
791 while (it != ite) { ++itf;
if (it->first >= n) this->erase(it); it=itf; }
796 template <
typename T>
struct linalg_traits<wsvector<T> > {
797 typedef wsvector<T> this_type;
798 typedef this_type origin_type;
799 typedef linalg_false is_reference;
800 typedef abstract_vector linalg_type;
801 typedef T value_type;
802 typedef ref_elt_vector<T, wsvector<T> > reference;
803 typedef wsvector_iterator<T> iterator;
804 typedef wsvector_const_iterator<T> const_iterator;
805 typedef abstract_sparse storage_type;
806 typedef linalg_true index_sorted;
807 static size_type size(
const this_type &v) {
return v.size(); }
808 static iterator begin(this_type &v) {
return v.begin(); }
809 static const_iterator begin(
const this_type &v) {
return v.begin(); }
810 static iterator end(this_type &v) {
return v.end(); }
811 static const_iterator end(
const this_type &v) {
return v.end(); }
812 static origin_type* origin(this_type &v) {
return &v; }
813 static const origin_type* origin(
const this_type &v) {
return &v; }
814 static void clear(origin_type* o,
const iterator &,
const iterator &)
816 static void do_clear(this_type &v) { v.clear(); }
817 static value_type access(
const origin_type *o,
const const_iterator &,
818 const const_iterator &, size_type i)
820 static reference access(origin_type *o,
const iterator &,
const iterator &,
823 static void resize(this_type &v, size_type n) { v.resize(n); }
826 template<
typename T> std::ostream &
operator <<
827 (std::ostream &o,
const wsvector<T>& v) { gmm::write(o,v);
return o; }
831 template <
typename T>
inline void copy(
const wsvector<T> &v1,
833 GMM_ASSERT2(vect_size(v1) == vect_size(v2),
"dimensions mismatch");
836 template <
typename T>
inline 837 void copy(
const wsvector<T> &v1,
const simple_vector_ref<wsvector<T> *> &v2){
838 simple_vector_ref<wsvector<T> *>
839 *svr =
const_cast<simple_vector_ref<wsvector<T> *
> *>(&v2);
841 *pv =
const_cast<wsvector<T> *
>(v2.origin);
842 GMM_ASSERT2(vect_size(v1) == vect_size(v2),
"dimensions mismatch");
843 *pv = v1; svr->begin_ = vect_begin(*pv); svr->end_ = vect_end(*pv);
845 template <
typename T>
inline 846 void copy(
const simple_vector_ref<
const wsvector<T> *> &v1,
848 {
copy(*(v1.origin), v2); }
849 template <
typename T>
inline 850 void copy(
const simple_vector_ref<wsvector<T> *> &v1, wsvector<T> &v2)
851 {
copy(*(v1.origin), v2); }
853 template <
typename T>
inline void clean(wsvector<T> &v,
double eps) {
854 typedef typename number_traits<T>::magnitude_type R;
855 typename wsvector<T>::iterator it = v.begin(), ite = v.end(), itc;
857 if (gmm::abs((*it).second) <= R(eps))
858 { itc=it; ++it; v.erase(itc); }
else ++it;
861 template <
typename T>
862 inline void clean(
const simple_vector_ref<wsvector<T> *> &l,
double eps) {
863 simple_vector_ref<wsvector<T> *>
864 *svr =
const_cast<simple_vector_ref<wsvector<T> *
> *>(&l);
866 *pv =
const_cast<wsvector<T> *
>((l.origin));
868 svr->begin_ = vect_begin(*pv); svr->end_ = vect_end(*pv);
871 template <
typename T>
872 inline size_type
nnz(
const wsvector<T>& l) {
return l.nb_stored(); }
880 template<
typename T>
struct elt_rsvector_ {
893 elt_rsvector_(
void) : e(0) {}
894 elt_rsvector_(size_type cc) : c(cc), e(0) {}
895 elt_rsvector_(size_type cc,
const T &ee) : c(cc), e(ee) {}
896 bool operator < (
const elt_rsvector_ &a)
const {
return c < a.c; }
897 bool operator == (
const elt_rsvector_ &a)
const {
return c == a.c; }
898 bool operator != (
const elt_rsvector_ &a)
const {
return c != a.c; }
901 template<
typename T>
struct rsvector_iterator {
902 typedef typename std::vector<elt_rsvector_<T> >::iterator IT;
903 typedef T value_type;
904 typedef value_type* pointer;
905 typedef value_type& reference;
907 typedef ptrdiff_t difference_type;
908 typedef std::bidirectional_iterator_tag iterator_category;
909 typedef rsvector_iterator<T> iterator;
913 reference operator *()
const {
return it->e; }
914 pointer operator->()
const {
return &(operator*()); }
916 iterator &operator ++() { ++it;
return *
this; }
917 iterator operator ++(
int) { iterator tmp = *
this; ++(*this);
return tmp; }
918 iterator &operator --() { --it;
return *
this; }
919 iterator operator --(
int) { iterator tmp = *
this; --(*this);
return tmp; }
921 bool operator ==(
const iterator &i)
const {
return it == i.it; }
922 bool operator !=(
const iterator &i)
const {
return !(i == *
this); }
924 size_type index(
void)
const {
return it->c; }
925 rsvector_iterator(
void) {}
926 rsvector_iterator(
const IT &i) : it(i) {}
929 template<
typename T>
struct rsvector_const_iterator {
930 typedef typename std::vector<elt_rsvector_<T> >::const_iterator IT;
931 typedef T value_type;
932 typedef const value_type* pointer;
933 typedef const value_type& reference;
935 typedef ptrdiff_t difference_type;
936 typedef std::forward_iterator_tag iterator_category;
937 typedef rsvector_const_iterator<T> iterator;
941 reference operator *()
const {
return it->e; }
942 pointer operator->()
const {
return &(operator*()); }
943 size_type index(
void)
const {
return it->c; }
945 iterator &operator ++() { ++it;
return *
this; }
946 iterator operator ++(
int) { iterator tmp = *
this; ++(*this);
return tmp; }
947 iterator &operator --() { --it;
return *
this; }
948 iterator operator --(
int) { iterator tmp = *
this; --(*this);
return tmp; }
950 bool operator ==(
const iterator &i)
const {
return it == i.it; }
951 bool operator !=(
const iterator &i)
const {
return !(i == *
this); }
953 rsvector_const_iterator(
void) {}
954 rsvector_const_iterator(
const rsvector_iterator<T> &i) : it(i.it) {}
955 rsvector_const_iterator(
const IT &i) : it(i) {}
962 template<
typename T>
class rsvector :
public std::vector<elt_rsvector_<T> > {
965 typedef std::vector<elt_rsvector_<T> > base_type_;
966 typedef typename base_type_::iterator iterator;
967 typedef typename base_type_::const_iterator const_iterator;
969 typedef T value_type;
976 void sup(size_type j);
977 void base_resize(size_type n) { base_type_::resize(n); }
980 ref_elt_vector<T, rsvector<T> > operator [](size_type c)
981 {
return ref_elt_vector<T, rsvector<T> >(
this, c); }
983 void w(size_type c,
const T &e);
984 void wa(size_type c,
const T &e);
985 T r(size_type c)
const;
986 void swap_indices(size_type i, size_type j);
988 inline T operator [](size_type c)
const {
return r(c); }
990 size_type nb_stored(
void)
const {
return base_type_::size(); }
991 size_type size(
void)
const {
return nbl; }
992 void clear(
void) { base_type_::resize(0); }
993 void swap(rsvector<T> &v)
994 { std::swap(nbl, v.nbl); std::vector<elt_rsvector_<T> >::swap(v); }
997 explicit rsvector(size_type l) : nbl(l) { }
998 rsvector(
void) : nbl(0) { }
1001 template <
typename T>
1002 void rsvector<T>::swap_indices(size_type i, size_type j) {
1003 if (i > j) std::swap(i, j);
1006 elt_rsvector_<T> ei(i), ej(j), a;
1007 iterator it, ite, iti, itj;
1008 iti = std::lower_bound(this->begin(), this->end(), ei);
1009 if (iti != this->end() && iti->c == i) situation += 1;
1010 itj = std::lower_bound(this->begin(), this->end(), ej);
1011 if (itj != this->end() && itj->c == j) situation += 2;
1013 switch (situation) {
1014 case 1 : a = *iti; a.c = j; it = iti; ++it; ite = this->end();
1015 for (; it != ite && it->c <= j; ++it, ++iti) *iti = *it;
1018 case 2 : a = *itj; a.c = i; it = itj; ite = this->begin();
1021 while (it->c >= i) { *itj = *it; --itj;
if (it==ite)
break; --it; }
1025 case 3 : std::swap(iti->e, itj->e);
1031 template <
typename T>
void rsvector<T>::sup(size_type j) {
1032 if (nb_stored() != 0) {
1033 elt_rsvector_<T> ev(j);
1034 iterator it = std::lower_bound(this->begin(), this->end(), ev);
1035 if (it != this->end() && it->c == j) {
1036 for (iterator ite = this->end() - 1; it != ite; ++it) *it = *(it+1);
1037 base_resize(nb_stored()-1);
1042 template<
typename T>
void rsvector<T>::resize(size_type n) {
1044 for (size_type i = 0; i < nb_stored(); ++i)
1045 if (base_type_::operator[](i).c >= n) { base_resize(i);
break; }
1050 template <
typename T>
void rsvector<T>::w(size_type c,
const T &e) {
1051 GMM_ASSERT2(c < nbl,
"out of range");
1052 if (e == T(0)) sup(c);
1054 elt_rsvector_<T> ev(c, e);
1055 if (nb_stored() == 0) {
1056 base_type_::push_back(ev);
1059 iterator it = std::lower_bound(this->begin(), this->end(), ev);
1060 if (it != this->end() && it->c == c) it->e = e;
1062 size_type ind = it - this->begin(), nb = this->nb_stored();
1063 if (nb - ind > 1100)
1064 GMM_WARNING2(
"Inefficient addition of element in rsvector with " 1065 << this->nb_stored() - ind <<
" non-zero entries");
1066 base_type_::push_back(ev);
1068 it = this->begin() + ind;
1069 iterator ite = this->end(); --ite; iterator itee = ite;
1070 for (; ite != it; --ite) { --itee; *ite = *itee; }
1078 template <
typename T>
void rsvector<T>::wa(size_type c,
const T &e) {
1079 GMM_ASSERT2(c < nbl,
"out of range");
1081 elt_rsvector_<T> ev(c, e);
1082 if (nb_stored() == 0) {
1083 base_type_::push_back(ev);
1086 iterator it = std::lower_bound(this->begin(), this->end(), ev);
1087 if (it != this->end() && it->c == c) it->e += e;
1089 size_type ind = it - this->begin(), nb = this->nb_stored();
1090 if (nb - ind > 1100)
1091 GMM_WARNING2(
"Inefficient addition of element in rsvector with " 1092 << this->nb_stored() - ind <<
" non-zero entries");
1093 base_type_::push_back(ev);
1095 it = this->begin() + ind;
1096 iterator ite = this->end(); --ite; iterator itee = ite;
1097 for (; ite != it; --ite) { --itee; *ite = *itee; }
1105 template <
typename T> T rsvector<T>::r(size_type c)
const {
1106 GMM_ASSERT2(c < nbl,
"out of range. Index " << c
1107 <<
" for a length of " << nbl);
1108 if (nb_stored() != 0) {
1109 elt_rsvector_<T> ev(c);
1110 const_iterator it = std::lower_bound(this->begin(), this->end(), ev);
1111 if (it != this->end() && it->c == c)
return it->e;
1116 template <
typename T>
struct linalg_traits<rsvector<T> > {
1117 typedef rsvector<T> this_type;
1118 typedef this_type origin_type;
1119 typedef linalg_false is_reference;
1120 typedef abstract_vector linalg_type;
1121 typedef T value_type;
1122 typedef ref_elt_vector<T, rsvector<T> > reference;
1123 typedef rsvector_iterator<T> iterator;
1124 typedef rsvector_const_iterator<T> const_iterator;
1125 typedef abstract_sparse storage_type;
1126 typedef linalg_true index_sorted;
1127 static size_type size(
const this_type &v) {
return v.size(); }
1128 static iterator begin(this_type &v) {
return iterator(v.begin()); }
1129 static const_iterator begin(
const this_type &v)
1130 {
return const_iterator(v.begin()); }
1131 static iterator end(this_type &v) {
return iterator(v.end()); }
1132 static const_iterator end(
const this_type &v)
1133 {
return const_iterator(v.end()); }
1134 static origin_type* origin(this_type &v) {
return &v; }
1135 static const origin_type* origin(
const this_type &v) {
return &v; }
1136 static void clear(origin_type* o,
const iterator &,
const iterator &)
1138 static void do_clear(this_type &v) { v.clear(); }
1139 static value_type access(
const origin_type *o,
const const_iterator &,
1140 const const_iterator &, size_type i)
1142 static reference access(origin_type *o,
const iterator &,
const iterator &,
1145 static void resize(this_type &v, size_type n) { v.resize(n); }
1148 template<
typename T> std::ostream &
operator <<
1149 (std::ostream &o,
const rsvector<T>& v) { gmm::write(o,v);
return o; }
1153 template <
typename T>
inline void copy(
const rsvector<T> &v1,
1155 GMM_ASSERT2(vect_size(v1) == vect_size(v2),
"dimensions mismatch");
1158 template <
typename T>
inline 1159 void copy(
const rsvector<T> &v1,
const simple_vector_ref<rsvector<T> *> &v2){
1160 simple_vector_ref<rsvector<T> *>
1161 *svr =
const_cast<simple_vector_ref<rsvector<T> *
> *>(&v2);
1163 *pv =
const_cast<rsvector<T> *
>((v2.origin));
1164 GMM_ASSERT2(vect_size(v1) == vect_size(v2),
"dimensions mismatch");
1165 *pv = v1; svr->begin_ = vect_begin(*pv); svr->end_ = vect_end(*pv);
1167 template <
typename T>
inline 1168 void copy(
const simple_vector_ref<
const rsvector<T> *> &v1,
1170 {
copy(*(v1.origin), v2); }
1171 template <
typename T>
inline 1172 void copy(
const simple_vector_ref<rsvector<T> *> &v1, rsvector<T> &v2)
1173 {
copy(*(v1.origin), v2); }
1175 template <
typename V,
typename T>
inline void add(
const V &v1,
1177 if ((
const void *)(&v1) != (
const void *)(&v2)) {
1178 GMM_ASSERT2(vect_size(v1) == vect_size(v2),
"dimensions mismatch");
1179 add_rsvector(v1, v2,
typename linalg_traits<V>::storage_type());
1183 template <
typename V,
typename T>
1184 inline void add_rsvector(
const V &v1, rsvector<T> &v2, abstract_dense)
1185 {
add(v1, v2, abstract_dense(), abstract_sparse()); }
1187 template <
typename V,
typename T>
1188 inline void add_rsvector(
const V &v1, rsvector<T> &v2, abstract_skyline)
1189 {
add(v1, v2, abstract_skyline(), abstract_sparse()); }
1191 template <
typename V,
typename T>
1192 void add_rsvector(
const V &v1, rsvector<T> &v2, abstract_sparse) {
1193 add_rsvector(v1, v2,
typename linalg_traits<V>::index_sorted());
1196 template <
typename V,
typename T>
1197 void add_rsvector(
const V &v1, rsvector<T> &v2, linalg_false) {
1198 add(v1, v2, abstract_sparse(), abstract_sparse());
1201 template <
typename V,
typename T>
1202 void add_rsvector(
const V &v1, rsvector<T> &v2, linalg_true) {
1203 typename linalg_traits<V>::const_iterator it1 = vect_const_begin(v1),
1204 ite1 = vect_const_end(v1);
1205 typename rsvector<T>::iterator it2 = v2.begin(), ite2 = v2.end(), it3;
1206 size_type nbc = 0, old_nbc = v2.nb_stored();
1207 for (; it1 != ite1 && it2 != ite2 ; ++nbc)
1208 if (it1.index() == it2->c) { ++it1; ++it2; }
1209 else if (it1.index() < it2->c) ++it1;
else ++it2;
1210 for (; it1 != ite1; ++it1) ++nbc;
1211 for (; it2 != ite2; ++it2) ++nbc;
1213 v2.base_resize(nbc);
1214 it3 = v2.begin() + old_nbc;
1215 it2 = v2.end(); ite2 = v2.begin();
1216 it1 = vect_end(v1); ite1 = vect_const_begin(v1);
1217 while (it1 != ite1 && it3 != ite2) {
1218 --it3; --it1; --it2;
1219 if (it3->c > it1.index()) { *it2 = *it3; ++it1; }
1220 else if (it3->c == it1.index()) { *it2=*it3; it2->e+=*it1; }
1221 else { it2->c = it1.index(); it2->e = *it1; ++it3; }
1223 while (it1 != ite1) { --it1; --it2; it2->c = it1.index(); it2->e = *it1; }
1226 template <
typename V,
typename T>
void copy(
const V &v1, rsvector<T> &v2) {
1227 if ((
const void *)(&v1) != (
const void *)(&v2)) {
1228 GMM_ASSERT2(vect_size(v1) == vect_size(v2),
"dimensions mismatch");
1229 if (same_origin(v1, v2))
1230 GMM_WARNING2(
"a conflict is possible in vector copy\n");
1231 copy_rsvector(v1, v2,
typename linalg_traits<V>::storage_type());
1235 template <
typename V,
typename T>
1236 void copy_rsvector(
const V &v1, rsvector<T> &v2, abstract_dense)
1237 { copy_vect(v1, v2, abstract_dense(), abstract_sparse()); }
1239 template <
typename V,
typename T>
1240 void copy_rsvector(
const V &v1, rsvector<T> &v2, abstract_skyline)
1241 { copy_vect(v1, v2, abstract_skyline(), abstract_sparse()); }
1243 template <
typename V,
typename T>
1244 void copy_rsvector(
const V &v1, rsvector<T> &v2, abstract_sparse) {
1245 copy_rsvector(v1, v2,
typename linalg_traits<V>::index_sorted());
1248 template <
typename V,
typename T2>
1249 void copy_rsvector(
const V &v1, rsvector<T2> &v2, linalg_true) {
1250 typedef typename linalg_traits<V>::value_type T1;
1251 typename linalg_traits<V>::const_iterator it = vect_const_begin(v1),
1252 ite = vect_const_end(v1);
1253 v2.base_resize(
nnz(v1));
1254 typename rsvector<T2>::iterator it2 = v2.begin();
1256 for (; it != ite; ++it)
1257 if ((*it) != T1(0)) { it2->c = it.index(); it2->e = *it; ++it2; ++nn; }
1261 template <
typename V,
typename T2>
1262 void copy_rsvector(
const V &v1, rsvector<T2> &v2, linalg_false) {
1263 typedef typename linalg_traits<V>::value_type T1;
1264 typename linalg_traits<V>::const_iterator it = vect_const_begin(v1),
1265 ite = vect_const_end(v1);
1266 v2.base_resize(
nnz(v1));
1267 typename rsvector<T2>::iterator it2 = v2.begin();
1269 for (; it != ite; ++it)
1270 if ((*it) != T1(0)) { it2->c = it.index(); it2->e = *it; ++it2; ++nn; }
1272 std::sort(v2.begin(), v2.end());
1275 template <
typename T>
inline void clean(rsvector<T> &v,
double eps) {
1276 typedef typename number_traits<T>::magnitude_type R;
1277 typename rsvector<T>::iterator it = v.begin(), ite = v.end();
1278 for (; it != ite; ++it)
if (gmm::abs((*it).e) <= eps)
break;
1280 typename rsvector<T>::iterator itc = it;
1281 size_type erased = 1;
1282 for (++it; it != ite; ++it)
1283 { *itc = *it;
if (gmm::abs((*it).e) <= R(eps)) ++erased;
else ++itc; }
1284 v.base_resize(v.nb_stored() - erased);
1288 template <
typename T>
1289 inline void clean(
const simple_vector_ref<rsvector<T> *> &l,
double eps) {
1290 simple_vector_ref<rsvector<T> *>
1291 *svr =
const_cast<simple_vector_ref<rsvector<T> *
> *>(&l);
1293 *pv =
const_cast<rsvector<T> *
>((l.origin));
1295 svr->begin_ = vect_begin(*pv); svr->end_ = vect_end(*pv);
1298 template <
typename T>
1299 inline size_type
nnz(
const rsvector<T>& l) {
return l.nb_stored(); }
1307 template<
typename T>
struct slvector_iterator {
1308 typedef T value_type;
1310 typedef T &reference;
1311 typedef ptrdiff_t difference_type;
1312 typedef std::random_access_iterator_tag iterator_category;
1314 typedef slvector_iterator<T> iterator;
1315 typedef typename std::vector<T>::iterator base_iterator;
1321 iterator &operator ++()
1322 { ++it; ++shift;
return *
this; }
1323 iterator &operator --()
1324 { --it; --shift;
return *
this; }
1325 iterator operator ++(
int)
1326 { iterator tmp = *
this; ++(*(
this));
return tmp; }
1327 iterator operator --(
int)
1328 { iterator tmp = *
this; --(*(
this));
return tmp; }
1329 iterator &operator +=(difference_type i)
1330 { it += i; shift += i;
return *
this; }
1331 iterator &operator -=(difference_type i)
1332 { it -= i; shift -= i;
return *
this; }
1333 iterator operator +(difference_type i)
const 1334 { iterator tmp = *
this;
return (tmp += i); }
1335 iterator operator -(difference_type i)
const 1336 { iterator tmp = *
this;
return (tmp -= i); }
1337 difference_type operator -(
const iterator &i)
const 1338 {
return it - i.it; }
1340 reference operator *()
const 1342 reference operator [](
int ii)
1343 {
return *(it + ii); }
1345 bool operator ==(
const iterator &i)
const 1346 {
return it == i.it; }
1347 bool operator !=(
const iterator &i)
const 1348 {
return !(i == *
this); }
1349 bool operator < (
const iterator &i)
const 1350 {
return it < i.it; }
1351 size_type index(
void)
const {
return shift; }
1353 slvector_iterator(
void) {}
1354 slvector_iterator(
const base_iterator &iter, size_type s)
1355 : it(iter), shift(s) {}
1358 template<
typename T>
struct slvector_const_iterator {
1359 typedef T value_type;
1360 typedef const T *pointer;
1361 typedef value_type reference;
1362 typedef ptrdiff_t difference_type;
1363 typedef std::random_access_iterator_tag iterator_category;
1365 typedef slvector_const_iterator<T> iterator;
1366 typedef typename std::vector<T>::const_iterator base_iterator;
1372 iterator &operator ++()
1373 { ++it; ++shift;
return *
this; }
1374 iterator &operator --()
1375 { --it; --shift;
return *
this; }
1376 iterator operator ++(
int)
1377 { iterator tmp = *
this; ++(*(
this));
return tmp; }
1378 iterator operator --(
int)
1379 { iterator tmp = *
this; --(*(
this));
return tmp; }
1380 iterator &operator +=(difference_type i)
1381 { it += i; shift += i;
return *
this; }
1382 iterator &operator -=(difference_type i)
1383 { it -= i; shift -= i;
return *
this; }
1384 iterator operator +(difference_type i)
const 1385 { iterator tmp = *
this;
return (tmp += i); }
1386 iterator operator -(difference_type i)
const 1387 { iterator tmp = *
this;
return (tmp -= i); }
1388 difference_type operator -(
const iterator &i)
const 1389 {
return it - i.it; }
1391 value_type operator *()
const 1393 value_type operator [](
int ii)
1394 {
return *(it + ii); }
1396 bool operator ==(
const iterator &i)
const 1397 {
return it == i.it; }
1398 bool operator !=(
const iterator &i)
const 1399 {
return !(i == *
this); }
1400 bool operator < (
const iterator &i)
const 1401 {
return it < i.it; }
1402 size_type index(
void)
const {
return shift; }
1404 slvector_const_iterator(
void) {}
1405 slvector_const_iterator(
const slvector_iterator<T>& iter)
1406 : it(iter.it), shift(iter.shift) {}
1407 slvector_const_iterator(
const base_iterator &iter, size_type s)
1408 : it(iter), shift(s) {}
1414 template <
typename T>
class slvector {
1417 typedef slvector_iterator<T> iterators;
1418 typedef slvector_const_iterator<T> const_iterators;
1420 typedef T value_type;
1423 std::vector<T> data;
1430 size_type size(
void)
const {
return size_; }
1431 size_type first(
void)
const {
return shift; }
1432 size_type last(
void)
const {
return shift + data.size(); }
1433 ref_elt_vector<T, slvector<T> > operator [](size_type c)
1434 {
return ref_elt_vector<T, slvector<T> >(
this, c); }
1436 typename std::vector<T>::iterator data_begin(
void) {
return data.begin(); }
1437 typename std::vector<T>::iterator data_end(
void) {
return data.end(); }
1438 typename std::vector<T>::const_iterator data_begin(
void)
const 1439 {
return data.begin(); }
1440 typename std::vector<T>::const_iterator data_end(
void)
const 1441 {
return data.end(); }
1443 void w(size_type c,
const T &e);
1444 void wa(size_type c,
const T &e);
1445 T r(size_type c)
const {
1446 GMM_ASSERT2(c < size_,
"out of range");
1447 if (c < shift || c >= shift + data.size())
return T(0);
1448 return data[c - shift];
1451 inline T operator [](size_type c)
const {
return r(c); }
1453 void clear(
void) { data.resize(0); shift = 0; }
1454 void swap(slvector<T> &v) {
1455 std::swap(data, v.data);
1456 std::swap(shift, v.shift);
1457 std::swap(size_, v.size_);
1461 slvector(
void) : data(0), shift(0), size_(0) {}
1462 explicit slvector(size_type l) : data(0), shift(0), size_(l) {}
1463 slvector(size_type l, size_type d, size_type s)
1464 : data(d), shift(s), size_(l) {}
1468 template<
typename T>
void slvector<T>::resize(size_type n) {
1470 if (shift >= n)
clear();
else { data.resize(n-shift); }
1475 template<
typename T>
void slvector<T>::w(size_type c,
const T &e) {
1476 GMM_ASSERT2(c < size_,
"out of range");
1477 size_type s = data.size();
1478 if (!s) { data.resize(1); shift = c; }
1479 else if (c < shift) {
1480 data.resize(s + shift - c);
1481 typename std::vector<T>::iterator it = data.begin(),it2=data.end()-1;
1482 typename std::vector<T>::iterator it3 = it2 - shift + c;
1483 for (; it3 >= it; --it3, --it2) *it2 = *it3;
1484 std::fill(it, it + shift - c, T(0));
1487 else if (c >= shift + s) {
1488 data.resize(c - shift + 1, T(0));
1491 data[c - shift] = e;
1494 template<
typename T>
void slvector<T>::wa(size_type c,
const T &e) {
1495 GMM_ASSERT2(c < size_,
"out of range");
1496 size_type s = data.size();
1497 if (!s) { data.resize(1, e); shift = c;
return; }
1498 else if (c < shift) {
1499 data.resize(s + shift - c);
1500 typename std::vector<T>::iterator it = data.begin(),it2=data.end()-1;
1501 typename std::vector<T>::iterator it3 = it2 - shift + c;
1502 for (; it3 >= it; --it3, --it2) *it2 = *it3;
1503 std::fill(it, it + shift - c, T(0));
1505 data[c - shift] = e;
1508 else if (c >= shift + s) {
1509 data.resize(c - shift + 1, T(0));
1510 data[c - shift] = e;
1514 data[c - shift] += e;
1518 template <
typename T>
struct linalg_traits<slvector<T> > {
1519 typedef slvector<T> this_type;
1520 typedef this_type origin_type;
1521 typedef linalg_false is_reference;
1522 typedef abstract_vector linalg_type;
1523 typedef T value_type;
1524 typedef ref_elt_vector<T, slvector<T> > reference;
1525 typedef slvector_iterator<T> iterator;
1526 typedef slvector_const_iterator<T> const_iterator;
1527 typedef abstract_skyline storage_type;
1528 typedef linalg_true index_sorted;
1529 static size_type size(
const this_type &v) {
return v.size(); }
1530 static iterator begin(this_type &v)
1531 {
return iterator(v.data_begin(), v.first()); }
1532 static const_iterator begin(
const this_type &v)
1533 {
return const_iterator(v.data_begin(), v.first()); }
1534 static iterator end(this_type &v)
1535 {
return iterator(v.data_end(), v.last()); }
1536 static const_iterator end(
const this_type &v)
1537 {
return const_iterator(v.data_end(), v.last()); }
1538 static origin_type* origin(this_type &v) {
return &v; }
1539 static const origin_type* origin(
const this_type &v) {
return &v; }
1540 static void clear(origin_type* o,
const iterator &,
const iterator &)
1542 static void do_clear(this_type &v) { v.clear(); }
1543 static value_type access(
const origin_type *o,
const const_iterator &,
1544 const const_iterator &, size_type i)
1546 static reference access(origin_type *o,
const iterator &,
const iterator &,
1549 static void resize(this_type &v, size_type n) { v.resize(n); }
1552 template<
typename T> std::ostream &
operator <<
1553 (std::ostream &o,
const slvector<T>& v) { gmm::write(o,v);
return o; }
1555 template <
typename T>
1556 inline size_type
nnz(
const slvector<T>& l) {
return l.last() - l.first(); }
size_t size_type
used as the common size type in the library
void copy(const L1 &l1, L2 &l2)
*/
void resize(V &v, size_type n)
*/
sparse vector built upon std::vector.
void clear(L &l)
clear (fill with zeros) a vector or matrix.
void clean(L &l, double threshold)
Clean a vector or matrix (replace near-zero entries with zeroes).
gmm interface for STL vectors.
sparse vector built upon std::map.
size_type nnz(const L &l)
count the number of non-zero entries of a vector or matrix.
void add(const L1 &l1, L2 &l2)
*/