|
#ifndef __SimpleMap_h__
#define __SimpleMap_h__
// intended for small number of simple types or pointers
template <class TKey, class TVal>
class CeSimpleMap
{
public:
TKey* m_aKey;
TVal* m_aVal;
int m_nSize;
// Construction/destruction
CeSimpleMap() : m_aKey(NULL), m_aVal(NULL), m_nSize(0)
{ }
~CeSimpleMap()
{
RemoveAll();
}
// Operations
int GetSize() const
{
return m_nSize;
}
BOOL Add(TKey key, TVal val)
{
TKey* pKey;
pKey = (TKey*)realloc(m_aKey, (m_nSize + 1) * sizeof(TKey));
if(pKey == NULL)
return FALSE;
m_aKey = pKey;
TVal* pVal;
pVal = (TVal*)realloc(m_aVal, (m_nSize + 1) * sizeof(TVal));
if(pVal == NULL)
return FALSE;
m_aVal = pVal;
m_nSize++;
SetAtIndex(m_nSize - 1, key, val);
return TRUE;
}
BOOL Remove(TKey key)
{
int nIndex = FindKey(key);
if(nIndex == -1)
return FALSE;
if(nIndex != (m_nSize - 1))
{
m_aKey[nIndex].~TKey();
m_aVal[nIndex].~TVal();
memmove((void*)&m_aKey[nIndex], (void*)&m_aKey[nIndex + 1], (m_nSize - (nIndex + 1)) * sizeof(TKey));
memmove((void*)&m_aVal[nIndex], (void*)&m_aVal[nIndex + 1], (m_nSize - (nIndex + 1)) * sizeof(TVal));
}
TKey* pKey;
pKey = (TKey*)realloc(m_aKey, (m_nSize - 1) * sizeof(TKey));
if(pKey != NULL || m_nSize == 1)
m_aKey = pKey;
TVal* pVal;
pVal = (TVal*)realloc(m_aVal, (m_nSize - 1) * sizeof(TVal));
if(pVal != NULL || m_nSize == 1)
m_aVal = pVal;
m_nSize--;
return TRUE;
}
void RemoveAll()
{
if(m_aKey != NULL)
{
for(int i = 0; i < m_nSize; i++)
{
m_aKey[i].~TKey();
m_aVal[i].~TVal();
}
free(m_aKey);
m_aKey = NULL;
}
if(m_aVal != NULL)
{
free(m_aVal);
m_aVal = NULL;
}
m_nSize = 0;
}
BOOL SetAt(TKey key, TVal val)
{
int nIndex = FindKey(key);
if(nIndex == -1)
return FALSE;
SetAtIndex(nIndex, key, val);
return TRUE;
}
TVal Lookup(TKey key) const
{
int nIndex = FindKey(key);
if(nIndex == -1)
return NULL; // must be able to convert
return GetValueAt(nIndex);
}
TKey ReverseLookup(TVal val) const
{
int nIndex = FindVal(val);
if(nIndex == -1)
return NULL; // must be able to convert
return GetKeyAt(nIndex);
}
TKey& GetKeyAt(int nIndex) const
{
ASSERT(nIndex >= 0 && nIndex < m_nSize);
return m_aKey[nIndex];
}
TVal& GetValueAt(int nIndex) const
{
ASSERT(nIndex >= 0 && nIndex < m_nSize);
return m_aVal[nIndex];
}
// Implementation
template <typename T>
class Wrapper
{
public:
Wrapper(T& _t) : t(_t)
{
}
template <typename _Ty>
void *operator new(size_t, _Ty* p)
{
return p;
}
T t;
};
void SetAtIndex(int nIndex, TKey& key, TVal& val)
{
ASSERT(nIndex >= 0 && nIndex < m_nSize);
new(&m_aKey[nIndex]) Wrapper<TKey>(key);
new(&m_aVal[nIndex]) Wrapper<TVal>(val);
}
int FindKey(TKey& key) const
{
for(int i = 0; i < m_nSize; i++)
{
if(m_aKey[i] == key)
return i;
}
return -1; // not found
}
int FindVal(TVal& val) const
{
for(int i = 0; i < m_nSize; i++)
{
if(m_aVal[i] == val)
return i;
}
return -1; // not found
}
};
#endif // __SimpleMap_h__
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.