shared-array.hpp cstdarg iostream sstream /usr/include/gecode/kernel.hh Gecode::SharedArray Gecode::SharedArray::SAO 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-1306:01:49+0100(Wed,13Mar2013)$by$Author:tack$ *$Revision:13521$ * *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<sstream> namespaceGecode{ template<classT> classSharedArray:publicSharedHandle{ protected: classSAO:publicSharedHandle::Object{ private: T*a; intn; public: SAO(intn); virtualSharedHandle::Object*copy(void)const; virtual~SAO(void); T&operator [](inti); constT&operator [](inti)const; intsize(void)const; T*begin(void); constT*begin(void)const; T*end(void); constT*end(void)const; }; 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; SharedArray(void); SharedArray(intn); voidinit(intn); SharedArray(constSharedArray&a); SharedArray(constArgArrayBase<T>&a); T&operator [](inti); constT&operator [](inti)const; intsize(void)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; }; template<classChar,classTraits,classT> std::basic_ostream<Char,Traits>& operator<<(std::basic_ostream<Char,Traits>&os, constSharedArray<T>&x); /* *Implementation * */ /* *Sharedarrays * */ template<classT> forceinline SharedArray<T>::SAO::SAO(intn0):n(n0){ a=(n>0)?heap.alloc<T>(n):NULL; } template<classT> SharedHandle::Object* SharedArray<T>::SAO::copy(void)const{ SAO*o=newSAO(n); for(inti=n;i--;) o->a[i]=a[i]; returno; } template<classT> SharedArray<T>::SAO::~SAO(void){ if(n>0){ heap.free<T>(a,n); } } template<classT> forceinlineT& SharedArray<T>::SAO::operator [](inti){ assert((i>=0)&&(i<n)); returna[i]; } template<classT> forceinlineconstT& SharedArray<T>::SAO::operator [](inti)const{ assert((i>=0)&&(i<n)); returna[i]; } template<classT> forceinlineint SharedArray<T>::SAO::size(void)const{ returnn; } template<classT> forceinlineT* SharedArray<T>::SAO::begin(void){ returna; } template<classT> forceinlineconstT* SharedArray<T>::SAO::begin(void)const{ returna; } template<classT> forceinlineT* SharedArray<T>::SAO::end(void){ returna+n; } template<classT> forceinlineconstT* SharedArray<T>::SAO::end(void)const{ returna+n; } template<classT> forceinline SharedArray<T>::SharedArray(void){} template<classT> forceinline SharedArray<T>::SharedArray(intn) :SharedHandle(newSAO(n)){} template<classT> forceinline SharedArray<T>::SharedArray(constSharedArray<T>&sa) :SharedHandle(sa){} template<classT> forceinlinevoid SharedArray<T>::init(intn){ assert(object()==NULL); object(newSAO(n)); } template<classT> forceinlineT& SharedArray<T>::operator [](inti){ assert(object()!=NULL); return(*static_cast<SAO*>(object()))[i]; } template<classT> forceinlineconstT& SharedArray<T>::operator [](inti)const{ assert(object()!=NULL); return(*static_cast<SAO*>(object()))[i]; } template<classT> forceinline SharedArray<T>::SharedArray(constArgArrayBase<T>&a) :SharedHandle(newSAO(a.size())){ for(inti=a.size();i--;) operator[](i)=a[i]; } template<classT> forceinlineint SharedArray<T>::size(void)const{ assert(object()!=NULL); returnstatic_cast<SAO*>(object())->size(); } template<classT> forceinlinetypenameSharedArray<T>::iterator SharedArray<T>::begin(void){ assert(object()!=NULL); returnstatic_cast<SAO*>(object())->begin(); } template<classT> forceinlinetypenameSharedArray<T>::const_iterator SharedArray<T>::begin(void)const{ assert(object()!=NULL); returnstatic_cast<SAO*>(object())->begin(); } template<classT> forceinlinetypenameSharedArray<T>::iterator SharedArray<T>::end(void){ assert(object()!=NULL); returnstatic_cast<SAO*>(object())->end(); } template<classT> forceinlinetypenameSharedArray<T>::const_iterator SharedArray<T>::end(void)const{ assert(object()!=NULL); returnstatic_cast<SAO*>(object())->end(); } template<classT> forceinlinetypenameSharedArray<T>::reverse_iterator SharedArray<T>::rbegin(void){ assert(object()!=NULL); returnreverse_iterator(static_cast<SAO*>(object())->end()); } template<classT> forceinlinetypenameSharedArray<T>::const_reverse_iterator SharedArray<T>::rbegin(void)const{ assert(object()!=NULL); returnconst_reverse_iterator(static_cast<SAO*>(object())->end()); } template<classT> forceinlinetypenameSharedArray<T>::reverse_iterator SharedArray<T>::rend(void){ assert(object()!=NULL); returnreverse_iterator(static_cast<SAO*>(object())->begin()); } template<classT> forceinlinetypenameSharedArray<T>::const_reverse_iterator SharedArray<T>::rend(void)const{ assert(object()!=NULL); returnconst_reverse_iterator(static_cast<SAO*>(object())->begin()); } template<classChar,classTraits,classT> std::basic_ostream<Char,Traits>& operator<<(std::basic_ostream<Char,Traits>&os, constSharedArray<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