/* * Copyright 2009, Andrea Anzani. All rights reserved. * Distributed under the terms of the MIT License. */ #ifndef _KEY_MAP_H #define _KEY_MAP_H #include #include #include template class KeyMap { public: uint32 CountItems(); void AddItem(KEY k, TYPE t); TYPE ValueFor(KEY, bool* found = NULL); void RemoveItemAt(int32 position); void RemoveItemFor(KEY); TYPE ValueAt(int32 position); KEY KeyAt(int32 position); BList* Items(); private: std::map fMap; typedef typename std::map::iterator fIter; }; template uint32 KeyMap::CountItems() { return fMap.size(); } template void KeyMap::AddItem(KEY k, TYPE t) { fMap[k] = t; } template TYPE KeyMap::ValueFor(KEY k, bool* found) { fIter i = fMap.find(k); if (found) { if (i == fMap.end()) *found = false; else *found = true; } if (i == fMap.end()) return NULL; return i->second; } template void KeyMap::RemoveItemAt(int32 position) { fIter i = fMap.begin(); std::advance(i, position); fMap.erase(i->first); } template void KeyMap::RemoveItemFor(KEY k) { fMap.erase(k); } template TYPE KeyMap::ValueAt(int32 position) { fIter i = fMap.begin(); std::advance(i, position); if (i == fMap.end()) return NULL; return i->second; } template KEY KeyMap::KeyAt(int32 position) { fIter i = fMap.begin(); std::advance(i, position); if (i == fMap.end()) return NULL; return i->first; } template BList* KeyMap::Items() { BList* list = new BList(); for (fIter i = fMap.begin(); i != fMap.end(); ++i) list->AddItem(i->second); return list; } #endif // _KEY_MAP_H