38 #ifndef GMM_SUB_VECTOR_H__
39 #define GMM_SUB_VECTOR_H__
50 template <
typename IT,
typename MIT,
typename SUBI>
51 struct sparse_sub_vector_iterator {
56 typedef std::iterator_traits<IT> traits_type;
57 typedef typename traits_type::value_type value_type;
58 typedef typename traits_type::pointer pointer;
59 typedef typename traits_type::reference reference;
60 typedef typename traits_type::difference_type difference_type;
61 typedef std::bidirectional_iterator_tag iterator_category;
63 typedef sparse_sub_vector_iterator<IT, MIT, SUBI> iterator;
65 size_type index()
const {
return si.rindex(itb.index()); }
68 iterator &operator ++()
69 { ++itb; forward();
return *
this; }
70 iterator operator ++(
int) { iterator tmp = *
this; ++(*this);
return tmp; }
71 iterator &operator --()
72 { --itb; backward();
return *
this; }
73 iterator operator --(
int) { iterator tmp = *
this; --(*this);
return tmp; }
74 reference operator *()
const {
return *itb; }
76 bool operator ==(
const iterator &i)
const {
return itb == i.itb; }
77 bool operator !=(
const iterator &i)
const {
return !(i == *
this); }
79 sparse_sub_vector_iterator() {}
80 sparse_sub_vector_iterator(
const IT &it,
const IT &ite,
const SUBI &s)
81 : itb(it), itbe(ite), si(s) { forward(); }
82 sparse_sub_vector_iterator
83 (
const sparse_sub_vector_iterator<MIT, MIT, SUBI> &it)
84 : itb(it.itb), itbe(it.itbe), si(it.si) {}
85 sparse_sub_vector_iterator &
operator =
86 (
const sparse_sub_vector_iterator<MIT, MIT, SUBI> &it)
87 { itb = it.itb; itbe = it.itbe; si = it.si;
return *
this; }
91 template <
typename IT,
typename MIT,
typename SUBI>
92 void sparse_sub_vector_iterator<IT, MIT, SUBI>::forward()
93 {
while(itb!=itbe && index()==
size_type(-1)) { ++itb; } }
95 template <
typename IT,
typename MIT,
typename SUBI>
96 void sparse_sub_vector_iterator<IT, MIT, SUBI>::backward()
97 {
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;
103 typedef typename select_ref<typename linalg_traits<V>::const_iterator,
104 typename linalg_traits<V>::iterator, PT>::ref_type iterator;
105 typedef typename linalg_traits<this_type>::reference reference;
106 typedef typename linalg_traits<this_type>::porigin_type porigin_type;
108 iterator begin_, end_;
112 size_type size()
const {
return si.size(); }
115 {
return linalg_traits<V>::access(origin, begin_, end_, si.index(i)); }
117 sparse_sub_vector(V &v,
const SUBI &s) : begin_(vect_begin(v)),
118 end_(vect_end(v)), origin(linalg_origin(v)), si(s) {}
119 sparse_sub_vector(
const V &v,
const SUBI &s)
120 : begin_(vect_begin(const_cast<V &>(v))),
121 end_(vect_end(const_cast<V &>(v))),
122 origin(linalg_origin(const_cast<V &>(v))), si(s) {}
123 sparse_sub_vector() {}
124 sparse_sub_vector(
const sparse_sub_vector<CPT, SUBI> &cr)
125 : begin_(cr.begin_),end_(cr.end_),origin(cr.origin), si(cr.si) {}
128 template <
typename IT,
typename MIT,
typename SUBI,
typename ORG,
130 void set_to_begin(sparse_sub_vector_iterator<IT, MIT, SUBI> &it,
131 ORG o, sparse_sub_vector<PT, SUBI> *,
133 typedef sparse_sub_vector<PT, SUBI> VECT;
134 typedef typename linalg_traits<VECT>::V_reference ref_t;
135 set_to_begin(it.itb, o,
typename linalg_traits<VECT>::pV(), ref_t());
136 set_to_end(it.itbe, o,
typename linalg_traits<VECT>::pV(), ref_t());
139 template <
typename IT,
typename MIT,
typename SUBI,
typename ORG,
141 void set_to_begin(sparse_sub_vector_iterator<IT, MIT, SUBI> &it,
142 ORG o,
const sparse_sub_vector<PT, SUBI> *,
144 typedef sparse_sub_vector<PT, SUBI> VECT;
145 typedef typename linalg_traits<VECT>::V_reference ref_t;
146 set_to_begin(it.itb, o,
typename linalg_traits<VECT>::pV(), ref_t());
147 set_to_end(it.itbe, o,
typename linalg_traits<VECT>::pV(), ref_t());
151 template <
typename IT,
typename MIT,
typename SUBI,
typename ORG,
153 void set_to_end(sparse_sub_vector_iterator<IT, MIT, SUBI> &it,
154 ORG o, sparse_sub_vector<PT, SUBI> *, linalg_modifiable) {
155 typedef sparse_sub_vector<PT, SUBI> VECT;
156 typedef typename linalg_traits<VECT>::V_reference ref_t;
157 set_to_end(it.itb, o,
typename linalg_traits<VECT>::pV(), ref_t());
158 set_to_end(it.itbe, o,
typename linalg_traits<VECT>::pV(), ref_t());
161 template <
typename IT,
typename MIT,
typename SUBI,
typename ORG,
163 void set_to_end(sparse_sub_vector_iterator<IT, MIT, SUBI> &it,
164 ORG o,
const sparse_sub_vector<PT, SUBI> *,
166 typedef sparse_sub_vector<PT, SUBI> VECT;
167 typedef typename linalg_traits<VECT>::V_reference ref_t;
168 set_to_end(it.itb, o,
typename linalg_traits<VECT>::pV(), ref_t());
169 set_to_end(it.itbe, o,
typename linalg_traits<VECT>::pV(), ref_t());
173 template <
typename PT,
typename SUBI>
174 struct linalg_traits<sparse_sub_vector<PT, SUBI> > {
175 typedef sparse_sub_vector<PT, SUBI> this_type;
176 typedef this_type * pthis_type;
178 typedef typename std::iterator_traits<PT>::value_type V;
179 typedef typename linalg_and<typename index_is_sorted<SUBI>::bool_type,
180 typename linalg_traits<V>::index_sorted>::bool_type index_sorted;
181 typedef typename linalg_traits<V>::is_reference V_reference;
182 typedef typename linalg_traits<V>::origin_type origin_type;
183 typedef typename select_ref<
const origin_type *, origin_type *,
184 PT>::ref_type porigin_type;
185 typedef typename which_reference<PT>::is_reference is_reference;
186 typedef abstract_vector linalg_type;
187 typedef typename linalg_traits<V>::value_type value_type;
188 typedef typename select_ref<value_type,
typename
189 linalg_traits<V>::reference, PT>::ref_type reference;
190 typedef typename select_ref<typename linalg_traits<V>::const_iterator,
191 typename linalg_traits<V>::iterator, PT>::ref_type pre_iterator;
192 typedef typename select_ref<abstract_null_type,
193 sparse_sub_vector_iterator<pre_iterator,
195 PT>::ref_type iterator;
196 typedef sparse_sub_vector_iterator<typename linalg_traits<V>
197 ::const_iterator, pre_iterator, SUBI> const_iterator;
198 typedef abstract_sparse storage_type;
199 static size_type size(
const this_type &v) {
return v.size(); }
200 static iterator begin(this_type &v) {
205 if (!is_const_reference(is_reference()))
206 set_to_begin(it, v.origin, pthis_type(), is_reference());
211 static const_iterator begin(
const this_type &v) {
216 if (!is_const_reference(is_reference()))
217 set_to_begin(it, v.origin, pthis_type(), is_reference());
222 static iterator end(this_type &v) {
227 if (!is_const_reference(is_reference()))
228 set_to_end(it, v.origin, pthis_type(), is_reference());
233 static const_iterator end(
const this_type &v) {
238 if (!is_const_reference(is_reference()))
239 set_to_end(it, v.origin, pthis_type(), is_reference());
244 static origin_type* origin(this_type &v) {
return v.origin; }
245 static const origin_type* origin(
const this_type &v) {
return v.origin; }
246 static void clear(origin_type* o,
const iterator &begin_,
247 const iterator &end_) {
248 std::deque<size_type> ind;
249 iterator it = begin_;
250 for (; it != end_; ++it) ind.push_front(it.index());
251 for (; !(ind.empty()); ind.pop_back())
252 access(o, begin_, end_, ind.back()) = value_type(0);
254 static void do_clear(this_type &v) {
clear(v.origin, begin(v), end(v)); }
255 static value_type access(
const origin_type *o,
const const_iterator &it,
257 {
return linalg_traits<V>::access(o, it.itb, ite.itb, it.si.index(i)); }
258 static reference access(origin_type *o,
const iterator &it,
260 {
return linalg_traits<V>::access(o, it.itb, ite.itb, it.si.index(i)); }
263 template <
typename PT,
typename SUBI> std::ostream &
operator <<
264 (std::ostream &o,
const sparse_sub_vector<PT, SUBI>& m)
265 { gmm::write(o,m);
return o; }
271 template <
typename IT,
typename MIT,
typename SUBI>
272 struct skyline_sub_vector_iterator {
277 typedef std::iterator_traits<IT> traits_type;
278 typedef typename traits_type::value_type value_type;
279 typedef typename traits_type::pointer pointer;
280 typedef typename traits_type::reference reference;
281 typedef typename traits_type::difference_type difference_type;
282 typedef std::bidirectional_iterator_tag iterator_category;
284 typedef skyline_sub_vector_iterator<IT, MIT, SUBI> iterator;
287 {
return (itb.index() - si.min + si.step() - 1) / si.step(); }
289 iterator &operator ++()
290 { itb += si.step();
return *
this; }
291 iterator operator ++(
int) { iterator tmp = *
this; ++(*this);
return tmp; }
292 iterator &operator --()
293 { itb -= si.step();
return *
this; }
294 iterator operator --(
int) { iterator tmp = *
this; --(*this);
return tmp; }
296 iterator &operator +=(difference_type i)
297 { itb += si.step() * i;
return *
this; }
298 iterator &operator -=(difference_type i)
299 { itb -= si.step() * i;
return *
this; }
301 { iterator ii = *
this;
return (ii += i); }
303 { iterator ii = *
this;
return (ii -= i); }
304 difference_type
operator -(
const iterator &i)
const
305 {
return (itb - i.itb) / si.step(); }
307 reference operator *()
const {
return *itb; }
308 reference operator [](
int ii) {
return *(itb + ii * si.step()); }
310 bool operator ==(
const iterator &i)
const {
return index() == i.index();}
311 bool operator !=(
const iterator &i)
const {
return !(i == *
this); }
312 bool operator < (
const iterator &i)
const {
return index() < i.index();}
313 bool operator > (
const iterator &i)
const {
return index() > i.index();}
314 bool operator >=(
const iterator &i)
const {
return index() >= i.index();}
316 skyline_sub_vector_iterator() {}
317 skyline_sub_vector_iterator(
const IT &it,
const SUBI &s)
319 skyline_sub_vector_iterator
320 (
const skyline_sub_vector_iterator<MIT, MIT, SUBI> &it)
321 : itb(it.itb), si(it.si) {}
322 skyline_sub_vector_iterator &
323 operator =(
const skyline_sub_vector_iterator<MIT, MIT, SUBI> &it)
324 { itb=it.itb; si=it.si;
return *
this; }
327 template <
typename IT,
typename SUBI>
328 void update_for_sub_skyline(IT &it, IT &ite,
const SUBI &si) {
329 if (it.index() >= si.max || ite.index() <= si.min) { it = ite;
return; }
330 ptrdiff_t dec1 = si.min - it.index(), dec2 = ite.index() - si.max;
331 it += (dec1 < 0) ? ((si.step()-((-dec1) % si.step())) % si.step()) : dec1;
332 ite -= (dec2 < 0) ? -((-dec2) % si.step()) : dec2;
335 template <
typename PT,
typename SUBI>
struct skyline_sub_vector {
336 typedef skyline_sub_vector<PT, SUBI> this_type;
337 typedef typename std::iterator_traits<PT>::value_type V;
339 typedef typename select_ref<typename linalg_traits<V>::const_iterator,
340 typename linalg_traits<V>::iterator, PT>::ref_type iterator;
341 typedef typename linalg_traits<this_type>::reference reference;
342 typedef typename linalg_traits<this_type>::porigin_type porigin_type;
344 iterator begin_, end_;
348 size_type size()
const {
return si.size(); }
351 {
return linalg_traits<V>::access(origin, begin_, end_, si.index(i)); }
353 skyline_sub_vector(V &v,
const SUBI &s) : begin_(vect_begin(v)),
354 end_(vect_end(v)), origin(linalg_origin(v)), si(s) {
355 update_for_sub_skyline(begin_, end_, si);
357 skyline_sub_vector(
const V &v,
const SUBI &s)
358 : begin_(vect_begin(const_cast<V &>(v))),
359 end_(vect_end(const_cast<V &>(v))),
360 origin(linalg_origin(const_cast<V &>(v))), si(s) {
361 update_for_sub_skyline(begin_, end_, si);
363 skyline_sub_vector() {}
364 skyline_sub_vector(
const skyline_sub_vector<pV, SUBI> &cr)
365 : begin_(cr.begin_),end_(cr.end_),origin(cr.origin), si(cr.si) {}
368 template <
typename IT,
typename MIT,
typename SUBI,
typename ORG,
370 void set_to_begin(skyline_sub_vector_iterator<IT, MIT, SUBI> &it,
371 ORG o, skyline_sub_vector<PT, SUBI> *,
373 typedef skyline_sub_vector<PT, SUBI> VECT;
374 typedef typename linalg_traits<VECT>::V_reference ref_t;
376 set_to_begin(it.itb, o,
typename linalg_traits<VECT>::pV(), ref_t());
377 set_to_end(itbe, o,
typename linalg_traits<VECT>::pV(), ref_t());
378 update_for_sub_skyline(it.itb, itbe, it.si);
380 template <
typename IT,
typename MIT,
typename SUBI,
typename ORG,
382 void set_to_begin(skyline_sub_vector_iterator<IT, MIT, SUBI> &it,
383 ORG o,
const skyline_sub_vector<PT, SUBI> *,
385 typedef skyline_sub_vector<PT, SUBI> VECT;
386 typedef typename linalg_traits<VECT>::V_reference ref_t;
388 set_to_begin(it.itb, o,
typename linalg_traits<VECT>::pV(), ref_t());
389 set_to_end(itbe, o,
typename linalg_traits<VECT>::pV(), ref_t());
390 update_for_sub_skyline(it.itb, itbe, it.si);
393 template <
typename IT,
typename MIT,
typename SUBI,
typename ORG,
395 void set_to_end(skyline_sub_vector_iterator<IT, MIT, SUBI> &it,
396 ORG o, skyline_sub_vector<PT, SUBI> *,
398 typedef skyline_sub_vector<PT, SUBI> VECT;
399 typedef typename linalg_traits<VECT>::V_reference ref_t;
401 set_to_begin(itb, o,
typename linalg_traits<VECT>::pV(), ref_t());
402 set_to_end(it.itb, o,
typename linalg_traits<VECT>::pV(), ref_t());
403 update_for_sub_skyline(itb, it.itb, it.si);
405 template <
typename IT,
typename MIT,
typename SUBI,
typename ORG,
407 void set_to_end(skyline_sub_vector_iterator<IT, MIT, SUBI> &it,
408 ORG o,
const skyline_sub_vector<PT, SUBI> *,
410 typedef skyline_sub_vector<PT, SUBI> VECT;
411 typedef typename linalg_traits<VECT>::V_reference ref_t;
413 set_to_begin(itb, o,
typename linalg_traits<VECT>::pV(), ref_t());
414 set_to_end(it.itb, o,
typename linalg_traits<VECT>::pV(), ref_t());
415 update_for_sub_skyline(itb, it.itb, it.si);
419 template <
typename PT,
typename SUBI>
420 struct linalg_traits<skyline_sub_vector<PT, SUBI> > {
421 typedef skyline_sub_vector<PT, SUBI> this_type;
422 typedef this_type *pthis_type;
423 typedef typename std::iterator_traits<PT>::value_type V;
424 typedef typename linalg_traits<V>::is_reference V_reference;
425 typedef typename linalg_traits<V>::origin_type origin_type;
426 typedef typename select_ref<
const origin_type *,
427 origin_type *, PT>::ref_type porigin_type;
429 typedef typename which_reference<PT>::is_reference is_reference;
430 typedef abstract_vector linalg_type;
431 typedef typename linalg_traits<V>::value_type value_type;
432 typedef typename select_ref<value_type,
typename
433 linalg_traits<V>::reference, PT>::ref_type reference;
434 typedef typename linalg_traits<V>::const_iterator const_V_iterator;
435 typedef typename linalg_traits<V>::iterator V_iterator;
436 typedef typename select_ref<const_V_iterator,
437 V_iterator, PT>::ref_type pre_iterator;
438 typedef typename select_ref<abstract_null_type,
439 skyline_sub_vector_iterator<pre_iterator,
441 PT>::ref_type iterator;
442 typedef skyline_sub_vector_iterator<const_V_iterator, pre_iterator, SUBI>
444 typedef abstract_skyline storage_type;
445 typedef linalg_true index_sorted;
446 static size_type size(
const this_type &v) {
return v.size(); }
447 static iterator begin(this_type &v) {
451 if (!is_const_reference(is_reference()))
452 set_to_begin(it, v.origin, pthis_type(), is_reference());
455 static const_iterator begin(
const this_type &v) {
459 if (!is_const_reference(is_reference()))
460 set_to_begin(it, v.origin, pthis_type(), is_reference());
463 static iterator end(this_type &v) {
467 if (!is_const_reference(is_reference()))
468 set_to_end(it, v.origin, pthis_type(), is_reference());
471 static const_iterator end(
const this_type &v) {
475 if (!is_const_reference(is_reference()))
476 set_to_end(it, v.origin, pthis_type(), is_reference());
479 static origin_type* origin(this_type &v) {
return v.origin; }
480 static const origin_type* origin(
const this_type &v) {
return v.origin; }
481 static void clear(origin_type*,
const iterator &it,
const iterator &ite)
482 { std::fill(it, ite, value_type(0)); }
483 static void do_clear(this_type &v) {
clear(v.origin, begin(v), end(v)); }
484 static value_type access(
const origin_type *o,
const const_iterator &it,
486 {
return linalg_traits<V>::access(o, it.itb, ite.itb, it.si.index(i)); }
487 static reference access(origin_type *o,
const iterator &it,
489 {
return linalg_traits<V>::access(o, it.itb, ite.itb, it.si.index(i)); }
492 template <
typename PT,
typename SUBI> std::ostream &
operator <<
493 (std::ostream &o,
const skyline_sub_vector<PT, SUBI>& m)
494 { gmm::write(o,m);
return o; }
503 template <
typename PT,
typename SUBI,
typename st_type>
struct svrt_ir {
504 typedef abstract_null_type vector_type;
507 template <
typename PT>
508 struct svrt_ir<PT, sub_index, abstract_dense> {
509 typedef typename std::iterator_traits<PT>::value_type V;
510 typedef typename vect_ref_type<PT, V>::iterator iterator;
511 typedef tab_ref_index_ref_with_origin<iterator,
512 sub_index::const_iterator, V> vector_type;
515 template <
typename PT>
516 struct svrt_ir<PT, unsorted_sub_index, abstract_dense> {
517 typedef typename std::iterator_traits<PT>::value_type V;
518 typedef typename vect_ref_type<PT, V>::iterator iterator;
519 typedef tab_ref_index_ref_with_origin<iterator,
520 unsorted_sub_index::const_iterator, V> vector_type;
523 template <
typename PT>
524 struct svrt_ir<PT, sub_interval, abstract_dense> {
525 typedef typename std::iterator_traits<PT>::value_type V;
526 typedef typename vect_ref_type<PT, V>::iterator iterator;
527 typedef tab_ref_with_origin<iterator, V> vector_type;
530 template <
typename PT>
531 struct svrt_ir<PT, sub_slice, abstract_dense> {
532 typedef typename std::iterator_traits<PT>::value_type V;
533 typedef typename vect_ref_type<PT, V>::iterator iterator;
534 typedef tab_ref_reg_spaced_with_origin<iterator, V> vector_type;
537 template <
typename PT,
typename SUBI>
538 struct svrt_ir<PT, SUBI, abstract_skyline> {
539 typedef skyline_sub_vector<PT, SUBI> vector_type;
542 template <
typename PT>
543 struct svrt_ir<PT, sub_index, abstract_skyline> {
544 typedef sparse_sub_vector<PT, sub_index> vector_type;
547 template <
typename PT>
548 struct svrt_ir<PT, unsorted_sub_index, abstract_skyline> {
549 typedef sparse_sub_vector<PT, unsorted_sub_index> vector_type;
553 template <
typename PT,
typename SUBI>
554 struct svrt_ir<PT, SUBI, abstract_sparse> {
555 typedef sparse_sub_vector<PT, SUBI> vector_type;
558 template <
typename PT,
typename SUBI>
559 struct sub_vector_type {
560 typedef typename std::iterator_traits<PT>::value_type V;
561 typedef typename svrt_ir<PT, SUBI,
562 typename linalg_traits<V>::storage_type>::vector_type vector_type;
565 template <
typename V,
typename SUBI>
566 typename select_return<
567 typename sub_vector_type<const V *, SUBI>::vector_type,
568 typename sub_vector_type<V *, SUBI>::vector_type,
const V *>::return_type
569 sub_vector(
const V &v,
const SUBI &si) {
570 GMM_ASSERT2(si.last() <= vect_size(v),
571 "sub vector too large, " << si.last() <<
" > " << vect_size(v));
572 return typename select_return<
573 typename sub_vector_type<const V *, SUBI>::vector_type,
574 typename sub_vector_type<V *, SUBI>::vector_type,
const V *>::return_type
575 (linalg_cast(v), si);
578 template <
typename V,
typename SUBI>
579 typename select_return<
580 typename sub_vector_type<const V *, SUBI>::vector_type,
581 typename sub_vector_type<V *, SUBI>::vector_type, V *>::return_type
582 sub_vector(V &v,
const SUBI &si) {
583 GMM_ASSERT2(si.last() <= vect_size(v),
584 "sub vector too large, " << si.last() <<
" > " << vect_size(v));
585 return typename select_return<
586 typename sub_vector_type<const V *, SUBI>::vector_type,
587 typename sub_vector_type<V *, SUBI>::vector_type, V *>::return_type
588 (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