32 #ifndef PLANCK_CRANGESET_H 33 #define PLANCK_CRANGESET_H 79 typedef std::vector<T> rtype;
86 bool operator()(
const T &a,
const T &b)
101 bool singleVal()
const 103 if (idx==ref.r.size()-1)
104 return (ref.r[idx]>=0);
106 return ((ref.r[idx]^ref.r[idx+1])>=0);
110 RsIter(
const crangeset &ref_) : idx(0), extra(
false), ref(ref_), val(0)
116 bool atEnd()
const {
return idx>=ref.r.size(); }
121 RsIter &operator++ ()
128 if (!atEnd()) val=abs(ref.r[idx]);
140 if (!atEnd()) val=abs(ref.r[idx]);
145 void advance_up_to(T goal)
157 {
return (ref.r[idx]>=0)^extra; }
167 IvIter(
const crangeset &ref_) : rsi(ref_)
169 if (rsi.atEnd())
return;
174 bool atEnd()
const {
return rsi.atEnd(); }
175 T ivbegin()
const {
return b; }
176 T ivend()
const {
return e; }
177 tdiff ivlen()
const {
return e-b; }
178 IvIter &operator++ ()
181 if (rsi.atEnd())
return *
this;
196 RsOutputIter(
crangeset &ref_) : val(T(0)), val_set(
false), ref(ref_)
198 RsOutputIter &operator*() {
return *
this; }
199 RsOutputIter &operator++ () {
return *
this; }
200 RsOutputIter &operator++ (
int) {
return *
this; }
201 RsOutputIter &operator= (
const T &v)
216 return tdiff(std::upper_bound(r.begin(),r.end(),val,abscomp())
223 const double fct1 = 1.;
224 const double fct2 = 1.;
225 tsize slo = sza<szb ? sza : szb,
226 shi = sza<szb ? szb : sza;
227 double cost1 = fct1 * (sza+szb);
228 double cost2 = fct2 * slo * std::max(1,
ilog2(shi));
229 return (cost1<=cost2) ? 1 : (slo==sza) ? 2 : 3;
233 bool flip_a,
bool flip_b)
236 bool state_a=flip_a, state_b=flip_b, state_res=state_a||state_b;
238 RsOutputIter io(res);
239 bool runa = !ia.atEnd(), runb = !ib.atEnd();
242 T va = runa ? *ia : T(0),
243 vb = runb ? *ib : T(0);
244 bool adv_a = runa && (!runb || (va<=vb)),
245 adv_b = runb && (!runa || (vb<=va));
246 if (adv_a) { state_a=!state_a; ++ia; runa = !ia.atEnd(); }
247 if (adv_b) { state_b=!state_b; ++ib; runb = !ib.atEnd(); }
248 if ((state_a||state_b)!=state_res)
249 { *io++ = (adv_a ? va : vb); state_res = !state_res; }
254 bool flip_a,
bool flip_b)
257 bool state_a=flip_a, state_b=flip_b, state_res=state_a||state_b;
259 RsOutputIter io(res);
260 bool runa = !ia.atEnd(), runb = !ib.atEnd();
263 T va = runa ? *ia : T(0);
269 ib.advance_up_to (va);
270 state_b=!(flip_b^ib.onoff());
273 T vb = runb ? *ib : T(0);
274 bool adv_a = runa && (!runb || (va<=vb)),
275 adv_b = runb && (!runa || (vb<=va));
276 if (adv_a) { state_a=!state_a; ++ia; runa = !ia.atEnd(); }
277 if (adv_b) { state_b=!state_b; ++ib; runb = !ib.atEnd(); }
278 if ((state_a||state_b)!=state_res)
279 { *io++ = (adv_a ? va : vb); state_res = !state_res; }
284 bool flip_a,
bool flip_b)
291 int strat =
strategy (a.r.size(), b.r.size());
292 return (strat==1) ? generalUnion1(a,b,flip_a,flip_b) :
293 ((strat==2) ? generalUnion2(a,b,flip_a,flip_b)
294 : generalUnion2(b,a,flip_b,flip_a));
299 bool state_a=
false, state_b=
false, state_res=state_a||state_b;
301 RsOutputIter io(res);
302 bool runa = !ia.atEnd(), runb = !ib.atEnd();
305 T va = runa ? *ia : T(0),
306 vb = runb ? *ib : T(0);
307 bool adv_a = runa && (!runb || (va<=vb)),
308 adv_b = runb && (!runa || (vb<=va));
309 if (adv_a) { state_a=!state_a; ++ia; runa = !ia.atEnd(); }
310 if (adv_b) { state_b=!state_b; ++ib; runb = !ib.atEnd(); }
311 if ((state_a^state_b)!=state_res)
312 { *io++ = (adv_a ? va : vb); state_res = !state_res; }
318 bool flip_a,
bool flip_b)
320 bool state_a=flip_a, state_b=flip_b, state_res=state_a||state_b;
322 bool runa = !ia.atEnd(), runb = !ib.atEnd();
326 T va = runa ? *ia : T(0),
327 vb = runb ? *ib : T(0);
328 bool adv_a = runa && (!runb || (va<=vb)),
329 adv_b = runb && (!runa || (vb<=va));
330 if (adv_a) { state_a=!state_a; ++ia; runa = !ia.atEnd(); }
331 if (adv_b) { state_b=!state_b; ++ib; runb = !ib.atEnd(); }
332 if ((state_a||state_b)!=state_res)
338 bool flip_a,
bool flip_b)
340 bool state_a=flip_a, state_b=flip_b, state_res=state_a||state_b;
342 bool runa = !ia.atEnd(), runb = !ib.atEnd();
345 T va = runa ? *ia : T(0);
351 ib.advance_up_to (va);
352 state_b=!(flip_b^ib.onoff());
355 T vb = runb ? *ib : T(0);
356 bool adv_a = runa && (!runb || (va<=vb)),
357 adv_b = runb && (!runa || (vb<=va));
358 if (adv_a) { state_a=!state_a; ++ia; runa = !ia.atEnd(); }
359 if (adv_b) { state_b=!state_b; ++ib; runb = !ib.atEnd(); }
360 if ((state_a||state_b)!=state_res)
367 bool flip_a,
bool flip_b)
370 return flip_a ?
true : b.r.empty();
372 return flip_b ?
true : a.r.empty();
373 int strat =
strategy (a.r.size(), b.r.size());
374 return (strat==1) ? generalAllOrNothing1(a,b,flip_a,flip_b) :
375 ((strat==2) ? generalAllOrNothing2(a,b,flip_a,flip_b)
376 : generalAllOrNothing2(b,a,flip_b,flip_a));
381 bool empty()
const {
return r.empty(); }
382 const rtype &data()
const {
return r; }
383 void checkConsistency()
const 386 if (r.size()==0)
return;
387 planck_assert(r[0]>=0,
"incorrect first element in range set");
388 for (
tsize i=1; i<r.size(); ++i)
389 planck_assert(abs(r[i])>abs(r[i-1]+1),
"inconsistent entries");
391 void setData (
const rtype &inp)
404 le=(r.back()<0) ? -r.back() : r.back()+1;
408 if ((v1>le+1)||(r.back()>0))
411 if (v2-v1>1) r.push_back(-v2);
420 if (r.back()>=0) lastbegin= r.back();
421 else if (r[r.size()-2]<0) lastbegin = -r[r.size()-2]+1;
422 else lastbegin = r[r.size()-2];
426 T endval=max(abs(r.back()),v2);
430 if (endval>r.back()+1) r.push_back(-endval);
442 while (!iter.atEnd())
444 append(iter.ivbegin(),iter.ivend());
453 while (!iter.atEnd())
454 {res+=iter.ivlen(); ++iter;}
459 {
return generalUnion (*
this,other,
false,
false); }
461 {
return generalUnion (*
this,other,
true,
true); }
463 {
return generalUnion (*
this,other,
true,
false); }
465 {
return generalXor (*
this,other); }
470 {
return r==other.r; }
477 if (res<0)
return false;
480 if (res==
tdiff(r.size())-1)
return false;
481 if (r[res+1]>=0)
return false;
482 return ((a>-r[res])&&(b<=-r[res+1]));
485 if ((res==
tdiff(r.size())-1) || (r[res+1]>=0))
486 return ((a==r[res])&&(b==a+1));
494 if (res<0)
return false;
497 if (res==
tdiff(r.size())-1)
return false;
498 if (r[res+1]>=0)
return false;
501 if ((res<
tdiff(r.size())-1) && (r[res+1]<0))
508 {
return generalAllOrNothing(*
this,other,
false,
true); }
515 if (empty())
return false;
517 if (res<
tdiff(r.size())-1)
518 if (b>abs(r[res+1]))
return true;
521 if (res==-1)
return false;
522 if (res==
tdiff(r.size())-1)
526 return (a==r[res]) || (r[res+1]<0);
527 return (r[res+1]<0) && (b>-r[res]+1);
533 {
return !generalAllOrNothing(*
this,other,
true,
true); }
536 template<
typename T>
inline std::ostream &operator<< (std::ostream &os,
541 while (!iter.atEnd())
543 os <<
"["<<iter.ivbegin()<<
";"<<iter.ivend()<<
"[ ";
void append(const T &v1, const T &v2)
bool overlaps(T a, T b) const
bool contains(const crangeset &other) const
static int strategy(tsize sza, tsize szb)
bool overlaps(const crangeset &other) const
tdiff iiv(const T &val) const
bool operator==(const crangeset &other) const
bool contains(T a, T b) const
#define planck_assert(testval, msg)
void append(const crangeset &other)