////////////////////////////////////////////////////////////////////////////// //文件 ObjectSeg.h //主要功能: // 模板数组基础类 //程序编写: 2011-7-07 ///////////////////////////////////////////////////////////////////////////// #ifndef objectset_h #define objectset_h #include "VectorSet.h" #include "vectorAccess.h" /*!\brief Set of pointers to objects The TObjectSet does not manage the objects. */ namespace NSet { template class TObjectSet : public CVectorSet { public: inline TObjectSet(); inline TObjectSet(const TObjectSet&); inline virtual ~TObjectSet() { deepErase(); } virtual bool isManaged() const { return false; } inline bool nullAllowed() const { return allow0_; } inline void allowNull(bool yn=true); inline virtual int size() const { return vec_.size(); } inline virtual bool IsValidIndex(int) const; inline virtual int indexOf(const T*) const; inline virtual T* operator[](int idx) const { /*if ( !IsValidIndex(idx) )*/ return (T*)vec_[idx]; } //inline virtual const T* operator[](int idx) const { /*if ( !IsValidIndex(idx) )*/ return (const T*)vec_[idx]; } inline virtual T* GetAt(int idx) const { return (T*)vec_[idx]; } inline virtual T* operator[](const T*) const; //!< check & unconst inline TObjectSet& operator =(const TObjectSet&); inline virtual TObjectSet& operator +=(T*); inline virtual TObjectSet& operator -=(T*); inline virtual T* replace(int idx,T*); inline virtual void insertAt(T* newptr, int); inline virtual void insertAfter(T* newptr, int); inline virtual void copy(const TObjectSet&); inline virtual void append(const TObjectSet&); inline virtual void swap( int idx0, int idx1 ); inline virtual void Add(T* ptr) { push(ptr); } inline virtual void push(T* ptr); inline virtual T* pop(); inline void deepErase(); //深度空间释放,包括指针指向的空间 inline virtual void erase() { plainErase(); } //简单空间释放,并不释放指针指向的空间 virtual inline T* remove(int,bool preserve_order=true); /*!<\returns the removed pointer. */ inline virtual void remove(int from,int to); protected: TVectorAccess vec_; bool allow0_; public: inline void plainErase() { vec_.erase(); } /*!< Not virtual. Don't use casually. */ }; //! empty the TObjectSet deleting all objects pointed to. template inline void deepErase( TObjectSet& os ) { for ( int sz=os.size(), idx=0; idx inline void deepEraseArr( TObjectSet& os ) { for ( int sz=os.size(), idx=0; idx inline void deepAppend( TObjectSet& to, const TObjectSet& from ) { const int sz = from.size(); for ( int idx=0; idx inline void deepCopy( TObjectSet& to, const TObjectSet& from ) { if ( &to == &from ) return; deepErase( to ); to.allowNull( from.nullAllowed() ); deepAppend( to, from ); } //! Locate object in set template inline int indexOf( const TObjectSet& os, const S& val ) { for ( int idx=0; idx inline const T* find( const TObjectSet& os, const S& val ) { const int idx = indexOf( os, val ); return idx == -1 ? 0 : os[idx]; } //! Get object in set template inline T* find( TObjectSet& os, const S& val ) { const int idx = indexOf( os, val ); return idx == -1 ? 0 : os[idx]; } //! Sort TObjectSet. Must have operator > defined for elements template inline void sort( TObjectSet& os ) { T* tmp; const int sz = os.size(); for ( int d=sz/2; d>0; d=d/2 ) for ( int i=d; i=0 && *os[j]>*os[j+d]; j-=d ) os.swap( j, j+d ); } // Member function implementations template inline TObjectSet::TObjectSet() : allow0_(false) { } template inline TObjectSet::TObjectSet( const TObjectSet& t ) { *this = t; } template inline TObjectSet& TObjectSet::operator =( const TObjectSet& os ) { allow0_ = os.allow0_; copy(os); return *this; } template inline void TObjectSet::deepErase() { ::deepErase( *this ); } template inline void TObjectSet::allowNull( bool yn ) { allow0_ = yn; } template inline bool TObjectSet::IsValidIndex( int idx ) const { return idx>=0 && idx inline T* TObjectSet::operator[]( const T* t ) const { const int idx = indexOf(t); return idx < 0 ? 0 : const_cast(t); } template inline int TObjectSet::indexOf( const T* ptr ) const { for ( int idx=0; idx inline TObjectSet& TObjectSet::operator +=( T* ptr ) { if ( ptr || allow0_ ) vec_.push_back( (void*)ptr ); return *this; } template inline TObjectSet& TObjectSet::operator -=( T* ptr ) { if ( ptr || allow0_ ) vec_.erase( (void*)ptr ); return *this; } template inline void TObjectSet::swap( int idx0, int idx1 ) { if ( idx0<0 || idx0>=size() || idx1<0 || idx1>=size() ) return; void* tmp = vec_[idx0]; vec_[idx0] = vec_[idx1]; vec_[idx1] = tmp; } template inline T* TObjectSet::replace( int idx, T* newptr ) { if ( idx<0 || idx>=size() ) return 0; T* ptr = (T*)vec_[idx]; vec_[idx] = (void*)newptr; return ptr; } template inline void TObjectSet::insertAt( T* newptr, int idx ) { vec_.insert( idx, (void*)newptr ); } template inline void TObjectSet::insertAfter( T* newptr, int idx ) { *this += newptr; if ( idx < 0 ) vec_.moveToStart( (void*)newptr ); else vec_.moveAfter( (void*)newptr, vec_[idx] ); } template inline void TObjectSet::copy( const TObjectSet& os ) { if ( &os != this ) { erase(); allow0_ = os.allow0_; append( os ); } } template inline void TObjectSet::append( const TObjectSet& os ) { const int sz = os.size(); vec_.setCapacity( size()+sz ); for ( int idx=0; idx( os[idx] ); } template inline void TObjectSet::push( T* ptr ) { *this +=ptr; } template inline T* TObjectSet::pop() { int sz = size(); if ( !sz ) return 0; return remove( sz-1 ); } template inline T* TObjectSet::remove( int idx, bool kporder ) { T* res = (T*)vec_[idx]; if ( kporder ) vec_.remove( idx ); else { const int lastidx = size()-1; if ( idx!=lastidx ) vec_[idx] = vec_[lastidx]; vec_.remove( lastidx ); } return res; } template inline void TObjectSet::remove( int i1, int i2 ) { vec_.remove( i1, i2 ); } }//namesapce #endif