KeyMap.h 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /*
  2. * Copyright 2009-2010, Andrea Anzani. All rights reserved.
  3. * Distributed under the terms of the MIT License.
  4. */
  5. #ifndef _KEY_MAP_H
  6. #define _KEY_MAP_H
  7. #include <map>
  8. #include "List.h"
  9. template<class KEY, class TYPE>
  10. class KeyMap {
  11. public:
  12. uint32 CountItems() const;
  13. void AddItem(KEY k, TYPE t);
  14. TYPE ValueFor(KEY, bool* found = NULL) const;
  15. TYPE RemoveItemAt(int32 position);
  16. TYPE RemoveItemFor(KEY);
  17. KEY KeyAt(uint32 position) const;
  18. TYPE ValueAt(uint32 position) const;
  19. void AddList(KeyMap<KEY, TYPE> appendList);
  20. private:
  21. std::map<KEY, TYPE> fMap;
  22. typedef typename std::map<KEY, TYPE>::iterator fIter;
  23. typedef typename std::map<KEY, TYPE>::const_iterator fConstIter;
  24. };
  25. template<class KEY, class TYPE>
  26. inline uint32
  27. KeyMap<KEY, TYPE>::CountItems() const
  28. {
  29. return fMap.size();
  30. }
  31. template<class KEY, class TYPE>
  32. inline void
  33. KeyMap<KEY, TYPE>::AddItem(KEY k, TYPE t)
  34. {
  35. fMap[k] = t;
  36. }
  37. template<class KEY, class TYPE>
  38. inline TYPE
  39. KeyMap<KEY, TYPE>::ValueFor(KEY k, bool* found) const
  40. {
  41. fConstIter i = fMap.find(k);
  42. if (found) {
  43. if (i == fMap.end())
  44. *found = false;
  45. else
  46. *found = true;
  47. }
  48. if (i == fMap.end())
  49. return NULL;
  50. return i->second;
  51. }
  52. template<class KEY, class TYPE>
  53. inline TYPE
  54. KeyMap<KEY, TYPE>::RemoveItemAt(int32 position)
  55. {
  56. TYPE value = ValueAt(position);
  57. fIter i = fMap.begin();
  58. std::advance(i, position);
  59. fMap.erase(i->first);
  60. return value;
  61. }
  62. template<class KEY, class TYPE>
  63. inline TYPE
  64. KeyMap<KEY, TYPE>::RemoveItemFor(KEY k)
  65. {
  66. TYPE value = ValueFor(k);
  67. fMap.erase(k);
  68. return value;
  69. }
  70. template<class KEY, class TYPE>
  71. inline KEY
  72. KeyMap<KEY, TYPE>::KeyAt(uint32 position) const
  73. {
  74. fConstIter i = fMap.begin();
  75. std::advance(i, position);
  76. if (i == fMap.end())
  77. return NULL;
  78. return i->first;
  79. }
  80. template<class KEY, class TYPE>
  81. inline TYPE
  82. KeyMap<KEY, TYPE>::ValueAt(uint32 position) const
  83. {
  84. fConstIter i = fMap.begin();
  85. std::advance(i, position);
  86. if (i == fMap.end())
  87. return NULL;
  88. return i->second;
  89. }
  90. template<class KEY, class TYPE>
  91. inline void
  92. KeyMap<KEY, TYPE>::AddList(KeyMap<KEY, TYPE> appendList)
  93. {
  94. if (appendList.CountItems() == 0)
  95. return;
  96. for (int i = 0; i < appendList.CountItems(); i++)
  97. AddItem(appendList.KeyAt(i), appendList.ValueAt(i));
  98. }
  99. #endif // _KEY_MAP_H