/* * Copyright 2009-2010, Andrea Anzani. All rights reserved. * Distributed under the terms of the MIT License. */ #ifndef _KEY_MAP_H #define _KEY_MAP_H #include #include "List.h" template class KeyMap { public: uint32 CountItems() const; void AddItem(KEY k, TYPE t); TYPE ValueFor(KEY, bool* found = NULL) const; TYPE RemoveItemAt(int32 position); TYPE RemoveItemFor(KEY); KEY KeyAt(uint32 position) const; TYPE ValueAt(uint32 position) const; void AddList(KeyMap appendList); private: std::map fMap; typedef typename std::map::iterator fIter; typedef typename std::map::const_iterator fConstIter; }; template inline uint32 KeyMap::CountItems() const { return fMap.size(); } template inline void KeyMap::AddItem(KEY k, TYPE t) { fMap[k] = t; } template inline TYPE KeyMap::ValueFor(KEY k, bool* found) const { fConstIter 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 inline TYPE KeyMap::RemoveItemAt(int32 position) { TYPE value = ValueAt(position); fIter i = fMap.begin(); std::advance(i, position); fMap.erase(i->first); return value; } template inline TYPE KeyMap::RemoveItemFor(KEY k) { TYPE value = ValueFor(k); fMap.erase(k); return value; } template inline KEY KeyMap::KeyAt(uint32 position) const { fConstIter i = fMap.begin(); std::advance(i, position); if (i == fMap.end()) return NULL; return i->first; } template inline TYPE KeyMap::ValueAt(uint32 position) const { fConstIter i = fMap.begin(); std::advance(i, position); if (i == fMap.end()) return NULL; return i->second; } template inline void KeyMap::AddList(KeyMap appendList) { if (appendList.CountItems() == 0) return; for (int i = 0; i < appendList.CountItems(); i++) AddItem(appendList.KeyAt(i), appendList.ValueAt(i)); } #endif // _KEY_MAP_H