Skip to content

Commit

Permalink
Detect bad initialization when not under -DNDEBUG
Browse files Browse the repository at this point in the history
* initialization list too short or too long
* duplicate keys for unordered_set/unordered_map
  • Loading branch information
serge-sans-paille committed Jan 16, 2025
1 parent 765b0e7 commit a691c16
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 4 deletions.
2 changes: 1 addition & 1 deletion include/frozen/bits/basic_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ class carray {

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

public:
Expand Down
10 changes: 9 additions & 1 deletion include/frozen/bits/pmh.h
Original file line number Diff line number Diff line change
Expand Up @@ -184,15 +184,23 @@ struct pmh_tables : private Hasher {
};

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

#ifndef NDEBUG
// Step 1.5: Detect redundant keys.
for(auto const& bucket : step_one.buckets)
for(std::size_t i = 1; i < bucket.size(); ++i)
constexpr_assert(!equal(key(items[0]), key(items[i])), "unique keys");
#endif

// Step 2: Sort the buckets to process the ones with the most items first.
auto buckets = step_one.get_sorted_buckets();

Expand Down
2 changes: 1 addition & 1 deletion include/frozen/unordered_map.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ class unordered_map : private KeyEqual {
, items_{items}
, tables_{
bits::make_pmh_tables<storage_size>(
items_, hash, bits::GetKey{}, default_prg_t{})} {}
items_, hash, equal, bits::GetKey{}, default_prg_t{})} {}
explicit constexpr unordered_map(container_type items)
: unordered_map{items, Hash{}, KeyEqual{}} {
}
Expand Down
2 changes: 1 addition & 1 deletion include/frozen/unordered_set.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ class unordered_set : private KeyEqual {
: KeyEqual{equal}
, keys_{keys}
, tables_{bits::make_pmh_tables<storage_size>(
keys_, hash, bits::Get{}, default_prg_t{})} {}
keys_, hash, equal, bits::Get{}, default_prg_t{})} {}
explicit constexpr unordered_set(container_type keys)
: unordered_set{keys, Hash{}, KeyEqual{}} {}

Expand Down

0 comments on commit a691c16

Please sign in to comment.