Okay so I rewrote my map class, and changed pretty much everything.
What I fixed (I think)-
- I added memory recording. Probably not accurate, but I don't know how else to do it.
- Removed a lot of unnecessary searching, thus saving a lot of time I think.
- Added functionality for copying.
- Added better exception. using std::runtime_error
Well, here it is. I am hoping you all can point out mistakes, or better ways of doing something.
Although not really looking for people to tell me it doesn't work like an associative array, as I already know that ( just do not have a better name for the class )
Thank you for any input you have.Code:#pragma once #include <map> #include <list> #include <string> #include <stdexcept> template <class T> class cMap { private: /* Index management */ size_t m_iCurIndex; std::list<size_t> m_cList; /* Memory Recording */ size_t m_iMemUsage; bool m_fRecordMemory; std::map<size_t, T*> m_cMap; size_t _GetIndex() { if ( !m_cList.empty() ) { size_t iIndex = m_cList.back(); m_cList.pop_back(); return iIndex; } ++m_iCurIndex; return m_iCurIndex; } void _Copy(const cMap& p_cCopy) { //Make sure the map is empty first Clear(); //Now copy all indices, and pointers typename std::map<size_t, T*>::const_iterator iter = p_cCopy.m_cMap.begin(); for (/**/; iter != p_cCopy.m_cMap.end(); ++iter ) { m_cMap[(*iter).first] = new T(*(*iter).second); } //Copy index list m_cList = p_cCopy.m_cList; //Copy variable values m_iCurIndex = p_cCopy.m_iCurIndex ; m_iMemUsage = p_cCopy.m_iMemUsage; m_fRecordMemory = p_cCopy.m_fRecordMemory; } public: explicit cMap() :m_iCurIndex(0), m_iMemUsage(0),m_fRecordMemory(true) { } ~cMap() { Clear(); } /* Copying */ explicit cMap(const cMap& p_cCopy) { _Copy(p_cCopy); } const cMap& operator=(const cMap& p_cCopy) { _Copy(p_cCopy); return *this; } /* Memory Recording */ void SetMemRec(bool p_fValue) { m_fRecordMemory = p_fValue; } bool GetMemRec() const { return m_fRecordMemory; } int GetMemUsage() const { return m_iMemUsage;} size_t Push(const T& p_cCopy) { size_t iIndex = _GetIndex(); m_cMap[iIndex] = new T(p_cCopy); /* Manage memory */ if ( m_fRecordMemory ) { m_iMemUsage += sizeof(T); } return iIndex; } void Remove(size_t p_iIndex) throw(...) { typename std::map<size_t,T*>::iterator iter = m_cMap.find(p_iIndex); if ( iter == m_cMap.end() ) { throw std::runtime_error("Invalid index was provided to cMap<T*>::Remove(size_t)"); } delete (*iter).second; (*iter).second = 0; m_cList.push_back(p_iIndex); m_cMap.erase(iter); /* Manage memory */ if ( m_fRecordMemory ) { m_iMemUsage -= sizeof(T); } } void Clear() { typename std::map<size_t,T*>::iterator iter = m_cMap.begin(); for(/**/; iter != m_cMap.end(); ++iter ) { delete (*iter).second; (*iter).second = 0; } m_iMemUsage = 0; m_iCurIndex = 0; m_cMap.clear(); } T& operator[](size_t p_iIndex) { if ( m_cMap.find(p_iIndex) == m_cMap.end() ) { throw std::runtime_error("Invalid index was provided to cMap<T*>::operator[](size_t)"); } return *m_cMap[p_iIndex]; } const T& operator[](size_t p_iIndex) const { if ( m_cMap.find(p_iIndex) == m_cMap.end() ) { throw std::runtime_error("Invalid index was provided to cMap<T*>::operator[](size_t)"); } return *m_cMap[p_iIndex]; } };