Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 42 additions & 20 deletions include/rigtorp/HashMap.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,11 @@ probing.

#include <algorithm>
#include <cstddef>
#include <iterator>
#include <memory>
#include <stdexcept>
#include <type_traits>
#include <utility>
#include <vector>

namespace rigtorp {
Expand Down Expand Up @@ -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<allocator_type>::pointer;
using const_pointer =
typename std::allocator_traits<allocator_type>::const_pointer;
using buckets = std::vector<value_type, allocator_type>;

template <typename ContT, typename IterVal> struct hm_iterator {
Expand All @@ -74,39 +81,52 @@ 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_;
advance_past_empty();
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 <typename OtherContT, typename OtherIterVal>
requires std::is_convertible_v<OtherContT *, ContT *> &&
std::is_convertible_v<OtherIterVal *, IterVal *>
hm_iterator(const hm_iterator<OtherContT, OtherIterVal> &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 <typename, typename> friend struct hm_iterator;
};

using iterator = hm_iterator<HashMap, value_type>;
Expand Down Expand Up @@ -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; }
Expand All @@ -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_;
}
}
Expand All @@ -173,15 +189,17 @@ class HashMap {
}

std::pair<iterator, bool> 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 <typename... Args>
std::pair<iterator, bool> emplace(Args &&...args) {
return emplace_impl(std::forward<Args>(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); }

Expand All @@ -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 <typename K> size_type count(const K &x) const {
Expand Down Expand Up @@ -249,15 +271,15 @@ class HashMap {

private:
template <typename K, typename... Args>
std::pair<iterator, bool> emplace_impl(const K &key, Args &&...args) {
std::pair<iterator, bool> emplace_impl(K &&key, Args &&...args) {
if (key_equal()(empty_key_, key)) [[unlikely]] {
detail::throw_empty_key();
}
reserve(size_ + 1);
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>(args)...);
buckets_[idx].first = key;
buckets_[idx].first = std::forward<K>(key);
size_++;
return {iterator(this, idx), true};
} else if (key_equal()(buckets_[idx].first, key)) {
Expand All @@ -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_)) {
Expand All @@ -286,7 +308,7 @@ class HashMap {
template <typename K> 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;
Expand Down
60 changes: 60 additions & 0 deletions src/HashMapTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@

#include <algorithm>
#include <array>
#include <iterator>
#include <ranges>
#include <string>
#include <type_traits>

#include <rigtorp/HashMap.h>

Expand Down Expand Up @@ -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<int, int>;
static_assert(std::forward_iterator<Map::iterator>);
static_assert(std::forward_iterator<Map::const_iterator>);
static_assert(std::ranges::forward_range<Map>);
static_assert(std::ranges::forward_range<const Map>);
static_assert(std::is_convertible_v<Map::iterator, Map::const_iterator>);
static_assert(!std::is_convertible_v<Map::const_iterator, Map::iterator>);
static_assert(std::is_same_v<Map::pointer, Map::value_type *>);
static_assert(std::is_same_v<Map::const_pointer, const Map::value_type *>);

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<std::string, int> 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<int>{}(k.id);
}
};
struct CustomEqual {
bool operator()(const CustomKey &a, const CustomKey &b) const {
return a.id == b.id;
}
};
HashMap<CustomKey, int, CustomHash, CustomEqual> cm(16, CustomKey{0});
cm.emplace(CustomKey{1}, 10);
cm.clear();
EXPECT(cm.empty());
}

if (!ok) {
fprintf(stderr, "FAILED!\n");
}
Expand Down
Loading