Skip to content

Commit ac2b97d

Browse files
committed
Throw std::invalid_argument when empty key is used
1 parent e9f909f commit ac2b97d

3 files changed

Lines changed: 44 additions & 4 deletions

File tree

‎README.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ main differences are:
2626
- A key value to represent the empty key is required.
2727
- `Key` and `T` needs to be default constructible.
2828
- Iterators are invalidated on all modifying operations.
29-
- It's invalid to perform any operations with the empty key.
29+
- Performing operations with the empty key throws `std::invalid_argument`.
3030
- Destructors are not called on `erase`.
3131
- Extensions for lookups using related key types.
3232

‎include/rigtorp/HashMap.h‎

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,24 @@ probing.
2525
#pragma once
2626

2727
#include <algorithm>
28-
#include <cassert>
2928
#include <cstddef>
3029
#include <stdexcept>
3130
#include <vector>
3231

3332
namespace rigtorp {
3433

34+
namespace detail {
35+
[[noreturn]]
36+
#if defined(_MSC_VER)
37+
__declspec(noinline)
38+
#elif defined(__GNUC__) || defined(__clang__)
39+
__attribute__((noinline))
40+
#endif
41+
inline void throw_empty_key() {
42+
throw std::invalid_argument("HashMap: empty key cannot be used");
43+
}
44+
} // namespace detail
45+
3546
template <typename Key, typename T, typename Hash = std::hash<Key>,
3647
typename KeyEqual = std::equal_to<void>,
3748
typename Allocator = std::allocator<std::pair<Key, T>>>
@@ -231,7 +242,9 @@ class HashMap {
231242
private:
232243
template <typename K, typename... Args>
233244
std::pair<iterator, bool> emplace_impl(const K &key, Args &&...args) {
234-
assert(!key_equal()(empty_key_, key) && "empty key shouldn't be used");
245+
if (key_equal()(empty_key_, key)) [[unlikely]] {
246+
detail::throw_empty_key();
247+
}
235248
reserve(size_ + 1);
236249
for (size_t idx = key_to_idx(key);; idx = probe_next(idx)) {
237250
if (key_equal()(buckets_[idx].first, empty_key_)) {
@@ -288,7 +301,9 @@ class HashMap {
288301
}
289302

290303
template <typename K> iterator find_impl(const K &key) {
291-
assert(!key_equal()(empty_key_, key) && "empty key shouldn't be used");
304+
if (key_equal()(empty_key_, key)) [[unlikely]] {
305+
detail::throw_empty_key();
306+
}
292307
for (size_t idx = key_to_idx(key);; idx = probe_next(idx)) {
293308
if (key_equal()(buckets_[idx].first, key)) {
294309
return iterator(this, idx);

‎src/HashMapTest.cpp‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -546,6 +546,31 @@ int main(int argc, char *argv[]) {
546546
EXPECT(chm.bucket_count() == 32);
547547
}
548548

549+
// Empty key throws std::invalid_argument
550+
{
551+
HashMap<int, int> hm(16, 0);
552+
const auto &chm = hm;
553+
auto throws_invalid_arg = [](auto &&fn) {
554+
try {
555+
fn();
556+
} catch (const std::invalid_argument &) {
557+
return true;
558+
} catch (...) {
559+
return false;
560+
}
561+
return false;
562+
};
563+
EXPECT(throws_invalid_arg([&]() { hm.insert({0, 1}); }));
564+
EXPECT(throws_invalid_arg([&]() { hm.emplace(0, 1); }));
565+
EXPECT(throws_invalid_arg([&]() { hm[0] = 1; }));
566+
EXPECT(throws_invalid_arg([&]() { hm.find(0); }));
567+
EXPECT(throws_invalid_arg([&]() { chm.find(0); }));
568+
EXPECT(throws_invalid_arg([&]() { hm.count(0); }));
569+
EXPECT(throws_invalid_arg([&]() { hm.at(0); }));
570+
EXPECT(throws_invalid_arg([&]() { chm.at(0); }));
571+
EXPECT(throws_invalid_arg([&]() { hm.erase(0); }));
572+
}
573+
549574
if (!ok) {
550575
fprintf(stderr, "FAILED!\n");
551576
}

0 commit comments

Comments
 (0)