bucket_hash_set.hpp 8.0 KB

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