diff --git a/include/rigtorp/HashMap.h b/include/rigtorp/HashMap.h index 3ed6396..c336032 100644 --- a/include/rigtorp/HashMap.h +++ b/include/rigtorp/HashMap.h @@ -26,7 +26,11 @@ probing. #include #include +#include +#include #include +#include +#include #include namespace rigtorp { @@ -65,6 +69,9 @@ class HashMap { using allocator_type = Allocator; using reference = value_type &; using const_reference = const value_type &; + using pointer = typename std::allocator_traits::pointer; + using const_pointer = + typename std::allocator_traits::const_pointer; using buckets = std::vector; template struct hm_iterator { @@ -74,12 +81,11 @@ class HashMap { using reference = value_type &; using iterator_category = std::forward_iterator_tag; + hm_iterator() = default; + bool operator==(const hm_iterator &other) const { return other.hm_ == hm_ && other.idx_ == idx_; } - bool operator!=(const hm_iterator &other) const { - return !(other == *this); - } hm_iterator &operator++() { ++idx_; @@ -87,26 +93,40 @@ class HashMap { return *this; } + hm_iterator operator++(int) { + auto tmp = *this; + ++(*this); + return tmp; + } + reference operator*() const { return hm_->buckets_[idx_]; } pointer operator->() const { return &hm_->buckets_[idx_]; } - private: - explicit hm_iterator(ContT *hm) : hm_(hm) { advance_past_empty(); } - explicit hm_iterator(ContT *hm, size_type idx) : hm_(hm), idx_(idx) {} template + requires std::is_convertible_v && + std::is_convertible_v hm_iterator(const hm_iterator &other) : hm_(other.hm_), idx_(other.idx_) {} + private: + explicit hm_iterator(ContT *hm) : hm_(hm) { advance_past_empty(); } + explicit hm_iterator(ContT *hm, size_type idx) : hm_(hm), idx_(idx) {} + void advance_past_empty() { while (idx_ < hm_->buckets_.size() && key_equal()(hm_->buckets_[idx_].first, hm_->empty_key_)) { ++idx_; } + if (idx_ == hm_->buckets_.size()) { + hm_ = nullptr; + idx_ = 0; + } } ContT *hm_ = nullptr; typename ContT::size_type idx_ = 0; friend ContT; + template friend struct hm_iterator; }; using iterator = hm_iterator; @@ -141,15 +161,11 @@ class HashMap { const_iterator cbegin() const noexcept { return const_iterator(this); } - iterator end() noexcept { return iterator(this, buckets_.size()); } + iterator end() noexcept { return iterator(); } - const_iterator end() const noexcept { - return const_iterator(this, buckets_.size()); - } + const_iterator end() const noexcept { return const_iterator(); } - const_iterator cend() const noexcept { - return const_iterator(this, buckets_.size()); - } + const_iterator cend() const noexcept { return const_iterator(); } // Capacity bool empty() const noexcept { return size() == 0; } @@ -161,7 +177,7 @@ class HashMap { // Modifiers void clear() noexcept { for (auto &b : buckets_) { - if (b.first != empty_key_) { + if (!key_equal()(b.first, empty_key_)) { b.first = empty_key_; } } @@ -173,7 +189,7 @@ class HashMap { } std::pair insert(value_type &&value) { - return emplace_impl(value.first, std::move(value.second)); + return emplace_impl(std::move(value.first), std::move(value.second)); } template @@ -181,7 +197,9 @@ class HashMap { return emplace_impl(std::forward(args)...); } - void erase(iterator it) { erase_impl(it); } + void erase(iterator it) { erase_at(it); } + + void erase(const_iterator it) { erase_at(it); } size_type erase(const key_type &key) { return erase_impl(key); } @@ -208,6 +226,10 @@ class HashMap { return emplace_impl(key).first->second; } + mapped_type &operator[](key_type &&key) { + return emplace_impl(std::move(key)).first->second; + } + size_type count(const key_type &key) const { return count_impl(key); } template size_type count(const K &x) const { @@ -249,7 +271,7 @@ class HashMap { private: template - std::pair emplace_impl(const K &key, Args &&...args) { + std::pair emplace_impl(K &&key, Args &&...args) { if (key_equal()(empty_key_, key)) [[unlikely]] { detail::throw_empty_key(); } @@ -257,7 +279,7 @@ class HashMap { for (size_t idx = key_to_idx(key);; idx = probe_next(idx)) { if (key_equal()(buckets_[idx].first, empty_key_)) { buckets_[idx].second = mapped_type(std::forward(args)...); - buckets_[idx].first = key; + buckets_[idx].first = std::forward(key); size_++; return {iterator(this, idx), true}; } else if (key_equal()(buckets_[idx].first, key)) { @@ -266,7 +288,7 @@ class HashMap { } } - void erase_impl(iterator it) { + void erase_at(const_iterator it) { size_t bucket = it.idx_; for (size_t idx = probe_next(bucket);; idx = probe_next(idx)) { if (key_equal()(buckets_[idx].first, empty_key_)) { @@ -286,7 +308,7 @@ class HashMap { template size_type erase_impl(const K &key) { auto it = find_impl(key); if (it != end()) { - erase_impl(it); + erase_at(it); return 1; } return 0; diff --git a/src/HashMapTest.cpp b/src/HashMapTest.cpp index 2c24db2..40de2dc 100644 --- a/src/HashMapTest.cpp +++ b/src/HashMapTest.cpp @@ -3,7 +3,10 @@ #include #include +#include +#include #include +#include #include @@ -571,6 +574,63 @@ int main(int argc, char *argv[]) { EXPECT(throws_invalid_arg([&]() { hm.erase(0); })); } + // C++20 iterator/range concepts, erase(const_iterator), operator[](&&), + // clear() + { + using Map = HashMap; + static_assert(std::forward_iterator); + static_assert(std::forward_iterator); + static_assert(std::ranges::forward_range); + static_assert(std::ranges::forward_range); + static_assert(std::is_convertible_v); + static_assert(!std::is_convertible_v); + static_assert(std::is_same_v); + static_assert(std::is_same_v); + + Map::iterator default_it; + Map::iterator default_it2; + EXPECT(default_it == default_it2); + + Map hm(16, 0); + EXPECT(default_it == hm.end()); + EXPECT(hm.cbegin() == Map::const_iterator()); + hm[1] = 10; + hm[2] = 20; + auto it = hm.begin(); + auto prev = it++; + EXPECT(prev != it); + EXPECT(it != hm.end()); + + hm.erase(hm.cbegin()); + EXPECT(hm.size() == 1); + + HashMap sm(16, ""); + std::string k = "hello"; + sm[std::move(k)] = 42; + EXPECT(sm["hello"] == 42); + } + + // clear() uses key_equal rather than operator!= + { + struct CustomKey { + int id = 0; + }; + struct CustomHash { + size_t operator()(const CustomKey &k) const { + return std::hash{}(k.id); + } + }; + struct CustomEqual { + bool operator()(const CustomKey &a, const CustomKey &b) const { + return a.id == b.id; + } + }; + HashMap cm(16, CustomKey{0}); + cm.emplace(CustomKey{1}, 10); + cm.clear(); + EXPECT(cm.empty()); + } + if (!ok) { fprintf(stderr, "FAILED!\n"); }