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(
void)
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(
void) {}
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(
const sparse_sub_vector_iterator<MIT, MIT,
83 SUBI> &it) : itb(it.itb), itbe(it.itbe), si(it.si) {}
86 template <
typename IT,
typename MIT,
typename SUBI>
87 void sparse_sub_vector_iterator<IT, MIT, SUBI>::forward(
void)
88 {
while(itb!=itbe && index()==
size_type(-1)) { ++itb; } }
90 template <
typename IT,
typename MIT,
typename SUBI>
91 void sparse_sub_vector_iterator<IT, MIT, SUBI>::backward(
void)
92 {
while(itb!=itbe && index()==
size_type(-1)) --itb; }
94 template <
typename PT,
typename SUBI>
struct sparse_sub_vector {
95 typedef sparse_sub_vector<PT, SUBI> this_type;
96 typedef typename std::iterator_traits<PT>::value_type V;
98 typedef typename select_ref<typename linalg_traits<V>::const_iterator,
99 typename linalg_traits<V>::iterator, PT>::ref_type iterator;
100 typedef typename linalg_traits<this_type>::reference reference;
101 typedef typename linalg_traits<this_type>::porigin_type porigin_type;
103 iterator begin_, end_;
107 size_type size(
void)
const {
return si.size(); }
109 reference operator[](size_type i)
const 110 {
return linalg_traits<V>::access(origin, begin_, end_, si.index(i)); }
112 sparse_sub_vector(V &v,
const SUBI &s) : begin_(vect_begin(v)),
113 end_(vect_end(v)), origin(linalg_origin(v)), si(s) {}
114 sparse_sub_vector(
const V &v,
const SUBI &s)
115 : begin_(vect_begin(const_cast<V &>(v))),
116 end_(vect_end(const_cast<V &>(v))),
117 origin(linalg_origin(const_cast<V &>(v))), si(s) {}
118 sparse_sub_vector() {}
119 sparse_sub_vector(
const sparse_sub_vector<CPT, SUBI> &cr)
120 : begin_(cr.begin_),end_(cr.end_),origin(cr.origin), si(cr.si) {}
123 template <
typename IT,
typename MIT,
typename SUBI,
typename ORG,
125 void set_to_begin(sparse_sub_vector_iterator<IT, MIT, SUBI> &it,
126 ORG o, sparse_sub_vector<PT, SUBI> *,
128 typedef sparse_sub_vector<PT, SUBI> VECT;
129 typedef typename linalg_traits<VECT>::V_reference ref_t;
130 set_to_begin(it.itb, o,
typename linalg_traits<VECT>::pV(), ref_t());
131 set_to_end(it.itbe, o,
typename linalg_traits<VECT>::pV(), ref_t());
134 template <
typename IT,
typename MIT,
typename SUBI,
typename ORG,
136 void set_to_begin(sparse_sub_vector_iterator<IT, MIT, SUBI> &it,
137 ORG o,
const sparse_sub_vector<PT, SUBI> *,
139 typedef sparse_sub_vector<PT, SUBI> VECT;
140 typedef typename linalg_traits<VECT>::V_reference ref_t;
141 set_to_begin(it.itb, o,
typename linalg_traits<VECT>::pV(), ref_t());
142 set_to_end(it.itbe, o,
typename linalg_traits<VECT>::pV(), ref_t());
146 template <
typename IT,
typename MIT,
typename SUBI,
typename ORG,
148 void set_to_end(sparse_sub_vector_iterator<IT, MIT, SUBI> &it,
149 ORG o, sparse_sub_vector<PT, SUBI> *, linalg_modifiable) {
150 typedef sparse_sub_vector<PT, SUBI> VECT;
151 typedef typename linalg_traits<VECT>::V_reference ref_t;
152 set_to_end(it.itb, o,
typename linalg_traits<VECT>::pV(), ref_t());
153 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,
const sparse_sub_vector<PT, SUBI> *,
161 typedef sparse_sub_vector<PT, SUBI> VECT;
162 typedef typename linalg_traits<VECT>::V_reference ref_t;
163 set_to_end(it.itb, o,
typename linalg_traits<VECT>::pV(), ref_t());
164 set_to_end(it.itbe, o,
typename linalg_traits<VECT>::pV(), ref_t());
168 template <
typename PT,
typename SUBI>
169 struct linalg_traits<sparse_sub_vector<PT, SUBI> > {
170 typedef sparse_sub_vector<PT, SUBI> this_type;
171 typedef this_type * pthis_type;
173 typedef typename std::iterator_traits<PT>::value_type V;
174 typedef typename linalg_and<typename index_is_sorted<SUBI>::bool_type,
175 typename linalg_traits<V>::index_sorted>::bool_type index_sorted;
176 typedef typename linalg_traits<V>::is_reference V_reference;
177 typedef typename linalg_traits<V>::origin_type origin_type;
178 typedef typename select_ref<
const origin_type *, origin_type *,
179 PT>::ref_type porigin_type;
180 typedef typename which_reference<PT>::is_reference is_reference;
181 typedef abstract_vector linalg_type;
182 typedef typename linalg_traits<V>::value_type value_type;
183 typedef typename select_ref<value_type,
typename 184 linalg_traits<V>::reference, PT>::ref_type reference;
185 typedef typename select_ref<typename linalg_traits<V>::const_iterator,
186 typename linalg_traits<V>::iterator, PT>::ref_type pre_iterator;
187 typedef typename select_ref<abstract_null_type,
188 sparse_sub_vector_iterator<pre_iterator, pre_iterator, SUBI>,
189 PT>::ref_type iterator;
190 typedef sparse_sub_vector_iterator<typename linalg_traits<V>
191 ::const_iterator, pre_iterator, SUBI> const_iterator;
192 typedef abstract_sparse storage_type;
193 static size_type size(
const this_type &v) {
return v.size(); }
194 static iterator begin(this_type &v) {
196 it.itb = v.begin_; it.itbe = v.end_; it.si = v.si;
197 if (!is_const_reference(is_reference()))
198 set_to_begin(it, v.origin, pthis_type(), is_reference());
202 static const_iterator begin(
const this_type &v) {
203 const_iterator it; it.itb = v.begin_; it.itbe = v.end_; it.si = v.si;
204 if (!is_const_reference(is_reference()))
205 { set_to_begin(it, v.origin, pthis_type(), is_reference()); }
209 static iterator end(this_type &v) {
211 it.itb = v.end_; it.itbe = v.end_; it.si = v.si;
212 if (!is_const_reference(is_reference()))
213 set_to_end(it, v.origin, pthis_type(), is_reference());
217 static const_iterator end(
const this_type &v) {
218 const_iterator it; it.itb = v.end_; it.itbe = v.end_; it.si = v.si;
219 if (!is_const_reference(is_reference()))
220 set_to_end(it, v.origin, pthis_type(), is_reference());
224 static origin_type* origin(this_type &v) {
return v.origin; }
225 static const origin_type* origin(
const this_type &v) {
return v.origin; }
226 static void clear(origin_type* o,
const iterator &begin_,
227 const iterator &end_) {
228 std::deque<size_type> ind;
229 iterator it = begin_;
230 for (; it != end_; ++it) ind.push_front(it.index());
231 for (; !(ind.empty()); ind.pop_back())
232 access(o, begin_, end_, ind.back()) = value_type(0);
234 static void do_clear(this_type &v) {
clear(v.origin, begin(v), end(v)); }
235 static value_type access(
const origin_type *o,
const const_iterator &it,
236 const const_iterator &ite, size_type i)
237 {
return linalg_traits<V>::access(o, it.itb, ite.itb, it.si.index(i)); }
238 static reference access(origin_type *o,
const iterator &it,
239 const iterator &ite, size_type i)
240 {
return linalg_traits<V>::access(o, it.itb, ite.itb, it.si.index(i)); }
243 template <
typename PT,
typename SUBI> std::ostream &
operator <<
244 (std::ostream &o,
const sparse_sub_vector<PT, SUBI>& m)
245 { gmm::write(o,m);
return o; }
251 template <
typename IT,
typename MIT,
typename SUBI>
252 struct skyline_sub_vector_iterator {
257 typedef std::iterator_traits<IT> traits_type;
258 typedef typename traits_type::value_type value_type;
259 typedef typename traits_type::pointer pointer;
260 typedef typename traits_type::reference reference;
261 typedef typename traits_type::difference_type difference_type;
262 typedef std::bidirectional_iterator_tag iterator_category;
264 typedef skyline_sub_vector_iterator<IT, MIT, SUBI> iterator;
266 size_type index(
void)
const 267 {
return (itb.index() - si.min + si.step() - 1) / si.step(); }
269 iterator &operator ++()
270 { itb += si.step();
return *
this; }
271 iterator operator ++(
int) { iterator tmp = *
this; ++(*this);
return tmp; }
272 iterator &operator --()
273 { itb -= si.step();
return *
this; }
274 iterator operator --(
int) { iterator tmp = *
this; --(*this);
return tmp; }
276 iterator &operator +=(difference_type i)
277 { itb += si.step() * i;
return *
this; }
278 iterator &operator -=(difference_type i)
279 { itb -= si.step() * i;
return *
this; }
281 { iterator ii = *
this;
return (ii += i); }
283 { iterator ii = *
this;
return (ii -= i); }
284 difference_type
operator -(
const iterator &i)
const 285 {
return (itb - i.itb) / si.step(); }
287 reference operator *()
const {
return *itb; }
288 reference operator [](
int ii) {
return *(itb + ii * si.step()); }
290 bool operator ==(
const iterator &i)
const {
return index() == i.index();}
291 bool operator !=(
const iterator &i)
const {
return !(i == *
this); }
292 bool operator < (
const iterator &i)
const {
return index() < i.index();}
294 skyline_sub_vector_iterator(
void) {}
295 skyline_sub_vector_iterator(
const IT &it,
const SUBI &s)
297 skyline_sub_vector_iterator(
const skyline_sub_vector_iterator<MIT, MIT,
298 SUBI> &it) : itb(it.itb), si(it.si) {}
301 template <
typename IT,
typename SUBI>
302 void update_for_sub_skyline(IT &it, IT &ite,
const SUBI &si) {
303 if (it.index() >= si.max || ite.index() <= si.min) { it = ite;
return; }
304 ptrdiff_t dec1 = si.min - it.index(), dec2 = ite.index() - si.max;
305 it += (dec1 < 0) ? ((si.step()-((-dec1) % si.step())) % si.step()) : dec1;
306 ite -= (dec2 < 0) ? -((-dec2) % si.step()) : dec2;
309 template <
typename PT,
typename SUBI>
struct skyline_sub_vector {
310 typedef skyline_sub_vector<PT, SUBI> this_type;
311 typedef typename std::iterator_traits<PT>::value_type V;
313 typedef typename select_ref<typename linalg_traits<V>::const_iterator,
314 typename linalg_traits<V>::iterator, PT>::ref_type iterator;
315 typedef typename linalg_traits<this_type>::reference reference;
316 typedef typename linalg_traits<this_type>::porigin_type porigin_type;
318 iterator begin_, end_;
322 size_type size(
void)
const {
return si.size(); }
324 reference operator[](size_type i)
const 325 {
return linalg_traits<V>::access(origin, begin_, end_, si.index(i)); }
327 skyline_sub_vector(V &v,
const SUBI &s) : begin_(vect_begin(v)),
328 end_(vect_end(v)), origin(linalg_origin(v)), si(s) {
329 update_for_sub_skyline(begin_, end_, si);
331 skyline_sub_vector(
const V &v,
const SUBI &s)
332 : begin_(vect_begin(const_cast<V &>(v))),
333 end_(vect_end(const_cast<V &>(v))),
334 origin(linalg_origin(const_cast<V &>(v))), si(s) {
335 update_for_sub_skyline(begin_, end_, si);
337 skyline_sub_vector() {}
338 skyline_sub_vector(
const skyline_sub_vector<pV, SUBI> &cr)
339 : begin_(cr.begin_),end_(cr.end_),origin(cr.origin), si(cr.si) {}
342 template <
typename IT,
typename MIT,
typename SUBI,
typename ORG,
344 void set_to_begin(skyline_sub_vector_iterator<IT, MIT, SUBI> &it,
345 ORG o, skyline_sub_vector<PT, SUBI> *,
347 typedef skyline_sub_vector<PT, SUBI> VECT;
348 typedef typename linalg_traits<VECT>::V_reference ref_t;
350 set_to_begin(it.itb, o,
typename linalg_traits<VECT>::pV(), ref_t());
351 set_to_end(itbe, o,
typename linalg_traits<VECT>::pV(), ref_t());
352 update_for_sub_skyline(it.itb, itbe, it.si);
354 template <
typename IT,
typename MIT,
typename SUBI,
typename ORG,
356 void set_to_begin(skyline_sub_vector_iterator<IT, MIT, SUBI> &it,
357 ORG o,
const skyline_sub_vector<PT, SUBI> *,
359 typedef skyline_sub_vector<PT, SUBI> VECT;
360 typedef typename linalg_traits<VECT>::V_reference ref_t;
362 set_to_begin(it.itb, o,
typename linalg_traits<VECT>::pV(), ref_t());
363 set_to_end(itbe, o,
typename linalg_traits<VECT>::pV(), ref_t());
364 update_for_sub_skyline(it.itb, itbe, it.si);
367 template <
typename IT,
typename MIT,
typename SUBI,
typename ORG,
369 void set_to_end(skyline_sub_vector_iterator<IT, MIT, SUBI> &it,
370 ORG o, skyline_sub_vector<PT, SUBI> *,
372 typedef skyline_sub_vector<PT, SUBI> VECT;
373 typedef typename linalg_traits<VECT>::V_reference ref_t;
375 set_to_begin(itb, o,
typename linalg_traits<VECT>::pV(), ref_t());
376 set_to_end(it.itb, o,
typename linalg_traits<VECT>::pV(), ref_t());
377 update_for_sub_skyline(itb, it.itb, it.si);
379 template <
typename IT,
typename MIT,
typename SUBI,
typename ORG,
381 void set_to_end(skyline_sub_vector_iterator<IT, MIT, SUBI> &it,
382 ORG o,
const skyline_sub_vector<PT, SUBI> *,
384 typedef skyline_sub_vector<PT, SUBI> VECT;
385 typedef typename linalg_traits<VECT>::V_reference ref_t;
387 set_to_begin(itb, o,
typename linalg_traits<VECT>::pV(), ref_t());
388 set_to_end(it.itb, o,
typename linalg_traits<VECT>::pV(), ref_t());
389 update_for_sub_skyline(itb, it.itb, it.si);
393 template <
typename PT,
typename SUBI>
394 struct linalg_traits<skyline_sub_vector<PT, SUBI> > {
395 typedef skyline_sub_vector<PT, SUBI> this_type;
396 typedef this_type *pthis_type;
397 typedef typename std::iterator_traits<PT>::value_type V;
398 typedef typename linalg_traits<V>::is_reference V_reference;
399 typedef typename linalg_traits<V>::origin_type origin_type;
400 typedef typename select_ref<
const origin_type *, origin_type *,
401 PT>::ref_type porigin_type;
403 typedef typename which_reference<PT>::is_reference is_reference;
404 typedef abstract_vector linalg_type;
405 typedef typename linalg_traits<V>::value_type value_type;
406 typedef typename select_ref<value_type,
typename 407 linalg_traits<V>::reference, PT>::ref_type reference;
408 typedef typename linalg_traits<V>::const_iterator const_V_iterator;
409 typedef typename linalg_traits<V>::iterator V_iterator;
410 typedef typename select_ref<const_V_iterator, V_iterator,
411 PT>::ref_type pre_iterator;
412 typedef typename select_ref<abstract_null_type,
413 skyline_sub_vector_iterator<pre_iterator, pre_iterator, SUBI>,
414 PT>::ref_type iterator;
415 typedef skyline_sub_vector_iterator<const_V_iterator, pre_iterator, SUBI>
417 typedef abstract_skyline storage_type;
418 typedef linalg_true index_sorted;
419 static size_type size(
const this_type &v) {
return v.size(); }
420 static iterator begin(this_type &v) {
422 it.itb = v.begin_; it.si = v.si;
423 if (!is_const_reference(is_reference()))
424 set_to_begin(it, v.origin, pthis_type(), is_reference());
427 static const_iterator begin(
const this_type &v) {
428 const_iterator it; it.itb = v.begin_; it.si = v.si;
429 if (!is_const_reference(is_reference()))
430 { set_to_begin(it, v.origin, pthis_type(), is_reference()); }
433 static iterator end(this_type &v) {
435 it.itb = v.end_; it.si = v.si;
436 if (!is_const_reference(is_reference()))
437 set_to_end(it, v.origin, pthis_type(), is_reference());
440 static const_iterator end(
const this_type &v) {
441 const_iterator it; it.itb = v.end_; it.si = v.si;
442 if (!is_const_reference(is_reference()))
443 set_to_end(it, v.origin, pthis_type(), is_reference());
446 static origin_type* origin(this_type &v) {
return v.origin; }
447 static const origin_type* origin(
const this_type &v) {
return v.origin; }
448 static void clear(origin_type*,
const iterator &it,
const iterator &ite)
449 { std::fill(it, ite, value_type(0)); }
450 static void do_clear(this_type &v) {
clear(v.origin, begin(v), end(v)); }
451 static value_type access(
const origin_type *o,
const const_iterator &it,
452 const const_iterator &ite, size_type i)
453 {
return linalg_traits<V>::access(o, it.itb, ite.itb, it.si.index(i)); }
454 static reference access(origin_type *o,
const iterator &it,
455 const iterator &ite, size_type i)
456 {
return linalg_traits<V>::access(o, it.itb, ite.itb, it.si.index(i)); }
459 template <
typename PT,
typename SUBI> std::ostream &
operator <<
460 (std::ostream &o,
const skyline_sub_vector<PT, SUBI>& m)
461 { gmm::write(o,m);
return o; }
470 template <
typename PT,
typename SUBI,
typename st_type>
struct svrt_ir {
471 typedef abstract_null_type vector_type;
474 template <
typename PT>
475 struct svrt_ir<PT, sub_index, abstract_dense> {
476 typedef typename std::iterator_traits<PT>::value_type V;
477 typedef typename vect_ref_type<PT, V>::iterator iterator;
478 typedef tab_ref_index_ref_with_origin<iterator,
479 sub_index::const_iterator, V> vector_type;
482 template <
typename PT>
483 struct svrt_ir<PT, unsorted_sub_index, abstract_dense> {
484 typedef typename std::iterator_traits<PT>::value_type V;
485 typedef typename vect_ref_type<PT, V>::iterator iterator;
486 typedef tab_ref_index_ref_with_origin<iterator,
487 unsorted_sub_index::const_iterator, V> vector_type;
490 template <
typename PT>
491 struct svrt_ir<PT, sub_interval, abstract_dense> {
492 typedef typename std::iterator_traits<PT>::value_type V;
493 typedef typename vect_ref_type<PT, V>::iterator iterator;
494 typedef tab_ref_with_origin<iterator, V> vector_type;
497 template <
typename PT>
498 struct svrt_ir<PT, sub_slice, abstract_dense> {
499 typedef typename std::iterator_traits<PT>::value_type V;
500 typedef typename vect_ref_type<PT, V>::iterator iterator;
501 typedef tab_ref_reg_spaced_with_origin<iterator, V> vector_type;
504 template <
typename PT,
typename SUBI>
505 struct svrt_ir<PT, SUBI, abstract_skyline> {
506 typedef skyline_sub_vector<PT, SUBI> vector_type;
509 template <
typename PT>
510 struct svrt_ir<PT, sub_index, abstract_skyline> {
511 typedef sparse_sub_vector<PT, sub_index> vector_type;
514 template <
typename PT>
515 struct svrt_ir<PT, unsorted_sub_index, abstract_skyline> {
516 typedef sparse_sub_vector<PT, unsorted_sub_index> vector_type;
520 template <
typename PT,
typename SUBI>
521 struct svrt_ir<PT, SUBI, abstract_sparse> {
522 typedef sparse_sub_vector<PT, SUBI> vector_type;
525 template <
typename PT,
typename SUBI>
526 struct sub_vector_type {
527 typedef typename std::iterator_traits<PT>::value_type V;
528 typedef typename svrt_ir<PT, SUBI,
529 typename linalg_traits<V>::storage_type>::vector_type vector_type;
532 template <
typename V,
typename SUBI>
533 typename select_return<
534 typename sub_vector_type<const V *, SUBI>::vector_type,
535 typename sub_vector_type<V *, SUBI>::vector_type,
const V *>::return_type
536 sub_vector(
const V &v,
const SUBI &si) {
537 GMM_ASSERT2(si.last() <= vect_size(v),
538 "sub vector too large, " << si.last() <<
" > " << vect_size(v));
539 return typename select_return<
540 typename sub_vector_type<const V *, SUBI>::vector_type,
541 typename sub_vector_type<V *, SUBI>::vector_type,
const V *>::return_type
542 (linalg_cast(v), si);
545 template <
typename V,
typename SUBI>
546 typename select_return<
547 typename sub_vector_type<const V *, SUBI>::vector_type,
548 typename sub_vector_type<V *, SUBI>::vector_type, V *>::return_type
549 sub_vector(V &v,
const SUBI &si) {
550 GMM_ASSERT2(si.last() <= vect_size(v),
551 "sub vector too large, " << si.last() <<
" > " << vect_size(v));
552 return typename select_return<
553 typename sub_vector_type<const V *, SUBI>::vector_type,
554 typename sub_vector_type<V *, SUBI>::vector_type, V *>::return_type
555 (linalg_cast(v), si);
560 #endif // GMM_SUB_VECTOR_H__ rational_fraction< T > operator+(const polynomial< T > &P, const rational_fraction< T > &Q)
Add Q to P.
rational_fraction< T > operator-(const polynomial< T > &P, const rational_fraction< T > &Q)
Subtract Q from P.
size_t size_type
used as the common size type in the library
void clear(L &l)
clear (fill with zeros) a vector or matrix.
gmm interface for STL vectors.