21class hash_index_iterator {
24 std::size_t bucket_index;
25 std::size_t element_index;
30 using reference = ReferenceT;
33 inline hash_index_iterator(
35 std::size_t bucket_index,
36 std::size_t element_index
38 bucket_index(bucket_index),
39 element_index(element_index) {}
41 value_type operator*() {
42 return buckets[bucket_index][element_index];
47 if (bucket_index >= buckets.size()) {
50 if (element_index >= buckets[bucket_index].size()) {
55 if (element_index < buckets[bucket_index].size()) {
63 if (element_index == 0) {
65 element_index = buckets[bucket_index].size();
69 if (element_index < buckets[bucket_index].size()) {
75 bool operator==(
const hash_index_iterator& other)
const {
76 return bucket_index == other.bucket_index
77 && element_index == other.element_index;
95 using bucket = hash_bucket<T, KeyT>;
97 std::hash<KeyT> hasher;
99 std::vector<bucket> buckets;
101 const KeyT& get_key(
const T& element) {
103 return element->first;
109 buckets.reserve(256);
110 for (std::size_t i = 0; i < 256; ++i) {
111 buckets.push_back(bucket {});
115 using value_type = T;
116 using reference = T&;
121 void insert(T element) {
122 const auto& key = get_key(element);
123 const auto h = hasher(key);
124 auto& bucket = buckets[h % buckets.size()];
125 bucket.push_back(element);
128 iterator lookup(
const KeyT& key) {
129 const auto h = hasher(key);
130 const auto bucket_index = h % buckets.size();
131 auto& bucket = buckets[bucket_index];
133 const auto end = bucket.end();
134 for (
auto it = bucket.begin(); it != end; ++it, ++i) {
135 if (get_key(*it) == key) {
142 const_iterator lookup(
const KeyT& key)
const {
143 const auto h = hasher(key);
144 const auto bucket_index = h % buckets.size();
145 auto& bucket = buckets[bucket_index];
147 const auto end = bucket.end();
148 for (
auto it = bucket.begin(); it != end; ++it, ++i) {
149 if (get_key(*it) == key) {
156 iterator begin()
noexcept {
157 return iterator(buckets, 0, 0);
160 iterator end()
noexcept {
161 return iterator(buckets, buckets.size(), 0);
164 const_iterator begin()
const noexcept {
168 const_iterator end()
const noexcept {
172 const_iterator cbegin()
const noexcept {
173 return const_iterator(buckets, 0, 0);
176 const_iterator cend()
const noexcept {
177 return const_iterator(buckets, buckets.size(), 0);