SortMap.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. /*
  2. Copyright (c) 2013 Daniele Bartolini, Michele Rossi
  3. Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
  4. Permission is hereby granted, free of charge, to any person
  5. obtaining a copy of this software and associated documentation
  6. files (the "Software"), to deal in the Software without
  7. restriction, including without limitation the rights to use,
  8. copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the
  10. Software is furnished to do so, subject to the following
  11. conditions:
  12. The above copyright notice and this permission notice shall be
  13. included in all copies or substantial portions of the Software.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  15. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  16. OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  17. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  18. HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  19. WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  21. OTHER DEALINGS IN THE SOFTWARE.
  22. */
  23. #pragma once
  24. #include "Array.h"
  25. #include <algorithm>
  26. namespace crown
  27. {
  28. /// Functions to manipulate SortMap.
  29. ///
  30. /// @ingroup Containers
  31. namespace sort_map
  32. {
  33. /// Returns whether the @a key exists in the map.
  34. template <typename TKey, typename TValue, typename Compare> bool has(const SortMap<TKey, TValue, Compare>& m, const TKey& key);
  35. /// Returns the value for the given @a key or @a deffault if
  36. /// the key does not exist in the map.
  37. template <typename TKey, typename TValue, typename Compare> const TValue& get(const SortMap<TKey, TValue, Compare>& m, const TKey& key, const TValue& deffault);
  38. /// Sorts the keys in the map.
  39. template <typename TKey, typename TValue, typename Compare> void sort(SortMap<TKey, TValue, Compare>& m);
  40. template <typename TKey, typename TValue, typename Compare> void set(SortMap<TKey, TValue, Compare>& m, const TKey& key, const TValue& val);
  41. /// Removes the @a key from the map if it exists.
  42. template <typename TKey, typename TValue, typename Compare> void remove(SortMap<TKey, TValue, Compare>& m, const TKey& key);
  43. /// Removes all the items in the map.
  44. template <typename TKey, typename TValue, typename Compare> void clear(SortMap<TKey, TValue, Compare>& m);
  45. /// Returns a pointer to the first item in the map, can be used to
  46. /// efficiently iterate over the elements.
  47. template <typename TKey, typename TValue, typename Compare> const typename SortMap<TKey, TValue, Compare>::Entry* begin(const SortMap<TKey, TValue, Compare>& m);
  48. template <typename TKey, typename TValue, typename Compare> const typename SortMap<TKey, TValue, Compare>::Entry* end(const SortMap<TKey, TValue, Compare>& m);
  49. } // namespace sort_map
  50. namespace sort_map_internal
  51. {
  52. const uint32_t END_OF_LIST = 0xFFFFFFFFu;
  53. struct FindResult
  54. {
  55. uint32_t item_i;
  56. };
  57. template <typename TKey, typename TValue, typename Compare>
  58. struct CompareEntry
  59. {
  60. bool operator()(const typename SortMap<TKey, TValue, Compare>::Entry& a,
  61. const typename SortMap<TKey, TValue, Compare>::Entry& b) const
  62. {
  63. return comp(a.key, b.key);
  64. }
  65. bool operator()(const typename SortMap<TKey, TValue, Compare>::Entry& a,
  66. const TKey& key) const
  67. {
  68. return comp(a.key, key);
  69. }
  70. Compare comp;
  71. };
  72. template <typename TKey, typename TValue, typename Compare>
  73. inline FindResult find(const SortMap<TKey, TValue, Compare>& m, const TKey& key)
  74. {
  75. CE_ASSERT(m.m_is_sorted, "Map not sorted");
  76. FindResult result;
  77. result.item_i = END_OF_LIST;
  78. const typename SortMap<TKey, TValue, Compare>::Entry* first =
  79. std::lower_bound(array::begin(m.m_data), array::end(m.m_data), key,
  80. sort_map_internal::CompareEntry<TKey, TValue, Compare>());
  81. if (first != array::end(m.m_data) && !(key < first->key))
  82. result.item_i = first - array::begin(m.m_data);
  83. return result;
  84. }
  85. } // namespace sort_map_internal
  86. namespace sort_map
  87. {
  88. template <typename TKey, typename TValue, typename Compare>
  89. inline bool has(const SortMap<TKey, TValue, Compare>& m, const TKey& key)
  90. {
  91. return sort_map_internal::find(m, key).item_i != sort_map_internal::END_OF_LIST;
  92. }
  93. template <typename TKey, typename TValue, typename Compare>
  94. const TValue& get(const SortMap<TKey, TValue, Compare>& m, const TKey& key, const TValue& deffault)
  95. {
  96. sort_map_internal::FindResult result = sort_map_internal::find(m, key);
  97. if (result.item_i == sort_map_internal::END_OF_LIST)
  98. return deffault;
  99. return m.m_data[result.item_i].value;
  100. }
  101. template <typename TKey, typename TValue, typename Compare>
  102. inline void sort(SortMap<TKey, TValue, Compare>& m)
  103. {
  104. std::sort(array::begin(m.m_data), array::end(m.m_data),
  105. sort_map_internal::CompareEntry<TKey, TValue, Compare>());
  106. m.m_is_sorted = true;
  107. }
  108. template <typename TKey, typename TValue, typename Compare>
  109. inline void set(SortMap<TKey, TValue, Compare>& m, const TKey& key, const TValue& val)
  110. {
  111. sort_map_internal::FindResult result = sort_map_internal::find(m, key);
  112. if (result.item_i == sort_map_internal::END_OF_LIST)
  113. {
  114. typename SortMap<TKey, TValue, Compare>::Entry e;
  115. e.key = key;
  116. e.value = val;
  117. array::push_back(m.m_data, e);
  118. }
  119. else
  120. {
  121. m.m_data[result.item_i].value = val;
  122. }
  123. m.m_is_sorted = false;
  124. }
  125. template <typename TKey, typename TValue, typename Compare>
  126. inline void remove(SortMap<TKey, TValue, Compare>& m, const TKey& key)
  127. {
  128. sort_map_internal::FindResult result = sort_map_internal::find(m, key);
  129. if (result.item_i == sort_map_internal::END_OF_LIST)
  130. return;
  131. if (array::size(m.m_data))
  132. {
  133. m.m_data[result.item_i] = m.m_data[array::size(m.m_data) - 1];
  134. array::pop_back(m.m_data);
  135. }
  136. m.m_is_sorted = false;
  137. }
  138. template <typename TKey, typename TValue, typename Compare>
  139. inline void clear(SortMap<TKey, TValue, Compare>& m)
  140. {
  141. array::clear(m.m_data);
  142. m.m_is_sorted = true;
  143. }
  144. template <typename TKey, typename TValue, typename Compare>
  145. inline const typename SortMap<TKey, TValue, Compare>::Entry* begin(const SortMap<TKey, TValue, Compare>& m)
  146. {
  147. return array::begin(m.m_data);
  148. }
  149. template <typename TKey, typename TValue, typename Compare>
  150. inline const typename SortMap<TKey, TValue, Compare>::Entry* end(const SortMap<TKey, TValue, Compare>& m)
  151. {
  152. return array::end(m.m_data);
  153. }
  154. } // namespace sort_map
  155. template <typename TKey, typename TValue, typename Compare>
  156. inline SortMap<TKey, TValue, Compare>::SortMap(Allocator& a)
  157. : m_is_sorted(true), m_data(a)
  158. {
  159. }
  160. } // namespace crown