2010-05-07 04:47:10 -05:00
|
|
|
/*
|
2010-05-07 19:23:36 -05:00
|
|
|
* Copyright 2009-2010, Andrea Anzani. All rights reserved.
|
2010-05-07 04:47:10 -05:00
|
|
|
* Distributed under the terms of the MIT License.
|
|
|
|
*/
|
|
|
|
#ifndef _KEY_MAP_H
|
|
|
|
#define _KEY_MAP_H
|
|
|
|
|
|
|
|
#include <map>
|
|
|
|
|
2021-05-19 16:12:19 -05:00
|
|
|
#include "List.h"
|
2010-05-07 04:47:10 -05:00
|
|
|
|
|
|
|
template<class KEY, class TYPE>
|
|
|
|
class KeyMap {
|
|
|
|
public:
|
2010-05-28 12:10:56 -05:00
|
|
|
uint32 CountItems() const;
|
2010-05-07 04:47:10 -05:00
|
|
|
|
2010-05-16 16:02:50 -05:00
|
|
|
void AddItem(KEY k, TYPE t);
|
2010-05-07 04:47:10 -05:00
|
|
|
|
2010-05-28 12:10:56 -05:00
|
|
|
TYPE ValueFor(KEY, bool* found = NULL) const;
|
2022-02-20 21:00:56 -06:00
|
|
|
KEY KeyFor(TYPE, bool* found = NULL) const;
|
2010-05-07 04:47:10 -05:00
|
|
|
|
2021-05-24 01:47:21 -05:00
|
|
|
TYPE RemoveItemAt(int32 position);
|
|
|
|
TYPE RemoveItemFor(KEY);
|
2010-05-07 04:47:10 -05:00
|
|
|
|
2010-05-28 12:10:56 -05:00
|
|
|
KEY KeyAt(uint32 position) const;
|
|
|
|
TYPE ValueAt(uint32 position) const;
|
2010-05-07 04:47:10 -05:00
|
|
|
|
2021-06-15 14:40:28 -05:00
|
|
|
void AddList(KeyMap<KEY, TYPE> appendList);
|
|
|
|
|
2010-05-07 04:47:10 -05:00
|
|
|
private:
|
2010-05-28 12:10:56 -05:00
|
|
|
std::map<KEY, TYPE> fMap;
|
|
|
|
typedef typename std::map<KEY, TYPE>::iterator fIter;
|
|
|
|
typedef typename std::map<KEY, TYPE>::const_iterator fConstIter;
|
2010-05-07 04:47:10 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
template<class KEY, class TYPE>
|
2010-05-28 12:10:56 -05:00
|
|
|
inline uint32
|
|
|
|
KeyMap<KEY, TYPE>::CountItems() const
|
2010-05-07 04:47:10 -05:00
|
|
|
{
|
|
|
|
return fMap.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template<class KEY, class TYPE>
|
2010-05-28 12:10:56 -05:00
|
|
|
inline void
|
|
|
|
KeyMap<KEY, TYPE>::AddItem(KEY k, TYPE t)
|
2010-05-07 04:47:10 -05:00
|
|
|
{
|
|
|
|
fMap[k] = t;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template<class KEY, class TYPE>
|
2010-05-28 12:10:56 -05:00
|
|
|
inline TYPE
|
|
|
|
KeyMap<KEY, TYPE>::ValueFor(KEY k, bool* found) const
|
2010-05-07 04:47:10 -05:00
|
|
|
{
|
2010-05-28 12:10:56 -05:00
|
|
|
fConstIter i = fMap.find(k);
|
2010-05-07 04:47:10 -05:00
|
|
|
|
|
|
|
if (found) {
|
|
|
|
if (i == fMap.end())
|
|
|
|
*found = false;
|
|
|
|
else
|
|
|
|
*found = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (i == fMap.end())
|
|
|
|
return NULL;
|
|
|
|
return i->second;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-02-20 21:00:56 -06:00
|
|
|
template<class KEY, class TYPE>
|
|
|
|
inline KEY
|
|
|
|
KeyMap<KEY, TYPE>::KeyFor(TYPE v, bool* found) const
|
|
|
|
{
|
|
|
|
*found = false;
|
|
|
|
for (int32 i = 0; i < CountItems(); i++)
|
|
|
|
if (ValueAt(i) == v) {
|
|
|
|
*found = true;
|
|
|
|
return KeyAt(i);
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-05-07 04:47:10 -05:00
|
|
|
template<class KEY, class TYPE>
|
2021-05-24 01:47:21 -05:00
|
|
|
inline TYPE
|
2010-05-28 12:10:56 -05:00
|
|
|
KeyMap<KEY, TYPE>::RemoveItemAt(int32 position)
|
2010-05-07 04:47:10 -05:00
|
|
|
{
|
2021-05-24 01:47:21 -05:00
|
|
|
TYPE value = ValueAt(position);
|
2010-05-07 04:47:10 -05:00
|
|
|
fIter i = fMap.begin();
|
|
|
|
std::advance(i, position);
|
|
|
|
fMap.erase(i->first);
|
2021-05-24 01:47:21 -05:00
|
|
|
return value;
|
2010-05-07 04:47:10 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template<class KEY, class TYPE>
|
2021-05-24 01:47:21 -05:00
|
|
|
inline TYPE
|
2010-05-28 12:10:56 -05:00
|
|
|
KeyMap<KEY, TYPE>::RemoveItemFor(KEY k)
|
2010-05-07 04:47:10 -05:00
|
|
|
{
|
2021-05-24 01:47:21 -05:00
|
|
|
TYPE value = ValueFor(k);
|
2010-05-07 04:47:10 -05:00
|
|
|
fMap.erase(k);
|
2021-05-24 01:47:21 -05:00
|
|
|
return value;
|
2010-05-07 04:47:10 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template<class KEY, class TYPE>
|
2010-05-28 12:10:56 -05:00
|
|
|
inline KEY
|
|
|
|
KeyMap<KEY, TYPE>::KeyAt(uint32 position) const
|
2010-05-07 04:47:10 -05:00
|
|
|
{
|
2021-06-01 21:41:49 -05:00
|
|
|
fConstIter i = fMap.begin();
|
2010-05-07 04:47:10 -05:00
|
|
|
std::advance(i, position);
|
|
|
|
if (i == fMap.end())
|
|
|
|
return NULL;
|
2010-05-28 12:10:56 -05:00
|
|
|
return i->first;
|
2010-05-07 04:47:10 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template<class KEY, class TYPE>
|
2010-05-28 12:10:56 -05:00
|
|
|
inline TYPE
|
|
|
|
KeyMap<KEY, TYPE>::ValueAt(uint32 position) const
|
2010-05-07 04:47:10 -05:00
|
|
|
{
|
2010-05-28 12:10:56 -05:00
|
|
|
fConstIter i = fMap.begin();
|
2010-05-07 04:47:10 -05:00
|
|
|
std::advance(i, position);
|
|
|
|
if (i == fMap.end())
|
|
|
|
return NULL;
|
2010-05-28 12:10:56 -05:00
|
|
|
return i->second;
|
2010-05-07 04:47:10 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-06-15 14:40:28 -05:00
|
|
|
template<class KEY, class TYPE>
|
|
|
|
inline void
|
|
|
|
KeyMap<KEY, TYPE>::AddList(KeyMap<KEY, TYPE> appendList)
|
|
|
|
{
|
|
|
|
if (appendList.CountItems() == 0)
|
|
|
|
return;
|
|
|
|
for (int i = 0; i < appendList.CountItems(); i++)
|
|
|
|
AddItem(appendList.KeyAt(i), appendList.ValueAt(i));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-05-07 04:47:10 -05:00
|
|
|
#endif // _KEY_MAP_H
|