27 #include "juice/variant.hpp"
28 #include "globals.hpp"
31 #include "timezone/interval.hpp"
34 using namespace Juice;
39 template<
typename I,
typename T,
typename U>
40 void subassign_generic(
const I& idx,
44 template<
typename I,
typename T,
typename U>
45 inline void subassignScalar_generic(
const I& idx, Vector<T>& rv, U u);
57 inline bool getfirst(idx_type&
val, idx_type& i)
const {
62 inline bool getnext(idx_type&
val, idx_type& i)
const {
71 inline size_t trueSize()
const {
return size(); }
72 inline size_t size()
const {
return sz; }
83 template<
typename T,
typename U>
87 for (idx_type j=0; j<rv.size(); ++j) {
88 setv_checkbefore(rv, j, convert<T,typename U::value_type>(u[vj++]));
95 template<
typename T,
typename U>
97 for (idx_type j=0; j<rv.size(); ++j) {
98 setv(rv, j, convert<T,U>(u));
102 inline void selectNames(
Dname& tonames,
const Dname& fromnames)
const {
108 template <
typename I>
111 if (vi.isOrdered()) {
113 std::iota(uidx.begin(), uidx.end(), 0);
116 const auto& idx = vi.template sort_idx<size_t>();
117 for (idx_type j=0; j<idx.size(); ++j) {
118 setv(vidx, idx[j], vi[j]);
119 setv(uidx, idx[j], j);
128 bool getfirst(idx_type& iv, idx_type& ii)
const {
139 bool getnext(idx_type& iv, idx_type& ii)
const {
141 if (++ii < vi.size()) {
154 inline size_t trueSize()
const {
157 inline size_t size()
const {
164 for (idx_type j=0; j<vi.size(); ++j) {
165 if (vi[j] < 0)
throw range_error(
"mixed positive and negative subscripts");
166 if (vi[j] >= v.size())
throw range_error(
"subscript out of bounds");
167 rv.push_back(v[vi[j]]);
177 for (; ii<vi.size(); ++ii) {
178 if (vi[ii] < 0)
throw range_error(
"mixed positive and negative subscripts");
180 if (vi[ii] < pos || vi[ii] >= v.size() + pos)
return;
181 rv.push_back(v[vi[ii]-pos]);
189 template<
typename T,
typename U>
192 idx_type& vj)
const {
193 for (idx_type j=0; j<vi.size(); ++j) {
194 j+1 < vi.size() && vidx[j+1] - vidx[j] == 1 ?
195 setv_checkbefore(rv, vidx[j], convert<T,typename U::value_type>(u[vj+uidx[j]])) :
196 setv(rv, vidx[j], convert<T,typename U::value_type>(u[vj+uidx[j]]));
201 template<
typename T,
typename U>
202 inline void subassignScalar(
Vector<T>& rv, U u)
const {
203 for (idx_type j=0; j<vi.size(); ++j) {
204 j+1 < vi.size() && vidx[j+1] - vidx[j] == 1 ?
205 setv_checkbefore(rv, vidx[j], convert<T,U>(u)) :
206 setv(rv, vidx[j],
convert<T,U>(u));
210 inline void selectNames(Dname& tonames,
const Dname& fromnames)
const {
211 if (fromnames.names.size() > 0) {
212 for (idx_type j=0; j<vi.size(); ++j) {
213 if (fromnames.sz <= vi[j]) {
214 throw range_error(
"subscript out of bounds");
216 tonames.addafter(fromnames.names[vi[j]]);
220 tonames = Dname(vi.size());
226 template <
typename I>
230 if (vi.size() && !vi.isOrdered()) {
232 vi.erase(unique(vi.begin(), vi.end()), vi.end());
234 if (vi.size() && (vi.back() >= sz || !sz))
throw range_error(
"subscript out of bounds");
240 bool getfirst(idx_type& iv, idx_type& ii)
const {
242 while (ii < vi.size() && vi[ii] == iv) {
255 bool getnext(idx_type& iv, idx_type& ii)
const {
257 while (ii < vi.size() && vi[ii] == iv) {
270 inline size_t trueSize()
const {
273 return sz - vi.size();
275 inline size_t size()
const {
283 for (idx_type j=0; j<sz; ++j) {
284 if (ii < vi.size() && vi[ii] == j)
297 idx_type& ii, idx_type& iv, idx_type pos)
const {
298 for (; iv<sz; ++iv) {
299 if (iv >= pos + v.size())
return;
300 if (ii < vi.size() && vi[ii] == iv)
303 rv.push_back(v[iv - pos]);
307 template<
typename T,
typename U>
310 idx_type& vj)
const {
311 subassign_generic(*
this, rv, u, vj);
314 template<
typename T,
typename U>
315 inline void subassignScalar(Vector<T>& rv, U u)
const {
316 subassignScalar_generic(*
this, rv, u);
319 inline void selectNames(Dname& tonames,
const Dname& fromnames)
const {
321 if (fromnames.names.size() > 0) {
322 for (idx_type j=0; j<sz; ++j) {
323 if (fromnames.sz <= j) {
324 throw range_error(
"subscript out of bounds");
326 if (!(ii < vi.size() && vi[ii] == j)) {
327 tonames.addafter(fromnames.names[j]);
334 tonames = Dname(size());
341 const vector<string> vs;
344 bool getfirst(idx_type&
val, idx_type& i)
const {
354 bool getnext(idx_type&
val, idx_type& i)
const {
355 if (++i < vs.size()) {
364 inline size_t trueSize()
const {
return vs.size(); }
365 inline size_t size()
const {
return vs.size(); }
369 for (idx_type j=0; j<vs.size(); ++j) {
370 rv.push_back(v[names[vs[j]]]);
374 template<
typename T,
typename U>
377 idx_type& vj)
const {
378 subassign_generic(*
this, rv, u, vj);
381 template<
typename T,
typename U>
382 inline void subassignScalar(
Vector<T>& rv, U u)
const {
383 subassignScalar_generic(*
this, rv, u);
386 inline void selectNames(
Dname& tonames,
const Dname& fromnames)
const {
387 for (idx_type j=0; j<vs.size(); ++j) {
389 tonames.addafter(vs[j]);
397 bool getfirst(idx_type&
val, idx_type& i)
const {
398 for (i=0; i<vb.size(); ++i) {
407 bool getnext(idx_type&
val, idx_type& i)
const {
408 for (; ++i<vb.size(); ++i) {
418 inline size_t trueSize()
const {
return size(); }
419 inline size_t size()
const {
return std::count(vb.cbegin(), vb.cend(),
true); }
423 for (idx_type j=0; j<vb.size(); ++j) {
436 for (idx_type j=0; j<v.size(); ++j) {
437 if (ii >= vb.size()) {
438 throw range_error(
"subscript out of bounds");
446 template<
typename T,
typename U>
449 idx_type& vj)
const {
450 subassign_generic(*
this, rv, u, vj);
453 template<
typename T,
typename U>
454 inline void subassignScalar(Vector<T>& rv, U u)
const {
455 subassignScalar_generic(*
this, rv, u);
458 inline void selectNames(Dname& tonames,
const Dname& fromnames)
const {
459 if (fromnames.names.size() > 0) {
460 for (idx_type j=0; j<vb.size(); ++j) {
462 if (fromnames.sz <= j) {
463 throw range_error(
"subscript out of bounds");
465 tonames.addafter(fromnames.names[j]);
470 idx_type nb = size();
482 inline bool getfirst(idx_type&
val, idx_type& i)
const {
483 while (i < idx.size()) {
484 auto p = bsearch(&idx[i], vd.c_ptr(), vd.size(),
sizeof(Global::dtime), comp);
486 val =
static_cast<Global::dtime*
>(p) - vd.c_ptr();
496 inline bool getnext(idx_type&
val, idx_type& i)
const {
497 return getfirst(
val, ++i);
500 inline size_t trueSize()
const {
501 idx_type ii = 0, iv = 0;
503 while (iv < vd.size() && ii < idx.size()) {
504 if (vd[iv] < idx[ii]) {
506 }
else if (vd[iv] > idx[ii]) {
517 inline size_t size()
const {
525 if (vd.size() != v.size()) {
526 throw range_error(
"size mismatch between vector to subset and time index");
530 if (getfirst(
val, i)) {
531 rv.push_back(v[
val]);
532 while (getnext(
val, i)) {
533 rv.push_back(v[
val]);
538 template<
typename T,
typename U>
541 idx_type& vj)
const {
542 subassign_generic(*
this, rv, u, vj);
545 template<
typename T,
typename U>
546 inline void subassignScalar(
Vector<T>& rv, U u)
const {
547 subassignScalar_generic(*
this, rv, u);
550 inline void selectNames(
Dname& tonames,
const Dname& fromnames)
const {
551 if (fromnames.names.size() > 0) {
552 idx_type ii = 0, iv = 0;
553 while (iv < vd.size() && ii < idx.size()) {
554 if (vd[iv] < idx[ii]) {
556 }
else if (vd[iv] > idx[ii]) {
559 tonames.addafter(fromnames.names[iv++]);
564 idx_type nb = size();
571 static inline int comp(
const void* a,
const void* b)
573 if (*
static_cast<const Global::dtime*
>(a) < *
static_cast<const Global::dtime*
>(b))
return -1;
574 if (*
static_cast<const Global::dtime*
>(a) > *
static_cast<const Global::dtime*
>(b))
return 1;
592 inline bool getfirst(idx_type&
val, idx_type& i)
const {
593 while (i < idx.size()) {
594 const auto iter = idx[i].sopen ?
595 std::upper_bound(vi.begin(), vi.end(), idx[i].s) :
596 std::lower_bound(vi.begin(), vi.end(), idx[i].s);
597 val = iter - vi.begin();
598 if (iter == vi.end() || (!idx[i].eopen ? vi[
val] > idx[i].e : vi[
val] >= idx[i].e)) {
608 inline bool getnext(idx_type&
val, idx_type& i)
const {
610 while (
val < vi.size() && i < idx.size()) {
611 if (!idx[i].sopen ? vi[
val] < idx[i].s : vi[
val] <= idx[i].s) {
613 }
else if (!idx[i].eopen ? vi[
val] > idx[i].e : vi[
val] >= idx[i].e) {
614 if (!getfirst(
val, ++i)) {
627 inline size_t trueSize()
const {
630 while (iv < vi.size() && ii < idx.size()) {
631 if (!idx[ii].sopen ? vi[iv] < idx[ii].s : vi[iv] <= idx[ii].s) {
633 }
else if (!idx[ii].eopen ? vi[iv] > idx[ii].e : vi[iv] >= idx[ii].e) {
643 inline size_t size()
const {
652 if (vi.size() != v.size()) {
653 std::cout <<
"vi.size(): " << vi.size() <<
" v.size(): " << v.size() << std::endl;
654 throw range_error(
"size mismatch between vector to subset and time index");
658 if (getfirst(
val, i)) {
659 rv.push_back(v[
val]);
660 while (getnext(
val, i)) {
661 rv.push_back(v[
val]);
666 template<
typename T,
typename U>
669 idx_type& vj)
const {
670 subassign_generic(*
this, rv, u, vj);
673 template<
typename T,
typename U>
674 inline void subassignScalar(
Vector<T>& rv, U u)
const {
675 subassignScalar_generic(*
this, rv, u);
678 inline void selectNames(
Dname& tonames,
const Dname& fromnames)
const {
679 if (fromnames.names.size() > 0) {
680 idx_type ii = 0, iv = 0;
681 while (iv < vi.size() && ii < idx.size()) {
682 if (!idx[ii].sopen ? vi[iv] < idx[ii].s : vi[iv] <= idx[ii].s) {
684 }
else if (!idx[ii].eopen ? vi[iv] > idx[ii].e : vi[iv] >= idx[ii].e) {
687 tonames.addafter(fromnames.names[iv++]);
691 idx_type nb = size();
741 inline size_t trueSize()
const {
742 switch (idx.which()) {
744 return get<NullIndex>(idx).trueSize();
746 return get<IntIndex>(idx).trueSize();
748 return get<IntIndexNeg>(idx).trueSize();
750 return get<NameIndex>(idx).trueSize();
752 return get<BoolIndex>(idx).trueSize();
754 return get<DtimeIndex>(idx).trueSize();
756 return get<IntervalIndex>(idx).trueSize();
758 throw std::range_error(
"Index::trueSize: unknown index type");
762 inline size_t size()
const {
763 switch (idx.which()) {
765 return get<NullIndex>(idx).size();
767 return get<IntIndex>(idx).size();
769 return get<IntIndexNeg>(idx).size();
771 return get<NameIndex>(idx).size();
773 return get<BoolIndex>(idx).size();
775 return get<DtimeIndex>(idx).size();
777 return get<IntervalIndex>(idx).size();
779 throw std::range_error(
"Index::size: unknown index type");
783 inline bool getfirst(idx_type&
val, idx_type& ii)
const {
784 switch (idx.which()) {
786 return get<NullIndex>(idx).getfirst(
val, ii);
788 return get<IntIndex>(idx).getfirst(
val, ii);
790 return get<IntIndexNeg>(idx).getfirst(
val, ii);
792 return get<NameIndex>(idx).getfirst(
val, ii);
794 return get<BoolIndex>(idx).getfirst(
val, ii);
796 return get<DtimeIndex>(idx).getfirst(
val, ii);
798 return get<IntervalIndex>(idx).getfirst(
val, ii);
800 throw range_error(
"Index::getfirst: unknown index type");
804 inline bool getnext(idx_type&
val, idx_type& ii)
const {
805 switch (idx.which()) {
807 return get<NullIndex>(idx).getnext(
val, ii);
809 return get<IntIndex>(idx).getnext(
val, ii);
811 return get<IntIndexNeg>(idx).getnext(
val, ii);
813 return get<NameIndex>(idx).getnext(
val, ii);
815 return get<BoolIndex>(idx).getnext(
val, ii);
817 return get<DtimeIndex>(idx).getnext(
val, ii);
819 return get<IntervalIndex>(idx).getnext(
val, ii);
821 throw range_error(
"Index::getnext: unknown index type");
827 switch (idx.which()) {
829 get<NullIndex>(idx).subset(rv, v);
832 get<IntIndex>(idx).subset(rv, v);
835 get<IntIndexNeg>(idx).subset(rv, v);
838 get<NameIndex>(idx).subset(rv, v);
841 get<BoolIndex>(idx).subset(rv, v);
844 get<DtimeIndex>(idx).subset(rv, v);
847 get<IntervalIndex>(idx).subset(rv, v);
850 throw range_error(
"Index::subset: unknown index type");
856 idx_type& ii, idx_type& iv, idx_type pos)
const {
857 switch (idx.which()) {
859 get<NullIndex>(idx).subset(rv, v);
862 get<IntIndex>(idx).subset(rv, v, ii, pos);
865 get<IntIndexNeg>(idx).subset(rv, v, ii, iv, pos);
868 get<BoolIndex>(idx).subset(rv, v, ii);
873 throw range_error(
"can't do a vector subset with this index type");
875 throw range_error(
"Index::subset: unknown index type");
879 template<
typename T,
typename U>
882 idx_type& vj)
const {
883 switch (idx.which()) {
885 get<NullIndex>(idx).subassign(rv, v, vj);
888 get<IntIndex>(idx).subassign(rv, v, vj);
891 get<IntIndexNeg>(idx).subassign(rv, v, vj);
894 get<NameIndex>(idx).subassign(rv, v, vj);
897 get<BoolIndex>(idx).subassign(rv, v, vj);
900 get<DtimeIndex>(idx).subassign(rv, v, vj);
903 get<IntervalIndex>(idx).subassign(rv, v, vj);
906 throw range_error(
"Index::subassign: unknown index type");
910 template<
typename T,
typename U>
911 void subassignScalar(
Vector<T>& rv, U u)
const {
912 switch (idx.which()) {
914 get<NullIndex>(idx).subassignScalar(rv, u);
917 get<IntIndex>(idx).subassignScalar(rv, u);
920 get<IntIndexNeg>(idx).subassignScalar(rv, u);
923 get<NameIndex>(idx).subassignScalar(rv, u);
926 get<BoolIndex>(idx).subassignScalar(rv, u);
929 get<DtimeIndex>(idx).subassignScalar(rv, u);
932 get<IntervalIndex>(idx).subassignScalar(rv, u);
935 throw range_error(
"Index::subassignScalar: unknown index type");
939 inline void selectNames(
Dname& tonames,
const Dname& fromnames)
const {
940 switch (idx.which()) {
942 get<NullIndex>(idx).selectNames(tonames, fromnames);
945 get<IntIndex>(idx).selectNames(tonames, fromnames);
948 get<IntIndexNeg>(idx).selectNames(tonames, fromnames);
951 get<NameIndex>(idx).selectNames(tonames, fromnames);
954 get<BoolIndex>(idx).selectNames(tonames, fromnames);
957 get<DtimeIndex>(idx).selectNames(tonames, fromnames);
960 get<IntervalIndex>(idx).selectNames(tonames, fromnames);
963 throw range_error(
"Index::selectNames: unknown index type");
967 static inline bool getfirstcol(idx_type& col,
968 vector<idx_type>&
val,
969 vector<idx_type>& pi,
970 const vector<Index>& i,
974 for (idx_type pd = 1; pd<dim.size(); ++pd) {
975 if (!i[pd].getfirst(
val[pd], pi[pd])) {
983 for (idx_type k=1; k<dim.size(); ++k) {
984 if (
val[k] >= dim[k]) {
985 throw range_error(
"subscript out of bounds");
994 static inline bool getnextcol(idx_type& col,
995 vector<idx_type>&
val,
996 vector<idx_type>& pi,
997 const vector<Index>& i,
1002 while (pd < dim.size() && !i[pd].getnext(
val[pd], pi[pd])) {
1005 if (pd >= dim.size()) {
1012 for (idx_type k=1; k<dim.size(); ++k) {
1013 if (
val[k] >= dim[k]) {
1014 throw range_error(
"subscript out of bounds");
1025 template<
typename I,
typename T,
typename U>
1026 void subassign_generic(
const I& idx,
1031 idx_type ii = 0, j = 0, nextj = 0;
1032 std::cout <<
"subassign_generic entry j: " << j <<
", vj: " << vj << std::endl;
1033 std::cout <<
"u.size(): " << u.size() << std::endl;
1034 if (!idx.getfirst(j, ii))
return;
1038 while (idx.getnext(nextj, ii)) {
1039 std::cout <<
"we found next and nextj: " << nextj <<
", j: " << j <<
", ii: " << ii << std::endl;
1040 if (nextj - j <= 1) setv_checkbefore(rv, j, convert<T,typename U::value_type>(u[vj++]));
1041 else if (nextj - j > 1) setv(rv, j, convert<T,typename U::value_type>(u[vj++]));
1046 std::cout <<
"last j: " << j <<
", vj: " << vj << std::endl;
1047 setv(rv, j, convert<T,typename U::value_type>(u[vj++]));
1048 std::cout <<
"vj is: " << vj << std::endl;
1052 template<
typename I,
typename T,
typename U>
1053 inline void subassignScalar_generic(
const I& idx, Vector<T>& rv, U u) {
1055 idx_type ii = 0, j = 0, nextj = 0;
1056 if (!idx.getfirst(j, ii))
return;
1060 while (idx.getnext(nextj, ii)) {
1061 if (nextj - j <= 1) setv_checkbefore(rv, j, convert<T,U>(u));
1062 else if (nextj - j > 1) setv(rv, j, convert<T,U>(u));
1067 setv(rv, j, convert<T,U>(u));