Hash Map

Normal+ 4 Exclusive 15s 512 MB
Data StructuresMaps
Part 1

Insert, find, erase

Open

Implement 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.

C++
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-constructed Hash, whose call operator returns std::size_t. One of the key types in the tests provides operator== 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.
  • insert never overwrites: inserting a key that is present returns false and leaves the stored value alone. operator[] constructs a value-initialized V() 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, and reserve(n) makes bucket_count() >= n, so the next inserts up to n entries do not change bucket_count(). reserve never 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 O(1)O(1) 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_map makes 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.

C++
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 true

None. 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, no max_load_factor setter: the maximum load factor is fixed at 1.

  • insert takes a key and a value rather than a pair, and returns only whether the key was new.

  • At most 2⋅1052 \cdot 10^5 entries at once and 8⋅1058 \cdot 10^5 operations per scenario.
Accepted 4/5
Acceptance 80%
Loading editor...
Input
insert-find-erase
Expected output
PASS