flat_multimap.h 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. /************************************************************************************
  2. * *
  3. * Copyright (c) 2014 - 2018 Axel Menzel <info@rttr.org> *
  4. * *
  5. * This file is part of RTTR (Run Time Type Reflection) *
  6. * License: MIT License *
  7. * *
  8. * Permission is hereby granted, free of charge, to any person obtaining *
  9. * a copy of this software and associated documentation files (the "Software"), *
  10. * to deal in the Software without restriction, including without limitation *
  11. * the rights to use, copy, modify, merge, publish, distribute, sublicense, *
  12. * and/or sell copies of the Software, and to permit persons to whom the *
  13. * Software is furnished to do so, subject to the following conditions: *
  14. * *
  15. * The above copyright notice and this permission notice shall be included in *
  16. * all copies or substantial portions of the Software. *
  17. * *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *
  21. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, *
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE *
  24. * SOFTWARE. *
  25. * *
  26. *************************************************************************************/
  27. #ifndef RTTR_FLAT_MULTIMAP_H_
  28. #define RTTR_FLAT_MULTIMAP_H_
  29. #include "rttr/detail/base/core_prerequisites.h"
  30. #include "rttr/detail/misc/misc_type_traits.h"
  31. #include "rttr/detail/misc/std_type_traits.h"
  32. #include <vector>
  33. #include <utility>
  34. #include <functional>
  35. #include <algorithm>
  36. namespace rttr
  37. {
  38. namespace detail
  39. {
  40. /*!
  41. * \brief The \ref flat_multimap class implements a simple multimap based on std::vector instead of a binary tree.
  42. *
  43. */
  44. template<typename Key, typename Value, template<class> class Hash = std::hash, typename Compare = std::equal_to<Key>>
  45. class flat_multimap
  46. {
  47. template<typename Hash_Type = std::size_t>
  48. struct key_data
  49. {
  50. Key m_key;
  51. Hash_Type m_hash_value;
  52. struct order
  53. {
  54. RTTR_INLINE bool operator () (const key_data& left, const key_data& right) const
  55. {
  56. return (left.m_hash_value < right.m_hash_value);
  57. }
  58. RTTR_INLINE bool operator () (const Hash_Type& left, const key_data& right) const
  59. {
  60. return (left < right.m_hash_value);
  61. }
  62. RTTR_INLINE bool operator () (const key_data& left, const Hash_Type& right) const
  63. {
  64. return (left.m_hash_value < right);
  65. }
  66. };
  67. };
  68. public:
  69. using key_data_type = key_data<>;
  70. using value_type = std::pair<const Key, Value>;
  71. using iterator = typename std::vector<Value>::iterator;
  72. using const_iterator = typename std::vector<Value>::const_iterator;
  73. using const_iterator_key = typename std::vector<key_data_type>::const_iterator;
  74. using iterator_key = typename std::vector<key_data_type>::iterator;
  75. using hash_type = std::size_t;
  76. flat_multimap() {}
  77. private:
  78. using has_type = Hash<Key>;
  79. public:
  80. iterator end()
  81. {
  82. return m_value_list.end();
  83. }
  84. const_iterator end() const
  85. {
  86. return m_value_list.end();
  87. }
  88. const_iterator cend() const
  89. {
  90. return m_value_list.cend();
  91. }
  92. void insert(value_type&& value)
  93. {
  94. insert(std::move(value.first), std::move(value.second));
  95. }
  96. void insert(const Key&& key, Value&& value)
  97. {
  98. m_key_list.push_back(key_data_type{std::move(key), has_type()(key)});
  99. std::stable_sort(m_key_list.begin(), m_key_list.end(), typename key_data_type::order());
  100. auto found_key = find_key_const(key);
  101. if (found_key != m_key_list.cend())
  102. {
  103. auto itr_key = found_key;
  104. for (; itr_key != m_key_list.cend(); ++itr_key)
  105. {
  106. if (Compare()(itr_key->m_key, key))
  107. found_key = itr_key;
  108. else
  109. break;
  110. }
  111. const auto index = std::distance(m_key_list.cbegin(), found_key);
  112. m_value_list.insert(m_value_list.begin() + index, std::move(value));
  113. }
  114. }
  115. iterator find(const Key& key)
  116. {
  117. const auto itr = find_key_const(key);
  118. if (itr != m_key_list.end())
  119. return (m_value_list.begin() + std::distance(m_key_list.cbegin(), itr));
  120. else
  121. return (m_value_list.end());
  122. }
  123. const_iterator find(const Key& key) const
  124. {
  125. const auto itr = find_key_const(key);
  126. if (itr != m_key_list.end())
  127. return (m_value_list.cbegin() + std::distance(m_key_list.cbegin(), itr));
  128. else
  129. return (m_value_list.cend());
  130. }
  131. // older versions of gcc stl, have no support for const_iterator in std::vector<T>::erase(const_iterator)
  132. #if RTTR_COMPILER == RTTR_COMPILER_GNUC && RTTR_COMP_VER < 490
  133. bool erase(const Key& key)
  134. {
  135. iterator_key itr = find_key(key);
  136. if (itr != m_key_list.end())
  137. {
  138. auto value_itr = m_value_list.begin() + std::distance(m_key_list.begin(), itr);
  139. if (value_itr != m_value_list.end())
  140. {
  141. m_key_list.erase(itr);
  142. m_value_list.erase(value_itr);
  143. return true;
  144. }
  145. }
  146. return false;
  147. }
  148. #else
  149. bool erase(const Key& key)
  150. {
  151. const_iterator_key itr = find_key_const(key);
  152. if (itr != m_key_list.end())
  153. {
  154. auto value_itr = m_value_list.cbegin() + std::distance(m_key_list.cbegin(), itr);
  155. if (value_itr != m_value_list.cend())
  156. {
  157. m_key_list.erase(itr);
  158. m_value_list.erase(value_itr);
  159. return true;
  160. }
  161. }
  162. return false;
  163. }
  164. #endif
  165. const std::vector<Value>& value_data() const
  166. {
  167. return m_value_list;
  168. }
  169. private:
  170. const_iterator_key find_key_const(const Key& key) const
  171. {
  172. const auto hash_value = has_type()(key);
  173. auto itr = std::lower_bound(m_key_list.begin(), m_key_list.end(),
  174. hash_value,
  175. typename key_data_type::order());
  176. for (; itr != m_key_list.end(); ++itr)
  177. {
  178. auto& item = *itr;
  179. if (item.m_hash_value != hash_value)
  180. break;
  181. if (Compare()(item.m_key, key))
  182. return itr;
  183. }
  184. return m_key_list.end();
  185. }
  186. iterator_key find_key(const Key& key)
  187. {
  188. const auto hash_value = has_type()(key);
  189. auto itr = std::lower_bound(m_key_list.begin(), m_key_list.end(),
  190. hash_value,
  191. typename key_data_type::order());
  192. for (; itr != m_key_list.end(); ++itr)
  193. {
  194. auto& item = *itr;
  195. if (item.m_hash_value != hash_value)
  196. break;
  197. if (Compare()(item.m_key, key))
  198. return itr;
  199. }
  200. return m_key_list.end();
  201. }
  202. private:
  203. std::vector<key_data_type> m_key_list;
  204. std::vector<Value> m_value_list;
  205. };
  206. } // end namespace detail
  207. } // end namespace rttr
  208. #endif // RTTR_FLAT_MULTIMAP_H_