Skip to content

Commit b11efea

Browse files
authored
Merge pull request #12 from rigtorp/fix/api-compatibility
Fix C++20 iterator concepts and std::unordered_map API gaps
2 parents 16d0eea + c65c5ad commit b11efea

2 files changed

Lines changed: 102 additions & 20 deletions

File tree

‎include/rigtorp/HashMap.h‎

Lines changed: 42 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,11 @@ probing.
2626

2727
#include <algorithm>
2828
#include <cstddef>
29+
#include <iterator>
30+
#include <memory>
2931
#include <stdexcept>
32+
#include <type_traits>
33+
#include <utility>
3034
#include <vector>
3135

3236
namespace rigtorp {
@@ -65,6 +69,9 @@ class HashMap {
6569
using allocator_type = Allocator;
6670
using reference = value_type &;
6771
using const_reference = const value_type &;
72+
using pointer = typename std::allocator_traits<allocator_type>::pointer;
73+
using const_pointer =
74+
typename std::allocator_traits<allocator_type>::const_pointer;
6875
using buckets = std::vector<value_type, allocator_type>;
6976

7077
template <typename ContT, typename IterVal> struct hm_iterator {
@@ -74,39 +81,52 @@ class HashMap {
7481
using reference = value_type &;
7582
using iterator_category = std::forward_iterator_tag;
7683

84+
hm_iterator() = default;
85+
7786
bool operator==(const hm_iterator &other) const {
7887
return other.hm_ == hm_ && other.idx_ == idx_;
7988
}
80-
bool operator!=(const hm_iterator &other) const {
81-
return !(other == *this);
82-
}
8389

8490
hm_iterator &operator++() {
8591
++idx_;
8692
advance_past_empty();
8793
return *this;
8894
}
8995

96+
hm_iterator operator++(int) {
97+
auto tmp = *this;
98+
++(*this);
99+
return tmp;
100+
}
101+
90102
reference operator*() const { return hm_->buckets_[idx_]; }
91103
pointer operator->() const { return &hm_->buckets_[idx_]; }
92104

93-
private:
94-
explicit hm_iterator(ContT *hm) : hm_(hm) { advance_past_empty(); }
95-
explicit hm_iterator(ContT *hm, size_type idx) : hm_(hm), idx_(idx) {}
96105
template <typename OtherContT, typename OtherIterVal>
106+
requires std::is_convertible_v<OtherContT *, ContT *> &&
107+
std::is_convertible_v<OtherIterVal *, IterVal *>
97108
hm_iterator(const hm_iterator<OtherContT, OtherIterVal> &other)
98109
: hm_(other.hm_), idx_(other.idx_) {}
99110

111+
private:
112+
explicit hm_iterator(ContT *hm) : hm_(hm) { advance_past_empty(); }
113+
explicit hm_iterator(ContT *hm, size_type idx) : hm_(hm), idx_(idx) {}
114+
100115
void advance_past_empty() {
101116
while (idx_ < hm_->buckets_.size() &&
102117
key_equal()(hm_->buckets_[idx_].first, hm_->empty_key_)) {
103118
++idx_;
104119
}
120+
if (idx_ == hm_->buckets_.size()) {
121+
hm_ = nullptr;
122+
idx_ = 0;
123+
}
105124
}
106125

107126
ContT *hm_ = nullptr;
108127
typename ContT::size_type idx_ = 0;
109128
friend ContT;
129+
template <typename, typename> friend struct hm_iterator;
110130
};
111131

112132
using iterator = hm_iterator<HashMap, value_type>;
@@ -141,15 +161,11 @@ class HashMap {
141161

142162
const_iterator cbegin() const noexcept { return const_iterator(this); }
143163

144-
iterator end() noexcept { return iterator(this, buckets_.size()); }
164+
iterator end() noexcept { return iterator(); }
145165

146-
const_iterator end() const noexcept {
147-
return const_iterator(this, buckets_.size());
148-
}
166+
const_iterator end() const noexcept { return const_iterator(); }
149167

150-
const_iterator cend() const noexcept {
151-
return const_iterator(this, buckets_.size());
152-
}
168+
const_iterator cend() const noexcept { return const_iterator(); }
153169

154170
// Capacity
155171
bool empty() const noexcept { return size() == 0; }
@@ -161,7 +177,7 @@ class HashMap {
161177
// Modifiers
162178
void clear() noexcept {
163179
for (auto &b : buckets_) {
164-
if (b.first != empty_key_) {
180+
if (!key_equal()(b.first, empty_key_)) {
165181
b.first = empty_key_;
166182
}
167183
}
@@ -173,15 +189,17 @@ class HashMap {
173189
}
174190

175191
std::pair<iterator, bool> insert(value_type &&value) {
176-
return emplace_impl(value.first, std::move(value.second));
192+
return emplace_impl(std::move(value.first), std::move(value.second));
177193
}
178194

179195
template <typename... Args>
180196
std::pair<iterator, bool> emplace(Args &&...args) {
181197
return emplace_impl(std::forward<Args>(args)...);
182198
}
183199

184-
void erase(iterator it) { erase_impl(it); }
200+
void erase(iterator it) { erase_at(it); }
201+
202+
void erase(const_iterator it) { erase_at(it); }
185203

186204
size_type erase(const key_type &key) { return erase_impl(key); }
187205

@@ -208,6 +226,10 @@ class HashMap {
208226
return emplace_impl(key).first->second;
209227
}
210228

229+
mapped_type &operator[](key_type &&key) {
230+
return emplace_impl(std::move(key)).first->second;
231+
}
232+
211233
size_type count(const key_type &key) const { return count_impl(key); }
212234

213235
template <typename K> size_type count(const K &x) const {
@@ -249,15 +271,15 @@ class HashMap {
249271

250272
private:
251273
template <typename K, typename... Args>
252-
std::pair<iterator, bool> emplace_impl(const K &key, Args &&...args) {
274+
std::pair<iterator, bool> emplace_impl(K &&key, Args &&...args) {
253275
if (key_equal()(empty_key_, key)) [[unlikely]] {
254276
detail::throw_empty_key();
255277
}
256278
reserve(size_ + 1);
257279
for (size_t idx = key_to_idx(key);; idx = probe_next(idx)) {
258280
if (key_equal()(buckets_[idx].first, empty_key_)) {
259281
buckets_[idx].second = mapped_type(std::forward<Args>(args)...);
260-
buckets_[idx].first = key;
282+
buckets_[idx].first = std::forward<K>(key);
261283
size_++;
262284
return {iterator(this, idx), true};
263285
} else if (key_equal()(buckets_[idx].first, key)) {
@@ -266,7 +288,7 @@ class HashMap {
266288
}
267289
}
268290

269-
void erase_impl(iterator it) {
291+
void erase_at(const_iterator it) {
270292
size_t bucket = it.idx_;
271293
for (size_t idx = probe_next(bucket);; idx = probe_next(idx)) {
272294
if (key_equal()(buckets_[idx].first, empty_key_)) {
@@ -286,7 +308,7 @@ class HashMap {
286308
template <typename K> size_type erase_impl(const K &key) {
287309
auto it = find_impl(key);
288310
if (it != end()) {
289-
erase_impl(it);
311+
erase_at(it);
290312
return 1;
291313
}
292314
return 0;

‎src/HashMapTest.cpp‎

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@
33

44
#include <algorithm>
55
#include <array>
6+
#include <iterator>
7+
#include <ranges>
68
#include <string>
9+
#include <type_traits>
710

811
#include <rigtorp/HashMap.h>
912

@@ -571,6 +574,63 @@ int main(int argc, char *argv[]) {
571574
EXPECT(throws_invalid_arg([&]() { hm.erase(0); }));
572575
}
573576

577+
// C++20 iterator/range concepts, erase(const_iterator), operator[](&&),
578+
// clear()
579+
{
580+
using Map = HashMap<int, int>;
581+
static_assert(std::forward_iterator<Map::iterator>);
582+
static_assert(std::forward_iterator<Map::const_iterator>);
583+
static_assert(std::ranges::forward_range<Map>);
584+
static_assert(std::ranges::forward_range<const Map>);
585+
static_assert(std::is_convertible_v<Map::iterator, Map::const_iterator>);
586+
static_assert(!std::is_convertible_v<Map::const_iterator, Map::iterator>);
587+
static_assert(std::is_same_v<Map::pointer, Map::value_type *>);
588+
static_assert(std::is_same_v<Map::const_pointer, const Map::value_type *>);
589+
590+
Map::iterator default_it;
591+
Map::iterator default_it2;
592+
EXPECT(default_it == default_it2);
593+
594+
Map hm(16, 0);
595+
EXPECT(default_it == hm.end());
596+
EXPECT(hm.cbegin() == Map::const_iterator());
597+
hm[1] = 10;
598+
hm[2] = 20;
599+
auto it = hm.begin();
600+
auto prev = it++;
601+
EXPECT(prev != it);
602+
EXPECT(it != hm.end());
603+
604+
hm.erase(hm.cbegin());
605+
EXPECT(hm.size() == 1);
606+
607+
HashMap<std::string, int> sm(16, "");
608+
std::string k = "hello";
609+
sm[std::move(k)] = 42;
610+
EXPECT(sm["hello"] == 42);
611+
}
612+
613+
// clear() uses key_equal rather than operator!=
614+
{
615+
struct CustomKey {
616+
int id = 0;
617+
};
618+
struct CustomHash {
619+
size_t operator()(const CustomKey &k) const {
620+
return std::hash<int>{}(k.id);
621+
}
622+
};
623+
struct CustomEqual {
624+
bool operator()(const CustomKey &a, const CustomKey &b) const {
625+
return a.id == b.id;
626+
}
627+
};
628+
HashMap<CustomKey, int, CustomHash, CustomEqual> cm(16, CustomKey{0});
629+
cm.emplace(CustomKey{1}, 10);
630+
cm.clear();
631+
EXPECT(cm.empty());
632+
}
633+
574634
if (!ok) {
575635
fprintf(stderr, "FAILED!\n");
576636
}

0 commit comments

Comments
 (0)