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
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(
void)
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(
void)
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(
void)
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, pre_iterator, SUBI>,
194 PT>::ref_type iterator;
195 typedef sparse_sub_vector_iterator<typename linalg_traits<V>
196 ::const_iterator, pre_iterator, SUBI> const_iterator;
197 typedef abstract_sparse storage_type;
198 static size_type size(
const this_type &v) {
return v.size(); }
199 static iterator begin(this_type &v) {
201 it.itb = v.begin_; it.itbe = v.end_; it.si = v.si;
202 if (!is_const_reference(is_reference()))
203 set_to_begin(it, v.origin, pthis_type(), is_reference());
207 static const_iterator begin(
const this_type &v) {
208 const_iterator it; it.itb = v.begin_; it.itbe = v.end_; it.si = v.si;
209 if (!is_const_reference(is_reference()))
210 { set_to_begin(it, v.origin, pthis_type(), is_reference()); }
214 static iterator end(this_type &v) {
216 it.itb = v.end_; it.itbe = v.end_; it.si = v.si;
217 if (!is_const_reference(is_reference()))
218 set_to_end(it, v.origin, pthis_type(), is_reference());
222 static const_iterator end(
const this_type &v) {
223 const_iterator it; it.itb = v.end_; it.itbe = v.end_; it.si = v.si;
224 if (!is_const_reference(is_reference()))
225 set_to_end(it, v.origin, pthis_type(), is_reference());
229 static origin_type* origin(this_type &v) {
return v.origin; }
230 static const origin_type* origin(
const this_type &v) {
return v.origin; }
231 static void clear(origin_type* o,
const iterator &begin_,
232 const iterator &end_) {
233 std::deque<size_type> ind;
234 iterator it = begin_;
235 for (; it != end_; ++it) ind.push_front(it.index());
236 for (; !(ind.empty()); ind.pop_back())
237 access(o, begin_, end_, ind.back()) = value_type(0);
239 static void do_clear(this_type &v) {
clear(v.origin, begin(v), end(v)); }
240 static value_type access(
const origin_type *o,
const const_iterator &it,
242 {
return linalg_traits<V>::access(o, it.itb, ite.itb, it.si.index(i)); }
243 static reference access(origin_type *o,
const iterator &it,
245 {
return linalg_traits<V>::access(o, it.itb, ite.itb, it.si.index(i)); }
248 template <
typename PT,
typename SUBI> std::ostream &
operator <<
249 (std::ostream &o,
const sparse_sub_vector<PT, SUBI>& m)
250 { gmm::write(o,m);
return o; }
256 template <
typename IT,
typename MIT,
typename SUBI>
257 struct skyline_sub_vector_iterator {
262 typedef std::iterator_traits<IT> traits_type;
263 typedef typename traits_type::value_type value_type;
264 typedef typename traits_type::pointer pointer;
265 typedef typename traits_type::reference reference;
266 typedef typename traits_type::difference_type difference_type;
267 typedef std::bidirectional_iterator_tag iterator_category;
269 typedef skyline_sub_vector_iterator<IT, MIT, SUBI> iterator;
272 {
return (itb.index() - si.min + si.step() - 1) / si.step(); }
274 iterator &operator ++()
275 { itb += si.step();
return *
this; }
276 iterator operator ++(
int) { iterator tmp = *
this; ++(*this);
return tmp; }
277 iterator &operator --()
278 { itb -= si.step();
return *
this; }
279 iterator operator --(
int) { iterator tmp = *
this; --(*this);
return tmp; }
281 iterator &operator +=(difference_type i)
282 { itb += si.step() * i;
return *
this; }
283 iterator &operator -=(difference_type i)
284 { itb -= si.step() * i;
return *
this; }
286 { iterator ii = *
this;
return (ii += i); }
288 { iterator ii = *
this;
return (ii -= i); }
289 difference_type
operator -(
const iterator &i)
const
290 {
return (itb - i.itb) / si.step(); }
292 reference operator *()
const {
return *itb; }
293 reference operator [](
int ii) {
return *(itb + ii * si.step()); }
295 bool operator ==(
const iterator &i)
const {
return index() == i.index();}
296 bool operator !=(
const iterator &i)
const {
return !(i == *
this); }
297 bool operator < (
const iterator &i)
const {
return index() < i.index();}
299 skyline_sub_vector_iterator(
void) {}
300 skyline_sub_vector_iterator(
const IT &it,
const SUBI &s)
302 skyline_sub_vector_iterator(
const skyline_sub_vector_iterator<MIT, MIT,
303 SUBI> &it) : itb(it.itb), si(it.si) {}
306 template <
typename IT,
typename SUBI>
307 void update_for_sub_skyline(IT &it, IT &ite,
const SUBI &si) {
308 if (it.index() >= si.max || ite.index() <= si.min) { it = ite;
return; }
309 ptrdiff_t dec1 = si.min - it.index(), dec2 = ite.index() - si.max;
310 it += (dec1 < 0) ? ((si.step()-((-dec1) % si.step())) % si.step()) : dec1;
311 ite -= (dec2 < 0) ? -((-dec2) % si.step()) : dec2;
314 template <
typename PT,
typename SUBI>
struct skyline_sub_vector {
315 typedef skyline_sub_vector<PT, SUBI> this_type;
316 typedef typename std::iterator_traits<PT>::value_type V;
318 typedef typename select_ref<typename linalg_traits<V>::const_iterator,
319 typename linalg_traits<V>::iterator, PT>::ref_type iterator;
320 typedef typename linalg_traits<this_type>::reference reference;
321 typedef typename linalg_traits<this_type>::porigin_type porigin_type;
323 iterator begin_, end_;
327 size_type size(
void)
const {
return si.size(); }
330 {
return linalg_traits<V>::access(origin, begin_, end_, si.index(i)); }
332 skyline_sub_vector(V &v,
const SUBI &s) : begin_(vect_begin(v)),
333 end_(vect_end(v)), origin(linalg_origin(v)), si(s) {
334 update_for_sub_skyline(begin_, end_, si);
336 skyline_sub_vector(
const V &v,
const SUBI &s)
337 : begin_(vect_begin(const_cast<V &>(v))),
338 end_(vect_end(const_cast<V &>(v))),
339 origin(linalg_origin(const_cast<V &>(v))), si(s) {
340 update_for_sub_skyline(begin_, end_, si);
342 skyline_sub_vector() {}
343 skyline_sub_vector(
const skyline_sub_vector<pV, SUBI> &cr)
344 : begin_(cr.begin_),end_(cr.end_),origin(cr.origin), si(cr.si) {}
347 template <
typename IT,
typename MIT,
typename SUBI,
typename ORG,
349 void set_to_begin(skyline_sub_vector_iterator<IT, MIT, SUBI> &it,
350 ORG o, skyline_sub_vector<PT, SUBI> *,
352 typedef skyline_sub_vector<PT, SUBI> VECT;
353 typedef typename linalg_traits<VECT>::V_reference ref_t;
355 set_to_begin(it.itb, o,
typename linalg_traits<VECT>::pV(), ref_t());
356 set_to_end(itbe, o,
typename linalg_traits<VECT>::pV(), ref_t());
357 update_for_sub_skyline(it.itb, itbe, it.si);
359 template <
typename IT,
typename MIT,
typename SUBI,
typename ORG,
361 void set_to_begin(skyline_sub_vector_iterator<IT, MIT, SUBI> &it,
362 ORG o,
const skyline_sub_vector<PT, SUBI> *,
364 typedef skyline_sub_vector<PT, SUBI> VECT;
365 typedef typename linalg_traits<VECT>::V_reference ref_t;
367 set_to_begin(it.itb, o,
typename linalg_traits<VECT>::pV(), ref_t());
368 set_to_end(itbe, o,
typename linalg_traits<VECT>::pV(), ref_t());
369 update_for_sub_skyline(it.itb, itbe, it.si);
372 template <
typename IT,
typename MIT,
typename SUBI,
typename ORG,
374 void set_to_end(skyline_sub_vector_iterator<IT, MIT, SUBI> &it,
375 ORG o, skyline_sub_vector<PT, SUBI> *,
377 typedef skyline_sub_vector<PT, SUBI> VECT;
378 typedef typename linalg_traits<VECT>::V_reference ref_t;
380 set_to_begin(itb, o,
typename linalg_traits<VECT>::pV(), ref_t());
381 set_to_end(it.itb, o,
typename linalg_traits<VECT>::pV(), ref_t());
382 update_for_sub_skyline(itb, it.itb, it.si);
384 template <
typename IT,
typename MIT,
typename SUBI,
typename ORG,
386 void set_to_end(skyline_sub_vector_iterator<IT, MIT, SUBI> &it,
387 ORG o,
const skyline_sub_vector<PT, SUBI> *,
389 typedef skyline_sub_vector<PT, SUBI> VECT;
390 typedef typename linalg_traits<VECT>::V_reference ref_t;
392 set_to_begin(itb, o,
typename linalg_traits<VECT>::pV(), ref_t());
393 set_to_end(it.itb, o,
typename linalg_traits<VECT>::pV(), ref_t());
394 update_for_sub_skyline(itb, it.itb, it.si);
398 template <
typename PT,
typename SUBI>
399 struct linalg_traits<skyline_sub_vector<PT, SUBI> > {
400 typedef skyline_sub_vector<PT, SUBI> this_type;
401 typedef this_type *pthis_type;
402 typedef typename std::iterator_traits<PT>::value_type V;
403 typedef typename linalg_traits<V>::is_reference V_reference;
404 typedef typename linalg_traits<V>::origin_type origin_type;
405 typedef typename select_ref<
const origin_type *, origin_type *,
406 PT>::ref_type porigin_type;
408 typedef typename which_reference<PT>::is_reference is_reference;
409 typedef abstract_vector linalg_type;
410 typedef typename linalg_traits<V>::value_type value_type;
411 typedef typename select_ref<value_type,
typename
412 linalg_traits<V>::reference, PT>::ref_type reference;
413 typedef typename linalg_traits<V>::const_iterator const_V_iterator;
414 typedef typename linalg_traits<V>::iterator V_iterator;
415 typedef typename select_ref<const_V_iterator, V_iterator,
416 PT>::ref_type pre_iterator;
417 typedef typename select_ref<abstract_null_type,
418 skyline_sub_vector_iterator<pre_iterator, pre_iterator, SUBI>,
419 PT>::ref_type iterator;
420 typedef skyline_sub_vector_iterator<const_V_iterator, pre_iterator, SUBI>
422 typedef abstract_skyline storage_type;
423 typedef linalg_true index_sorted;
424 static size_type size(
const this_type &v) {
return v.size(); }
425 static iterator begin(this_type &v) {
427 it.itb = v.begin_; it.si = v.si;
428 if (!is_const_reference(is_reference()))
429 set_to_begin(it, v.origin, pthis_type(), is_reference());
432 static const_iterator begin(
const this_type &v) {
433 const_iterator it; it.itb = v.begin_; it.si = v.si;
434 if (!is_const_reference(is_reference()))
435 { set_to_begin(it, v.origin, pthis_type(), is_reference()); }
438 static iterator end(this_type &v) {
440 it.itb = v.end_; it.si = v.si;
441 if (!is_const_reference(is_reference()))
442 set_to_end(it, v.origin, pthis_type(), is_reference());
445 static const_iterator end(
const this_type &v) {
446 const_iterator it; it.itb = v.end_; it.si = v.si;
447 if (!is_const_reference(is_reference()))
448 set_to_end(it, v.origin, pthis_type(), is_reference());
451 static origin_type* origin(this_type &v) {
return v.origin; }
452 static const origin_type* origin(
const this_type &v) {
return v.origin; }
453 static void clear(origin_type*,
const iterator &it,
const iterator &ite)
454 { std::fill(it, ite, value_type(0)); }
455 static void do_clear(this_type &v) {
clear(v.origin, begin(v), end(v)); }
456 static value_type access(
const origin_type *o,
const const_iterator &it,
458 {
return linalg_traits<V>::access(o, it.itb, ite.itb, it.si.index(i)); }
459 static reference access(origin_type *o,
const iterator &it,
461 {
return linalg_traits<V>::access(o, it.itb, ite.itb, it.si.index(i)); }
464 template <
typename PT,
typename SUBI> std::ostream &
operator <<
465 (std::ostream &o,
const skyline_sub_vector<PT, SUBI>& m)
466 { gmm::write(o,m);
return o; }
475 template <
typename PT,
typename SUBI,
typename st_type>
struct svrt_ir {
476 typedef abstract_null_type vector_type;
479 template <
typename PT>
480 struct svrt_ir<PT, sub_index, abstract_dense> {
481 typedef typename std::iterator_traits<PT>::value_type V;
482 typedef typename vect_ref_type<PT, V>::iterator iterator;
483 typedef tab_ref_index_ref_with_origin<iterator,
484 sub_index::const_iterator, V> vector_type;
487 template <
typename PT>
488 struct svrt_ir<PT, unsorted_sub_index, abstract_dense> {
489 typedef typename std::iterator_traits<PT>::value_type V;
490 typedef typename vect_ref_type<PT, V>::iterator iterator;
491 typedef tab_ref_index_ref_with_origin<iterator,
492 unsorted_sub_index::const_iterator, V> vector_type;
495 template <
typename PT>
496 struct svrt_ir<PT, sub_interval, abstract_dense> {
497 typedef typename std::iterator_traits<PT>::value_type V;
498 typedef typename vect_ref_type<PT, V>::iterator iterator;
499 typedef tab_ref_with_origin<iterator, V> vector_type;
502 template <
typename PT>
503 struct svrt_ir<PT, sub_slice, abstract_dense> {
504 typedef typename std::iterator_traits<PT>::value_type V;
505 typedef typename vect_ref_type<PT, V>::iterator iterator;
506 typedef tab_ref_reg_spaced_with_origin<iterator, V> vector_type;
509 template <
typename PT,
typename SUBI>
510 struct svrt_ir<PT, SUBI, abstract_skyline> {
511 typedef skyline_sub_vector<PT, SUBI> vector_type;
514 template <
typename PT>
515 struct svrt_ir<PT, sub_index, abstract_skyline> {
516 typedef sparse_sub_vector<PT, sub_index> vector_type;
519 template <
typename PT>
520 struct svrt_ir<PT, unsorted_sub_index, abstract_skyline> {
521 typedef sparse_sub_vector<PT, unsorted_sub_index> vector_type;
525 template <
typename PT,
typename SUBI>
526 struct svrt_ir<PT, SUBI, abstract_sparse> {
527 typedef sparse_sub_vector<PT, SUBI> vector_type;
530 template <
typename PT,
typename SUBI>
531 struct sub_vector_type {
532 typedef typename std::iterator_traits<PT>::value_type V;
533 typedef typename svrt_ir<PT, SUBI,
534 typename linalg_traits<V>::storage_type>::vector_type vector_type;
537 template <
typename V,
typename SUBI>
538 typename select_return<
539 typename sub_vector_type<const V *, SUBI>::vector_type,
540 typename sub_vector_type<V *, SUBI>::vector_type,
const V *>::return_type
541 sub_vector(
const V &v,
const SUBI &si) {
542 GMM_ASSERT2(si.last() <= vect_size(v),
543 "sub vector too large, " << si.last() <<
" > " << vect_size(v));
544 return typename select_return<
545 typename sub_vector_type<const V *, SUBI>::vector_type,
546 typename sub_vector_type<V *, SUBI>::vector_type,
const V *>::return_type
547 (linalg_cast(v), si);
550 template <
typename V,
typename SUBI>
551 typename select_return<
552 typename sub_vector_type<const V *, SUBI>::vector_type,
553 typename sub_vector_type<V *, SUBI>::vector_type, V *>::return_type
554 sub_vector(V &v,
const SUBI &si) {
555 GMM_ASSERT2(si.last() <= vect_size(v),
556 "sub vector too large, " << si.last() <<
" > " << vect_size(v));
557 return typename select_return<
558 typename sub_vector_type<const V *, SUBI>::vector_type,
559 typename sub_vector_type<V *, SUBI>::vector_type, V *>::return_type
560 (linalg_cast(v), si);
565 #endif // GMM_SUB_VECTOR_H__