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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ main differences are:
- A key value to represent the empty key is required.
- `Key` and `T` needs to be default constructible.
- Iterators are invalidated on all modifying operations.
- It's invalid to perform any operations with the empty key.
- Performing operations with the empty key throws `std::invalid_argument`.
- Destructors are not called on `erase`.
- Extensions for lookups using related key types.

Expand Down
33 changes: 28 additions & 5 deletions include/rigtorp/HashMap.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,32 @@ probing.
#pragma once

#include <algorithm>
#include <cassert>
#include <cstddef>
#include <stdexcept>
#include <vector>

namespace rigtorp {

namespace detail {
#if defined(_MSC_VER)
#define RIGTORP_HASHMAP_NOINLINE __declspec(noinline)
#elif defined(__GNUC__) || defined(__clang__)
#define RIGTORP_HASHMAP_NOINLINE __attribute__((noinline))
#else
#define RIGTORP_HASHMAP_NOINLINE
#endif

[[noreturn]] RIGTORP_HASHMAP_NOINLINE inline void throw_empty_key() {
throw std::invalid_argument("HashMap: empty key cannot be used");
}

[[noreturn]] RIGTORP_HASHMAP_NOINLINE inline void throw_out_of_range() {
throw std::out_of_range("HashMap::at");
}

#undef RIGTORP_HASHMAP_NOINLINE
} // namespace detail

template <typename Key, typename T, typename Hash = std::hash<Key>,
typename KeyEqual = std::equal_to<void>,
typename Allocator = std::allocator<std::pair<Key, T>>>
Expand Down Expand Up @@ -231,7 +250,9 @@ class HashMap {
private:
template <typename K, typename... Args>
std::pair<iterator, bool> emplace_impl(const K &key, Args &&...args) {
assert(!key_equal()(empty_key_, key) && "empty key shouldn't be used");
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_)) {
Expand Down Expand Up @@ -273,10 +294,10 @@ class HashMap {

template <typename K> mapped_type &at_impl(const K &key) {
iterator it = find_impl(key);
if (it != end()) {
if (it != end()) [[likely]] {
return it->second;
}
throw std::out_of_range("HashMap::at");
detail::throw_out_of_range();
}

template <typename K> const mapped_type &at_impl(const K &key) const {
Expand All @@ -288,7 +309,9 @@ class HashMap {
}

template <typename K> iterator find_impl(const K &key) {
assert(!key_equal()(empty_key_, key) && "empty key shouldn't be used");
if (key_equal()(empty_key_, key)) [[unlikely]] {
detail::throw_empty_key();
}
for (size_t idx = key_to_idx(key);; idx = probe_next(idx)) {
if (key_equal()(buckets_[idx].first, key)) {
return iterator(this, idx);
Expand Down
25 changes: 25 additions & 0 deletions src/HashMapTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -546,6 +546,31 @@ int main(int argc, char *argv[]) {
EXPECT(chm.bucket_count() == 32);
}

// Empty key throws std::invalid_argument
{
HashMap<int, int> hm(16, 0);
const auto &chm = hm;
auto throws_invalid_arg = [](auto &&fn) {
try {
fn();
} catch (const std::invalid_argument &) {
return true;
} catch (...) {
return false;
}
return false;
};
EXPECT(throws_invalid_arg([&]() { hm.insert({0, 1}); }));
EXPECT(throws_invalid_arg([&]() { hm.emplace(0, 1); }));
EXPECT(throws_invalid_arg([&]() { hm[0] = 1; }));
EXPECT(throws_invalid_arg([&]() { hm.find(0); }));
EXPECT(throws_invalid_arg([&]() { chm.find(0); }));
EXPECT(throws_invalid_arg([&]() { hm.count(0); }));
EXPECT(throws_invalid_arg([&]() { hm.at(0); }));
EXPECT(throws_invalid_arg([&]() { chm.at(0); }));
EXPECT(throws_invalid_arg([&]() { hm.erase(0); }));
}

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