Skip to content

Commit 15f8253

Browse files
committed
fix custom_hash
1 parent 0ba9ba0 commit 15f8253

File tree

3 files changed

+28
-24
lines changed

3 files changed

+28
-24
lines changed

data_structure/test/assosiative_array.test.cpp

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,7 @@
11
#define PROBLEM "https://judge.yosupo.jp/problem/associative_array"
2+
#include "../../random/custom_hash.hpp"
23
#include <iostream>
3-
#include <unordered_map>
44

5-
#include <chrono>
6-
struct custom_hash {
7-
// <https://codeforces.com/blog/entry/62393>
8-
static uint64_t splitmix64(uint64_t x) {
9-
// http://xorshift.di.unimi.it/splitmix64.c
10-
x += 0x9e3779b97f4a7c15;
11-
x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
12-
x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
13-
return x ^ (x >> 31);
14-
}
15-
16-
size_t operator()(uint64_t x) const {
17-
static const uint64_t FIXED_RANDOM =
18-
std::chrono::steady_clock::now().time_since_epoch().count();
19-
return splitmix64(x + FIXED_RANDOM);
20-
}
21-
};
225
#include <ext/pb_ds/assoc_container.hpp>
236
using namespace __gnu_pbds;
247

random/custom_hash.hpp

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#pragma once
22
#include <chrono>
33
#include <cstdlib>
4+
#include <functional>
45

56
struct custom_hash {
67
// https://codeforces.com/blog/entry/62393
@@ -19,10 +20,19 @@ struct custom_hash {
1920
}
2021
};
2122

22-
// Usage
23-
#include <unordered_map>
24-
std::unordered_map<int, int, custom_hash> robust_unordered_map;
23+
// Template of std::hash for arbitrary structs
24+
// template <> struct std::hash<T> {
25+
// std::size_t operator()(const T &x) const noexcept {
26+
// static custom_hash h;
27+
// return h(/* */);
28+
// }
29+
// };
2530

26-
#include <ext/pb_ds/assoc_container.hpp>
27-
using namespace __gnu_pbds;
28-
gp_hash_table<int, null_type, custom_hash> robust_hash_table; // fast unordered_set / unordered_map
31+
// robust unordered_map
32+
// #include <unordered_map>
33+
// std::unordered_map<int, int, custom_hash> robust_unordered_map;
34+
35+
// fast unordered_set / unordered_map
36+
// #include <ext/pb_ds/assoc_container.hpp>
37+
// using namespace __gnu_pbds;
38+
// gp_hash_table<int, null_type, custom_hash> fast_hash_table;

random/custom_hash.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
---
2+
title: Custom hash functions (各種データ構造のためのハッシュ関数)
3+
documentation_of: ./custom_hash.hpp
4+
---
5+
6+
Codeforces 等で `std::unordered_set<>``std::unordered_map<>` を使用した場合のハッシュ衝突攻撃を防止するハッシュ関数.また,任意の構造体をこれらのキーとして与えるための `std::hash` の特殊化のテンプレートを含む.
7+
8+
## Link
9+
10+
- [Blowing up unordered_map, and how to stop getting hacked on it - Codeforces](https://codeforces.com/blog/entry/62393)
11+
- [<unordered_set> [🟢C++20 対応]|競プロのための標準 C++](https://zenn.dev/reputeless/books/standard-cpp-for-competitive-programming/viewer/unordered_set)

0 commit comments

Comments
 (0)