|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
//<2F>ļ<EFBFBD> ObjectSeg.h
|
|
|
|
|
|
|
|
|
|
|
|
/
|
|
|
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
|
|
|
|
#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 T>
|
|
|
|
|
|
class TObjectSet : public CVectorSet
|
|
|
|
|
|
{
|
|
|
|
|
|
public:
|
|
|
|
|
|
|
|
|
|
|
|
inline TObjectSet();
|
|
|
|
|
|
inline TObjectSet(const TObjectSet<T>&);
|
|
|
|
|
|
inline virtual ~TObjectSet() { deepErase(); }
|
|
|
|
|
|
|
|
|
|
|
|
inline bool nullAllowed() const { return allow0_; }
|
|
|
|
|
|
inline void allowNull(bool yn=true);
|
|
|
|
|
|
|
|
|
|
|
|
inline virtual int size() const { return vec_.size(); }
|
|
|
|
|
|
inline virtual int GetCount() 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* replace(int idx,T*);
|
|
|
|
|
|
inline virtual void insertAt(T* newptr, int);
|
|
|
|
|
|
inline virtual void insertAfter(T* newptr, int);
|
|
|
|
|
|
inline virtual void copy(const TObjectSet<T>&);
|
|
|
|
|
|
inline virtual void append(const TObjectSet<T>&);
|
|
|
|
|
|
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(); //<2F><><EFBFBD>ȿռ<C8BF><D5BC>ͷţ<CDB7><C5A3><EFBFBD><EFBFBD><EFBFBD>ָ<EFBFBD><D6B8>ָ<EFBFBD><D6B8><EFBFBD>Ŀռ<C4BF>
|
|
|
|
|
|
inline virtual void erase() { vec_.erase(); } //<2F>ռ<F2B5A5BF><D5BC>ͷţ<CDB7><C5A3><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͷ<EFBFBD>ָ<EFBFBD><D6B8>ָ<EFBFBD><D6B8><EFBFBD>Ŀռ<C4BF>
|
|
|
|
|
|
virtual inline T* remove(int,bool preserve_order=true); /*!<\returns the removed pointer. */
|
|
|
|
|
|
inline virtual void remove(int from,int to);
|
|
|
|
|
|
inline void setCapacity(int sz) {vec_.setCapacity(sz); }//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ԥ<EFBFBD><D4A4><EFBFBD>ռ<EFBFBD> added wcw 2014-9-2
|
|
|
|
|
|
inline void AppendSetCapacity(int sz) {setCapacity(size()+sz); }//<2F><>չ<EFBFBD><D5B9><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ԥ<EFBFBD><D4A4><EFBFBD>ռ<EFBFBD> added zgq 2016-5-12
|
|
|
|
|
|
|
|
|
|
|
|
inline virtual T* operator[](const T*) const; //!< check & unconst
|
|
|
|
|
|
inline TObjectSet<T>& operator =(const TObjectSet<T>&);
|
|
|
|
|
|
inline virtual TObjectSet<T>& operator +=(T*);
|
|
|
|
|
|
inline virtual TObjectSet<T>& operator -=(T*);
|
|
|
|
|
|
|
|
|
|
|
|
inline std::vector<T*>& vec();
|
|
|
|
|
|
inline const std::vector<T*>& vec() const;
|
|
|
|
|
|
|
|
|
|
|
|
inline void sort(bool ( * _PtFuncCompare)(const T *, const T *));
|
|
|
|
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
|
TVectorAccess<T*> vec_;
|
|
|
|
|
|
bool allow0_;
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
//! empty the TObjectSet deleting all objects pointed to.
|
|
|
|
|
|
template <class T>
|
|
|
|
|
|
inline void deepErase( TObjectSet<T>& os )
|
|
|
|
|
|
{
|
|
|
|
|
|
for ( int sz=os.size(), idx=0; idx<sz; idx++ )
|
|
|
|
|
|
delete os[idx];
|
|
|
|
|
|
os.erase();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//! empty the TObjectSet deleting all objects pointed to.
|
|
|
|
|
|
template <class T>
|
|
|
|
|
|
inline void deepEraseArr( TObjectSet<T>& os )
|
|
|
|
|
|
{
|
|
|
|
|
|
for ( int sz=os.size(), idx=0; idx<sz; idx++ )
|
|
|
|
|
|
delete [] os[idx];
|
|
|
|
|
|
os.erase();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//! append copies of one set's objects to another TObjectSet.
|
|
|
|
|
|
template <class T,class S>
|
|
|
|
|
|
inline void deepAppend( TObjectSet<T>& to, const TObjectSet<S>& from )
|
|
|
|
|
|
{
|
|
|
|
|
|
const int sz = from.size();
|
|
|
|
|
|
for ( int idx=0; idx<sz; idx++ )
|
|
|
|
|
|
to += from[idx] ? new T( *from[idx] ) : 0;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//! fill an TObjectSet with copies of the objects in the other set.
|
|
|
|
|
|
template <class T,class S>
|
|
|
|
|
|
inline void deepCopy( TObjectSet<T>& to, const TObjectSet<S>& from )
|
|
|
|
|
|
{
|
|
|
|
|
|
if ( &to == &from ) return;
|
|
|
|
|
|
deepErase( to );
|
|
|
|
|
|
to.allowNull( from.nullAllowed() );
|
|
|
|
|
|
deepAppend( to, from );
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//! Locate object in set
|
|
|
|
|
|
template <class T,class S>
|
|
|
|
|
|
inline int indexOf( const TObjectSet<T>& os, const S& val )
|
|
|
|
|
|
{
|
|
|
|
|
|
for ( int idx=0; idx<os.size(); idx++ )
|
|
|
|
|
|
{
|
|
|
|
|
|
if ( *os[idx] == val )
|
|
|
|
|
|
return idx;
|
|
|
|
|
|
}
|
|
|
|
|
|
return -1;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//! Get const object in set
|
|
|
|
|
|
template <class T,class S>
|
|
|
|
|
|
inline const T* find( const TObjectSet<T>& os, const S& val )
|
|
|
|
|
|
{
|
|
|
|
|
|
const int idx = indexOf( os, val );
|
|
|
|
|
|
return idx == -1 ? 0 : os[idx];
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//! Get object in set
|
|
|
|
|
|
template <class T,class S>
|
|
|
|
|
|
inline T* find( TObjectSet<T>& 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 <class T>
|
|
|
|
|
|
inline void sort( TObjectSet<T>& os )
|
|
|
|
|
|
{
|
|
|
|
|
|
T* tmp; const int sz = os.size();
|
|
|
|
|
|
for ( int d=sz/2; d>0; d=d/2 )
|
|
|
|
|
|
for ( int i=d; i<sz; i++ )
|
|
|
|
|
|
for ( int j=i-d; j>=0 && *os[j]>*os[j+d]; j-=d )
|
|
|
|
|
|
os.swap( j, j+d );
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Member function implementations
|
|
|
|
|
|
template <class T> inline
|
|
|
|
|
|
TObjectSet<T>::TObjectSet() : allow0_(false)
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
template <class T> inline
|
|
|
|
|
|
TObjectSet<T>::TObjectSet( const TObjectSet<T>& t )
|
|
|
|
|
|
{
|
|
|
|
|
|
*this = t;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
template <class T> inline
|
|
|
|
|
|
TObjectSet<T>& TObjectSet<T>::operator =( const TObjectSet<T>& os )
|
|
|
|
|
|
{
|
|
|
|
|
|
allow0_ = os.allow0_;
|
|
|
|
|
|
copy(os);
|
|
|
|
|
|
return *this;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
template <class T> inline
|
|
|
|
|
|
void TObjectSet<T>::copy( const TObjectSet<T>& os )
|
|
|
|
|
|
{
|
|
|
|
|
|
if ( &os != this )
|
|
|
|
|
|
{
|
|
|
|
|
|
//::deepCopy(*this, os);
|
|
|
|
|
|
erase();
|
|
|
|
|
|
allow0_ = os.allow0_;
|
|
|
|
|
|
append( os );
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
template <class T> inline
|
|
|
|
|
|
void TObjectSet<T>::append( const TObjectSet<T>& os )
|
|
|
|
|
|
{
|
|
|
|
|
|
const int sz = os.size();
|
|
|
|
|
|
vec_.setCapacity( size()+sz );
|
|
|
|
|
|
for ( int idx=0; idx<sz; idx++ )
|
|
|
|
|
|
*this += const_cast<T*>( os[idx] );
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template <class T> inline
|
|
|
|
|
|
void TObjectSet<T>::deepErase()
|
|
|
|
|
|
{
|
|
|
|
|
|
::deepErase( *this );
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template <class T>
|
|
|
|
|
|
inline void TObjectSet<T>::sort(bool ( * _PtFuncCompare)(const T *, const T *))
|
|
|
|
|
|
{
|
|
|
|
|
|
std::stable_sort(vec().begin(), vec().end(), _PtFuncCompare);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template <class T> inline
|
|
|
|
|
|
void TObjectSet<T>::allowNull( bool yn )
|
|
|
|
|
|
{
|
|
|
|
|
|
allow0_ = yn;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
template <class T> inline
|
|
|
|
|
|
bool TObjectSet<T>::IsValidIndex( int idx ) const
|
|
|
|
|
|
{
|
|
|
|
|
|
return idx>=0 && idx<size();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
template <class T> inline
|
|
|
|
|
|
T* TObjectSet<T>::operator[]( const T* t ) const
|
|
|
|
|
|
{
|
|
|
|
|
|
const int idx = indexOf(t);
|
|
|
|
|
|
return idx < 0 ? 0 : const_cast<T*>(t);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
template <class T> inline
|
|
|
|
|
|
int TObjectSet<T>::indexOf( const T* ptr ) const
|
|
|
|
|
|
{
|
|
|
|
|
|
for ( int idx=0; idx<size(); idx++ )
|
|
|
|
|
|
if ( (const T*)vec_[idx] == ptr ) return idx;
|
|
|
|
|
|
return -1;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
template <class T> inline
|
|
|
|
|
|
TObjectSet<T>& TObjectSet<T>::operator +=( T* ptr )
|
|
|
|
|
|
{
|
|
|
|
|
|
if ( ptr || allow0_ )
|
|
|
|
|
|
vec_.push_back( ptr );
|
|
|
|
|
|
return *this;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
template <class T> inline
|
|
|
|
|
|
TObjectSet<T>& TObjectSet<T>::operator -=( T* ptr )
|
|
|
|
|
|
{
|
|
|
|
|
|
if ( ptr || allow0_ )
|
|
|
|
|
|
vec_.erase( ptr );
|
|
|
|
|
|
return *this;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
template <class T> inline
|
|
|
|
|
|
void TObjectSet<T>::swap( int idx0, int idx1 )
|
|
|
|
|
|
{
|
|
|
|
|
|
if ( idx0<0 || idx0>=size() || idx1<0 || idx1>=size() )
|
|
|
|
|
|
return;
|
|
|
|
|
|
T* tmp = vec_[idx0];
|
|
|
|
|
|
vec_[idx0] = vec_[idx1];
|
|
|
|
|
|
vec_[idx1] = tmp;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
template <class T> inline
|
|
|
|
|
|
T* TObjectSet<T>::replace( int idx, T* newptr )
|
|
|
|
|
|
{
|
|
|
|
|
|
if ( idx<0 || idx>=size() ) return 0;
|
|
|
|
|
|
T* ptr = (T*)vec_[idx];
|
|
|
|
|
|
vec_[idx] = newptr; return ptr;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
template <class T> inline
|
|
|
|
|
|
void TObjectSet<T>::insertAt( T* newptr, int idx )
|
|
|
|
|
|
{
|
|
|
|
|
|
vec_.insert( idx, newptr );
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
template <class T> inline
|
|
|
|
|
|
void TObjectSet<T>::insertAfter( T* newptr, int idx )
|
|
|
|
|
|
{
|
|
|
|
|
|
*this += newptr;
|
|
|
|
|
|
if ( idx < 0 )
|
|
|
|
|
|
vec_.moveToStart( newptr );
|
|
|
|
|
|
else
|
|
|
|
|
|
vec_.moveAfter( newptr, vec_[idx] );
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
template <class T> inline
|
|
|
|
|
|
void TObjectSet<T>::push( T* ptr )
|
|
|
|
|
|
{
|
|
|
|
|
|
if ( ptr || allow0_ )
|
|
|
|
|
|
vec_.push_back( ptr );
|
|
|
|
|
|
return ;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
template <class T> inline
|
|
|
|
|
|
T* TObjectSet<T>::pop()
|
|
|
|
|
|
{
|
|
|
|
|
|
int sz = size();
|
|
|
|
|
|
if ( !sz ) return 0;
|
|
|
|
|
|
return remove( sz-1 );
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
template <class T> inline
|
|
|
|
|
|
T* TObjectSet<T>::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 <class T> inline
|
|
|
|
|
|
void TObjectSet<T>::remove( int i1, int i2 )
|
|
|
|
|
|
{ vec_.remove( i1, i2 ); }
|
|
|
|
|
|
|
|
|
|
|
|
template <class T> inline
|
|
|
|
|
|
std::vector<T*>& TObjectSet<T>::vec()
|
|
|
|
|
|
{ return vec_.vec(); }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
template <class T> inline
|
|
|
|
|
|
const std::vector<T*>& TObjectSet<T>::vec() const
|
|
|
|
|
|
{ return vec_.vec(); }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}//namesapce
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#endif
|