int-set-1.hpp sstream /usr/include/gecode/int.hh Gecode::IntSetInit Gecode::IntSetInit< IntSet > Gecode /*-*-mode:C++;c-basic-offset:2;indent-tabs-mode:nil-*-*/ /* *Mainauthors: *ChristianSchulte<schulte@gecode.org> * *Copyright: *ChristianSchulte,2003 * *Lastmodified: *$Date:2013-03-1114:47:11+0100(Mon,11Mar2013)$by$Author:schulte$ *$Revision:13490$ * *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. * */ #include<sstream> namespaceGecode{ /* *Integersets * */ forceinline IntSet::IntSet(void){} template<classI> classIntSetInit{ public: staticvoidinit(IntSet&s,I&i){ Support::DynamicArray<IntSet::Range,Heap>d(heap); intn=0; unsignedintsize=0; while(i()){ d[n].min=i.min();d[n].max=i.max();size+=i.width(); ++n;++i; } if(n>0){ IntSet::IntSetObject*o=IntSet::IntSetObject::allocate(n); for(intj=n;j--;) o->r[j]=d[j]; o->size=size; s.object(o); } } }; template<> classIntSetInit<IntSet>{ public: staticvoidinit(IntSet&s,constIntSet&i){ s.object(i.object()); } }; template<classI> IntSet::IntSet(I&i){ IntSetInit<I>::init(*this,i); } template<classI> IntSet::IntSet(constI&i){ IntSetInit<I>::init(*this,i); } forceinline IntSet::IntSet(constintr[][2],intn){ init(r,n); } forceinline IntSet::IntSet(constintr[],intn){ init(r,n); } forceinline IntSet::IntSet(intn,intm){ init(n,m); } forceinlineint IntSet::min(inti)const{ assert(object()!=NULL); returnstatic_cast<IntSetObject*>(object())->r[i].min; } forceinlineint IntSet::max(inti)const{ assert(object()!=NULL); returnstatic_cast<IntSetObject*>(object())->r[i].max; } forceinlineunsignedint IntSet::width(inti)const{ assert(object()!=NULL); IntSetObject*o=static_cast<IntSetObject*>(object()); returnstatic_cast<unsignedint>(o->r[i].max-o->r[i].min)+1; } forceinlineint IntSet::ranges(void)const{ IntSetObject*o=static_cast<IntSetObject*>(object()); return(o==NULL)?0:o->n; } forceinlinebool IntSet::in(intn)const{ IntSetObject*o=static_cast<IntSetObject*>(object()); if((o==NULL)||(n<o->r[0].min)||(n>o->r[o->n-1].max)) returnfalse; else returno->in(n); } forceinlineint IntSet::min(void)const{ IntSetObject*o=static_cast<IntSetObject*>(object()); return(o==NULL)?Int::Limits::max:o->r[0].min; } forceinlineint IntSet::max(void)const{ IntSetObject*o=static_cast<IntSetObject*>(object()); return(o==NULL)?Int::Limits::min:o->r[o->n-1].max; } forceinlineunsignedint IntSet::size(void)const{ IntSetObject*o=static_cast<IntSetObject*>(object()); return(o==NULL)?0:o->size; } forceinlineunsignedint IntSet::width(void)const{ returnstatic_cast<unsignedint>(max()-min()+1); } /* *Rangeiteratorforintegersets * */ forceinline IntSetRanges::IntSetRanges(void){} forceinline void IntSetRanges::init(constIntSet&s){ intn=s.ranges(); if(n>0){ i=&static_cast<IntSet::IntSetObject*>(s.object())->r[0];e=i+n; }else{ i=e=NULL; } } forceinline IntSetRanges::IntSetRanges(constIntSet&s){init(s);} forceinlinevoid IntSetRanges::operator ++(void){ i++; } forceinlinebool IntSetRanges::operator ()(void)const{ returni<e; } forceinlineint IntSetRanges::min(void)const{ returni->min; } forceinlineint IntSetRanges::max(void)const{ returni->max; } forceinlineunsignedint IntSetRanges::width(void)const{ returnstatic_cast<unsignedint>(i->max-i->min)+1; } /* *Valueiteratorforintegersets * */ forceinline IntSetValues::IntSetValues(void){} forceinline IntSetValues::IntSetValues(constIntSet&s){ IntSetRangesr(s); Iter::Ranges::ToValues<IntSetRanges>::init(r); } forceinlinevoid IntSetValues::init(constIntSet&s){ IntSetRangesr(s); Iter::Ranges::ToValues<IntSetRanges>::init(r); } template<classChar,classTraits> std::basic_ostream<Char,Traits>& operator<<(std::basic_ostream<Char,Traits>&os,constIntSet&is){ std::basic_ostringstream<Char,Traits>s; s.copyfmt(os);s.width(0); s<<'{'; for(inti=0;i<is.ranges();){ intmin=is.min(i); intmax=is.max(i); if(min==max) s<<min; else s<<min<<".."<<max; i++; if(i<is.ranges()) s<<','; } s<<'}'; returnos<<s.str(); } } //STATISTICS:int-var