Hash Map
Insert, find, erase
OpenImplement every member: insert, operator[], find, contains, erase, size, empty, clear, bucket_count, load_factor, reserve and the destructor.
The tests compare every operation against a reference model, check size() <= bucket_count() after every insert, and count every key and value the map constructs and destroys. One hash sends every key to the same bucket, one key type provides operator== and no other comparison, and one scenario runs 800,000 operations while counting key comparisons.
Implement HashMap<K, V, Hash>, a hash table that mirrors the core of std::unordered_map<K, V, Hash>: unique keys, expected constant-time inserts, lookups and erases, a load factor that never rises above 1, and, in Part 2, references that survive every rehash.
template <class K, class V, class Hash = std::hash<K>>
class HashMap {
public:
HashMap() noexcept;
~HashMap();
HashMap(const HashMap&) = delete;
HashMap& operator=(const HashMap&) = delete;
bool insert(const K& key, const V& value); // false, and no change, if key is present
V& operator[](const K& key); // inserts V() first if key is missing
V* find(const K& key); // nullptr if key is missing
const V* find(const K& key) const;
bool contains(const K& key) const;
bool erase(const K& key); // false if key is missing
std::size_t size() const noexcept;
bool empty() const noexcept;
void clear() noexcept;
std::size_t bucket_count() const noexcept;
float load_factor() const noexcept; // size() / bucket_count(), 0 when bucket_count() is 0
void reserve(std::size_t count); // room for count entries at load factor 1
};- Keys are compared with
operator==and hashed with a default-constructedHash, whose call operator returnsstd::size_t. One of the key types in the tests providesoperator==and a hash and nothing else; every other comparison operator on it fails the test that triggers it. Another hash sends every key to the same bucket. insertnever overwrites: inserting a key that is present returnsfalseand leaves the stored value alone.operator[]constructs a value-initializedV()only when the key is missing, and returns a reference to the stored value either way.- The maximum load factor is 1: after every insert,
size() <= bucket_count(). A table may stay sparser than that. It grows geometrically, andreserve(n)makesbucket_count() >= n, so the next inserts up tonentries do not changebucket_count().reservenever shrinks the table. - Only the entries the map holds exist: each key and value is constructed when its entry is added and destroyed exactly once when it is erased, cleared or destroyed with the map. The tests count both, and abort on a double destroy.
- Every operation takes expected time with a good hash. The tests count the key comparisons a scenario makes and fail it as soon as they outgrow a constant per operation.
- Part 2 adds the guarantee
std::unordered_mapmakes about references: a pointer or reference to an entry's value stays valid until that entry is erased, through any number of rehashes and through other entries' erases.
K and V are copy-constructible and nothrow-move-constructible, with non-throwing destructors, and V is default-constructible.
The harness defines the standard containers away: vector, deque, list, forward_list, array, valarray, map, multimap, set, multiset, unordered_map, unordered_multimap, unordered_set and unordered_multiset may not appear in a submission, member and variable names included. std::hash is fine. Do not write main; the harness owns it. The standard headers are already included.
HashMap<std::string, int> stock;
stock.insert("apples", 3); // true
stock.insert("apples", 9); // false, still 3
stock["pears"] += 2; // inserts "pears" -> 0, then adds 2
*stock.find("pears"); // 2
stock.erase("apples"); // true
stock.contains("apples"); // false
stock.size() <= stock.bucket_count(); // always trueNone. The judge compiles your class into a hidden harness that drives it directly and reports one row per scenario.
None. Your code prints nothing; the harness owns the verdict.
Single-threaded. No iterators, no
rehash, nomax_load_factorsetter: the maximum load factor is fixed at 1.inserttakes a key and a value rather than a pair, and returns only whether the key was new.
- At most entries at once and operations per scenario.
insert-find-erase
PASS