array.hpp cstdarg iostream iterator vector sstream /usr/include/gecode/kernel.hh Gecode::VarArray Gecode::VarArgArray Gecode::ArrayTraits Gecode::VarArray Gecode::ViewArray Gecode::ViewArray::ViewLess Gecode::ArgArrayBase Gecode::PrimArgArray Gecode::PrimArgArray Gecode::ArgArray Gecode::ArgArray Gecode::VarArgArray Gecode::VarArgArray Gecode::VarArgArray::VarLess Gecode /*-*-mode:C++;c-basic-offset:2;indent-tabs-mode:nil-*-*/ /* *Mainauthors: *ChristianSchulte<schulte@gecode.org> *GuidoTack<tack@gecode.org> * *Contributingauthors: *GregoryCrosswhite<gcross@phys.washington.edu> * *Copyright: *GregoryCrosswhite,2011 *ChristianSchulte,2003 *GuidoTack,2004 * *Lastmodified: *$Date:2013-03-0717:39:13+0100(Thu,07Mar2013)$by$Author:schulte$ *$Revision:13458$ * *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<cstdarg> #include<iostream> #include<iterator> #include<vector> #include<sstream> namespaceGecode{ template<classVar>classVarArray; template<classVar>classVarArgArray; template<classA> classArrayTraits{}; template<classVar> classVarArray{ protected: intn; Var*x; public: typedefVarvalue_type; typedefVar&reference; typedefconstVar&const_reference; typedefVar*pointer; typedefconstVar*const_pointer; typedefVar*iterator; typedefconstVar*const_iterator; typedefstd::reverse_iterator<Var*>reverse_iterator; typedefstd::reverse_iterator<constVar*>const_reverse_iterator; VarArray(void); VarArray(Space&home,intm); VarArray(Space&home,constVarArgArray<Var>&); VarArray(constVarArray<Var>&a); constVarArray<Var>&operator =(constVarArray<Var>&a); intsize(void)const; Var&operator [](inti); constVar&operator [](inti)const; typenameArrayTraits<VarArgArray<Var>>::ArgsType slice(intstart,intinc=1,intn=-1); iteratorbegin(void); const_iteratorbegin(void)const; iteratorend(void); const_iteratorend(void)const; reverse_iteratorrbegin(void); const_reverse_iteratorrbegin(void)const; reverse_iteratorrend(void); const_reverse_iteratorrend(void)const; boolassigned(void)const; voidupdate(Space&,boolshare,VarArray<Var>&a); private: staticvoid*operatornew(size_t); staticvoidoperatordelete(void*,size_t); }; template<classT> typenameArrayTraits<VarArray<T>>::ArgsType operator+(constVarArray<T>&x,constVarArgArray<T>&y); template<classT> typenameArrayTraits<VarArray<T>>::ArgsType operator+(constVarArray<T>&x,constVarArray<T>&y); template<classT> typenameArrayTraits<VarArray<T>>::ArgsType operator+(constVarArgArray<T>&x,constVarArray<T>&y); template<classT> typenameArrayTraits<VarArray<T>>::ArgsType operator+(constVarArray<T>&x,constT&y); template<classT> typenameArrayTraits<VarArray<T>>::ArgsType operator+(constT&x,constVarArray<T>&y); template<classView> classViewArray{ private: intn; View*x; template<classX> classViewLess{ public: booloperator()(constX&,constX&); }; staticvoidsort(View*x,intn); public: typedefViewvalue_type; typedefView&reference; typedefconstView&const_reference; typedefView*pointer; typedefconstView*const_pointer; typedefView*iterator; typedefconstView*const_iterator; typedefstd::reverse_iterator<View*>reverse_iterator; typedefstd::reverse_iterator<constView*>const_reverse_iterator; ViewArray(void); ViewArray(Space&home,intm); ViewArray(Region&r,intm); ViewArray(constViewArray<View>&a); ViewArray(Space&home,constViewArray<View>&a); ViewArray(Region&r,constViewArray<View>&a); constViewArray<View>&operator =(constViewArray<View>&a); template<classVar> ViewArray(Space&home,constVarArgArray<Var>&a) :n(a.size()){ //Thismaynotbeinthehppfile(tosatisfytheMScompiler) if(n>0){ x=home.alloc<View>(n); for(inti=n;i--;) x[i]=a[i]; }else{ x=NULL; } } template<classVar> ViewArray(Region&r,constVarArgArray<Var>&a) :n(a.size()){ //Thismaynotbeinthehppfile(tosatisfytheMScompiler) if(n>0){ x=r.alloc<View>(n); for(inti=n;i--;) x[i]=a[i]; }else{ x=NULL; } } intsize(void)const; voidsize(intn); View&operator [](inti); constView&operator [](inti)const; iteratorbegin(void); const_iteratorbegin(void)const; iteratorend(void); const_iteratorend(void)const; reverse_iteratorrbegin(void); const_reverse_iteratorrbegin(void)const; reverse_iteratorrend(void); const_reverse_iteratorrend(void)const; voidsubscribe(Space&home,Propagator&p,PropCondpc,boolprocess=true); voidcancel(Space&home,Propagator&p,PropCondpc); voidsubscribe(Space&home,Advisor&a); voidcancel(Space&home,Advisor&a); voidupdate(Space&,boolshare,ViewArray<View>&a); voidmove_fst(inti); voidmove_lst(inti); voidmove_fst(inti,Space&home,Propagator&p,PropCondpc); voidmove_lst(inti,Space&home,Propagator&p,PropCondpc); voidmove_fst(inti,Space&home,Advisor&a); voidmove_lst(inti,Space&home,Advisor&a); voiddrop_fst(inti); voiddrop_lst(inti); voiddrop_fst(inti,Space&home,Propagator&p,PropCondpc); voiddrop_lst(inti,Space&home,Propagator&p,PropCondpc); voiddrop_fst(inti,Space&home,Advisor&a); voiddrop_lst(inti,Space&home,Advisor&a); boolassigned(void)const; boolsame(constSpace&home)const; boolsame(constSpace&home,constView&y)const; voidunique(constSpace&home); boolshared(constSpace&home)const; template<classViewY> boolshared(constSpace&home,constViewY&y)const; template<classViewY> boolshared(constSpace&home,constViewArray<ViewY>&y)const; private: staticvoid*operatornew(size_t); staticvoidoperatordelete(void*,size_t); }; template<classT> classArgArrayBase{ protected: intn; intcapacity; T*a; staticconstintonstack_size=16; Tonstack[onstack_size]; T*allocate(intn); voidresize(inti); template<classA> Aconcat(constArgArrayBase<T>&x)const; template<classA> Aconcat(constT&x)const; template<classA> A&append(constT&x); template<classA> A&append(constArgArrayBase<T>&x); template<classA> Aslice(intstart,intinc=1,intn=-1); public: typedefTvalue_type; typedefT&reference; typedefconstT&const_reference; typedefT*pointer; typedefconstT*const_pointer; typedefT*iterator; typedefconstT*const_iterator; typedefstd::reverse_iterator<T*>reverse_iterator; typedefstd::reverse_iterator<constT*>const_reverse_iterator; ArgArrayBase(void); explicitArgArrayBase(intn); ArgArrayBase(constArgArrayBase<T>&a); constArgArrayBase<T>&operator =(constArgArrayBase<T>&a); ArgArrayBase(conststd::vector<T>&a); template<classInputIterator> ArgArrayBase(InputIteratorfirst,InputIteratorlast); intsize(void)const; T&operator [](inti); constT&operator [](inti)const; iteratorbegin(void); const_iteratorbegin(void)const; iteratorend(void); const_iteratorend(void)const; reverse_iteratorrbegin(void); const_reverse_iteratorrbegin(void)const; reverse_iteratorrend(void); const_reverse_iteratorrend(void)const; ~ArgArrayBase(void); private: staticvoid*operatornew(size_t); staticvoidoperatordelete(void*,size_t); }; template<class>classPrimArgArray; template<classT> typenameArrayTraits<PrimArgArray<T>>::ArgsType operator +(constPrimArgArray<T>&x,constPrimArgArray<T>&y); template<classT> typenameArrayTraits<PrimArgArray<T>>::ArgsType operator +(constPrimArgArray<T>&x,constT&y); template<classT> typenameArrayTraits<PrimArgArray<T>>::ArgsType operator +(constT&x,constPrimArgArray<T>&y); template<classT> classPrimArgArray:publicArgArrayBase<T>{ protected: usingArgArrayBase<T>::a; public: usingArgArrayBase<T>::size; PrimArgArray(void); explicitPrimArgArray(intn); PrimArgArray(intn,Te0,...); PrimArgArray(intn,constT*e); PrimArgArray(constPrimArgArray<T>&a); PrimArgArray(conststd::vector<T>&a); template<classInputIterator> PrimArgArray(InputIteratorfirst,InputIteratorlast); typenameArrayTraits<PrimArgArray<T>>::ArgsType slice(intstart,intinc=1,intn=-1); typenameArrayTraits<PrimArgArray<T>>::ArgsType& operator <<(constT&x); typenameArrayTraits<PrimArgArray<T>>::ArgsType& operator<<(constPrimArgArray<T>&x); friendtypenameArrayTraits<PrimArgArray<T>>::ArgsType operator+<>(constPrimArgArray<T>&x,constPrimArgArray<T>&y); friendtypenameArrayTraits<PrimArgArray<T>>::ArgsType operator+<>(constPrimArgArray<T>&x,constT&y); friend typenameArrayTraits<PrimArgArray<T>>::ArgsType operator+<>(constT&x,constPrimArgArray<T>&y); }; template<class>classArgArray; template<classT> typenameArrayTraits<ArgArray<T>>::ArgsType operator +(constArgArray<T>&x,constArgArray<T>&y); template<classT> typenameArrayTraits<ArgArray<T>>::ArgsType operator +(constArgArray<T>&x,constT&y); template<classT> typenameArrayTraits<ArgArray<T>>::ArgsType operator +(constT&x,constArgArray<T>&y); template<classT> classArgArray:publicArgArrayBase<T>{ protected: usingArgArrayBase<T>::a; public: usingArgArrayBase<T>::size; ArgArray(void); explicitArgArray(intn); ArgArray(intn,constT*e); ArgArray(constArgArray<T>&a); ArgArray(conststd::vector<T>&a); template<classInputIterator> ArgArray(InputIteratorfirst,InputIteratorlast); typenameArrayTraits<ArgArray<T>>::ArgsType slice(intstart,intinc=1,intn=-1); typenameArrayTraits<ArgArray<T>>::ArgsType& operator <<(constT&x); typenameArrayTraits<ArgArray<T>>::ArgsType& operator<<(constArgArray<T>&x); friendtypenameArrayTraits<ArgArray<T>>::ArgsType operator+<>(constArgArray<T>&x,constArgArray<T>&y); friendtypenameArrayTraits<ArgArray<T>>::ArgsType operator+<>(constArgArray<T>&x,constT&y); friend typenameArrayTraits<ArgArray<T>>::ArgsType operator+<>(constT&x,constArgArray<T>&y); }; template<class>classVarArgArray; template<classVar> typenameArrayTraits<VarArgArray<Var>>::ArgsType operator +(constVarArgArray<Var>&x,constVarArgArray<Var>&y); template<classVar> typenameArrayTraits<VarArgArray<Var>>::ArgsType operator +(constVarArgArray<Var>&x,constVar&y); template<classVar> typenameArrayTraits<VarArgArray<Var>>::ArgsType operator +(constVar&x,constVarArgArray<Var>&y); template<classVar> classVarArgArray:publicArgArrayBase<Var>{ protected: usingArgArrayBase<Var>::a; usingArgArrayBase<Var>::n; classVarLess{ public: booloperator()(constVar&,constVar&); }; public: usingArgArrayBase<Var>::size; VarArgArray(void); explicitVarArgArray(intn); VarArgArray(constVarArgArray<Var>&a); VarArgArray(constVarArray<Var>&a); VarArgArray(conststd::vector<Var>&a); template<classInputIterator> VarArgArray(InputIteratorfirst,InputIteratorlast); typenameArrayTraits<VarArgArray<Var>>::ArgsType slice(intstart,intinc=1,intn=-1); typenameArrayTraits<VarArgArray<Var>>::ArgsType& operator <<(constVar&x); typenameArrayTraits<VarArgArray<Var>>::ArgsType& operator<<(constVarArgArray<Var>&x); boolassigned(void)const; friendtypenameArrayTraits<VarArgArray<Var>>::ArgsType operator+<>(constVarArgArray<Var>&x,constVarArgArray<Var>&y); friendtypenameArrayTraits<VarArgArray<Var>>::ArgsType operator+<>(constVarArgArray<Var>&x,constVar&y); friend typenameArrayTraits<VarArgArray<Var>>::ArgsType operator+<>(constVar&x,constVarArgArray<Var>&y); boolsame(constSpace&home)const; boolsame(constSpace&home,constVar&y)const; boolsame(constSpace&home,constVarArgArray<Var>&y)const; }; template<classChar,classTraits,classVar> std::basic_ostream<Char,Traits>& operator<<(std::basic_ostream<Char,Traits>&os, constVarArray<Var>&x); template<classChar,classTraits,classView> std::basic_ostream<Char,Traits>& operator<<(std::basic_ostream<Char,Traits>&os,constViewArray<View>&x); template<classChar,classTraits,classT> std::basic_ostream<Char,Traits>& operator<<(std::basic_ostream<Char,Traits>&os,constArgArrayBase<T>&x); /* *Implementation * */ /* *Variablearrays * *Thesearraysareallocatedinthespace. * */ template<classVar> forceinline VarArray<Var>::VarArray(void):n(0),x(NULL){} template<classVar> forceinline VarArray<Var>::VarArray(Space&home,intn0) :n(n0){ //Allocatefromspace x=(n>0)?home.alloc<Var>(n):NULL; } template<classVar> forceinline VarArray<Var>::VarArray(constVarArray<Var>&a){ n=a.n;x=a.x; } template<classVar> inlineconstVarArray<Var>& VarArray<Var>::operator =(constVarArray<Var>&a){ n=a.n;x=a.x; return*this; } template<classVar> forceinlineint VarArray<Var>::size(void)const{ returnn; } template<classVar> forceinlineVar& VarArray<Var>::operator [](inti){ assert((i>=0)&&(i<size())); returnx[i]; } template<classVar> forceinlineconstVar& VarArray<Var>::operator [](inti)const{ assert((i>=0)&&(i<size())); returnx[i]; } template<classVar> typenameArrayTraits<VarArgArray<Var>>::ArgsType VarArray<Var>::slice(intstart,intinc,intmaxN){ assert(n==0||start<n); if(n==0||maxN<0) maxN=n; ints; if(inc==0) s=n-start; elseif(inc>0) s=(n-start)/inc+((n-start)%inc==0?0:1); else s=(start+1)/-inc+((start+1)%-inc==0?0:1); typenameArrayTraits<VarArgArray<Var>>::ArgsTyper(std::min(maxN,s)); for(inti=0;i<r.size();i++,start+=inc) r[i]=x[start]; returnr; } template<classVar> forceinlinetypenameVarArray<Var>::iterator VarArray<Var>::begin(void){ returnx; } template<classVar> forceinlinetypenameVarArray<Var>::const_iterator VarArray<Var>::begin(void)const{ returnx; } template<classVar> forceinlinetypenameVarArray<Var>::iterator VarArray<Var>::end(void){ returnx+n; } template<classVar> forceinlinetypenameVarArray<Var>::const_iterator VarArray<Var>::end(void)const{ returnx+n; } template<classVar> forceinlinetypenameVarArray<Var>::reverse_iterator VarArray<Var>::rbegin(void){ returnreverse_iterator(x+n); } template<classVar> forceinlinetypenameVarArray<Var>::const_reverse_iterator VarArray<Var>::rbegin(void)const{ returnconst_reverse_iterator(x+n); } template<classVar> forceinlinetypenameVarArray<Var>::reverse_iterator VarArray<Var>::rend(void){ returnreverse_iterator(x); } template<classVar> forceinlinetypenameVarArray<Var>::const_reverse_iterator VarArray<Var>::rend(void)const{ returnconst_reverse_iterator(x); } template<classVar> forceinlinevoid VarArray<Var>::update(Space&home,boolshare,VarArray<Var>&a){ n=a.n; if(n>0){ x=home.alloc<Var>(n); for(inti=n;i--;) x[i].update(home,share,a.x[i]); }else{ x=NULL; } } template<classVar> forceinlinebool VarArray<Var>::assigned(void)const{ for(inti=n;i--;) if(!x[i].assigned()) returnfalse; returntrue; } template<classVar> forceinlinevoid* VarArray<Var>::operatornew(size_t){ returnNULL; } template<classVar> forceinlinevoid VarArray<Var>::operatordelete(void*,size_t){ } template<classVar> typenameArrayTraits<VarArray<Var>>::ArgsType operator+(constVarArray<Var>&x,constVarArray<Var>&y){ typenameArrayTraits<VarArray<Var>>::ArgsTyper(x.size()+y.size()); for(inti=x.size();i--;) r[i]=x[i]; for(inti=y.size();i--;) r[x.size()+i]=y[i]; returnr; } template<classVar> typenameArrayTraits<VarArray<Var>>::ArgsType operator+(constVarArray<Var>&x,constVarArgArray<Var>&y){ typenameArrayTraits<VarArray<Var>>::ArgsTyper(x.size()+y.size()); for(inti=x.size();i--;) r[i]=x[i]; for(inti=y.size();i--;) r[x.size()+i]=y[i]; returnr; } template<classVar> typenameArrayTraits<VarArray<Var>>::ArgsType operator+(constVarArgArray<Var>&x,constVarArray<Var>&y){ typenameArrayTraits<VarArray<Var>>::ArgsTyper(x.size()+y.size()); for(inti=x.size();i--;) r[i]=x[i]; for(inti=y.size();i--;) r[x.size()+i]=y[i]; returnr; } template<classVar> typenameArrayTraits<VarArray<Var>>::ArgsType operator+(constVarArray<Var>&x,constVar&y){ typenameArrayTraits<VarArray<Var>>::ArgsTyper(x.size()+1); for(inti=x.size();i--;) r[i]=x[i]; r[x.size()]=y; returnr; } template<classVar> typenameArrayTraits<VarArray<Var>>::ArgsType operator+(constVar&x,constVarArray<Var>&y){ typenameArrayTraits<VarArray<Var>>::ArgsTyper(y.size()+1); r[0]=x; for(inti=y.size();i--;) r[1+i]=y[i]; returnr; } /* *Viewarrays * */ template<classView> forceinline ViewArray<View>::ViewArray(void):n(0),x(NULL){} template<classView> forceinline ViewArray<View>::ViewArray(Space&home,intn0) :n(n0){ x=(n>0)?home.alloc<View>(n):NULL; } template<classView> forceinline ViewArray<View>::ViewArray(Region&r,intn0) :n(n0){ x=(n>0)?r.alloc<View>(n):NULL; } template<classView> ViewArray<View>::ViewArray(Space&home,constViewArray<View>&a) :n(a.size()){ if(n>0){ x=home.alloc<View>(n); for(inti=n;i--;) x[i]=a[i]; }else{ x=NULL; } } template<classView> ViewArray<View>::ViewArray(Region&r,constViewArray<View>&a) :n(a.size()){ if(n>0){ x=r.alloc<View>(n); for(inti=n;i--;) x[i]=a[i]; }else{ x=NULL; } } template<classView> forceinline ViewArray<View>::ViewArray(constViewArray<View>&a) :n(a.n),x(a.x){} template<classView> forceinlineconstViewArray<View>& ViewArray<View>::operator =(constViewArray<View>&a){ n=a.n;x=a.x; return*this; } template<classView> forceinlineint ViewArray<View>::size(void)const{ returnn; } template<classView> forceinlinevoid ViewArray<View>::size(intn0){ n=n0; } template<classView> forceinlineView& ViewArray<View>::operator [](inti){ assert((i>=0)&&(i<size())); returnx[i]; } template<classView> forceinlineconstView& ViewArray<View>::operator [](inti)const{ assert((i>=0)&&(i<size())); returnx[i]; } template<classView> forceinlinetypenameViewArray<View>::iterator ViewArray<View>::begin(void){ returnx; } template<classView> forceinlinetypenameViewArray<View>::const_iterator ViewArray<View>::begin(void)const{ returnx; } template<classView> forceinlinetypenameViewArray<View>::iterator ViewArray<View>::end(void){ returnx+n; } template<classView> forceinlinetypenameViewArray<View>::const_iterator ViewArray<View>::end(void)const{ returnx+n; } template<classView> forceinlinetypenameViewArray<View>::reverse_iterator ViewArray<View>::rbegin(void){ returnreverse_iterator(x+n); } template<classView> forceinlinetypenameViewArray<View>::const_reverse_iterator ViewArray<View>::rbegin(void)const{ returnconst_reverse_iterator(x+n); } template<classView> forceinlinetypenameViewArray<View>::reverse_iterator ViewArray<View>::rend(void){ returnreverse_iterator(x); } template<classView> forceinlinetypenameViewArray<View>::const_reverse_iterator ViewArray<View>::rend(void)const{ returnconst_reverse_iterator(x); } template<classView> forceinlinevoid ViewArray<View>::move_fst(inti){ x[i]=x[0];x++;n--; } template<classView> forceinlinevoid ViewArray<View>::move_lst(inti){ n--;x[i]=x[n]; } template<classView> forceinlinevoid ViewArray<View>::drop_fst(inti){ assert(i>=0); x+=i;n-=i; } template<classView> forceinlinevoid ViewArray<View>::drop_lst(inti){ assert(i<n); n=i+1; } template<classView> forceinlinevoid ViewArray<View>::move_fst(inti,Space&home,Propagator&p,PropCondpc){ //Movex[0]tox[i] x[i].cancel(home,p,pc); x[i]=x[0];x++;n--; } template<classView> forceinlinevoid ViewArray<View>::move_lst(inti,Space&home,Propagator&p,PropCondpc){ //Movex[n-1]tox[i] x[i].cancel(home,p,pc); n--;x[i]=x[n]; } template<classView> void ViewArray<View>::drop_fst(inti,Space&home,Propagator&p,PropCondpc){ //Dropelementsfrom0..i-1 assert(i>=0); for(intj=i;j--;) x[j].cancel(home,p,pc); x+=i;n-=i; } template<classView> void ViewArray<View>::drop_lst(inti,Space&home,Propagator&p,PropCondpc){ //Dropelementsfromi+1..n-1 assert(i<n); for(intj=i+1;j<n;j++) x[j].cancel(home,p,pc); n=i+1; } template<classView> forceinlinevoid ViewArray<View>::move_fst(inti,Space&home,Advisor&a){ //Movex[0]tox[i] x[i].cancel(home,a); x[i]=x[0];x++;n--; } template<classView> forceinlinevoid ViewArray<View>::move_lst(inti,Space&home,Advisor&a){ //Movex[n-1]tox[i] x[i].cancel(home,a); n--;x[i]=x[n]; } template<classView> void ViewArray<View>::drop_fst(inti,Space&home,Advisor&a){ //Dropelementsfrom0..i-1 assert(i>=0); for(intj=i;j--;) x[j].cancel(home,a); x+=i;n-=i; } template<classView> void ViewArray<View>::drop_lst(inti,Space&home,Advisor&a){ //Dropelementsfromi+1..n-1 assert(i<n); for(intj=i+1;j<n;j++) x[j].cancel(home,a); n=i+1; } template<classView> void ViewArray<View>::update(Space&home,boolshare,ViewArray<View>&y){ n=y.n; if(n>0){ x=home.alloc<View>(n); for(inti=n;i--;) x[i].update(home,share,y.x[i]); }else{ x=NULL; } } template<classView> void ViewArray<View>::subscribe(Space&home,Propagator&p,PropCondpc, boolprocess){ for(inti=n;i--;) x[i].subscribe(home,p,pc,process); } template<classView> void ViewArray<View>::cancel(Space&home,Propagator&p,PropCondpc){ for(inti=n;i--;) x[i].cancel(home,p,pc); } template<classView> void ViewArray<View>::subscribe(Space&home,Advisor&a){ for(inti=n;i--;) x[i].subscribe(home,a); } template<classView> void ViewArray<View>::cancel(Space&home,Advisor&a){ for(inti=n;i--;) x[i].cancel(home,a); } template<classView> forceinlinebool ViewArray<View>::assigned(void)const{ for(inti=n;i--;) if(!x[i].assigned()) returnfalse; returntrue; } template<classView> forceinlinebool __before(constView&x,constView&y){ returnbefore(x,y); } template<classView>template<classX> forceinlinebool ViewArray<View>::ViewLess<X>::operator()(constX&a,constX&b){ return__before(a,b); } template<classView> void ViewArray<View>::sort(View*y,intm){ ViewLess<View>vl; Support::quicksort<View,ViewLess<View>>(y,m,vl); } template<classX,classY> forceinlinebool __same(constX&x,constY&y){ returnsame(x,y); } template<classX,classY> forceinlinebool __shared(constX&x,constY&y){ returnshared(x,y); } template<classView> bool ViewArray<View>::same(constSpace&home)const{ if(n<2) returnfalse; Regionr(home); View*y=r.alloc<View>(n); for(inti=n;i--;) y[i]=x[i]; sort(y,n); for(inti=n-1;i--;) if(!y[i].assigned()&&__same(y[i+1],y[i])){ r.free<View>(y,n); returntrue; } r.free<View>(y,n); returnfalse; } template<classView> bool ViewArray<View>::same(constSpace&,constView&y)const{ if(y.assigned()) returnfalse; for(inti=n;i--;) if(__same(x[i],y)) returntrue; returnfalse; } template<classView> void ViewArray<View>::unique(constSpace&){ if(n<2) return; sort(x,n); intj=0; for(inti=1;i<n;i++) if(!__same(x[j],x[i])) x[++j]=x[i]; n=j+1; } template<classView> bool ViewArray<View>::shared(constSpace&home)const{ if(n<2) returnfalse; Regionr(home); View*y=r.alloc<View>(n); for(inti=n;i--;) y[i]=x[i]; sort(y,n); for(inti=n-1;i--;) if(!y[i].assigned()&&__shared(y[i+1],y[i])){ r.free<View>(y,n); returntrue; } r.free<View>(y,n); returnfalse; } template<classView>template<classViewY> bool ViewArray<View>::shared(constSpace&,constViewY&y)const{ if(y.assigned()) returnfalse; for(inti=n;i--;) if(!x[i].assigned()&&__shared(x[i],y)) returntrue; returnfalse; } template<classView>template<classViewY> bool ViewArray<View>::shared(constSpace&home,constViewArray<ViewY>&y)const{ if((size()<1)||(y.size()<1)) returnfalse; Regionr(home); View*xs=r.alloc<View>(size()); for(inti=size();i--;) xs[i]=x[i]; ViewLess<View>xvl; Support::quicksort<View,ViewLess<View>>(xs,size(),xvl); ViewY*ys=r.alloc<ViewY>(y.size()); for(intj=y.size();j--;) ys[j]=y[j]; ViewLess<ViewY>yvl; Support::quicksort<ViewY,ViewLess<ViewY>>(ys,y.size(),yvl); { inti=0,j=0; while((i<size())&&(j<y.size())) if(!x[i].assigned()&&__shared(x[i],y[j])){ r.free<View>(xs,size()); r.free<ViewY>(ys,y.size()); returntrue; }elseif(before(x[i],y[j])){ i++; }else{ j++; } } r.free<View>(xs,size()); r.free<ViewY>(ys,y.size()); returnfalse; } template<classView> forceinlinevoid* ViewArray<View>::operatornew(size_t){ returnNULL; } template<classView> forceinlinevoid ViewArray<View>::operatordelete(void*,size_t){ } /* *Argumentarrays:baseclass * */ template<classT> forceinlineT* ArgArrayBase<T>::allocate(intn){ return(n>onstack_size)? heap.alloc<T>(static_cast<unsignedint>(n)):&onstack[0]; } template<classT> forceinlinevoid ArgArrayBase<T>::resize(inti){ if(n+i>=capacity){ assert(n+i>=onstack_size); intnewCapacity=(3*capacity)/2; if(newCapacity<=n+i) newCapacity=n+i; T*newA=allocate(newCapacity); heap.copy<T>(newA,a,n); if(capacity>onstack_size) heap.free(a,capacity); capacity=newCapacity; a=newA; } } template<classT> forceinline ArgArrayBase<T>::ArgArrayBase(void) :n(0),capacity(onstack_size),a(allocate(0)){} template<classT> forceinline ArgArrayBase<T>::ArgArrayBase(intn0) :n(n0),capacity(n<onstack_size?onstack_size:n),a(allocate(n)){} template<classT> inline ArgArrayBase<T>::ArgArrayBase(constArgArrayBase<T>&aa) :n(aa.n),capacity(n<onstack_size?onstack_size:n),a(allocate(n)){ heap.copy<T>(a,aa.a,n); } template<classT> inline ArgArrayBase<T>::ArgArrayBase(conststd::vector<T>&aa) :n(static_cast<int>(aa.size())), capacity(n<onstack_size?onstack_size:n),a(allocate(n)){ heap.copy<T>(a,&aa[0],n); } template<classT> forceinline ArgArrayBase<T>::~ArgArrayBase(void){ if(capacity>onstack_size) heap.free(a,capacity); } template<classT> forceinlineconstArgArrayBase<T>& ArgArrayBase<T>::operator =(constArgArrayBase<T>&aa){ if(&aa!=this){ if(capacity>onstack_size) heap.free(a,capacity); n=aa.n; capacity=(n<onstack_size?onstack_size:n); a=allocate(aa.n); heap.copy<T>(a,aa.a,n); } return*this; } template<classT> forceinlineint ArgArrayBase<T>::size(void)const{ returnn; } template<classT> forceinlineT& ArgArrayBase<T>::operator [](inti){ assert((i>=0)&&(i<n)); returna[i]; } template<classT> forceinlineconstT& ArgArrayBase<T>::operator [](inti)const{ assert((i>=0)&&(i<n)); returna[i]; } template<classT> forceinlinetypenameArgArrayBase<T>::iterator ArgArrayBase<T>::begin(void){ returna; } template<classT> forceinlinetypenameArgArrayBase<T>::const_iterator ArgArrayBase<T>::begin(void)const{ returna; } template<classT> forceinlinetypenameArgArrayBase<T>::iterator ArgArrayBase<T>::end(void){ returna+n; } template<classT> forceinlinetypenameArgArrayBase<T>::const_iterator ArgArrayBase<T>::end(void)const{ returna+n; } template<classT> forceinlinetypenameArgArrayBase<T>::reverse_iterator ArgArrayBase<T>::rbegin(void){ returnreverse_iterator(a+n); } template<classT> forceinlinetypenameArgArrayBase<T>::const_reverse_iterator ArgArrayBase<T>::rbegin(void)const{ returnconst_reverse_iterator(a+n); } template<classT> forceinlinetypenameArgArrayBase<T>::reverse_iterator ArgArrayBase<T>::rend(void){ returnreverse_iterator(a); } template<classT> forceinlinetypenameArgArrayBase<T>::const_reverse_iterator ArgArrayBase<T>::rend(void)const{ returnconst_reverse_iterator(a); } template<classT>template<classA> A ArgArrayBase<T>::slice(intstart,intinc,intmaxN){ assert(n==0||start<n); if(n==0||maxN<0) maxN=n; ints; if(inc==0) s=n-start; elseif(inc>0) s=(n-start)/inc+((n-start)%inc==0?0:1); else s=(start+1)/-inc+((start+1)%-inc==0?0:1); Ar(std::min(maxN,s)); for(inti=0;i<r.size();i++,start+=inc) new(&r[i])T(a[start]); returnr; } template<classT>template<classA> inlineA& ArgArrayBase<T>::append(constT&x){ resize(1); new(&a[n++])T(x); returnstatic_cast<A&>(*this); } template<classT> template<classInputIterator> inline ArgArrayBase<T>::ArgArrayBase(InputIteratorfirst,InputIteratorlast) :n(0),capacity(onstack_size),a(allocate(0)){ while(first!=last){ (void)append<ArgArrayBase<T>>(*first); ++first; } } template<classT>template<classA> inlineA& ArgArrayBase<T>::append(constArgArrayBase<T>&x){ resize(x.size()); for(inti=0;i<x.size();i++) new(&a[n++])T(x[i]); returnstatic_cast<A&>(*this); } template<classT>template<classA> inlineA ArgArrayBase<T>::concat(constArgArrayBase<T>&x)const{ Ar(n+x.n); for(inti=n;i--;) new(&r[i])T(a[i]); for(inti=x.n;i--;) new(&r[n+i])T(x.a[i]); returnr; } template<classT>template<classA> inlineA ArgArrayBase<T>::concat(constT&x)const{ Ar(n+1); for(inti=n;i--;) new(&r[i])T(a[i]); new(&r[n])T(x); returnr; } template<classT> forceinlinevoid* ArgArrayBase<T>::operatornew(size_t){ returnNULL; } template<classT> forceinlinevoid ArgArrayBase<T>::operatordelete(void*,size_t){ } /* *Argumentarraysforprimitivetypes * */ template<classT> forceinline PrimArgArray<T>::PrimArgArray(void){} template<classT> forceinline PrimArgArray<T>::PrimArgArray(intn):ArgArrayBase<T>(n){} template<classT> PrimArgArray<T>::PrimArgArray(intn,Ta0,...) :ArgArrayBase<T>(n){ va_listargs; va_start(args,a0); a[0]=a0; for(inti=1;i<n;i++) a[i]=va_arg(args,T); va_end(args); } template<classT> PrimArgArray<T>::PrimArgArray(intn,constT*a0) :ArgArrayBase<T>(n){ for(inti=n;i--;) a[i]=a0[i]; } template<classT> forceinline PrimArgArray<T>::PrimArgArray(constPrimArgArray<T>&aa) :ArgArrayBase<T>(aa){} template<classT> forceinline PrimArgArray<T>::PrimArgArray(conststd::vector<T>&aa) :ArgArrayBase<T>(aa){} template<classT> template<classInputIterator> forceinline PrimArgArray<T>::PrimArgArray(InputIteratorfirst,InputIteratorlast) :ArgArrayBase<T>(first,last){} template<classT> forceinlinetypenameArrayTraits<PrimArgArray<T>>::ArgsType PrimArgArray<T>::slice(intstart,intinc,intmaxN){ returnArgArrayBase<T>::templateslice <typenameArrayTraits<PrimArgArray<T>>::ArgsType> (start,inc,maxN); } template<classT> forceinlinetypenameArrayTraits<PrimArgArray<T>>::ArgsType& PrimArgArray<T>::operator <<(constT&x){ return ArgArrayBase<T>::templateappend <typenameArrayTraits<PrimArgArray<T>>::ArgsType>(x); } template<classT> forceinlinetypenameArrayTraits<PrimArgArray<T>>::ArgsType& PrimArgArray<T>::operator <<(constPrimArgArray<T>&x){ return ArgArrayBase<T>::templateappend <typenameArrayTraits<PrimArgArray<T>>::ArgsType>(x); } template<classT> typenameArrayTraits<PrimArgArray<T>>::ArgsType operator+(constPrimArgArray<T>&x,constPrimArgArray<T>&y){ returnx.templateconcat <typenameArrayTraits<PrimArgArray<T>>::ArgsType>(y); } template<classT> typenameArrayTraits<PrimArgArray<T>>::ArgsType operator+(constPrimArgArray<T>&x,constT&y){ returnx.templateconcat <typenameArrayTraits<PrimArgArray<T>>::ArgsType>(y); } template<classT> typenameArrayTraits<PrimArgArray<T>>::ArgsType operator+(constT&x,constPrimArgArray<T>&y){ returnPrimArgArray<T>(1,x).templateconcat <typenameArrayTraits<PrimArgArray<T>>::ArgsType>(y); } /* *Argumentarraysfornon-primitivetypes * */ template<classT> forceinline ArgArray<T>::ArgArray(void){} template<classT> forceinline ArgArray<T>::ArgArray(intn):ArgArrayBase<T>(n){} template<classT> ArgArray<T>::ArgArray(intn,constT*a0) :ArgArrayBase<T>(n){ for(inti=n;i--;) a[i]=a0[i]; } template<classT> forceinline ArgArray<T>::ArgArray(constArgArray<T>&aa) :ArgArrayBase<T>(aa){} template<classT> forceinline ArgArray<T>::ArgArray(conststd::vector<T>&aa) :ArgArrayBase<T>(aa){} template<classT> template<classInputIterator> forceinline ArgArray<T>::ArgArray(InputIteratorfirst,InputIteratorlast) :ArgArrayBase<T>(first,last){} template<classT> forceinlinetypenameArrayTraits<ArgArray<T>>::ArgsType ArgArray<T>::slice(intstart,intinc,intmaxN){ returnArgArrayBase<T>::templateslice <typenameArrayTraits<ArgArray<T>>::ArgsType> (start,inc,maxN); } template<classT> forceinlinetypenameArrayTraits<ArgArray<T>>::ArgsType& ArgArray<T>::operator <<(constT&x){ return ArgArrayBase<T>::templateappend <typenameArrayTraits<ArgArray<T>>::ArgsType>(x); } template<classT> forceinlinetypenameArrayTraits<ArgArray<T>>::ArgsType& ArgArray<T>::operator <<(constArgArray<T>&x){ return ArgArrayBase<T>::templateappend <typenameArrayTraits<ArgArray<T>>::ArgsType>(x); } template<classT> typenameArrayTraits<ArgArray<T>>::ArgsType operator+(constArgArray<T>&x,constArgArray<T>&y){ returnx.templateconcat <typenameArrayTraits<ArgArray<T>>::ArgsType>(y); } template<classT> typenameArrayTraits<ArgArray<T>>::ArgsType operator+(constArgArray<T>&x,constT&y){ returnx.templateconcat <typenameArrayTraits<ArgArray<T>>::ArgsType>(y); } template<classT> typenameArrayTraits<ArgArray<T>>::ArgsType operator+(constT&x,constArgArray<T>&y){ ArgArray<T>xa(1); xa[0]=x; returnxa.templateconcat <typenameArrayTraits<ArgArray<T>>::ArgsType>(y); } /* *Argumentarraysforvariables * */ template<classVar> forceinline VarArgArray<Var>::VarArgArray(void){} template<classVar> forceinline VarArgArray<Var>::VarArgArray(intn):ArgArrayBase<Var>(n){} template<classVar> forceinline VarArgArray<Var>::VarArgArray(constVarArgArray<Var>&aa) :ArgArrayBase<Var>(aa){} template<classVar> forceinline VarArgArray<Var>::VarArgArray(conststd::vector<Var>&aa) :ArgArrayBase<Var>(aa){} template<classVar> template<classInputIterator> forceinline VarArgArray<Var>::VarArgArray(InputIteratorfirst,InputIteratorlast) :ArgArrayBase<Var>(first,last){} template<classVar> inline VarArgArray<Var>::VarArgArray(constVarArray<Var>&x) :ArgArrayBase<Var>(x.size()){ for(inti=x.size();i--;) a[i]=x[i]; } template<classVar> forceinlinetypenameArrayTraits<VarArgArray<Var>>::ArgsType VarArgArray<Var>::slice(intstart,intinc,intmaxN){ returnArgArrayBase<Var>::templateslice <typenameArrayTraits<VarArgArray<Var>>::ArgsType> (start,inc,maxN); } template<classVar> forceinlinetypenameArrayTraits<VarArgArray<Var>>::ArgsType& VarArgArray<Var>::operator <<(constVar&x){ return ArgArrayBase<Var>::templateappend <typenameArrayTraits<VarArgArray<Var>>::ArgsType>(x); } template<classVar> forceinlinetypenameArrayTraits<VarArgArray<Var>>::ArgsType& VarArgArray<Var>::operator <<(constVarArgArray<Var>&x){ return ArgArrayBase<Var>::templateappend <typenameArrayTraits<VarArgArray<Var>>::ArgsType>(x); } template<classVar> typenameArrayTraits<VarArgArray<Var>>::ArgsType operator+(constVarArgArray<Var>&x,constVarArgArray<Var>&y){ returnx.templateconcat <typenameArrayTraits<VarArgArray<Var>>::ArgsType>(y); } template<classVar> typenameArrayTraits<VarArgArray<Var>>::ArgsType operator+(constVarArgArray<Var>&x,constVar&y){ returnx.templateconcat <typenameArrayTraits<VarArgArray<Var>>::ArgsType>(y); } template<classVar> typenameArrayTraits<VarArgArray<Var>>::ArgsType operator+(constVar&x,constVarArgArray<Var>&y){ VarArgArray<Var>xa(1); xa[0]=x; returnxa.templateconcat <typenameArrayTraits<VarArgArray<Var>>::ArgsType>(y); } template<classVar> forceinlinebool VarArgArray<Var>::VarLess::operator ()(constVar&a,constVar&b){ returna.varimp()<b.varimp(); } template<classVar> forceinlinebool VarArgArray<Var>::assigned(void)const{ for(inti=n;i--;) if(!a[i].assigned()) returnfalse; returntrue; } template<classVar> bool VarArgArray<Var>::same(constSpace&home)const{ if(n<2) returnfalse; Regionr(home); Var*y=r.alloc<Var>(n); for(inti=n;i--;) y[i]=a[i]; VarLessvl; Support::quicksort<Var,VarLess>(y,n,vl); for(inti=n-1;i--;) if(!y[i].assigned()&&(y[i+1].varimp()==y[i].varimp())){ r.free<Var>(y,n); returntrue; } r.free<Var>(y,n); returnfalse; } template<classVar> bool VarArgArray<Var>::same(constSpace&home,constVarArgArray<Var>&y)const{ intm=n+y.n; if(m<2) returnfalse; Regionr(home); Var*z=r.alloc<Var>(m); for(inti=n;i--;) z[i]=a[i]; for(inti=y.n;i--;) z[i+n]=y.a[i]; VarLessvl; Support::quicksort<Var,VarLess>(z,m,vl); for(inti=m-1;i--;) if(!z[i].assigned()&&(z[i+1].varimp()==z[i].varimp())){ r.free<Var>(z,m); returntrue; } r.free<Var>(z,m); returnfalse; } template<classVar> bool VarArgArray<Var>::same(constSpace&,constVar&y)const{ if(y.assigned()) returnfalse; for(inti=n;i--;) if(a[i].varimp()==y.varimp()) returntrue; returnfalse; } /* *Interdependentcode * */ template<classVar> inline VarArray<Var>::VarArray(Space&home,constVarArgArray<Var>&a) :n(a.size()){ if(n>0){ x=home.alloc<Var>(n); for(inti=n;i--;) x[i]=a[i]; }else{ x=NULL; } } /* *Printingofarrays * */ template<classChar,classTraits,classVar> std::basic_ostream<Char,Traits>& operator<<(std::basic_ostream<Char,Traits>&os, constVarArray<Var>&x){ std::basic_ostringstream<Char,Traits>s; s.copyfmt(os);s.width(0); s<<'{'; if(x.size()>0){ s<<x[0]; for(inti=1;i<x.size();i++) s<<","<<x[i]; } s<<'}'; returnos<<s.str(); } template<classChar,classTraits,classView> std::basic_ostream<Char,Traits>& operator<<(std::basic_ostream<Char,Traits>&os, constViewArray<View>&x){ std::basic_ostringstream<Char,Traits>s; s.copyfmt(os);s.width(0); s<<'{'; if(x.size()>0){ s<<x[0]; for(inti=1;i<x.size();i++) s<<","<<x[i]; } s<<'}'; returnos<<s.str(); } template<classChar,classTraits,classT> std::basic_ostream<Char,Traits>& operator<<(std::basic_ostream<Char,Traits>&os, constArgArrayBase<T>&x){ std::basic_ostringstream<Char,Traits>s; s.copyfmt(os);s.width(0); s<<'{'; if(x.size()>0){ s<<x[0]; for(inti=1;i<x.size();i++) s<<","<<x[i]; } s<<'}'; returnos<<s.str(); } } //STATISTICS:kernel-other