bucket_hash_set.hpp 8.0 KB

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