////////////////////////////////////////////////////////////////////////////// //文件 VectorAccess.h //主要功能: // 模板数组基础类 //程序编写: 2011-7-07 ///////////////////////////////////////////////////////////////////////////// #ifndef vectoraccess_h #define vectoraccess_h #include /*!\brief Simple vector-based container simplifying index-based work. This class is an implementation detail of the 'vectorSets.h' and 'sortedlist.h' classes. Thus, this class is not meant to be used anywhere else! Use TTypeSet, TObjectSet or TSortedList instead. If you need to have the std::vector to pass to an external C++ object, use the TTypeSet::vec(). NOTE: because this class is based directly upon the STL vector, we have a problem for the bool type. In STL, they have made the vector implemented in terms of the bit_vector. That really sucks because we cannot return a reference to T! This is why there is a 'TBoolTypeSet'. */ namespace NSet { template class TVectorAccess { public: inline TVectorAccess() {} inline TVectorAccess( unsigned int n ) : v(n) {} inline TVectorAccess( unsigned int n, const T& t ): v(n,t) {} inline TVectorAccess( const TVectorAccess& v2 ): v(v2.v) {} inline std::vector& vec() { return v; } inline const std::vector& vec() const { return v; } inline T& operator[]( int idx ) { return v[idx]; } inline const T& operator[]( int idx ) const { return (*const_cast(this))[idx]; } inline unsigned int size() const { return (unsigned int) v.size(); } inline bool setCapacity( int sz ); /*!=0 && idx i2 ) std::swap( i1, i2 ); const unsigned int sz = size(); if ( i1 >= sz ) return; if ( i2 >= sz-1 ) i2 = sz-1; v.erase( v.begin()+i1, v.begin()+i2+1 ); } inline void swap( unsigned int i, unsigned int j ) { std::swap( v[i], v[j] ); } inline void fillWith( const T& val ) { const int sz = size(); T* arr = sz ? &v[0] : 0; for ( int i=sz-1; i>=0; i--,arr++ ) *arr = val; } void moveAfter( const T& t, const T& aft ) { if ( t == aft || size() < 2 ) return; int tidx = -1; int aftidx = -1; for ( int idx=size()-1; idx!=-1; idx-- ) { if ( v[idx] == t ) { tidx = idx; if ( aftidx != -1 ) break; } if ( v[idx] == aft ) { aftidx = idx; if ( tidx != -1 ) break; } } if ( tidx == -1 || aftidx == -1 || tidx == aftidx ) return; if ( aftidx > tidx ) for ( int idx=tidx; idxaftidx+1; idx-- ) swap( idx, idx-1 ); } void moveToStart( const T& t ) { if ( size() < 2 ) return; int tidx = -1; for ( int idx=size()-1; idx!=-1; idx-- ) if ( v[idx] == t ) { tidx = idx; break; } for ( int idx=tidx; idx>0; idx-- ) swap( idx, idx-1 ); } protected: std::vector v; }; template inline bool TVectorAccess::setCapacity( int sz ) { try { v.reserve(sz); } catch ( std::bad_alloc ) { return false; } catch ( std::length_error ) { return false; } return true; } template inline bool TVectorAccess::push_back( const T& t ) { try { v.push_back(t); } catch ( std::bad_alloc ) { return false; } return true; } template inline bool TVectorAccess::setSize( int sz, T val ) { try { v.resize(sz,val); } catch ( std::bad_alloc ) { return false; } return true; } template inline bool TVectorAccess::setSize( int sz ) { try { v.resize(sz); } catch ( std::bad_alloc ) { return false; } return true; } } //namespace NSet using namespace NSet; #endif