cpp-library

This documentation is automatically generated by competitive-verifier/competitive-verifier

View the Project on GitHub shino16/cpp-library

:heavy_check_mark: ds/hash_map.hpp

Depends on

Required by

Verified with

Code

#pragma once
#include "prelude.hpp"
#include "util/hash.hpp"
#include <ext/pb_ds/assoc_container.hpp>

template <class K, class V, class Hash = anti_hack_hash<K>>
using hash_map = __gnu_pbds::gp_hash_table<K, V, Hash>;
#line 2 "prelude.hpp"
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vi = vector<int>;
using vvi = vector<vector<int>>;
using vll = vector<ll>;
using vvll = vector<vector<ll>>;
using vc = vector<char>;
#define rep2(i, m, n) for (auto i = (m); i < (n); i++)
#define rep(i, n) rep2(i, 0, n)
#define repr2(i, m, n) for (auto i = (n); i-- > (m);)
#define repr(i, n) repr2(i, 0, n)
#define all(x) begin(x), end(x)
auto ndvec(int n, auto e) { return vector(n, e); }
auto ndvec(int n, auto ...e) { return vector(n, ndvec(e...)); }
auto comp_key(auto&& f) { return [&](auto&& a, auto&& b) { return f(a) < f(b); }; }
auto& max(const auto& a, const auto& b) { return a < b ? b : a; }
auto& min(const auto& a, const auto& b) { return b < a ? b : a; }
#if __cpp_lib_ranges
namespace R = std::ranges;
namespace V = std::views;
#endif
#line 3 "util/seed.hpp"

auto seed() {
#if defined(LOCAL) && !defined(NO_FIX_SEED)
  return 314169265258979;
#endif
  return chrono::steady_clock::now().time_since_epoch().count();
}
#line 3 "util/rand.hpp"

uint32_t rand32() {
  static uint32_t x = seed();
  x ^= x << 13;
  x ^= x >> 17;
  x ^= x << 5;
  return x;
}

uint64_t rand64() {
  return uint64_t(rand32()) << 32 | rand32();
}
#line 3 "util/hash.hpp"

[[gnu::const]] 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);
}

template <class T, class = void>
struct anti_hack_hash;

template <class T>
struct anti_hack_hash<T, enable_if_t<is_convertible_v<T, uint64_t>>> {
  size_t operator()(T x) const {
    static const uint64_t ofs = seed();
    return splitmix64((uint64_t)x + ofs);
  }
};

template <class T>
struct anti_hack_hash<T, void_t<decltype(tuple_size<T>::value)>> {
  size_t operator()(const T& x) const {
    return hash_impl(x, make_index_sequence<tuple_size_v<T>>{});
  }

 private:
  static auto make_seed() {
    array<uint64_t, tuple_size_v<T>> res;
    res[0] = seed();
    rep(i, tuple_size_v<T> - 1) res[i + 1] = splitmix64(res[i]) + seed();
    return res;
  }
  template <size_t... Is>
  static size_t hash_impl(const T& x, index_sequence<Is...>) {
    size_t res = 0;
    ((void)(res = splitmix64(
                res + anti_hack_hash<tuple_element_t<Is, T>>{}(get<Is>(x)))),
     ...);
    return res;
  }
};
#line 4 "ds/hash_map.hpp"
#include <ext/pb_ds/assoc_container.hpp>

template <class K, class V, class Hash = anti_hack_hash<K>>
using hash_map = __gnu_pbds::gp_hash_table<K, V, Hash>;
Back to top page