flat_map.h 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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_MAP_H_
  28. #define RTTR_FLAT_MAP_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. #include <ciso646> // _LIBCPP_VERSION
  37. namespace rttr
  38. {
  39. namespace detail
  40. {
  41. /*!
  42. * \brief The flat_map class implements a simple map based on std::vector instead of a binary tree.
  43. *
  44. */
  45. template<typename Key, typename Value, template<class> class Hash = std::hash, typename Compare = std::equal_to<Key>>
  46. class flat_map
  47. {
  48. template<typename Hash_Type = std::size_t>
  49. struct key_data
  50. {
  51. Key m_key;
  52. Hash_Type m_hash_value;
  53. struct order
  54. {
  55. RTTR_INLINE bool operator () (const key_data& left, const key_data& right) const
  56. {
  57. return (left.m_hash_value < right.m_hash_value);
  58. }
  59. RTTR_INLINE bool operator () (const Hash_Type& left, const key_data& right) const
  60. {
  61. return (left < right.m_hash_value);
  62. }
  63. RTTR_INLINE bool operator () (const key_data& left, const Hash_Type& right) const
  64. {
  65. return (left.m_hash_value < right);
  66. }
  67. };
  68. };
  69. public:
  70. using key_data_type = key_data<>;
  71. using value_type = std::pair<const Key, Value>;
  72. using iterator = typename std::vector<Value>::iterator;
  73. using const_iterator = typename std::vector<Value>::const_iterator;
  74. using const_iterator_key = typename std::vector<key_data_type>::const_iterator;
  75. using iterator_key = typename std::vector<key_data_type>::iterator;
  76. using hash_type = std::size_t;
  77. flat_map() {}
  78. private:
  79. using has_type = Hash<Key>;
  80. public:
  81. iterator end()
  82. {
  83. return m_value_list.end();
  84. }
  85. const_iterator end() const
  86. {
  87. return m_value_list.end();
  88. }
  89. const_iterator cend() const
  90. {
  91. return m_value_list.cend();
  92. }
  93. void insert(value_type&& value)
  94. {
  95. insert(std::move(value.first), std::move(value.second));
  96. }
  97. void insert(const Key&& key, Value&& value)
  98. {
  99. if (find(key) != end())
  100. return;
  101. m_key_list.push_back(key_data_type{std::move(key), has_type()(key)});
  102. std::stable_sort(m_key_list.begin(), m_key_list.end(), typename key_data_type::order());
  103. auto found_key = find_key_const(key);
  104. if (found_key != m_key_list.cend())
  105. {
  106. const auto index = std::distance(m_key_list.cbegin(), found_key);
  107. m_value_list.insert(m_value_list.begin() + index, std::move(value));
  108. }
  109. }
  110. template<typename T>
  111. const_iterator find(const T& key) const
  112. {
  113. const auto hash_value = Hash<T>()(key);
  114. auto itr = std::lower_bound(m_key_list.begin(), m_key_list.end(),
  115. hash_value,
  116. typename key_data_type::order());
  117. for (; itr != m_key_list.end(); ++itr)
  118. {
  119. auto& item = *itr;
  120. if (item.m_hash_value != hash_value)
  121. break;
  122. if (item.m_key == key)
  123. return (m_value_list.cbegin() + std::distance(m_key_list.cbegin(), itr));;
  124. }
  125. return m_value_list.cend();
  126. }
  127. iterator find(const Key& key)
  128. {
  129. const auto itr = find_key_const(key);
  130. if (itr != m_key_list.end())
  131. return (m_value_list.begin() + std::distance(m_key_list.cbegin(), itr));
  132. else
  133. return (m_value_list.end());
  134. }
  135. const_iterator find(const Key& key) const
  136. {
  137. const auto itr = find_key_const(key);
  138. if (itr != m_key_list.end())
  139. return (m_value_list.cbegin() + std::distance(m_key_list.cbegin(), itr));
  140. else
  141. return (m_value_list.cend());
  142. }
  143. #ifdef _LIBCPP_VERSION
  144. # if _LIBCPP_VERSION <= 3700
  145. # define RTTR_NO_CXX11_CONST_EREASE_SUPPORT_IN_STL 1
  146. # endif
  147. #elif (RTTR_COMPILER == RTTR_COMPILER_GNUC && RTTR_COMP_VER < 490)
  148. # define RTTR_NO_CXX11_CONST_EREASE_SUPPORT_IN_STL 1
  149. #endif
  150. // older versions of gcc stl, have no support for const_iterator in std::vector<T>::erase(const_iterator)
  151. #if RTTR_NO_CXX11_CONST_EREASE_SUPPORT_IN_STL
  152. bool erase(const Key& key)
  153. {
  154. iterator_key itr = find_key(key);
  155. if (itr != m_key_list.end())
  156. {
  157. auto value_itr = m_value_list.begin() + std::distance(m_key_list.begin(), itr);
  158. if (value_itr != m_value_list.end())
  159. {
  160. m_key_list.erase(itr);
  161. m_value_list.erase(value_itr);
  162. return true;
  163. }
  164. }
  165. return false;
  166. }
  167. #else
  168. bool erase(const Key& key)
  169. {
  170. const_iterator_key itr = find_key_const(key);
  171. if (itr != m_key_list.end())
  172. {
  173. auto value_itr = m_value_list.cbegin() + std::distance(m_key_list.cbegin(), itr);
  174. if (value_itr != m_value_list.cend())
  175. {
  176. m_key_list.erase(itr);
  177. m_value_list.erase(value_itr);
  178. return true;
  179. }
  180. }
  181. return false;
  182. }
  183. #endif
  184. void clear()
  185. {
  186. m_key_list.clear();
  187. m_value_list.clear();
  188. }
  189. const std::vector<Value>& value_data() const
  190. {
  191. return m_value_list;
  192. }
  193. private:
  194. const_iterator_key find_key_const(const Key& key) const
  195. {
  196. const auto hash_value = has_type()(key);
  197. auto itr = std::lower_bound(m_key_list.begin(), m_key_list.end(),
  198. hash_value,
  199. typename key_data_type::order());
  200. for (; itr != m_key_list.end(); ++itr)
  201. {
  202. auto& item = *itr;
  203. if (item.m_hash_value != hash_value)
  204. break;
  205. if (Compare()(item.m_key, key))
  206. return itr;
  207. }
  208. return m_key_list.end();
  209. }
  210. iterator_key find_key(const Key& key)
  211. {
  212. const auto hash_value = has_type()(key);
  213. auto itr = std::lower_bound(m_key_list.begin(), m_key_list.end(),
  214. hash_value,
  215. typename key_data_type::order());
  216. for (; itr != m_key_list.end(); ++itr)
  217. {
  218. auto& item = *itr;
  219. if (item.m_hash_value != hash_value)
  220. break;
  221. if (Compare()(item.m_key, key))
  222. return itr;
  223. }
  224. return m_key_list.end();
  225. }
  226. private:
  227. std::vector<key_data_type> m_key_list;
  228. std::vector<Value> m_value_list;
  229. };
  230. } // end namespace detail
  231. } // end namespace rttr
  232. #endif // RTTR_FLAT_MAP_H_