const.hpp /usr/include/gecode/set/view.hpp Gecode::Set::ArrayRanges Gecode::Set::LubRanges< EmptyView > Gecode::Set::GlbRanges< EmptyView > Gecode::Set::LubRanges< UniverseView > Gecode::Set::GlbRanges< UniverseView > Gecode::Set::LubRanges< ConstSetView > Gecode::Set::GlbRanges< ConstSetView > Gecode Gecode::Set /*-*-mode:C++;c-basic-offset:2;indent-tabs-mode:nil-*-*/ /* *Mainauthors: *GuidoTack<tack@gecode.org> * *Copyright: *GuidoTack,2004 * *Lastmodified: *$Date:2010-07-2817:35:33+0200(Wed,28Jul2010)$by$Author:schulte$ *$Revision:11294$ * *ThisfileispartofGecode,thegenericconstraint *developmentenvironment: *http://www.gecode.org * *Permissionisherebygranted,freeofcharge,toanypersonobtaining *acopyofthissoftwareandassociateddocumentationfiles(the *"Software"),todealintheSoftwarewithoutrestriction,including *withoutlimitationtherightstouse,copy,modify,merge,publish, *distribute,sublicense,and/orsellcopiesoftheSoftware,andto *permitpersonstowhomtheSoftwareisfurnishedtodoso,subjectto *thefollowingconditions: * *Theabovecopyrightnoticeandthispermissionnoticeshallbe *includedinallcopiesorsubstantialportionsoftheSoftware. * *THESOFTWAREISPROVIDED"ASIS",WITHOUTWARRANTYOFANYKIND, *EXPRESSORIMPLIED,INCLUDINGBUTNOTLIMITEDTOTHEWARRANTIESOF *MERCHANTABILITY,FITNESSFORAPARTICULARPURPOSEAND *NONINFRINGEMENT.INNOEVENTSHALLTHEAUTHORSORCOPYRIGHTHOLDERSBE *LIABLEFORANYCLAIM,DAMAGESOROTHERLIABILITY,WHETHERINANACTION *OFCONTRACT,TORTOROTHERWISE,ARISINGFROM,OUTOFORINCONNECTION *WITHTHESOFTWAREORTHEUSEOROTHERDEALINGSINTHESOFTWARE. * */ namespaceGecode{namespaceSet{ classArrayRanges{ private: int*_ranges; int_size; int_pos; public: ArrayRanges(void):_ranges(NULL),_size(0),_pos(0){} ArrayRanges(int*ranges,intsize) :_ranges(ranges),_size(size),_pos(0){} voidinit(int*ranges,intsize){ _ranges=ranges;_size=size;_pos=0; } booloperator ()(void)const{return_pos<_size;} voidoperator ++(void){_pos++;} intmin(void)const{return_ranges[_pos*2];} intmax(void)const{return_ranges[_pos*2+1];} unsignedintwidth(void)const{ returnstatic_cast<unsignedint>(_ranges[_pos*2+1]-_ranges[_pos*2]+1); } }; forceinline ConstSetView::ConstSetView(void):ranges(NULL),size(0),domSize(0){} forceinline ConstSetView::ConstSetView(Space&home,constIntSet&dom){ size=dom.ranges(); domSize=0; if(size>0){ ranges=home.alloc<int>(2*size); IntSetRangesdr(dom); for(inti=0;dr();++dr,i+=2){ intmin=dr.min();intmax=dr.max(); ranges[i]=min; ranges[i+1]=max; domSize+=static_cast<unsignedint>(max-min+1); } }else{ ranges=NULL; } } forceinlineunsignedint ConstSetView::glbSize(void)const{returndomSize;} forceinlineunsignedint ConstSetView::lubSize(void)const{returndomSize;} forceinlineunsignedint ConstSetView::unknownSize(void)const{return0;} forceinlinebool ConstSetView::contains(inti)const{ for(intj=size;j--;){ if(ranges[2*j+1]<i) returnfalse; if(ranges[2*j]>=i) returntrue; } returnfalse; } forceinlinebool ConstSetView::notContains(inti)const{ return!contains(i); } forceinlineunsignedint ConstSetView::cardMin(void)const{returndomSize;} forceinlineunsignedint ConstSetView::cardMax(void)const{returndomSize;} forceinlineint ConstSetView::lubMin(void)const{ returnsize==0?BndSet::MIN_OF_EMPTY:ranges[0]; } forceinlineint ConstSetView::lubMax(void)const{ returnsize==0?BndSet::MAX_OF_EMPTY:ranges[size*2-1]; } forceinlineint ConstSetView::glbMin(void)const{returnlubMin();} forceinlineint ConstSetView::glbMax(void)const{returnlubMax();} forceinlineModEvent ConstSetView::cardMin(Space&,unsignedintc){ returnc<=domSize?ME_SET_NONE:ME_SET_FAILED; } forceinlineModEvent ConstSetView::cardMax(Space&,unsignedintc){ returnc>=domSize?ME_SET_NONE:ME_SET_FAILED; } forceinlineModEvent ConstSetView::include(Space&,intc){ returncontains(c)?ME_SET_NONE:ME_SET_FAILED; } forceinlineModEvent ConstSetView::exclude(Space&,intc){ returncontains(c)?ME_SET_FAILED:ME_SET_NONE; } forceinlineModEvent ConstSetView::intersect(Space&,intc){ return(size==0|| (size==1&& ranges[0]==ranges[1]&&ranges[0]==c))? ME_SET_NONE:ME_SET_FAILED; } forceinlineModEvent ConstSetView::intersect(Space&,inti,intj){ return(glbMin()>=i&&glbMax()<=j)? ME_SET_NONE:ME_SET_FAILED; } forceinlineModEvent ConstSetView::include(Space&,inti,intj){ Iter::Ranges::Singletonsingle(i,j); ArrayRangesar(ranges,size); return(single()&&Iter::Ranges::subset(single,ar))? ME_SET_NONE:ME_SET_FAILED; } forceinlineModEvent ConstSetView::exclude(Space&,inti,intj){ Iter::Ranges::Singletonsingle(i,j); ArrayRangesar(ranges,size); return(single()&&Iter::Ranges::subset(single,ar))? ME_SET_FAILED:ME_SET_NONE; } template<classI>ModEvent ConstSetView::excludeI(Space&,I&i){ ArrayRangesar(ranges,size); return(i()&&Iter::Ranges::subset(i,ar))?ME_SET_FAILED:ME_SET_NONE; } template<classI>ModEvent ConstSetView::includeI(Space&,I&i){ ArrayRangesar(ranges,size); returnIter::Ranges::subset(i,ar)?ME_SET_NONE:ME_SET_FAILED; } template<classI>ModEvent ConstSetView::intersectI(Space&,I&i){ ArrayRangesar(ranges,size); returnIter::Ranges::subset(ar,i)?ME_SET_NONE:ME_SET_FAILED; } forceinlinevoid ConstSetView::update(Space&home,boolshare,ConstSetView&p){ ConstView<SetView>::update(home,share,p); //disposeoldranges if(size>0) home.free<int>(ranges,2); domSize=p.domSize; size=p.size; if(size==0){ ranges=NULL; }else{ //copyrangesfromp ranges=home.alloc<int>(2*size); for(inti=size;i--;){ ranges[2*i]=p.ranges[2*i]; ranges[2*i+1]=p.ranges[2*i+1]; } } } /* *Deltainformationforadvisors * */ forceinlineint ConstSetView::glbMin(constDelta&)const{ GECODE_NEVER; return0; } forceinlineint ConstSetView::glbMax(constDelta&)const{ GECODE_NEVER; return0; } forceinlinebool ConstSetView::glbAny(constDelta&)const{ GECODE_NEVER; returnfalse; } forceinlineint ConstSetView::lubMin(constDelta&)const{ GECODE_NEVER; return0; } forceinlineint ConstSetView::lubMax(constDelta&)const{ GECODE_NEVER; return0; } forceinlinebool ConstSetView::lubAny(constDelta&)const{ GECODE_NEVER; returnfalse; } forceinline EmptyView::EmptyView(void){} forceinlineunsignedint EmptyView::glbSize(void)const{return0;} forceinlineunsignedint EmptyView::lubSize(void)const{return0;} forceinlineunsignedint EmptyView::unknownSize(void)const{return0;} forceinlinebool EmptyView::contains(int)const{returnfalse;} forceinlinebool EmptyView::notContains(int)const{returntrue;} forceinlineunsignedint EmptyView::cardMin(void)const{return0;} forceinlineunsignedint EmptyView::cardMax(void)const{return0;} forceinlineint EmptyView::lubMin(void)const{return0;} forceinlineint EmptyView::lubMax(void)const{return0;} forceinlineint EmptyView::glbMin(void)const{return0;} forceinlineint EmptyView::glbMax(void)const{return0;} forceinlineModEvent EmptyView::cardMin(Space&,unsignedintc){ returnc==0?ME_SET_NONE:ME_SET_FAILED; } forceinlineModEvent EmptyView::cardMax(Space&,unsignedint){ returnME_SET_NONE; } forceinlineModEvent EmptyView::include(Space&,int){ returnME_SET_FAILED; } forceinlineModEvent EmptyView::exclude(Space&,int){returnME_SET_NONE;} forceinlineModEvent EmptyView::intersect(Space&,int){returnME_SET_NONE;} forceinlineModEvent EmptyView::intersect(Space&,int,int){returnME_SET_NONE;} forceinlineModEvent EmptyView::include(Space&,int,int){ returnME_SET_FAILED;} forceinlineModEvent EmptyView::exclude(Space&,int,int){returnME_SET_NONE;} template<classI>ModEvent EmptyView::excludeI(Space&,I&){ returnME_SET_NONE; } template<classI>ModEvent EmptyView::includeI(Space&,I&i){ returni()?ME_SET_FAILED:ME_SET_NONE; } template<classI>ModEvent EmptyView::intersectI(Space&,I&){ returnME_SET_NONE; } /* *Deltainformationforadvisors * */ forceinlineint EmptyView::glbMin(constDelta&)const{ GECODE_NEVER; return0; } forceinlineint EmptyView::glbMax(constDelta&)const{ GECODE_NEVER; return0; } forceinlinebool EmptyView::glbAny(constDelta&)const{ GECODE_NEVER; returnfalse; } forceinlineint EmptyView::lubMin(constDelta&)const{ GECODE_NEVER; return0; } forceinlineint EmptyView::lubMax(constDelta&)const{ GECODE_NEVER; return0; } forceinlinebool EmptyView::lubAny(constDelta&)const{ GECODE_NEVER; returnfalse; } //Constantuniversevariable forceinline UniverseView::UniverseView(void){} forceinlineunsignedint UniverseView::glbSize(void)const{returnSet::Limits::card;} forceinlineunsignedint UniverseView::lubSize(void)const{returnSet::Limits::card;} forceinlineunsignedint UniverseView::unknownSize(void)const{return0;} forceinlinebool UniverseView::contains(int)const{returntrue;} forceinlinebool UniverseView::notContains(int)const{returnfalse;} forceinlineunsignedint UniverseView::cardMin(void)const{returnSet::Limits::card;} forceinlineunsignedint UniverseView::cardMax(void)const{returnLimits::card;} forceinlineint UniverseView::lubMin(void)const{returnLimits::card;} forceinlineint UniverseView::lubMax(void)const{returnLimits::card;} forceinlineint UniverseView::glbMin(void)const{returnLimits::card;} forceinlineint UniverseView::glbMax(void)const{returnLimits::card;} forceinlineModEvent UniverseView::cardMin(Space&,unsignedintc){ returnc>Limits::card?ME_SET_FAILED:ME_SET_NONE; } forceinlineModEvent UniverseView::cardMax(Space&,unsignedintc){ returnc>=Limits::card?ME_SET_NONE:ME_SET_FAILED; } forceinlineModEvent UniverseView::include(Space&,int){ returnME_SET_NONE; } forceinlineModEvent UniverseView::exclude(Space&,int){returnME_SET_FAILED;} forceinlineModEvent UniverseView::intersect(Space&,int){returnME_SET_FAILED;} forceinlineModEvent UniverseView::include(Space&,int,int){returnME_SET_NONE;} forceinlineModEvent UniverseView::exclude(Space&,int,int){returnME_SET_FAILED;} template<classI>ModEvent UniverseView::excludeI(Space&,I&i){ returni()?ME_SET_FAILED:ME_SET_NONE; } template<classI>forceinlineModEvent UniverseView::includeI(Space&,I&){ returnME_SET_NONE; } forceinlineModEvent UniverseView::intersect(Space&,inti,intj){ return(i>Limits::min|| j<Limits::max)?ME_SET_FAILED:ME_SET_NONE; } template<classI>forceinlineModEvent UniverseView::intersectI(Space&,I&i){ return(i()&& (i.min()>Limits::min|| i.max()<Limits::max))? ME_SET_FAILED:ME_SET_NONE; } /* *Deltainformationforadvisors * */ forceinlineint UniverseView::glbMin(constDelta&)const{ GECODE_NEVER; return0; } forceinlineint UniverseView::glbMax(constDelta&)const{ GECODE_NEVER; return0; } forceinlinebool UniverseView::glbAny(constDelta&)const{ GECODE_NEVER; returnfalse; } forceinlineint UniverseView::lubMin(constDelta&)const{ GECODE_NEVER; return0; } forceinlineint UniverseView::lubMax(constDelta&)const{ GECODE_NEVER; return0; } forceinlinebool UniverseView::lubAny(constDelta&)const{ GECODE_NEVER; returnfalse; } /* *Iterators * */ template<> classLubRanges<EmptyView>:publicIter::Ranges::Empty{ public: LubRanges(void){} LubRanges(constEmptyView&x){(void)x;} voidinit(constEmptyView&x){(void)x;} }; template<> classGlbRanges<EmptyView>:publicIter::Ranges::Empty{ public: GlbRanges(void){} GlbRanges(constEmptyView&x){(void)x;} voidinit(constEmptyView&x){(void)x;} }; template<> classLubRanges<UniverseView>:publicIter::Ranges::Singleton{ public: LubRanges(void) :Iter::Ranges::Singleton(Limits::min, Limits::max){} LubRanges(constUniverseView&x) :Iter::Ranges::Singleton(Limits::min, Limits::max){ (void)x; } voidinit(constUniverseView&x){(void)x;} }; template<> classGlbRanges<UniverseView>:publicIter::Ranges::Singleton{ public: GlbRanges(void) :Iter::Ranges::Singleton(Limits::min, Limits::max){} GlbRanges(constUniverseView&x) :Iter::Ranges::Singleton(Limits::min, Limits::max){ (void)x; } voidinit(constUniverseView&x){(void)x;} }; template<> classLubRanges<ConstSetView>{ private: ArrayRangesar; public: LubRanges(void){} LubRanges(constConstSetView&x):ar(x.ranges,x.size){} voidinit(constConstSetView&x){ ar.init(x.ranges,x.size); } booloperator ()(void)const{returnar();} voidoperator ++(void){++ar;} intmin(void)const{returnar.min();} intmax(void)const{returnar.max();} unsignedintwidth(void)const{returnar.width();} }; template<> classGlbRanges<ConstSetView>:publicLubRanges<ConstSetView>{ public: GlbRanges(void){} GlbRanges(constConstSetView&x):LubRanges<ConstSetView>(x){} voidinit(constConstSetView&x){ LubRanges<ConstSetView>::init(x); } }; /* *Testing * */ forceinlinebool same(constConstSetView&x,constConstSetView&y){ if((x.size!=y.size)||(x.domSize!=y.domSize)) returnfalse; for(inti=x.size;i--;) if(x.ranges[2*i]!=y.ranges[2*i]|| x.ranges[2*i+1]!=y.ranges[2*i+1]) returnfalse; returntrue; } forceinlinebool before(constConstSetView&x,constConstSetView&y){ if(x.size<y.size) returntrue; if(x.domSize<y.domSize) returntrue; for(inti=x.size;i--;) if(x.ranges[2*i]<y.ranges[2*i]|| x.ranges[2*i+1]<y.ranges[2*i+1]) returntrue; returnfalse; } forceinlinebool same(constEmptyView&,constEmptyView&){ returntrue; } forceinlinebool same(constUniverseView&,constUniverseView&){ returntrue; } }} //STATISTICS:set-var