32 #ifndef PLANCK_RANGESET_H 33 #define PLANCK_RANGESET_H 47 typedef std::vector<T> rtype;
50 typedef typename rtype::iterator iterator;
51 typedef typename rtype::const_iterator c_iterator;
56 tdiff iiv (
const T &val)
const 57 {
return tdiff(std::upper_bound(r.begin(),r.end(),val)-r.begin())-1; }
59 void addRemove (T a, T b,
tdiff v)
61 tdiff pos1=iiv(a), pos2=iiv(b);
62 if ((pos1>=0) && (r[pos1]==a)) --pos1;
64 bool insert_a = (pos1&1)==v;
65 bool insert_b = (pos2&1)==v;
66 tdiff rmstart=pos1+1+(insert_a ? 1 : 0);
67 tdiff rmend =pos2-(insert_b ? 1 : 0);
71 if (insert_a && insert_b && (pos1+1>pos2))
73 r.insert(r.begin()+pos1+1,2,a);
78 if (insert_a) r[pos1+1]=a;
79 if (insert_b) r[pos2]=b;
80 r.erase(r.begin()+rmstart,r.begin()+rmend+1);
87 const double fct1 = 1.;
88 const double fct2 = 1.;
89 tsize slo = sza<szb ? sza : szb,
90 shi = sza<szb ? szb : sza;
91 double cost1 = fct1 * (sza+szb);
92 double cost2 = fct2 * slo * std::max(1,
ilog2(shi));
93 return (cost1<=cost2) ? 1 : (slo==sza) ? 2 : 3;
97 bool flip_a,
bool flip_b)
99 bool state_a=flip_a, state_b=flip_b, state_res=state_a||state_b;
100 tsize ia=0, ea=a.r.size(), ib=0, eb=b.r.size();
101 bool runa = ia!=ea, runb = ib!=eb;
104 T va = runa ? a.r[ia] : T(0),
105 vb = runb ? b.r[ib] : T(0);
106 bool adv_a = runa && (!runb || (va<=vb)),
107 adv_b = runb && (!runa || (vb<=va));
108 if (adv_a) { state_a=!state_a; ++ia; runa = ia!=ea; }
109 if (adv_b) { state_b=!state_b; ++ib; runb = ib!=eb; }
110 if ((state_a||state_b)!=state_res)
111 { r.push_back(adv_a ? va : vb); state_res = !state_res; }
115 bool flip_a,
bool flip_b)
117 tdiff iva = flip_a ? 0 : -1;
121 tdiff ivb = (iva==-1) ? -1 : b.iiv(a.r[iva]);
122 bool state_b = flip_b^((ivb&1)==0);
123 if ((iva>-1) && (!state_b)) r.push_back(a.r[iva]);
124 while((ivb<bsz-1)&&((iva==asz-1)||(b.r[ivb+1]<a.r[iva+1])))
125 { ++ivb; state_b=!state_b; r.push_back(b.r[ivb]); }
126 if ((iva<asz-1)&&(!state_b)) r.push_back(a.r[iva+1]);
131 bool flip_a,
bool flip_b)
133 planck_assert((
this!=&a)&&(
this!=&b),
"cannot overwrite the rangeset");
136 if (flip_a)
clear();
else *
this=b;
141 if (flip_b)
clear();
else *
this=a;
147 (strat==1) ? generalUnion1(a,b,flip_a,flip_b) :
148 ((strat==2) ? generalUnion2(a,b,flip_a,flip_b)
149 : generalUnion2(b,a,flip_b,flip_a));
154 bool state_a=
false, state_b=
false, state_res=state_a||state_b;
155 tsize ia=0, ea=a.r.size(), ib=0, eb=b.r.size();
156 bool runa = ia!=ea, runb = ib!=eb;
159 T va = runa ? a.r[ia] : T(0),
160 vb = runb ? b.r[ib] : T(0);
161 bool adv_a = runa && (!runb || (va<=vb)),
162 adv_b = runb && (!runa || (vb<=va));
163 if (adv_a) { state_a=!state_a; ++ia; runa = ia!=ea; }
164 if (adv_b) { state_b=!state_b; ++ib; runb = ib!=eb; }
165 if ((state_a^state_b)!=state_res)
166 { r.push_back(adv_a ? va : vb); state_res = !state_res; }
171 bool flip_a,
bool flip_b)
173 bool state_a=flip_a, state_b=flip_b, state_res=state_a||state_b;
174 tsize ia=0, ea=a.r.size(), ib=0, eb=b.r.size();
175 bool runa = ia!=ea, runb = ib!=eb;
178 T va = runa ? a.r[ia] : T(0),
179 vb = runb ? b.r[ib] : T(0);
180 bool adv_a = runa && (!runb || (va<=vb)),
181 adv_b = runb && (!runa || (vb<=va));
182 if (adv_a) { state_a=!state_a; ++ia; runa = ia!=ea; }
183 if (adv_b) { state_b=!state_b; ++ib; runb = ib!=eb; }
184 if ((state_a||state_b)!=state_res)
190 bool flip_a,
bool flip_b)
192 tdiff iva = flip_a ? 0 : -1;
197 {
if ((!flip_b)||(b.r[0]<a.r[0]))
return false; }
199 {
if ((!flip_b)||(b.r[bsz-1]>a.r[asz-1]))
return false; }
202 tdiff ivb=b.iiv(a.r[iva]);
203 if ((ivb!=bsz-1)&&(b.r[ivb+1]<a.r[iva+1]))
return false;
204 if (flip_b==((ivb&1)==0))
return false;
211 bool flip_a,
bool flip_b)
214 return flip_a ?
true : b.r.empty();
216 return flip_b ?
true : a.r.empty();
218 return (strat==1) ? generalAllOrNothing1(a,b,flip_a,flip_b) :
219 ((strat==2) ? generalAllOrNothing2(a,b,flip_a,flip_b)
220 : generalAllOrNothing2(b,a,flip_b,flip_a));
231 bool empty()
const {
return r.empty(); }
233 const rtype &
data()
const {
return r; }
234 void checkConsistency()
const 237 for (
tsize i=1; i<r.size(); ++i)
240 void setData (
const rtype &inp)
258 if ((!r.empty()) && (v1<=r.back()))
261 if (v2>r.back()) r.back()=v2;
264 { r.push_back(v1); r.push_back(v2); }
281 void add(
const T &v1,
const T &v2)
284 if (r.empty() || (v1>=r[r.size()-2]))
append(v1,v2);
292 void remove(
const T &v1,
const T &v2)
295 if (r.empty())
return;
296 if ((v2<=r[0])||(v1>=r.back()))
return;
297 if ((v1<=r[0]) && (v2>=r.back())) { r.clear();
return; }
301 void remove(
const T &v) {
remove(v,v+1); }
306 if (r.empty())
return;
307 if ((b<=r[0]) || (a>=r.back())) { r.clear();
return; }
308 if ((a<=r[0]) && (b>=r.back()))
return;
311 if ((pos2>=0) && (r[pos2]==b)) --pos2;
312 bool insert_b = (pos2&1)==0;
313 r.erase(r.begin()+pos2+1,r.end());
314 if (insert_b) r.push_back(b);
317 bool insert_a = (pos1&1)==0;
318 if (insert_a) r[pos1--]=a;
320 r.erase(r.begin(),r.begin()+pos1+1);
327 for (
tsize i=0; i<r.size(); i+=2)
338 for (
tsize i=0; i<r.size(); i+=2)
339 for (T m(r[i]); m<r[i+1]; ++m)
356 res.generalUnion (*
this,other,
false,
false);
363 res.generalUnion (*
this,other,
true,
true);
370 res.generalUnion (*
this,other,
true,
false);
378 res.generalXor (*
this,other);
387 return (res&1) ? -1 : res>>1;
393 {
return r==other.r; }
400 if (res&1)
return false;
401 return (b<=r[res+1]);
406 {
return !(iiv(v)&1); }
410 {
return generalAllOrNothing(*
this,other,
false,
true); }
416 if ((res&1)==0)
return true;
417 if (res==
tdiff(r.size())-1)
return false;
423 {
return !generalAllOrNothing(*
this,other,
true,
true); }
426 template<
typename T>
inline std::ostream &
operator<< (std::ostream &os,
bool operator==(const rangeset &other) const
bool contains(const rangeset &other) const
bool contains(T a, T b) const
rangeset op_xor(const rangeset &other) const
const rtype & data() const
rangeset op_and(const rangeset &other) const
std::ostream & operator<<(std::ostream &os, const pointing &p)
void toVector(std::vector< T > &res) const
bool overlaps(const rangeset &other) const
void intersect(const T &a, const T &b)
std::vector< T > toVector() const
tdiff findInterval(const T &v) const
bool overlaps(T a, T b) const
void append(const T &v1, const T &v2)
void add(const T &v1, const T &v2)
rangeset op_or(const rangeset &other) const
const T & ivbegin(tdiff i) const
#define planck_assert(testval, msg)
void append(const rangeset &other)
const T & ivend(tdiff i) const
rangeset op_andnot(const rangeset &other) const