maomao90's Library
A C++20 library for competitive programming.
Loading...
Searching...
No Matches
hashing.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <array>
4#include <chrono>
5#include <random>
6
9
11using namespace std;
12const int MIN_HASH_BASE = 128;
13static mt19937_64
14 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
15template <typename mint, size_t num_bases>
16constexpr array<mint, num_bases> gen_bases() {
17 array<mint, num_bases> res;
18 for (int i = 0; i < num_bases; i++) {
19 res[i] = mint::raw(rng() % (mint::umod() - MIN_HASH_BASE) + MIN_HASH_BASE);
20 }
21 return res;
22}
23template <typename mint, size_t num_bases>
24constexpr array<mint, num_bases>
25gen_inverse(const array<mint, num_bases> &bases) {
26 array<mint, num_bases> res;
27 for (int i = 0; i < num_bases; i++) {
28 res[i] = bases[i].inv();
29 }
30 return res;
31}
32template <typename mint, size_t num_bases, size_t CACHE>
33constexpr array<array<mint, CACHE>, num_bases>
34init_power(const array<mint, num_bases> &bases) {
35 array<array<mint, CACHE>, num_bases> res;
36 for (int i = 0; i < num_bases; i++) {
37 res[i][0] = 1;
38 for (int j = 1; j < CACHE; j++) {
39 res[i][j] = res[i][j - 1] * bases[i];
40 }
41 }
42 return res;
43}
44
45template <typename T> unsigned long long hash_function(const T &x) {
46 static unsigned long long r =
47 chrono::high_resolution_clock::now().time_since_epoch().count();
48 constexpr unsigned long long z1 = 11995408973635179863ull;
49 if constexpr (internal::type_traits::is_broadly_integral_v<T>) {
50 return ((unsigned long long)x ^ r) * z1;
51 } else if constexpr (internal::type_traits::is_pair_v<T>) {
52 constexpr unsigned long long z2 = 10150724397891781847ull;
53 return hash_function(x.first) + hash_function(x.second) * z2;
54 } else if constexpr (internal::concepts::Iterable<T>) {
55 constexpr unsigned long long mod = (1ll << 61) - 1;
56 constexpr unsigned long long base = 950699498548472943ull;
57 unsigned long long m = 0;
58 for (auto &i : x) {
59 unsigned long long v = hash_function(i);
60 unsigned __int128 r = (unsigned __int128)m * base + (v & mod);
61 m = (r & mod) + (r >> 61);
62 if (m >= mod) {
63 m -= mod;
64 }
65 }
66 m ^= m << 24;
67 m ^= m >> 31;
68 m ^= m << 35;
69 return m;
70 }
71}
72
73template <typename T> struct HashObject {
74 constexpr size_t operator()(const T &o) const { return hash_function(o); }
75};
76} // namespace maomao90::internal::hashing
Definition concepts.hpp:40
Definition hashing.hpp:10
constexpr array< mint, num_bases > gen_inverse(const array< mint, num_bases > &bases)
Definition hashing.hpp:25
unsigned long long hash_function(const T &x)
Definition hashing.hpp:45
const int MIN_HASH_BASE
Definition hashing.hpp:12
constexpr array< mint, num_bases > gen_bases()
Definition hashing.hpp:16
constexpr array< array< mint, CACHE >, num_bases > init_power(const array< mint, num_bases > &bases)
Definition hashing.hpp:34
constexpr size_t operator()(const T &o) const
Definition hashing.hpp:74