Skip to content

Commit 16d0eea

Browse files
authored
Merge pull request #11 from rigtorp/feat/throw-on-empty-key
Throw std::invalid_argument when empty key is used
2 parents e9f909f + c002215 commit 16d0eea

3 files changed

Lines changed: 54 additions & 6 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: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,32 @@ 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+
#if defined(_MSC_VER)
36+
#define RIGTORP_HASHMAP_NOINLINE __declspec(noinline)
37+
#elif defined(__GNUC__) || defined(__clang__)
38+
#define RIGTORP_HASHMAP_NOINLINE __attribute__((noinline))
39+
#else
40+
#define RIGTORP_HASHMAP_NOINLINE
41+
#endif
42+
43+
[[noreturn]] RIGTORP_HASHMAP_NOINLINE inline void throw_empty_key() {
44+
throw std::invalid_argument("HashMap: empty key cannot be used");
45+
}
46+
47+
[[noreturn]] RIGTORP_HASHMAP_NOINLINE inline void throw_out_of_range() {
48+
throw std::out_of_range("HashMap::at");
49+
}
50+
51+
#undef RIGTORP_HASHMAP_NOINLINE
52+
} // namespace detail
53+
3554
template <typename Key, typename T, typename Hash = std::hash<Key>,
3655
typename KeyEqual = std::equal_to<void>,
3756
typename Allocator = std::allocator<std::pair<Key, T>>>
@@ -231,7 +250,9 @@ class HashMap {
231250
private:
232251
template <typename K, typename... Args>
233252
std::pair<iterator, bool> emplace_impl(const K &key, Args &&...args) {
234-
assert(!key_equal()(empty_key_, key) && "empty key shouldn't be used");
253+
if (key_equal()(empty_key_, key)) [[unlikely]] {
254+
detail::throw_empty_key();
255+
}
235256
reserve(size_ + 1);
236257
for (size_t idx = key_to_idx(key);; idx = probe_next(idx)) {
237258
if (key_equal()(buckets_[idx].first, empty_key_)) {
@@ -273,10 +294,10 @@ class HashMap {
273294

274295
template <typename K> mapped_type &at_impl(const K &key) {
275296
iterator it = find_impl(key);
276-
if (it != end()) {
297+
if (it != end()) [[likely]] {
277298
return it->second;
278299
}
279-
throw std::out_of_range("HashMap::at");
300+
detail::throw_out_of_range();
280301
}
281302

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

290311
template <typename K> iterator find_impl(const K &key) {
291-
assert(!key_equal()(empty_key_, key) && "empty key shouldn't be used");
312+
if (key_equal()(empty_key_, key)) [[unlikely]] {
313+
detail::throw_empty_key();
314+
}
292315
for (size_t idx = key_to_idx(key);; idx = probe_next(idx)) {
293316
if (key_equal()(buckets_[idx].first, key)) {
294317
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)