37 #ifndef GMM_SUB_VECTOR_H__
38 #define GMM_SUB_VECTOR_H__
49 template <
typename IT,
typename MIT,
typename SUBI>
50 struct sparse_sub_vector_iterator {
55 typedef std::iterator_traits<IT> traits_type;
56 typedef typename traits_type::value_type value_type;
57 typedef typename traits_type::pointer pointer;
58 typedef typename traits_type::reference reference;
59 typedef typename traits_type::difference_type difference_type;
60 typedef std::bidirectional_iterator_tag iterator_category;
62 typedef sparse_sub_vector_iterator<IT, MIT, SUBI> iterator;
64 size_type index()
const {
return si.rindex(itb.index()); }
67 iterator &operator ++()
68 { ++itb; forward();
return *
this; }
69 iterator operator ++(
int) { iterator tmp = *
this; ++(*this);
return tmp; }
70 iterator &operator --()
71 { --itb; backward();
return *
this; }
72 iterator operator --(
int) { iterator tmp = *
this; --(*this);
return tmp; }
73 reference operator *()
const {
return *itb; }
75 bool operator ==(
const iterator &i)
const {
return itb == i.itb; }
76 bool operator !=(
const iterator &i)
const {
return !(i == *
this); }
78 sparse_sub_vector_iterator() {}
79 sparse_sub_vector_iterator(
const IT &it,
const IT &ite,
const SUBI &s)
80 : itb(it), itbe(ite), si(s) { forward(); }
81 sparse_sub_vector_iterator
82 (
const sparse_sub_vector_iterator<MIT, MIT, SUBI> &it)
83 : itb(it.itb), itbe(it.itbe), si(it.si) {}
84 sparse_sub_vector_iterator &
operator =
85 (
const sparse_sub_vector_iterator<MIT, MIT, SUBI> &it)
86 { itb = it.itb; itbe = it.itbe; si = it.si;
return *
this; }
90 template <
typename IT,
typename MIT,
typename SUBI>
91 void sparse_sub_vector_iterator<IT, MIT, SUBI>::forward()
92 {
while(itb!=itbe && index()==
size_type(-1)) { ++itb; } }
94 template <
typename IT,
typename MIT,
typename SUBI>
95 void sparse_sub_vector_iterator<IT, MIT, SUBI>::backward()
96 {
while(itb!=itbe && index()==
size_type(-1)) --itb; }
99 template <
typename PT,
typename SUBI>
struct sparse_sub_vector {
100 typedef sparse_sub_vector<PT, SUBI> this_type;
101 typedef typename std::iterator_traits<PT>::value_type V;
102 typedef typename select_ref<typename linalg_traits<V>::const_iterator,
103 typename linalg_traits<V>::iterator,
106 typedef typename linalg_traits<this_type>::reference reference;
107 typedef typename linalg_traits<this_type>::porigin_type porigin_type;
109 iterator begin_, end_;
113 size_type size()
const {
return si.size(); }
116 {
return linalg_traits<V>::access(origin, begin_, end_, si.index(i)); }
118 sparse_sub_vector(V &v,
const SUBI &s)
119 : begin_(vect_begin(v)), end_(vect_end(v)),
120 origin(linalg_origin(v)), si(s) {}
121 sparse_sub_vector(
const V &v,
const SUBI &s)
122 : begin_(vect_begin(const_cast<V &>(v))),
123 end_(vect_end(const_cast<V &>(v))),
124 origin(linalg_origin(const_cast<V &>(v))), si(s) {}
125 sparse_sub_vector() {}
126 sparse_sub_vector(
const sparse_sub_vector<V *, SUBI> &cr)
127 : begin_(cr.begin_), end_(cr.end_),
128 origin(cr.origin), si(cr.si) {}
132 template <
typename IT,
typename MIT,
typename SUBI,
typename ORG,
134 void set_to_begin(sparse_sub_vector_iterator<IT, MIT, SUBI> &it,
135 ORG o, sparse_sub_vector<PT, SUBI> *,
137 typedef sparse_sub_vector<PT, SUBI> VECT;
138 typedef typename linalg_traits<VECT>::V_reference ref_t;
139 set_to_begin(it.itb, o,
typename linalg_traits<VECT>::pV(), ref_t());
140 set_to_end(it.itbe, o,
typename linalg_traits<VECT>::pV(), ref_t());
144 template <
typename IT,
typename MIT,
typename SUBI,
typename ORG,
146 void set_to_begin(sparse_sub_vector_iterator<IT, MIT, SUBI> &it,
147 ORG o,
const sparse_sub_vector<PT, SUBI> *,
149 typedef sparse_sub_vector<PT, SUBI> VECT;
150 typedef typename linalg_traits<VECT>::V_reference ref_t;
151 set_to_begin(it.itb, o,
typename linalg_traits<VECT>::pV(), ref_t());
152 set_to_end(it.itbe, o,
typename linalg_traits<VECT>::pV(), ref_t());
156 template <
typename IT,
typename MIT,
typename SUBI,
typename ORG,
158 void set_to_end(sparse_sub_vector_iterator<IT, MIT, SUBI> &it,
159 ORG o, sparse_sub_vector<PT, SUBI> *, linalg_modifiable) {
160 typedef sparse_sub_vector<PT, SUBI> VECT;
161 typedef typename linalg_traits<VECT>::V_reference ref_t;
162 set_to_end(it.itb, o,
typename linalg_traits<VECT>::pV(), ref_t());
163 set_to_end(it.itbe, o,
typename linalg_traits<VECT>::pV(), ref_t());
167 template <
typename IT,
typename MIT,
typename SUBI,
typename ORG,
169 void set_to_end(sparse_sub_vector_iterator<IT, MIT, SUBI> &it,
170 ORG o,
const sparse_sub_vector<PT, SUBI> *,
172 typedef sparse_sub_vector<PT, SUBI> VECT;
173 typedef typename linalg_traits<VECT>::V_reference ref_t;
174 set_to_end(it.itb, o,
typename linalg_traits<VECT>::pV(), ref_t());
175 set_to_end(it.itbe, o,
typename linalg_traits<VECT>::pV(), ref_t());
179 template <
typename PT,
typename SUBI>
180 struct linalg_traits<sparse_sub_vector<PT, SUBI> > {
181 typedef sparse_sub_vector<PT, SUBI> this_type;
182 typedef this_type * pthis_type;
184 typedef typename std::iterator_traits<PT>::value_type V;
185 typedef typename linalg_and<typename index_is_sorted<SUBI>::bool_type,
186 typename linalg_traits<V>::index_sorted>::bool_type index_sorted;
187 typedef typename linalg_traits<V>::is_reference V_reference;
188 typedef typename linalg_traits<V>::origin_type origin_type;
189 typedef typename select_ref<
const origin_type *,
193 typedef typename which_reference<PT>::is_reference is_reference;
194 typedef abstract_vector linalg_type;
195 typedef typename linalg_traits<V>::value_type value_type;
196 typedef typename select_ref<value_type,
197 typename linalg_traits<V>::reference,
200 typedef typename select_ref<typename linalg_traits<V>::const_iterator,
201 typename linalg_traits<V>::iterator,
204 typedef typename select_ref<abstract_null_type,
205 sparse_sub_vector_iterator<pre_iterator,
209 typedef sparse_sub_vector_iterator
210 <
typename linalg_traits<V>::const_iterator, pre_iterator, SUBI>
212 typedef abstract_sparse storage_type;
214 static size_type size(
const this_type &v) {
return v.size(); }
216 static iterator begin(this_type &v) {
221 if (!is_const_reference(is_reference()))
222 set_to_begin(it, v.origin, pthis_type(), is_reference());
228 static const_iterator begin(
const this_type &v) {
233 if (!is_const_reference(is_reference()))
234 set_to_begin(it, v.origin, pthis_type(), is_reference());
240 static iterator end(this_type &v) {
245 if (!is_const_reference(is_reference()))
246 set_to_end(it, v.origin, pthis_type(), is_reference());
252 static const_iterator end(
const this_type &v) {
257 if (!is_const_reference(is_reference()))
258 set_to_end(it, v.origin, pthis_type(), is_reference());
264 static origin_type* origin(this_type &v)
267 static const origin_type* origin(
const this_type &v)
270 static void clear(origin_type* o,
const iterator &begin_,
271 const iterator &end_) {
272 std::deque<size_type> ind;
273 iterator it = begin_;
274 for (; it != end_; ++it) ind.push_front(it.index());
275 for (; !(ind.empty()); ind.pop_back())
276 access(o, begin_, end_, ind.back()) = value_type(0);
279 static void do_clear(this_type &v)
280 {
clear(v.origin, begin(v), end(v)); }
282 static value_type access(
const origin_type *o,
const const_iterator &it,
284 {
return linalg_traits<V>::access(o, it.itb, ite.itb, it.si.index(i)); }
286 static reference access(origin_type *o,
const iterator &it,
288 {
return linalg_traits<V>::access(o, it.itb, ite.itb, it.si.index(i)); }
291 template <
typename PT,
typename SUBI>
292 std::ostream &operator <<(std::ostream &o,
293 const sparse_sub_vector<PT, SUBI>& m)
294 { gmm::write(o,m);
return o; }
301 template <
typename IT,
typename MIT,
typename SUBI>
302 struct skyline_sub_vector_iterator {
307 typedef std::iterator_traits<IT> traits_type;
308 typedef typename traits_type::value_type value_type;
309 typedef typename traits_type::pointer pointer;
310 typedef typename traits_type::reference reference;
311 typedef typename traits_type::difference_type difference_type;
312 typedef std::bidirectional_iterator_tag iterator_category;
314 typedef skyline_sub_vector_iterator<IT, MIT, SUBI> iterator;
317 {
return (itb.index() - si.min + si.step() - 1) / si.step(); }
319 iterator &operator ++()
320 { itb += si.step();
return *
this; }
321 iterator operator ++(
int) { iterator tmp = *
this; ++(*this);
return tmp; }
322 iterator &operator --()
323 { itb -= si.step();
return *
this; }
324 iterator operator --(
int) { iterator tmp = *
this; --(*this);
return tmp; }
326 iterator &operator +=(difference_type i)
327 { itb += si.step() * i;
return *
this; }
328 iterator &operator -=(difference_type i)
329 { itb -= si.step() * i;
return *
this; }
331 { iterator ii = *
this;
return (ii += i); }
333 { iterator ii = *
this;
return (ii -= i); }
334 difference_type
operator -(
const iterator &i)
const
335 {
return (itb - i.itb) / si.step(); }
337 reference operator *()
const {
return *itb; }
338 reference operator [](
int ii) {
return *(itb + ii * si.step()); }
340 bool operator ==(
const iterator &i)
const {
return index() == i.index();}
341 bool operator !=(
const iterator &i)
const {
return !(i == *
this); }
342 bool operator < (
const iterator &i)
const {
return index() < i.index();}
343 bool operator > (
const iterator &i)
const {
return index() > i.index();}
344 bool operator >=(
const iterator &i)
const {
return index() >= i.index();}
346 skyline_sub_vector_iterator() {}
347 skyline_sub_vector_iterator(
const IT &it,
const SUBI &s)
349 skyline_sub_vector_iterator
350 (
const skyline_sub_vector_iterator<MIT, MIT, SUBI> &it)
351 : itb(it.itb), si(it.si) {}
352 skyline_sub_vector_iterator &
353 operator =(
const skyline_sub_vector_iterator<MIT, MIT, SUBI> &it)
354 { itb=it.itb; si=it.si;
return *
this; }
357 template <
typename IT,
typename SUBI>
358 void update_for_sub_skyline(IT &it, IT &ite,
const SUBI &si) {
359 if (it.index() >= si.max || ite.index() <= si.min) { it = ite;
return; }
360 ptrdiff_t dec1 = si.min - it.index(), dec2 = ite.index() - si.max;
361 it += (dec1 < 0) ? ((si.step()-((-dec1) % si.step())) % si.step()) : dec1;
362 ite -= (dec2 < 0) ? -((-dec2) % si.step()) : dec2;
365 template <
typename PT,
typename SUBI>
struct skyline_sub_vector {
366 typedef skyline_sub_vector<PT, SUBI> this_type;
367 typedef typename std::iterator_traits<PT>::value_type V;
369 typedef typename select_ref<typename linalg_traits<V>::const_iterator,
370 typename linalg_traits<V>::iterator,
373 typedef typename linalg_traits<this_type>::reference reference;
374 typedef typename linalg_traits<this_type>::porigin_type porigin_type;
376 iterator begin_, end_;
380 size_type size()
const {
return si.size(); }
383 {
return linalg_traits<V>::access(origin, begin_, end_, si.index(i)); }
385 skyline_sub_vector(V &v,
const SUBI &s) : begin_(vect_begin(v)),
386 end_(vect_end(v)), origin(linalg_origin(v)), si(s) {
387 update_for_sub_skyline(begin_, end_, si);
389 skyline_sub_vector(
const V &v,
const SUBI &s)
390 : begin_(vect_begin(const_cast<V &>(v))),
391 end_(vect_end(const_cast<V &>(v))),
392 origin(linalg_origin(const_cast<V &>(v))), si(s) {
393 update_for_sub_skyline(begin_, end_, si);
395 skyline_sub_vector() {}
396 skyline_sub_vector(
const skyline_sub_vector<pV, SUBI> &cr)
397 : begin_(cr.begin_),end_(cr.end_),origin(cr.origin), si(cr.si) {}
400 template <
typename IT,
typename MIT,
typename SUBI,
typename ORG,
402 void set_to_begin(skyline_sub_vector_iterator<IT, MIT, SUBI> &it,
403 ORG o, skyline_sub_vector<PT, SUBI> *,
405 typedef skyline_sub_vector<PT, SUBI> VECT;
406 typedef typename linalg_traits<VECT>::V_reference ref_t;
408 set_to_begin(it.itb, o,
typename linalg_traits<VECT>::pV(), ref_t());
409 set_to_end(itbe, o,
typename linalg_traits<VECT>::pV(), ref_t());
410 update_for_sub_skyline(it.itb, itbe, it.si);
412 template <
typename IT,
typename MIT,
typename SUBI,
typename ORG,
414 void set_to_begin(skyline_sub_vector_iterator<IT, MIT, SUBI> &it,
415 ORG o,
const skyline_sub_vector<PT, SUBI> *,
417 typedef skyline_sub_vector<PT, SUBI> VECT;
418 typedef typename linalg_traits<VECT>::V_reference ref_t;
420 set_to_begin(it.itb, o,
typename linalg_traits<VECT>::pV(), ref_t());
421 set_to_end(itbe, o,
typename linalg_traits<VECT>::pV(), ref_t());
422 update_for_sub_skyline(it.itb, itbe, it.si);
425 template <
typename IT,
typename MIT,
typename SUBI,
typename ORG,
427 void set_to_end(skyline_sub_vector_iterator<IT, MIT, SUBI> &it,
428 ORG o, skyline_sub_vector<PT, SUBI> *,
430 typedef skyline_sub_vector<PT, SUBI> VECT;
431 typedef typename linalg_traits<VECT>::V_reference ref_t;
433 set_to_begin(itb, o,
typename linalg_traits<VECT>::pV(), ref_t());
434 set_to_end(it.itb, o,
typename linalg_traits<VECT>::pV(), ref_t());
435 update_for_sub_skyline(itb, it.itb, it.si);
437 template <
typename IT,
typename MIT,
typename SUBI,
typename ORG,
439 void set_to_end(skyline_sub_vector_iterator<IT, MIT, SUBI> &it,
440 ORG o,
const skyline_sub_vector<PT, SUBI> *,
442 typedef skyline_sub_vector<PT, SUBI> VECT;
443 typedef typename linalg_traits<VECT>::V_reference ref_t;
445 set_to_begin(itb, o,
typename linalg_traits<VECT>::pV(), ref_t());
446 set_to_end(it.itb, o,
typename linalg_traits<VECT>::pV(), ref_t());
447 update_for_sub_skyline(itb, it.itb, it.si);
451 template <
typename PT,
typename SUBI>
452 struct linalg_traits<skyline_sub_vector<PT, SUBI> > {
453 typedef skyline_sub_vector<PT, SUBI> this_type;
454 typedef this_type *pthis_type;
455 typedef typename std::iterator_traits<PT>::value_type V;
456 typedef typename linalg_traits<V>::is_reference V_reference;
457 typedef typename linalg_traits<V>::origin_type origin_type;
458 typedef typename select_ref<
const origin_type *,
463 typedef typename which_reference<PT>::is_reference is_reference;
464 typedef abstract_vector linalg_type;
465 typedef typename linalg_traits<V>::value_type value_type;
466 typedef typename select_ref<value_type,
467 typename linalg_traits<V>::reference,
470 typedef typename linalg_traits<V>::const_iterator const_V_iterator;
471 typedef typename linalg_traits<V>::iterator V_iterator;
472 typedef typename select_ref<const_V_iterator,
476 typedef typename select_ref<abstract_null_type,
477 skyline_sub_vector_iterator<pre_iterator,
479 PT>::ref_type iterator;
480 typedef skyline_sub_vector_iterator<const_V_iterator, pre_iterator, SUBI>
482 typedef abstract_skyline storage_type;
483 typedef linalg_true index_sorted;
484 static size_type size(
const this_type &v) {
return v.size(); }
485 static iterator begin(this_type &v) {
489 if (!is_const_reference(is_reference()))
490 set_to_begin(it, v.origin, pthis_type(), is_reference());
493 static const_iterator begin(
const this_type &v) {
497 if (!is_const_reference(is_reference()))
498 set_to_begin(it, v.origin, pthis_type(), is_reference());
501 static iterator end(this_type &v) {
505 if (!is_const_reference(is_reference()))
506 set_to_end(it, v.origin, pthis_type(), is_reference());
509 static const_iterator end(
const this_type &v) {
513 if (!is_const_reference(is_reference()))
514 set_to_end(it, v.origin, pthis_type(), is_reference());
517 static origin_type* origin(this_type &v) {
return v.origin; }
518 static const origin_type* origin(
const this_type &v) {
return v.origin; }
519 static void clear(origin_type*,
const iterator &it,
const iterator &ite)
520 { std::fill(it, ite, value_type(0)); }
521 static void do_clear(this_type &v) {
clear(v.origin, begin(v), end(v)); }
522 static value_type access(
const origin_type *o,
const const_iterator &it,
524 {
return linalg_traits<V>::access(o, it.itb, ite.itb, it.si.index(i)); }
525 static reference access(origin_type *o,
const iterator &it,
527 {
return linalg_traits<V>::access(o, it.itb, ite.itb, it.si.index(i)); }
530 template <
typename PT,
typename SUBI> std::ostream &
operator <<
531 (std::ostream &o,
const skyline_sub_vector<PT, SUBI>& m)
532 { gmm::write(o,m);
return o; }
541 template <
typename PT,
typename SUBI,
typename st_type>
struct svrt_ir {
542 typedef abstract_null_type vector_type;
545 template <
typename PT>
546 struct svrt_ir<PT, sub_index, abstract_dense> {
547 typedef typename std::iterator_traits<PT>::value_type V;
548 typedef typename vect_ref_type<PT, V>::iterator iterator;
549 typedef tab_ref_index_ref_with_origin<iterator,
550 sub_index::const_iterator, V> vector_type;
553 template <
typename PT>
554 struct svrt_ir<PT, unsorted_sub_index, abstract_dense> {
555 typedef typename std::iterator_traits<PT>::value_type V;
556 typedef typename vect_ref_type<PT, V>::iterator iterator;
557 typedef tab_ref_index_ref_with_origin<iterator,
558 unsorted_sub_index::const_iterator, V> vector_type;
561 template <
typename PT>
562 struct svrt_ir<PT, sub_interval, abstract_dense> {
563 typedef typename std::iterator_traits<PT>::value_type V;
564 typedef typename vect_ref_type<PT, V>::iterator iterator;
565 typedef tab_ref_with_origin<iterator, V> vector_type;
568 template <
typename PT>
569 struct svrt_ir<PT, sub_slice, abstract_dense> {
570 typedef typename std::iterator_traits<PT>::value_type V;
571 typedef typename vect_ref_type<PT, V>::iterator iterator;
572 typedef tab_ref_reg_spaced_with_origin<iterator, V> vector_type;
575 template <
typename PT,
typename SUBI>
576 struct svrt_ir<PT, SUBI, abstract_skyline> {
577 typedef skyline_sub_vector<PT, SUBI> vector_type;
580 template <
typename PT>
581 struct svrt_ir<PT, sub_index, abstract_skyline> {
582 typedef sparse_sub_vector<PT, sub_index> vector_type;
585 template <
typename PT>
586 struct svrt_ir<PT, unsorted_sub_index, abstract_skyline> {
587 typedef sparse_sub_vector<PT, unsorted_sub_index> vector_type;
591 template <
typename PT,
typename SUBI>
592 struct svrt_ir<PT, SUBI, abstract_sparse> {
593 typedef sparse_sub_vector<PT, SUBI> vector_type;
596 template <
typename PT,
typename SUBI>
597 struct sub_vector_type {
598 typedef typename std::iterator_traits<PT>::value_type V;
599 typedef typename svrt_ir<PT, SUBI,
600 typename linalg_traits<V>::storage_type>::vector_type vector_type;
603 template <
typename V,
typename SUBI>
604 typename select_return<
605 typename sub_vector_type<const V *, SUBI>::vector_type,
606 typename sub_vector_type<V *, SUBI>::vector_type,
const V *>::return_type
607 sub_vector(
const V &v,
const SUBI &si) {
608 GMM_ASSERT2(si.last() <= vect_size(v),
609 "sub vector too large, " << si.last() <<
" > " << vect_size(v));
610 return typename select_return<
611 typename sub_vector_type<const V *, SUBI>::vector_type,
612 typename sub_vector_type<V *, SUBI>::vector_type,
const V *>::return_type
613 (linalg_cast(v), si);
616 template <
typename V,
typename SUBI>
617 typename select_return<
618 typename sub_vector_type<const V *, SUBI>::vector_type,
619 typename sub_vector_type<V *, SUBI>::vector_type, V *>::return_type
620 sub_vector(V &v,
const SUBI &si) {
621 GMM_ASSERT2(si.last() <= vect_size(v),
622 "sub vector too large, " << si.last() <<
" > " << vect_size(v));
623 return typename select_return<
624 typename sub_vector_type<const V *, SUBI>::vector_type,
625 typename sub_vector_type<V *, SUBI>::vector_type, V *>::return_type
626 (linalg_cast(v), si);
void clear(L &l)
clear (fill with zeros) a vector or matrix.
gmm interface for STL vectors.
rational_fraction< T > operator-(const polynomial< T > &P, const rational_fraction< T > &Q)
Subtract Q from P.
rational_fraction< T > operator+(const polynomial< T > &P, const rational_fraction< T > &Q)
Add Q to P.
size_t size_type
used as the common size type in the library