https://github.com/hordi/hash
Fast C++ flat (open addressing) hash set, map
https://github.com/hordi/hash
cpp hash hashing map set unordered-map unordered-set
Last synced: about 1 month ago
JSON representation
Fast C++ flat (open addressing) hash set, map
- Host: GitHub
- URL: https://github.com/hordi/hash
- Owner: hordi
- License: mit
- Created: 2019-02-03T22:35:50.000Z (about 7 years ago)
- Default Branch: master
- Last Pushed: 2026-01-16T23:22:22.000Z (3 months ago)
- Last Synced: 2026-02-14T03:39:02.283Z (about 2 months ago)
- Topics: cpp, hash, hashing, map, set, unordered-map, unordered-set
- Language: C++
- Homepage:
- Size: 2.25 MB
- Stars: 3
- Watchers: 1
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# hash
Fast C++ flat (open addressing) hash set/map header only library. Requred C++11 (only because of "constexpr" and "noexcept" modifiers usage).
Drop in replacement (mostly, references invaildated if reallocation happens, allocator-type is absent) implementation of unordered hash-set and hash-map.
Default hash-functions use actual 32-bits hash-value, makes sense to use if amount of elements less than UINT_MAX/2 for good distribution. In other case - should be used 64-bits result hash-function (hash_set1.hpp supports full range of size_t).
hdr::hash_grow_map_heavy and hdr::hash_grow_set_heavy added for big (sizeof) objects to minimize memory usage and improve iteration speed.
EXAMPLES
Simplest set-map
```cpp
#include "hash_set1.h"
#include
int main() {
hrd1::hash_set ss(1024);
hrd1::hash_map ms(1024);
for (int i = 0; i != 1024; ++i) {
ss.insert(i);
ms[i] = i + 10;
}
hrd1::hash_set ss1(ss.begin(), ss.end());
size_t cnt = 0, cnt2 = 0, cnt3 = 0;
for (int i = 0; i != 1024 * 1024; ++i) {
cnt += ss.count(i);
cnt2 += ms.count(i);
cnt3 += ss1.count(i);
}
std::cout << "found: " << cnt << ':' << cnt2 << ':' << cnt3 << '\n';
return 0;
}
```
Custom "complex" type with own hash-function and equal-operator
```cpp
#include "hash_set.h"
#include
struct Key1 {
Key1(uint64_t k, uint64_t d = 0) :key(k), data(d) {}
bool operator==(const Key1& r) const noexcept {
return key == r.key;
}
uint64_t key;
uint64_t data;
};
template<>
struct hrd::hash_base::hash_ {
size_t operator()(const Key1& r) const noexcept {
return hrd::hash_base::hash_1<8>(&r.key);
}
};
int main() {
hrd::hash_set ss(1024);
for (size_t i = 0; i != 1024; ++i)
ss.insert(Key1(i, i + 10));
size_t cnt = 0, cnt1 = 0;
for (size_t i = 0; i != 1024 * 1024; ++i)
cnt += ss.count(Key1(i));
for (auto i = ss.begin(), e = ss.end(); i != e; ++i)
cnt1++;
std::cout << "found: " << cnt << ':' << cnt << '\n';
return 0;
}
```