diff --git a/data_structure/test/assosiative_array.test.cpp b/data_structure/test/assosiative_array.test.cpp index d9b105bb..fcdc245a 100644 --- a/data_structure/test/assosiative_array.test.cpp +++ b/data_structure/test/assosiative_array.test.cpp @@ -1,24 +1,7 @@ #define PROBLEM "https://judge.yosupo.jp/problem/associative_array" +#include "../../random/custom_hash.hpp" #include -#include -#include -struct custom_hash { - // - static uint64_t splitmix64(uint64_t x) { - // http://xorshift.di.unimi.it/splitmix64.c - x += 0x9e3779b97f4a7c15; - x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9; - x = (x ^ (x >> 27)) * 0x94d049bb133111eb; - return x ^ (x >> 31); - } - - size_t operator()(uint64_t x) const { - static const uint64_t FIXED_RANDOM = - std::chrono::steady_clock::now().time_since_epoch().count(); - return splitmix64(x + FIXED_RANDOM); - } -}; #include using namespace __gnu_pbds; diff --git a/random/custom_hash.hpp b/random/custom_hash.hpp index 7079ba86..865619ac 100644 --- a/random/custom_hash.hpp +++ b/random/custom_hash.hpp @@ -1,6 +1,7 @@ #pragma once #include #include +#include struct custom_hash { // https://codeforces.com/blog/entry/62393 @@ -19,10 +20,19 @@ struct custom_hash { } }; -// Usage -#include -std::unordered_map robust_unordered_map; +// Template of std::hash for arbitrary structs +// template <> struct std::hash { +// std::size_t operator()(const T &x) const noexcept { +// static custom_hash h; +// return h(/* */); +// } +// }; -#include -using namespace __gnu_pbds; -gp_hash_table robust_hash_table; // fast unordered_set / unordered_map +// robust unordered_map +// #include +// std::unordered_map robust_unordered_map; + +// fast unordered_set / unordered_map +// #include +// using namespace __gnu_pbds; +// gp_hash_table fast_hash_table; diff --git a/random/custom_hash.md b/random/custom_hash.md new file mode 100644 index 00000000..d4032e72 --- /dev/null +++ b/random/custom_hash.md @@ -0,0 +1,11 @@ +--- +title: Custom hash functions (各種データ構造のためのハッシュ関数) +documentation_of: ./custom_hash.hpp +--- + +Codeforces 等で `std::unordered_set<>` や `std::unordered_map<>` を使用した場合のハッシュ衝突攻撃を防止するハッシュ関数.また,任意の構造体をこれらのキーとして与えるための `std::hash` の特殊化のテンプレートを含む. + +## Link + +- [Blowing up unordered_map, and how to stop getting hacked on it - Codeforces](https://codeforces.com/blog/entry/62393) +- [ [🟢C++20 対応]|競プロのための標準 C++](https://zenn.dev/reputeless/books/standard-cpp-for-competitive-programming/viewer/unordered_set)