bucket_hash_map.hpp 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. //
  2. // bucket_hash_map.hpp
  3. // bucket_hash_map
  4. //
  5. // Created by Sam Jaffe on 2/7/17.
  6. //
  7. #pragma once
  8. #include <algorithm>
  9. #include <list>
  10. #include <memory>
  11. #include <utility>
  12. #include <vector>
  13. #include "iterator/recursive_iterator.hpp"
  14. template <typename K, typename V, typename Hash = std::hash<K>, typename KeyEqual = std::equal_to<K> >
  15. class bucket_hash_map;
  16. template <typename K, typename V, typename Hash, typename KeyEqual>
  17. class bucket_hash_map {
  18. public: // typedefs
  19. using key_type = K;
  20. using mapped_type = V;
  21. using value_type = std::pair<key_type const, mapped_type>;
  22. using size_type = std::size_t;
  23. using difference_type = std::ptrdiff_t;
  24. using hasher = Hash;
  25. using key_equal = KeyEqual;
  26. using reference = value_type &;
  27. using const_reference = value_type const &;
  28. using pointer = value_type *;
  29. using const_pointer = value_type const *;
  30. using bucket_type = std::list<value_type>;
  31. using impl_type = std::vector<bucket_type>;
  32. template <typename It>
  33. using iter_wrap = iterator::recursive_iterator<It>;
  34. using iterator = iter_wrap<typename impl_type::iterator>;
  35. using const_iterator = iter_wrap<typename impl_type::const_iterator>;
  36. using local_iterator = typename bucket_type::iterator;
  37. using local_const_iterator = typename bucket_type::const_iterator;
  38. static const constexpr size_type default_buckets = 10;
  39. static const constexpr float default_load_factor = 1.0F;
  40. static const constexpr float growth_factor = 2.0F;
  41. public:
  42. // Construction
  43. bucket_hash_map() : bucket_hash_map(default_buckets) {}
  44. bucket_hash_map(size_type num_buckets,
  45. hasher const & hash = hasher(),
  46. key_equal const & keq = key_equal()) :
  47. buckets_(num_buckets), hasher_(hash), key_equals_(keq) {}
  48. template <typename InputIt>
  49. bucket_hash_map(InputIt first, InputIt last,
  50. size_type num_buckets = default_buckets,
  51. hasher const & hash = hasher(),
  52. key_equal const & keq = key_equal())
  53. : bucket_hash_map(num_buckets, hash, keq) {
  54. insert(first, last);
  55. }
  56. bucket_hash_map(std::initializer_list<value_type> && ilist,
  57. size_type num_buckets = default_buckets,
  58. hasher const & hash = hasher(),
  59. key_equal const & keq = key_equal())
  60. : bucket_hash_map(num_buckets, hash, keq) {
  61. insert(ilist);
  62. }
  63. // Metadata
  64. bool empty() const { return size() == 0; }
  65. size_type size() const { return size_; }
  66. // Iterators
  67. iterator begin() { return { make_end_aware_iterator( buckets_ ) }; }
  68. const_iterator begin() const { return cbegin(); }
  69. const_iterator cbegin() const { return { make_end_aware_iterator( buckets_ ) }; }
  70. iterator end() { return iterator{}; }
  71. const_iterator end() const { return cend(); }
  72. const_iterator cend() const { return const_iterator{}; }
  73. // Create
  74. std::pair<iterator, bool> insert(value_type const & vt) {
  75. maybe_expand(1);
  76. auto lookup = lookup_impl(buckets_, vt.first);
  77. auto const end = lookup.first->end();
  78. bool const create = lookup.second == end;
  79. if ( create ) {
  80. ++size_;
  81. lookup.second = lookup.first->insert(end, vt);
  82. }
  83. return { {::iterator::in_place, make_end_aware_iterator(lookup.first, buckets_.end()), make_end_aware_iterator(lookup.second, end)}, create };
  84. }
  85. iterator insert(const_iterator, value_type const & vt) {
  86. return insert(vt).first;
  87. }
  88. template <typename InputIt>
  89. void insert(InputIt first, InputIt last) {
  90. maybe_expand(static_cast<std::size_t>(std::distance(first, last)));
  91. while (first != last) { insert(*first++); }
  92. }
  93. void insert(std::initializer_list<value_type> ilist) { insert(ilist.begin(), ilist.end()); }
  94. // Access
  95. mapped_type & operator[](key_type const & key) {
  96. auto lookup = lookup_impl(buckets_, key);
  97. auto end = lookup.first->end();
  98. if (lookup.second == end) {
  99. ++size_;
  100. return lookup.first->insert(lookup.second, {key, mapped_type{}})->second;
  101. } else {
  102. return lookup.second->second;
  103. }
  104. }
  105. mapped_type & at(key_type const & key) {
  106. auto it = find(key);
  107. if (it == end()) { throw std::out_of_range{"no element at key"}; }
  108. return it->second;
  109. }
  110. mapped_type const & at(key_type const & key) const {
  111. auto it = find(key);
  112. if (it == end()) { throw std::out_of_range{"no element at key"}; }
  113. return it->second;
  114. }
  115. iterator find(key_type const & key) {
  116. return find_impl<iterator>(buckets_, key);
  117. }
  118. std::pair<iterator, iterator> equal_range(key_type const & key) {
  119. auto it = find(key);
  120. return { it, ++iterator(it) };
  121. }
  122. const_iterator find(key_type const & key) const {
  123. return find_impl<const_iterator>(buckets_, key);
  124. }
  125. std::pair<const_iterator, const_iterator> equal_range(key_type const & key) const {
  126. auto it = find(key);
  127. return { it, ++const_iterator(it) };
  128. }
  129. size_type count(key_type const & key) const { return find(key) != end(); }
  130. // Remove
  131. iterator erase(const_iterator it) {
  132. return erase_impl(unconst_iterator(it));
  133. }
  134. iterator erase(const_iterator first, const_iterator last) {
  135. iterator it = unconst_iterator(first);
  136. while (last != it) { it = erase_impl(it); }
  137. return it;
  138. }
  139. size_type erase(key_type const & key) {
  140. size_type old_size = size_;
  141. erase(find(key));
  142. return old_size - size_; // 1 or 0
  143. }
  144. void clear() { erase(begin(), end()); }
  145. // Bucket Interaction Functions
  146. local_iterator begin(size_type bkt) { return buckets_[bkt].begin(); }
  147. local_const_iterator begin(size_type bkt) const { return buckets_[bkt].cbegin(); }
  148. local_const_iterator cbegin(size_type bkt) const { return buckets_[bkt].cbegin(); }
  149. local_iterator end(size_type bkt) { return buckets_[bkt].end(); }
  150. local_const_iterator end(size_type bkt) const { return buckets_[bkt].cend(); }
  151. local_const_iterator cend(size_type bkt) const { return buckets_[bkt].cend(); }
  152. size_type bucket_count() const { return buckets_.size(); }
  153. size_type bucket_size(size_type bkt) const { return buckets_[bkt].size(); }
  154. size_type bucket(key_type const & key) const { return hasher_(key) % bucket_count(); }
  155. // Hash Policy
  156. float load_factor() const { return static_cast<float>(size()) / bucket_count(); }
  157. float max_load_factor() const { return max_load_; }
  158. void max_load_factor(float max_load) { max_load_ = max_load; }
  159. void rehash(size_type buckets) {
  160. buckets = std::max({1UL, buckets, static_cast<size_type>(size() / max_load_factor())});
  161. bucket_hash_map next{buckets, hasher_, key_equals_};
  162. next.max_load_factor(max_load_factor());
  163. next.insert(begin(), end());
  164. swap(next);
  165. }
  166. void reserve(size_type count) {
  167. rehash(static_cast<size_type>(count / max_load_factor()));
  168. }
  169. void swap( bucket_hash_map & other ) {
  170. using std::swap;
  171. swap(size_, other.size_);
  172. swap(buckets_, other.buckets_);
  173. swap(max_load_, other.max_load_);
  174. swap(hasher_, other.hasher_);
  175. swap(key_equals_, other.key_equals_);
  176. }
  177. friend void swap(bucket_hash_map & lhs, bucket_hash_map & rhs) { lhs.swap(rhs); }
  178. friend bool operator==(bucket_hash_map const & lhs, bucket_hash_map const & rhs) {
  179. if (lhs.size() != rhs.size()) { return false; }
  180. auto end = rhs.end();
  181. for (auto & pair : lhs) {
  182. auto it = rhs.find(pair.first);
  183. if (it == end || it->second != pair.second) {
  184. return false;
  185. }
  186. }
  187. return true;
  188. }
  189. friend bool operator!=(bucket_hash_map const & lhs, bucket_hash_map const & rhs) {
  190. return !(lhs == rhs);
  191. }
  192. private:
  193. void maybe_expand(size_type add) {
  194. if ( static_cast<float>(size() + add) / bucket_count() > max_load_factor() ) {
  195. reserve(std::max(size() + add, static_cast<size_type>(size() * growth_factor)));
  196. }
  197. }
  198. template <typename Bucket>
  199. auto lookup_impl(Bucket & bkt, key_type const & key) const -> std::pair<decltype(bkt.begin()), decltype(bkt.begin()->begin())> {
  200. auto listit = bkt.begin() + static_cast<std::ptrdiff_t>(bucket(key));
  201. auto it = search(listit, key);
  202. return {listit, it};
  203. }
  204. template <typename Iterator>
  205. auto search(Iterator lit, key_type const & key) const -> decltype(lit->begin()) {
  206. for ( auto it = lit->begin(); it != lit->end(); ++it ) {
  207. if ( key_equals_(key, it->first) ) { return it; }
  208. }
  209. return lit->end();
  210. }
  211. template <typename Iterator, typename Bucket>
  212. Iterator find_impl(Bucket & bkt, key_type const & key) const {
  213. auto lookup = lookup_impl(bkt, key);
  214. if (lookup.second == lookup.first->end()) {
  215. return Iterator{};
  216. } else {
  217. return { ::iterator::in_place, make_end_aware_iterator(lookup.first, bkt.end()), make_end_aware_iterator(lookup.second, lookup.first->end()) };
  218. }
  219. }
  220. iterator unconst_iterator(const_iterator it) {
  221. auto lit = std::get<0>(it);
  222. auto iter = std::get<1>(it);
  223. if ( lit.done() ) { return end(); }
  224. auto nit = buckets_.begin();
  225. std::advance(nit, std::distance(buckets_.cbegin(), lit.current()));
  226. return { ::iterator::in_place, make_end_aware_iterator(nit, buckets_.end()), make_end_aware_iterator(nit->erase(iter.current(), iter.current()), nit->end()) };
  227. }
  228. iterator erase_impl(iterator it) {
  229. auto b = std::get<0>(it);
  230. auto l = std::get<1>(it);
  231. if ( b.done() || l.done() ) { return it; }
  232. --size_;
  233. return { ::iterator::in_place, make_end_aware_iterator(b.current(), b.end()), make_end_aware_iterator(b->erase(l.current()), l.end()) };
  234. }
  235. private: // members
  236. impl_type buckets_{default_buckets};
  237. hasher hasher_;
  238. key_equal key_equals_;
  239. //allocator_type alloc_;
  240. size_type size_{0};
  241. float max_load_{default_load_factor};
  242. };