#if !defined(_VREC_MAP_VIT_HPP_06_03_2002_) #define _VREC_MAP_VIT_HPP_06_03_2002_ // VRecMap.cpp: implementation of the VRecMap class. // //*****************************************************************************| // Vitaly Eremenko // created: 06.03.2002 // updated: 07.03.2002 //*****************************************************************************| ////////////////////////////////////////////////////////////////////// #include "stdafx.h" #include // CList,... #include using namespace std; #include "VRecMap.h" ////////////////////////////////////////////////////////////////////// // Construction/Destruction ////////////////////////////////////////////////////////////////////// template VRecMap :: VRecMap() //---------------------------------------------------------- { m_pNextIter = NULL; m_pFindIter = NULL; } //=============================================================================| template VRecMap :: ~VRecMap() //---------------------------------------------------------- { } //=============================================================================| template void VRecMap :: DeleteAll() //--------------------------------------------------------------- { m_mapData.clear(); m_pNextIter = NULL; m_pFindIter = NULL; } //=============================================================================| // It adds only new unique item (by it's name or ID) // ret: -1 - cannot add, item's Key exists in this list already. // 1..N - new num.of items in list. template int VRecMap :: AddItem(const TKey &i_Key, TValue &i_Rec) //-------------------------------------------------------------- { int nRecs1 = GetNumOfItems(); m_mapPair mAdd(i_Key, i_Rec); m_mapData.insert(mAdd); int nRecs2 = GetNumOfItems(); if (nRecs2 <= nRecs1) return -1; // cannot insert (i_Key is not unique etc.) return nRecs2; // new items num. } //=============================================================================| template int VRecMap :: DeleteItem(const TKey &i_KeyDel) //--------------------------------------------------------------- { if (m_mapData.empty()) return -1; // cannot delete m_mapData.erase(i_KeyDel); return GetNumOfItems(); // new items num. } //=============================================================================| // NOTE: Call it 1-st time before GetNextItem() etc. template int VRecMap :: SetInitPos() //----------------------------------------------------------- { m_pNextIter = m_mapData.begin(); if (m_pNextIter == m_mapData.end()) return -1; return 0; } //=============================================================================| template TValue* VRecMap :: GetNextItem(TKey *o_pRetKey) //---------------------------------------------------------------- { if (m_pNextIter == m_mapData.end()) return NULL; TValue &rec = m_pNextIter->second; if (o_pRetKey != NULL) *o_pRetKey = m_pNextIter->first; m_pNextIter++; return &rec; } //=============================================================================| // NOTE: Call it 1-st time before GetPrevItem() etc. template int VRecMap :: SetLastPos() //-------------------------------------------------------------- { m_pNextIter = m_mapData.end(); if (m_pNextIter == m_mapData.begin()) return -1; return (GetNumOfItems() - 1); } //=============================================================================| template TValue* VRecMap :: GetPrevItem(TKey *o_pRetKey) //------------------------------------------------------------------ { if (m_pNextIter == m_mapData.begin()) return NULL; m_pNextIter--; // 1-st time it = end() and cannot be used TValue &rec = m_pNextIter->second; if (o_pRetKey != NULL) *o_pRetKey = m_pNextIter->first; return &rec; } //=============================================================================| template TValue* VRecMap :: FindItemByKey(const TKey &i_Key) //------------------------------------------------------------------- { if (m_mapData.empty()) return NULL; m_pFindIter = m_mapData.find(i_Key); if (m_pFindIter == m_mapData.end()) return NULL; // cannot find TValue &rec = m_pFindIter->second; m_pNextIter = m_pFindIter; return &rec; } //=============================================================================| // NOTE: if i_SatrtKey not found in set, procedure will find from init.key; template const TKey* VRecMap :: FindNextKeyByValue(TValue &i_Val, TKey &i_StartKey) //------------------------------------------------------------- { if (m_mapData.empty()) return NULL; int nRecs = GetNumOfItems(); m_pFindIter = m_mapData.find(i_StartKey); if (m_pFindIter == m_mapData.end()) // key not found m_pFindIter = m_mapData.begin(); // restart find from init.record bool bFound = false; for (m_pNextIter = m_pFindIter; m_pNextIter != m_mapData.end(); m_pNextIter++) { TValue &rec = m_pNextIter->second; if (rec == i_Val) { bFound = true; m_pFindIter = m_pNextIter; break; } } const TKey *pKey = NULL; if (bFound) pKey = &m_pNextIter->first; return pKey; } //=============================================================================| template int VRecMap :: FindIndexByKey(TKey &i_Key, TValue *o_pRetVal) //------------------------------------------------------------- { if (m_mapData.empty()) return -1; int nRecs = GetNumOfItems(); m_pNextIter = m_mapData.begin(); int niFind = -1; TKey key = m_pNextIter->first; TValue *pVal = NULL; for (int niPos=0; niPos TValue* VRecMap :: GetItemByIndex(int i_niPos, TKey *o_pRetKey) //------------------------------------------------------------------ { if (m_mapData.empty()) return NULL; int nRecs = GetNumOfItems(); m_pNextIter = m_mapData.begin(); int niFind = -1; TKey key = m_pNextIter->first; TValue *pVal = NULL; for (int niPos=0; niPos <= i_niPos; niPos++) { pVal = GetNextItem(&key); } if (o_pRetKey != NULL) *o_pRetKey = key; // copy key return pVal; } //=============================================================================| template TValue& VRecMap :: operator[] (const TKey i_Key) //------------------------------------------------------------------ { TValue *pVal = FindItemByKey(i_Key); if (pVal == NULL) { TValue valAdd; AddItem(i_Key, valAdd); pVal = FindItemByKey(i_Key); // cannot use local obj. valAdd } ASSERT(pVal); return *pVal; } //=============================================================================| // ret: new num.of items in map; 0 - cannot insert new items; // template int VRecMap :: InsertFromMap(VRecMap i_Map) //------------------------------------------------------------------ { int nAdds = i_Map.GetNumOfItems(); if (nAdds < 1) return 0; i_Map.SetInitPos(); for (int niAdd = 0; niAdd < nAdds; niAdd++) { TKey retKey; TRecord *pRec = i_Map.GetNextItem(&retKey); if (pRec == NULL) continue; AddItem(retKey, *pRec); } int nRecs = GetNumOfItems(); return nRecs; } //=============================================================================| //*****************************************************************************| #endif // _VREC_MAP_VIT_HPP_06_03_2002_