propagate.hpp /usr/include/gecode/int/bin-packing.hh Gecode Gecode::Int Gecode::Int::BinPacking /*-*-mode:C++;c-basic-offset:2;indent-tabs-mode:nil-*-*/ /* *Mainauthors: *ChristianSchulte<schulte@gecode.org> * *Copyright: *ChristianSchulte,2010 * *Lastmodified: *$Date:2010-10-0517:03:20+0200(Tue,05Oct2010)$by$Author:schulte$ *$Revision:11448$ * *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{namespaceInt{namespaceBinPacking{ /* *Item * */ forceinline Item::Item(void) :s(0){} forceinline Item::Item(IntViewb,ints0) :DerivedView<IntView>(b),s(s0){} forceinlineIntView Item::bin(void)const{ returnx; } forceinline voidItem::bin(IntViewb){ x=b; } forceinlineint Item::size(void)const{ returns; } forceinlinevoid Item::size(ints0){ s=s0; } forceinlinevoid Item::update(Space&home,boolshare,Item&i){ x.update(home,share,i.x); s=i.s; } forceinlinebool same(constItem&i,constItem&j){ returnsame(i.bin(),j.bin())&&(i.size()==j.size()); } forceinlinebool before(constItem&i,constItem&j){ returnbefore(i.bin(),j.bin()) ||(same(i.bin(),j.bin())&&(i.size()==j.size())); } forceinlinebool operator <(constItem&i,constItem&j){ returni.size()>j.size(); } /* *Sizeset * */ forceinline SizeSet::SizeSet(void){} forceinline SizeSet::SizeSet(Region&region,intn_max) :n(0),t(0),s(region.alloc<int>(n_max)){} forceinlinevoid SizeSet::add(ints0){ t+=s0;s[n++]=s0; } forceinlineint SizeSet::card(void)const{ returnn; } forceinlineint SizeSet::total(void)const{ returnt; } forceinlineint SizeSet::operator [](inti)const{ returns[i]; } forceinline SizeSetMinusOne::SizeSetMinusOne(void){} forceinline SizeSetMinusOne::SizeSetMinusOne(Region&region,intn_max) :SizeSet(region,n_max),p(-1){} forceinlinevoid SizeSetMinusOne::minus(ints0){ //Thisrestsonthefactthatitemsareremovedinorder do p++; while(s[p]>s0); assert(p<n); } forceinlineint SizeSetMinusOne::card(void)const{ assert(p>=0); returnn-1; } forceinlineint SizeSetMinusOne::total(void)const{ assert(p>=0); returnt-s[p]; } forceinlineint SizeSetMinusOne::operator [](inti)const{ assert(p>=0); returns[(i<p)?i:i+1]; } /* *Packingpropagator * */ forceinline Pack::Pack(Homehome,ViewArray<OffsetView>&l0,ViewArray<Item>&bs0) :Propagator(home),l(l0),bs(bs0),t(0){ l.subscribe(home,*this,PC_INT_BND); bs.subscribe(home,*this,PC_INT_DOM); for(inti=bs.size();i--;) t+=bs[i].size(); } forceinline Pack::Pack(Space&home,boolshared,Pack&p) :Propagator(home,shared,p),t(p.t){ l.update(home,shared,p.l); bs.update(home,shared,p.bs); } forceinlinesize_t Pack::dispose(Space&home){ l.cancel(home,*this,PC_INT_BND); bs.cancel(home,*this,PC_INT_DOM); (void)Propagator::dispose(home); returnsizeof(*this); } template<classSizeSet> forceinlinebool Pack::nosum(constSizeSet&s,inta,intb,int&ap,int&bp){ if((a<=0)||(b>=s.total())) returnfalse; intn=s.card()-1; intsc=0; intkp=0; while(sc+s[n-kp]<a){ sc+=s[n-kp]; kp++; } intk=0; intsa=0,sb=s[n-kp]; while((sa<a)&&(sb<=b)){ sa+=s[k++]; if(sa<a){ kp--; sb+=s[n-kp]; sc-=s[n-kp]; while(sa+sc>=a){ kp--; sc-=s[n-kp]; sb+=s[n-kp]-s[n-kp-k-1]; } } } ap=sa+sc;bp=sb; returnsa<a; } template<classSizeSet> forceinlinebool Pack::nosum(constSizeSet&s,inta,intb){ intap,bp; returnnosum(s,a,b,ap,bp); } }}} //STATISTICS:int-prop