"L. Spiro Engine"
|
00001 00019 #ifndef __LSTL_SET_H__ 00020 #define __LSTL_SET_H__ 00021 00022 #include "../LSTLib.h" 00023 #include "../Algorithm/LSTLAlgorithm.h" 00024 #include "../Vector/LSTLVector.h" 00025 #include "LSTLSetBase.h" 00026 00027 #pragma warning( push ) 00028 00029 // warning C4127: conditional expression is constant 00030 #pragma warning( disable : 4127 ) 00031 00032 namespace lstl { 00033 00043 template <typename _tKeyType> 00044 class CSet : protected CSetBase { 00045 public : 00046 // == Various constructors. 00047 LSE_CALL CSet() { 00048 } 00049 explicit LSE_CALL CSet( const CSet<_tKeyType> &_sSrc ) : 00050 m_vKeys( _sSrc.m_vKeys ) { 00051 } 00052 explicit LSE_CALL CSet( CAllocator * _paAllocator ) : 00053 m_vKeys( _paAllocator ) { 00054 } 00055 LSE_CALL ~CSet() { 00056 } 00057 00058 00059 // == Operators. 00067 CSet<_tKeyType> & LSE_CALL operator = ( const CSet<_tKeyType> &_sSrc ) { 00068 // The copy is as easy as copying the vector. 00069 m_vKeys = _sSrc.m_vKeys; 00070 return (*this); 00071 } 00072 00073 00074 // == Functions. 00082 LSBOOL LSE_CALL AllocateAtLeast( LSUINT32 _ui32Size ) { 00083 if ( m_vKeys.GetAllocated() >= _ui32Size ) { return true; } 00084 return m_vKeys.Allocate( _ui32Size ); 00085 } 00086 00095 LSBOOL LSE_CALL Insert( const _tKeyType * _ptKeys, LSUINT32 _ui32Total ) { 00096 for ( LSUINT32 I = _ui32Total; I--; ) { 00097 if ( !Insert( _ptKeys[I] ) ) { return false; } 00098 } 00099 return true; 00100 } 00101 00109 LSBOOL LSE_CALL Insert( const _tKeyType &_tKey ) { 00110 LSUINT32 ui32Index; 00111 if ( !GetItemIndex( _tKey, ui32Index ) ) { 00112 // Insert the item at the given index. 00113 if ( !m_vKeys.Insert( _tKey, ui32Index ) ) { 00114 return false; 00115 } 00116 } 00117 return true; 00118 } 00119 00126 LSVOID LSE_CALL RemoveNoDealloc( const _tKeyType * _ptKeys, LSUINT32 _ui32Total ) { 00127 for ( LSUINT32 I = _ui32Total; I--; ) { 00128 RemoveNoDealloc( _ptKeys[I] ); 00129 } 00130 } 00131 00137 LSVOID LSE_CALL RemoveNoDealloc( const _tKeyType &_tKey ) { 00138 LSUINT32 ui32Index; 00139 if ( GetItemIndex( _tKey, ui32Index ) ) { 00140 m_vKeys.RemoveNoDealloc( ui32Index ); 00141 } 00142 } 00143 00150 LSVOID LSE_CALL Remove( const _tKeyType * _ptKeys, LSUINT32 _ui32Total ) { 00151 for ( LSUINT32 I = _ui32Total; I--; ) { 00152 Remove( _ptKeys[I] ); 00153 } 00154 } 00155 00161 LSVOID LSE_CALL Remove( const _tKeyType &_tKey ) { 00162 LSUINT32 ui32Index; 00163 if ( GetItemIndex( _tKey, ui32Index ) ) { 00164 m_vKeys.Remove( ui32Index ); 00165 } 00166 } 00167 00171 LSVOID LSE_CALL ResetNoDealloc() { 00172 m_vKeys.ResetNoDealloc(); 00173 } 00174 00178 LSVOID LSE_CALL Reset() { 00179 m_vKeys.Reset(); 00180 } 00181 00187 LSUINT32 LSE_CALL Length() const { 00188 return m_vKeys.Length(); 00189 } 00190 00197 const _tKeyType & LSE_CALL GetByIndex( LSUINT32 _ui32Index ) const { 00198 return m_vKeys[_ui32Index]; 00199 } 00200 00208 LSVOID LSE_CALL SetAllocator( CAllocator * _paAllocator ) { 00209 m_vKeys.SetAllocator( _paAllocator ); 00210 } 00211 00217 CAllocator * LSE_CALL GetAllocator() { 00218 return m_vKeys.GetAllocator(); 00219 } 00220 00227 LSBOOL LSE_CALL ItemExists( const _tKeyType &_tKey ) const { 00228 LSUINT32 ui32Index; 00229 return GetItemIndex( _tKey, ui32Index ); 00230 } 00231 00240 LSBOOL LSE_CALL GetItemIndex( const _tKeyType &_tKey, LSUINT32 &_ui32Index ) const { 00241 return CAlgorithm::BSearch( m_vKeys, _tKey, _ui32Index ); 00242 } 00243 00244 protected : 00245 // == Members. 00249 CVector<_tKeyType, LSUINT32> m_vKeys; 00250 00251 }; 00252 00253 } // namespace lstl 00254 00255 #pragma warning( pop ) 00256 00257 #endif // __LSTL_SET_H__