Skip to content

Commit a691c16

Browse files
Detect bad initialization when not under -DNDEBUG
* initialization list too short or too long * duplicate keys for unordered_set/unordered_map
1 parent 765b0e7 commit a691c16

File tree

4 files changed

+12
-4
lines changed

4 files changed

+12
-4
lines changed

include/frozen/bits/basic_types.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ class carray {
9999

100100
static constexpr void check_initializer(std::initializer_list<T> init) {
101101
(void)init;
102-
constexpr_assert(init.size() >= N, "Cannot initialize a carray with an smaller initializer list");
102+
constexpr_assert(init.size() == N, "Cannot initialize a carray with an initializer list of different size.");
103103
}
104104

105105
public:

include/frozen/bits/pmh.h

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,15 +184,23 @@ struct pmh_tables : private Hasher {
184184
};
185185

186186
// Make pmh tables for given items, hash function, prg, etc.
187-
template <std::size_t M, class Item, std::size_t N, class Hash, class Key, class PRG>
187+
template <std::size_t M, class Item, std::size_t N, class Hash, class Key, class KeyEqual, class PRG>
188188
pmh_tables<M, Hash> constexpr make_pmh_tables(const carray<Item, N> &
189189
items,
190190
Hash const &hash,
191+
KeyEqual const &equal,
191192
Key const &key,
192193
PRG prg) {
193194
// Step 1: Place all of the keys into buckets
194195
auto step_one = make_pmh_buckets<M>(items, hash, key, prg);
195196

197+
#ifndef NDEBUG
198+
// Step 1.5: Detect redundant keys.
199+
for(auto const& bucket : step_one.buckets)
200+
for(std::size_t i = 1; i < bucket.size(); ++i)
201+
constexpr_assert(!equal(key(items[0]), key(items[i])), "unique keys");
202+
#endif
203+
196204
// Step 2: Sort the buckets to process the ones with the most items first.
197205
auto buckets = step_one.get_sorted_buckets();
198206

include/frozen/unordered_map.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ class unordered_map : private KeyEqual {
8383
, items_{items}
8484
, tables_{
8585
bits::make_pmh_tables<storage_size>(
86-
items_, hash, bits::GetKey{}, default_prg_t{})} {}
86+
items_, hash, equal, bits::GetKey{}, default_prg_t{})} {}
8787
explicit constexpr unordered_map(container_type items)
8888
: unordered_map{items, Hash{}, KeyEqual{}} {
8989
}

include/frozen/unordered_set.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ class unordered_set : private KeyEqual {
7777
: KeyEqual{equal}
7878
, keys_{keys}
7979
, tables_{bits::make_pmh_tables<storage_size>(
80-
keys_, hash, bits::Get{}, default_prg_t{})} {}
80+
keys_, hash, equal, bits::Get{}, default_prg_t{})} {}
8181
explicit constexpr unordered_set(container_type keys)
8282
: unordered_set{keys, Hash{}, KeyEqual{}} {}
8383

0 commit comments

Comments
 (0)