"L. Spiro Engine"

F:/My Projects/LSEngine/Modules/LSTL/Src/Map/LSTLMap.h

00001 
00020 #ifndef __LSTL_MAP_H__
00021 #define __LSTL_MAP_H__
00022 
00023 #include "../LSTLib.h"
00024 #include "../Algorithm/LSTLAlgorithm.h"
00025 #include "../Vector/LSTLVector.h"
00026 #include "LSTLMapBase.h"
00027 #include "Search/LSSTDSearch.h"
00028 #include <new>
00029 
00030 #pragma warning( push )
00031 
00032 // warning C4127: conditional expression is constant
00033 #pragma warning( disable : 4127 )
00034 
00035 namespace lstl {
00036 
00047         template <typename _tKeyType, typename _tMappedType>
00048         class CMap : protected CMapBase {
00049         public :
00050                 // == Various constructors.
00051                 LSE_CALL                                                                CMap() {
00052                 }
00053                 explicit LSE_CALL                                               CMap( const CMap<_tKeyType, _tMappedType> &_mSrc ) :
00054                         m_vPairs( _mSrc.m_vPairs ) {
00055                 }
00056                 explicit LSE_CALL                                               CMap( CAllocator * _paAllocator ) :
00057                         m_vPairs( _paAllocator ) {
00058                 }
00059                 LSE_CALL                                                                ~CMap() {
00060                         Reset();
00061                 }
00062 
00063 
00064                 // == Types.
00068                 typedef struct LSTL_PAIR {
00072                         _tKeyType                                                       tKey;
00073 
00077                         _tMappedType                                            tValue;
00078 
00085                         LSTL_PAIR & LSE_CALL                            operator = ( const LSTL_PAIR &_pSrc ) {
00086                                 tKey = _pSrc.tKey;
00087                                 tValue = _pSrc.tValue;
00088                                 return (*this);
00089                         }
00090 
00097                         LSBOOL LSE_CALL                                         operator < ( const _tKeyType &_tVal ) const {
00098                                 return tKey < _tVal;
00099                         }
00100 
00107                         LSBOOL LSE_CALL                                         operator == ( const _tKeyType &_tVal ) const {
00108                                 return tKey == _tVal;
00109                         }
00110 
00114                         LSE_CALL                                                        LSTL_PAIR() :
00115                                 tKey(),
00116                                 tValue() {
00117                         }
00118                 } * LPLSTL_PAIR, * const LPCLSTL_PAIR;
00119 
00120 
00121                 // == Operators.
00129                 _tMappedType & LSE_CALL                                 operator [] ( const _tKeyType &_tKey ) {
00130                         _tMappedType * ptValue = NULL;
00131                         _tMappedType tDefault = _tMappedType();
00132                         if ( !Insert( _tKey, tDefault, &ptValue ) ) {
00133                                 throw LSSTD_E_OUTOFMEMORY;
00134                         }
00135                         return (*ptValue);
00136                 }
00137 
00145                 const _tMappedType & LSE_CALL                   operator [] ( const _tKeyType &_tKey ) const {
00146                         LSUINT32 ui32Index;
00147                         if ( !GetItemIndex( _tKey, ui32Index ) ) {
00148                                 // The const form of this function can only be used when values are
00149                                 //      known to exist in the list.
00150                                 throw LSSTD_E_INVALIDCALL;
00151                         }
00152                         return m_vPairs[ui32Index].tValue;
00153                 }
00154 
00162                 CMap<_tKeyType, _tMappedType> & LSE_CALL
00163                                                                                                 operator = ( const CMap<_tKeyType, _tMappedType> &_mSrc ) {
00164                         // The copy is as easy as copying the vector.
00165                         m_vPairs = _mSrc.m_vPairs;
00166                         return (*this);
00167                 }
00168 
00169 
00170                 // == Functions.
00179                 LSBOOL LSE_CALL                                                 Allocate( LSUINT32 _ui32Total ) {
00180                         return m_vPairs.Allocate( _ui32Total );
00181                 }
00182 
00191                 LSBOOL LSE_CALL                                                 Insert( const LSTL_PAIR * _ppPairs, LSUINT32 _ui32Total ) {
00192                         for ( LSUINT32 I = _ui32Total; I--; ) {
00193                                 if ( !Insert( _ppPairs[I] ) ) { return false; }
00194                         }
00195                         return true;
00196                 }
00197 
00206                 LSBOOL LSE_CALL                                                 Insert( const LSTL_PAIR &_pPair, _tMappedType ** _pptReturnValue = NULL ) {
00207                         LSUINT32 ui32Index;
00208                         if ( !GetItemIndex( _pPair.tKey, ui32Index ) ) {
00209                                 // Insert the item at the given index.
00210                                 if ( !m_vPairs.Insert( _pPair, ui32Index ) ) {
00211                                         if ( _pptReturnValue ) {
00212                                                 (*_pptReturnValue) = NULL;
00213                                         }
00214                                         return false;
00215                                 }
00216                         }
00217                         if ( _pptReturnValue ) {
00218                                 (*_pptReturnValue) = &m_vPairs[ui32Index].tValue;
00219                         }
00220                         return true;
00221                 }
00222 
00232                 LSBOOL LSE_CALL                                                 Insert( const _tKeyType &_tKey, const _tMappedType &_tValue, _tMappedType ** _pptReturnValue = NULL ) {
00233                         LSTL_PAIR pPair;
00234                         pPair.tKey = _tKey;
00235                         pPair.tValue = _tValue;
00236                         return Insert( pPair, _pptReturnValue );
00237                 }
00238 
00245                 LSVOID LSE_CALL                                                 RemoveNoDealloc( const _tKeyType * _ptKeys, LSUINT32 _ui32Total ) {
00246                         for ( LSUINT32 I = _ui32Total; I--; ) {
00247                                 RemoveNoDealloc( _ptKeys[I] );
00248                         }
00249                 }
00250 
00256                 LSVOID LSE_CALL                                                 RemoveNoDealloc( const _tKeyType &_tKey ) {
00257                         LSUINT32 ui32Index;
00258                         if ( GetItemIndex( _tKey, ui32Index ) ) {
00259                                 m_vPairs.RemoveNoDealloc( ui32Index );
00260                         }
00261                 }
00262 
00269                 LSVOID LSE_CALL                                                 Remove( const _tKeyType * _ptKeys, LSUINT32 _ui32Total ) {
00270                         for ( LSUINT32 I = _ui32Total; I--; ) {
00271                                 Remove( _ptKeys[I] );
00272                         }
00273                 }
00274 
00280                 LSVOID LSE_CALL                                                 Remove( const _tKeyType &_tKey ) {
00281                         LSUINT32 ui32Index;
00282                         if ( GetItemIndex( _tKey, ui32Index ) ) {
00283                                 m_vPairs.Remove( ui32Index );
00284                         }
00285                 }
00286 
00292                 LSVOID LSE_CALL                                                 RemoveByIndex( LSUINT32 _ui32Index ) {
00293                         m_vPairs.Remove( _ui32Index );
00294                 }
00295 
00301                 LSVOID LSE_CALL                                                 RemoveByIndexNoDealloc( LSUINT32 _ui32Index ) {
00302                         m_vPairs.RemoveNoDealloc( _ui32Index );
00303                 }
00304 
00308                 LSVOID LSE_CALL                                                 ResetNoDealloc() {
00309                         m_vPairs.ResetNoDealloc();
00310                 }
00311 
00315                 LSVOID LSE_CALL                                                 Reset() {
00316                         m_vPairs.Reset();
00317                 }
00318 
00324                 LSUINT32 LSE_CALL                                               Length() const {
00325                         return m_vPairs.Length();
00326                 }
00327 
00334                 _tMappedType & LSE_CALL                                 GetByIndex( LSUINT32 _ui32Index ) {
00335                         return m_vPairs[_ui32Index].tValue;
00336                 }
00337 
00344                 const _tMappedType & LSE_CALL                   GetByIndex( LSUINT32 _ui32Index ) const {
00345                         return m_vPairs[_ui32Index].tValue;
00346                 }
00347 
00354                 _tKeyType & LSE_CALL                                    GetKeyByIndex( LSUINT32 _ui32Index ) {
00355                         return m_vPairs[_ui32Index].tKey;
00356                 }
00357 
00364                 const _tKeyType & LSE_CALL                              GetKeyByIndex( LSUINT32 _ui32Index ) const {
00365                         return m_vPairs[_ui32Index].tKey;
00366                 }
00367 
00376                 LSBOOL LSE_CALL                                                 GetItemIndex( const _tKeyType &_tKey, LSUINT32 &_ui32Index ) const {
00377                         return CAlgorithm::BSearch( m_vPairs, _tKey, _ui32Index );
00378                 }
00379 
00386                 LSBOOL LSE_CALL                                                 ItemExists( const _tKeyType &_tKey ) const {
00387                         LSUINT32 ui32Index;
00388                         return GetItemIndex( _tKey, ui32Index );
00389                 }
00390 
00399                 LSBOOL LSE_CALL                                                 ItemExists( const _tKeyType &_tKey, LSUINT32 &_ui32Index ) const {
00400                         return GetItemIndex( _tKey, _ui32Index );
00401                 }
00402 
00410                 LSVOID LSE_CALL                                                 SetAllocator( CAllocator * _paAllocator ) {
00411                         m_vPairs.SetAllocator( _paAllocator );
00412                 }
00413 
00419                 CAllocator * LSE_CALL                                   GetAllocator() {
00420                         return m_vPairs.GetAllocator();
00421                 }
00422 
00423         protected :
00424                 // == Members.
00428                 CVector<LSTL_PAIR, LSUINT32, 1024UL>    m_vPairs;
00429 
00430         };
00431 
00432 }       // namespace lstl
00433 
00434 #pragma warning( pop )
00435 
00436 #endif  // __LSTL_MAP_H__
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator