Skip to content

Faster hashmap #212

@Gullesnuffs

Description

@Gullesnuffs

The following was >2x faster than std::unordered_map for https://codeforces.com/gym/103861/problem/B and the pbds hash map was also terrible (slow construction time? too much memory use?):

typedef uint64_t ull;                                                                                                                   
template<class T>
struct HashMap {
    int b;
    vector<pair<ull, T>> v;
    HashMap(int b) : b(b), v(1 << b) {}
    T& operator[](ull x) {
        ull y = x >> (64 - b), m = (1 << b) - 1;
        while (v[y].first && v[y].first != x) ++y &= m;
        v[y].first = x;
        return v[y].second;
    }   
};

Maybe worth adding to KACTL instead of the pbds one?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions