Thread: Rewrote my cMap class. Looking for more criticism

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    673

    Rewrote my cMap class. Looking for more criticism

    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 )
    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];
    	}
    };
    Thank you for any input you have.
    Last edited by Raigne; 11-06-2008 at 01:54 PM. Reason: typo in code

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Specializing class
    By Elysia in forum C++ Programming
    Replies: 6
    Last Post: 09-28-2008, 04:30 AM
  2. Default class template problem
    By Elysia in forum C++ Programming
    Replies: 5
    Last Post: 07-11-2008, 08:44 AM
  3. Defining derivated class problem
    By mikahell in forum C++ Programming
    Replies: 9
    Last Post: 08-22-2007, 02:46 PM
  4. matrix class
    By shuo in forum C++ Programming
    Replies: 2
    Last Post: 07-13-2007, 01:03 AM
  5. Replies: 7
    Last Post: 05-26-2005, 10:48 AM