summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorTyge Løvset <[email protected]>2021-03-01 16:26:51 +0100
committerTyge Løvset <[email protected]>2021-03-01 16:26:51 +0100
commitdf259d54a43039f0cd2e4bbb168bf60a8dd97973 (patch)
treefda12d24ad87e0cb2f9f5d471faf55bed993f4b2
parent3add074518345bf3b6b965c3f79bd0d35079891b (diff)
downloadSTC-modified-df259d54a43039f0cd2e4bbb168bf60a8dd97973.tar.gz
STC-modified-df259d54a43039f0cd2e4bbb168bf60a8dd97973.zip
Update exteral maps
-rw-r--r--benchmarks/others/hopscotch_growth_policy.h346
-rw-r--r--benchmarks/others/hopscotch_hash.h1827
-rw-r--r--benchmarks/others/hopscotch_map.h710
-rw-r--r--benchmarks/others/khashl.h345
-rw-r--r--benchmarks/others/skarupke/bytell_hash_map.hpp (renamed from benchmarks/others/bytell_hash_map.hpp)2520
-rw-r--r--benchmarks/others/skarupke/flat_hash_map.hpp (renamed from benchmarks/others/flat_hash_map.hpp)2992
-rw-r--r--benchmarks/others/sparsepp/spp.h5
-rw-r--r--benchmarks/others/tsl/hopscotch_growth_policy.h404
-rw-r--r--benchmarks/others/tsl/hopscotch_hash.h1883
-rw-r--r--benchmarks/others/tsl/hopscotch_map.h735
-rw-r--r--benchmarks/others/update.sh31
-rw-r--r--benchmarks/shootout1_cmap.cpp77
-rw-r--r--benchmarks/shootout2_cmap.cpp23
-rw-r--r--stc/cmap.h7
14 files changed, 5855 insertions, 6050 deletions
diff --git a/benchmarks/others/hopscotch_growth_policy.h b/benchmarks/others/hopscotch_growth_policy.h
deleted file mode 100644
index 8c9f9694..00000000
--- a/benchmarks/others/hopscotch_growth_policy.h
+++ /dev/null
@@ -1,346 +0,0 @@
-/**
- * MIT License
- *
- * Copyright (c) 2018 Thibaut Goetghebuer-Planchon <[email protected]>
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy
- * of this software and associated documentation files (the "Software"), to deal
- * in the Software without restriction, including without limitation the rights
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- * copies of the Software, and to permit persons to whom the Software is
- * furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in all
- * copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
- * SOFTWARE.
- */
-#ifndef TSL_HOPSCOTCH_GROWTH_POLICY_H
-#define TSL_HOPSCOTCH_GROWTH_POLICY_H
-
-
-#include <algorithm>
-#include <array>
-#include <climits>
-#include <cmath>
-#include <cstddef>
-#include <cstdint>
-#include <iterator>
-#include <limits>
-#include <ratio>
-#include <stdexcept>
-
-
-/**
- * Only activate tsl_hh_assert if TSL_DEBUG is defined.
- * This way we avoid the performance hit when NDEBUG is not defined with assert as tsl_hh_assert is used a lot
- * (people usually compile with "-O3" and not "-O3 -DNDEBUG").
- */
-#ifdef TSL_DEBUG
-# define tsl_hh_assert(expr) assert(expr)
-#else
-# define tsl_hh_assert(expr) (static_cast<void>(0))
-#endif
-
-
-/**
- * If exceptions are enabled, throw the exception passed in parameter, otherwise call std::terminate.
- */
-#if (defined(__cpp_exceptions) || defined(__EXCEPTIONS) || (defined (_MSC_VER) && defined (_CPPUNWIND))) && !defined(TSL_NO_EXCEPTIONS)
-# define TSL_HH_THROW_OR_TERMINATE(ex, msg) throw ex(msg)
-#else
-# define TSL_HH_NO_EXCEPTIONS
-# ifdef NDEBUG
-# define TSL_HH_THROW_OR_TERMINATE(ex, msg) std::terminate()
-# else
-# include <iostream>
-# define TSL_HH_THROW_OR_TERMINATE(ex, msg) do { std::cerr << msg << std::endl; std::terminate(); } while(0)
-# endif
-#endif
-
-
-namespace tsl {
-namespace hh {
-
-/**
- * Grow the hash table by a factor of GrowthFactor keeping the bucket count to a power of two. It allows
- * the table to use a mask operation instead of a modulo operation to map a hash to a bucket.
- *
- * GrowthFactor must be a power of two >= 2.
- */
-template<std::size_t GrowthFactor>
-class power_of_two_growth_policy {
-public:
- /**
- * Called on the hash table creation and on rehash. The number of buckets for the table is passed in parameter.
- * This number is a minimum, the policy may update this value with a higher value if needed (but not lower).
- *
- * If 0 is given, min_bucket_count_in_out must still be 0 after the policy creation and
- * bucket_for_hash must always return 0 in this case.
- */
- explicit power_of_two_growth_policy(std::size_t& min_bucket_count_in_out) {
- if(min_bucket_count_in_out > max_bucket_count()) {
- TSL_HH_THROW_OR_TERMINATE(std::length_error, "The hash table exceeds its maximum size.");
- }
-
- if(min_bucket_count_in_out > 0) {
- min_bucket_count_in_out = round_up_to_power_of_two(min_bucket_count_in_out);
- m_mask = min_bucket_count_in_out - 1;
- }
- else {
- m_mask = 0;
- }
- }
-
- /**
- * Return the bucket [0, bucket_count()) to which the hash belongs.
- * If bucket_count() is 0, it must always return 0.
- */
- std::size_t bucket_for_hash(std::size_t hash) const noexcept {
- return hash & m_mask;
- }
-
- /**
- * Return the bucket count to use when the bucket array grows on rehash.
- */
- std::size_t next_bucket_count() const {
- if((m_mask + 1) > max_bucket_count() / GrowthFactor) {
- TSL_HH_THROW_OR_TERMINATE(std::length_error, "The hash table exceeds its maximum size.");
- }
-
- return (m_mask + 1) * GrowthFactor;
- }
-
- /**
- * Return the maximum number of buckets supported by the policy.
- */
- std::size_t max_bucket_count() const {
- // Largest power of two.
- return (std::numeric_limits<std::size_t>::max() / 2) + 1;
- }
-
- /**
- * Reset the growth policy as if it was created with a bucket count of 0.
- * After a clear, the policy must always return 0 when bucket_for_hash is called.
- */
- void clear() noexcept {
- m_mask = 0;
- }
-
-private:
- static std::size_t round_up_to_power_of_two(std::size_t value) {
- if(is_power_of_two(value)) {
- return value;
- }
-
- if(value == 0) {
- return 1;
- }
-
- --value;
- for(std::size_t i = 1; i < sizeof(std::size_t) * CHAR_BIT; i *= 2) {
- value |= value >> i;
- }
-
- return value + 1;
- }
-
- static constexpr bool is_power_of_two(std::size_t value) {
- return value != 0 && (value & (value - 1)) == 0;
- }
-
-private:
- static_assert(is_power_of_two(GrowthFactor) && GrowthFactor >= 2, "GrowthFactor must be a power of two >= 2.");
-
- std::size_t m_mask;
-};
-
-
-/**
- * Grow the hash table by GrowthFactor::num / GrowthFactor::den and use a modulo to map a hash
- * to a bucket. Slower but it can be useful if you want a slower growth.
- */
-template<class GrowthFactor = std::ratio<3, 2>>
-class mod_growth_policy {
-public:
- explicit mod_growth_policy(std::size_t& min_bucket_count_in_out) {
- if(min_bucket_count_in_out > max_bucket_count()) {
- TSL_HH_THROW_OR_TERMINATE(std::length_error, "The hash table exceeds its maximum size.");
- }
-
- if(min_bucket_count_in_out > 0) {
- m_mod = min_bucket_count_in_out;
- }
- else {
- m_mod = 1;
- }
- }
-
- std::size_t bucket_for_hash(std::size_t hash) const noexcept {
- return hash % m_mod;
- }
-
- std::size_t next_bucket_count() const {
- if(m_mod == max_bucket_count()) {
- TSL_HH_THROW_OR_TERMINATE(std::length_error, "The hash table exceeds its maximum size.");
- }
-
- const double next_bucket_count = std::ceil(double(m_mod) * REHASH_SIZE_MULTIPLICATION_FACTOR);
- if(!std::isnormal(next_bucket_count)) {
- TSL_HH_THROW_OR_TERMINATE(std::length_error, "The hash table exceeds its maximum size.");
- }
-
- if(next_bucket_count > double(max_bucket_count())) {
- return max_bucket_count();
- }
- else {
- return std::size_t(next_bucket_count);
- }
- }
-
- std::size_t max_bucket_count() const {
- return MAX_BUCKET_COUNT;
- }
-
- void clear() noexcept {
- m_mod = 1;
- }
-
-private:
- static constexpr double REHASH_SIZE_MULTIPLICATION_FACTOR = 1.0 * GrowthFactor::num / GrowthFactor::den;
- static const std::size_t MAX_BUCKET_COUNT =
- std::size_t(double(
- std::numeric_limits<std::size_t>::max() / REHASH_SIZE_MULTIPLICATION_FACTOR
- ));
-
- static_assert(REHASH_SIZE_MULTIPLICATION_FACTOR >= 1.1, "Growth factor should be >= 1.1.");
-
- std::size_t m_mod;
-};
-
-
-
-namespace detail {
-
-#if SIZE_MAX >= ULLONG_MAX
-#define TSL_HH_NB_PRIMES 51
-#elif SIZE_MAX >= ULONG_MAX
-#define TSL_HH_NB_PRIMES 40
-#else
-#define TSL_HH_NB_PRIMES 23
-#endif
-
-static constexpr const std::array<std::size_t, TSL_HH_NB_PRIMES> PRIMES = {{
- 1u, 5u, 17u, 29u, 37u, 53u, 67u, 79u, 97u, 131u, 193u, 257u, 389u, 521u, 769u, 1031u,
- 1543u, 2053u, 3079u, 6151u, 12289u, 24593u, 49157u,
-#if SIZE_MAX >= ULONG_MAX
- 98317ul, 196613ul, 393241ul, 786433ul, 1572869ul, 3145739ul, 6291469ul, 12582917ul,
- 25165843ul, 50331653ul, 100663319ul, 201326611ul, 402653189ul, 805306457ul, 1610612741ul,
- 3221225473ul, 4294967291ul,
-#endif
-#if SIZE_MAX >= ULLONG_MAX
- 6442450939ull, 12884901893ull, 25769803751ull, 51539607551ull, 103079215111ull, 206158430209ull,
- 412316860441ull, 824633720831ull, 1649267441651ull, 3298534883309ull, 6597069766657ull,
-#endif
-}};
-
-template<unsigned int IPrime>
-static constexpr std::size_t mod(std::size_t hash) { return hash % PRIMES[IPrime]; }
-
-// MOD_PRIME[iprime](hash) returns hash % PRIMES[iprime]. This table allows for faster modulo as the
-// compiler can optimize the modulo code better with a constant known at the compilation.
-static constexpr const std::array<std::size_t(*)(std::size_t), TSL_HH_NB_PRIMES> MOD_PRIME = {{
- &mod<0>, &mod<1>, &mod<2>, &mod<3>, &mod<4>, &mod<5>, &mod<6>, &mod<7>, &mod<8>, &mod<9>, &mod<10>,
- &mod<11>, &mod<12>, &mod<13>, &mod<14>, &mod<15>, &mod<16>, &mod<17>, &mod<18>, &mod<19>, &mod<20>,
- &mod<21>, &mod<22>,
-#if SIZE_MAX >= ULONG_MAX
- &mod<23>, &mod<24>, &mod<25>, &mod<26>, &mod<27>, &mod<28>, &mod<29>, &mod<30>, &mod<31>, &mod<32>,
- &mod<33>, &mod<34>, &mod<35>, &mod<36>, &mod<37> , &mod<38>, &mod<39>,
-#endif
-#if SIZE_MAX >= ULLONG_MAX
- &mod<40>, &mod<41>, &mod<42>, &mod<43>, &mod<44>, &mod<45>, &mod<46>, &mod<47>, &mod<48>, &mod<49>,
- &mod<50>,
-#endif
-}};
-
-}
-
-/**
- * Grow the hash table by using prime numbers as bucket count. Slower than tsl::hh::power_of_two_growth_policy in
- * general but will probably distribute the values around better in the buckets with a poor hash function.
- *
- * To allow the compiler to optimize the modulo operation, a lookup table is used with constant primes numbers.
- *
- * With a switch the code would look like:
- * \code
- * switch(iprime) { // iprime is the current prime of the hash table
- * case 0: hash % 5ul;
- * break;
- * case 1: hash % 17ul;
- * break;
- * case 2: hash % 29ul;
- * break;
- * ...
- * }
- * \endcode
- *
- * Due to the constant variable in the modulo the compiler is able to optimize the operation
- * by a series of multiplications, substractions and shifts.
- *
- * The 'hash % 5' could become something like 'hash - (hash * 0xCCCCCCCD) >> 34) * 5' in a 64 bits environment.
- */
-class prime_growth_policy {
-public:
- explicit prime_growth_policy(std::size_t& min_bucket_count_in_out) {
- auto it_prime = std::lower_bound(detail::PRIMES.begin(),
- detail::PRIMES.end(), min_bucket_count_in_out);
- if(it_prime == detail::PRIMES.end()) {
- TSL_HH_THROW_OR_TERMINATE(std::length_error, "The hash table exceeds its maximum size.");
- }
-
- m_iprime = static_cast<unsigned int>(std::distance(detail::PRIMES.begin(), it_prime));
- if(min_bucket_count_in_out > 0) {
- min_bucket_count_in_out = *it_prime;
- }
- else {
- min_bucket_count_in_out = 0;
- }
- }
-
- std::size_t bucket_for_hash(std::size_t hash) const noexcept {
- return detail::MOD_PRIME[m_iprime](hash);
- }
-
- std::size_t next_bucket_count() const {
- if(m_iprime + 1 >= detail::PRIMES.size()) {
- TSL_HH_THROW_OR_TERMINATE(std::length_error, "The hash table exceeds its maximum size.");
- }
-
- return detail::PRIMES[m_iprime + 1];
- }
-
- std::size_t max_bucket_count() const {
- return detail::PRIMES.back();
- }
-
- void clear() noexcept {
- m_iprime = 0;
- }
-
-private:
- unsigned int m_iprime;
-
- static_assert(std::numeric_limits<decltype(m_iprime)>::max() >= detail::PRIMES.size(),
- "The type of m_iprime is not big enough.");
-};
-
-}
-}
-
-#endif
diff --git a/benchmarks/others/hopscotch_hash.h b/benchmarks/others/hopscotch_hash.h
deleted file mode 100644
index a97fa2b4..00000000
--- a/benchmarks/others/hopscotch_hash.h
+++ /dev/null
@@ -1,1827 +0,0 @@
-/**
- * MIT License
- *
- * Copyright (c) 2017 Thibaut Goetghebuer-Planchon <[email protected]>
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy
- * of this software and associated documentation files (the "Software"), to deal
- * in the Software without restriction, including without limitation the rights
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- * copies of the Software, and to permit persons to whom the Software is
- * furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in all
- * copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
- * SOFTWARE.
- */
-#ifndef TSL_HOPSCOTCH_HASH_H
-#define TSL_HOPSCOTCH_HASH_H
-
-
-#include <algorithm>
-#include <cassert>
-#include <cmath>
-#include <cstddef>
-#include <cstdint>
-#include <exception>
-#include <functional>
-#include <initializer_list>
-#include <iterator>
-#include <limits>
-#include <memory>
-#include <stdexcept>
-#include <tuple>
-#include <type_traits>
-#include <utility>
-#include <vector>
-#include "hopscotch_growth_policy.h"
-
-
-#if (defined(__GNUC__) && (__GNUC__ == 4) && (__GNUC_MINOR__ < 9))
-# define TSL_HH_NO_RANGE_ERASE_WITH_CONST_ITERATOR
-#endif
-
-
-namespace tsl {
-namespace detail_hopscotch_hash {
-
-
-template<typename T>
-struct make_void {
- using type = void;
-};
-
-
-template<typename T, typename = void>
-struct has_is_transparent : std::false_type {
-};
-
-template<typename T>
-struct has_is_transparent<T, typename make_void<typename T::is_transparent>::type> : std::true_type {
-};
-
-
-template<typename T, typename = void>
-struct has_key_compare : std::false_type {
-};
-
-template<typename T>
-struct has_key_compare<T, typename make_void<typename T::key_compare>::type> : std::true_type {
-};
-
-
-template<typename U>
-struct is_power_of_two_policy: std::false_type {
-};
-
-template<std::size_t GrowthFactor>
-struct is_power_of_two_policy<tsl::hh::power_of_two_growth_policy<GrowthFactor>>: std::true_type {
-};
-
-
-template<typename T, typename U>
-static T numeric_cast(U value, const char* error_message = "numeric_cast() failed.") {
- T ret = static_cast<T>(value);
- if(static_cast<U>(ret) != value) {
- TSL_HH_THROW_OR_TERMINATE(std::runtime_error, error_message);
- }
-
- const bool is_same_signedness = (std::is_unsigned<T>::value && std::is_unsigned<U>::value) ||
- (std::is_signed<T>::value && std::is_signed<U>::value);
- if(!is_same_signedness && (ret < T{}) != (value < U{})) {
- TSL_HH_THROW_OR_TERMINATE(std::runtime_error, error_message);
- }
-
- return ret;
-}
-
-
-/*
- * smallest_type_for_min_bits::type returns the smallest type that can fit MinBits.
- */
-static const std::size_t SMALLEST_TYPE_MAX_BITS_SUPPORTED = 64;
-template<unsigned int MinBits, typename Enable = void>
-class smallest_type_for_min_bits {
-};
-
-template<unsigned int MinBits>
-class smallest_type_for_min_bits<MinBits, typename std::enable_if<(MinBits > 0) && (MinBits <= 8)>::type> {
-public:
- using type = std::uint_least8_t;
-};
-
-template<unsigned int MinBits>
-class smallest_type_for_min_bits<MinBits, typename std::enable_if<(MinBits > 8) && (MinBits <= 16)>::type> {
-public:
- using type = std::uint_least16_t;
-};
-
-template<unsigned int MinBits>
-class smallest_type_for_min_bits<MinBits, typename std::enable_if<(MinBits > 16) && (MinBits <= 32)>::type> {
-public:
- using type = std::uint_least32_t;
-};
-
-template<unsigned int MinBits>
-class smallest_type_for_min_bits<MinBits, typename std::enable_if<(MinBits > 32) && (MinBits <= 64)>::type> {
-public:
- using type = std::uint_least64_t;
-};
-
-
-
-/*
- * Each bucket may store up to three elements:
- * - An aligned storage to store a value_type object with placement-new.
- * - An (optional) hash of the value in the bucket.
- * - An unsigned integer of type neighborhood_bitmap used to tell us which buckets in the neighborhood of the
- * current bucket contain a value with a hash belonging to the current bucket.
- *
- * For a bucket 'bct', a bit 'i' (counting from 0 and from the least significant bit to the most significant)
- * set to 1 means that the bucket 'bct + i' contains a value with a hash belonging to bucket 'bct'.
- * The bits used for that, start from the third least significant bit.
- * The two least significant bits are reserved:
- * - The least significant bit is set to 1 if there is a value in the bucket storage.
- * - The second least significant bit is set to 1 if there is an overflow. More than NeighborhoodSize values
- * give the same hash, all overflow values are stored in the m_overflow_elements list of the map.
- *
- * Details regarding hopscotch hashing an its implementation can be found here:
- * https://tessil.github.io/2016/08/29/hopscotch-hashing.html
- */
-static const std::size_t NB_RESERVED_BITS_IN_NEIGHBORHOOD = 2;
-
-
-using truncated_hash_type = std::uint_least32_t;
-
-/**
- * Helper class that stores a truncated hash if StoreHash is true and nothing otherwise.
- */
-template<bool StoreHash>
-class hopscotch_bucket_hash {
-public:
- bool bucket_hash_equal(std::size_t /*hash*/) const noexcept {
- return true;
- }
-
- truncated_hash_type truncated_bucket_hash() const noexcept {
- return 0;
- }
-
-protected:
- void copy_hash(const hopscotch_bucket_hash& ) noexcept {
- }
-
- void set_hash(truncated_hash_type /*hash*/) noexcept {
- }
-};
-
-template<>
-class hopscotch_bucket_hash<true> {
-public:
- bool bucket_hash_equal(std::size_t hash) const noexcept {
- return m_hash == truncated_hash_type(hash);
- }
-
- truncated_hash_type truncated_bucket_hash() const noexcept {
- return m_hash;
- }
-
-protected:
- void copy_hash(const hopscotch_bucket_hash& bucket) noexcept {
- m_hash = bucket.m_hash;
- }
-
- void set_hash(truncated_hash_type hash) noexcept {
- m_hash = hash;
- }
-
-private:
- truncated_hash_type m_hash;
-};
-
-
-template<typename ValueType, unsigned int NeighborhoodSize, bool StoreHash>
-class hopscotch_bucket: public hopscotch_bucket_hash<StoreHash> {
-private:
- static const std::size_t MIN_NEIGHBORHOOD_SIZE = 4;
- static const std::size_t MAX_NEIGHBORHOOD_SIZE = SMALLEST_TYPE_MAX_BITS_SUPPORTED - NB_RESERVED_BITS_IN_NEIGHBORHOOD;
-
-
- static_assert(NeighborhoodSize >= 4, "NeighborhoodSize should be >= 4.");
- // We can't put a variable in the message, ensure coherence
- static_assert(MIN_NEIGHBORHOOD_SIZE == 4, "");
-
- static_assert(NeighborhoodSize <= 62, "NeighborhoodSize should be <= 62.");
- // We can't put a variable in the message, ensure coherence
- static_assert(MAX_NEIGHBORHOOD_SIZE == 62, "");
-
-
- static_assert(!StoreHash || NeighborhoodSize <= 30,
- "NeighborhoodSize should be <= 30 if StoreHash is true.");
- // We can't put a variable in the message, ensure coherence
- static_assert(MAX_NEIGHBORHOOD_SIZE - 32 == 30, "");
-
- using bucket_hash = hopscotch_bucket_hash<StoreHash>;
-
-public:
- using value_type = ValueType;
- using neighborhood_bitmap =
- typename smallest_type_for_min_bits<NeighborhoodSize + NB_RESERVED_BITS_IN_NEIGHBORHOOD>::type;
-
-
- hopscotch_bucket() noexcept: bucket_hash(), m_neighborhood_infos(0) {
- tsl_hh_assert(empty());
- }
-
-
- hopscotch_bucket(const hopscotch_bucket& bucket)
- noexcept(std::is_nothrow_copy_constructible<value_type>::value): bucket_hash(bucket),
- m_neighborhood_infos(0)
- {
- if(!bucket.empty()) {
- ::new (static_cast<void*>(std::addressof(m_value))) value_type(bucket.value());
- }
-
- m_neighborhood_infos = bucket.m_neighborhood_infos;
- }
-
- hopscotch_bucket(hopscotch_bucket&& bucket)
- noexcept(std::is_nothrow_move_constructible<value_type>::value) : bucket_hash(std::move(bucket)),
- m_neighborhood_infos(0)
- {
- if(!bucket.empty()) {
- ::new (static_cast<void*>(std::addressof(m_value))) value_type(std::move(bucket.value()));
- }
-
- m_neighborhood_infos = bucket.m_neighborhood_infos;
- }
-
- hopscotch_bucket& operator=(const hopscotch_bucket& bucket)
- noexcept(std::is_nothrow_copy_constructible<value_type>::value)
- {
- if(this != &bucket) {
- remove_value();
-
- bucket_hash::operator=(bucket);
- if(!bucket.empty()) {
- ::new (static_cast<void*>(std::addressof(m_value))) value_type(bucket.value());
- }
-
- m_neighborhood_infos = bucket.m_neighborhood_infos;
- }
-
- return *this;
- }
-
- hopscotch_bucket& operator=(hopscotch_bucket&& ) = delete;
-
- ~hopscotch_bucket() noexcept {
- if(!empty()) {
- destroy_value();
- }
- }
-
- neighborhood_bitmap neighborhood_infos() const noexcept {
- return neighborhood_bitmap(m_neighborhood_infos >> NB_RESERVED_BITS_IN_NEIGHBORHOOD);
- }
-
- void set_overflow(bool has_overflow) noexcept {
- if(has_overflow) {
- m_neighborhood_infos = neighborhood_bitmap(m_neighborhood_infos | 2);
- }
- else {
- m_neighborhood_infos = neighborhood_bitmap(m_neighborhood_infos & ~2);
- }
- }
-
- bool has_overflow() const noexcept {
- return (m_neighborhood_infos & 2) != 0;
- }
-
- bool empty() const noexcept {
- return (m_neighborhood_infos & 1) == 0;
- }
-
- void toggle_neighbor_presence(std::size_t ineighbor) noexcept {
- tsl_hh_assert(ineighbor <= NeighborhoodSize);
- m_neighborhood_infos = neighborhood_bitmap(
- m_neighborhood_infos ^ (1ull << (ineighbor + NB_RESERVED_BITS_IN_NEIGHBORHOOD)));
- }
-
- bool check_neighbor_presence(std::size_t ineighbor) const noexcept {
- tsl_hh_assert(ineighbor <= NeighborhoodSize);
- if(((m_neighborhood_infos >> (ineighbor + NB_RESERVED_BITS_IN_NEIGHBORHOOD)) & 1) == 1) {
- return true;
- }
-
- return false;
- }
-
- value_type& value() noexcept {
- tsl_hh_assert(!empty());
- return *reinterpret_cast<value_type*>(std::addressof(m_value));
- }
-
- const value_type& value() const noexcept {
- tsl_hh_assert(!empty());
- return *reinterpret_cast<const value_type*>(std::addressof(m_value));
- }
-
- template<typename... Args>
- void set_value_of_empty_bucket(truncated_hash_type hash, Args&&... value_type_args) {
- tsl_hh_assert(empty());
-
- ::new (static_cast<void*>(std::addressof(m_value))) value_type(std::forward<Args>(value_type_args)...);
- set_empty(false);
- this->set_hash(hash);
- }
-
- void swap_value_into_empty_bucket(hopscotch_bucket& empty_bucket) {
- tsl_hh_assert(empty_bucket.empty());
- if(!empty()) {
- ::new (static_cast<void*>(std::addressof(empty_bucket.m_value))) value_type(std::move(value()));
- empty_bucket.copy_hash(*this);
- empty_bucket.set_empty(false);
-
- destroy_value();
- set_empty(true);
- }
- }
-
- void remove_value() noexcept {
- if(!empty()) {
- destroy_value();
- set_empty(true);
- }
- }
-
- void clear() noexcept {
- if(!empty()) {
- destroy_value();
- }
-
- m_neighborhood_infos = 0;
- tsl_hh_assert(empty());
- }
-
- static truncated_hash_type truncate_hash(std::size_t hash) noexcept {
- return truncated_hash_type(hash);
- }
-
-private:
- void set_empty(bool is_empty) noexcept {
- if(is_empty) {
- m_neighborhood_infos = neighborhood_bitmap(m_neighborhood_infos & ~1);
- }
- else {
- m_neighborhood_infos = neighborhood_bitmap(m_neighborhood_infos | 1);
- }
- }
-
- void destroy_value() noexcept {
- tsl_hh_assert(!empty());
- value().~value_type();
- }
-
-private:
- using storage = typename std::aligned_storage<sizeof(value_type), alignof(value_type)>::type;
-
- neighborhood_bitmap m_neighborhood_infos;
- storage m_value;
-};
-
-
-/**
- * Internal common class used by (b)hopscotch_map and (b)hopscotch_set.
- *
- * ValueType is what will be stored by hopscotch_hash (usually std::pair<Key, T> for a map and Key for a set).
- *
- * KeySelect should be a FunctionObject which takes a ValueType in parameter and returns a reference to the key.
- *
- * ValueSelect should be a FunctionObject which takes a ValueType in parameter and returns a reference to the value.
- * ValueSelect should be void if there is no value (in a set for example).
- *
- * OverflowContainer will be used as containers for overflown elements. Usually it should be a list<ValueType>
- * or a set<Key>/map<Key, T>.
- */
-template<class ValueType,
- class KeySelect,
- class ValueSelect,
- class Hash,
- class KeyEqual,
- class Allocator,
- unsigned int NeighborhoodSize,
- bool StoreHash,
- class GrowthPolicy,
- class OverflowContainer>
-class hopscotch_hash: private Hash, private KeyEqual, private GrowthPolicy {
-private:
- template<typename U>
- using has_mapped_type = typename std::integral_constant<bool, !std::is_same<U, void>::value>;
-
- static_assert(noexcept(std::declval<GrowthPolicy>().bucket_for_hash(std::size_t(0))), "GrowthPolicy::bucket_for_hash must be noexcept.");
- static_assert(noexcept(std::declval<GrowthPolicy>().clear()), "GrowthPolicy::clear must be noexcept.");
-
-public:
- template<bool IsConst>
- class hopscotch_iterator;
-
- using key_type = typename KeySelect::key_type;
- using value_type = ValueType;
- using size_type = std::size_t;
- using difference_type = std::ptrdiff_t;
- using hasher = Hash;
- using key_equal = KeyEqual;
- using allocator_type = Allocator;
- using reference = value_type&;
- using const_reference = const value_type&;
- using pointer = value_type*;
- using const_pointer = const value_type*;
- using iterator = hopscotch_iterator<false>;
- using const_iterator = hopscotch_iterator<true>;
-
-private:
- using hopscotch_bucket = tsl::detail_hopscotch_hash::hopscotch_bucket<ValueType, NeighborhoodSize, StoreHash>;
- using neighborhood_bitmap = typename hopscotch_bucket::neighborhood_bitmap;
-
- using buckets_allocator = typename std::allocator_traits<allocator_type>::template rebind_alloc<hopscotch_bucket>;
- using buckets_container_type = std::vector<hopscotch_bucket, buckets_allocator>;
-
- using overflow_container_type = OverflowContainer;
-
- static_assert(std::is_same<typename overflow_container_type::value_type, ValueType>::value,
- "OverflowContainer should have ValueType as type.");
-
- static_assert(std::is_same<typename overflow_container_type::allocator_type, Allocator>::value,
- "Invalid allocator, not the same type as the value_type.");
-
-
- using iterator_buckets = typename buckets_container_type::iterator;
- using const_iterator_buckets = typename buckets_container_type::const_iterator;
-
- using iterator_overflow = typename overflow_container_type::iterator;
- using const_iterator_overflow = typename overflow_container_type::const_iterator;
-
-public:
- /**
- * The `operator*()` and `operator->()` methods return a const reference and const pointer respectively to the
- * stored value type.
- *
- * In case of a map, to get a modifiable reference to the value associated to a key (the `.second` in the
- * stored pair), you have to call `value()`.
- */
- template<bool IsConst>
- class hopscotch_iterator {
- friend class hopscotch_hash;
- private:
- using iterator_bucket = typename std::conditional<IsConst,
- typename hopscotch_hash::const_iterator_buckets,
- typename hopscotch_hash::iterator_buckets>::type;
- using iterator_overflow = typename std::conditional<IsConst,
- typename hopscotch_hash::const_iterator_overflow,
- typename hopscotch_hash::iterator_overflow>::type;
-
-
- hopscotch_iterator(iterator_bucket buckets_iterator, iterator_bucket buckets_end_iterator,
- iterator_overflow overflow_iterator) noexcept :
- m_buckets_iterator(buckets_iterator), m_buckets_end_iterator(buckets_end_iterator),
- m_overflow_iterator(overflow_iterator)
- {
- }
-
- public:
- using iterator_category = std::forward_iterator_tag;
- using value_type = const typename hopscotch_hash::value_type;
- using difference_type = std::ptrdiff_t;
- using reference = value_type&;
- using pointer = value_type*;
-
-
- hopscotch_iterator() noexcept {
- }
-
- // Copy constructor from iterator to const_iterator.
- template<bool TIsConst = IsConst, typename std::enable_if<TIsConst>::type* = nullptr>
- hopscotch_iterator(const hopscotch_iterator<!TIsConst>& other) noexcept :
- m_buckets_iterator(other.m_buckets_iterator), m_buckets_end_iterator(other.m_buckets_end_iterator),
- m_overflow_iterator(other.m_overflow_iterator)
- {
- }
-
- hopscotch_iterator(const hopscotch_iterator& other) = default;
- hopscotch_iterator(hopscotch_iterator&& other) = default;
- hopscotch_iterator& operator=(const hopscotch_iterator& other) = default;
- hopscotch_iterator& operator=(hopscotch_iterator&& other) = default;
-
- const typename hopscotch_hash::key_type& key() const {
- if(m_buckets_iterator != m_buckets_end_iterator) {
- return KeySelect()(m_buckets_iterator->value());
- }
-
- return KeySelect()(*m_overflow_iterator);
- }
-
- template<class U = ValueSelect, typename std::enable_if<has_mapped_type<U>::value>::type* = nullptr>
- typename std::conditional<
- IsConst,
- const typename U::value_type&,
- typename U::value_type&>::type value() const
- {
- if(m_buckets_iterator != m_buckets_end_iterator) {
- return U()(m_buckets_iterator->value());
- }
-
- return U()(*m_overflow_iterator);
- }
-
- reference operator*() const {
- if(m_buckets_iterator != m_buckets_end_iterator) {
- return m_buckets_iterator->value();
- }
-
- return *m_overflow_iterator;
- }
-
- pointer operator->() const {
- if(m_buckets_iterator != m_buckets_end_iterator) {
- return std::addressof(m_buckets_iterator->value());
- }
-
- return std::addressof(*m_overflow_iterator);
- }
-
- hopscotch_iterator& operator++() {
- if(m_buckets_iterator == m_buckets_end_iterator) {
- ++m_overflow_iterator;
- return *this;
- }
-
- do {
- ++m_buckets_iterator;
- } while(m_buckets_iterator != m_buckets_end_iterator && m_buckets_iterator->empty());
-
- return *this;
- }
-
- hopscotch_iterator operator++(int) {
- hopscotch_iterator tmp(*this);
- ++*this;
-
- return tmp;
- }
-
- friend bool operator==(const hopscotch_iterator& lhs, const hopscotch_iterator& rhs) {
- return lhs.m_buckets_iterator == rhs.m_buckets_iterator &&
- lhs.m_overflow_iterator == rhs.m_overflow_iterator;
- }
-
- friend bool operator!=(const hopscotch_iterator& lhs, const hopscotch_iterator& rhs) {
- return !(lhs == rhs);
- }
-
- private:
- iterator_bucket m_buckets_iterator;
- iterator_bucket m_buckets_end_iterator;
- iterator_overflow m_overflow_iterator;
- };
-
-public:
- template<class OC = OverflowContainer, typename std::enable_if<!has_key_compare<OC>::value>::type* = nullptr>
- hopscotch_hash(size_type bucket_count,
- const Hash& hash,
- const KeyEqual& equal,
- const Allocator& alloc,
- float max_load_factor) : Hash(hash),
- KeyEqual(equal),
- GrowthPolicy(bucket_count),
- m_buckets_data(alloc),
- m_overflow_elements(alloc),
- m_buckets(static_empty_bucket_ptr()),
- m_nb_elements(0)
- {
- if(bucket_count > max_bucket_count()) {
- TSL_HH_THROW_OR_TERMINATE(std::length_error, "The map exceeds its maximum size.");
- }
-
- if(bucket_count > 0) {
- static_assert(NeighborhoodSize - 1 > 0, "");
-
- // Can't directly construct with the appropriate size in the initializer
- // as m_buckets_data(bucket_count, alloc) is not supported by GCC 4.8
- m_buckets_data.resize(bucket_count + NeighborhoodSize - 1);
- m_buckets = m_buckets_data.data();
- }
-
-
- this->max_load_factor(max_load_factor);
-
-
- // Check in the constructor instead of outside of a function to avoid compilation issues
- // when value_type is not complete.
- static_assert(std::is_nothrow_move_constructible<value_type>::value ||
- std::is_copy_constructible<value_type>::value,
- "value_type must be either copy constructible or nothrow move constructible.");
- }
-
- template<class OC = OverflowContainer, typename std::enable_if<has_key_compare<OC>::value>::type* = nullptr>
- hopscotch_hash(size_type bucket_count,
- const Hash& hash,
- const KeyEqual& equal,
- const Allocator& alloc,
- float max_load_factor,
- const typename OC::key_compare& comp) : Hash(hash),
- KeyEqual(equal),
- GrowthPolicy(bucket_count),
- m_buckets_data(alloc),
- m_overflow_elements(comp, alloc),
- m_buckets(static_empty_bucket_ptr()),
- m_nb_elements(0)
- {
-
- if(bucket_count > max_bucket_count()) {
- TSL_HH_THROW_OR_TERMINATE(std::length_error, "The map exceeds its maximum size.");
- }
-
- if(bucket_count > 0) {
- static_assert(NeighborhoodSize - 1 > 0, "");
-
- // Can't directly construct with the appropriate size in the initializer
- // as m_buckets_data(bucket_count, alloc) is not supported by GCC 4.8
- m_buckets_data.resize(bucket_count + NeighborhoodSize - 1);
- m_buckets = m_buckets_data.data();
- }
-
-
- this->max_load_factor(max_load_factor);
-
-
- // Check in the constructor instead of outside of a function to avoid compilation issues
- // when value_type is not complete.
- static_assert(std::is_nothrow_move_constructible<value_type>::value ||
- std::is_copy_constructible<value_type>::value,
- "value_type must be either copy constructible or nothrow move constructible.");
- }
-
- hopscotch_hash(const hopscotch_hash& other):
- Hash(other),
- KeyEqual(other),
- GrowthPolicy(other),
- m_buckets_data(other.m_buckets_data),
- m_overflow_elements(other.m_overflow_elements),
- m_buckets(m_buckets_data.empty()?static_empty_bucket_ptr():
- m_buckets_data.data()),
- m_nb_elements(other.m_nb_elements),
- m_min_load_threshold_rehash(other.m_min_load_threshold_rehash),
- m_max_load_threshold_rehash(other.m_max_load_threshold_rehash),
- m_max_load_factor(other.m_max_load_factor)
- {
- }
-
- hopscotch_hash(hopscotch_hash&& other)
- noexcept(
- std::is_nothrow_move_constructible<Hash>::value &&
- std::is_nothrow_move_constructible<KeyEqual>::value &&
- std::is_nothrow_move_constructible<GrowthPolicy>::value &&
- std::is_nothrow_move_constructible<buckets_container_type>::value &&
- std::is_nothrow_move_constructible<overflow_container_type>::value
- ):
- Hash(std::move(static_cast<Hash&>(other))),
- KeyEqual(std::move(static_cast<KeyEqual&>(other))),
- GrowthPolicy(std::move(static_cast<GrowthPolicy&>(other))),
- m_buckets_data(std::move(other.m_buckets_data)),
- m_overflow_elements(std::move(other.m_overflow_elements)),
- m_buckets(m_buckets_data.empty()?static_empty_bucket_ptr():
- m_buckets_data.data()),
- m_nb_elements(other.m_nb_elements),
- m_min_load_threshold_rehash(other.m_min_load_threshold_rehash),
- m_max_load_threshold_rehash(other.m_max_load_threshold_rehash),
- m_max_load_factor(other.m_max_load_factor)
- {
- other.GrowthPolicy::clear();
- other.m_buckets_data.clear();
- other.m_overflow_elements.clear();
- other.m_buckets = static_empty_bucket_ptr();
- other.m_nb_elements = 0;
- other.m_min_load_threshold_rehash = 0;
- other.m_max_load_threshold_rehash = 0;
- }
-
- hopscotch_hash& operator=(const hopscotch_hash& other) {
- if(&other != this) {
- Hash::operator=(other);
- KeyEqual::operator=(other);
- GrowthPolicy::operator=(other);
-
- m_buckets_data = other.m_buckets_data;
- m_overflow_elements = other.m_overflow_elements;
- m_buckets = m_buckets_data.empty()?static_empty_bucket_ptr():
- m_buckets_data.data();
- m_nb_elements = other.m_nb_elements;
-
- m_min_load_threshold_rehash = other.m_min_load_threshold_rehash;
- m_max_load_threshold_rehash = other.m_max_load_threshold_rehash;
- m_max_load_factor = other.m_max_load_factor;
- }
-
- return *this;
- }
-
- hopscotch_hash& operator=(hopscotch_hash&& other) {
- other.swap(*this);
- other.clear();
-
- return *this;
- }
-
- allocator_type get_allocator() const {
- return m_buckets_data.get_allocator();
- }
-
-
- /*
- * Iterators
- */
- iterator begin() noexcept {
- auto begin = m_buckets_data.begin();
- while(begin != m_buckets_data.end() && begin->empty()) {
- ++begin;
- }
-
- return iterator(begin, m_buckets_data.end(), m_overflow_elements.begin());
- }
-
- const_iterator begin() const noexcept {
- return cbegin();
- }
-
- const_iterator cbegin() const noexcept {
- auto begin = m_buckets_data.cbegin();
- while(begin != m_buckets_data.cend() && begin->empty()) {
- ++begin;
- }
-
- return const_iterator(begin, m_buckets_data.cend(), m_overflow_elements.cbegin());
- }
-
- iterator end() noexcept {
- return iterator(m_buckets_data.end(), m_buckets_data.end(), m_overflow_elements.end());
- }
-
- const_iterator end() const noexcept {
- return cend();
- }
-
- const_iterator cend() const noexcept {
- return const_iterator(m_buckets_data.cend(), m_buckets_data.cend(), m_overflow_elements.cend());
- }
-
-
- /*
- * Capacity
- */
- bool empty() const noexcept {
- return m_nb_elements == 0;
- }
-
- size_type size() const noexcept {
- return m_nb_elements;
- }
-
- size_type max_size() const noexcept {
- return m_buckets_data.max_size();
- }
-
- /*
- * Modifiers
- */
- void clear() noexcept {
- for(auto& bucket: m_buckets_data) {
- bucket.clear();
- }
-
- m_overflow_elements.clear();
- m_nb_elements = 0;
- }
-
-
- std::pair<iterator, bool> insert(const value_type& value) {
- return insert_impl(value);
- }
-
- template<class P, typename std::enable_if<std::is_constructible<value_type, P&&>::value>::type* = nullptr>
- std::pair<iterator, bool> insert(P&& value) {
- return insert_impl(value_type(std::forward<P>(value)));
- }
-
- std::pair<iterator, bool> insert(value_type&& value) {
- return insert_impl(std::move(value));
- }
-
-
- iterator insert(const_iterator hint, const value_type& value) {
- if(hint != cend() && compare_keys(KeySelect()(*hint), KeySelect()(value))) {
- return mutable_iterator(hint);
- }
-
- return insert(value).first;
- }
-
- template<class P, typename std::enable_if<std::is_constructible<value_type, P&&>::value>::type* = nullptr>
- iterator insert(const_iterator hint, P&& value) {
- return emplace_hint(hint, std::forward<P>(value));
- }
-
- iterator insert(const_iterator hint, value_type&& value) {
- if(hint != cend() && compare_keys(KeySelect()(*hint), KeySelect()(value))) {
- return mutable_iterator(hint);
- }
-
- return insert(std::move(value)).first;
- }
-
-
- template<class InputIt>
- void insert(InputIt first, InputIt last) {
- if(std::is_base_of<std::forward_iterator_tag,
- typename std::iterator_traits<InputIt>::iterator_category>::value)
- {
- const auto nb_elements_insert = std::distance(first, last);
- const std::size_t nb_elements_in_buckets = m_nb_elements - m_overflow_elements.size();
- const std::size_t nb_free_buckets = m_max_load_threshold_rehash - nb_elements_in_buckets;
- tsl_hh_assert(m_nb_elements >= m_overflow_elements.size());
- tsl_hh_assert(m_max_load_threshold_rehash >= nb_elements_in_buckets);
-
- if(nb_elements_insert > 0 && nb_free_buckets < std::size_t(nb_elements_insert)) {
- reserve(nb_elements_in_buckets + std::size_t(nb_elements_insert));
- }
- }
-
- for(; first != last; ++first) {
- insert(*first);
- }
- }
-
-
- template<class M>
- std::pair<iterator, bool> insert_or_assign(const key_type& k, M&& obj) {
- return insert_or_assign_impl(k, std::forward<M>(obj));
- }
-
- template<class M>
- std::pair<iterator, bool> insert_or_assign(key_type&& k, M&& obj) {
- return insert_or_assign_impl(std::move(k), std::forward<M>(obj));
- }
-
-
- template<class M>
- iterator insert_or_assign(const_iterator hint, const key_type& k, M&& obj) {
- if(hint != cend() && compare_keys(KeySelect()(*hint), k)) {
- auto it = mutable_iterator(hint);
- it.value() = std::forward<M>(obj);
-
- return it;
- }
-
- return insert_or_assign(k, std::forward<M>(obj)).first;
- }
-
- template<class M>
- iterator insert_or_assign(const_iterator hint, key_type&& k, M&& obj) {
- if(hint != cend() && compare_keys(KeySelect()(*hint), k)) {
- auto it = mutable_iterator(hint);
- it.value() = std::forward<M>(obj);
-
- return it;
- }
-
- return insert_or_assign(std::move(k), std::forward<M>(obj)).first;
- }
-
-
- template<class... Args>
- std::pair<iterator, bool> emplace(Args&&... args) {
- return insert(value_type(std::forward<Args>(args)...));
- }
-
- template<class... Args>
- iterator emplace_hint(const_iterator hint, Args&&... args) {
- return insert(hint, value_type(std::forward<Args>(args)...));
- }
-
- template<class... Args>
- std::pair<iterator, bool> try_emplace(const key_type& k, Args&&... args) {
- return try_emplace_impl(k, std::forward<Args>(args)...);
- }
-
- template<class... Args>
- std::pair<iterator, bool> try_emplace(key_type&& k, Args&&... args) {
- return try_emplace_impl(std::move(k), std::forward<Args>(args)...);
- }
-
- template<class... Args>
- iterator try_emplace(const_iterator hint, const key_type& k, Args&&... args) {
- if(hint != cend() && compare_keys(KeySelect()(*hint), k)) {
- return mutable_iterator(hint);
- }
-
- return try_emplace(k, std::forward<Args>(args)...).first;
- }
-
- template<class... Args>
- iterator try_emplace(const_iterator hint, key_type&& k, Args&&... args) {
- if(hint != cend() && compare_keys(KeySelect()(*hint), k)) {
- return mutable_iterator(hint);
- }
-
- return try_emplace(std::move(k), std::forward<Args>(args)...).first;
- }
-
-
- /**
- * Here to avoid `template<class K> size_type erase(const K& key)` being used when
- * we use an iterator instead of a const_iterator.
- */
- iterator erase(iterator pos) {
- return erase(const_iterator(pos));
- }
-
- iterator erase(const_iterator pos) {
- const std::size_t ibucket_for_hash = bucket_for_hash(hash_key(pos.key()));
-
- if(pos.m_buckets_iterator != pos.m_buckets_end_iterator) {
- auto it_bucket = m_buckets_data.begin() + std::distance(m_buckets_data.cbegin(), pos.m_buckets_iterator);
- erase_from_bucket(*it_bucket, ibucket_for_hash);
-
- return ++iterator(it_bucket, m_buckets_data.end(), m_overflow_elements.begin());
- }
- else {
- auto it_next_overflow = erase_from_overflow(pos.m_overflow_iterator, ibucket_for_hash);
- return iterator(m_buckets_data.end(), m_buckets_data.end(), it_next_overflow);
- }
- }
-
- iterator erase(const_iterator first, const_iterator last) {
- if(first == last) {
- return mutable_iterator(first);
- }
-
- auto to_delete = erase(first);
- while(to_delete != last) {
- to_delete = erase(to_delete);
- }
-
- return to_delete;
- }
-
- template<class K>
- size_type erase(const K& key) {
- return erase(key, hash_key(key));
- }
-
- template<class K>
- size_type erase(const K& key, std::size_t hash) {
- const std::size_t ibucket_for_hash = bucket_for_hash(hash);
-
- hopscotch_bucket* bucket_found = find_in_buckets(key, hash, m_buckets + ibucket_for_hash);
- if(bucket_found != nullptr) {
- erase_from_bucket(*bucket_found, ibucket_for_hash);
-
- return 1;
- }
-
- if(m_buckets[ibucket_for_hash].has_overflow()) {
- auto it_overflow = find_in_overflow(key);
- if(it_overflow != m_overflow_elements.end()) {
- erase_from_overflow(it_overflow, ibucket_for_hash);
-
- return 1;
- }
- }
-
- return 0;
- }
-
- void swap(hopscotch_hash& other) {
- using std::swap;
-
- swap(static_cast<Hash&>(*this), static_cast<Hash&>(other));
- swap(static_cast<KeyEqual&>(*this), static_cast<KeyEqual&>(other));
- swap(static_cast<GrowthPolicy&>(*this), static_cast<GrowthPolicy&>(other));
- swap(m_buckets_data, other.m_buckets_data);
- swap(m_overflow_elements, other.m_overflow_elements);
- swap(m_buckets, other.m_buckets);
- swap(m_nb_elements, other.m_nb_elements);
- swap(m_min_load_threshold_rehash, other.m_min_load_threshold_rehash);
- swap(m_max_load_threshold_rehash, other.m_max_load_threshold_rehash);
- swap(m_max_load_factor, other.m_max_load_factor);
- }
-
-
- /*
- * Lookup
- */
- template<class K, class U = ValueSelect, typename std::enable_if<has_mapped_type<U>::value>::type* = nullptr>
- typename U::value_type& at(const K& key) {
- return at(key, hash_key(key));
- }
-
- template<class K, class U = ValueSelect, typename std::enable_if<has_mapped_type<U>::value>::type* = nullptr>
- typename U::value_type& at(const K& key, std::size_t hash) {
- return const_cast<typename U::value_type&>(static_cast<const hopscotch_hash*>(this)->at(key, hash));
- }
-
-
- template<class K, class U = ValueSelect, typename std::enable_if<has_mapped_type<U>::value>::type* = nullptr>
- const typename U::value_type& at(const K& key) const {
- return at(key, hash_key(key));
- }
-
- template<class K, class U = ValueSelect, typename std::enable_if<has_mapped_type<U>::value>::type* = nullptr>
- const typename U::value_type& at(const K& key, std::size_t hash) const {
- using T = typename U::value_type;
-
- const T* value = find_value_impl(key, hash, m_buckets + bucket_for_hash(hash));
- if(value == nullptr) {
- TSL_HH_THROW_OR_TERMINATE(std::out_of_range, "Couldn't find key.");
- }
- else {
- return *value;
- }
- }
-
-
- template<class K, class U = ValueSelect, typename std::enable_if<has_mapped_type<U>::value>::type* = nullptr>
- typename U::value_type& operator[](K&& key) {
- using T = typename U::value_type;
-
- const std::size_t hash = hash_key(key);
- const std::size_t ibucket_for_hash = bucket_for_hash(hash);
-
- T* value = find_value_impl(key, hash, m_buckets + ibucket_for_hash);
- if(value != nullptr) {
- return *value;
- }
- else {
- return insert_value(ibucket_for_hash, hash, std::piecewise_construct,
- std::forward_as_tuple(std::forward<K>(key)),
- std::forward_as_tuple()).first.value();
- }
- }
-
-
- template<class K>
- size_type count(const K& key) const {
- return count(key, hash_key(key));
- }
-
- template<class K>
- size_type count(const K& key, std::size_t hash) const {
- return count_impl(key, hash, m_buckets + bucket_for_hash(hash));
- }
-
-
- template<class K>
- iterator find(const K& key) {
- return find(key, hash_key(key));
- }
-
- template<class K>
- iterator find(const K& key, std::size_t hash) {
- return find_impl(key, hash, m_buckets + bucket_for_hash(hash));
- }
-
-
- template<class K>
- const_iterator find(const K& key) const {
- return find(key, hash_key(key));
- }
-
- template<class K>
- const_iterator find(const K& key, std::size_t hash) const {
- return find_impl(key, hash, m_buckets + bucket_for_hash(hash));
- }
-
-
- template<class K>
- bool contains(const K& key) const {
- return contains(key, hash_key(key));
- }
-
- template<class K>
- bool contains(const K& key, std::size_t hash) const {
- return count(key, hash) != 0;
- }
-
-
- template<class K>
- std::pair<iterator, iterator> equal_range(const K& key) {
- return equal_range(key, hash_key(key));
- }
-
- template<class K>
- std::pair<iterator, iterator> equal_range(const K& key, std::size_t hash) {
- iterator it = find(key, hash);
- return std::make_pair(it, (it == end())?it:std::next(it));
- }
-
-
- template<class K>
- std::pair<const_iterator, const_iterator> equal_range(const K& key) const {
- return equal_range(key, hash_key(key));
- }
-
- template<class K>
- std::pair<const_iterator, const_iterator> equal_range(const K& key, std::size_t hash) const {
- const_iterator it = find(key, hash);
- return std::make_pair(it, (it == cend())?it:std::next(it));
- }
-
- /*
- * Bucket interface
- */
- size_type bucket_count() const {
- /*
- * So that the last bucket can have NeighborhoodSize neighbors, the size of the bucket array is a little
- * bigger than the real number of buckets when not empty.
- * We could use some of the buckets at the beginning, but it is faster this way as we avoid extra checks.
- */
- if(m_buckets_data.empty()) {
- return 0;
- }
-
- return m_buckets_data.size() - NeighborhoodSize + 1;
- }
-
- size_type max_bucket_count() const {
- const std::size_t max_bucket_count = std::min(GrowthPolicy::max_bucket_count(), m_buckets_data.max_size());
- return max_bucket_count - NeighborhoodSize + 1;
- }
-
-
- /*
- * Hash policy
- */
- float load_factor() const {
- if(bucket_count() == 0) {
- return 0;
- }
-
- return float(m_nb_elements)/float(bucket_count());
- }
-
- float max_load_factor() const {
- return m_max_load_factor;
- }
-
- void max_load_factor(float ml) {
- m_max_load_factor = std::max(0.1f, std::min(ml, 0.95f));
- m_min_load_threshold_rehash = size_type(float(bucket_count())*MIN_LOAD_FACTOR_FOR_REHASH);
- m_max_load_threshold_rehash = size_type(float(bucket_count())*m_max_load_factor);
- }
-
- void rehash(size_type count_) {
- count_ = std::max(count_, size_type(std::ceil(float(size())/max_load_factor())));
- rehash_impl(count_);
- }
-
- void reserve(size_type count_) {
- rehash(size_type(std::ceil(float(count_)/max_load_factor())));
- }
-
-
- /*
- * Observers
- */
- hasher hash_function() const {
- return static_cast<const Hash&>(*this);
- }
-
- key_equal key_eq() const {
- return static_cast<const KeyEqual&>(*this);
- }
-
- /*
- * Other
- */
- iterator mutable_iterator(const_iterator pos) {
- if(pos.m_buckets_iterator != pos.m_buckets_end_iterator) {
- // Get a non-const iterator
- auto it = m_buckets_data.begin() + std::distance(m_buckets_data.cbegin(), pos.m_buckets_iterator);
- return iterator(it, m_buckets_data.end(), m_overflow_elements.begin());
- }
- else {
- // Get a non-const iterator
- auto it = mutable_overflow_iterator(pos.m_overflow_iterator);
- return iterator(m_buckets_data.end(), m_buckets_data.end(), it);
- }
- }
-
- size_type overflow_size() const noexcept {
- return m_overflow_elements.size();
- }
-
- template<class U = OverflowContainer, typename std::enable_if<has_key_compare<U>::value>::type* = nullptr>
- typename U::key_compare key_comp() const {
- return m_overflow_elements.key_comp();
- }
-
-
-private:
- template<class K>
- std::size_t hash_key(const K& key) const {
- return Hash::operator()(key);
- }
-
- template<class K1, class K2>
- bool compare_keys(const K1& key1, const K2& key2) const {
- return KeyEqual::operator()(key1, key2);
- }
-
- std::size_t bucket_for_hash(std::size_t hash) const {
- const std::size_t bucket = GrowthPolicy::bucket_for_hash(hash);
- tsl_hh_assert(bucket < m_buckets_data.size() || (bucket == 0 && m_buckets_data.empty()));
-
- return bucket;
- }
-
- template<typename U = value_type,
- typename std::enable_if<std::is_nothrow_move_constructible<U>::value>::type* = nullptr>
- void rehash_impl(size_type count_) {
- hopscotch_hash new_map = new_hopscotch_hash(count_);
-
- if(!m_overflow_elements.empty()) {
- new_map.m_overflow_elements.swap(m_overflow_elements);
- new_map.m_nb_elements += new_map.m_overflow_elements.size();
-
- for(const value_type& value : new_map.m_overflow_elements) {
- const std::size_t ibucket_for_hash = new_map.bucket_for_hash(new_map.hash_key(KeySelect()(value)));
- new_map.m_buckets[ibucket_for_hash].set_overflow(true);
- }
- }
-
-#ifndef TSL_HH_NO_EXCEPTIONS
- try {
-#endif
- const bool use_stored_hash = USE_STORED_HASH_ON_REHASH(new_map.bucket_count());
- for(auto it_bucket = m_buckets_data.begin(); it_bucket != m_buckets_data.end(); ++it_bucket) {
- if(it_bucket->empty()) {
- continue;
- }
-
- const std::size_t hash = use_stored_hash?
- it_bucket->truncated_bucket_hash():
- new_map.hash_key(KeySelect()(it_bucket->value()));
- const std::size_t ibucket_for_hash = new_map.bucket_for_hash(hash);
-
- new_map.insert_value(ibucket_for_hash, hash, std::move(it_bucket->value()));
-
-
- erase_from_bucket(*it_bucket, bucket_for_hash(hash));
- }
-#ifndef TSL_HH_NO_EXCEPTIONS
- }
- /*
- * The call to insert_value may throw an exception if an element is added to the overflow
- * list and the memory allocation fails. Rollback the elements in this case.
- */
- catch(...) {
- m_overflow_elements.swap(new_map.m_overflow_elements);
-
- const bool use_stored_hash = USE_STORED_HASH_ON_REHASH(new_map.bucket_count());
- for(auto it_bucket = new_map.m_buckets_data.begin(); it_bucket != new_map.m_buckets_data.end(); ++it_bucket) {
- if(it_bucket->empty()) {
- continue;
- }
-
- const std::size_t hash = use_stored_hash?
- it_bucket->truncated_bucket_hash():
- hash_key(KeySelect()(it_bucket->value()));
- const std::size_t ibucket_for_hash = bucket_for_hash(hash);
-
- // The elements we insert were not in the overflow list before the switch.
- // They will not be go in the overflow list if we rollback the switch.
- insert_value(ibucket_for_hash, hash, std::move(it_bucket->value()));
- }
-
- throw;
- }
-#endif
-
- new_map.swap(*this);
- }
-
- template<typename U = value_type,
- typename std::enable_if<std::is_copy_constructible<U>::value &&
- !std::is_nothrow_move_constructible<U>::value>::type* = nullptr>
- void rehash_impl(size_type count_) {
- hopscotch_hash new_map = new_hopscotch_hash(count_);
-
- const bool use_stored_hash = USE_STORED_HASH_ON_REHASH(new_map.bucket_count());
- for(const hopscotch_bucket& bucket: m_buckets_data) {
- if(bucket.empty()) {
- continue;
- }
-
- const std::size_t hash = use_stored_hash?
- bucket.truncated_bucket_hash():
- new_map.hash_key(KeySelect()(bucket.value()));
- const std::size_t ibucket_for_hash = new_map.bucket_for_hash(hash);
-
- new_map.insert_value(ibucket_for_hash, hash, bucket.value());
- }
-
- for(const value_type& value: m_overflow_elements) {
- const std::size_t hash = new_map.hash_key(KeySelect()(value));
- const std::size_t ibucket_for_hash = new_map.bucket_for_hash(hash);
-
- new_map.insert_value(ibucket_for_hash, hash, value);
- }
-
- new_map.swap(*this);
- }
-
-#ifdef TSL_HH_NO_RANGE_ERASE_WITH_CONST_ITERATOR
- iterator_overflow mutable_overflow_iterator(const_iterator_overflow it) {
- return std::next(m_overflow_elements.begin(), std::distance(m_overflow_elements.cbegin(), it));
- }
-#else
- iterator_overflow mutable_overflow_iterator(const_iterator_overflow it) {
- return m_overflow_elements.erase(it, it);
- }
-#endif
-
- // iterator is in overflow list
- iterator_overflow erase_from_overflow(const_iterator_overflow pos, std::size_t ibucket_for_hash) {
-#ifdef TSL_HH_NO_RANGE_ERASE_WITH_CONST_ITERATOR
- auto it_next = m_overflow_elements.erase(mutable_overflow_iterator(pos));
-#else
- auto it_next = m_overflow_elements.erase(pos);
-#endif
- m_nb_elements--;
-
-
- // Check if we can remove the overflow flag
- tsl_hh_assert(m_buckets[ibucket_for_hash].has_overflow());
- for(const value_type& value: m_overflow_elements) {
- const std::size_t bucket_for_value = bucket_for_hash(hash_key(KeySelect()(value)));
- if(bucket_for_value == ibucket_for_hash) {
- return it_next;
- }
- }
-
- m_buckets[ibucket_for_hash].set_overflow(false);
- return it_next;
- }
-
-
- /**
- * bucket_for_value is the bucket in which the value is.
- * ibucket_for_hash is the bucket where the value belongs.
- */
- void erase_from_bucket(hopscotch_bucket& bucket_for_value, std::size_t ibucket_for_hash) noexcept {
- const std::size_t ibucket_for_value = std::distance(m_buckets_data.data(), &bucket_for_value);
- tsl_hh_assert(ibucket_for_value >= ibucket_for_hash);
-
- bucket_for_value.remove_value();
- m_buckets[ibucket_for_hash].toggle_neighbor_presence(ibucket_for_value - ibucket_for_hash);
- m_nb_elements--;
- }
-
-
-
- template<class K, class M>
- std::pair<iterator, bool> insert_or_assign_impl(K&& key, M&& obj) {
- auto it = try_emplace_impl(std::forward<K>(key), std::forward<M>(obj));
- if(!it.second) {
- it.first.value() = std::forward<M>(obj);
- }
-
- return it;
- }
-
- template<typename P, class... Args>
- std::pair<iterator, bool> try_emplace_impl(P&& key, Args&&... args_value) {
- const std::size_t hash = hash_key(key);
- const std::size_t ibucket_for_hash = bucket_for_hash(hash);
-
- // Check if already presents
- auto it_find = find_impl(key, hash, m_buckets + ibucket_for_hash);
- if(it_find != end()) {
- return std::make_pair(it_find, false);
- }
-
- return insert_value(ibucket_for_hash, hash, std::piecewise_construct,
- std::forward_as_tuple(std::forward<P>(key)),
- std::forward_as_tuple(std::forward<Args>(args_value)...));
- }
-
- template<typename P>
- std::pair<iterator, bool> insert_impl(P&& value) {
- const std::size_t hash = hash_key(KeySelect()(value));
- const std::size_t ibucket_for_hash = bucket_for_hash(hash);
-
- // Check if already presents
- auto it_find = find_impl(KeySelect()(value), hash, m_buckets + ibucket_for_hash);
- if(it_find != end()) {
- return std::make_pair(it_find, false);
- }
-
-
- return insert_value(ibucket_for_hash, hash, std::forward<P>(value));
- }
-
- template<typename... Args>
- std::pair<iterator, bool> insert_value(std::size_t ibucket_for_hash, std::size_t hash, Args&&... value_type_args) {
- if((m_nb_elements - m_overflow_elements.size()) >= m_max_load_threshold_rehash) {
- rehash(GrowthPolicy::next_bucket_count());
- ibucket_for_hash = bucket_for_hash(hash);
- }
-
- std::size_t ibucket_empty = find_empty_bucket(ibucket_for_hash);
- if(ibucket_empty < m_buckets_data.size()) {
- do {
- tsl_hh_assert(ibucket_empty >= ibucket_for_hash);
-
- // Empty bucket is in range of NeighborhoodSize, use it
- if(ibucket_empty - ibucket_for_hash < NeighborhoodSize) {
- auto it = insert_in_bucket(ibucket_empty, ibucket_for_hash,
- hash, std::forward<Args>(value_type_args)...);
- return std::make_pair(iterator(it, m_buckets_data.end(), m_overflow_elements.begin()), true);
- }
- }
- // else, try to swap values to get a closer empty bucket
- while(swap_empty_bucket_closer(ibucket_empty));
- }
-
- // Load factor is too low or a rehash will not change the neighborhood, put the value in overflow list
- if(size() < m_min_load_threshold_rehash || !will_neighborhood_change_on_rehash(ibucket_for_hash)) {
- auto it = insert_in_overflow(ibucket_for_hash, std::forward<Args>(value_type_args)...);
- return std::make_pair(iterator(m_buckets_data.end(), m_buckets_data.end(), it), true);
- }
-
- rehash(GrowthPolicy::next_bucket_count());
- ibucket_for_hash = bucket_for_hash(hash);
-
- return insert_value(ibucket_for_hash, hash, std::forward<Args>(value_type_args)...);
- }
-
- /*
- * Return true if a rehash will change the position of a key-value in the neighborhood of
- * ibucket_neighborhood_check. In this case a rehash is needed instead of puting the value in overflow list.
- */
- bool will_neighborhood_change_on_rehash(size_t ibucket_neighborhood_check) const {
- std::size_t expand_bucket_count = GrowthPolicy::next_bucket_count();
- GrowthPolicy expand_growth_policy(expand_bucket_count);
-
- const bool use_stored_hash = USE_STORED_HASH_ON_REHASH(expand_bucket_count);
- for(size_t ibucket = ibucket_neighborhood_check;
- ibucket < m_buckets_data.size() && (ibucket - ibucket_neighborhood_check) < NeighborhoodSize;
- ++ibucket)
- {
- tsl_hh_assert(!m_buckets[ibucket].empty());
-
- const size_t hash = use_stored_hash?
- m_buckets[ibucket].truncated_bucket_hash():
- hash_key(KeySelect()(m_buckets[ibucket].value()));
- if(bucket_for_hash(hash) != expand_growth_policy.bucket_for_hash(hash)) {
- return true;
- }
- }
-
- return false;
- }
-
- /*
- * Return the index of an empty bucket in m_buckets_data.
- * If none, the returned index equals m_buckets_data.size()
- */
- std::size_t find_empty_bucket(std::size_t ibucket_start) const {
- const std::size_t limit = std::min(ibucket_start + MAX_PROBES_FOR_EMPTY_BUCKET, m_buckets_data.size());
- for(; ibucket_start < limit; ibucket_start++) {
- if(m_buckets[ibucket_start].empty()) {
- return ibucket_start;
- }
- }
-
- return m_buckets_data.size();
- }
-
- /*
- * Insert value in ibucket_empty where value originally belongs to ibucket_for_hash
- *
- * Return bucket iterator to ibucket_empty
- */
- template<typename... Args>
- iterator_buckets insert_in_bucket(std::size_t ibucket_empty, std::size_t ibucket_for_hash,
- std::size_t hash, Args&&... value_type_args)
- {
- tsl_hh_assert(ibucket_empty >= ibucket_for_hash );
- tsl_hh_assert(m_buckets[ibucket_empty].empty());
- m_buckets[ibucket_empty].set_value_of_empty_bucket(hopscotch_bucket::truncate_hash(hash), std::forward<Args>(value_type_args)...);
-
- tsl_hh_assert(!m_buckets[ibucket_for_hash].empty());
- m_buckets[ibucket_for_hash].toggle_neighbor_presence(ibucket_empty - ibucket_for_hash);
- m_nb_elements++;
-
- return m_buckets_data.begin() + ibucket_empty;
- }
-
- template<class... Args, class U = OverflowContainer, typename std::enable_if<!has_key_compare<U>::value>::type* = nullptr>
- iterator_overflow insert_in_overflow(std::size_t ibucket_for_hash, Args&&... value_type_args) {
- auto it = m_overflow_elements.emplace(m_overflow_elements.end(), std::forward<Args>(value_type_args)...);
-
- m_buckets[ibucket_for_hash].set_overflow(true);
- m_nb_elements++;
-
- return it;
- }
-
- template<class... Args, class U = OverflowContainer, typename std::enable_if<has_key_compare<U>::value>::type* = nullptr>
- iterator_overflow insert_in_overflow(std::size_t ibucket_for_hash, Args&&... value_type_args) {
- auto it = m_overflow_elements.emplace(std::forward<Args>(value_type_args)...).first;
-
- m_buckets[ibucket_for_hash].set_overflow(true);
- m_nb_elements++;
-
- return it;
- }
-
- /*
- * Try to swap the bucket ibucket_empty_in_out with a bucket preceding it while keeping the neighborhood
- * conditions correct.
- *
- * If a swap was possible, the position of ibucket_empty_in_out will be closer to 0 and true will re returned.
- */
- bool swap_empty_bucket_closer(std::size_t& ibucket_empty_in_out) {
- tsl_hh_assert(ibucket_empty_in_out >= NeighborhoodSize);
- const std::size_t neighborhood_start = ibucket_empty_in_out - NeighborhoodSize + 1;
-
- for(std::size_t to_check = neighborhood_start; to_check < ibucket_empty_in_out; to_check++) {
- neighborhood_bitmap neighborhood_infos = m_buckets[to_check].neighborhood_infos();
- std::size_t to_swap = to_check;
-
- while(neighborhood_infos != 0 && to_swap < ibucket_empty_in_out) {
- if((neighborhood_infos & 1) == 1) {
- tsl_hh_assert(m_buckets[ibucket_empty_in_out].empty());
- tsl_hh_assert(!m_buckets[to_swap].empty());
-
- m_buckets[to_swap].swap_value_into_empty_bucket(m_buckets[ibucket_empty_in_out]);
-
- tsl_hh_assert(!m_buckets[to_check].check_neighbor_presence(ibucket_empty_in_out - to_check));
- tsl_hh_assert(m_buckets[to_check].check_neighbor_presence(to_swap - to_check));
-
- m_buckets[to_check].toggle_neighbor_presence(ibucket_empty_in_out - to_check);
- m_buckets[to_check].toggle_neighbor_presence(to_swap - to_check);
-
-
- ibucket_empty_in_out = to_swap;
-
- return true;
- }
-
- to_swap++;
- neighborhood_infos = neighborhood_bitmap(neighborhood_infos >> 1);
- }
- }
-
- return false;
- }
-
-
-
- template<class K, class U = ValueSelect, typename std::enable_if<has_mapped_type<U>::value>::type* = nullptr>
- typename U::value_type* find_value_impl(const K& key, std::size_t hash, hopscotch_bucket* bucket_for_hash) {
- return const_cast<typename U::value_type*>(
- static_cast<const hopscotch_hash*>(this)->find_value_impl(key, hash, bucket_for_hash));
- }
-
- /*
- * Avoid the creation of an iterator to just get the value for operator[] and at() in maps. Faster this way.
- *
- * Return null if no value for the key (TODO use std::optional when available).
- */
- template<class K, class U = ValueSelect, typename std::enable_if<has_mapped_type<U>::value>::type* = nullptr>
- const typename U::value_type* find_value_impl(const K& key, std::size_t hash,
- const hopscotch_bucket* bucket_for_hash) const
- {
- const hopscotch_bucket* bucket_found = find_in_buckets(key, hash, bucket_for_hash);
- if(bucket_found != nullptr) {
- return std::addressof(ValueSelect()(bucket_found->value()));
- }
-
- if(bucket_for_hash->has_overflow()) {
- auto it_overflow = find_in_overflow(key);
- if(it_overflow != m_overflow_elements.end()) {
- return std::addressof(ValueSelect()(*it_overflow));
- }
- }
-
- return nullptr;
- }
-
- template<class K>
- size_type count_impl(const K& key, std::size_t hash, const hopscotch_bucket* bucket_for_hash) const {
- if(find_in_buckets(key, hash, bucket_for_hash) != nullptr) {
- return 1;
- }
- else if(bucket_for_hash->has_overflow() && find_in_overflow(key) != m_overflow_elements.cend()) {
- return 1;
- }
- else {
- return 0;
- }
- }
-
- template<class K>
- iterator find_impl(const K& key, std::size_t hash, hopscotch_bucket* bucket_for_hash) {
- hopscotch_bucket* bucket_found = find_in_buckets(key, hash, bucket_for_hash);
- if(bucket_found != nullptr) {
- return iterator(m_buckets_data.begin() + std::distance(m_buckets_data.data(), bucket_found),
- m_buckets_data.end(), m_overflow_elements.begin());
- }
-
- if(!bucket_for_hash->has_overflow()) {
- return end();
- }
-
- return iterator(m_buckets_data.end(), m_buckets_data.end(), find_in_overflow(key));
- }
-
- template<class K>
- const_iterator find_impl(const K& key, std::size_t hash, const hopscotch_bucket* bucket_for_hash) const {
- const hopscotch_bucket* bucket_found = find_in_buckets(key, hash, bucket_for_hash);
- if(bucket_found != nullptr) {
- return const_iterator(m_buckets_data.cbegin() + std::distance(m_buckets_data.data(), bucket_found),
- m_buckets_data.cend(), m_overflow_elements.cbegin());
- }
-
- if(!bucket_for_hash->has_overflow()) {
- return cend();
- }
-
-
- return const_iterator(m_buckets_data.cend(), m_buckets_data.cend(), find_in_overflow(key));
- }
-
- template<class K>
- hopscotch_bucket* find_in_buckets(const K& key, std::size_t hash, hopscotch_bucket* bucket_for_hash) {
- const hopscotch_bucket* bucket_found =
- static_cast<const hopscotch_hash*>(this)->find_in_buckets(key, hash, bucket_for_hash);
- return const_cast<hopscotch_bucket*>(bucket_found);
- }
-
-
- /**
- * Return a pointer to the bucket which has the value, nullptr otherwise.
- */
- template<class K>
- const hopscotch_bucket* find_in_buckets(const K& key, std::size_t hash, const hopscotch_bucket* bucket_for_hash) const {
- (void) hash; // Avoid warning of unused variable when StoreHash is false;
-
- // TODO Try to optimize the function.
- // I tried to use ffs and __builtin_ffs functions but I could not reduce the time the function
- // takes with -march=native
-
- neighborhood_bitmap neighborhood_infos = bucket_for_hash->neighborhood_infos();
- while(neighborhood_infos != 0) {
- if((neighborhood_infos & 1) == 1) {
- // Check StoreHash before calling bucket_hash_equal. Functionally it doesn't change anythin.
- // If StoreHash is false, bucket_hash_equal is a no-op. Avoiding the call is there to help
- // GCC optimizes `hash` parameter away, it seems to not be able to do without this hint.
- if((!StoreHash || bucket_for_hash->bucket_hash_equal(hash)) &&
- compare_keys(KeySelect()(bucket_for_hash->value()), key))
- {
- return bucket_for_hash;
- }
- }
-
- ++bucket_for_hash;
- neighborhood_infos = neighborhood_bitmap(neighborhood_infos >> 1);
- }
-
- return nullptr;
- }
-
-
-
- template<class K, class U = OverflowContainer, typename std::enable_if<!has_key_compare<U>::value>::type* = nullptr>
- iterator_overflow find_in_overflow(const K& key) {
- return std::find_if(m_overflow_elements.begin(), m_overflow_elements.end(),
- [&](const value_type& value) {
- return compare_keys(key, KeySelect()(value));
- });
- }
-
- template<class K, class U = OverflowContainer, typename std::enable_if<!has_key_compare<U>::value>::type* = nullptr>
- const_iterator_overflow find_in_overflow(const K& key) const {
- return std::find_if(m_overflow_elements.cbegin(), m_overflow_elements.cend(),
- [&](const value_type& value) {
- return compare_keys(key, KeySelect()(value));
- });
- }
-
- template<class K, class U = OverflowContainer, typename std::enable_if<has_key_compare<U>::value>::type* = nullptr>
- iterator_overflow find_in_overflow(const K& key) {
- return m_overflow_elements.find(key);
- }
-
- template<class K, class U = OverflowContainer, typename std::enable_if<has_key_compare<U>::value>::type* = nullptr>
- const_iterator_overflow find_in_overflow(const K& key) const {
- return m_overflow_elements.find(key);
- }
-
-
-
- template<class U = OverflowContainer, typename std::enable_if<!has_key_compare<U>::value>::type* = nullptr>
- hopscotch_hash new_hopscotch_hash(size_type bucket_count) {
- return hopscotch_hash(bucket_count, static_cast<Hash&>(*this), static_cast<KeyEqual&>(*this),
- get_allocator(), m_max_load_factor);
- }
-
- template<class U = OverflowContainer, typename std::enable_if<has_key_compare<U>::value>::type* = nullptr>
- hopscotch_hash new_hopscotch_hash(size_type bucket_count) {
- return hopscotch_hash(bucket_count, static_cast<Hash&>(*this), static_cast<KeyEqual&>(*this),
- get_allocator(), m_max_load_factor, m_overflow_elements.key_comp());
- }
-
-public:
- static const size_type DEFAULT_INIT_BUCKETS_SIZE = 0;
- static constexpr float DEFAULT_MAX_LOAD_FACTOR = (NeighborhoodSize <= 30)?0.8f:0.9f;
-
-private:
- static const std::size_t MAX_PROBES_FOR_EMPTY_BUCKET = 12*NeighborhoodSize;
- static constexpr float MIN_LOAD_FACTOR_FOR_REHASH = 0.1f;
-
- /**
- * We can only use the hash on rehash if the size of the hash type is the same as the stored one or
- * if we use a power of two modulo. In the case of the power of two modulo, we just mask
- * the least significant bytes, we just have to check that the truncated_hash_type didn't truncated
- * too much bytes.
- */
- template<class T = size_type, typename std::enable_if<std::is_same<T, truncated_hash_type>::value>::type* = nullptr>
- static bool USE_STORED_HASH_ON_REHASH(size_type /*bucket_count*/) {
- return StoreHash;
- }
-
- template<class T = size_type, typename std::enable_if<!std::is_same<T, truncated_hash_type>::value>::type* = nullptr>
- static bool USE_STORED_HASH_ON_REHASH(size_type bucket_count) {
- (void) bucket_count;
- if(StoreHash && is_power_of_two_policy<GrowthPolicy>::value) {
- tsl_hh_assert(bucket_count > 0);
- return (bucket_count - 1) <= std::numeric_limits<truncated_hash_type>::max();
- }
- else {
- return false;
- }
- }
-
- /**
- * Return an always valid pointer to an static empty hopscotch_bucket.
- */
- hopscotch_bucket* static_empty_bucket_ptr() {
- static hopscotch_bucket empty_bucket;
- return &empty_bucket;
- }
-
-private:
- buckets_container_type m_buckets_data;
- overflow_container_type m_overflow_elements;
-
- /**
- * Points to m_buckets_data.data() if !m_buckets_data.empty() otherwise points to static_empty_bucket_ptr.
- * This variable is useful to avoid the cost of checking if m_buckets_data is empty when trying
- * to find an element.
- *
- * TODO Remove m_buckets_data and only use a pointer+size instead of a pointer+vector to save some space in the hopscotch_hash object.
- */
- hopscotch_bucket* m_buckets;
-
- size_type m_nb_elements;
-
- /**
- * Min size of the hash table before a rehash can occurs automatically (except if m_max_load_threshold_rehash os reached).
- * If the neighborhood of a bucket is full before the min is reacher, the elements are put into m_overflow_elements.
- */
- size_type m_min_load_threshold_rehash;
-
- /**
- * Max size of the hash table before a rehash occurs automatically to grow the table.
- */
- size_type m_max_load_threshold_rehash;
-
- float m_max_load_factor;
-};
-
-} // end namespace detail_hopscotch_hash
-
-
-} // end namespace tsl
-
-#endif
diff --git a/benchmarks/others/hopscotch_map.h b/benchmarks/others/hopscotch_map.h
deleted file mode 100644
index f9fa41f0..00000000
--- a/benchmarks/others/hopscotch_map.h
+++ /dev/null
@@ -1,710 +0,0 @@
-/**
- * MIT License
- *
- * Copyright (c) 2017 Thibaut Goetghebuer-Planchon <[email protected]>
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy
- * of this software and associated documentation files (the "Software"), to deal
- * in the Software without restriction, including without limitation the rights
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- * copies of the Software, and to permit persons to whom the Software is
- * furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in all
- * copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
- * SOFTWARE.
- */
-#ifndef TSL_HOPSCOTCH_MAP_H
-#define TSL_HOPSCOTCH_MAP_H
-
-
-#include <algorithm>
-#include <cstddef>
-#include <functional>
-#include <initializer_list>
-#include <list>
-#include <memory>
-#include <type_traits>
-#include <utility>
-#include "hopscotch_hash.h"
-
-
-namespace tsl {
-
-/**
- * Implementation of a hash map using the hopscotch hashing algorithm.
- *
- * The Key and the value T must be either nothrow move-constructible, copy-constructible or both.
- *
- * The size of the neighborhood (NeighborhoodSize) must be > 0 and <= 62 if StoreHash is false.
- * When StoreHash is true, 32-bits of the hash will be stored alongside the neighborhood limiting
- * the NeighborhoodSize to <= 30. There is no memory usage difference between
- * 'NeighborhoodSize 62; StoreHash false' and 'NeighborhoodSize 30; StoreHash true'.
- *
- * Storing the hash may improve performance on insert during the rehash process if the hash takes time
- * to compute. It may also improve read performance if the KeyEqual function takes time (or incurs a cache-miss).
- * If used with simple Hash and KeyEqual it may slow things down.
- *
- * StoreHash can only be set if the GrowthPolicy is set to tsl::power_of_two_growth_policy.
- *
- * GrowthPolicy defines how the map grows and consequently how a hash value is mapped to a bucket.
- * By default the map uses tsl::power_of_two_growth_policy. This policy keeps the number of buckets
- * to a power of two and uses a mask to map the hash to a bucket instead of the slow modulo.
- * You may define your own growth policy, check tsl::power_of_two_growth_policy for the interface.
- *
- * If the destructors of Key or T throw an exception, behaviour of the class is undefined.
- *
- * Iterators invalidation:
- * - clear, operator=, reserve, rehash: always invalidate the iterators.
- * - insert, emplace, emplace_hint, operator[]: if there is an effective insert, invalidate the iterators
- * if a displacement is needed to resolve a collision (which mean that most of the time,
- * insert will invalidate the iterators). Or if there is a rehash.
- * - erase: iterator on the erased element is the only one which become invalid.
- */
-template<class Key,
- class T,
- class Hash = std::hash<Key>,
- class KeyEqual = std::equal_to<Key>,
- class Allocator = std::allocator<std::pair<Key, T>>,
- unsigned int NeighborhoodSize = 62,
- bool StoreHash = false,
- class GrowthPolicy = tsl::hh::power_of_two_growth_policy<2>>
-class hopscotch_map {
-private:
- template<typename U>
- using has_is_transparent = tsl::detail_hopscotch_hash::has_is_transparent<U>;
-
- class KeySelect {
- public:
- using key_type = Key;
-
- const key_type& operator()(const std::pair<Key, T>& key_value) const {
- return key_value.first;
- }
-
- key_type& operator()(std::pair<Key, T>& key_value) {
- return key_value.first;
- }
- };
-
- class ValueSelect {
- public:
- using value_type = T;
-
- const value_type& operator()(const std::pair<Key, T>& key_value) const {
- return key_value.second;
- }
-
- value_type& operator()(std::pair<Key, T>& key_value) {
- return key_value.second;
- }
- };
-
-
- using overflow_container_type = std::list<std::pair<Key, T>, Allocator>;
- using ht = detail_hopscotch_hash::hopscotch_hash<std::pair<Key, T>, KeySelect, ValueSelect,
- Hash, KeyEqual,
- Allocator, NeighborhoodSize,
- StoreHash, GrowthPolicy,
- overflow_container_type>;
-
-public:
- using key_type = typename ht::key_type;
- using mapped_type = T;
- using value_type = typename ht::value_type;
- using size_type = typename ht::size_type;
- using difference_type = typename ht::difference_type;
- using hasher = typename ht::hasher;
- using key_equal = typename ht::key_equal;
- using allocator_type = typename ht::allocator_type;
- using reference = typename ht::reference;
- using const_reference = typename ht::const_reference;
- using pointer = typename ht::pointer;
- using const_pointer = typename ht::const_pointer;
- using iterator = typename ht::iterator;
- using const_iterator = typename ht::const_iterator;
-
-
-
- /*
- * Constructors
- */
- hopscotch_map() : hopscotch_map(ht::DEFAULT_INIT_BUCKETS_SIZE) {
- }
-
- explicit hopscotch_map(size_type bucket_count,
- const Hash& hash = Hash(),
- const KeyEqual& equal = KeyEqual(),
- const Allocator& alloc = Allocator()) :
- m_ht(bucket_count, hash, equal, alloc, ht::DEFAULT_MAX_LOAD_FACTOR)
- {
- }
-
- hopscotch_map(size_type bucket_count,
- const Allocator& alloc) : hopscotch_map(bucket_count, Hash(), KeyEqual(), alloc)
- {
- }
-
- hopscotch_map(size_type bucket_count,
- const Hash& hash,
- const Allocator& alloc) : hopscotch_map(bucket_count, hash, KeyEqual(), alloc)
- {
- }
-
- explicit hopscotch_map(const Allocator& alloc) : hopscotch_map(ht::DEFAULT_INIT_BUCKETS_SIZE, alloc) {
- }
-
- template<class InputIt>
- hopscotch_map(InputIt first, InputIt last,
- size_type bucket_count = ht::DEFAULT_INIT_BUCKETS_SIZE,
- const Hash& hash = Hash(),
- const KeyEqual& equal = KeyEqual(),
- const Allocator& alloc = Allocator()) : hopscotch_map(bucket_count, hash, equal, alloc)
- {
- insert(first, last);
- }
-
- template<class InputIt>
- hopscotch_map(InputIt first, InputIt last,
- size_type bucket_count,
- const Allocator& alloc) : hopscotch_map(first, last, bucket_count, Hash(), KeyEqual(), alloc)
- {
- }
-
- template<class InputIt>
- hopscotch_map(InputIt first, InputIt last,
- size_type bucket_count,
- const Hash& hash,
- const Allocator& alloc) : hopscotch_map(first, last, bucket_count, hash, KeyEqual(), alloc)
- {
- }
-
- hopscotch_map(std::initializer_list<value_type> init,
- size_type bucket_count = ht::DEFAULT_INIT_BUCKETS_SIZE,
- const Hash& hash = Hash(),
- const KeyEqual& equal = KeyEqual(),
- const Allocator& alloc = Allocator()) :
- hopscotch_map(init.begin(), init.end(), bucket_count, hash, equal, alloc)
- {
- }
-
- hopscotch_map(std::initializer_list<value_type> init,
- size_type bucket_count,
- const Allocator& alloc) :
- hopscotch_map(init.begin(), init.end(), bucket_count, Hash(), KeyEqual(), alloc)
- {
- }
-
- hopscotch_map(std::initializer_list<value_type> init,
- size_type bucket_count,
- const Hash& hash,
- const Allocator& alloc) :
- hopscotch_map(init.begin(), init.end(), bucket_count, hash, KeyEqual(), alloc)
- {
- }
-
-
- hopscotch_map& operator=(std::initializer_list<value_type> ilist) {
- m_ht.clear();
-
- m_ht.reserve(ilist.size());
- m_ht.insert(ilist.begin(), ilist.end());
-
- return *this;
- }
-
- allocator_type get_allocator() const { return m_ht.get_allocator(); }
-
-
- /*
- * Iterators
- */
- iterator begin() noexcept { return m_ht.begin(); }
- const_iterator begin() const noexcept { return m_ht.begin(); }
- const_iterator cbegin() const noexcept { return m_ht.cbegin(); }
-
- iterator end() noexcept { return m_ht.end(); }
- const_iterator end() const noexcept { return m_ht.end(); }
- const_iterator cend() const noexcept { return m_ht.cend(); }
-
-
- /*
- * Capacity
- */
- bool empty() const noexcept { return m_ht.empty(); }
- size_type size() const noexcept { return m_ht.size(); }
- size_type max_size() const noexcept { return m_ht.max_size(); }
-
- /*
- * Modifiers
- */
- void clear() noexcept { m_ht.clear(); }
-
-
-
-
- std::pair<iterator, bool> insert(const value_type& value) {
- return m_ht.insert(value);
- }
-
- template<class P, typename std::enable_if<std::is_constructible<value_type, P&&>::value>::type* = nullptr>
- std::pair<iterator, bool> insert(P&& value) {
- return m_ht.insert(std::forward<P>(value));
- }
-
- std::pair<iterator, bool> insert(value_type&& value) {
- return m_ht.insert(std::move(value));
- }
-
-
- iterator insert(const_iterator hint, const value_type& value) {
- return m_ht.insert(hint, value);
- }
-
- template<class P, typename std::enable_if<std::is_constructible<value_type, P&&>::value>::type* = nullptr>
- iterator insert(const_iterator hint, P&& value) {
- return m_ht.insert(hint, std::forward<P>(value));
- }
-
- iterator insert(const_iterator hint, value_type&& value) {
- return m_ht.insert(hint, std::move(value));
- }
-
-
- template<class InputIt>
- void insert(InputIt first, InputIt last) {
- m_ht.insert(first, last);
- }
-
- void insert(std::initializer_list<value_type> ilist) {
- m_ht.insert(ilist.begin(), ilist.end());
- }
-
-
-
-
- template<class M>
- std::pair<iterator, bool> insert_or_assign(const key_type& k, M&& obj) {
- return m_ht.insert_or_assign(k, std::forward<M>(obj));
- }
-
- template<class M>
- std::pair<iterator, bool> insert_or_assign(key_type&& k, M&& obj) {
- return m_ht.insert_or_assign(std::move(k), std::forward<M>(obj));
- }
-
- template<class M>
- iterator insert_or_assign(const_iterator hint, const key_type& k, M&& obj) {
- return m_ht.insert_or_assign(hint, k, std::forward<M>(obj));
- }
-
- template<class M>
- iterator insert_or_assign(const_iterator hint, key_type&& k, M&& obj) {
- return m_ht.insert_or_assign(hint, std::move(k), std::forward<M>(obj));
- }
-
-
-
-
- /**
- * Due to the way elements are stored, emplace will need to move or copy the key-value once.
- * The method is equivalent to insert(value_type(std::forward<Args>(args)...));
- *
- * Mainly here for compatibility with the std::unordered_map interface.
- */
- template<class... Args>
- std::pair<iterator, bool> emplace(Args&&... args) {
- return m_ht.emplace(std::forward<Args>(args)...);
- }
-
-
-
-
- /**
- * Due to the way elements are stored, emplace_hint will need to move or copy the key-value once.
- * The method is equivalent to insert(hint, value_type(std::forward<Args>(args)...));
- *
- * Mainly here for compatibility with the std::unordered_map interface.
- */
- template<class... Args>
- iterator emplace_hint(const_iterator hint, Args&&... args) {
- return m_ht.emplace_hint(hint, std::forward<Args>(args)...);
- }
-
-
-
-
- template<class... Args>
- std::pair<iterator, bool> try_emplace(const key_type& k, Args&&... args) {
- return m_ht.try_emplace(k, std::forward<Args>(args)...);
- }
-
- template<class... Args>
- std::pair<iterator, bool> try_emplace(key_type&& k, Args&&... args) {
- return m_ht.try_emplace(std::move(k), std::forward<Args>(args)...);
- }
-
- template<class... Args>
- iterator try_emplace(const_iterator hint, const key_type& k, Args&&... args) {
- return m_ht.try_emplace(hint, k, std::forward<Args>(args)...);
- }
-
- template<class... Args>
- iterator try_emplace(const_iterator hint, key_type&& k, Args&&... args) {
- return m_ht.try_emplace(hint, std::move(k), std::forward<Args>(args)...);
- }
-
-
-
-
- iterator erase(iterator pos) { return m_ht.erase(pos); }
- iterator erase(const_iterator pos) { return m_ht.erase(pos); }
- iterator erase(const_iterator first, const_iterator last) { return m_ht.erase(first, last); }
- size_type erase(const key_type& key) { return m_ht.erase(key); }
-
- /**
- * Use the hash value 'precalculated_hash' instead of hashing the key. The hash value should be the same
- * as hash_function()(key). Useful to speed-up the lookup to the value if you already have the hash.
- */
- size_type erase(const key_type& key, std::size_t precalculated_hash) {
- return m_ht.erase(key, precalculated_hash);
- }
-
- /**
- * This overload only participates in the overload resolution if the typedef KeyEqual::is_transparent exists.
- * If so, K must be hashable and comparable to Key.
- */
- template<class K, class KE = KeyEqual, typename std::enable_if<has_is_transparent<KE>::value>::type* = nullptr>
- size_type erase(const K& key) { return m_ht.erase(key); }
-
- /**
- * @copydoc erase(const K& key)
- *
- * Use the hash value 'precalculated_hash' instead of hashing the key. The hash value should be the same
- * as hash_function()(key). Useful to speed-up the lookup to the value if you already have the hash.
- */
- template<class K, class KE = KeyEqual, typename std::enable_if<has_is_transparent<KE>::value>::type* = nullptr>
- size_type erase(const K& key, std::size_t precalculated_hash) {
- return m_ht.erase(key, precalculated_hash);
- }
-
-
-
-
- void swap(hopscotch_map& other) { other.m_ht.swap(m_ht); }
-
- /*
- * Lookup
- */
- T& at(const Key& key) { return m_ht.at(key); }
-
- /**
- * Use the hash value 'precalculated_hash' instead of hashing the key. The hash value should be the same
- * as hash_function()(key). Useful to speed-up the lookup if you already have the hash.
- */
- T& at(const Key& key, std::size_t precalculated_hash) { return m_ht.at(key, precalculated_hash); }
-
-
- const T& at(const Key& key) const { return m_ht.at(key); }
-
- /**
- * @copydoc at(const Key& key, std::size_t precalculated_hash)
- */
- const T& at(const Key& key, std::size_t precalculated_hash) const { return m_ht.at(key, precalculated_hash); }
-
-
- /**
- * This overload only participates in the overload resolution if the typedef KeyEqual::is_transparent exists.
- * If so, K must be hashable and comparable to Key.
- */
- template<class K, class KE = KeyEqual, typename std::enable_if<has_is_transparent<KE>::value>::type* = nullptr>
- T& at(const K& key) { return m_ht.at(key); }
-
- /**
- * @copydoc at(const K& key)
- *
- * Use the hash value 'precalculated_hash' instead of hashing the key. The hash value should be the same
- * as hash_function()(key). Useful to speed-up the lookup if you already have the hash.
- */
- template<class K, class KE = KeyEqual, typename std::enable_if<has_is_transparent<KE>::value>::type* = nullptr>
- T& at(const K& key, std::size_t precalculated_hash) { return m_ht.at(key, precalculated_hash); }
-
-
- /**
- * @copydoc at(const K& key)
- */
- template<class K, class KE = KeyEqual, typename std::enable_if<has_is_transparent<KE>::value>::type* = nullptr>
- const T& at(const K& key) const { return m_ht.at(key); }
-
- /**
- * @copydoc at(const K& key, std::size_t precalculated_hash)
- */
- template<class K, class KE = KeyEqual, typename std::enable_if<has_is_transparent<KE>::value>::type* = nullptr>
- const T& at(const K& key, std::size_t precalculated_hash) const { return m_ht.at(key, precalculated_hash); }
-
-
-
-
- T& operator[](const Key& key) { return m_ht[key]; }
- T& operator[](Key&& key) { return m_ht[std::move(key)]; }
-
-
-
-
- size_type count(const Key& key) const { return m_ht.count(key); }
-
- /**
- * Use the hash value 'precalculated_hash' instead of hashing the key. The hash value should be the same
- * as hash_function()(key). Useful to speed-up the lookup if you already have the hash.
- */
- size_type count(const Key& key, std::size_t precalculated_hash) const {
- return m_ht.count(key, precalculated_hash);
- }
-
- /**
- * This overload only participates in the overload resolution if the typedef KeyEqual::is_transparent exists.
- * If so, K must be hashable and comparable to Key.
- */
- template<class K, class KE = KeyEqual, typename std::enable_if<has_is_transparent<KE>::value>::type* = nullptr>
- size_type count(const K& key) const { return m_ht.count(key); }
-
- /**
- * @copydoc count(const K& key) const
- *
- * Use the hash value 'precalculated_hash' instead of hashing the key. The hash value should be the same
- * as hash_function()(key). Useful to speed-up the lookup if you already have the hash.
- */
- template<class K, class KE = KeyEqual, typename std::enable_if<has_is_transparent<KE>::value>::type* = nullptr>
- size_type count(const K& key, std::size_t precalculated_hash) const { return m_ht.count(key, precalculated_hash); }
-
-
-
-
- iterator find(const Key& key) { return m_ht.find(key); }
-
- /**
- * Use the hash value 'precalculated_hash' instead of hashing the key. The hash value should be the same
- * as hash_function()(key). Useful to speed-up the lookup if you already have the hash.
- */
- iterator find(const Key& key, std::size_t precalculated_hash) { return m_ht.find(key, precalculated_hash); }
-
- const_iterator find(const Key& key) const { return m_ht.find(key); }
-
- /**
- * @copydoc find(const Key& key, std::size_t precalculated_hash)
- */
- const_iterator find(const Key& key, std::size_t precalculated_hash) const {
- return m_ht.find(key, precalculated_hash);
- }
-
- /**
- * This overload only participates in the overload resolution if the typedef KeyEqual::is_transparent exists.
- * If so, K must be hashable and comparable to Key.
- */
- template<class K, class KE = KeyEqual, typename std::enable_if<has_is_transparent<KE>::value>::type* = nullptr>
- iterator find(const K& key) { return m_ht.find(key); }
-
- /**
- * @copydoc find(const K& key)
- *
- * Use the hash value 'precalculated_hash' instead of hashing the key. The hash value should be the same
- * as hash_function()(key). Useful to speed-up the lookup if you already have the hash.
- */
- template<class K, class KE = KeyEqual, typename std::enable_if<has_is_transparent<KE>::value>::type* = nullptr>
- iterator find(const K& key, std::size_t precalculated_hash) { return m_ht.find(key, precalculated_hash); }
-
- /**
- * @copydoc find(const K& key)
- */
- template<class K, class KE = KeyEqual, typename std::enable_if<has_is_transparent<KE>::value>::type* = nullptr>
- const_iterator find(const K& key) const { return m_ht.find(key); }
-
- /**
- * @copydoc find(const K& key)
- *
- * Use the hash value 'precalculated_hash' instead of hashing the key. The hash value should be the same
- * as hash_function()(key). Useful to speed-up the lookup if you already have the hash.
- */
- template<class K, class KE = KeyEqual, typename std::enable_if<has_is_transparent<KE>::value>::type* = nullptr>
- const_iterator find(const K& key, std::size_t precalculated_hash) const {
- return m_ht.find(key, precalculated_hash);
- }
-
-
-
-
- bool contains(const Key& key) const { return m_ht.contains(key); }
-
- /**
- * Use the hash value 'precalculated_hash' instead of hashing the key. The hash value should be the same
- * as hash_function()(key). Useful to speed-up the lookup if you already have the hash.
- */
- bool contains(const Key& key, std::size_t precalculated_hash) const {
- return m_ht.contains(key, precalculated_hash);
- }
-
- /**
- * This overload only participates in the overload resolution if the typedef KeyEqual::is_transparent exists.
- * If so, K must be hashable and comparable to Key.
- */
- template<class K, class KE = KeyEqual, typename std::enable_if<has_is_transparent<KE>::value>::type* = nullptr>
- bool contains(const K& key) const { return m_ht.contains(key); }
-
- /**
- * @copydoc contains(const K& key) const
- *
- * Use the hash value 'precalculated_hash' instead of hashing the key. The hash value should be the same
- * as hash_function()(key). Useful to speed-up the lookup if you already have the hash.
- */
- template<class K, class KE = KeyEqual, typename std::enable_if<has_is_transparent<KE>::value>::type* = nullptr>
- bool contains(const K& key, std::size_t precalculated_hash) const {
- return m_ht.contains(key, precalculated_hash);
- }
-
-
-
-
- std::pair<iterator, iterator> equal_range(const Key& key) { return m_ht.equal_range(key); }
-
- /**
- * Use the hash value 'precalculated_hash' instead of hashing the key. The hash value should be the same
- * as hash_function()(key). Useful to speed-up the lookup if you already have the hash.
- */
- std::pair<iterator, iterator> equal_range(const Key& key, std::size_t precalculated_hash) {
- return m_ht.equal_range(key, precalculated_hash);
- }
-
- std::pair<const_iterator, const_iterator> equal_range(const Key& key) const { return m_ht.equal_range(key); }
-
- /**
- * @copydoc equal_range(const Key& key, std::size_t precalculated_hash)
- */
- std::pair<const_iterator, const_iterator> equal_range(const Key& key, std::size_t precalculated_hash) const {
- return m_ht.equal_range(key, precalculated_hash);
- }
-
- /**
- * This overload only participates in the overload resolution if the typedef KeyEqual::is_transparent exists.
- * If so, K must be hashable and comparable to Key.
- */
- template<class K, class KE = KeyEqual, typename std::enable_if<has_is_transparent<KE>::value>::type* = nullptr>
- std::pair<iterator, iterator> equal_range(const K& key) { return m_ht.equal_range(key); }
-
-
- /**
- * @copydoc equal_range(const K& key)
- *
- * Use the hash value 'precalculated_hash' instead of hashing the key. The hash value should be the same
- * as hash_function()(key). Useful to speed-up the lookup if you already have the hash.
- */
- template<class K, class KE = KeyEqual, typename std::enable_if<has_is_transparent<KE>::value>::type* = nullptr>
- std::pair<iterator, iterator> equal_range(const K& key, std::size_t precalculated_hash) {
- return m_ht.equal_range(key, precalculated_hash);
- }
-
- /**
- * @copydoc equal_range(const K& key)
- */
- template<class K, class KE = KeyEqual, typename std::enable_if<has_is_transparent<KE>::value>::type* = nullptr>
- std::pair<const_iterator, const_iterator> equal_range(const K& key) const { return m_ht.equal_range(key); }
-
- /**
- * @copydoc equal_range(const K& key, std::size_t precalculated_hash)
- */
- template<class K, class KE = KeyEqual, typename std::enable_if<has_is_transparent<KE>::value>::type* = nullptr>
- std::pair<const_iterator, const_iterator> equal_range(const K& key, std::size_t precalculated_hash) const {
- return m_ht.equal_range(key, precalculated_hash);
- }
-
-
-
-
- /*
- * Bucket interface
- */
- size_type bucket_count() const { return m_ht.bucket_count(); }
- size_type max_bucket_count() const { return m_ht.max_bucket_count(); }
-
-
- /*
- * Hash policy
- */
- float load_factor() const { return m_ht.load_factor(); }
- float max_load_factor() const { return m_ht.max_load_factor(); }
- void max_load_factor(float ml) { m_ht.max_load_factor(ml); }
-
- void rehash(size_type count_) { m_ht.rehash(count_); }
- void reserve(size_type count_) { m_ht.reserve(count_); }
-
-
- /*
- * Observers
- */
- hasher hash_function() const { return m_ht.hash_function(); }
- key_equal key_eq() const { return m_ht.key_eq(); }
-
- /*
- * Other
- */
-
- /**
- * Convert a const_iterator to an iterator.
- */
- iterator mutable_iterator(const_iterator pos) {
- return m_ht.mutable_iterator(pos);
- }
-
- size_type overflow_size() const noexcept { return m_ht.overflow_size(); }
-
- friend bool operator==(const hopscotch_map& lhs, const hopscotch_map& rhs) {
- if(lhs.size() != rhs.size()) {
- return false;
- }
-
- for(const auto& element_lhs : lhs) {
- const auto it_element_rhs = rhs.find(element_lhs.first);
- if(it_element_rhs == rhs.cend() || element_lhs.second != it_element_rhs->second) {
- return false;
- }
- }
-
- return true;
- }
-
- friend bool operator!=(const hopscotch_map& lhs, const hopscotch_map& rhs) {
- return !operator==(lhs, rhs);
- }
-
- friend void swap(hopscotch_map& lhs, hopscotch_map& rhs) {
- lhs.swap(rhs);
- }
-
-
-
-private:
- ht m_ht;
-};
-
-
-/**
- * Same as `tsl::hopscotch_map<Key, T, Hash, KeyEqual, Allocator, NeighborhoodSize, StoreHash, tsl::hh::prime_growth_policy>`.
- */
-template<class Key,
- class T,
- class Hash = std::hash<Key>,
- class KeyEqual = std::equal_to<Key>,
- class Allocator = std::allocator<std::pair<Key, T>>,
- unsigned int NeighborhoodSize = 62,
- bool StoreHash = false>
-using hopscotch_pg_map = hopscotch_map<Key, T, Hash, KeyEqual, Allocator, NeighborhoodSize, StoreHash, tsl::hh::prime_growth_policy>;
-
-} // end namespace tsl
-
-#endif
diff --git a/benchmarks/others/khashl.h b/benchmarks/others/khashl.h
deleted file mode 100644
index 3542ba98..00000000
--- a/benchmarks/others/khashl.h
+++ /dev/null
@@ -1,345 +0,0 @@
-/* The MIT License
- Copyright (c) 2019 by Attractive Chaos <[email protected]>
- Permission is hereby granted, free of charge, to any person obtaining
- a copy of this software and associated documentation files (the
- "Software"), to deal in the Software without restriction, including
- without limitation the rights to use, copy, modify, merge, publish,
- distribute, sublicense, and/or sell copies of the Software, and to
- permit persons to whom the Software is furnished to do so, subject to
- the following conditions:
- The above copyright notice and this permission notice shall be
- included in all copies or substantial portions of the Software.
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
- EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
- MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
- NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
- BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
- ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
- CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
- SOFTWARE.
-*/
-
-#ifndef __AC_KHASHL_H
-#define __AC_KHASHL_H
-
-#define AC_VERSION_KHASHL_H "0.1"
-
-#include <stdlib.h>
-#include <string.h>
-#include <limits.h>
-
-/************************************
- * Compiler specific configurations *
- ************************************/
-
-#if UINT_MAX == 0xffffffffu
-typedef unsigned int khint32_t;
-#elif ULONG_MAX == 0xffffffffu
-typedef unsigned long khint32_t;
-#endif
-
-#if ULONG_MAX == ULLONG_MAX
-typedef unsigned long khint64_t;
-#else
-typedef unsigned long long khint64_t;
-#endif
-
-#ifndef kh_inline
-#ifdef _MSC_VER
-#define kh_inline __inline
-#else
-#define kh_inline inline
-#endif
-#endif /* kh_inline */
-
-#ifndef klib_unused
-#if (defined __clang__ && __clang_major__ >= 3) || (defined __GNUC__ && __GNUC__ >= 3)
-#define klib_unused __attribute__ ((__unused__))
-#else
-#define klib_unused
-#endif
-#endif /* klib_unused */
-
-#define KH_LOCAL static kh_inline klib_unused
-
-typedef khint32_t khint_t;
-
-/******************
- * malloc aliases *
- ******************/
-
-#ifndef kcalloc
-#define kcalloc(N,Z) calloc(N,Z)
-#endif
-#ifndef kmalloc
-#define kmalloc(Z) malloc(Z)
-#endif
-#ifndef krealloc
-#define krealloc(P,Z) realloc(P,Z)
-#endif
-#ifndef kfree
-#define kfree(P) free(P)
-#endif
-
-/****************************
- * Simple private functions *
- ****************************/
-
-#define __kh_used(flag, i) (flag[i>>5] >> (i&0x1fU) & 1U)
-#define __kh_set_used(flag, i) (flag[i>>5] |= 1U<<(i&0x1fU))
-#define __kh_set_unused(flag, i) (flag[i>>5] &= ~(1U<<(i&0x1fU)))
-
-#define __kh_fsize(m) ((m) < 32? 1 : (m)>>5)
-
-static kh_inline khint_t __kh_h2b(khint_t hash, khint_t bits) { return hash * 2654435769U >> (32 - bits); }
-
-/*******************
- * Hash table base *
- *******************/
-
-#define __KHASHL_TYPE(HType, khkey_t) \
- typedef struct { \
- khint_t bits, count; \
- khint32_t *used; \
- khkey_t *keys; \
- } HType;
-
-#define __KHASHL_PROTOTYPES(HType, prefix, khkey_t) \
- extern HType *prefix##_init(void); \
- extern void prefix##_destroy(HType *h); \
- extern void prefix##_clear(HType *h); \
- extern khint_t prefix##_getp(const HType *h, const khkey_t *key); \
- extern int prefix##_resize(HType *h, khint_t new_n_buckets); \
- extern khint_t prefix##_putp(HType *h, const khkey_t *key, int *absent); \
- extern void prefix##_del(HType *h, khint_t k);
-
-#define __KHASHL_IMPL_BASIC(SCOPE, HType, prefix) \
- SCOPE HType *prefix##_init(void) { \
- return (HType*)kcalloc(1, sizeof(HType)); \
- } \
- SCOPE void prefix##_destroy(HType *h) { \
- if (!h) return; \
- kfree((void *)h->keys); kfree(h->used); \
- kfree(h); \
- } \
- SCOPE void prefix##_clear(HType *h) { \
- if (h && h->used) { \
- uint32_t n_buckets = 1U << h->bits; \
- memset(h->used, 0, __kh_fsize(n_buckets) * sizeof(khint32_t)); \
- h->count = 0; \
- } \
- }
-
-#define __KHASHL_IMPL_GET(SCOPE, HType, prefix, khkey_t, __hash_fn, __hash_eq) \
- SCOPE khint_t prefix##_getp(const HType *h, const khkey_t *key) { \
- khint_t i, last, n_buckets, mask; \
- if (h->keys == 0) return 0; \
- n_buckets = 1U << h->bits; \
- mask = n_buckets - 1U; \
- i = last = __kh_h2b(__hash_fn(*key), h->bits); \
- while (__kh_used(h->used, i) && !__hash_eq(h->keys[i], *key)) { \
- i = (i + 1U) & mask; \
- if (i == last) return n_buckets; \
- } \
- return !__kh_used(h->used, i)? n_buckets : i; \
- } \
- SCOPE khint_t prefix##_get(const HType *h, khkey_t key) { return prefix##_getp(h, &key); }
-
-#define __KHASHL_IMPL_RESIZE(SCOPE, HType, prefix, khkey_t, __hash_fn, __hash_eq) \
- SCOPE int prefix##_resize(HType *h, khint_t new_n_buckets) { \
- khint32_t *new_used = 0; \
- khint_t j = 0, x = new_n_buckets, n_buckets, new_bits, new_mask; \
- while ((x >>= 1) != 0) ++j; \
- if (new_n_buckets & (new_n_buckets - 1)) ++j; \
- new_bits = j > 2? j : 2; \
- new_n_buckets = 1U << new_bits; \
- if (h->count > (new_n_buckets>>1) + (new_n_buckets>>2)) return 0; /* requested size is too small */ \
- new_used = (khint32_t*)kmalloc(__kh_fsize(new_n_buckets) * sizeof(khint32_t)); \
- memset(new_used, 0, __kh_fsize(new_n_buckets) * sizeof(khint32_t)); \
- if (!new_used) return -1; /* not enough memory */ \
- n_buckets = h->keys? 1U<<h->bits : 0U; \
- if (n_buckets < new_n_buckets) { /* expand */ \
- khkey_t *new_keys = (khkey_t*)krealloc((void*)h->keys, new_n_buckets * sizeof(khkey_t)); \
- if (!new_keys) { kfree(new_used); return -1; } \
- h->keys = new_keys; \
- } /* otherwise shrink */ \
- new_mask = new_n_buckets - 1; \
- for (j = 0; j != n_buckets; ++j) { \
- khkey_t key; \
- if (!__kh_used(h->used, j)) continue; \
- key = h->keys[j]; \
- __kh_set_unused(h->used, j); \
- while (1) { /* kick-out process; sort of like in Cuckoo hashing */ \
- khint_t i; \
- i = __kh_h2b(__hash_fn(key), new_bits); \
- while (__kh_used(new_used, i)) i = (i + 1) & new_mask; \
- __kh_set_used(new_used, i); \
- if (i < n_buckets && __kh_used(h->used, i)) { /* kick out the existing element */ \
- { khkey_t tmp = h->keys[i]; h->keys[i] = key; key = tmp; } \
- __kh_set_unused(h->used, i); /* mark it as deleted in the old hash table */ \
- } else { /* write the element and jump out of the loop */ \
- h->keys[i] = key; \
- break; \
- } \
- } \
- } \
- if (n_buckets > new_n_buckets) /* shrink the hash table */ \
- h->keys = (khkey_t*)krealloc((void *)h->keys, new_n_buckets * sizeof(khkey_t)); \
- kfree(h->used); /* free the working space */ \
- h->used = new_used, h->bits = new_bits; \
- return 0; \
- }
-
-#define __KHASHL_IMPL_PUT(SCOPE, HType, prefix, khkey_t, __hash_fn, __hash_eq) \
- SCOPE khint_t prefix##_putp(HType *h, const khkey_t *key, int *absent) { \
- khint_t n_buckets, i, last, mask; \
- n_buckets = h->keys? 1U<<h->bits : 0U; \
- *absent = -1; \
- if (h->count >= (n_buckets>>1) + (n_buckets>>2)) { /* rehashing */ \
- if (prefix##_resize(h, n_buckets + 1U) < 0) \
- return n_buckets; \
- n_buckets = 1U<<h->bits; \
- } /* TODO: to implement automatically shrinking; resize() already support shrinking */ \
- mask = n_buckets - 1; \
- i = last = __kh_h2b(__hash_fn(*key), h->bits); \
- while (__kh_used(h->used, i) && !__hash_eq(h->keys[i], *key)) { \
- i = (i + 1U) & mask; \
- if (i == last) break; \
- } \
- if (!__kh_used(h->used, i)) { /* not present at all */ \
- h->keys[i] = *key; \
- __kh_set_used(h->used, i); \
- ++h->count; \
- *absent = 1; \
- } else *absent = 0; /* Don't touch h->keys[i] if present */ \
- return i; \
- } \
- SCOPE khint_t prefix##_put(HType *h, khkey_t key, int *absent) { return prefix##_putp(h, &key, absent); }
-
-#define __KHASHL_IMPL_DEL(SCOPE, HType, prefix, khkey_t, __hash_fn) \
- SCOPE int prefix##_del(HType *h, khint_t i) { \
- khint_t j = i, k, mask, n_buckets; \
- if (h->keys == 0) return 0; \
- n_buckets = 1U<<h->bits; \
- mask = n_buckets - 1U; \
- while (1) { \
- j = (j + 1U) & mask; \
- if (j == i || !__kh_used(h->used, j)) break; /* j==i only when the table is completely full */ \
- k = __kh_h2b(__hash_fn(h->keys[j]), h->bits); \
- if ((j > i && (k <= i || k > j)) || (j < i && (k <= i && k > j))) \
- h->keys[i] = h->keys[j], i = j; \
- } \
- __kh_set_unused(h->used, i); \
- --h->count; \
- return 1; \
- }
-
-#define KHASHL_DECLARE(HType, prefix, khkey_t) \
- __KHASHL_TYPE(HType, khkey_t) \
- __KHASHL_PROTOTYPES(HType, prefix, khkey_t)
-
-#define KHASHL_INIT(SCOPE, HType, prefix, khkey_t, __hash_fn, __hash_eq) \
- __KHASHL_TYPE(HType, khkey_t) \
- __KHASHL_IMPL_BASIC(SCOPE, HType, prefix) \
- __KHASHL_IMPL_GET(SCOPE, HType, prefix, khkey_t, __hash_fn, __hash_eq) \
- __KHASHL_IMPL_RESIZE(SCOPE, HType, prefix, khkey_t, __hash_fn, __hash_eq) \
- __KHASHL_IMPL_PUT(SCOPE, HType, prefix, khkey_t, __hash_fn, __hash_eq) \
- __KHASHL_IMPL_DEL(SCOPE, HType, prefix, khkey_t, __hash_fn)
-
-/*****************************
- * More convenient interface *
- *****************************/
-
-#define __kh_packed __attribute__ ((__packed__))
-#define __kh_cached_hash(x) ((x).hash)
-
-#define KHASHL_SET_INIT(SCOPE, HType, prefix, khkey_t, __hash_fn, __hash_eq) \
- typedef struct { khkey_t key; } __kh_packed HType##_s_bucket_t; \
- static kh_inline khint_t prefix##_s_hash(HType##_s_bucket_t x) { return __hash_fn(x.key); } \
- static kh_inline int prefix##_s_eq(HType##_s_bucket_t x, HType##_s_bucket_t y) { return __hash_eq(x.key, y.key); } \
- KHASHL_INIT(KH_LOCAL, HType, prefix##_s, HType##_s_bucket_t, prefix##_s_hash, prefix##_s_eq) \
- SCOPE HType *prefix##_init(void) { return prefix##_s_init(); } \
- SCOPE void prefix##_destroy(HType *h) { prefix##_s_destroy(h); } \
- SCOPE khint_t prefix##_get(const HType *h, khkey_t key) { HType##_s_bucket_t t; t.key = key; return prefix##_s_getp(h, &t); } \
- SCOPE int prefix##_del(HType *h, khint_t k) { return prefix##_s_del(h, k); } \
- SCOPE khint_t prefix##_put(HType *h, khkey_t key, int *absent) { HType##_s_bucket_t t; t.key = key; return prefix##_s_putp(h, &t, absent); }
-
-#define KHASHL_MAP_INIT(SCOPE, HType, prefix, khkey_t, kh_val_t, __hash_fn, __hash_eq) \
- typedef struct { khkey_t key; kh_val_t val; } __kh_packed HType##_m_bucket_t; \
- static kh_inline khint_t prefix##_m_hash(HType##_m_bucket_t x) { return __hash_fn(x.key); } \
- static kh_inline int prefix##_m_eq(HType##_m_bucket_t x, HType##_m_bucket_t y) { return __hash_eq(x.key, y.key); } \
- KHASHL_INIT(KH_LOCAL, HType, prefix##_m, HType##_m_bucket_t, prefix##_m_hash, prefix##_m_eq) \
- SCOPE HType *prefix##_init(void) { return prefix##_m_init(); } \
- SCOPE void prefix##_destroy(HType *h) { prefix##_m_destroy(h); } \
- SCOPE khint_t prefix##_get(const HType *h, khkey_t key) { HType##_m_bucket_t t; t.key = key; return prefix##_m_getp(h, &t); } \
- SCOPE int prefix##_del(HType *h, khint_t k) { return prefix##_m_del(h, k); } \
- SCOPE khint_t prefix##_put(HType *h, khkey_t key, int *absent) { HType##_m_bucket_t t; t.key = key; return prefix##_m_putp(h, &t, absent); }
-
-#define KHASHL_CSET_INIT(SCOPE, HType, prefix, khkey_t, __hash_fn, __hash_eq) \
- typedef struct { khkey_t key; khint_t hash; } __kh_packed HType##_cs_bucket_t; \
- static kh_inline int prefix##_cs_eq(HType##_cs_bucket_t x, HType##_cs_bucket_t y) { return x.hash == y.hash && __hash_eq(x.key, y.key); } \
- KHASHL_INIT(KH_LOCAL, HType, prefix##_cs, HType##_cs_bucket_t, __kh_cached_hash, prefix##_cs_eq) \
- SCOPE HType *prefix##_init(void) { return prefix##_cs_init(); } \
- SCOPE void prefix##_destroy(HType *h) { prefix##_cs_destroy(h); } \
- SCOPE khint_t prefix##_get(const HType *h, khkey_t key) { HType##_cs_bucket_t t; t.key = key; t.hash = __hash_fn(key); return prefix##_cs_getp(h, &t); } \
- SCOPE int prefix##_del(HType *h, khint_t k) { return prefix##_cs_del(h, k); } \
- SCOPE khint_t prefix##_put(HType *h, khkey_t key, int *absent) { HType##_cs_bucket_t t; t.key = key, t.hash = __hash_fn(key); return prefix##_cs_putp(h, &t, absent); }
-
-#define KHASHL_CMAP_INIT(SCOPE, HType, prefix, khkey_t, kh_val_t, __hash_fn, __hash_eq) \
- typedef struct { khkey_t key; kh_val_t val; khint_t hash; } __kh_packed HType##_cm_bucket_t; \
- static kh_inline int prefix##_cm_eq(HType##_cm_bucket_t x, HType##_cm_bucket_t y) { return x.hash == y.hash && __hash_eq(x.key, y.key); } \
- KHASHL_INIT(KH_LOCAL, HType, prefix##_cm, HType##_cm_bucket_t, __kh_cached_hash, prefix##_cm_eq) \
- SCOPE HType *prefix##_init(void) { return prefix##_cm_init(); } \
- SCOPE void prefix##_destroy(HType *h) { prefix##_cm_destroy(h); } \
- SCOPE khint_t prefix##_get(const HType *h, khkey_t key) { HType##_cm_bucket_t t; t.key = key; t.hash = __hash_fn(key); return prefix##_cm_getp(h, &t); } \
- SCOPE int prefix##_del(HType *h, khint_t k) { return prefix##_cm_del(h, k); } \
- SCOPE khint_t prefix##_put(HType *h, khkey_t key, int *absent) { HType##_cm_bucket_t t; t.key = key, t.hash = __hash_fn(key); return prefix##_cm_putp(h, &t, absent); }
-
-/**************************
- * Public macro functions *
- **************************/
-
-#define kh_bucket(h, x) ((h)->keys[x])
-#define kh_size(h) ((h)->count)
-#define kh_capacity(h) ((h)->keys? 1U<<(h)->bits : 0U)
-#define kh_end(h) kh_capacity(h)
-
-#define kh_key(h, x) ((h)->keys[x].key)
-#define kh_val(h, x) ((h)->keys[x].val)
-
-/**************************************
- * Common hash and equality functions *
- **************************************/
-
-#define kh_eq_generic(a, b) ((a) == (b))
-#define kh_eq_str(a, b) (strcmp((a), (b)) == 0)
-#define kh_hash_dummy(x) ((khint_t)(x))
-
-static kh_inline khint_t kh_hash_uint32(khint_t key) {
- key += ~(key << 15);
- key ^= (key >> 10);
- key += (key << 3);
- key ^= (key >> 6);
- key += ~(key << 11);
- key ^= (key >> 16);
- return key;
-}
-
-static kh_inline khint_t kh_hash_uint64(khint64_t key) {
- key = ~key + (key << 21);
- key = key ^ key >> 24;
- key = (key + (key << 3)) + (key << 8);
- key = key ^ key >> 14;
- key = (key + (key << 2)) + (key << 4);
- key = key ^ key >> 28;
- key = key + (key << 31);
- return (khint_t)key;
-}
-
-static kh_inline khint_t kh_hash_str(const char *s) {
- khint_t h = (khint_t)*s;
- if (h) for (++s ; *s; ++s) h = (h << 5) - h + (khint_t)*s;
- return h;
-}
-
-#endif /* __AC_KHASHL_H */
diff --git a/benchmarks/others/bytell_hash_map.hpp b/benchmarks/others/skarupke/bytell_hash_map.hpp
index 2e348cdb..9c9a2467 100644
--- a/benchmarks/others/bytell_hash_map.hpp
+++ b/benchmarks/others/skarupke/bytell_hash_map.hpp
@@ -1,1260 +1,1260 @@
-// Copyright Malte Skarupke 2017.
-// Distributed under the Boost Software License, Version 1.0.
-// (See http://www.boost.org/LICENSE_1_0.txt)
-
-#pragma once
-
-#include <cstdint>
-#include <cstddef>
-#include <cmath>
-#include <algorithm>
-#include <iterator>
-#include <utility>
-#include <type_traits>
-#include "flat_hash_map.hpp"
-#include <vector>
-#include <array>
-
-namespace ska
-{
-
-namespace detailv8
-{
-using ska::detailv3::functor_storage;
-using ska::detailv3::KeyOrValueHasher;
-using ska::detailv3::KeyOrValueEquality;
-using ska::detailv3::AssignIfTrue;
-using ska::detailv3::HashPolicySelector;
-
-template<typename = void>
-struct sherwood_v8_constants
-{
- static constexpr int8_t magic_for_empty = int8_t(0b11111111);
- static constexpr int8_t magic_for_reserved = int8_t(0b11111110);
- static constexpr int8_t bits_for_direct_hit = int8_t(0b10000000);
- static constexpr int8_t magic_for_direct_hit = int8_t(0b00000000);
- static constexpr int8_t magic_for_list_entry = int8_t(0b10000000);
-
- static constexpr int8_t bits_for_distance = int8_t(0b01111111);
- inline static int distance_from_metadata(int8_t metadata)
- {
- return metadata & bits_for_distance;
- }
-
- static constexpr int num_jump_distances = 126;
- // jump distances chosen like this:
- // 1. pick the first 16 integers to promote staying in the same block
- // 2. add the next 66 triangular numbers to get even jumps when
- // the hash table is a power of two
- // 3. add 44 more triangular numbers at a much steeper growth rate
- // to get a sequence that allows large jumps so that a table
- // with 10000 sequential numbers doesn't endlessly re-allocate
- static constexpr size_t jump_distances[num_jump_distances]
- {
- 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
-
- 21, 28, 36, 45, 55, 66, 78, 91, 105, 120, 136, 153, 171, 190, 210, 231,
- 253, 276, 300, 325, 351, 378, 406, 435, 465, 496, 528, 561, 595, 630,
- 666, 703, 741, 780, 820, 861, 903, 946, 990, 1035, 1081, 1128, 1176,
- 1225, 1275, 1326, 1378, 1431, 1485, 1540, 1596, 1653, 1711, 1770, 1830,
- 1891, 1953, 2016, 2080, 2145, 2211, 2278, 2346, 2415, 2485, 2556,
-
- 3741, 8385, 18915, 42486, 95703, 215496, 485605, 1091503, 2456436,
- 5529475, 12437578, 27986421, 62972253, 141700195, 318819126, 717314626,
- 1614000520, 3631437253, 8170829695, 18384318876, 41364501751,
- 93070021080, 209407709220, 471167588430, 1060127437995, 2385287281530,
- 5366895564381, 12075513791265, 27169907873235, 61132301007778,
- 137547673121001, 309482258302503, 696335090510256, 1566753939653640,
- 3525196427195653, 7931691866727775, 17846306747368716,
- 40154190394120111, 90346928493040500, 203280588949935750,
- 457381324898247375, 1029107980662394500, 2315492957028380766,
- 5209859150892887590,
- };
-};
-template<typename T>
-constexpr int8_t sherwood_v8_constants<T>::magic_for_empty;
-template<typename T>
-constexpr int8_t sherwood_v8_constants<T>::magic_for_reserved;
-template<typename T>
-constexpr int8_t sherwood_v8_constants<T>::bits_for_direct_hit;
-template<typename T>
-constexpr int8_t sherwood_v8_constants<T>::magic_for_direct_hit;
-template<typename T>
-constexpr int8_t sherwood_v8_constants<T>::magic_for_list_entry;
-
-template<typename T>
-constexpr int8_t sherwood_v8_constants<T>::bits_for_distance;
-
-template<typename T>
-constexpr int sherwood_v8_constants<T>::num_jump_distances;
-template<typename T>
-constexpr size_t sherwood_v8_constants<T>::jump_distances[num_jump_distances];
-
-template<typename T, uint8_t BlockSize>
-struct sherwood_v8_block
-{
- sherwood_v8_block()
- {
- }
- ~sherwood_v8_block()
- {
- }
- int8_t control_bytes[BlockSize];
- union
- {
- T data[BlockSize];
- };
-
- static sherwood_v8_block * empty_block()
- {
- static std::array<int8_t, BlockSize> empty_bytes = []
- {
- std::array<int8_t, BlockSize> result;
- result.fill(sherwood_v8_constants<>::magic_for_empty);
- return result;
- }();
- return reinterpret_cast<sherwood_v8_block *>(&empty_bytes);
- }
-
- int first_empty_index() const
- {
- for (int i = 0; i < BlockSize; ++i)
- {
- if (control_bytes[i] == sherwood_v8_constants<>::magic_for_empty)
- return i;
- }
- return -1;
- }
-
- void fill_control_bytes(int8_t value)
- {
- std::fill(std::begin(control_bytes), std::end(control_bytes), value);
- }
-};
-
-template<typename T, typename FindKey, typename ArgumentHash, typename Hasher, typename ArgumentEqual, typename Equal, typename ArgumentAlloc, typename ByteAlloc, uint8_t BlockSize>
-class sherwood_v8_table : private ByteAlloc, private Hasher, private Equal
-{
- using AllocatorTraits = std::allocator_traits<ByteAlloc>;
- using BlockType = sherwood_v8_block<T, BlockSize>;
- using BlockPointer = BlockType *;
- using BytePointer = typename AllocatorTraits::pointer;
- struct convertible_to_iterator;
- using Constants = sherwood_v8_constants<>;
-
-public:
-
- using value_type = T;
- using size_type = size_t;
- using difference_type = std::ptrdiff_t;
- using hasher = ArgumentHash;
- using key_equal = ArgumentEqual;
- using allocator_type = ByteAlloc;
- using reference = value_type &;
- using const_reference = const value_type &;
- using pointer = value_type *;
- using const_pointer = const value_type *;
-
- sherwood_v8_table()
- {
- }
- explicit sherwood_v8_table(size_type bucket_count, const ArgumentHash & hash = ArgumentHash(), const ArgumentEqual & equal = ArgumentEqual(), const ArgumentAlloc & alloc = ArgumentAlloc())
- : ByteAlloc(alloc), Hasher(hash), Equal(equal)
- {
- if (bucket_count)
- rehash(bucket_count);
- }
- sherwood_v8_table(size_type bucket_count, const ArgumentAlloc & alloc)
- : sherwood_v8_table(bucket_count, ArgumentHash(), ArgumentEqual(), alloc)
- {
- }
- sherwood_v8_table(size_type bucket_count, const ArgumentHash & hash, const ArgumentAlloc & alloc)
- : sherwood_v8_table(bucket_count, hash, ArgumentEqual(), alloc)
- {
- }
- explicit sherwood_v8_table(const ArgumentAlloc & alloc)
- : ByteAlloc(alloc)
- {
- }
- template<typename It>
- sherwood_v8_table(It first, It last, size_type bucket_count = 0, const ArgumentHash & hash = ArgumentHash(), const ArgumentEqual & equal = ArgumentEqual(), const ArgumentAlloc & alloc = ArgumentAlloc())
- : sherwood_v8_table(bucket_count, hash, equal, alloc)
- {
- insert(first, last);
- }
- template<typename It>
- sherwood_v8_table(It first, It last, size_type bucket_count, const ArgumentAlloc & alloc)
- : sherwood_v8_table(first, last, bucket_count, ArgumentHash(), ArgumentEqual(), alloc)
- {
- }
- template<typename It>
- sherwood_v8_table(It first, It last, size_type bucket_count, const ArgumentHash & hash, const ArgumentAlloc & alloc)
- : sherwood_v8_table(first, last, bucket_count, hash, ArgumentEqual(), alloc)
- {
- }
- sherwood_v8_table(std::initializer_list<T> il, size_type bucket_count = 0, const ArgumentHash & hash = ArgumentHash(), const ArgumentEqual & equal = ArgumentEqual(), const ArgumentAlloc & alloc = ArgumentAlloc())
- : sherwood_v8_table(bucket_count, hash, equal, alloc)
- {
- if (bucket_count == 0)
- rehash(il.size());
- insert(il.begin(), il.end());
- }
- sherwood_v8_table(std::initializer_list<T> il, size_type bucket_count, const ArgumentAlloc & alloc)
- : sherwood_v8_table(il, bucket_count, ArgumentHash(), ArgumentEqual(), alloc)
- {
- }
- sherwood_v8_table(std::initializer_list<T> il, size_type bucket_count, const ArgumentHash & hash, const ArgumentAlloc & alloc)
- : sherwood_v8_table(il, bucket_count, hash, ArgumentEqual(), alloc)
- {
- }
- sherwood_v8_table(const sherwood_v8_table & other)
- : sherwood_v8_table(other, AllocatorTraits::select_on_container_copy_construction(other.get_allocator()))
- {
- }
- sherwood_v8_table(const sherwood_v8_table & other, const ArgumentAlloc & alloc)
- : ByteAlloc(alloc), Hasher(other), Equal(other), _max_load_factor(other._max_load_factor)
- {
- rehash_for_other_container(other);
- try
- {
- insert(other.begin(), other.end());
- }
- catch(...)
- {
- clear();
- deallocate_data(entries, num_slots_minus_one);
- throw;
- }
- }
- sherwood_v8_table(sherwood_v8_table && other) noexcept
- : ByteAlloc(std::move(other)), Hasher(std::move(other)), Equal(std::move(other))
- , _max_load_factor(other._max_load_factor)
- {
- swap_pointers(other);
- }
- sherwood_v8_table(sherwood_v8_table && other, const ArgumentAlloc & alloc) noexcept
- : ByteAlloc(alloc), Hasher(std::move(other)), Equal(std::move(other))
- , _max_load_factor(other._max_load_factor)
- {
- swap_pointers(other);
- }
- sherwood_v8_table & operator=(const sherwood_v8_table & other)
- {
- if (this == std::addressof(other))
- return *this;
-
- clear();
- if (AllocatorTraits::propagate_on_container_copy_assignment::value)
- {
- if (static_cast<ByteAlloc &>(*this) != static_cast<const ByteAlloc &>(other))
- {
- reset_to_empty_state();
- }
- AssignIfTrue<ByteAlloc, AllocatorTraits::propagate_on_container_copy_assignment::value>()(*this, other);
- }
- _max_load_factor = other._max_load_factor;
- static_cast<Hasher &>(*this) = other;
- static_cast<Equal &>(*this) = other;
- rehash_for_other_container(other);
- insert(other.begin(), other.end());
- return *this;
- }
- sherwood_v8_table & operator=(sherwood_v8_table && other) noexcept
- {
- if (this == std::addressof(other))
- return *this;
- else if (AllocatorTraits::propagate_on_container_move_assignment::value)
- {
- clear();
- reset_to_empty_state();
- AssignIfTrue<ByteAlloc, AllocatorTraits::propagate_on_container_move_assignment::value>()(*this, std::move(other));
- swap_pointers(other);
- }
- else if (static_cast<ByteAlloc &>(*this) == static_cast<ByteAlloc &>(other))
- {
- swap_pointers(other);
- }
- else
- {
- clear();
- _max_load_factor = other._max_load_factor;
- rehash_for_other_container(other);
- for (T & elem : other)
- emplace(std::move(elem));
- other.clear();
- }
- static_cast<Hasher &>(*this) = std::move(other);
- static_cast<Equal &>(*this) = std::move(other);
- return *this;
- }
- ~sherwood_v8_table()
- {
- clear();
- deallocate_data(entries, num_slots_minus_one);
- }
-
- const allocator_type & get_allocator() const
- {
- return static_cast<const allocator_type &>(*this);
- }
- const ArgumentEqual & key_eq() const
- {
- return static_cast<const ArgumentEqual &>(*this);
- }
- const ArgumentHash & hash_function() const
- {
- return static_cast<const ArgumentHash &>(*this);
- }
-
- template<typename ValueType>
- struct templated_iterator
- {
- private:
- friend class sherwood_v8_table;
- BlockPointer current = BlockPointer();
- size_t index = 0;
-
- public:
- templated_iterator()
- {
- }
- templated_iterator(BlockPointer entries, size_t index)
- : current(entries)
- , index(index)
- {
- }
-
- using iterator_category = std::forward_iterator_tag;
- using value_type = ValueType;
- using difference_type = ptrdiff_t;
- using pointer = ValueType *;
- using reference = ValueType &;
-
- friend bool operator==(const templated_iterator & lhs, const templated_iterator & rhs)
- {
- return lhs.index == rhs.index;
- }
- friend bool operator!=(const templated_iterator & lhs, const templated_iterator & rhs)
- {
- return !(lhs == rhs);
- }
-
- templated_iterator & operator++()
- {
- do
- {
- if (index % BlockSize == 0)
- --current;
- if (index-- == 0)
- break;
- }
- while(current->control_bytes[index % BlockSize] == Constants::magic_for_empty);
- return *this;
- }
- templated_iterator operator++(int)
- {
- templated_iterator copy(*this);
- ++*this;
- return copy;
- }
-
- ValueType & operator*() const
- {
- return current->data[index % BlockSize];
- }
- ValueType * operator->() const
- {
- return current->data + index % BlockSize;
- }
-
- operator templated_iterator<const value_type>() const
- {
- return { current, index };
- }
- };
- using iterator = templated_iterator<value_type>;
- using const_iterator = templated_iterator<const value_type>;
-
- iterator begin()
- {
- size_t num_slots = num_slots_minus_one ? num_slots_minus_one + 1 : 0;
- return ++iterator{ entries + num_slots / BlockSize, num_slots };
- }
- const_iterator begin() const
- {
- size_t num_slots = num_slots_minus_one ? num_slots_minus_one + 1 : 0;
- return ++iterator{ entries + num_slots / BlockSize, num_slots };
- }
- const_iterator cbegin() const
- {
- return begin();
- }
- iterator end()
- {
- return { entries - 1, std::numeric_limits<size_t>::max() };
- }
- const_iterator end() const
- {
- return { entries - 1, std::numeric_limits<size_t>::max() };
- }
- const_iterator cend() const
- {
- return end();
- }
-
- inline iterator find(const FindKey & key)
- {
- size_t index = hash_object(key);
- size_t num_slots_minus_one = this->num_slots_minus_one;
- BlockPointer entries = this->entries;
- index = hash_policy.index_for_hash(index, num_slots_minus_one);
- bool first = true;
- for (;;)
- {
- size_t block_index = index / BlockSize;
- int index_in_block = index % BlockSize;
- BlockPointer block = entries + block_index;
- int8_t metadata = block->control_bytes[index_in_block];
- if (first)
- {
- if ((metadata & Constants::bits_for_direct_hit) != Constants::magic_for_direct_hit)
- return end();
- first = false;
- }
- if (compares_equal(key, block->data[index_in_block]))
- return { block, index };
- int8_t to_next_index = metadata & Constants::bits_for_distance;
- if (to_next_index == 0)
- return end();
- index += Constants::jump_distances[to_next_index];
- index = hash_policy.keep_in_range(index, num_slots_minus_one);
- }
- }
- inline const_iterator find(const FindKey & key) const
- {
- return const_cast<sherwood_v8_table *>(this)->find(key);
- }
- size_t count(const FindKey & key) const
- {
- return find(key) == end() ? 0 : 1;
- }
- std::pair<iterator, iterator> equal_range(const FindKey & key)
- {
- iterator found = find(key);
- if (found == end())
- return { found, found };
- else
- return { found, std::next(found) };
- }
- std::pair<const_iterator, const_iterator> equal_range(const FindKey & key) const
- {
- const_iterator found = find(key);
- if (found == end())
- return { found, found };
- else
- return { found, std::next(found) };
- }
-
-
- template<typename Key, typename... Args>
- inline std::pair<iterator, bool> emplace(Key && key, Args &&... args)
- {
- size_t index = hash_object(key);
- size_t num_slots_minus_one = this->num_slots_minus_one;
- BlockPointer entries = this->entries;
- index = hash_policy.index_for_hash(index, num_slots_minus_one);
- bool first = true;
- for (;;)
- {
- size_t block_index = index / BlockSize;
- int index_in_block = index % BlockSize;
- BlockPointer block = entries + block_index;
- int8_t metadata = block->control_bytes[index_in_block];
- if (first)
- {
- if ((metadata & Constants::bits_for_direct_hit) != Constants::magic_for_direct_hit)
- return emplace_direct_hit({ index, block }, std::forward<Key>(key), std::forward<Args>(args)...);
- first = false;
- }
- if (compares_equal(key, block->data[index_in_block]))
- return { { block, index }, false };
- int8_t to_next_index = metadata & Constants::bits_for_distance;
- if (to_next_index == 0)
- return emplace_new_key({ index, block }, std::forward<Key>(key), std::forward<Args>(args)...);
- index += Constants::jump_distances[to_next_index];
- index = hash_policy.keep_in_range(index, num_slots_minus_one);
- }
- }
-
- std::pair<iterator, bool> insert(const value_type & value)
- {
- return emplace(value);
- }
- std::pair<iterator, bool> insert(value_type && value)
- {
- return emplace(std::move(value));
- }
- template<typename... Args>
- iterator emplace_hint(const_iterator, Args &&... args)
- {
- return emplace(std::forward<Args>(args)...).first;
- }
- iterator insert(const_iterator, const value_type & value)
- {
- return emplace(value).first;
- }
- iterator insert(const_iterator, value_type && value)
- {
- return emplace(std::move(value)).first;
- }
-
- template<typename It>
- void insert(It begin, It end)
- {
- for (; begin != end; ++begin)
- {
- emplace(*begin);
- }
- }
- void insert(std::initializer_list<value_type> il)
- {
- insert(il.begin(), il.end());
- }
-
- void rehash(size_t num_items)
- {
- num_items = std::max(num_items, static_cast<size_t>(std::ceil(num_elements / static_cast<double>(_max_load_factor))));
- if (num_items == 0)
- {
- reset_to_empty_state();
- return;
- }
- auto new_prime_index = hash_policy.next_size_over(num_items);
- if (num_items == num_slots_minus_one + 1)
- return;
- size_t num_blocks = num_items / BlockSize;
- if (num_items % BlockSize)
- ++num_blocks;
- size_t memory_requirement = calculate_memory_requirement(num_blocks);
- unsigned char * new_memory = &*AllocatorTraits::allocate(*this, memory_requirement);
-
- BlockPointer new_buckets = reinterpret_cast<BlockPointer>(new_memory);
-
- BlockPointer special_end_item = new_buckets + num_blocks;
- for (BlockPointer it = new_buckets; it <= special_end_item; ++it)
- it->fill_control_bytes(Constants::magic_for_empty);
- using std::swap;
- swap(entries, new_buckets);
- swap(num_slots_minus_one, num_items);
- --num_slots_minus_one;
- hash_policy.commit(new_prime_index);
- num_elements = 0;
- if (num_items)
- ++num_items;
- size_t old_num_blocks = num_items / BlockSize;
- if (num_items % BlockSize)
- ++old_num_blocks;
- for (BlockPointer it = new_buckets, end = new_buckets + old_num_blocks; it != end; ++it)
- {
- for (int i = 0; i < BlockSize; ++i)
- {
- int8_t metadata = it->control_bytes[i];
- if (metadata != Constants::magic_for_empty && metadata != Constants::magic_for_reserved)
- {
- emplace(std::move(it->data[i]));
- AllocatorTraits::destroy(*this, it->data + i);
- }
- }
- }
- deallocate_data(new_buckets, num_items - 1);
- }
-
- void reserve(size_t num_elements)
- {
- size_t required_buckets = num_buckets_for_reserve(num_elements);
- if (required_buckets > bucket_count())
- rehash(required_buckets);
- }
-
- // the return value is a type that can be converted to an iterator
- // the reason for doing this is that it's not free to find the
- // iterator pointing at the next element. if you care about the
- // next iterator, turn the return value into an iterator
- convertible_to_iterator erase(const_iterator to_erase)
- {
- LinkedListIt current = { to_erase.index, to_erase.current };
- if (current.has_next())
- {
- LinkedListIt previous = current;
- LinkedListIt next = current.next(*this);
- while (next.has_next())
- {
- previous = next;
- next = next.next(*this);
- }
- AllocatorTraits::destroy(*this, std::addressof(*current));
- AllocatorTraits::construct(*this, std::addressof(*current), std::move(*next));
- AllocatorTraits::destroy(*this, std::addressof(*next));
- next.set_metadata(Constants::magic_for_empty);
- previous.clear_next();
- }
- else
- {
- if (!current.is_direct_hit())
- find_parent_block(current).clear_next();
- AllocatorTraits::destroy(*this, std::addressof(*current));
- current.set_metadata(Constants::magic_for_empty);
- }
- --num_elements;
- return { to_erase.current, to_erase.index };
- }
-
- iterator erase(const_iterator begin_it, const_iterator end_it)
- {
- if (begin_it == end_it)
- return { begin_it.current, begin_it.index };
- if (std::next(begin_it) == end_it)
- return erase(begin_it);
- if (begin_it == begin() && end_it == end())
- {
- clear();
- return { end_it.current, end_it.index };
- }
- std::vector<std::pair<int, LinkedListIt>> depth_in_chain;
- for (const_iterator it = begin_it; it != end_it; ++it)
- {
- LinkedListIt list_it(it.index, it.current);
- if (list_it.is_direct_hit())
- depth_in_chain.emplace_back(0, list_it);
- else
- {
- LinkedListIt root = find_direct_hit(list_it);
- int distance = 1;
- for (;;)
- {
- LinkedListIt next = root.next(*this);
- if (next == list_it)
- break;
- ++distance;
- root = next;
- }
- depth_in_chain.emplace_back(distance, list_it);
- }
- }
- std::sort(depth_in_chain.begin(), depth_in_chain.end(), [](const auto & a, const auto & b) { return a.first < b.first; });
- for (auto it = depth_in_chain.rbegin(), end = depth_in_chain.rend(); it != end; ++it)
- {
- erase(it->second.it());
- }
-
- if (begin_it.current->control_bytes[begin_it.index % BlockSize] == Constants::magic_for_empty)
- return ++iterator{ begin_it.current, begin_it.index };
- else
- return { begin_it.current, begin_it.index };
- }
-
- size_t erase(const FindKey & key)
- {
- auto found = find(key);
- if (found == end())
- return 0;
- else
- {
- erase(found);
- return 1;
- }
- }
-
- void clear()
- {
- if (!num_slots_minus_one)
- return;
- size_t num_slots = num_slots_minus_one + 1;
- size_t num_blocks = num_slots / BlockSize;
- if (num_slots % BlockSize)
- ++num_blocks;
- for (BlockPointer it = entries, end = it + num_blocks; it != end; ++it)
- {
- for (int i = 0; i < BlockSize; ++i)
- {
- if (it->control_bytes[i] != Constants::magic_for_empty)
- {
- AllocatorTraits::destroy(*this, std::addressof(it->data[i]));
- it->control_bytes[i] = Constants::magic_for_empty;
- }
- }
- }
- num_elements = 0;
- }
-
- void shrink_to_fit()
- {
- rehash_for_other_container(*this);
- }
-
- void swap(sherwood_v8_table & other)
- {
- using std::swap;
- swap_pointers(other);
- swap(static_cast<ArgumentHash &>(*this), static_cast<ArgumentHash &>(other));
- swap(static_cast<ArgumentEqual &>(*this), static_cast<ArgumentEqual &>(other));
- if (AllocatorTraits::propagate_on_container_swap::value)
- swap(static_cast<ByteAlloc &>(*this), static_cast<ByteAlloc &>(other));
- }
-
- size_t size() const
- {
- return num_elements;
- }
- size_t max_size() const
- {
- return (AllocatorTraits::max_size(*this)) / sizeof(T);
- }
- size_t bucket_count() const
- {
- return num_slots_minus_one ? num_slots_minus_one + 1 : 0;
- }
- size_type max_bucket_count() const
- {
- return (AllocatorTraits::max_size(*this)) / sizeof(T);
- }
- size_t bucket(const FindKey & key) const
- {
- return hash_policy.index_for_hash(hash_object(key), num_slots_minus_one);
- }
- float load_factor() const
- {
- return static_cast<double>(num_elements) / (num_slots_minus_one + 1);
- }
- void max_load_factor(float value)
- {
- _max_load_factor = value;
- }
- float max_load_factor() const
- {
- return _max_load_factor;
- }
-
- bool empty() const
- {
- return num_elements == 0;
- }
-
-private:
- BlockPointer entries = BlockType::empty_block();
- size_t num_slots_minus_one = 0;
- typename HashPolicySelector<ArgumentHash>::type hash_policy;
- float _max_load_factor = 0.9375f;
- size_t num_elements = 0;
-
- size_t num_buckets_for_reserve(size_t num_elements) const
- {
- return static_cast<size_t>(std::ceil(num_elements / static_cast<double>(_max_load_factor)));
- }
- void rehash_for_other_container(const sherwood_v8_table & other)
- {
- rehash(std::min(num_buckets_for_reserve(other.size()), other.bucket_count()));
- }
- bool is_full() const
- {
- if (!num_slots_minus_one)
- return true;
- else
- return num_elements + 1 > (num_slots_minus_one + 1) * static_cast<double>(_max_load_factor);
- }
-
- void swap_pointers(sherwood_v8_table & other)
- {
- using std::swap;
- swap(hash_policy, other.hash_policy);
- swap(entries, other.entries);
- swap(num_slots_minus_one, other.num_slots_minus_one);
- swap(num_elements, other.num_elements);
- swap(_max_load_factor, other._max_load_factor);
- }
-
- struct LinkedListIt
- {
- size_t index = 0;
- BlockPointer block = nullptr;
-
- LinkedListIt()
- {
- }
- LinkedListIt(size_t index, BlockPointer block)
- : index(index), block(block)
- {
- }
-
- iterator it() const
- {
- return { block, index };
- }
- int index_in_block() const
- {
- return index % BlockSize;
- }
- bool is_direct_hit() const
- {
- return (metadata() & Constants::bits_for_direct_hit) == Constants::magic_for_direct_hit;
- }
- bool is_empty() const
- {
- return metadata() == Constants::magic_for_empty;
- }
- bool has_next() const
- {
- return jump_index() != 0;
- }
- int8_t jump_index() const
- {
- return Constants::distance_from_metadata(metadata());
- }
- int8_t metadata() const
- {
- return block->control_bytes[index_in_block()];
- }
- void set_metadata(int8_t metadata)
- {
- block->control_bytes[index_in_block()] = metadata;
- }
-
- LinkedListIt next(sherwood_v8_table & table) const
- {
- int8_t distance = jump_index();
- size_t next_index = table.hash_policy.keep_in_range(index + Constants::jump_distances[distance], table.num_slots_minus_one);
- return { next_index, table.entries + next_index / BlockSize };
- }
- void set_next(int8_t jump_index)
- {
- int8_t & metadata = block->control_bytes[index_in_block()];
- metadata = (metadata & ~Constants::bits_for_distance) | jump_index;
- }
- void clear_next()
- {
- set_next(0);
- }
-
- value_type & operator*() const
- {
- return block->data[index_in_block()];
- }
- bool operator!() const
- {
- return !block;
- }
- explicit operator bool() const
- {
- return block != nullptr;
- }
- bool operator==(const LinkedListIt & other) const
- {
- return index == other.index;
- }
- bool operator!=(const LinkedListIt & other) const
- {
- return !(*this == other);
- }
- };
-
- template<typename... Args>
- SKA_NOINLINE(std::pair<iterator, bool>) emplace_direct_hit(LinkedListIt block, Args &&... args)
- {
- using std::swap;
- if (is_full())
- {
- grow();
- return emplace(std::forward<Args>(args)...);
- }
- if (block.metadata() == Constants::magic_for_empty)
- {
- AllocatorTraits::construct(*this, std::addressof(*block), std::forward<Args>(args)...);
- block.set_metadata(Constants::magic_for_direct_hit);
- ++num_elements;
- return { block.it(), true };
- }
- else
- {
- LinkedListIt parent_block = find_parent_block(block);
- std::pair<int8_t, LinkedListIt> free_block = find_free_index(parent_block);
- if (!free_block.first)
- {
- grow();
- return emplace(std::forward<Args>(args)...);
- }
- value_type new_value(std::forward<Args>(args)...);
- for (LinkedListIt it = block;;)
- {
- AllocatorTraits::construct(*this, std::addressof(*free_block.second), std::move(*it));
- AllocatorTraits::destroy(*this, std::addressof(*it));
- parent_block.set_next(free_block.first);
- free_block.second.set_metadata(Constants::magic_for_list_entry);
- if (!it.has_next())
- {
- it.set_metadata(Constants::magic_for_empty);
- break;
- }
- LinkedListIt next = it.next(*this);
- it.set_metadata(Constants::magic_for_empty);
- block.set_metadata(Constants::magic_for_reserved);
- it = next;
- parent_block = free_block.second;
- free_block = find_free_index(free_block.second);
- if (!free_block.first)
- {
- grow();
- return emplace(std::move(new_value));
- }
- }
- AllocatorTraits::construct(*this, std::addressof(*block), std::move(new_value));
- block.set_metadata(Constants::magic_for_direct_hit);
- ++num_elements;
- return { block.it(), true };
- }
- }
-
- template<typename... Args>
- SKA_NOINLINE(std::pair<iterator, bool>) emplace_new_key(LinkedListIt parent, Args &&... args)
- {
- if (is_full())
- {
- grow();
- return emplace(std::forward<Args>(args)...);
- }
- std::pair<int8_t, LinkedListIt> free_block = find_free_index(parent);
- if (!free_block.first)
- {
- grow();
- return emplace(std::forward<Args>(args)...);
- }
- AllocatorTraits::construct(*this, std::addressof(*free_block.second), std::forward<Args>(args)...);
- free_block.second.set_metadata(Constants::magic_for_list_entry);
- parent.set_next(free_block.first);
- ++num_elements;
- return { free_block.second.it(), true };
- }
-
- LinkedListIt find_direct_hit(LinkedListIt child) const
- {
- size_t to_move_hash = hash_object(*child);
- size_t to_move_index = hash_policy.index_for_hash(to_move_hash, num_slots_minus_one);
- return { to_move_index, entries + to_move_index / BlockSize };
- }
- LinkedListIt find_parent_block(LinkedListIt child)
- {
- LinkedListIt parent_block = find_direct_hit(child);
- for (;;)
- {
- LinkedListIt next = parent_block.next(*this);
- if (next == child)
- return parent_block;
- parent_block = next;
- }
- }
-
- std::pair<int8_t, LinkedListIt> find_free_index(LinkedListIt parent) const
- {
- for (int8_t jump_index = 1; jump_index < Constants::num_jump_distances; ++jump_index)
- {
- size_t index = hash_policy.keep_in_range(parent.index + Constants::jump_distances[jump_index], num_slots_minus_one);
- BlockPointer block = entries + index / BlockSize;
- if (block->control_bytes[index % BlockSize] == Constants::magic_for_empty)
- return { jump_index, { index, block } };
- }
- return { 0, {} };
- }
-
- void grow()
- {
- rehash(std::max(size_t(10), 2 * bucket_count()));
- }
-
- size_t calculate_memory_requirement(size_t num_blocks)
- {
- size_t memory_required = sizeof(BlockType) * num_blocks;
- memory_required += BlockSize; // for metadata of past-the-end pointer
- return memory_required;
- }
-
- void deallocate_data(BlockPointer begin, size_t num_slots_minus_one)
- {
- if (begin == BlockType::empty_block())
- return;
-
- ++num_slots_minus_one;
- size_t num_blocks = num_slots_minus_one / BlockSize;
- if (num_slots_minus_one % BlockSize)
- ++num_blocks;
- size_t memory = calculate_memory_requirement(num_blocks);
- unsigned char * as_byte_pointer = reinterpret_cast<unsigned char *>(begin);
- AllocatorTraits::deallocate(*this, typename AllocatorTraits::pointer(as_byte_pointer), memory);
- }
-
- void reset_to_empty_state()
- {
- deallocate_data(entries, num_slots_minus_one);
- entries = BlockType::empty_block();
- num_slots_minus_one = 0;
- hash_policy.reset();
- }
-
- template<typename U>
- size_t hash_object(const U & key)
- {
- return static_cast<Hasher &>(*this)(key);
- }
- template<typename U>
- size_t hash_object(const U & key) const
- {
- return static_cast<const Hasher &>(*this)(key);
- }
- template<typename L, typename R>
- bool compares_equal(const L & lhs, const R & rhs)
- {
- return static_cast<Equal &>(*this)(lhs, rhs);
- }
-
- struct convertible_to_iterator
- {
- BlockPointer it;
- size_t index;
-
- operator iterator()
- {
- if (it->control_bytes[index % BlockSize] == Constants::magic_for_empty)
- return ++iterator{it, index};
- else
- return { it, index };
- }
- operator const_iterator()
- {
- if (it->control_bytes[index % BlockSize] == Constants::magic_for_empty)
- return ++iterator{it, index};
- else
- return { it, index };
- }
- };
-};
-template<typename T, typename Enable = void>
-struct AlignmentOr8Bytes
-{
- static constexpr size_t value = 8;
-};
-template<typename T>
-struct AlignmentOr8Bytes<T, typename std::enable_if<alignof(T) >= 1>::type>
-{
- static constexpr size_t value = alignof(T);
-};
-template<typename... Args>
-struct CalculateBytellBlockSize;
-template<typename First, typename... More>
-struct CalculateBytellBlockSize<First, More...>
-{
- static constexpr size_t this_value = AlignmentOr8Bytes<First>::value;
- static constexpr size_t base_value = CalculateBytellBlockSize<More...>::value;
- static constexpr size_t value = this_value > base_value ? this_value : base_value;
-};
-template<>
-struct CalculateBytellBlockSize<>
-{
- static constexpr size_t value = 8;
-};
-}
-
-template<typename K, typename V, typename H = std::hash<K>, typename E = std::equal_to<K>, typename A = std::allocator<std::pair<K, V> > >
-class bytell_hash_map
- : public detailv8::sherwood_v8_table
- <
- std::pair<K, V>,
- K,
- H,
- detailv8::KeyOrValueHasher<K, std::pair<K, V>, H>,
- E,
- detailv8::KeyOrValueEquality<K, std::pair<K, V>, E>,
- A,
- typename std::allocator_traits<A>::template rebind_alloc<unsigned char>,
- detailv8::CalculateBytellBlockSize<K, V>::value
- >
-{
- using Table = detailv8::sherwood_v8_table
- <
- std::pair<K, V>,
- K,
- H,
- detailv8::KeyOrValueHasher<K, std::pair<K, V>, H>,
- E,
- detailv8::KeyOrValueEquality<K, std::pair<K, V>, E>,
- A,
- typename std::allocator_traits<A>::template rebind_alloc<unsigned char>,
- detailv8::CalculateBytellBlockSize<K, V>::value
- >;
-public:
-
- using key_type = K;
- using mapped_type = V;
-
- using Table::Table;
- bytell_hash_map()
- {
- }
-
- inline V & operator[](const K & key)
- {
- return emplace(key, convertible_to_value()).first->second;
- }
- inline V & operator[](K && key)
- {
- return emplace(std::move(key), convertible_to_value()).first->second;
- }
- V & at(const K & key)
- {
- auto found = this->find(key);
- if (found == this->end())
- throw std::out_of_range("Argument passed to at() was not in the map.");
- return found->second;
- }
- const V & at(const K & key) const
- {
- auto found = this->find(key);
- if (found == this->end())
- throw std::out_of_range("Argument passed to at() was not in the map.");
- return found->second;
- }
-
- using Table::emplace;
- std::pair<typename Table::iterator, bool> emplace()
- {
- return emplace(key_type(), convertible_to_value());
- }
- template<typename M>
- std::pair<typename Table::iterator, bool> insert_or_assign(const key_type & key, M && m)
- {
- auto emplace_result = emplace(key, std::forward<M>(m));
- if (!emplace_result.second)
- emplace_result.first->second = std::forward<M>(m);
- return emplace_result;
- }
- template<typename M>
- std::pair<typename Table::iterator, bool> insert_or_assign(key_type && key, M && m)
- {
- auto emplace_result = emplace(std::move(key), std::forward<M>(m));
- if (!emplace_result.second)
- emplace_result.first->second = std::forward<M>(m);
- return emplace_result;
- }
- template<typename M>
- typename Table::iterator insert_or_assign(typename Table::const_iterator, const key_type & key, M && m)
- {
- return insert_or_assign(key, std::forward<M>(m)).first;
- }
- template<typename M>
- typename Table::iterator insert_or_assign(typename Table::const_iterator, key_type && key, M && m)
- {
- return insert_or_assign(std::move(key), std::forward<M>(m)).first;
- }
-
- friend bool operator==(const bytell_hash_map & lhs, const bytell_hash_map & rhs)
- {
- if (lhs.size() != rhs.size())
- return false;
- for (const typename Table::value_type & value : lhs)
- {
- auto found = rhs.find(value.first);
- if (found == rhs.end())
- return false;
- else if (value.second != found->second)
- return false;
- }
- return true;
- }
- friend bool operator!=(const bytell_hash_map & lhs, const bytell_hash_map & rhs)
- {
- return !(lhs == rhs);
- }
-
-private:
- struct convertible_to_value
- {
- operator V() const
- {
- return V();
- }
- };
-};
-
-template<typename T, typename H = std::hash<T>, typename E = std::equal_to<T>, typename A = std::allocator<T> >
-class bytell_hash_set
- : public detailv8::sherwood_v8_table
- <
- T,
- T,
- H,
- detailv8::functor_storage<size_t, H>,
- E,
- detailv8::functor_storage<bool, E>,
- A,
- typename std::allocator_traits<A>::template rebind_alloc<unsigned char>,
- detailv8::CalculateBytellBlockSize<T>::value
- >
-{
- using Table = detailv8::sherwood_v8_table
- <
- T,
- T,
- H,
- detailv8::functor_storage<size_t, H>,
- E,
- detailv8::functor_storage<bool, E>,
- A,
- typename std::allocator_traits<A>::template rebind_alloc<unsigned char>,
- detailv8::CalculateBytellBlockSize<T>::value
- >;
-public:
-
- using key_type = T;
-
- using Table::Table;
- bytell_hash_set()
- {
- }
-
- template<typename... Args>
- std::pair<typename Table::iterator, bool> emplace(Args &&... args)
- {
- return Table::emplace(T(std::forward<Args>(args)...));
- }
- std::pair<typename Table::iterator, bool> emplace(const key_type & arg)
- {
- return Table::emplace(arg);
- }
- std::pair<typename Table::iterator, bool> emplace(key_type & arg)
- {
- return Table::emplace(arg);
- }
- std::pair<typename Table::iterator, bool> emplace(const key_type && arg)
- {
- return Table::emplace(std::move(arg));
- }
- std::pair<typename Table::iterator, bool> emplace(key_type && arg)
- {
- return Table::emplace(std::move(arg));
- }
-
- friend bool operator==(const bytell_hash_set & lhs, const bytell_hash_set & rhs)
- {
- if (lhs.size() != rhs.size())
- return false;
- for (const T & value : lhs)
- {
- if (rhs.find(value) == rhs.end())
- return false;
- }
- return true;
- }
- friend bool operator!=(const bytell_hash_set & lhs, const bytell_hash_set & rhs)
- {
- return !(lhs == rhs);
- }
-};
-
-} // end namespace ska
+// Copyright Malte Skarupke 2017.
+// Distributed under the Boost Software License, Version 1.0.
+// (See http://www.boost.org/LICENSE_1_0.txt)
+
+#pragma once
+
+#include <cstdint>
+#include <cstddef>
+#include <cmath>
+#include <algorithm>
+#include <iterator>
+#include <utility>
+#include <type_traits>
+#include "flat_hash_map.hpp"
+#include <vector>
+#include <array>
+
+namespace ska
+{
+
+namespace detailv8
+{
+using ska::detailv3::functor_storage;
+using ska::detailv3::KeyOrValueHasher;
+using ska::detailv3::KeyOrValueEquality;
+using ska::detailv3::AssignIfTrue;
+using ska::detailv3::HashPolicySelector;
+
+template<typename = void>
+struct sherwood_v8_constants
+{
+ static constexpr int8_t magic_for_empty = int8_t(0b11111111);
+ static constexpr int8_t magic_for_reserved = int8_t(0b11111110);
+ static constexpr int8_t bits_for_direct_hit = int8_t(0b10000000);
+ static constexpr int8_t magic_for_direct_hit = int8_t(0b00000000);
+ static constexpr int8_t magic_for_list_entry = int8_t(0b10000000);
+
+ static constexpr int8_t bits_for_distance = int8_t(0b01111111);
+ inline static int distance_from_metadata(int8_t metadata)
+ {
+ return metadata & bits_for_distance;
+ }
+
+ static constexpr int num_jump_distances = 126;
+ // jump distances chosen like this:
+ // 1. pick the first 16 integers to promote staying in the same block
+ // 2. add the next 66 triangular numbers to get even jumps when
+ // the hash table is a power of two
+ // 3. add 44 more triangular numbers at a much steeper growth rate
+ // to get a sequence that allows large jumps so that a table
+ // with 10000 sequential numbers doesn't endlessly re-allocate
+ static constexpr size_t jump_distances[num_jump_distances]
+ {
+ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
+
+ 21, 28, 36, 45, 55, 66, 78, 91, 105, 120, 136, 153, 171, 190, 210, 231,
+ 253, 276, 300, 325, 351, 378, 406, 435, 465, 496, 528, 561, 595, 630,
+ 666, 703, 741, 780, 820, 861, 903, 946, 990, 1035, 1081, 1128, 1176,
+ 1225, 1275, 1326, 1378, 1431, 1485, 1540, 1596, 1653, 1711, 1770, 1830,
+ 1891, 1953, 2016, 2080, 2145, 2211, 2278, 2346, 2415, 2485, 2556,
+
+ 3741, 8385, 18915, 42486, 95703, 215496, 485605, 1091503, 2456436,
+ 5529475, 12437578, 27986421, 62972253, 141700195, 318819126, 717314626,
+ 1614000520, 3631437253, 8170829695, 18384318876, 41364501751,
+ 93070021080, 209407709220, 471167588430, 1060127437995, 2385287281530,
+ 5366895564381, 12075513791265, 27169907873235, 61132301007778,
+ 137547673121001, 309482258302503, 696335090510256, 1566753939653640,
+ 3525196427195653, 7931691866727775, 17846306747368716,
+ 40154190394120111, 90346928493040500, 203280588949935750,
+ 457381324898247375, 1029107980662394500, 2315492957028380766,
+ 5209859150892887590,
+ };
+};
+template<typename T>
+constexpr int8_t sherwood_v8_constants<T>::magic_for_empty;
+template<typename T>
+constexpr int8_t sherwood_v8_constants<T>::magic_for_reserved;
+template<typename T>
+constexpr int8_t sherwood_v8_constants<T>::bits_for_direct_hit;
+template<typename T>
+constexpr int8_t sherwood_v8_constants<T>::magic_for_direct_hit;
+template<typename T>
+constexpr int8_t sherwood_v8_constants<T>::magic_for_list_entry;
+
+template<typename T>
+constexpr int8_t sherwood_v8_constants<T>::bits_for_distance;
+
+template<typename T>
+constexpr int sherwood_v8_constants<T>::num_jump_distances;
+template<typename T>
+constexpr size_t sherwood_v8_constants<T>::jump_distances[num_jump_distances];
+
+template<typename T, uint8_t BlockSize>
+struct sherwood_v8_block
+{
+ sherwood_v8_block()
+ {
+ }
+ ~sherwood_v8_block()
+ {
+ }
+ int8_t control_bytes[BlockSize];
+ union
+ {
+ T data[BlockSize];
+ };
+
+ static sherwood_v8_block * empty_block()
+ {
+ static std::array<int8_t, BlockSize> empty_bytes = []
+ {
+ std::array<int8_t, BlockSize> result;
+ result.fill(sherwood_v8_constants<>::magic_for_empty);
+ return result;
+ }();
+ return reinterpret_cast<sherwood_v8_block *>(&empty_bytes);
+ }
+
+ int first_empty_index() const
+ {
+ for (int i = 0; i < BlockSize; ++i)
+ {
+ if (control_bytes[i] == sherwood_v8_constants<>::magic_for_empty)
+ return i;
+ }
+ return -1;
+ }
+
+ void fill_control_bytes(int8_t value)
+ {
+ std::fill(std::begin(control_bytes), std::end(control_bytes), value);
+ }
+};
+
+template<typename T, typename FindKey, typename ArgumentHash, typename Hasher, typename ArgumentEqual, typename Equal, typename ArgumentAlloc, typename ByteAlloc, uint8_t BlockSize>
+class sherwood_v8_table : private ByteAlloc, private Hasher, private Equal
+{
+ using AllocatorTraits = std::allocator_traits<ByteAlloc>;
+ using BlockType = sherwood_v8_block<T, BlockSize>;
+ using BlockPointer = BlockType *;
+ using BytePointer = typename AllocatorTraits::pointer;
+ struct convertible_to_iterator;
+ using Constants = sherwood_v8_constants<>;
+
+public:
+
+ using value_type = T;
+ using size_type = size_t;
+ using difference_type = std::ptrdiff_t;
+ using hasher = ArgumentHash;
+ using key_equal = ArgumentEqual;
+ using allocator_type = ByteAlloc;
+ using reference = value_type &;
+ using const_reference = const value_type &;
+ using pointer = value_type *;
+ using const_pointer = const value_type *;
+
+ sherwood_v8_table()
+ {
+ }
+ explicit sherwood_v8_table(size_type bucket_count, const ArgumentHash & hash = ArgumentHash(), const ArgumentEqual & equal = ArgumentEqual(), const ArgumentAlloc & alloc = ArgumentAlloc())
+ : ByteAlloc(alloc), Hasher(hash), Equal(equal)
+ {
+ if (bucket_count)
+ rehash(bucket_count);
+ }
+ sherwood_v8_table(size_type bucket_count, const ArgumentAlloc & alloc)
+ : sherwood_v8_table(bucket_count, ArgumentHash(), ArgumentEqual(), alloc)
+ {
+ }
+ sherwood_v8_table(size_type bucket_count, const ArgumentHash & hash, const ArgumentAlloc & alloc)
+ : sherwood_v8_table(bucket_count, hash, ArgumentEqual(), alloc)
+ {
+ }
+ explicit sherwood_v8_table(const ArgumentAlloc & alloc)
+ : ByteAlloc(alloc)
+ {
+ }
+ template<typename It>
+ sherwood_v8_table(It first, It last, size_type bucket_count = 0, const ArgumentHash & hash = ArgumentHash(), const ArgumentEqual & equal = ArgumentEqual(), const ArgumentAlloc & alloc = ArgumentAlloc())
+ : sherwood_v8_table(bucket_count, hash, equal, alloc)
+ {
+ insert(first, last);
+ }
+ template<typename It>
+ sherwood_v8_table(It first, It last, size_type bucket_count, const ArgumentAlloc & alloc)
+ : sherwood_v8_table(first, last, bucket_count, ArgumentHash(), ArgumentEqual(), alloc)
+ {
+ }
+ template<typename It>
+ sherwood_v8_table(It first, It last, size_type bucket_count, const ArgumentHash & hash, const ArgumentAlloc & alloc)
+ : sherwood_v8_table(first, last, bucket_count, hash, ArgumentEqual(), alloc)
+ {
+ }
+ sherwood_v8_table(std::initializer_list<T> il, size_type bucket_count = 0, const ArgumentHash & hash = ArgumentHash(), const ArgumentEqual & equal = ArgumentEqual(), const ArgumentAlloc & alloc = ArgumentAlloc())
+ : sherwood_v8_table(bucket_count, hash, equal, alloc)
+ {
+ if (bucket_count == 0)
+ rehash(il.size());
+ insert(il.begin(), il.end());
+ }
+ sherwood_v8_table(std::initializer_list<T> il, size_type bucket_count, const ArgumentAlloc & alloc)
+ : sherwood_v8_table(il, bucket_count, ArgumentHash(), ArgumentEqual(), alloc)
+ {
+ }
+ sherwood_v8_table(std::initializer_list<T> il, size_type bucket_count, const ArgumentHash & hash, const ArgumentAlloc & alloc)
+ : sherwood_v8_table(il, bucket_count, hash, ArgumentEqual(), alloc)
+ {
+ }
+ sherwood_v8_table(const sherwood_v8_table & other)
+ : sherwood_v8_table(other, AllocatorTraits::select_on_container_copy_construction(other.get_allocator()))
+ {
+ }
+ sherwood_v8_table(const sherwood_v8_table & other, const ArgumentAlloc & alloc)
+ : ByteAlloc(alloc), Hasher(other), Equal(other), _max_load_factor(other._max_load_factor)
+ {
+ rehash_for_other_container(other);
+ try
+ {
+ insert(other.begin(), other.end());
+ }
+ catch(...)
+ {
+ clear();
+ deallocate_data(entries, num_slots_minus_one);
+ throw;
+ }
+ }
+ sherwood_v8_table(sherwood_v8_table && other) noexcept
+ : ByteAlloc(std::move(other)), Hasher(std::move(other)), Equal(std::move(other))
+ , _max_load_factor(other._max_load_factor)
+ {
+ swap_pointers(other);
+ }
+ sherwood_v8_table(sherwood_v8_table && other, const ArgumentAlloc & alloc) noexcept
+ : ByteAlloc(alloc), Hasher(std::move(other)), Equal(std::move(other))
+ , _max_load_factor(other._max_load_factor)
+ {
+ swap_pointers(other);
+ }
+ sherwood_v8_table & operator=(const sherwood_v8_table & other)
+ {
+ if (this == std::addressof(other))
+ return *this;
+
+ clear();
+ if (AllocatorTraits::propagate_on_container_copy_assignment::value)
+ {
+ if (static_cast<ByteAlloc &>(*this) != static_cast<const ByteAlloc &>(other))
+ {
+ reset_to_empty_state();
+ }
+ AssignIfTrue<ByteAlloc, AllocatorTraits::propagate_on_container_copy_assignment::value>()(*this, other);
+ }
+ _max_load_factor = other._max_load_factor;
+ static_cast<Hasher &>(*this) = other;
+ static_cast<Equal &>(*this) = other;
+ rehash_for_other_container(other);
+ insert(other.begin(), other.end());
+ return *this;
+ }
+ sherwood_v8_table & operator=(sherwood_v8_table && other) noexcept
+ {
+ if (this == std::addressof(other))
+ return *this;
+ else if (AllocatorTraits::propagate_on_container_move_assignment::value)
+ {
+ clear();
+ reset_to_empty_state();
+ AssignIfTrue<ByteAlloc, AllocatorTraits::propagate_on_container_move_assignment::value>()(*this, std::move(other));
+ swap_pointers(other);
+ }
+ else if (static_cast<ByteAlloc &>(*this) == static_cast<ByteAlloc &>(other))
+ {
+ swap_pointers(other);
+ }
+ else
+ {
+ clear();
+ _max_load_factor = other._max_load_factor;
+ rehash_for_other_container(other);
+ for (T & elem : other)
+ emplace(std::move(elem));
+ other.clear();
+ }
+ static_cast<Hasher &>(*this) = std::move(other);
+ static_cast<Equal &>(*this) = std::move(other);
+ return *this;
+ }
+ ~sherwood_v8_table()
+ {
+ clear();
+ deallocate_data(entries, num_slots_minus_one);
+ }
+
+ const allocator_type & get_allocator() const
+ {
+ return static_cast<const allocator_type &>(*this);
+ }
+ const ArgumentEqual & key_eq() const
+ {
+ return static_cast<const ArgumentEqual &>(*this);
+ }
+ const ArgumentHash & hash_function() const
+ {
+ return static_cast<const ArgumentHash &>(*this);
+ }
+
+ template<typename ValueType>
+ struct templated_iterator
+ {
+ private:
+ friend class sherwood_v8_table;
+ BlockPointer current = BlockPointer();
+ size_t index = 0;
+
+ public:
+ templated_iterator()
+ {
+ }
+ templated_iterator(BlockPointer entries, size_t index)
+ : current(entries)
+ , index(index)
+ {
+ }
+
+ using iterator_category = std::forward_iterator_tag;
+ using value_type = ValueType;
+ using difference_type = ptrdiff_t;
+ using pointer = ValueType *;
+ using reference = ValueType &;
+
+ friend bool operator==(const templated_iterator & lhs, const templated_iterator & rhs)
+ {
+ return lhs.index == rhs.index;
+ }
+ friend bool operator!=(const templated_iterator & lhs, const templated_iterator & rhs)
+ {
+ return !(lhs == rhs);
+ }
+
+ templated_iterator & operator++()
+ {
+ do
+ {
+ if (index % BlockSize == 0)
+ --current;
+ if (index-- == 0)
+ break;
+ }
+ while(current->control_bytes[index % BlockSize] == Constants::magic_for_empty);
+ return *this;
+ }
+ templated_iterator operator++(int)
+ {
+ templated_iterator copy(*this);
+ ++*this;
+ return copy;
+ }
+
+ ValueType & operator*() const
+ {
+ return current->data[index % BlockSize];
+ }
+ ValueType * operator->() const
+ {
+ return current->data + index % BlockSize;
+ }
+
+ operator templated_iterator<const value_type>() const
+ {
+ return { current, index };
+ }
+ };
+ using iterator = templated_iterator<value_type>;
+ using const_iterator = templated_iterator<const value_type>;
+
+ iterator begin()
+ {
+ size_t num_slots = num_slots_minus_one ? num_slots_minus_one + 1 : 0;
+ return ++iterator{ entries + num_slots / BlockSize, num_slots };
+ }
+ const_iterator begin() const
+ {
+ size_t num_slots = num_slots_minus_one ? num_slots_minus_one + 1 : 0;
+ return ++iterator{ entries + num_slots / BlockSize, num_slots };
+ }
+ const_iterator cbegin() const
+ {
+ return begin();
+ }
+ iterator end()
+ {
+ return { entries - 1, std::numeric_limits<size_t>::max() };
+ }
+ const_iterator end() const
+ {
+ return { entries - 1, std::numeric_limits<size_t>::max() };
+ }
+ const_iterator cend() const
+ {
+ return end();
+ }
+
+ inline iterator find(const FindKey & key)
+ {
+ size_t index = hash_object(key);
+ size_t num_slots_minus_one = this->num_slots_minus_one;
+ BlockPointer entries = this->entries;
+ index = hash_policy.index_for_hash(index, num_slots_minus_one);
+ bool first = true;
+ for (;;)
+ {
+ size_t block_index = index / BlockSize;
+ int index_in_block = index % BlockSize;
+ BlockPointer block = entries + block_index;
+ int8_t metadata = block->control_bytes[index_in_block];
+ if (first)
+ {
+ if ((metadata & Constants::bits_for_direct_hit) != Constants::magic_for_direct_hit)
+ return end();
+ first = false;
+ }
+ if (compares_equal(key, block->data[index_in_block]))
+ return { block, index };
+ int8_t to_next_index = metadata & Constants::bits_for_distance;
+ if (to_next_index == 0)
+ return end();
+ index += Constants::jump_distances[to_next_index];
+ index = hash_policy.keep_in_range(index, num_slots_minus_one);
+ }
+ }
+ inline const_iterator find(const FindKey & key) const
+ {
+ return const_cast<sherwood_v8_table *>(this)->find(key);
+ }
+ size_t count(const FindKey & key) const
+ {
+ return find(key) == end() ? 0 : 1;
+ }
+ std::pair<iterator, iterator> equal_range(const FindKey & key)
+ {
+ iterator found = find(key);
+ if (found == end())
+ return { found, found };
+ else
+ return { found, std::next(found) };
+ }
+ std::pair<const_iterator, const_iterator> equal_range(const FindKey & key) const
+ {
+ const_iterator found = find(key);
+ if (found == end())
+ return { found, found };
+ else
+ return { found, std::next(found) };
+ }
+
+
+ template<typename Key, typename... Args>
+ inline std::pair<iterator, bool> emplace(Key && key, Args &&... args)
+ {
+ size_t index = hash_object(key);
+ size_t num_slots_minus_one = this->num_slots_minus_one;
+ BlockPointer entries = this->entries;
+ index = hash_policy.index_for_hash(index, num_slots_minus_one);
+ bool first = true;
+ for (;;)
+ {
+ size_t block_index = index / BlockSize;
+ int index_in_block = index % BlockSize;
+ BlockPointer block = entries + block_index;
+ int8_t metadata = block->control_bytes[index_in_block];
+ if (first)
+ {
+ if ((metadata & Constants::bits_for_direct_hit) != Constants::magic_for_direct_hit)
+ return emplace_direct_hit({ index, block }, std::forward<Key>(key), std::forward<Args>(args)...);
+ first = false;
+ }
+ if (compares_equal(key, block->data[index_in_block]))
+ return { { block, index }, false };
+ int8_t to_next_index = metadata & Constants::bits_for_distance;
+ if (to_next_index == 0)
+ return emplace_new_key({ index, block }, std::forward<Key>(key), std::forward<Args>(args)...);
+ index += Constants::jump_distances[to_next_index];
+ index = hash_policy.keep_in_range(index, num_slots_minus_one);
+ }
+ }
+
+ std::pair<iterator, bool> insert(const value_type & value)
+ {
+ return emplace(value);
+ }
+ std::pair<iterator, bool> insert(value_type && value)
+ {
+ return emplace(std::move(value));
+ }
+ template<typename... Args>
+ iterator emplace_hint(const_iterator, Args &&... args)
+ {
+ return emplace(std::forward<Args>(args)...).first;
+ }
+ iterator insert(const_iterator, const value_type & value)
+ {
+ return emplace(value).first;
+ }
+ iterator insert(const_iterator, value_type && value)
+ {
+ return emplace(std::move(value)).first;
+ }
+
+ template<typename It>
+ void insert(It begin, It end)
+ {
+ for (; begin != end; ++begin)
+ {
+ emplace(*begin);
+ }
+ }
+ void insert(std::initializer_list<value_type> il)
+ {
+ insert(il.begin(), il.end());
+ }
+
+ void rehash(size_t num_items)
+ {
+ num_items = std::max(num_items, static_cast<size_t>(std::ceil(num_elements / static_cast<double>(_max_load_factor))));
+ if (num_items == 0)
+ {
+ reset_to_empty_state();
+ return;
+ }
+ auto new_prime_index = hash_policy.next_size_over(num_items);
+ if (num_items == num_slots_minus_one + 1)
+ return;
+ size_t num_blocks = num_items / BlockSize;
+ if (num_items % BlockSize)
+ ++num_blocks;
+ size_t memory_requirement = calculate_memory_requirement(num_blocks);
+ unsigned char * new_memory = &*AllocatorTraits::allocate(*this, memory_requirement);
+
+ BlockPointer new_buckets = reinterpret_cast<BlockPointer>(new_memory);
+
+ BlockPointer special_end_item = new_buckets + num_blocks;
+ for (BlockPointer it = new_buckets; it <= special_end_item; ++it)
+ it->fill_control_bytes(Constants::magic_for_empty);
+ using std::swap;
+ swap(entries, new_buckets);
+ swap(num_slots_minus_one, num_items);
+ --num_slots_minus_one;
+ hash_policy.commit(new_prime_index);
+ num_elements = 0;
+ if (num_items)
+ ++num_items;
+ size_t old_num_blocks = num_items / BlockSize;
+ if (num_items % BlockSize)
+ ++old_num_blocks;
+ for (BlockPointer it = new_buckets, end = new_buckets + old_num_blocks; it != end; ++it)
+ {
+ for (int i = 0; i < BlockSize; ++i)
+ {
+ int8_t metadata = it->control_bytes[i];
+ if (metadata != Constants::magic_for_empty && metadata != Constants::magic_for_reserved)
+ {
+ emplace(std::move(it->data[i]));
+ AllocatorTraits::destroy(*this, it->data + i);
+ }
+ }
+ }
+ deallocate_data(new_buckets, num_items - 1);
+ }
+
+ void reserve(size_t num_elements)
+ {
+ size_t required_buckets = num_buckets_for_reserve(num_elements);
+ if (required_buckets > bucket_count())
+ rehash(required_buckets);
+ }
+
+ // the return value is a type that can be converted to an iterator
+ // the reason for doing this is that it's not free to find the
+ // iterator pointing at the next element. if you care about the
+ // next iterator, turn the return value into an iterator
+ convertible_to_iterator erase(const_iterator to_erase)
+ {
+ LinkedListIt current = { to_erase.index, to_erase.current };
+ if (current.has_next())
+ {
+ LinkedListIt previous = current;
+ LinkedListIt next = current.next(*this);
+ while (next.has_next())
+ {
+ previous = next;
+ next = next.next(*this);
+ }
+ AllocatorTraits::destroy(*this, std::addressof(*current));
+ AllocatorTraits::construct(*this, std::addressof(*current), std::move(*next));
+ AllocatorTraits::destroy(*this, std::addressof(*next));
+ next.set_metadata(Constants::magic_for_empty);
+ previous.clear_next();
+ }
+ else
+ {
+ if (!current.is_direct_hit())
+ find_parent_block(current).clear_next();
+ AllocatorTraits::destroy(*this, std::addressof(*current));
+ current.set_metadata(Constants::magic_for_empty);
+ }
+ --num_elements;
+ return { to_erase.current, to_erase.index };
+ }
+
+ iterator erase(const_iterator begin_it, const_iterator end_it)
+ {
+ if (begin_it == end_it)
+ return { begin_it.current, begin_it.index };
+ if (std::next(begin_it) == end_it)
+ return erase(begin_it);
+ if (begin_it == begin() && end_it == end())
+ {
+ clear();
+ return { end_it.current, end_it.index };
+ }
+ std::vector<std::pair<int, LinkedListIt>> depth_in_chain;
+ for (const_iterator it = begin_it; it != end_it; ++it)
+ {
+ LinkedListIt list_it(it.index, it.current);
+ if (list_it.is_direct_hit())
+ depth_in_chain.emplace_back(0, list_it);
+ else
+ {
+ LinkedListIt root = find_direct_hit(list_it);
+ int distance = 1;
+ for (;;)
+ {
+ LinkedListIt next = root.next(*this);
+ if (next == list_it)
+ break;
+ ++distance;
+ root = next;
+ }
+ depth_in_chain.emplace_back(distance, list_it);
+ }
+ }
+ std::sort(depth_in_chain.begin(), depth_in_chain.end(), [](const auto & a, const auto & b) { return a.first < b.first; });
+ for (auto it = depth_in_chain.rbegin(), end = depth_in_chain.rend(); it != end; ++it)
+ {
+ erase(it->second.it());
+ }
+
+ if (begin_it.current->control_bytes[begin_it.index % BlockSize] == Constants::magic_for_empty)
+ return ++iterator{ begin_it.current, begin_it.index };
+ else
+ return { begin_it.current, begin_it.index };
+ }
+
+ size_t erase(const FindKey & key)
+ {
+ auto found = find(key);
+ if (found == end())
+ return 0;
+ else
+ {
+ erase(found);
+ return 1;
+ }
+ }
+
+ void clear()
+ {
+ if (!num_slots_minus_one)
+ return;
+ size_t num_slots = num_slots_minus_one + 1;
+ size_t num_blocks = num_slots / BlockSize;
+ if (num_slots % BlockSize)
+ ++num_blocks;
+ for (BlockPointer it = entries, end = it + num_blocks; it != end; ++it)
+ {
+ for (int i = 0; i < BlockSize; ++i)
+ {
+ if (it->control_bytes[i] != Constants::magic_for_empty)
+ {
+ AllocatorTraits::destroy(*this, std::addressof(it->data[i]));
+ it->control_bytes[i] = Constants::magic_for_empty;
+ }
+ }
+ }
+ num_elements = 0;
+ }
+
+ void shrink_to_fit()
+ {
+ rehash_for_other_container(*this);
+ }
+
+ void swap(sherwood_v8_table & other)
+ {
+ using std::swap;
+ swap_pointers(other);
+ swap(static_cast<ArgumentHash &>(*this), static_cast<ArgumentHash &>(other));
+ swap(static_cast<ArgumentEqual &>(*this), static_cast<ArgumentEqual &>(other));
+ if (AllocatorTraits::propagate_on_container_swap::value)
+ swap(static_cast<ByteAlloc &>(*this), static_cast<ByteAlloc &>(other));
+ }
+
+ size_t size() const
+ {
+ return num_elements;
+ }
+ size_t max_size() const
+ {
+ return (AllocatorTraits::max_size(*this)) / sizeof(T);
+ }
+ size_t bucket_count() const
+ {
+ return num_slots_minus_one ? num_slots_minus_one + 1 : 0;
+ }
+ size_type max_bucket_count() const
+ {
+ return (AllocatorTraits::max_size(*this)) / sizeof(T);
+ }
+ size_t bucket(const FindKey & key) const
+ {
+ return hash_policy.index_for_hash(hash_object(key), num_slots_minus_one);
+ }
+ float load_factor() const
+ {
+ return static_cast<double>(num_elements) / (num_slots_minus_one + 1);
+ }
+ void max_load_factor(float value)
+ {
+ _max_load_factor = value;
+ }
+ float max_load_factor() const
+ {
+ return _max_load_factor;
+ }
+
+ bool empty() const
+ {
+ return num_elements == 0;
+ }
+
+private:
+ BlockPointer entries = BlockType::empty_block();
+ size_t num_slots_minus_one = 0;
+ typename HashPolicySelector<ArgumentHash>::type hash_policy;
+ float _max_load_factor = 0.9375f;
+ size_t num_elements = 0;
+
+ size_t num_buckets_for_reserve(size_t num_elements) const
+ {
+ return static_cast<size_t>(std::ceil(num_elements / static_cast<double>(_max_load_factor)));
+ }
+ void rehash_for_other_container(const sherwood_v8_table & other)
+ {
+ rehash(std::min(num_buckets_for_reserve(other.size()), other.bucket_count()));
+ }
+ bool is_full() const
+ {
+ if (!num_slots_minus_one)
+ return true;
+ else
+ return num_elements + 1 > (num_slots_minus_one + 1) * static_cast<double>(_max_load_factor);
+ }
+
+ void swap_pointers(sherwood_v8_table & other)
+ {
+ using std::swap;
+ swap(hash_policy, other.hash_policy);
+ swap(entries, other.entries);
+ swap(num_slots_minus_one, other.num_slots_minus_one);
+ swap(num_elements, other.num_elements);
+ swap(_max_load_factor, other._max_load_factor);
+ }
+
+ struct LinkedListIt
+ {
+ size_t index = 0;
+ BlockPointer block = nullptr;
+
+ LinkedListIt()
+ {
+ }
+ LinkedListIt(size_t index, BlockPointer block)
+ : index(index), block(block)
+ {
+ }
+
+ iterator it() const
+ {
+ return { block, index };
+ }
+ int index_in_block() const
+ {
+ return index % BlockSize;
+ }
+ bool is_direct_hit() const
+ {
+ return (metadata() & Constants::bits_for_direct_hit) == Constants::magic_for_direct_hit;
+ }
+ bool is_empty() const
+ {
+ return metadata() == Constants::magic_for_empty;
+ }
+ bool has_next() const
+ {
+ return jump_index() != 0;
+ }
+ int8_t jump_index() const
+ {
+ return Constants::distance_from_metadata(metadata());
+ }
+ int8_t metadata() const
+ {
+ return block->control_bytes[index_in_block()];
+ }
+ void set_metadata(int8_t metadata)
+ {
+ block->control_bytes[index_in_block()] = metadata;
+ }
+
+ LinkedListIt next(sherwood_v8_table & table) const
+ {
+ int8_t distance = jump_index();
+ size_t next_index = table.hash_policy.keep_in_range(index + Constants::jump_distances[distance], table.num_slots_minus_one);
+ return { next_index, table.entries + next_index / BlockSize };
+ }
+ void set_next(int8_t jump_index)
+ {
+ int8_t & metadata = block->control_bytes[index_in_block()];
+ metadata = (metadata & ~Constants::bits_for_distance) | jump_index;
+ }
+ void clear_next()
+ {
+ set_next(0);
+ }
+
+ value_type & operator*() const
+ {
+ return block->data[index_in_block()];
+ }
+ bool operator!() const
+ {
+ return !block;
+ }
+ explicit operator bool() const
+ {
+ return block != nullptr;
+ }
+ bool operator==(const LinkedListIt & other) const
+ {
+ return index == other.index;
+ }
+ bool operator!=(const LinkedListIt & other) const
+ {
+ return !(*this == other);
+ }
+ };
+
+ template<typename... Args>
+ SKA_NOINLINE(std::pair<iterator, bool>) emplace_direct_hit(LinkedListIt block, Args &&... args)
+ {
+ using std::swap;
+ if (is_full())
+ {
+ grow();
+ return emplace(std::forward<Args>(args)...);
+ }
+ if (block.metadata() == Constants::magic_for_empty)
+ {
+ AllocatorTraits::construct(*this, std::addressof(*block), std::forward<Args>(args)...);
+ block.set_metadata(Constants::magic_for_direct_hit);
+ ++num_elements;
+ return { block.it(), true };
+ }
+ else
+ {
+ LinkedListIt parent_block = find_parent_block(block);
+ std::pair<int8_t, LinkedListIt> free_block = find_free_index(parent_block);
+ if (!free_block.first)
+ {
+ grow();
+ return emplace(std::forward<Args>(args)...);
+ }
+ value_type new_value(std::forward<Args>(args)...);
+ for (LinkedListIt it = block;;)
+ {
+ AllocatorTraits::construct(*this, std::addressof(*free_block.second), std::move(*it));
+ AllocatorTraits::destroy(*this, std::addressof(*it));
+ parent_block.set_next(free_block.first);
+ free_block.second.set_metadata(Constants::magic_for_list_entry);
+ if (!it.has_next())
+ {
+ it.set_metadata(Constants::magic_for_empty);
+ break;
+ }
+ LinkedListIt next = it.next(*this);
+ it.set_metadata(Constants::magic_for_empty);
+ block.set_metadata(Constants::magic_for_reserved);
+ it = next;
+ parent_block = free_block.second;
+ free_block = find_free_index(free_block.second);
+ if (!free_block.first)
+ {
+ grow();
+ return emplace(std::move(new_value));
+ }
+ }
+ AllocatorTraits::construct(*this, std::addressof(*block), std::move(new_value));
+ block.set_metadata(Constants::magic_for_direct_hit);
+ ++num_elements;
+ return { block.it(), true };
+ }
+ }
+
+ template<typename... Args>
+ SKA_NOINLINE(std::pair<iterator, bool>) emplace_new_key(LinkedListIt parent, Args &&... args)
+ {
+ if (is_full())
+ {
+ grow();
+ return emplace(std::forward<Args>(args)...);
+ }
+ std::pair<int8_t, LinkedListIt> free_block = find_free_index(parent);
+ if (!free_block.first)
+ {
+ grow();
+ return emplace(std::forward<Args>(args)...);
+ }
+ AllocatorTraits::construct(*this, std::addressof(*free_block.second), std::forward<Args>(args)...);
+ free_block.second.set_metadata(Constants::magic_for_list_entry);
+ parent.set_next(free_block.first);
+ ++num_elements;
+ return { free_block.second.it(), true };
+ }
+
+ LinkedListIt find_direct_hit(LinkedListIt child) const
+ {
+ size_t to_move_hash = hash_object(*child);
+ size_t to_move_index = hash_policy.index_for_hash(to_move_hash, num_slots_minus_one);
+ return { to_move_index, entries + to_move_index / BlockSize };
+ }
+ LinkedListIt find_parent_block(LinkedListIt child)
+ {
+ LinkedListIt parent_block = find_direct_hit(child);
+ for (;;)
+ {
+ LinkedListIt next = parent_block.next(*this);
+ if (next == child)
+ return parent_block;
+ parent_block = next;
+ }
+ }
+
+ std::pair<int8_t, LinkedListIt> find_free_index(LinkedListIt parent) const
+ {
+ for (int8_t jump_index = 1; jump_index < Constants::num_jump_distances; ++jump_index)
+ {
+ size_t index = hash_policy.keep_in_range(parent.index + Constants::jump_distances[jump_index], num_slots_minus_one);
+ BlockPointer block = entries + index / BlockSize;
+ if (block->control_bytes[index % BlockSize] == Constants::magic_for_empty)
+ return { jump_index, { index, block } };
+ }
+ return { 0, {} };
+ }
+
+ void grow()
+ {
+ rehash(std::max(size_t(10), 2 * bucket_count()));
+ }
+
+ size_t calculate_memory_requirement(size_t num_blocks)
+ {
+ size_t memory_required = sizeof(BlockType) * num_blocks;
+ memory_required += BlockSize; // for metadata of past-the-end pointer
+ return memory_required;
+ }
+
+ void deallocate_data(BlockPointer begin, size_t num_slots_minus_one)
+ {
+ if (begin == BlockType::empty_block())
+ return;
+
+ ++num_slots_minus_one;
+ size_t num_blocks = num_slots_minus_one / BlockSize;
+ if (num_slots_minus_one % BlockSize)
+ ++num_blocks;
+ size_t memory = calculate_memory_requirement(num_blocks);
+ unsigned char * as_byte_pointer = reinterpret_cast<unsigned char *>(begin);
+ AllocatorTraits::deallocate(*this, typename AllocatorTraits::pointer(as_byte_pointer), memory);
+ }
+
+ void reset_to_empty_state()
+ {
+ deallocate_data(entries, num_slots_minus_one);
+ entries = BlockType::empty_block();
+ num_slots_minus_one = 0;
+ hash_policy.reset();
+ }
+
+ template<typename U>
+ size_t hash_object(const U & key)
+ {
+ return static_cast<Hasher &>(*this)(key);
+ }
+ template<typename U>
+ size_t hash_object(const U & key) const
+ {
+ return static_cast<const Hasher &>(*this)(key);
+ }
+ template<typename L, typename R>
+ bool compares_equal(const L & lhs, const R & rhs)
+ {
+ return static_cast<Equal &>(*this)(lhs, rhs);
+ }
+
+ struct convertible_to_iterator
+ {
+ BlockPointer it;
+ size_t index;
+
+ operator iterator()
+ {
+ if (it->control_bytes[index % BlockSize] == Constants::magic_for_empty)
+ return ++iterator{it, index};
+ else
+ return { it, index };
+ }
+ operator const_iterator()
+ {
+ if (it->control_bytes[index % BlockSize] == Constants::magic_for_empty)
+ return ++iterator{it, index};
+ else
+ return { it, index };
+ }
+ };
+};
+template<typename T, typename Enable = void>
+struct AlignmentOr8Bytes
+{
+ static constexpr size_t value = 8;
+};
+template<typename T>
+struct AlignmentOr8Bytes<T, typename std::enable_if<alignof(T) >= 1>::type>
+{
+ static constexpr size_t value = alignof(T);
+};
+template<typename... Args>
+struct CalculateBytellBlockSize;
+template<typename First, typename... More>
+struct CalculateBytellBlockSize<First, More...>
+{
+ static constexpr size_t this_value = AlignmentOr8Bytes<First>::value;
+ static constexpr size_t base_value = CalculateBytellBlockSize<More...>::value;
+ static constexpr size_t value = this_value > base_value ? this_value : base_value;
+};
+template<>
+struct CalculateBytellBlockSize<>
+{
+ static constexpr size_t value = 8;
+};
+}
+
+template<typename K, typename V, typename H = std::hash<K>, typename E = std::equal_to<K>, typename A = std::allocator<std::pair<K, V> > >
+class bytell_hash_map
+ : public detailv8::sherwood_v8_table
+ <
+ std::pair<K, V>,
+ K,
+ H,
+ detailv8::KeyOrValueHasher<K, std::pair<K, V>, H>,
+ E,
+ detailv8::KeyOrValueEquality<K, std::pair<K, V>, E>,
+ A,
+ typename std::allocator_traits<A>::template rebind_alloc<unsigned char>,
+ detailv8::CalculateBytellBlockSize<K, V>::value
+ >
+{
+ using Table = detailv8::sherwood_v8_table
+ <
+ std::pair<K, V>,
+ K,
+ H,
+ detailv8::KeyOrValueHasher<K, std::pair<K, V>, H>,
+ E,
+ detailv8::KeyOrValueEquality<K, std::pair<K, V>, E>,
+ A,
+ typename std::allocator_traits<A>::template rebind_alloc<unsigned char>,
+ detailv8::CalculateBytellBlockSize<K, V>::value
+ >;
+public:
+
+ using key_type = K;
+ using mapped_type = V;
+
+ using Table::Table;
+ bytell_hash_map()
+ {
+ }
+
+ inline V & operator[](const K & key)
+ {
+ return emplace(key, convertible_to_value()).first->second;
+ }
+ inline V & operator[](K && key)
+ {
+ return emplace(std::move(key), convertible_to_value()).first->second;
+ }
+ V & at(const K & key)
+ {
+ auto found = this->find(key);
+ if (found == this->end())
+ throw std::out_of_range("Argument passed to at() was not in the map.");
+ return found->second;
+ }
+ const V & at(const K & key) const
+ {
+ auto found = this->find(key);
+ if (found == this->end())
+ throw std::out_of_range("Argument passed to at() was not in the map.");
+ return found->second;
+ }
+
+ using Table::emplace;
+ std::pair<typename Table::iterator, bool> emplace()
+ {
+ return emplace(key_type(), convertible_to_value());
+ }
+ template<typename M>
+ std::pair<typename Table::iterator, bool> insert_or_assign(const key_type & key, M && m)
+ {
+ auto emplace_result = emplace(key, std::forward<M>(m));
+ if (!emplace_result.second)
+ emplace_result.first->second = std::forward<M>(m);
+ return emplace_result;
+ }
+ template<typename M>
+ std::pair<typename Table::iterator, bool> insert_or_assign(key_type && key, M && m)
+ {
+ auto emplace_result = emplace(std::move(key), std::forward<M>(m));
+ if (!emplace_result.second)
+ emplace_result.first->second = std::forward<M>(m);
+ return emplace_result;
+ }
+ template<typename M>
+ typename Table::iterator insert_or_assign(typename Table::const_iterator, const key_type & key, M && m)
+ {
+ return insert_or_assign(key, std::forward<M>(m)).first;
+ }
+ template<typename M>
+ typename Table::iterator insert_or_assign(typename Table::const_iterator, key_type && key, M && m)
+ {
+ return insert_or_assign(std::move(key), std::forward<M>(m)).first;
+ }
+
+ friend bool operator==(const bytell_hash_map & lhs, const bytell_hash_map & rhs)
+ {
+ if (lhs.size() != rhs.size())
+ return false;
+ for (const typename Table::value_type & value : lhs)
+ {
+ auto found = rhs.find(value.first);
+ if (found == rhs.end())
+ return false;
+ else if (value.second != found->second)
+ return false;
+ }
+ return true;
+ }
+ friend bool operator!=(const bytell_hash_map & lhs, const bytell_hash_map & rhs)
+ {
+ return !(lhs == rhs);
+ }
+
+private:
+ struct convertible_to_value
+ {
+ operator V() const
+ {
+ return V();
+ }
+ };
+};
+
+template<typename T, typename H = std::hash<T>, typename E = std::equal_to<T>, typename A = std::allocator<T> >
+class bytell_hash_set
+ : public detailv8::sherwood_v8_table
+ <
+ T,
+ T,
+ H,
+ detailv8::functor_storage<size_t, H>,
+ E,
+ detailv8::functor_storage<bool, E>,
+ A,
+ typename std::allocator_traits<A>::template rebind_alloc<unsigned char>,
+ detailv8::CalculateBytellBlockSize<T>::value
+ >
+{
+ using Table = detailv8::sherwood_v8_table
+ <
+ T,
+ T,
+ H,
+ detailv8::functor_storage<size_t, H>,
+ E,
+ detailv8::functor_storage<bool, E>,
+ A,
+ typename std::allocator_traits<A>::template rebind_alloc<unsigned char>,
+ detailv8::CalculateBytellBlockSize<T>::value
+ >;
+public:
+
+ using key_type = T;
+
+ using Table::Table;
+ bytell_hash_set()
+ {
+ }
+
+ template<typename... Args>
+ std::pair<typename Table::iterator, bool> emplace(Args &&... args)
+ {
+ return Table::emplace(T(std::forward<Args>(args)...));
+ }
+ std::pair<typename Table::iterator, bool> emplace(const key_type & arg)
+ {
+ return Table::emplace(arg);
+ }
+ std::pair<typename Table::iterator, bool> emplace(key_type & arg)
+ {
+ return Table::emplace(arg);
+ }
+ std::pair<typename Table::iterator, bool> emplace(const key_type && arg)
+ {
+ return Table::emplace(std::move(arg));
+ }
+ std::pair<typename Table::iterator, bool> emplace(key_type && arg)
+ {
+ return Table::emplace(std::move(arg));
+ }
+
+ friend bool operator==(const bytell_hash_set & lhs, const bytell_hash_set & rhs)
+ {
+ if (lhs.size() != rhs.size())
+ return false;
+ for (const T & value : lhs)
+ {
+ if (rhs.find(value) == rhs.end())
+ return false;
+ }
+ return true;
+ }
+ friend bool operator!=(const bytell_hash_set & lhs, const bytell_hash_set & rhs)
+ {
+ return !(lhs == rhs);
+ }
+};
+
+} // end namespace ska
diff --git a/benchmarks/others/flat_hash_map.hpp b/benchmarks/others/skarupke/flat_hash_map.hpp
index ea20af93..a8723ee8 100644
--- a/benchmarks/others/flat_hash_map.hpp
+++ b/benchmarks/others/skarupke/flat_hash_map.hpp
@@ -1,1496 +1,1496 @@
-// Copyright Malte Skarupke 2017.
-// Distributed under the Boost Software License, Version 1.0.
-// (See http://www.boost.org/LICENSE_1_0.txt)
-
-#pragma once
-
-#include <cstdint>
-#include <cstddef>
-#include <functional>
-#include <cmath>
-#include <algorithm>
-#include <iterator>
-#include <utility>
-#include <type_traits>
-
-#ifdef _MSC_VER
-#define SKA_NOINLINE(...) __declspec(noinline) __VA_ARGS__
-#else
-#define SKA_NOINLINE(...) __VA_ARGS__ __attribute__((noinline))
-#endif
-
-namespace ska
-{
-struct prime_number_hash_policy;
-struct power_of_two_hash_policy;
-struct fibonacci_hash_policy;
-
-namespace detailv3
-{
-template<typename Result, typename Functor>
-struct functor_storage : Functor
-{
- functor_storage() = default;
- functor_storage(const Functor & functor)
- : Functor(functor)
- {
- }
- template<typename... Args>
- Result operator()(Args &&... args)
- {
- return static_cast<Functor &>(*this)(std::forward<Args>(args)...);
- }
- template<typename... Args>
- Result operator()(Args &&... args) const
- {
- return static_cast<const Functor &>(*this)(std::forward<Args>(args)...);
- }
-};
-template<typename Result, typename... Args>
-struct functor_storage<Result, Result (*)(Args...)>
-{
- typedef Result (*function_ptr)(Args...);
- function_ptr function;
- functor_storage(function_ptr function)
- : function(function)
- {
- }
- Result operator()(Args... args) const
- {
- return function(std::forward<Args>(args)...);
- }
- operator function_ptr &()
- {
- return function;
- }
- operator const function_ptr &()
- {
- return function;
- }
-};
-template<typename key_type, typename value_type, typename hasher>
-struct KeyOrValueHasher : functor_storage<size_t, hasher>
-{
- typedef functor_storage<size_t, hasher> hasher_storage;
- KeyOrValueHasher() = default;
- KeyOrValueHasher(const hasher & hash)
- : hasher_storage(hash)
- {
- }
- size_t operator()(const key_type & key)
- {
- return static_cast<hasher_storage &>(*this)(key);
- }
- size_t operator()(const key_type & key) const
- {
- return static_cast<const hasher_storage &>(*this)(key);
- }
- size_t operator()(const value_type & value)
- {
- return static_cast<hasher_storage &>(*this)(value.first);
- }
- size_t operator()(const value_type & value) const
- {
- return static_cast<const hasher_storage &>(*this)(value.first);
- }
- template<typename F, typename S>
- size_t operator()(const std::pair<F, S> & value)
- {
- return static_cast<hasher_storage &>(*this)(value.first);
- }
- template<typename F, typename S>
- size_t operator()(const std::pair<F, S> & value) const
- {
- return static_cast<const hasher_storage &>(*this)(value.first);
- }
-};
-template<typename key_type, typename value_type, typename key_equal>
-struct KeyOrValueEquality : functor_storage<bool, key_equal>
-{
- typedef functor_storage<bool, key_equal> equality_storage;
- KeyOrValueEquality() = default;
- KeyOrValueEquality(const key_equal & equality)
- : equality_storage(equality)
- {
- }
- bool operator()(const key_type & lhs, const key_type & rhs)
- {
- return static_cast<equality_storage &>(*this)(lhs, rhs);
- }
- bool operator()(const key_type & lhs, const value_type & rhs)
- {
- return static_cast<equality_storage &>(*this)(lhs, rhs.first);
- }
- bool operator()(const value_type & lhs, const key_type & rhs)
- {
- return static_cast<equality_storage &>(*this)(lhs.first, rhs);
- }
- bool operator()(const value_type & lhs, const value_type & rhs)
- {
- return static_cast<equality_storage &>(*this)(lhs.first, rhs.first);
- }
- template<typename F, typename S>
- bool operator()(const key_type & lhs, const std::pair<F, S> & rhs)
- {
- return static_cast<equality_storage &>(*this)(lhs, rhs.first);
- }
- template<typename F, typename S>
- bool operator()(const std::pair<F, S> & lhs, const key_type & rhs)
- {
- return static_cast<equality_storage &>(*this)(lhs.first, rhs);
- }
- template<typename F, typename S>
- bool operator()(const value_type & lhs, const std::pair<F, S> & rhs)
- {
- return static_cast<equality_storage &>(*this)(lhs.first, rhs.first);
- }
- template<typename F, typename S>
- bool operator()(const std::pair<F, S> & lhs, const value_type & rhs)
- {
- return static_cast<equality_storage &>(*this)(lhs.first, rhs.first);
- }
- template<typename FL, typename SL, typename FR, typename SR>
- bool operator()(const std::pair<FL, SL> & lhs, const std::pair<FR, SR> & rhs)
- {
- return static_cast<equality_storage &>(*this)(lhs.first, rhs.first);
- }
-};
-static constexpr int8_t min_lookups = 4;
-template<typename T>
-struct sherwood_v3_entry
-{
- sherwood_v3_entry()
- {
- }
- sherwood_v3_entry(int8_t distance_from_desired)
- : distance_from_desired(distance_from_desired)
- {
- }
- ~sherwood_v3_entry()
- {
- }
- static sherwood_v3_entry * empty_default_table()
- {
- static sherwood_v3_entry result[min_lookups] = { {}, {}, {}, {special_end_value} };
- return result;
- }
-
- bool has_value() const
- {
- return distance_from_desired >= 0;
- }
- bool is_empty() const
- {
- return distance_from_desired < 0;
- }
- bool is_at_desired_position() const
- {
- return distance_from_desired <= 0;
- }
- template<typename... Args>
- void emplace(int8_t distance, Args &&... args)
- {
- new (std::addressof(value)) T(std::forward<Args>(args)...);
- distance_from_desired = distance;
- }
-
- void destroy_value()
- {
- value.~T();
- distance_from_desired = -1;
- }
-
- int8_t distance_from_desired = -1;
- static constexpr int8_t special_end_value = 0;
- union { T value; };
-};
-
-inline int8_t log2(size_t value)
-{
- static constexpr int8_t table[64] =
- {
- 63, 0, 58, 1, 59, 47, 53, 2,
- 60, 39, 48, 27, 54, 33, 42, 3,
- 61, 51, 37, 40, 49, 18, 28, 20,
- 55, 30, 34, 11, 43, 14, 22, 4,
- 62, 57, 46, 52, 38, 26, 32, 41,
- 50, 36, 17, 19, 29, 10, 13, 21,
- 56, 45, 25, 31, 35, 16, 9, 12,
- 44, 24, 15, 8, 23, 7, 6, 5
- };
- value |= value >> 1;
- value |= value >> 2;
- value |= value >> 4;
- value |= value >> 8;
- value |= value >> 16;
- value |= value >> 32;
- return table[((value - (value >> 1)) * 0x07EDD5E59A4E28C2) >> 58];
-}
-
-template<typename T, bool>
-struct AssignIfTrue
-{
- void operator()(T & lhs, const T & rhs)
- {
- lhs = rhs;
- }
- void operator()(T & lhs, T && rhs)
- {
- lhs = std::move(rhs);
- }
-};
-template<typename T>
-struct AssignIfTrue<T, false>
-{
- void operator()(T &, const T &)
- {
- }
- void operator()(T &, T &&)
- {
- }
-};
-
-inline size_t next_power_of_two(size_t i)
-{
- --i;
- i |= i >> 1;
- i |= i >> 2;
- i |= i >> 4;
- i |= i >> 8;
- i |= i >> 16;
- i |= i >> 32;
- ++i;
- return i;
-}
-
-template<typename...> using void_t = void;
-
-template<typename T, typename = void>
-struct HashPolicySelector
-{
- typedef fibonacci_hash_policy type;
-};
-template<typename T>
-struct HashPolicySelector<T, void_t<typename T::hash_policy>>
-{
- typedef typename T::hash_policy type;
-};
-
-template<typename T, typename FindKey, typename ArgumentHash, typename Hasher, typename ArgumentEqual, typename Equal, typename ArgumentAlloc, typename EntryAlloc>
-class sherwood_v3_table : private EntryAlloc, private Hasher, private Equal
-{
- using Entry = detailv3::sherwood_v3_entry<T>;
- using AllocatorTraits = std::allocator_traits<EntryAlloc>;
- using EntryPointer = typename AllocatorTraits::pointer;
- struct convertible_to_iterator;
-
-public:
-
- using value_type = T;
- using size_type = size_t;
- using difference_type = std::ptrdiff_t;
- using hasher = ArgumentHash;
- using key_equal = ArgumentEqual;
- using allocator_type = EntryAlloc;
- using reference = value_type &;
- using const_reference = const value_type &;
- using pointer = value_type *;
- using const_pointer = const value_type *;
-
- sherwood_v3_table()
- {
- }
- explicit sherwood_v3_table(size_type bucket_count, const ArgumentHash & hash = ArgumentHash(), const ArgumentEqual & equal = ArgumentEqual(), const ArgumentAlloc & alloc = ArgumentAlloc())
- : EntryAlloc(alloc), Hasher(hash), Equal(equal)
- {
- rehash(bucket_count);
- }
- sherwood_v3_table(size_type bucket_count, const ArgumentAlloc & alloc)
- : sherwood_v3_table(bucket_count, ArgumentHash(), ArgumentEqual(), alloc)
- {
- }
- sherwood_v3_table(size_type bucket_count, const ArgumentHash & hash, const ArgumentAlloc & alloc)
- : sherwood_v3_table(bucket_count, hash, ArgumentEqual(), alloc)
- {
- }
- explicit sherwood_v3_table(const ArgumentAlloc & alloc)
- : EntryAlloc(alloc)
- {
- }
- template<typename It>
- sherwood_v3_table(It first, It last, size_type bucket_count = 0, const ArgumentHash & hash = ArgumentHash(), const ArgumentEqual & equal = ArgumentEqual(), const ArgumentAlloc & alloc = ArgumentAlloc())
- : sherwood_v3_table(bucket_count, hash, equal, alloc)
- {
- insert(first, last);
- }
- template<typename It>
- sherwood_v3_table(It first, It last, size_type bucket_count, const ArgumentAlloc & alloc)
- : sherwood_v3_table(first, last, bucket_count, ArgumentHash(), ArgumentEqual(), alloc)
- {
- }
- template<typename It>
- sherwood_v3_table(It first, It last, size_type bucket_count, const ArgumentHash & hash, const ArgumentAlloc & alloc)
- : sherwood_v3_table(first, last, bucket_count, hash, ArgumentEqual(), alloc)
- {
- }
- sherwood_v3_table(std::initializer_list<T> il, size_type bucket_count = 0, const ArgumentHash & hash = ArgumentHash(), const ArgumentEqual & equal = ArgumentEqual(), const ArgumentAlloc & alloc = ArgumentAlloc())
- : sherwood_v3_table(bucket_count, hash, equal, alloc)
- {
- if (bucket_count == 0)
- rehash(il.size());
- insert(il.begin(), il.end());
- }
- sherwood_v3_table(std::initializer_list<T> il, size_type bucket_count, const ArgumentAlloc & alloc)
- : sherwood_v3_table(il, bucket_count, ArgumentHash(), ArgumentEqual(), alloc)
- {
- }
- sherwood_v3_table(std::initializer_list<T> il, size_type bucket_count, const ArgumentHash & hash, const ArgumentAlloc & alloc)
- : sherwood_v3_table(il, bucket_count, hash, ArgumentEqual(), alloc)
- {
- }
- sherwood_v3_table(const sherwood_v3_table & other)
- : sherwood_v3_table(other, AllocatorTraits::select_on_container_copy_construction(other.get_allocator()))
- {
- }
- sherwood_v3_table(const sherwood_v3_table & other, const ArgumentAlloc & alloc)
- : EntryAlloc(alloc), Hasher(other), Equal(other), _max_load_factor(other._max_load_factor)
- {
- rehash_for_other_container(other);
- try
- {
- insert(other.begin(), other.end());
- }
- catch(...)
- {
- clear();
- deallocate_data(entries, num_slots_minus_one, max_lookups);
- throw;
- }
- }
- sherwood_v3_table(sherwood_v3_table && other) noexcept
- : EntryAlloc(std::move(other)), Hasher(std::move(other)), Equal(std::move(other))
- {
- swap_pointers(other);
- }
- sherwood_v3_table(sherwood_v3_table && other, const ArgumentAlloc & alloc) noexcept
- : EntryAlloc(alloc), Hasher(std::move(other)), Equal(std::move(other))
- {
- swap_pointers(other);
- }
- sherwood_v3_table & operator=(const sherwood_v3_table & other)
- {
- if (this == std::addressof(other))
- return *this;
-
- clear();
- if (AllocatorTraits::propagate_on_container_copy_assignment::value)
- {
- if (static_cast<EntryAlloc &>(*this) != static_cast<const EntryAlloc &>(other))
- {
- reset_to_empty_state();
- }
- AssignIfTrue<EntryAlloc, AllocatorTraits::propagate_on_container_copy_assignment::value>()(*this, other);
- }
- _max_load_factor = other._max_load_factor;
- static_cast<Hasher &>(*this) = other;
- static_cast<Equal &>(*this) = other;
- rehash_for_other_container(other);
- insert(other.begin(), other.end());
- return *this;
- }
- sherwood_v3_table & operator=(sherwood_v3_table && other) noexcept
- {
- if (this == std::addressof(other))
- return *this;
- else if (AllocatorTraits::propagate_on_container_move_assignment::value)
- {
- clear();
- reset_to_empty_state();
- AssignIfTrue<EntryAlloc, AllocatorTraits::propagate_on_container_move_assignment::value>()(*this, std::move(other));
- swap_pointers(other);
- }
- else if (static_cast<EntryAlloc &>(*this) == static_cast<EntryAlloc &>(other))
- {
- swap_pointers(other);
- }
- else
- {
- clear();
- _max_load_factor = other._max_load_factor;
- rehash_for_other_container(other);
- for (T & elem : other)
- emplace(std::move(elem));
- other.clear();
- }
- static_cast<Hasher &>(*this) = std::move(other);
- static_cast<Equal &>(*this) = std::move(other);
- return *this;
- }
- ~sherwood_v3_table()
- {
- clear();
- deallocate_data(entries, num_slots_minus_one, max_lookups);
- }
-
- const allocator_type & get_allocator() const
- {
- return static_cast<const allocator_type &>(*this);
- }
- const ArgumentEqual & key_eq() const
- {
- return static_cast<const ArgumentEqual &>(*this);
- }
- const ArgumentHash & hash_function() const
- {
- return static_cast<const ArgumentHash &>(*this);
- }
-
- template<typename ValueType>
- struct templated_iterator
- {
- templated_iterator() = default;
- templated_iterator(EntryPointer current)
- : current(current)
- {
- }
- EntryPointer current = EntryPointer();
-
- using iterator_category = std::forward_iterator_tag;
- using value_type = ValueType;
- using difference_type = ptrdiff_t;
- using pointer = ValueType *;
- using reference = ValueType &;
-
- friend bool operator==(const templated_iterator & lhs, const templated_iterator & rhs)
- {
- return lhs.current == rhs.current;
- }
- friend bool operator!=(const templated_iterator & lhs, const templated_iterator & rhs)
- {
- return !(lhs == rhs);
- }
-
- templated_iterator & operator++()
- {
- do
- {
- ++current;
- }
- while(current->is_empty());
- return *this;
- }
- templated_iterator operator++(int)
- {
- templated_iterator copy(*this);
- ++*this;
- return copy;
- }
-
- ValueType & operator*() const
- {
- return current->value;
- }
- ValueType * operator->() const
- {
- return std::addressof(current->value);
- }
-
- operator templated_iterator<const value_type>() const
- {
- return { current };
- }
- };
- using iterator = templated_iterator<value_type>;
- using const_iterator = templated_iterator<const value_type>;
-
- iterator begin()
- {
- for (EntryPointer it = entries;; ++it)
- {
- if (it->has_value())
- return { it };
- }
- }
- const_iterator begin() const
- {
- for (EntryPointer it = entries;; ++it)
- {
- if (it->has_value())
- return { it };
- }
- }
- const_iterator cbegin() const
- {
- return begin();
- }
- iterator end()
- {
- return { entries + static_cast<ptrdiff_t>(num_slots_minus_one + max_lookups) };
- }
- const_iterator end() const
- {
- return { entries + static_cast<ptrdiff_t>(num_slots_minus_one + max_lookups) };
- }
- const_iterator cend() const
- {
- return end();
- }
-
- iterator find(const FindKey & key)
- {
- size_t index = hash_policy.index_for_hash(hash_object(key), num_slots_minus_one);
- EntryPointer it = entries + ptrdiff_t(index);
- for (int8_t distance = 0; it->distance_from_desired >= distance; ++distance, ++it)
- {
- if (compares_equal(key, it->value))
- return { it };
- }
- return end();
- }
- const_iterator find(const FindKey & key) const
- {
- return const_cast<sherwood_v3_table *>(this)->find(key);
- }
- size_t count(const FindKey & key) const
- {
- return find(key) == end() ? 0 : 1;
- }
- std::pair<iterator, iterator> equal_range(const FindKey & key)
- {
- iterator found = find(key);
- if (found == end())
- return { found, found };
- else
- return { found, std::next(found) };
- }
- std::pair<const_iterator, const_iterator> equal_range(const FindKey & key) const
- {
- const_iterator found = find(key);
- if (found == end())
- return { found, found };
- else
- return { found, std::next(found) };
- }
-
- template<typename Key, typename... Args>
- std::pair<iterator, bool> emplace(Key && key, Args &&... args)
- {
- size_t index = hash_policy.index_for_hash(hash_object(key), num_slots_minus_one);
- EntryPointer current_entry = entries + ptrdiff_t(index);
- int8_t distance_from_desired = 0;
- for (; current_entry->distance_from_desired >= distance_from_desired; ++current_entry, ++distance_from_desired)
- {
- if (compares_equal(key, current_entry->value))
- return { { current_entry }, false };
- }
- return emplace_new_key(distance_from_desired, current_entry, std::forward<Key>(key), std::forward<Args>(args)...);
- }
-
- std::pair<iterator, bool> insert(const value_type & value)
- {
- return emplace(value);
- }
- std::pair<iterator, bool> insert(value_type && value)
- {
- return emplace(std::move(value));
- }
- template<typename... Args>
- iterator emplace_hint(const_iterator, Args &&... args)
- {
- return emplace(std::forward<Args>(args)...).first;
- }
- iterator insert(const_iterator, const value_type & value)
- {
- return emplace(value).first;
- }
- iterator insert(const_iterator, value_type && value)
- {
- return emplace(std::move(value)).first;
- }
-
- template<typename It>
- void insert(It begin, It end)
- {
- for (; begin != end; ++begin)
- {
- emplace(*begin);
- }
- }
- void insert(std::initializer_list<value_type> il)
- {
- insert(il.begin(), il.end());
- }
-
- void rehash(size_t num_buckets)
- {
- num_buckets = std::max(num_buckets, static_cast<size_t>(std::ceil(num_elements / static_cast<double>(_max_load_factor))));
- if (num_buckets == 0)
- {
- reset_to_empty_state();
- return;
- }
- auto new_prime_index = hash_policy.next_size_over(num_buckets);
- if (num_buckets == bucket_count())
- return;
- int8_t new_max_lookups = compute_max_lookups(num_buckets);
- EntryPointer new_buckets(AllocatorTraits::allocate(*this, num_buckets + new_max_lookups));
- EntryPointer special_end_item = new_buckets + static_cast<ptrdiff_t>(num_buckets + new_max_lookups - 1);
- for (EntryPointer it = new_buckets; it != special_end_item; ++it)
- it->distance_from_desired = -1;
- special_end_item->distance_from_desired = Entry::special_end_value;
- std::swap(entries, new_buckets);
- std::swap(num_slots_minus_one, num_buckets);
- --num_slots_minus_one;
- hash_policy.commit(new_prime_index);
- int8_t old_max_lookups = max_lookups;
- max_lookups = new_max_lookups;
- num_elements = 0;
- for (EntryPointer it = new_buckets, end = it + static_cast<ptrdiff_t>(num_buckets + old_max_lookups); it != end; ++it)
- {
- if (it->has_value())
- {
- emplace(std::move(it->value));
- it->destroy_value();
- }
- }
- deallocate_data(new_buckets, num_buckets, old_max_lookups);
- }
-
- void reserve(size_t num_elements)
- {
- size_t required_buckets = num_buckets_for_reserve(num_elements);
- if (required_buckets > bucket_count())
- rehash(required_buckets);
- }
-
- // the return value is a type that can be converted to an iterator
- // the reason for doing this is that it's not free to find the
- // iterator pointing at the next element. if you care about the
- // next iterator, turn the return value into an iterator
- convertible_to_iterator erase(const_iterator to_erase)
- {
- EntryPointer current = to_erase.current;
- current->destroy_value();
- --num_elements;
- for (EntryPointer next = current + ptrdiff_t(1); !next->is_at_desired_position(); ++current, ++next)
- {
- current->emplace(next->distance_from_desired - 1, std::move(next->value));
- next->destroy_value();
- }
- return { to_erase.current };
- }
-
- iterator erase(const_iterator begin_it, const_iterator end_it)
- {
- if (begin_it == end_it)
- return { begin_it.current };
- for (EntryPointer it = begin_it.current, end = end_it.current; it != end; ++it)
- {
- if (it->has_value())
- {
- it->destroy_value();
- --num_elements;
- }
- }
- if (end_it == this->end())
- return this->end();
- ptrdiff_t num_to_move = std::min(static_cast<ptrdiff_t>(end_it.current->distance_from_desired), end_it.current - begin_it.current);
- EntryPointer to_return = end_it.current - num_to_move;
- for (EntryPointer it = end_it.current; !it->is_at_desired_position();)
- {
- EntryPointer target = it - num_to_move;
- target->emplace(it->distance_from_desired - num_to_move, std::move(it->value));
- it->destroy_value();
- ++it;
- num_to_move = std::min(static_cast<ptrdiff_t>(it->distance_from_desired), num_to_move);
- }
- return { to_return };
- }
-
- size_t erase(const FindKey & key)
- {
- auto found = find(key);
- if (found == end())
- return 0;
- else
- {
- erase(found);
- return 1;
- }
- }
-
- void clear()
- {
- for (EntryPointer it = entries, end = it + static_cast<ptrdiff_t>(num_slots_minus_one + max_lookups); it != end; ++it)
- {
- if (it->has_value())
- it->destroy_value();
- }
- num_elements = 0;
- }
-
- void shrink_to_fit()
- {
- rehash_for_other_container(*this);
- }
-
- void swap(sherwood_v3_table & other)
- {
- using std::swap;
- swap_pointers(other);
- swap(static_cast<ArgumentHash &>(*this), static_cast<ArgumentHash &>(other));
- swap(static_cast<ArgumentEqual &>(*this), static_cast<ArgumentEqual &>(other));
- if (AllocatorTraits::propagate_on_container_swap::value)
- swap(static_cast<EntryAlloc &>(*this), static_cast<EntryAlloc &>(other));
- }
-
- size_t size() const
- {
- return num_elements;
- }
- size_t max_size() const
- {
- return (AllocatorTraits::max_size(*this)) / sizeof(Entry);
- }
- size_t bucket_count() const
- {
- return num_slots_minus_one ? num_slots_minus_one + 1 : 0;
- }
- size_type max_bucket_count() const
- {
- return (AllocatorTraits::max_size(*this) - min_lookups) / sizeof(Entry);
- }
- size_t bucket(const FindKey & key) const
- {
- return hash_policy.index_for_hash(hash_object(key), num_slots_minus_one);
- }
- float load_factor() const
- {
- size_t buckets = bucket_count();
- if (buckets)
- return static_cast<float>(num_elements) / bucket_count();
- else
- return 0;
- }
- void max_load_factor(float value)
- {
- _max_load_factor = value;
- }
- float max_load_factor() const
- {
- return _max_load_factor;
- }
-
- bool empty() const
- {
- return num_elements == 0;
- }
-
-private:
- EntryPointer entries = Entry::empty_default_table();
- size_t num_slots_minus_one = 0;
- typename HashPolicySelector<ArgumentHash>::type hash_policy;
- int8_t max_lookups = detailv3::min_lookups - 1;
- float _max_load_factor = 0.5f;
- size_t num_elements = 0;
-
- static int8_t compute_max_lookups(size_t num_buckets)
- {
- int8_t desired = detailv3::log2(num_buckets);
- return std::max(detailv3::min_lookups, desired);
- }
-
- size_t num_buckets_for_reserve(size_t num_elements) const
- {
- return static_cast<size_t>(std::ceil(num_elements / std::min(0.5, static_cast<double>(_max_load_factor))));
- }
- void rehash_for_other_container(const sherwood_v3_table & other)
- {
- rehash(std::min(num_buckets_for_reserve(other.size()), other.bucket_count()));
- }
-
- void swap_pointers(sherwood_v3_table & other)
- {
- using std::swap;
- swap(hash_policy, other.hash_policy);
- swap(entries, other.entries);
- swap(num_slots_minus_one, other.num_slots_minus_one);
- swap(num_elements, other.num_elements);
- swap(max_lookups, other.max_lookups);
- swap(_max_load_factor, other._max_load_factor);
- }
-
- template<typename Key, typename... Args>
- SKA_NOINLINE(std::pair<iterator, bool>) emplace_new_key(int8_t distance_from_desired, EntryPointer current_entry, Key && key, Args &&... args)
- {
- using std::swap;
- if (num_slots_minus_one == 0 || distance_from_desired == max_lookups || num_elements + 1 > (num_slots_minus_one + 1) * static_cast<double>(_max_load_factor))
- {
- grow();
- return emplace(std::forward<Key>(key), std::forward<Args>(args)...);
- }
- else if (current_entry->is_empty())
- {
- current_entry->emplace(distance_from_desired, std::forward<Key>(key), std::forward<Args>(args)...);
- ++num_elements;
- return { { current_entry }, true };
- }
- value_type to_insert(std::forward<Key>(key), std::forward<Args>(args)...);
- swap(distance_from_desired, current_entry->distance_from_desired);
- swap(to_insert, current_entry->value);
- iterator result = { current_entry };
- for (++distance_from_desired, ++current_entry;; ++current_entry)
- {
- if (current_entry->is_empty())
- {
- current_entry->emplace(distance_from_desired, std::move(to_insert));
- ++num_elements;
- return { result, true };
- }
- else if (current_entry->distance_from_desired < distance_from_desired)
- {
- swap(distance_from_desired, current_entry->distance_from_desired);
- swap(to_insert, current_entry->value);
- ++distance_from_desired;
- }
- else
- {
- ++distance_from_desired;
- if (distance_from_desired == max_lookups)
- {
- swap(to_insert, result.current->value);
- grow();
- return emplace(std::move(to_insert));
- }
- }
- }
- }
-
- void grow()
- {
- rehash(std::max(size_t(4), 2 * bucket_count()));
- }
-
- void deallocate_data(EntryPointer begin, size_t num_slots_minus_one, int8_t max_lookups)
- {
- if (begin != Entry::empty_default_table())
- {
- AllocatorTraits::deallocate(*this, begin, num_slots_minus_one + max_lookups + 1);
- }
- }
-
- void reset_to_empty_state()
- {
- deallocate_data(entries, num_slots_minus_one, max_lookups);
- entries = Entry::empty_default_table();
- num_slots_minus_one = 0;
- hash_policy.reset();
- max_lookups = detailv3::min_lookups - 1;
- }
-
- template<typename U>
- size_t hash_object(const U & key)
- {
- return static_cast<Hasher &>(*this)(key);
- }
- template<typename U>
- size_t hash_object(const U & key) const
- {
- return static_cast<const Hasher &>(*this)(key);
- }
- template<typename L, typename R>
- bool compares_equal(const L & lhs, const R & rhs)
- {
- return static_cast<Equal &>(*this)(lhs, rhs);
- }
-
- struct convertible_to_iterator
- {
- EntryPointer it;
-
- operator iterator()
- {
- if (it->has_value())
- return { it };
- else
- return ++iterator{it};
- }
- operator const_iterator()
- {
- if (it->has_value())
- return { it };
- else
- return ++const_iterator{it};
- }
- };
-
-};
-}
-
-struct prime_number_hash_policy
-{
- static size_t mod0(size_t) { return 0llu; }
- static size_t mod2(size_t hash) { return hash % 2llu; }
- static size_t mod3(size_t hash) { return hash % 3llu; }
- static size_t mod5(size_t hash) { return hash % 5llu; }
- static size_t mod7(size_t hash) { return hash % 7llu; }
- static size_t mod11(size_t hash) { return hash % 11llu; }
- static size_t mod13(size_t hash) { return hash % 13llu; }
- static size_t mod17(size_t hash) { return hash % 17llu; }
- static size_t mod23(size_t hash) { return hash % 23llu; }
- static size_t mod29(size_t hash) { return hash % 29llu; }
- static size_t mod37(size_t hash) { return hash % 37llu; }
- static size_t mod47(size_t hash) { return hash % 47llu; }
- static size_t mod59(size_t hash) { return hash % 59llu; }
- static size_t mod73(size_t hash) { return hash % 73llu; }
- static size_t mod97(size_t hash) { return hash % 97llu; }
- static size_t mod127(size_t hash) { return hash % 127llu; }
- static size_t mod151(size_t hash) { return hash % 151llu; }
- static size_t mod197(size_t hash) { return hash % 197llu; }
- static size_t mod251(size_t hash) { return hash % 251llu; }
- static size_t mod313(size_t hash) { return hash % 313llu; }
- static size_t mod397(size_t hash) { return hash % 397llu; }
- static size_t mod499(size_t hash) { return hash % 499llu; }
- static size_t mod631(size_t hash) { return hash % 631llu; }
- static size_t mod797(size_t hash) { return hash % 797llu; }
- static size_t mod1009(size_t hash) { return hash % 1009llu; }
- static size_t mod1259(size_t hash) { return hash % 1259llu; }
- static size_t mod1597(size_t hash) { return hash % 1597llu; }
- static size_t mod2011(size_t hash) { return hash % 2011llu; }
- static size_t mod2539(size_t hash) { return hash % 2539llu; }
- static size_t mod3203(size_t hash) { return hash % 3203llu; }
- static size_t mod4027(size_t hash) { return hash % 4027llu; }
- static size_t mod5087(size_t hash) { return hash % 5087llu; }
- static size_t mod6421(size_t hash) { return hash % 6421llu; }
- static size_t mod8089(size_t hash) { return hash % 8089llu; }
- static size_t mod10193(size_t hash) { return hash % 10193llu; }
- static size_t mod12853(size_t hash) { return hash % 12853llu; }
- static size_t mod16193(size_t hash) { return hash % 16193llu; }
- static size_t mod20399(size_t hash) { return hash % 20399llu; }
- static size_t mod25717(size_t hash) { return hash % 25717llu; }
- static size_t mod32401(size_t hash) { return hash % 32401llu; }
- static size_t mod40823(size_t hash) { return hash % 40823llu; }
- static size_t mod51437(size_t hash) { return hash % 51437llu; }
- static size_t mod64811(size_t hash) { return hash % 64811llu; }
- static size_t mod81649(size_t hash) { return hash % 81649llu; }
- static size_t mod102877(size_t hash) { return hash % 102877llu; }
- static size_t mod129607(size_t hash) { return hash % 129607llu; }
- static size_t mod163307(size_t hash) { return hash % 163307llu; }
- static size_t mod205759(size_t hash) { return hash % 205759llu; }
- static size_t mod259229(size_t hash) { return hash % 259229llu; }
- static size_t mod326617(size_t hash) { return hash % 326617llu; }
- static size_t mod411527(size_t hash) { return hash % 411527llu; }
- static size_t mod518509(size_t hash) { return hash % 518509llu; }
- static size_t mod653267(size_t hash) { return hash % 653267llu; }
- static size_t mod823117(size_t hash) { return hash % 823117llu; }
- static size_t mod1037059(size_t hash) { return hash % 1037059llu; }
- static size_t mod1306601(size_t hash) { return hash % 1306601llu; }
- static size_t mod1646237(size_t hash) { return hash % 1646237llu; }
- static size_t mod2074129(size_t hash) { return hash % 2074129llu; }
- static size_t mod2613229(size_t hash) { return hash % 2613229llu; }
- static size_t mod3292489(size_t hash) { return hash % 3292489llu; }
- static size_t mod4148279(size_t hash) { return hash % 4148279llu; }
- static size_t mod5226491(size_t hash) { return hash % 5226491llu; }
- static size_t mod6584983(size_t hash) { return hash % 6584983llu; }
- static size_t mod8296553(size_t hash) { return hash % 8296553llu; }
- static size_t mod10453007(size_t hash) { return hash % 10453007llu; }
- static size_t mod13169977(size_t hash) { return hash % 13169977llu; }
- static size_t mod16593127(size_t hash) { return hash % 16593127llu; }
- static size_t mod20906033(size_t hash) { return hash % 20906033llu; }
- static size_t mod26339969(size_t hash) { return hash % 26339969llu; }
- static size_t mod33186281(size_t hash) { return hash % 33186281llu; }
- static size_t mod41812097(size_t hash) { return hash % 41812097llu; }
- static size_t mod52679969(size_t hash) { return hash % 52679969llu; }
- static size_t mod66372617(size_t hash) { return hash % 66372617llu; }
- static size_t mod83624237(size_t hash) { return hash % 83624237llu; }
- static size_t mod105359939(size_t hash) { return hash % 105359939llu; }
- static size_t mod132745199(size_t hash) { return hash % 132745199llu; }
- static size_t mod167248483(size_t hash) { return hash % 167248483llu; }
- static size_t mod210719881(size_t hash) { return hash % 210719881llu; }
- static size_t mod265490441(size_t hash) { return hash % 265490441llu; }
- static size_t mod334496971(size_t hash) { return hash % 334496971llu; }
- static size_t mod421439783(size_t hash) { return hash % 421439783llu; }
- static size_t mod530980861(size_t hash) { return hash % 530980861llu; }
- static size_t mod668993977(size_t hash) { return hash % 668993977llu; }
- static size_t mod842879579(size_t hash) { return hash % 842879579llu; }
- static size_t mod1061961721(size_t hash) { return hash % 1061961721llu; }
- static size_t mod1337987929(size_t hash) { return hash % 1337987929llu; }
- static size_t mod1685759167(size_t hash) { return hash % 1685759167llu; }
- static size_t mod2123923447(size_t hash) { return hash % 2123923447llu; }
- static size_t mod2675975881(size_t hash) { return hash % 2675975881llu; }
- static size_t mod3371518343(size_t hash) { return hash % 3371518343llu; }
- static size_t mod4247846927(size_t hash) { return hash % 4247846927llu; }
- static size_t mod5351951779(size_t hash) { return hash % 5351951779llu; }
- static size_t mod6743036717(size_t hash) { return hash % 6743036717llu; }
- static size_t mod8495693897(size_t hash) { return hash % 8495693897llu; }
- static size_t mod10703903591(size_t hash) { return hash % 10703903591llu; }
- static size_t mod13486073473(size_t hash) { return hash % 13486073473llu; }
- static size_t mod16991387857(size_t hash) { return hash % 16991387857llu; }
- static size_t mod21407807219(size_t hash) { return hash % 21407807219llu; }
- static size_t mod26972146961(size_t hash) { return hash % 26972146961llu; }
- static size_t mod33982775741(size_t hash) { return hash % 33982775741llu; }
- static size_t mod42815614441(size_t hash) { return hash % 42815614441llu; }
- static size_t mod53944293929(size_t hash) { return hash % 53944293929llu; }
- static size_t mod67965551447(size_t hash) { return hash % 67965551447llu; }
- static size_t mod85631228929(size_t hash) { return hash % 85631228929llu; }
- static size_t mod107888587883(size_t hash) { return hash % 107888587883llu; }
- static size_t mod135931102921(size_t hash) { return hash % 135931102921llu; }
- static size_t mod171262457903(size_t hash) { return hash % 171262457903llu; }
- static size_t mod215777175787(size_t hash) { return hash % 215777175787llu; }
- static size_t mod271862205833(size_t hash) { return hash % 271862205833llu; }
- static size_t mod342524915839(size_t hash) { return hash % 342524915839llu; }
- static size_t mod431554351609(size_t hash) { return hash % 431554351609llu; }
- static size_t mod543724411781(size_t hash) { return hash % 543724411781llu; }
- static size_t mod685049831731(size_t hash) { return hash % 685049831731llu; }
- static size_t mod863108703229(size_t hash) { return hash % 863108703229llu; }
- static size_t mod1087448823553(size_t hash) { return hash % 1087448823553llu; }
- static size_t mod1370099663459(size_t hash) { return hash % 1370099663459llu; }
- static size_t mod1726217406467(size_t hash) { return hash % 1726217406467llu; }
- static size_t mod2174897647073(size_t hash) { return hash % 2174897647073llu; }
- static size_t mod2740199326961(size_t hash) { return hash % 2740199326961llu; }
- static size_t mod3452434812973(size_t hash) { return hash % 3452434812973llu; }
- static size_t mod4349795294267(size_t hash) { return hash % 4349795294267llu; }
- static size_t mod5480398654009(size_t hash) { return hash % 5480398654009llu; }
- static size_t mod6904869625999(size_t hash) { return hash % 6904869625999llu; }
- static size_t mod8699590588571(size_t hash) { return hash % 8699590588571llu; }
- static size_t mod10960797308051(size_t hash) { return hash % 10960797308051llu; }
- static size_t mod13809739252051(size_t hash) { return hash % 13809739252051llu; }
- static size_t mod17399181177241(size_t hash) { return hash % 17399181177241llu; }
- static size_t mod21921594616111(size_t hash) { return hash % 21921594616111llu; }
- static size_t mod27619478504183(size_t hash) { return hash % 27619478504183llu; }
- static size_t mod34798362354533(size_t hash) { return hash % 34798362354533llu; }
- static size_t mod43843189232363(size_t hash) { return hash % 43843189232363llu; }
- static size_t mod55238957008387(size_t hash) { return hash % 55238957008387llu; }
- static size_t mod69596724709081(size_t hash) { return hash % 69596724709081llu; }
- static size_t mod87686378464759(size_t hash) { return hash % 87686378464759llu; }
- static size_t mod110477914016779(size_t hash) { return hash % 110477914016779llu; }
- static size_t mod139193449418173(size_t hash) { return hash % 139193449418173llu; }
- static size_t mod175372756929481(size_t hash) { return hash % 175372756929481llu; }
- static size_t mod220955828033581(size_t hash) { return hash % 220955828033581llu; }
- static size_t mod278386898836457(size_t hash) { return hash % 278386898836457llu; }
- static size_t mod350745513859007(size_t hash) { return hash % 350745513859007llu; }
- static size_t mod441911656067171(size_t hash) { return hash % 441911656067171llu; }
- static size_t mod556773797672909(size_t hash) { return hash % 556773797672909llu; }
- static size_t mod701491027718027(size_t hash) { return hash % 701491027718027llu; }
- static size_t mod883823312134381(size_t hash) { return hash % 883823312134381llu; }
- static size_t mod1113547595345903(size_t hash) { return hash % 1113547595345903llu; }
- static size_t mod1402982055436147(size_t hash) { return hash % 1402982055436147llu; }
- static size_t mod1767646624268779(size_t hash) { return hash % 1767646624268779llu; }
- static size_t mod2227095190691797(size_t hash) { return hash % 2227095190691797llu; }
- static size_t mod2805964110872297(size_t hash) { return hash % 2805964110872297llu; }
- static size_t mod3535293248537579(size_t hash) { return hash % 3535293248537579llu; }
- static size_t mod4454190381383713(size_t hash) { return hash % 4454190381383713llu; }
- static size_t mod5611928221744609(size_t hash) { return hash % 5611928221744609llu; }
- static size_t mod7070586497075177(size_t hash) { return hash % 7070586497075177llu; }
- static size_t mod8908380762767489(size_t hash) { return hash % 8908380762767489llu; }
- static size_t mod11223856443489329(size_t hash) { return hash % 11223856443489329llu; }
- static size_t mod14141172994150357(size_t hash) { return hash % 14141172994150357llu; }
- static size_t mod17816761525534927(size_t hash) { return hash % 17816761525534927llu; }
- static size_t mod22447712886978529(size_t hash) { return hash % 22447712886978529llu; }
- static size_t mod28282345988300791(size_t hash) { return hash % 28282345988300791llu; }
- static size_t mod35633523051069991(size_t hash) { return hash % 35633523051069991llu; }
- static size_t mod44895425773957261(size_t hash) { return hash % 44895425773957261llu; }
- static size_t mod56564691976601587(size_t hash) { return hash % 56564691976601587llu; }
- static size_t mod71267046102139967(size_t hash) { return hash % 71267046102139967llu; }
- static size_t mod89790851547914507(size_t hash) { return hash % 89790851547914507llu; }
- static size_t mod113129383953203213(size_t hash) { return hash % 113129383953203213llu; }
- static size_t mod142534092204280003(size_t hash) { return hash % 142534092204280003llu; }
- static size_t mod179581703095829107(size_t hash) { return hash % 179581703095829107llu; }
- static size_t mod226258767906406483(size_t hash) { return hash % 226258767906406483llu; }
- static size_t mod285068184408560057(size_t hash) { return hash % 285068184408560057llu; }
- static size_t mod359163406191658253(size_t hash) { return hash % 359163406191658253llu; }
- static size_t mod452517535812813007(size_t hash) { return hash % 452517535812813007llu; }
- static size_t mod570136368817120201(size_t hash) { return hash % 570136368817120201llu; }
- static size_t mod718326812383316683(size_t hash) { return hash % 718326812383316683llu; }
- static size_t mod905035071625626043(size_t hash) { return hash % 905035071625626043llu; }
- static size_t mod1140272737634240411(size_t hash) { return hash % 1140272737634240411llu; }
- static size_t mod1436653624766633509(size_t hash) { return hash % 1436653624766633509llu; }
- static size_t mod1810070143251252131(size_t hash) { return hash % 1810070143251252131llu; }
- static size_t mod2280545475268481167(size_t hash) { return hash % 2280545475268481167llu; }
- static size_t mod2873307249533267101(size_t hash) { return hash % 2873307249533267101llu; }
- static size_t mod3620140286502504283(size_t hash) { return hash % 3620140286502504283llu; }
- static size_t mod4561090950536962147(size_t hash) { return hash % 4561090950536962147llu; }
- static size_t mod5746614499066534157(size_t hash) { return hash % 5746614499066534157llu; }
- static size_t mod7240280573005008577(size_t hash) { return hash % 7240280573005008577llu; }
- static size_t mod9122181901073924329(size_t hash) { return hash % 9122181901073924329llu; }
- static size_t mod11493228998133068689(size_t hash) { return hash % 11493228998133068689llu; }
- static size_t mod14480561146010017169(size_t hash) { return hash % 14480561146010017169llu; }
- static size_t mod18446744073709551557(size_t hash) { return hash % 18446744073709551557llu; }
-
- using mod_function = size_t (*)(size_t);
-
- mod_function next_size_over(size_t & size) const
- {
- // prime numbers generated by the following method:
- // 1. start with a prime p = 2
- // 2. go to wolfram alpha and get p = NextPrime(2 * p)
- // 3. repeat 2. until you overflow 64 bits
- // you now have large gaps which you would hit if somebody called reserve() with an unlucky number.
- // 4. to fill the gaps for every prime p go to wolfram alpha and get ClosestPrime(p * 2^(1/3)) and ClosestPrime(p * 2^(2/3)) and put those in the gaps
- // 5. get PrevPrime(2^64) and put it at the end
- static constexpr const size_t prime_list[] =
- {
- 2llu, 3llu, 5llu, 7llu, 11llu, 13llu, 17llu, 23llu, 29llu, 37llu, 47llu,
- 59llu, 73llu, 97llu, 127llu, 151llu, 197llu, 251llu, 313llu, 397llu,
- 499llu, 631llu, 797llu, 1009llu, 1259llu, 1597llu, 2011llu, 2539llu,
- 3203llu, 4027llu, 5087llu, 6421llu, 8089llu, 10193llu, 12853llu, 16193llu,
- 20399llu, 25717llu, 32401llu, 40823llu, 51437llu, 64811llu, 81649llu,
- 102877llu, 129607llu, 163307llu, 205759llu, 259229llu, 326617llu,
- 411527llu, 518509llu, 653267llu, 823117llu, 1037059llu, 1306601llu,
- 1646237llu, 2074129llu, 2613229llu, 3292489llu, 4148279llu, 5226491llu,
- 6584983llu, 8296553llu, 10453007llu, 13169977llu, 16593127llu, 20906033llu,
- 26339969llu, 33186281llu, 41812097llu, 52679969llu, 66372617llu,
- 83624237llu, 105359939llu, 132745199llu, 167248483llu, 210719881llu,
- 265490441llu, 334496971llu, 421439783llu, 530980861llu, 668993977llu,
- 842879579llu, 1061961721llu, 1337987929llu, 1685759167llu, 2123923447llu,
- 2675975881llu, 3371518343llu, 4247846927llu, 5351951779llu, 6743036717llu,
- 8495693897llu, 10703903591llu, 13486073473llu, 16991387857llu,
- 21407807219llu, 26972146961llu, 33982775741llu, 42815614441llu,
- 53944293929llu, 67965551447llu, 85631228929llu, 107888587883llu,
- 135931102921llu, 171262457903llu, 215777175787llu, 271862205833llu,
- 342524915839llu, 431554351609llu, 543724411781llu, 685049831731llu,
- 863108703229llu, 1087448823553llu, 1370099663459llu, 1726217406467llu,
- 2174897647073llu, 2740199326961llu, 3452434812973llu, 4349795294267llu,
- 5480398654009llu, 6904869625999llu, 8699590588571llu, 10960797308051llu,
- 13809739252051llu, 17399181177241llu, 21921594616111llu, 27619478504183llu,
- 34798362354533llu, 43843189232363llu, 55238957008387llu, 69596724709081llu,
- 87686378464759llu, 110477914016779llu, 139193449418173llu,
- 175372756929481llu, 220955828033581llu, 278386898836457llu,
- 350745513859007llu, 441911656067171llu, 556773797672909llu,
- 701491027718027llu, 883823312134381llu, 1113547595345903llu,
- 1402982055436147llu, 1767646624268779llu, 2227095190691797llu,
- 2805964110872297llu, 3535293248537579llu, 4454190381383713llu,
- 5611928221744609llu, 7070586497075177llu, 8908380762767489llu,
- 11223856443489329llu, 14141172994150357llu, 17816761525534927llu,
- 22447712886978529llu, 28282345988300791llu, 35633523051069991llu,
- 44895425773957261llu, 56564691976601587llu, 71267046102139967llu,
- 89790851547914507llu, 113129383953203213llu, 142534092204280003llu,
- 179581703095829107llu, 226258767906406483llu, 285068184408560057llu,
- 359163406191658253llu, 452517535812813007llu, 570136368817120201llu,
- 718326812383316683llu, 905035071625626043llu, 1140272737634240411llu,
- 1436653624766633509llu, 1810070143251252131llu, 2280545475268481167llu,
- 2873307249533267101llu, 3620140286502504283llu, 4561090950536962147llu,
- 5746614499066534157llu, 7240280573005008577llu, 9122181901073924329llu,
- 11493228998133068689llu, 14480561146010017169llu, 18446744073709551557llu
- };
- static constexpr size_t (* const mod_functions[])(size_t) =
- {
- &mod0, &mod2, &mod3, &mod5, &mod7, &mod11, &mod13, &mod17, &mod23, &mod29, &mod37,
- &mod47, &mod59, &mod73, &mod97, &mod127, &mod151, &mod197, &mod251, &mod313, &mod397,
- &mod499, &mod631, &mod797, &mod1009, &mod1259, &mod1597, &mod2011, &mod2539, &mod3203,
- &mod4027, &mod5087, &mod6421, &mod8089, &mod10193, &mod12853, &mod16193, &mod20399,
- &mod25717, &mod32401, &mod40823, &mod51437, &mod64811, &mod81649, &mod102877,
- &mod129607, &mod163307, &mod205759, &mod259229, &mod326617, &mod411527, &mod518509,
- &mod653267, &mod823117, &mod1037059, &mod1306601, &mod1646237, &mod2074129,
- &mod2613229, &mod3292489, &mod4148279, &mod5226491, &mod6584983, &mod8296553,
- &mod10453007, &mod13169977, &mod16593127, &mod20906033, &mod26339969, &mod33186281,
- &mod41812097, &mod52679969, &mod66372617, &mod83624237, &mod105359939, &mod132745199,
- &mod167248483, &mod210719881, &mod265490441, &mod334496971, &mod421439783,
- &mod530980861, &mod668993977, &mod842879579, &mod1061961721, &mod1337987929,
- &mod1685759167, &mod2123923447, &mod2675975881, &mod3371518343, &mod4247846927,
- &mod5351951779, &mod6743036717, &mod8495693897, &mod10703903591, &mod13486073473,
- &mod16991387857, &mod21407807219, &mod26972146961, &mod33982775741, &mod42815614441,
- &mod53944293929, &mod67965551447, &mod85631228929, &mod107888587883, &mod135931102921,
- &mod171262457903, &mod215777175787, &mod271862205833, &mod342524915839,
- &mod431554351609, &mod543724411781, &mod685049831731, &mod863108703229,
- &mod1087448823553, &mod1370099663459, &mod1726217406467, &mod2174897647073,
- &mod2740199326961, &mod3452434812973, &mod4349795294267, &mod5480398654009,
- &mod6904869625999, &mod8699590588571, &mod10960797308051, &mod13809739252051,
- &mod17399181177241, &mod21921594616111, &mod27619478504183, &mod34798362354533,
- &mod43843189232363, &mod55238957008387, &mod69596724709081, &mod87686378464759,
- &mod110477914016779, &mod139193449418173, &mod175372756929481, &mod220955828033581,
- &mod278386898836457, &mod350745513859007, &mod441911656067171, &mod556773797672909,
- &mod701491027718027, &mod883823312134381, &mod1113547595345903, &mod1402982055436147,
- &mod1767646624268779, &mod2227095190691797, &mod2805964110872297, &mod3535293248537579,
- &mod4454190381383713, &mod5611928221744609, &mod7070586497075177, &mod8908380762767489,
- &mod11223856443489329, &mod14141172994150357, &mod17816761525534927,
- &mod22447712886978529, &mod28282345988300791, &mod35633523051069991,
- &mod44895425773957261, &mod56564691976601587, &mod71267046102139967,
- &mod89790851547914507, &mod113129383953203213, &mod142534092204280003,
- &mod179581703095829107, &mod226258767906406483, &mod285068184408560057,
- &mod359163406191658253, &mod452517535812813007, &mod570136368817120201,
- &mod718326812383316683, &mod905035071625626043, &mod1140272737634240411,
- &mod1436653624766633509, &mod1810070143251252131, &mod2280545475268481167,
- &mod2873307249533267101, &mod3620140286502504283, &mod4561090950536962147,
- &mod5746614499066534157, &mod7240280573005008577, &mod9122181901073924329,
- &mod11493228998133068689, &mod14480561146010017169, &mod18446744073709551557
- };
- const size_t * found = std::lower_bound(std::begin(prime_list), std::end(prime_list) - 1, size);
- size = *found;
- return mod_functions[1 + found - prime_list];
- }
- void commit(mod_function new_mod_function)
- {
- current_mod_function = new_mod_function;
- }
- void reset()
- {
- current_mod_function = &mod0;
- }
-
- size_t index_for_hash(size_t hash, size_t /*num_slots_minus_one*/) const
- {
- return current_mod_function(hash);
- }
- size_t keep_in_range(size_t index, size_t num_slots_minus_one) const
- {
- return index > num_slots_minus_one ? current_mod_function(index) : index;
- }
-
-private:
- mod_function current_mod_function = &mod0;
-};
-
-struct power_of_two_hash_policy
-{
- size_t index_for_hash(size_t hash, size_t num_slots_minus_one) const
- {
- return hash & num_slots_minus_one;
- }
- size_t keep_in_range(size_t index, size_t num_slots_minus_one) const
- {
- return index_for_hash(index, num_slots_minus_one);
- }
- int8_t next_size_over(size_t & size) const
- {
- size = detailv3::next_power_of_two(size);
- return 0;
- }
- void commit(int8_t)
- {
- }
- void reset()
- {
- }
-
-};
-
-struct fibonacci_hash_policy
-{
- size_t index_for_hash(size_t hash, size_t /*num_slots_minus_one*/) const
- {
- return (11400714819323198485ull * hash) >> shift;
- }
- size_t keep_in_range(size_t index, size_t num_slots_minus_one) const
- {
- return index & num_slots_minus_one;
- }
-
- int8_t next_size_over(size_t & size) const
- {
- size = std::max(size_t(2), detailv3::next_power_of_two(size));
- return 64 - detailv3::log2(size);
- }
- void commit(int8_t shift)
- {
- this->shift = shift;
- }
- void reset()
- {
- shift = 63;
- }
-
-private:
- int8_t shift = 63;
-};
-
-template<typename K, typename V, typename H = std::hash<K>, typename E = std::equal_to<K>, typename A = std::allocator<std::pair<K, V> > >
-class flat_hash_map
- : public detailv3::sherwood_v3_table
- <
- std::pair<K, V>,
- K,
- H,
- detailv3::KeyOrValueHasher<K, std::pair<K, V>, H>,
- E,
- detailv3::KeyOrValueEquality<K, std::pair<K, V>, E>,
- A,
- typename std::allocator_traits<A>::template rebind_alloc<detailv3::sherwood_v3_entry<std::pair<K, V>>>
- >
-{
- using Table = detailv3::sherwood_v3_table
- <
- std::pair<K, V>,
- K,
- H,
- detailv3::KeyOrValueHasher<K, std::pair<K, V>, H>,
- E,
- detailv3::KeyOrValueEquality<K, std::pair<K, V>, E>,
- A,
- typename std::allocator_traits<A>::template rebind_alloc<detailv3::sherwood_v3_entry<std::pair<K, V>>>
- >;
-public:
-
- using key_type = K;
- using mapped_type = V;
-
- using Table::Table;
- flat_hash_map()
- {
- }
-
- inline V & operator[](const K & key)
- {
- return emplace(key, convertible_to_value()).first->second;
- }
- inline V & operator[](K && key)
- {
- return emplace(std::move(key), convertible_to_value()).first->second;
- }
- V & at(const K & key)
- {
- auto found = this->find(key);
- if (found == this->end())
- throw std::out_of_range("Argument passed to at() was not in the map.");
- return found->second;
- }
- const V & at(const K & key) const
- {
- auto found = this->find(key);
- if (found == this->end())
- throw std::out_of_range("Argument passed to at() was not in the map.");
- return found->second;
- }
-
- using Table::emplace;
- std::pair<typename Table::iterator, bool> emplace()
- {
- return emplace(key_type(), convertible_to_value());
- }
- template<typename M>
- std::pair<typename Table::iterator, bool> insert_or_assign(const key_type & key, M && m)
- {
- auto emplace_result = emplace(key, std::forward<M>(m));
- if (!emplace_result.second)
- emplace_result.first->second = std::forward<M>(m);
- return emplace_result;
- }
- template<typename M>
- std::pair<typename Table::iterator, bool> insert_or_assign(key_type && key, M && m)
- {
- auto emplace_result = emplace(std::move(key), std::forward<M>(m));
- if (!emplace_result.second)
- emplace_result.first->second = std::forward<M>(m);
- return emplace_result;
- }
- template<typename M>
- typename Table::iterator insert_or_assign(typename Table::const_iterator, const key_type & key, M && m)
- {
- return insert_or_assign(key, std::forward<M>(m)).first;
- }
- template<typename M>
- typename Table::iterator insert_or_assign(typename Table::const_iterator, key_type && key, M && m)
- {
- return insert_or_assign(std::move(key), std::forward<M>(m)).first;
- }
-
- friend bool operator==(const flat_hash_map & lhs, const flat_hash_map & rhs)
- {
- if (lhs.size() != rhs.size())
- return false;
- for (const typename Table::value_type & value : lhs)
- {
- auto found = rhs.find(value.first);
- if (found == rhs.end())
- return false;
- else if (value.second != found->second)
- return false;
- }
- return true;
- }
- friend bool operator!=(const flat_hash_map & lhs, const flat_hash_map & rhs)
- {
- return !(lhs == rhs);
- }
-
-private:
- struct convertible_to_value
- {
- operator V() const
- {
- return V();
- }
- };
-};
-
-template<typename T, typename H = std::hash<T>, typename E = std::equal_to<T>, typename A = std::allocator<T> >
-class flat_hash_set
- : public detailv3::sherwood_v3_table
- <
- T,
- T,
- H,
- detailv3::functor_storage<size_t, H>,
- E,
- detailv3::functor_storage<bool, E>,
- A,
- typename std::allocator_traits<A>::template rebind_alloc<detailv3::sherwood_v3_entry<T>>
- >
-{
- using Table = detailv3::sherwood_v3_table
- <
- T,
- T,
- H,
- detailv3::functor_storage<size_t, H>,
- E,
- detailv3::functor_storage<bool, E>,
- A,
- typename std::allocator_traits<A>::template rebind_alloc<detailv3::sherwood_v3_entry<T>>
- >;
-public:
-
- using key_type = T;
-
- using Table::Table;
- flat_hash_set()
- {
- }
-
- template<typename... Args>
- std::pair<typename Table::iterator, bool> emplace(Args &&... args)
- {
- return Table::emplace(T(std::forward<Args>(args)...));
- }
- std::pair<typename Table::iterator, bool> emplace(const key_type & arg)
- {
- return Table::emplace(arg);
- }
- std::pair<typename Table::iterator, bool> emplace(key_type & arg)
- {
- return Table::emplace(arg);
- }
- std::pair<typename Table::iterator, bool> emplace(const key_type && arg)
- {
- return Table::emplace(std::move(arg));
- }
- std::pair<typename Table::iterator, bool> emplace(key_type && arg)
- {
- return Table::emplace(std::move(arg));
- }
-
- friend bool operator==(const flat_hash_set & lhs, const flat_hash_set & rhs)
- {
- if (lhs.size() != rhs.size())
- return false;
- for (const T & value : lhs)
- {
- if (rhs.find(value) == rhs.end())
- return false;
- }
- return true;
- }
- friend bool operator!=(const flat_hash_set & lhs, const flat_hash_set & rhs)
- {
- return !(lhs == rhs);
- }
-};
-
-
-template<typename T>
-struct power_of_two_std_hash : std::hash<T>
-{
- typedef ska::power_of_two_hash_policy hash_policy;
-};
-
-} // end namespace ska \ No newline at end of file
+// Copyright Malte Skarupke 2017.
+// Distributed under the Boost Software License, Version 1.0.
+// (See http://www.boost.org/LICENSE_1_0.txt)
+
+#pragma once
+
+#include <cstdint>
+#include <cstddef>
+#include <functional>
+#include <cmath>
+#include <algorithm>
+#include <iterator>
+#include <utility>
+#include <type_traits>
+
+#ifdef _MSC_VER
+#define SKA_NOINLINE(...) __declspec(noinline) __VA_ARGS__
+#else
+#define SKA_NOINLINE(...) __VA_ARGS__ __attribute__((noinline))
+#endif
+
+namespace ska
+{
+struct prime_number_hash_policy;
+struct power_of_two_hash_policy;
+struct fibonacci_hash_policy;
+
+namespace detailv3
+{
+template<typename Result, typename Functor>
+struct functor_storage : Functor
+{
+ functor_storage() = default;
+ functor_storage(const Functor & functor)
+ : Functor(functor)
+ {
+ }
+ template<typename... Args>
+ Result operator()(Args &&... args)
+ {
+ return static_cast<Functor &>(*this)(std::forward<Args>(args)...);
+ }
+ template<typename... Args>
+ Result operator()(Args &&... args) const
+ {
+ return static_cast<const Functor &>(*this)(std::forward<Args>(args)...);
+ }
+};
+template<typename Result, typename... Args>
+struct functor_storage<Result, Result (*)(Args...)>
+{
+ typedef Result (*function_ptr)(Args...);
+ function_ptr function;
+ functor_storage(function_ptr function)
+ : function(function)
+ {
+ }
+ Result operator()(Args... args) const
+ {
+ return function(std::forward<Args>(args)...);
+ }
+ operator function_ptr &()
+ {
+ return function;
+ }
+ operator const function_ptr &()
+ {
+ return function;
+ }
+};
+template<typename key_type, typename value_type, typename hasher>
+struct KeyOrValueHasher : functor_storage<size_t, hasher>
+{
+ typedef functor_storage<size_t, hasher> hasher_storage;
+ KeyOrValueHasher() = default;
+ KeyOrValueHasher(const hasher & hash)
+ : hasher_storage(hash)
+ {
+ }
+ size_t operator()(const key_type & key)
+ {
+ return static_cast<hasher_storage &>(*this)(key);
+ }
+ size_t operator()(const key_type & key) const
+ {
+ return static_cast<const hasher_storage &>(*this)(key);
+ }
+ size_t operator()(const value_type & value)
+ {
+ return static_cast<hasher_storage &>(*this)(value.first);
+ }
+ size_t operator()(const value_type & value) const
+ {
+ return static_cast<const hasher_storage &>(*this)(value.first);
+ }
+ template<typename F, typename S>
+ size_t operator()(const std::pair<F, S> & value)
+ {
+ return static_cast<hasher_storage &>(*this)(value.first);
+ }
+ template<typename F, typename S>
+ size_t operator()(const std::pair<F, S> & value) const
+ {
+ return static_cast<const hasher_storage &>(*this)(value.first);
+ }
+};
+template<typename key_type, typename value_type, typename key_equal>
+struct KeyOrValueEquality : functor_storage<bool, key_equal>
+{
+ typedef functor_storage<bool, key_equal> equality_storage;
+ KeyOrValueEquality() = default;
+ KeyOrValueEquality(const key_equal & equality)
+ : equality_storage(equality)
+ {
+ }
+ bool operator()(const key_type & lhs, const key_type & rhs)
+ {
+ return static_cast<equality_storage &>(*this)(lhs, rhs);
+ }
+ bool operator()(const key_type & lhs, const value_type & rhs)
+ {
+ return static_cast<equality_storage &>(*this)(lhs, rhs.first);
+ }
+ bool operator()(const value_type & lhs, const key_type & rhs)
+ {
+ return static_cast<equality_storage &>(*this)(lhs.first, rhs);
+ }
+ bool operator()(const value_type & lhs, const value_type & rhs)
+ {
+ return static_cast<equality_storage &>(*this)(lhs.first, rhs.first);
+ }
+ template<typename F, typename S>
+ bool operator()(const key_type & lhs, const std::pair<F, S> & rhs)
+ {
+ return static_cast<equality_storage &>(*this)(lhs, rhs.first);
+ }
+ template<typename F, typename S>
+ bool operator()(const std::pair<F, S> & lhs, const key_type & rhs)
+ {
+ return static_cast<equality_storage &>(*this)(lhs.first, rhs);
+ }
+ template<typename F, typename S>
+ bool operator()(const value_type & lhs, const std::pair<F, S> & rhs)
+ {
+ return static_cast<equality_storage &>(*this)(lhs.first, rhs.first);
+ }
+ template<typename F, typename S>
+ bool operator()(const std::pair<F, S> & lhs, const value_type & rhs)
+ {
+ return static_cast<equality_storage &>(*this)(lhs.first, rhs.first);
+ }
+ template<typename FL, typename SL, typename FR, typename SR>
+ bool operator()(const std::pair<FL, SL> & lhs, const std::pair<FR, SR> & rhs)
+ {
+ return static_cast<equality_storage &>(*this)(lhs.first, rhs.first);
+ }
+};
+static constexpr int8_t min_lookups = 4;
+template<typename T>
+struct sherwood_v3_entry
+{
+ sherwood_v3_entry()
+ {
+ }
+ sherwood_v3_entry(int8_t distance_from_desired)
+ : distance_from_desired(distance_from_desired)
+ {
+ }
+ ~sherwood_v3_entry()
+ {
+ }
+ static sherwood_v3_entry * empty_default_table()
+ {
+ static sherwood_v3_entry result[min_lookups] = { {}, {}, {}, {special_end_value} };
+ return result;
+ }
+
+ bool has_value() const
+ {
+ return distance_from_desired >= 0;
+ }
+ bool is_empty() const
+ {
+ return distance_from_desired < 0;
+ }
+ bool is_at_desired_position() const
+ {
+ return distance_from_desired <= 0;
+ }
+ template<typename... Args>
+ void emplace(int8_t distance, Args &&... args)
+ {
+ new (std::addressof(value)) T(std::forward<Args>(args)...);
+ distance_from_desired = distance;
+ }
+
+ void destroy_value()
+ {
+ value.~T();
+ distance_from_desired = -1;
+ }
+
+ int8_t distance_from_desired = -1;
+ static constexpr int8_t special_end_value = 0;
+ union { T value; };
+};
+
+inline int8_t log2(size_t value)
+{
+ static constexpr int8_t table[64] =
+ {
+ 63, 0, 58, 1, 59, 47, 53, 2,
+ 60, 39, 48, 27, 54, 33, 42, 3,
+ 61, 51, 37, 40, 49, 18, 28, 20,
+ 55, 30, 34, 11, 43, 14, 22, 4,
+ 62, 57, 46, 52, 38, 26, 32, 41,
+ 50, 36, 17, 19, 29, 10, 13, 21,
+ 56, 45, 25, 31, 35, 16, 9, 12,
+ 44, 24, 15, 8, 23, 7, 6, 5
+ };
+ value |= value >> 1;
+ value |= value >> 2;
+ value |= value >> 4;
+ value |= value >> 8;
+ value |= value >> 16;
+ value |= value >> 32;
+ return table[((value - (value >> 1)) * 0x07EDD5E59A4E28C2) >> 58];
+}
+
+template<typename T, bool>
+struct AssignIfTrue
+{
+ void operator()(T & lhs, const T & rhs)
+ {
+ lhs = rhs;
+ }
+ void operator()(T & lhs, T && rhs)
+ {
+ lhs = std::move(rhs);
+ }
+};
+template<typename T>
+struct AssignIfTrue<T, false>
+{
+ void operator()(T &, const T &)
+ {
+ }
+ void operator()(T &, T &&)
+ {
+ }
+};
+
+inline size_t next_power_of_two(size_t i)
+{
+ --i;
+ i |= i >> 1;
+ i |= i >> 2;
+ i |= i >> 4;
+ i |= i >> 8;
+ i |= i >> 16;
+ i |= i >> 32;
+ ++i;
+ return i;
+}
+
+template<typename...> using void_t = void;
+
+template<typename T, typename = void>
+struct HashPolicySelector
+{
+ typedef fibonacci_hash_policy type;
+};
+template<typename T>
+struct HashPolicySelector<T, void_t<typename T::hash_policy>>
+{
+ typedef typename T::hash_policy type;
+};
+
+template<typename T, typename FindKey, typename ArgumentHash, typename Hasher, typename ArgumentEqual, typename Equal, typename ArgumentAlloc, typename EntryAlloc>
+class sherwood_v3_table : private EntryAlloc, private Hasher, private Equal
+{
+ using Entry = detailv3::sherwood_v3_entry<T>;
+ using AllocatorTraits = std::allocator_traits<EntryAlloc>;
+ using EntryPointer = typename AllocatorTraits::pointer;
+ struct convertible_to_iterator;
+
+public:
+
+ using value_type = T;
+ using size_type = size_t;
+ using difference_type = std::ptrdiff_t;
+ using hasher = ArgumentHash;
+ using key_equal = ArgumentEqual;
+ using allocator_type = EntryAlloc;
+ using reference = value_type &;
+ using const_reference = const value_type &;
+ using pointer = value_type *;
+ using const_pointer = const value_type *;
+
+ sherwood_v3_table()
+ {
+ }
+ explicit sherwood_v3_table(size_type bucket_count, const ArgumentHash & hash = ArgumentHash(), const ArgumentEqual & equal = ArgumentEqual(), const ArgumentAlloc & alloc = ArgumentAlloc())
+ : EntryAlloc(alloc), Hasher(hash), Equal(equal)
+ {
+ rehash(bucket_count);
+ }
+ sherwood_v3_table(size_type bucket_count, const ArgumentAlloc & alloc)
+ : sherwood_v3_table(bucket_count, ArgumentHash(), ArgumentEqual(), alloc)
+ {
+ }
+ sherwood_v3_table(size_type bucket_count, const ArgumentHash & hash, const ArgumentAlloc & alloc)
+ : sherwood_v3_table(bucket_count, hash, ArgumentEqual(), alloc)
+ {
+ }
+ explicit sherwood_v3_table(const ArgumentAlloc & alloc)
+ : EntryAlloc(alloc)
+ {
+ }
+ template<typename It>
+ sherwood_v3_table(It first, It last, size_type bucket_count = 0, const ArgumentHash & hash = ArgumentHash(), const ArgumentEqual & equal = ArgumentEqual(), const ArgumentAlloc & alloc = ArgumentAlloc())
+ : sherwood_v3_table(bucket_count, hash, equal, alloc)
+ {
+ insert(first, last);
+ }
+ template<typename It>
+ sherwood_v3_table(It first, It last, size_type bucket_count, const ArgumentAlloc & alloc)
+ : sherwood_v3_table(first, last, bucket_count, ArgumentHash(), ArgumentEqual(), alloc)
+ {
+ }
+ template<typename It>
+ sherwood_v3_table(It first, It last, size_type bucket_count, const ArgumentHash & hash, const ArgumentAlloc & alloc)
+ : sherwood_v3_table(first, last, bucket_count, hash, ArgumentEqual(), alloc)
+ {
+ }
+ sherwood_v3_table(std::initializer_list<T> il, size_type bucket_count = 0, const ArgumentHash & hash = ArgumentHash(), const ArgumentEqual & equal = ArgumentEqual(), const ArgumentAlloc & alloc = ArgumentAlloc())
+ : sherwood_v3_table(bucket_count, hash, equal, alloc)
+ {
+ if (bucket_count == 0)
+ rehash(il.size());
+ insert(il.begin(), il.end());
+ }
+ sherwood_v3_table(std::initializer_list<T> il, size_type bucket_count, const ArgumentAlloc & alloc)
+ : sherwood_v3_table(il, bucket_count, ArgumentHash(), ArgumentEqual(), alloc)
+ {
+ }
+ sherwood_v3_table(std::initializer_list<T> il, size_type bucket_count, const ArgumentHash & hash, const ArgumentAlloc & alloc)
+ : sherwood_v3_table(il, bucket_count, hash, ArgumentEqual(), alloc)
+ {
+ }
+ sherwood_v3_table(const sherwood_v3_table & other)
+ : sherwood_v3_table(other, AllocatorTraits::select_on_container_copy_construction(other.get_allocator()))
+ {
+ }
+ sherwood_v3_table(const sherwood_v3_table & other, const ArgumentAlloc & alloc)
+ : EntryAlloc(alloc), Hasher(other), Equal(other), _max_load_factor(other._max_load_factor)
+ {
+ rehash_for_other_container(other);
+ try
+ {
+ insert(other.begin(), other.end());
+ }
+ catch(...)
+ {
+ clear();
+ deallocate_data(entries, num_slots_minus_one, max_lookups);
+ throw;
+ }
+ }
+ sherwood_v3_table(sherwood_v3_table && other) noexcept
+ : EntryAlloc(std::move(other)), Hasher(std::move(other)), Equal(std::move(other))
+ {
+ swap_pointers(other);
+ }
+ sherwood_v3_table(sherwood_v3_table && other, const ArgumentAlloc & alloc) noexcept
+ : EntryAlloc(alloc), Hasher(std::move(other)), Equal(std::move(other))
+ {
+ swap_pointers(other);
+ }
+ sherwood_v3_table & operator=(const sherwood_v3_table & other)
+ {
+ if (this == std::addressof(other))
+ return *this;
+
+ clear();
+ if (AllocatorTraits::propagate_on_container_copy_assignment::value)
+ {
+ if (static_cast<EntryAlloc &>(*this) != static_cast<const EntryAlloc &>(other))
+ {
+ reset_to_empty_state();
+ }
+ AssignIfTrue<EntryAlloc, AllocatorTraits::propagate_on_container_copy_assignment::value>()(*this, other);
+ }
+ _max_load_factor = other._max_load_factor;
+ static_cast<Hasher &>(*this) = other;
+ static_cast<Equal &>(*this) = other;
+ rehash_for_other_container(other);
+ insert(other.begin(), other.end());
+ return *this;
+ }
+ sherwood_v3_table & operator=(sherwood_v3_table && other) noexcept
+ {
+ if (this == std::addressof(other))
+ return *this;
+ else if (AllocatorTraits::propagate_on_container_move_assignment::value)
+ {
+ clear();
+ reset_to_empty_state();
+ AssignIfTrue<EntryAlloc, AllocatorTraits::propagate_on_container_move_assignment::value>()(*this, std::move(other));
+ swap_pointers(other);
+ }
+ else if (static_cast<EntryAlloc &>(*this) == static_cast<EntryAlloc &>(other))
+ {
+ swap_pointers(other);
+ }
+ else
+ {
+ clear();
+ _max_load_factor = other._max_load_factor;
+ rehash_for_other_container(other);
+ for (T & elem : other)
+ emplace(std::move(elem));
+ other.clear();
+ }
+ static_cast<Hasher &>(*this) = std::move(other);
+ static_cast<Equal &>(*this) = std::move(other);
+ return *this;
+ }
+ ~sherwood_v3_table()
+ {
+ clear();
+ deallocate_data(entries, num_slots_minus_one, max_lookups);
+ }
+
+ const allocator_type & get_allocator() const
+ {
+ return static_cast<const allocator_type &>(*this);
+ }
+ const ArgumentEqual & key_eq() const
+ {
+ return static_cast<const ArgumentEqual &>(*this);
+ }
+ const ArgumentHash & hash_function() const
+ {
+ return static_cast<const ArgumentHash &>(*this);
+ }
+
+ template<typename ValueType>
+ struct templated_iterator
+ {
+ templated_iterator() = default;
+ templated_iterator(EntryPointer current)
+ : current(current)
+ {
+ }
+ EntryPointer current = EntryPointer();
+
+ using iterator_category = std::forward_iterator_tag;
+ using value_type = ValueType;
+ using difference_type = ptrdiff_t;
+ using pointer = ValueType *;
+ using reference = ValueType &;
+
+ friend bool operator==(const templated_iterator & lhs, const templated_iterator & rhs)
+ {
+ return lhs.current == rhs.current;
+ }
+ friend bool operator!=(const templated_iterator & lhs, const templated_iterator & rhs)
+ {
+ return !(lhs == rhs);
+ }
+
+ templated_iterator & operator++()
+ {
+ do
+ {
+ ++current;
+ }
+ while(current->is_empty());
+ return *this;
+ }
+ templated_iterator operator++(int)
+ {
+ templated_iterator copy(*this);
+ ++*this;
+ return copy;
+ }
+
+ ValueType & operator*() const
+ {
+ return current->value;
+ }
+ ValueType * operator->() const
+ {
+ return std::addressof(current->value);
+ }
+
+ operator templated_iterator<const value_type>() const
+ {
+ return { current };
+ }
+ };
+ using iterator = templated_iterator<value_type>;
+ using const_iterator = templated_iterator<const value_type>;
+
+ iterator begin()
+ {
+ for (EntryPointer it = entries;; ++it)
+ {
+ if (it->has_value())
+ return { it };
+ }
+ }
+ const_iterator begin() const
+ {
+ for (EntryPointer it = entries;; ++it)
+ {
+ if (it->has_value())
+ return { it };
+ }
+ }
+ const_iterator cbegin() const
+ {
+ return begin();
+ }
+ iterator end()
+ {
+ return { entries + static_cast<ptrdiff_t>(num_slots_minus_one + max_lookups) };
+ }
+ const_iterator end() const
+ {
+ return { entries + static_cast<ptrdiff_t>(num_slots_minus_one + max_lookups) };
+ }
+ const_iterator cend() const
+ {
+ return end();
+ }
+
+ iterator find(const FindKey & key)
+ {
+ size_t index = hash_policy.index_for_hash(hash_object(key), num_slots_minus_one);
+ EntryPointer it = entries + ptrdiff_t(index);
+ for (int8_t distance = 0; it->distance_from_desired >= distance; ++distance, ++it)
+ {
+ if (compares_equal(key, it->value))
+ return { it };
+ }
+ return end();
+ }
+ const_iterator find(const FindKey & key) const
+ {
+ return const_cast<sherwood_v3_table *>(this)->find(key);
+ }
+ size_t count(const FindKey & key) const
+ {
+ return find(key) == end() ? 0 : 1;
+ }
+ std::pair<iterator, iterator> equal_range(const FindKey & key)
+ {
+ iterator found = find(key);
+ if (found == end())
+ return { found, found };
+ else
+ return { found, std::next(found) };
+ }
+ std::pair<const_iterator, const_iterator> equal_range(const FindKey & key) const
+ {
+ const_iterator found = find(key);
+ if (found == end())
+ return { found, found };
+ else
+ return { found, std::next(found) };
+ }
+
+ template<typename Key, typename... Args>
+ std::pair<iterator, bool> emplace(Key && key, Args &&... args)
+ {
+ size_t index = hash_policy.index_for_hash(hash_object(key), num_slots_minus_one);
+ EntryPointer current_entry = entries + ptrdiff_t(index);
+ int8_t distance_from_desired = 0;
+ for (; current_entry->distance_from_desired >= distance_from_desired; ++current_entry, ++distance_from_desired)
+ {
+ if (compares_equal(key, current_entry->value))
+ return { { current_entry }, false };
+ }
+ return emplace_new_key(distance_from_desired, current_entry, std::forward<Key>(key), std::forward<Args>(args)...);
+ }
+
+ std::pair<iterator, bool> insert(const value_type & value)
+ {
+ return emplace(value);
+ }
+ std::pair<iterator, bool> insert(value_type && value)
+ {
+ return emplace(std::move(value));
+ }
+ template<typename... Args>
+ iterator emplace_hint(const_iterator, Args &&... args)
+ {
+ return emplace(std::forward<Args>(args)...).first;
+ }
+ iterator insert(const_iterator, const value_type & value)
+ {
+ return emplace(value).first;
+ }
+ iterator insert(const_iterator, value_type && value)
+ {
+ return emplace(std::move(value)).first;
+ }
+
+ template<typename It>
+ void insert(It begin, It end)
+ {
+ for (; begin != end; ++begin)
+ {
+ emplace(*begin);
+ }
+ }
+ void insert(std::initializer_list<value_type> il)
+ {
+ insert(il.begin(), il.end());
+ }
+
+ void rehash(size_t num_buckets)
+ {
+ num_buckets = std::max(num_buckets, static_cast<size_t>(std::ceil(num_elements / static_cast<double>(_max_load_factor))));
+ if (num_buckets == 0)
+ {
+ reset_to_empty_state();
+ return;
+ }
+ auto new_prime_index = hash_policy.next_size_over(num_buckets);
+ if (num_buckets == bucket_count())
+ return;
+ int8_t new_max_lookups = compute_max_lookups(num_buckets);
+ EntryPointer new_buckets(AllocatorTraits::allocate(*this, num_buckets + new_max_lookups));
+ EntryPointer special_end_item = new_buckets + static_cast<ptrdiff_t>(num_buckets + new_max_lookups - 1);
+ for (EntryPointer it = new_buckets; it != special_end_item; ++it)
+ it->distance_from_desired = -1;
+ special_end_item->distance_from_desired = Entry::special_end_value;
+ std::swap(entries, new_buckets);
+ std::swap(num_slots_minus_one, num_buckets);
+ --num_slots_minus_one;
+ hash_policy.commit(new_prime_index);
+ int8_t old_max_lookups = max_lookups;
+ max_lookups = new_max_lookups;
+ num_elements = 0;
+ for (EntryPointer it = new_buckets, end = it + static_cast<ptrdiff_t>(num_buckets + old_max_lookups); it != end; ++it)
+ {
+ if (it->has_value())
+ {
+ emplace(std::move(it->value));
+ it->destroy_value();
+ }
+ }
+ deallocate_data(new_buckets, num_buckets, old_max_lookups);
+ }
+
+ void reserve(size_t num_elements)
+ {
+ size_t required_buckets = num_buckets_for_reserve(num_elements);
+ if (required_buckets > bucket_count())
+ rehash(required_buckets);
+ }
+
+ // the return value is a type that can be converted to an iterator
+ // the reason for doing this is that it's not free to find the
+ // iterator pointing at the next element. if you care about the
+ // next iterator, turn the return value into an iterator
+ convertible_to_iterator erase(const_iterator to_erase)
+ {
+ EntryPointer current = to_erase.current;
+ current->destroy_value();
+ --num_elements;
+ for (EntryPointer next = current + ptrdiff_t(1); !next->is_at_desired_position(); ++current, ++next)
+ {
+ current->emplace(next->distance_from_desired - 1, std::move(next->value));
+ next->destroy_value();
+ }
+ return { to_erase.current };
+ }
+
+ iterator erase(const_iterator begin_it, const_iterator end_it)
+ {
+ if (begin_it == end_it)
+ return { begin_it.current };
+ for (EntryPointer it = begin_it.current, end = end_it.current; it != end; ++it)
+ {
+ if (it->has_value())
+ {
+ it->destroy_value();
+ --num_elements;
+ }
+ }
+ if (end_it == this->end())
+ return this->end();
+ ptrdiff_t num_to_move = std::min(static_cast<ptrdiff_t>(end_it.current->distance_from_desired), end_it.current - begin_it.current);
+ EntryPointer to_return = end_it.current - num_to_move;
+ for (EntryPointer it = end_it.current; !it->is_at_desired_position();)
+ {
+ EntryPointer target = it - num_to_move;
+ target->emplace(it->distance_from_desired - num_to_move, std::move(it->value));
+ it->destroy_value();
+ ++it;
+ num_to_move = std::min(static_cast<ptrdiff_t>(it->distance_from_desired), num_to_move);
+ }
+ return { to_return };
+ }
+
+ size_t erase(const FindKey & key)
+ {
+ auto found = find(key);
+ if (found == end())
+ return 0;
+ else
+ {
+ erase(found);
+ return 1;
+ }
+ }
+
+ void clear()
+ {
+ for (EntryPointer it = entries, end = it + static_cast<ptrdiff_t>(num_slots_minus_one + max_lookups); it != end; ++it)
+ {
+ if (it->has_value())
+ it->destroy_value();
+ }
+ num_elements = 0;
+ }
+
+ void shrink_to_fit()
+ {
+ rehash_for_other_container(*this);
+ }
+
+ void swap(sherwood_v3_table & other)
+ {
+ using std::swap;
+ swap_pointers(other);
+ swap(static_cast<ArgumentHash &>(*this), static_cast<ArgumentHash &>(other));
+ swap(static_cast<ArgumentEqual &>(*this), static_cast<ArgumentEqual &>(other));
+ if (AllocatorTraits::propagate_on_container_swap::value)
+ swap(static_cast<EntryAlloc &>(*this), static_cast<EntryAlloc &>(other));
+ }
+
+ size_t size() const
+ {
+ return num_elements;
+ }
+ size_t max_size() const
+ {
+ return (AllocatorTraits::max_size(*this)) / sizeof(Entry);
+ }
+ size_t bucket_count() const
+ {
+ return num_slots_minus_one ? num_slots_minus_one + 1 : 0;
+ }
+ size_type max_bucket_count() const
+ {
+ return (AllocatorTraits::max_size(*this) - min_lookups) / sizeof(Entry);
+ }
+ size_t bucket(const FindKey & key) const
+ {
+ return hash_policy.index_for_hash(hash_object(key), num_slots_minus_one);
+ }
+ float load_factor() const
+ {
+ size_t buckets = bucket_count();
+ if (buckets)
+ return static_cast<float>(num_elements) / bucket_count();
+ else
+ return 0;
+ }
+ void max_load_factor(float value)
+ {
+ _max_load_factor = value;
+ }
+ float max_load_factor() const
+ {
+ return _max_load_factor;
+ }
+
+ bool empty() const
+ {
+ return num_elements == 0;
+ }
+
+private:
+ EntryPointer entries = Entry::empty_default_table();
+ size_t num_slots_minus_one = 0;
+ typename HashPolicySelector<ArgumentHash>::type hash_policy;
+ int8_t max_lookups = detailv3::min_lookups - 1;
+ float _max_load_factor = 0.5f;
+ size_t num_elements = 0;
+
+ static int8_t compute_max_lookups(size_t num_buckets)
+ {
+ int8_t desired = detailv3::log2(num_buckets);
+ return std::max(detailv3::min_lookups, desired);
+ }
+
+ size_t num_buckets_for_reserve(size_t num_elements) const
+ {
+ return static_cast<size_t>(std::ceil(num_elements / std::min(0.5, static_cast<double>(_max_load_factor))));
+ }
+ void rehash_for_other_container(const sherwood_v3_table & other)
+ {
+ rehash(std::min(num_buckets_for_reserve(other.size()), other.bucket_count()));
+ }
+
+ void swap_pointers(sherwood_v3_table & other)
+ {
+ using std::swap;
+ swap(hash_policy, other.hash_policy);
+ swap(entries, other.entries);
+ swap(num_slots_minus_one, other.num_slots_minus_one);
+ swap(num_elements, other.num_elements);
+ swap(max_lookups, other.max_lookups);
+ swap(_max_load_factor, other._max_load_factor);
+ }
+
+ template<typename Key, typename... Args>
+ SKA_NOINLINE(std::pair<iterator, bool>) emplace_new_key(int8_t distance_from_desired, EntryPointer current_entry, Key && key, Args &&... args)
+ {
+ using std::swap;
+ if (num_slots_minus_one == 0 || distance_from_desired == max_lookups || num_elements + 1 > (num_slots_minus_one + 1) * static_cast<double>(_max_load_factor))
+ {
+ grow();
+ return emplace(std::forward<Key>(key), std::forward<Args>(args)...);
+ }
+ else if (current_entry->is_empty())
+ {
+ current_entry->emplace(distance_from_desired, std::forward<Key>(key), std::forward<Args>(args)...);
+ ++num_elements;
+ return { { current_entry }, true };
+ }
+ value_type to_insert(std::forward<Key>(key), std::forward<Args>(args)...);
+ swap(distance_from_desired, current_entry->distance_from_desired);
+ swap(to_insert, current_entry->value);
+ iterator result = { current_entry };
+ for (++distance_from_desired, ++current_entry;; ++current_entry)
+ {
+ if (current_entry->is_empty())
+ {
+ current_entry->emplace(distance_from_desired, std::move(to_insert));
+ ++num_elements;
+ return { result, true };
+ }
+ else if (current_entry->distance_from_desired < distance_from_desired)
+ {
+ swap(distance_from_desired, current_entry->distance_from_desired);
+ swap(to_insert, current_entry->value);
+ ++distance_from_desired;
+ }
+ else
+ {
+ ++distance_from_desired;
+ if (distance_from_desired == max_lookups)
+ {
+ swap(to_insert, result.current->value);
+ grow();
+ return emplace(std::move(to_insert));
+ }
+ }
+ }
+ }
+
+ void grow()
+ {
+ rehash(std::max(size_t(4), 2 * bucket_count()));
+ }
+
+ void deallocate_data(EntryPointer begin, size_t num_slots_minus_one, int8_t max_lookups)
+ {
+ if (begin != Entry::empty_default_table())
+ {
+ AllocatorTraits::deallocate(*this, begin, num_slots_minus_one + max_lookups + 1);
+ }
+ }
+
+ void reset_to_empty_state()
+ {
+ deallocate_data(entries, num_slots_minus_one, max_lookups);
+ entries = Entry::empty_default_table();
+ num_slots_minus_one = 0;
+ hash_policy.reset();
+ max_lookups = detailv3::min_lookups - 1;
+ }
+
+ template<typename U>
+ size_t hash_object(const U & key)
+ {
+ return static_cast<Hasher &>(*this)(key);
+ }
+ template<typename U>
+ size_t hash_object(const U & key) const
+ {
+ return static_cast<const Hasher &>(*this)(key);
+ }
+ template<typename L, typename R>
+ bool compares_equal(const L & lhs, const R & rhs)
+ {
+ return static_cast<Equal &>(*this)(lhs, rhs);
+ }
+
+ struct convertible_to_iterator
+ {
+ EntryPointer it;
+
+ operator iterator()
+ {
+ if (it->has_value())
+ return { it };
+ else
+ return ++iterator{it};
+ }
+ operator const_iterator()
+ {
+ if (it->has_value())
+ return { it };
+ else
+ return ++const_iterator{it};
+ }
+ };
+
+};
+}
+
+struct prime_number_hash_policy
+{
+ static size_t mod0(size_t) { return 0llu; }
+ static size_t mod2(size_t hash) { return hash % 2llu; }
+ static size_t mod3(size_t hash) { return hash % 3llu; }
+ static size_t mod5(size_t hash) { return hash % 5llu; }
+ static size_t mod7(size_t hash) { return hash % 7llu; }
+ static size_t mod11(size_t hash) { return hash % 11llu; }
+ static size_t mod13(size_t hash) { return hash % 13llu; }
+ static size_t mod17(size_t hash) { return hash % 17llu; }
+ static size_t mod23(size_t hash) { return hash % 23llu; }
+ static size_t mod29(size_t hash) { return hash % 29llu; }
+ static size_t mod37(size_t hash) { return hash % 37llu; }
+ static size_t mod47(size_t hash) { return hash % 47llu; }
+ static size_t mod59(size_t hash) { return hash % 59llu; }
+ static size_t mod73(size_t hash) { return hash % 73llu; }
+ static size_t mod97(size_t hash) { return hash % 97llu; }
+ static size_t mod127(size_t hash) { return hash % 127llu; }
+ static size_t mod151(size_t hash) { return hash % 151llu; }
+ static size_t mod197(size_t hash) { return hash % 197llu; }
+ static size_t mod251(size_t hash) { return hash % 251llu; }
+ static size_t mod313(size_t hash) { return hash % 313llu; }
+ static size_t mod397(size_t hash) { return hash % 397llu; }
+ static size_t mod499(size_t hash) { return hash % 499llu; }
+ static size_t mod631(size_t hash) { return hash % 631llu; }
+ static size_t mod797(size_t hash) { return hash % 797llu; }
+ static size_t mod1009(size_t hash) { return hash % 1009llu; }
+ static size_t mod1259(size_t hash) { return hash % 1259llu; }
+ static size_t mod1597(size_t hash) { return hash % 1597llu; }
+ static size_t mod2011(size_t hash) { return hash % 2011llu; }
+ static size_t mod2539(size_t hash) { return hash % 2539llu; }
+ static size_t mod3203(size_t hash) { return hash % 3203llu; }
+ static size_t mod4027(size_t hash) { return hash % 4027llu; }
+ static size_t mod5087(size_t hash) { return hash % 5087llu; }
+ static size_t mod6421(size_t hash) { return hash % 6421llu; }
+ static size_t mod8089(size_t hash) { return hash % 8089llu; }
+ static size_t mod10193(size_t hash) { return hash % 10193llu; }
+ static size_t mod12853(size_t hash) { return hash % 12853llu; }
+ static size_t mod16193(size_t hash) { return hash % 16193llu; }
+ static size_t mod20399(size_t hash) { return hash % 20399llu; }
+ static size_t mod25717(size_t hash) { return hash % 25717llu; }
+ static size_t mod32401(size_t hash) { return hash % 32401llu; }
+ static size_t mod40823(size_t hash) { return hash % 40823llu; }
+ static size_t mod51437(size_t hash) { return hash % 51437llu; }
+ static size_t mod64811(size_t hash) { return hash % 64811llu; }
+ static size_t mod81649(size_t hash) { return hash % 81649llu; }
+ static size_t mod102877(size_t hash) { return hash % 102877llu; }
+ static size_t mod129607(size_t hash) { return hash % 129607llu; }
+ static size_t mod163307(size_t hash) { return hash % 163307llu; }
+ static size_t mod205759(size_t hash) { return hash % 205759llu; }
+ static size_t mod259229(size_t hash) { return hash % 259229llu; }
+ static size_t mod326617(size_t hash) { return hash % 326617llu; }
+ static size_t mod411527(size_t hash) { return hash % 411527llu; }
+ static size_t mod518509(size_t hash) { return hash % 518509llu; }
+ static size_t mod653267(size_t hash) { return hash % 653267llu; }
+ static size_t mod823117(size_t hash) { return hash % 823117llu; }
+ static size_t mod1037059(size_t hash) { return hash % 1037059llu; }
+ static size_t mod1306601(size_t hash) { return hash % 1306601llu; }
+ static size_t mod1646237(size_t hash) { return hash % 1646237llu; }
+ static size_t mod2074129(size_t hash) { return hash % 2074129llu; }
+ static size_t mod2613229(size_t hash) { return hash % 2613229llu; }
+ static size_t mod3292489(size_t hash) { return hash % 3292489llu; }
+ static size_t mod4148279(size_t hash) { return hash % 4148279llu; }
+ static size_t mod5226491(size_t hash) { return hash % 5226491llu; }
+ static size_t mod6584983(size_t hash) { return hash % 6584983llu; }
+ static size_t mod8296553(size_t hash) { return hash % 8296553llu; }
+ static size_t mod10453007(size_t hash) { return hash % 10453007llu; }
+ static size_t mod13169977(size_t hash) { return hash % 13169977llu; }
+ static size_t mod16593127(size_t hash) { return hash % 16593127llu; }
+ static size_t mod20906033(size_t hash) { return hash % 20906033llu; }
+ static size_t mod26339969(size_t hash) { return hash % 26339969llu; }
+ static size_t mod33186281(size_t hash) { return hash % 33186281llu; }
+ static size_t mod41812097(size_t hash) { return hash % 41812097llu; }
+ static size_t mod52679969(size_t hash) { return hash % 52679969llu; }
+ static size_t mod66372617(size_t hash) { return hash % 66372617llu; }
+ static size_t mod83624237(size_t hash) { return hash % 83624237llu; }
+ static size_t mod105359939(size_t hash) { return hash % 105359939llu; }
+ static size_t mod132745199(size_t hash) { return hash % 132745199llu; }
+ static size_t mod167248483(size_t hash) { return hash % 167248483llu; }
+ static size_t mod210719881(size_t hash) { return hash % 210719881llu; }
+ static size_t mod265490441(size_t hash) { return hash % 265490441llu; }
+ static size_t mod334496971(size_t hash) { return hash % 334496971llu; }
+ static size_t mod421439783(size_t hash) { return hash % 421439783llu; }
+ static size_t mod530980861(size_t hash) { return hash % 530980861llu; }
+ static size_t mod668993977(size_t hash) { return hash % 668993977llu; }
+ static size_t mod842879579(size_t hash) { return hash % 842879579llu; }
+ static size_t mod1061961721(size_t hash) { return hash % 1061961721llu; }
+ static size_t mod1337987929(size_t hash) { return hash % 1337987929llu; }
+ static size_t mod1685759167(size_t hash) { return hash % 1685759167llu; }
+ static size_t mod2123923447(size_t hash) { return hash % 2123923447llu; }
+ static size_t mod2675975881(size_t hash) { return hash % 2675975881llu; }
+ static size_t mod3371518343(size_t hash) { return hash % 3371518343llu; }
+ static size_t mod4247846927(size_t hash) { return hash % 4247846927llu; }
+ static size_t mod5351951779(size_t hash) { return hash % 5351951779llu; }
+ static size_t mod6743036717(size_t hash) { return hash % 6743036717llu; }
+ static size_t mod8495693897(size_t hash) { return hash % 8495693897llu; }
+ static size_t mod10703903591(size_t hash) { return hash % 10703903591llu; }
+ static size_t mod13486073473(size_t hash) { return hash % 13486073473llu; }
+ static size_t mod16991387857(size_t hash) { return hash % 16991387857llu; }
+ static size_t mod21407807219(size_t hash) { return hash % 21407807219llu; }
+ static size_t mod26972146961(size_t hash) { return hash % 26972146961llu; }
+ static size_t mod33982775741(size_t hash) { return hash % 33982775741llu; }
+ static size_t mod42815614441(size_t hash) { return hash % 42815614441llu; }
+ static size_t mod53944293929(size_t hash) { return hash % 53944293929llu; }
+ static size_t mod67965551447(size_t hash) { return hash % 67965551447llu; }
+ static size_t mod85631228929(size_t hash) { return hash % 85631228929llu; }
+ static size_t mod107888587883(size_t hash) { return hash % 107888587883llu; }
+ static size_t mod135931102921(size_t hash) { return hash % 135931102921llu; }
+ static size_t mod171262457903(size_t hash) { return hash % 171262457903llu; }
+ static size_t mod215777175787(size_t hash) { return hash % 215777175787llu; }
+ static size_t mod271862205833(size_t hash) { return hash % 271862205833llu; }
+ static size_t mod342524915839(size_t hash) { return hash % 342524915839llu; }
+ static size_t mod431554351609(size_t hash) { return hash % 431554351609llu; }
+ static size_t mod543724411781(size_t hash) { return hash % 543724411781llu; }
+ static size_t mod685049831731(size_t hash) { return hash % 685049831731llu; }
+ static size_t mod863108703229(size_t hash) { return hash % 863108703229llu; }
+ static size_t mod1087448823553(size_t hash) { return hash % 1087448823553llu; }
+ static size_t mod1370099663459(size_t hash) { return hash % 1370099663459llu; }
+ static size_t mod1726217406467(size_t hash) { return hash % 1726217406467llu; }
+ static size_t mod2174897647073(size_t hash) { return hash % 2174897647073llu; }
+ static size_t mod2740199326961(size_t hash) { return hash % 2740199326961llu; }
+ static size_t mod3452434812973(size_t hash) { return hash % 3452434812973llu; }
+ static size_t mod4349795294267(size_t hash) { return hash % 4349795294267llu; }
+ static size_t mod5480398654009(size_t hash) { return hash % 5480398654009llu; }
+ static size_t mod6904869625999(size_t hash) { return hash % 6904869625999llu; }
+ static size_t mod8699590588571(size_t hash) { return hash % 8699590588571llu; }
+ static size_t mod10960797308051(size_t hash) { return hash % 10960797308051llu; }
+ static size_t mod13809739252051(size_t hash) { return hash % 13809739252051llu; }
+ static size_t mod17399181177241(size_t hash) { return hash % 17399181177241llu; }
+ static size_t mod21921594616111(size_t hash) { return hash % 21921594616111llu; }
+ static size_t mod27619478504183(size_t hash) { return hash % 27619478504183llu; }
+ static size_t mod34798362354533(size_t hash) { return hash % 34798362354533llu; }
+ static size_t mod43843189232363(size_t hash) { return hash % 43843189232363llu; }
+ static size_t mod55238957008387(size_t hash) { return hash % 55238957008387llu; }
+ static size_t mod69596724709081(size_t hash) { return hash % 69596724709081llu; }
+ static size_t mod87686378464759(size_t hash) { return hash % 87686378464759llu; }
+ static size_t mod110477914016779(size_t hash) { return hash % 110477914016779llu; }
+ static size_t mod139193449418173(size_t hash) { return hash % 139193449418173llu; }
+ static size_t mod175372756929481(size_t hash) { return hash % 175372756929481llu; }
+ static size_t mod220955828033581(size_t hash) { return hash % 220955828033581llu; }
+ static size_t mod278386898836457(size_t hash) { return hash % 278386898836457llu; }
+ static size_t mod350745513859007(size_t hash) { return hash % 350745513859007llu; }
+ static size_t mod441911656067171(size_t hash) { return hash % 441911656067171llu; }
+ static size_t mod556773797672909(size_t hash) { return hash % 556773797672909llu; }
+ static size_t mod701491027718027(size_t hash) { return hash % 701491027718027llu; }
+ static size_t mod883823312134381(size_t hash) { return hash % 883823312134381llu; }
+ static size_t mod1113547595345903(size_t hash) { return hash % 1113547595345903llu; }
+ static size_t mod1402982055436147(size_t hash) { return hash % 1402982055436147llu; }
+ static size_t mod1767646624268779(size_t hash) { return hash % 1767646624268779llu; }
+ static size_t mod2227095190691797(size_t hash) { return hash % 2227095190691797llu; }
+ static size_t mod2805964110872297(size_t hash) { return hash % 2805964110872297llu; }
+ static size_t mod3535293248537579(size_t hash) { return hash % 3535293248537579llu; }
+ static size_t mod4454190381383713(size_t hash) { return hash % 4454190381383713llu; }
+ static size_t mod5611928221744609(size_t hash) { return hash % 5611928221744609llu; }
+ static size_t mod7070586497075177(size_t hash) { return hash % 7070586497075177llu; }
+ static size_t mod8908380762767489(size_t hash) { return hash % 8908380762767489llu; }
+ static size_t mod11223856443489329(size_t hash) { return hash % 11223856443489329llu; }
+ static size_t mod14141172994150357(size_t hash) { return hash % 14141172994150357llu; }
+ static size_t mod17816761525534927(size_t hash) { return hash % 17816761525534927llu; }
+ static size_t mod22447712886978529(size_t hash) { return hash % 22447712886978529llu; }
+ static size_t mod28282345988300791(size_t hash) { return hash % 28282345988300791llu; }
+ static size_t mod35633523051069991(size_t hash) { return hash % 35633523051069991llu; }
+ static size_t mod44895425773957261(size_t hash) { return hash % 44895425773957261llu; }
+ static size_t mod56564691976601587(size_t hash) { return hash % 56564691976601587llu; }
+ static size_t mod71267046102139967(size_t hash) { return hash % 71267046102139967llu; }
+ static size_t mod89790851547914507(size_t hash) { return hash % 89790851547914507llu; }
+ static size_t mod113129383953203213(size_t hash) { return hash % 113129383953203213llu; }
+ static size_t mod142534092204280003(size_t hash) { return hash % 142534092204280003llu; }
+ static size_t mod179581703095829107(size_t hash) { return hash % 179581703095829107llu; }
+ static size_t mod226258767906406483(size_t hash) { return hash % 226258767906406483llu; }
+ static size_t mod285068184408560057(size_t hash) { return hash % 285068184408560057llu; }
+ static size_t mod359163406191658253(size_t hash) { return hash % 359163406191658253llu; }
+ static size_t mod452517535812813007(size_t hash) { return hash % 452517535812813007llu; }
+ static size_t mod570136368817120201(size_t hash) { return hash % 570136368817120201llu; }
+ static size_t mod718326812383316683(size_t hash) { return hash % 718326812383316683llu; }
+ static size_t mod905035071625626043(size_t hash) { return hash % 905035071625626043llu; }
+ static size_t mod1140272737634240411(size_t hash) { return hash % 1140272737634240411llu; }
+ static size_t mod1436653624766633509(size_t hash) { return hash % 1436653624766633509llu; }
+ static size_t mod1810070143251252131(size_t hash) { return hash % 1810070143251252131llu; }
+ static size_t mod2280545475268481167(size_t hash) { return hash % 2280545475268481167llu; }
+ static size_t mod2873307249533267101(size_t hash) { return hash % 2873307249533267101llu; }
+ static size_t mod3620140286502504283(size_t hash) { return hash % 3620140286502504283llu; }
+ static size_t mod4561090950536962147(size_t hash) { return hash % 4561090950536962147llu; }
+ static size_t mod5746614499066534157(size_t hash) { return hash % 5746614499066534157llu; }
+ static size_t mod7240280573005008577(size_t hash) { return hash % 7240280573005008577llu; }
+ static size_t mod9122181901073924329(size_t hash) { return hash % 9122181901073924329llu; }
+ static size_t mod11493228998133068689(size_t hash) { return hash % 11493228998133068689llu; }
+ static size_t mod14480561146010017169(size_t hash) { return hash % 14480561146010017169llu; }
+ static size_t mod18446744073709551557(size_t hash) { return hash % 18446744073709551557llu; }
+
+ using mod_function = size_t (*)(size_t);
+
+ mod_function next_size_over(size_t & size) const
+ {
+ // prime numbers generated by the following method:
+ // 1. start with a prime p = 2
+ // 2. go to wolfram alpha and get p = NextPrime(2 * p)
+ // 3. repeat 2. until you overflow 64 bits
+ // you now have large gaps which you would hit if somebody called reserve() with an unlucky number.
+ // 4. to fill the gaps for every prime p go to wolfram alpha and get ClosestPrime(p * 2^(1/3)) and ClosestPrime(p * 2^(2/3)) and put those in the gaps
+ // 5. get PrevPrime(2^64) and put it at the end
+ static constexpr const size_t prime_list[] =
+ {
+ 2llu, 3llu, 5llu, 7llu, 11llu, 13llu, 17llu, 23llu, 29llu, 37llu, 47llu,
+ 59llu, 73llu, 97llu, 127llu, 151llu, 197llu, 251llu, 313llu, 397llu,
+ 499llu, 631llu, 797llu, 1009llu, 1259llu, 1597llu, 2011llu, 2539llu,
+ 3203llu, 4027llu, 5087llu, 6421llu, 8089llu, 10193llu, 12853llu, 16193llu,
+ 20399llu, 25717llu, 32401llu, 40823llu, 51437llu, 64811llu, 81649llu,
+ 102877llu, 129607llu, 163307llu, 205759llu, 259229llu, 326617llu,
+ 411527llu, 518509llu, 653267llu, 823117llu, 1037059llu, 1306601llu,
+ 1646237llu, 2074129llu, 2613229llu, 3292489llu, 4148279llu, 5226491llu,
+ 6584983llu, 8296553llu, 10453007llu, 13169977llu, 16593127llu, 20906033llu,
+ 26339969llu, 33186281llu, 41812097llu, 52679969llu, 66372617llu,
+ 83624237llu, 105359939llu, 132745199llu, 167248483llu, 210719881llu,
+ 265490441llu, 334496971llu, 421439783llu, 530980861llu, 668993977llu,
+ 842879579llu, 1061961721llu, 1337987929llu, 1685759167llu, 2123923447llu,
+ 2675975881llu, 3371518343llu, 4247846927llu, 5351951779llu, 6743036717llu,
+ 8495693897llu, 10703903591llu, 13486073473llu, 16991387857llu,
+ 21407807219llu, 26972146961llu, 33982775741llu, 42815614441llu,
+ 53944293929llu, 67965551447llu, 85631228929llu, 107888587883llu,
+ 135931102921llu, 171262457903llu, 215777175787llu, 271862205833llu,
+ 342524915839llu, 431554351609llu, 543724411781llu, 685049831731llu,
+ 863108703229llu, 1087448823553llu, 1370099663459llu, 1726217406467llu,
+ 2174897647073llu, 2740199326961llu, 3452434812973llu, 4349795294267llu,
+ 5480398654009llu, 6904869625999llu, 8699590588571llu, 10960797308051llu,
+ 13809739252051llu, 17399181177241llu, 21921594616111llu, 27619478504183llu,
+ 34798362354533llu, 43843189232363llu, 55238957008387llu, 69596724709081llu,
+ 87686378464759llu, 110477914016779llu, 139193449418173llu,
+ 175372756929481llu, 220955828033581llu, 278386898836457llu,
+ 350745513859007llu, 441911656067171llu, 556773797672909llu,
+ 701491027718027llu, 883823312134381llu, 1113547595345903llu,
+ 1402982055436147llu, 1767646624268779llu, 2227095190691797llu,
+ 2805964110872297llu, 3535293248537579llu, 4454190381383713llu,
+ 5611928221744609llu, 7070586497075177llu, 8908380762767489llu,
+ 11223856443489329llu, 14141172994150357llu, 17816761525534927llu,
+ 22447712886978529llu, 28282345988300791llu, 35633523051069991llu,
+ 44895425773957261llu, 56564691976601587llu, 71267046102139967llu,
+ 89790851547914507llu, 113129383953203213llu, 142534092204280003llu,
+ 179581703095829107llu, 226258767906406483llu, 285068184408560057llu,
+ 359163406191658253llu, 452517535812813007llu, 570136368817120201llu,
+ 718326812383316683llu, 905035071625626043llu, 1140272737634240411llu,
+ 1436653624766633509llu, 1810070143251252131llu, 2280545475268481167llu,
+ 2873307249533267101llu, 3620140286502504283llu, 4561090950536962147llu,
+ 5746614499066534157llu, 7240280573005008577llu, 9122181901073924329llu,
+ 11493228998133068689llu, 14480561146010017169llu, 18446744073709551557llu
+ };
+ static constexpr size_t (* const mod_functions[])(size_t) =
+ {
+ &mod0, &mod2, &mod3, &mod5, &mod7, &mod11, &mod13, &mod17, &mod23, &mod29, &mod37,
+ &mod47, &mod59, &mod73, &mod97, &mod127, &mod151, &mod197, &mod251, &mod313, &mod397,
+ &mod499, &mod631, &mod797, &mod1009, &mod1259, &mod1597, &mod2011, &mod2539, &mod3203,
+ &mod4027, &mod5087, &mod6421, &mod8089, &mod10193, &mod12853, &mod16193, &mod20399,
+ &mod25717, &mod32401, &mod40823, &mod51437, &mod64811, &mod81649, &mod102877,
+ &mod129607, &mod163307, &mod205759, &mod259229, &mod326617, &mod411527, &mod518509,
+ &mod653267, &mod823117, &mod1037059, &mod1306601, &mod1646237, &mod2074129,
+ &mod2613229, &mod3292489, &mod4148279, &mod5226491, &mod6584983, &mod8296553,
+ &mod10453007, &mod13169977, &mod16593127, &mod20906033, &mod26339969, &mod33186281,
+ &mod41812097, &mod52679969, &mod66372617, &mod83624237, &mod105359939, &mod132745199,
+ &mod167248483, &mod210719881, &mod265490441, &mod334496971, &mod421439783,
+ &mod530980861, &mod668993977, &mod842879579, &mod1061961721, &mod1337987929,
+ &mod1685759167, &mod2123923447, &mod2675975881, &mod3371518343, &mod4247846927,
+ &mod5351951779, &mod6743036717, &mod8495693897, &mod10703903591, &mod13486073473,
+ &mod16991387857, &mod21407807219, &mod26972146961, &mod33982775741, &mod42815614441,
+ &mod53944293929, &mod67965551447, &mod85631228929, &mod107888587883, &mod135931102921,
+ &mod171262457903, &mod215777175787, &mod271862205833, &mod342524915839,
+ &mod431554351609, &mod543724411781, &mod685049831731, &mod863108703229,
+ &mod1087448823553, &mod1370099663459, &mod1726217406467, &mod2174897647073,
+ &mod2740199326961, &mod3452434812973, &mod4349795294267, &mod5480398654009,
+ &mod6904869625999, &mod8699590588571, &mod10960797308051, &mod13809739252051,
+ &mod17399181177241, &mod21921594616111, &mod27619478504183, &mod34798362354533,
+ &mod43843189232363, &mod55238957008387, &mod69596724709081, &mod87686378464759,
+ &mod110477914016779, &mod139193449418173, &mod175372756929481, &mod220955828033581,
+ &mod278386898836457, &mod350745513859007, &mod441911656067171, &mod556773797672909,
+ &mod701491027718027, &mod883823312134381, &mod1113547595345903, &mod1402982055436147,
+ &mod1767646624268779, &mod2227095190691797, &mod2805964110872297, &mod3535293248537579,
+ &mod4454190381383713, &mod5611928221744609, &mod7070586497075177, &mod8908380762767489,
+ &mod11223856443489329, &mod14141172994150357, &mod17816761525534927,
+ &mod22447712886978529, &mod28282345988300791, &mod35633523051069991,
+ &mod44895425773957261, &mod56564691976601587, &mod71267046102139967,
+ &mod89790851547914507, &mod113129383953203213, &mod142534092204280003,
+ &mod179581703095829107, &mod226258767906406483, &mod285068184408560057,
+ &mod359163406191658253, &mod452517535812813007, &mod570136368817120201,
+ &mod718326812383316683, &mod905035071625626043, &mod1140272737634240411,
+ &mod1436653624766633509, &mod1810070143251252131, &mod2280545475268481167,
+ &mod2873307249533267101, &mod3620140286502504283, &mod4561090950536962147,
+ &mod5746614499066534157, &mod7240280573005008577, &mod9122181901073924329,
+ &mod11493228998133068689, &mod14480561146010017169, &mod18446744073709551557
+ };
+ const size_t * found = std::lower_bound(std::begin(prime_list), std::end(prime_list) - 1, size);
+ size = *found;
+ return mod_functions[1 + found - prime_list];
+ }
+ void commit(mod_function new_mod_function)
+ {
+ current_mod_function = new_mod_function;
+ }
+ void reset()
+ {
+ current_mod_function = &mod0;
+ }
+
+ size_t index_for_hash(size_t hash, size_t /*num_slots_minus_one*/) const
+ {
+ return current_mod_function(hash);
+ }
+ size_t keep_in_range(size_t index, size_t num_slots_minus_one) const
+ {
+ return index > num_slots_minus_one ? current_mod_function(index) : index;
+ }
+
+private:
+ mod_function current_mod_function = &mod0;
+};
+
+struct power_of_two_hash_policy
+{
+ size_t index_for_hash(size_t hash, size_t num_slots_minus_one) const
+ {
+ return hash & num_slots_minus_one;
+ }
+ size_t keep_in_range(size_t index, size_t num_slots_minus_one) const
+ {
+ return index_for_hash(index, num_slots_minus_one);
+ }
+ int8_t next_size_over(size_t & size) const
+ {
+ size = detailv3::next_power_of_two(size);
+ return 0;
+ }
+ void commit(int8_t)
+ {
+ }
+ void reset()
+ {
+ }
+
+};
+
+struct fibonacci_hash_policy
+{
+ size_t index_for_hash(size_t hash, size_t /*num_slots_minus_one*/) const
+ {
+ return (11400714819323198485ull * hash) >> shift;
+ }
+ size_t keep_in_range(size_t index, size_t num_slots_minus_one) const
+ {
+ return index & num_slots_minus_one;
+ }
+
+ int8_t next_size_over(size_t & size) const
+ {
+ size = std::max(size_t(2), detailv3::next_power_of_two(size));
+ return 64 - detailv3::log2(size);
+ }
+ void commit(int8_t shift)
+ {
+ this->shift = shift;
+ }
+ void reset()
+ {
+ shift = 63;
+ }
+
+private:
+ int8_t shift = 63;
+};
+
+template<typename K, typename V, typename H = std::hash<K>, typename E = std::equal_to<K>, typename A = std::allocator<std::pair<K, V> > >
+class flat_hash_map
+ : public detailv3::sherwood_v3_table
+ <
+ std::pair<K, V>,
+ K,
+ H,
+ detailv3::KeyOrValueHasher<K, std::pair<K, V>, H>,
+ E,
+ detailv3::KeyOrValueEquality<K, std::pair<K, V>, E>,
+ A,
+ typename std::allocator_traits<A>::template rebind_alloc<detailv3::sherwood_v3_entry<std::pair<K, V>>>
+ >
+{
+ using Table = detailv3::sherwood_v3_table
+ <
+ std::pair<K, V>,
+ K,
+ H,
+ detailv3::KeyOrValueHasher<K, std::pair<K, V>, H>,
+ E,
+ detailv3::KeyOrValueEquality<K, std::pair<K, V>, E>,
+ A,
+ typename std::allocator_traits<A>::template rebind_alloc<detailv3::sherwood_v3_entry<std::pair<K, V>>>
+ >;
+public:
+
+ using key_type = K;
+ using mapped_type = V;
+
+ using Table::Table;
+ flat_hash_map()
+ {
+ }
+
+ inline V & operator[](const K & key)
+ {
+ return emplace(key, convertible_to_value()).first->second;
+ }
+ inline V & operator[](K && key)
+ {
+ return emplace(std::move(key), convertible_to_value()).first->second;
+ }
+ V & at(const K & key)
+ {
+ auto found = this->find(key);
+ if (found == this->end())
+ throw std::out_of_range("Argument passed to at() was not in the map.");
+ return found->second;
+ }
+ const V & at(const K & key) const
+ {
+ auto found = this->find(key);
+ if (found == this->end())
+ throw std::out_of_range("Argument passed to at() was not in the map.");
+ return found->second;
+ }
+
+ using Table::emplace;
+ std::pair<typename Table::iterator, bool> emplace()
+ {
+ return emplace(key_type(), convertible_to_value());
+ }
+ template<typename M>
+ std::pair<typename Table::iterator, bool> insert_or_assign(const key_type & key, M && m)
+ {
+ auto emplace_result = emplace(key, std::forward<M>(m));
+ if (!emplace_result.second)
+ emplace_result.first->second = std::forward<M>(m);
+ return emplace_result;
+ }
+ template<typename M>
+ std::pair<typename Table::iterator, bool> insert_or_assign(key_type && key, M && m)
+ {
+ auto emplace_result = emplace(std::move(key), std::forward<M>(m));
+ if (!emplace_result.second)
+ emplace_result.first->second = std::forward<M>(m);
+ return emplace_result;
+ }
+ template<typename M>
+ typename Table::iterator insert_or_assign(typename Table::const_iterator, const key_type & key, M && m)
+ {
+ return insert_or_assign(key, std::forward<M>(m)).first;
+ }
+ template<typename M>
+ typename Table::iterator insert_or_assign(typename Table::const_iterator, key_type && key, M && m)
+ {
+ return insert_or_assign(std::move(key), std::forward<M>(m)).first;
+ }
+
+ friend bool operator==(const flat_hash_map & lhs, const flat_hash_map & rhs)
+ {
+ if (lhs.size() != rhs.size())
+ return false;
+ for (const typename Table::value_type & value : lhs)
+ {
+ auto found = rhs.find(value.first);
+ if (found == rhs.end())
+ return false;
+ else if (value.second != found->second)
+ return false;
+ }
+ return true;
+ }
+ friend bool operator!=(const flat_hash_map & lhs, const flat_hash_map & rhs)
+ {
+ return !(lhs == rhs);
+ }
+
+private:
+ struct convertible_to_value
+ {
+ operator V() const
+ {
+ return V();
+ }
+ };
+};
+
+template<typename T, typename H = std::hash<T>, typename E = std::equal_to<T>, typename A = std::allocator<T> >
+class flat_hash_set
+ : public detailv3::sherwood_v3_table
+ <
+ T,
+ T,
+ H,
+ detailv3::functor_storage<size_t, H>,
+ E,
+ detailv3::functor_storage<bool, E>,
+ A,
+ typename std::allocator_traits<A>::template rebind_alloc<detailv3::sherwood_v3_entry<T>>
+ >
+{
+ using Table = detailv3::sherwood_v3_table
+ <
+ T,
+ T,
+ H,
+ detailv3::functor_storage<size_t, H>,
+ E,
+ detailv3::functor_storage<bool, E>,
+ A,
+ typename std::allocator_traits<A>::template rebind_alloc<detailv3::sherwood_v3_entry<T>>
+ >;
+public:
+
+ using key_type = T;
+
+ using Table::Table;
+ flat_hash_set()
+ {
+ }
+
+ template<typename... Args>
+ std::pair<typename Table::iterator, bool> emplace(Args &&... args)
+ {
+ return Table::emplace(T(std::forward<Args>(args)...));
+ }
+ std::pair<typename Table::iterator, bool> emplace(const key_type & arg)
+ {
+ return Table::emplace(arg);
+ }
+ std::pair<typename Table::iterator, bool> emplace(key_type & arg)
+ {
+ return Table::emplace(arg);
+ }
+ std::pair<typename Table::iterator, bool> emplace(const key_type && arg)
+ {
+ return Table::emplace(std::move(arg));
+ }
+ std::pair<typename Table::iterator, bool> emplace(key_type && arg)
+ {
+ return Table::emplace(std::move(arg));
+ }
+
+ friend bool operator==(const flat_hash_set & lhs, const flat_hash_set & rhs)
+ {
+ if (lhs.size() != rhs.size())
+ return false;
+ for (const T & value : lhs)
+ {
+ if (rhs.find(value) == rhs.end())
+ return false;
+ }
+ return true;
+ }
+ friend bool operator!=(const flat_hash_set & lhs, const flat_hash_set & rhs)
+ {
+ return !(lhs == rhs);
+ }
+};
+
+
+template<typename T>
+struct power_of_two_std_hash : std::hash<T>
+{
+ typedef ska::power_of_two_hash_policy hash_policy;
+};
+
+} // end namespace ska
diff --git a/benchmarks/others/sparsepp/spp.h b/benchmarks/others/sparsepp/spp.h
index 35d58492..26aa2001 100644
--- a/benchmarks/others/sparsepp/spp.h
+++ b/benchmarks/others/sparsepp/spp.h
@@ -3719,7 +3719,10 @@ public:
rep(std::move(o.rep), alloc)
{}
- sparse_hash_map& operator=(sparse_hash_map &&o) = default;
+ sparse_hash_map& operator=(sparse_hash_map &&o)
+ {
+ rep = std::move(o.rep);
+ }
#endif
#if !defined(SPP_NO_CXX11_HDR_INITIALIZER_LIST)
diff --git a/benchmarks/others/tsl/hopscotch_growth_policy.h b/benchmarks/others/tsl/hopscotch_growth_policy.h
new file mode 100644
index 00000000..0e463868
--- /dev/null
+++ b/benchmarks/others/tsl/hopscotch_growth_policy.h
@@ -0,0 +1,404 @@
+/**
+ * MIT License
+ *
+ * Copyright (c) 2018 Thibaut Goetghebuer-Planchon <[email protected]>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+#ifndef TSL_HOPSCOTCH_GROWTH_POLICY_H
+#define TSL_HOPSCOTCH_GROWTH_POLICY_H
+
+#include <algorithm>
+#include <array>
+#include <climits>
+#include <cmath>
+#include <cstddef>
+#include <cstdint>
+#include <iterator>
+#include <limits>
+#include <ratio>
+#include <stdexcept>
+
+/**
+ * Only activate tsl_hh_assert if TSL_DEBUG is defined.
+ * This way we avoid the performance hit when NDEBUG is not defined with assert
+ * as tsl_hh_assert is used a lot (people usually compile with "-O3" and not
+ * "-O3 -DNDEBUG").
+ */
+#ifdef TSL_DEBUG
+#define tsl_hh_assert(expr) assert(expr)
+#else
+#define tsl_hh_assert(expr) (static_cast<void>(0))
+#endif
+
+/**
+ * If exceptions are enabled, throw the exception passed in parameter, otherwise
+ * call std::terminate.
+ */
+#if (defined(__cpp_exceptions) || defined(__EXCEPTIONS) || \
+ (defined(_MSC_VER) && defined(_CPPUNWIND))) && \
+ !defined(TSL_NO_EXCEPTIONS)
+#define TSL_HH_THROW_OR_TERMINATE(ex, msg) throw ex(msg)
+#else
+#define TSL_HH_NO_EXCEPTIONS
+#ifdef NDEBUG
+#define TSL_HH_THROW_OR_TERMINATE(ex, msg) std::terminate()
+#else
+#include <iostream>
+#define TSL_HH_THROW_OR_TERMINATE(ex, msg) \
+ do { \
+ std::cerr << msg << std::endl; \
+ std::terminate(); \
+ } while (0)
+#endif
+#endif
+
+namespace tsl {
+namespace hh {
+
+/**
+ * Grow the hash table by a factor of GrowthFactor keeping the bucket count to a
+ * power of two. It allows the table to use a mask operation instead of a modulo
+ * operation to map a hash to a bucket.
+ *
+ * GrowthFactor must be a power of two >= 2.
+ */
+template <std::size_t GrowthFactor>
+class power_of_two_growth_policy {
+ public:
+ /**
+ * Called on the hash table creation and on rehash. The number of buckets for
+ * the table is passed in parameter. This number is a minimum, the policy may
+ * update this value with a higher value if needed (but not lower).
+ *
+ * If 0 is given, min_bucket_count_in_out must still be 0 after the policy
+ * creation and bucket_for_hash must always return 0 in this case.
+ */
+ explicit power_of_two_growth_policy(std::size_t& min_bucket_count_in_out) {
+ if (min_bucket_count_in_out > max_bucket_count()) {
+ TSL_HH_THROW_OR_TERMINATE(std::length_error,
+ "The hash table exceeds its maximum size.");
+ }
+
+ if (min_bucket_count_in_out > 0) {
+ min_bucket_count_in_out =
+ round_up_to_power_of_two(min_bucket_count_in_out);
+ m_mask = min_bucket_count_in_out - 1;
+ } else {
+ m_mask = 0;
+ }
+ }
+
+ /**
+ * Return the bucket [0, bucket_count()) to which the hash belongs.
+ * If bucket_count() is 0, it must always return 0.
+ */
+ std::size_t bucket_for_hash(std::size_t hash) const noexcept {
+ return hash & m_mask;
+ }
+
+ /**
+ * Return the bucket count to use when the bucket array grows on rehash.
+ */
+ std::size_t next_bucket_count() const {
+ if ((m_mask + 1) > max_bucket_count() / GrowthFactor) {
+ TSL_HH_THROW_OR_TERMINATE(std::length_error,
+ "The hash table exceeds its maximum size.");
+ }
+
+ return (m_mask + 1) * GrowthFactor;
+ }
+
+ /**
+ * Return the maximum number of buckets supported by the policy.
+ */
+ std::size_t max_bucket_count() const {
+ // Largest power of two.
+ return (std::numeric_limits<std::size_t>::max() / 2) + 1;
+ }
+
+ /**
+ * Reset the growth policy as if it was created with a bucket count of 0.
+ * After a clear, the policy must always return 0 when bucket_for_hash is
+ * called.
+ */
+ void clear() noexcept { m_mask = 0; }
+
+ private:
+ static std::size_t round_up_to_power_of_two(std::size_t value) {
+ if (is_power_of_two(value)) {
+ return value;
+ }
+
+ if (value == 0) {
+ return 1;
+ }
+
+ --value;
+ for (std::size_t i = 1; i < sizeof(std::size_t) * CHAR_BIT; i *= 2) {
+ value |= value >> i;
+ }
+
+ return value + 1;
+ }
+
+ static constexpr bool is_power_of_two(std::size_t value) {
+ return value != 0 && (value & (value - 1)) == 0;
+ }
+
+ private:
+ static_assert(is_power_of_two(GrowthFactor) && GrowthFactor >= 2,
+ "GrowthFactor must be a power of two >= 2.");
+
+ std::size_t m_mask;
+};
+
+/**
+ * Grow the hash table by GrowthFactor::num / GrowthFactor::den and use a modulo
+ * to map a hash to a bucket. Slower but it can be useful if you want a slower
+ * growth.
+ */
+template <class GrowthFactor = std::ratio<3, 2>>
+class mod_growth_policy {
+ public:
+ explicit mod_growth_policy(std::size_t& min_bucket_count_in_out) {
+ if (min_bucket_count_in_out > max_bucket_count()) {
+ TSL_HH_THROW_OR_TERMINATE(std::length_error,
+ "The hash table exceeds its maximum size.");
+ }
+
+ if (min_bucket_count_in_out > 0) {
+ m_mod = min_bucket_count_in_out;
+ } else {
+ m_mod = 1;
+ }
+ }
+
+ std::size_t bucket_for_hash(std::size_t hash) const noexcept {
+ return hash % m_mod;
+ }
+
+ std::size_t next_bucket_count() const {
+ if (m_mod == max_bucket_count()) {
+ TSL_HH_THROW_OR_TERMINATE(std::length_error,
+ "The hash table exceeds its maximum size.");
+ }
+
+ const double next_bucket_count =
+ std::ceil(double(m_mod) * REHASH_SIZE_MULTIPLICATION_FACTOR);
+ if (!std::isnormal(next_bucket_count)) {
+ TSL_HH_THROW_OR_TERMINATE(std::length_error,
+ "The hash table exceeds its maximum size.");
+ }
+
+ if (next_bucket_count > double(max_bucket_count())) {
+ return max_bucket_count();
+ } else {
+ return std::size_t(next_bucket_count);
+ }
+ }
+
+ std::size_t max_bucket_count() const { return MAX_BUCKET_COUNT; }
+
+ void clear() noexcept { m_mod = 1; }
+
+ private:
+ static constexpr double REHASH_SIZE_MULTIPLICATION_FACTOR =
+ 1.0 * GrowthFactor::num / GrowthFactor::den;
+ static const std::size_t MAX_BUCKET_COUNT =
+ std::size_t(double(std::numeric_limits<std::size_t>::max() /
+ REHASH_SIZE_MULTIPLICATION_FACTOR));
+
+ static_assert(REHASH_SIZE_MULTIPLICATION_FACTOR >= 1.1,
+ "Growth factor should be >= 1.1.");
+
+ std::size_t m_mod;
+};
+
+namespace detail {
+
+#if SIZE_MAX >= ULLONG_MAX
+#define TSL_HH_NB_PRIMES 51
+#elif SIZE_MAX >= ULONG_MAX
+#define TSL_HH_NB_PRIMES 40
+#else
+#define TSL_HH_NB_PRIMES 23
+#endif
+
+static constexpr const std::array<std::size_t, TSL_HH_NB_PRIMES> PRIMES = {{
+ 1u,
+ 5u,
+ 17u,
+ 29u,
+ 37u,
+ 53u,
+ 67u,
+ 79u,
+ 97u,
+ 131u,
+ 193u,
+ 257u,
+ 389u,
+ 521u,
+ 769u,
+ 1031u,
+ 1543u,
+ 2053u,
+ 3079u,
+ 6151u,
+ 12289u,
+ 24593u,
+ 49157u,
+#if SIZE_MAX >= ULONG_MAX
+ 98317ul,
+ 196613ul,
+ 393241ul,
+ 786433ul,
+ 1572869ul,
+ 3145739ul,
+ 6291469ul,
+ 12582917ul,
+ 25165843ul,
+ 50331653ul,
+ 100663319ul,
+ 201326611ul,
+ 402653189ul,
+ 805306457ul,
+ 1610612741ul,
+ 3221225473ul,
+ 4294967291ul,
+#endif
+#if SIZE_MAX >= ULLONG_MAX
+ 6442450939ull,
+ 12884901893ull,
+ 25769803751ull,
+ 51539607551ull,
+ 103079215111ull,
+ 206158430209ull,
+ 412316860441ull,
+ 824633720831ull,
+ 1649267441651ull,
+ 3298534883309ull,
+ 6597069766657ull,
+#endif
+}};
+
+template <unsigned int IPrime>
+static constexpr std::size_t mod(std::size_t hash) {
+ return hash % PRIMES[IPrime];
+}
+
+// MOD_PRIME[iprime](hash) returns hash % PRIMES[iprime]. This table allows for
+// faster modulo as the compiler can optimize the modulo code better with a
+// constant known at the compilation.
+static constexpr const std::array<std::size_t (*)(std::size_t),
+ TSL_HH_NB_PRIMES>
+ MOD_PRIME = {{
+ &mod<0>, &mod<1>, &mod<2>, &mod<3>, &mod<4>, &mod<5>,
+ &mod<6>, &mod<7>, &mod<8>, &mod<9>, &mod<10>, &mod<11>,
+ &mod<12>, &mod<13>, &mod<14>, &mod<15>, &mod<16>, &mod<17>,
+ &mod<18>, &mod<19>, &mod<20>, &mod<21>, &mod<22>,
+#if SIZE_MAX >= ULONG_MAX
+ &mod<23>, &mod<24>, &mod<25>, &mod<26>, &mod<27>, &mod<28>,
+ &mod<29>, &mod<30>, &mod<31>, &mod<32>, &mod<33>, &mod<34>,
+ &mod<35>, &mod<36>, &mod<37>, &mod<38>, &mod<39>,
+#endif
+#if SIZE_MAX >= ULLONG_MAX
+ &mod<40>, &mod<41>, &mod<42>, &mod<43>, &mod<44>, &mod<45>,
+ &mod<46>, &mod<47>, &mod<48>, &mod<49>, &mod<50>,
+#endif
+ }};
+
+} // namespace detail
+
+/**
+ * Grow the hash table by using prime numbers as bucket count. Slower than
+ * tsl::hh::power_of_two_growth_policy in general but will probably distribute
+ * the values around better in the buckets with a poor hash function.
+ *
+ * To allow the compiler to optimize the modulo operation, a lookup table is
+ * used with constant primes numbers.
+ *
+ * With a switch the code would look like:
+ * \code
+ * switch(iprime) { // iprime is the current prime of the hash table
+ * case 0: hash % 5ul;
+ * break;
+ * case 1: hash % 17ul;
+ * break;
+ * case 2: hash % 29ul;
+ * break;
+ * ...
+ * }
+ * \endcode
+ *
+ * Due to the constant variable in the modulo the compiler is able to optimize
+ * the operation by a series of multiplications, substractions and shifts.
+ *
+ * The 'hash % 5' could become something like 'hash - (hash * 0xCCCCCCCD) >> 34)
+ * * 5' in a 64 bits environment.
+ */
+class prime_growth_policy {
+ public:
+ explicit prime_growth_policy(std::size_t& min_bucket_count_in_out) {
+ auto it_prime = std::lower_bound(
+ detail::PRIMES.begin(), detail::PRIMES.end(), min_bucket_count_in_out);
+ if (it_prime == detail::PRIMES.end()) {
+ TSL_HH_THROW_OR_TERMINATE(std::length_error,
+ "The hash table exceeds its maximum size.");
+ }
+
+ m_iprime = static_cast<unsigned int>(
+ std::distance(detail::PRIMES.begin(), it_prime));
+ if (min_bucket_count_in_out > 0) {
+ min_bucket_count_in_out = *it_prime;
+ } else {
+ min_bucket_count_in_out = 0;
+ }
+ }
+
+ std::size_t bucket_for_hash(std::size_t hash) const noexcept {
+ return detail::MOD_PRIME[m_iprime](hash);
+ }
+
+ std::size_t next_bucket_count() const {
+ if (m_iprime + 1 >= detail::PRIMES.size()) {
+ TSL_HH_THROW_OR_TERMINATE(std::length_error,
+ "The hash table exceeds its maximum size.");
+ }
+
+ return detail::PRIMES[m_iprime + 1];
+ }
+
+ std::size_t max_bucket_count() const { return detail::PRIMES.back(); }
+
+ void clear() noexcept { m_iprime = 0; }
+
+ private:
+ unsigned int m_iprime;
+
+ static_assert(std::numeric_limits<decltype(m_iprime)>::max() >=
+ detail::PRIMES.size(),
+ "The type of m_iprime is not big enough.");
+};
+
+} // namespace hh
+} // namespace tsl
+
+#endif
diff --git a/benchmarks/others/tsl/hopscotch_hash.h b/benchmarks/others/tsl/hopscotch_hash.h
new file mode 100644
index 00000000..ad4f58e0
--- /dev/null
+++ b/benchmarks/others/tsl/hopscotch_hash.h
@@ -0,0 +1,1883 @@
+/**
+ * MIT License
+ *
+ * Copyright (c) 2017 Thibaut Goetghebuer-Planchon <[email protected]>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+#ifndef TSL_HOPSCOTCH_HASH_H
+#define TSL_HOPSCOTCH_HASH_H
+
+#include <algorithm>
+#include <cassert>
+#include <cmath>
+#include <cstddef>
+#include <cstdint>
+#include <exception>
+#include <functional>
+#include <initializer_list>
+#include <iterator>
+#include <limits>
+#include <memory>
+#include <stdexcept>
+#include <tuple>
+#include <type_traits>
+#include <utility>
+#include <vector>
+
+#include "hopscotch_growth_policy.h"
+
+#if (defined(__GNUC__) && (__GNUC__ == 4) && (__GNUC_MINOR__ < 9))
+#define TSL_HH_NO_RANGE_ERASE_WITH_CONST_ITERATOR
+#endif
+
+namespace tsl {
+namespace detail_hopscotch_hash {
+
+template <typename T>
+struct make_void {
+ using type = void;
+};
+
+template <typename T, typename = void>
+struct has_is_transparent : std::false_type {};
+
+template <typename T>
+struct has_is_transparent<T,
+ typename make_void<typename T::is_transparent>::type>
+ : std::true_type {};
+
+template <typename T, typename = void>
+struct has_key_compare : std::false_type {};
+
+template <typename T>
+struct has_key_compare<T, typename make_void<typename T::key_compare>::type>
+ : std::true_type {};
+
+template <typename U>
+struct is_power_of_two_policy : std::false_type {};
+
+template <std::size_t GrowthFactor>
+struct is_power_of_two_policy<tsl::hh::power_of_two_growth_policy<GrowthFactor>>
+ : std::true_type {};
+
+template <typename T, typename U>
+static T numeric_cast(U value,
+ const char* error_message = "numeric_cast() failed.") {
+ T ret = static_cast<T>(value);
+ if (static_cast<U>(ret) != value) {
+ TSL_HH_THROW_OR_TERMINATE(std::runtime_error, error_message);
+ }
+
+ const bool is_same_signedness =
+ (std::is_unsigned<T>::value && std::is_unsigned<U>::value) ||
+ (std::is_signed<T>::value && std::is_signed<U>::value);
+ if (!is_same_signedness && (ret < T{}) != (value < U{})) {
+ TSL_HH_THROW_OR_TERMINATE(std::runtime_error, error_message);
+ }
+
+ return ret;
+}
+
+/*
+ * smallest_type_for_min_bits::type returns the smallest type that can fit
+ * MinBits.
+ */
+static const std::size_t SMALLEST_TYPE_MAX_BITS_SUPPORTED = 64;
+template <unsigned int MinBits, typename Enable = void>
+class smallest_type_for_min_bits {};
+
+template <unsigned int MinBits>
+class smallest_type_for_min_bits<
+ MinBits, typename std::enable_if<(MinBits > 0) && (MinBits <= 8)>::type> {
+ public:
+ using type = std::uint_least8_t;
+};
+
+template <unsigned int MinBits>
+class smallest_type_for_min_bits<
+ MinBits, typename std::enable_if<(MinBits > 8) && (MinBits <= 16)>::type> {
+ public:
+ using type = std::uint_least16_t;
+};
+
+template <unsigned int MinBits>
+class smallest_type_for_min_bits<
+ MinBits, typename std::enable_if<(MinBits > 16) && (MinBits <= 32)>::type> {
+ public:
+ using type = std::uint_least32_t;
+};
+
+template <unsigned int MinBits>
+class smallest_type_for_min_bits<
+ MinBits, typename std::enable_if<(MinBits > 32) && (MinBits <= 64)>::type> {
+ public:
+ using type = std::uint_least64_t;
+};
+
+/*
+ * Each bucket may store up to three elements:
+ * - An aligned storage to store a value_type object with placement-new.
+ * - An (optional) hash of the value in the bucket.
+ * - An unsigned integer of type neighborhood_bitmap used to tell us which
+ * buckets in the neighborhood of the current bucket contain a value with a hash
+ * belonging to the current bucket.
+ *
+ * For a bucket 'bct', a bit 'i' (counting from 0 and from the least significant
+ * bit to the most significant) set to 1 means that the bucket 'bct + i'
+ * contains a value with a hash belonging to bucket 'bct'. The bits used for
+ * that, start from the third least significant bit. The two least significant
+ * bits are reserved:
+ * - The least significant bit is set to 1 if there is a value in the bucket
+ * storage.
+ * - The second least significant bit is set to 1 if there is an overflow. More
+ * than NeighborhoodSize values give the same hash, all overflow values are
+ * stored in the m_overflow_elements list of the map.
+ *
+ * Details regarding hopscotch hashing an its implementation can be found here:
+ * https://tessil.github.io/2016/08/29/hopscotch-hashing.html
+ */
+static const std::size_t NB_RESERVED_BITS_IN_NEIGHBORHOOD = 2;
+
+using truncated_hash_type = std::uint_least32_t;
+
+/**
+ * Helper class that stores a truncated hash if StoreHash is true and nothing
+ * otherwise.
+ */
+template <bool StoreHash>
+class hopscotch_bucket_hash {
+ public:
+ bool bucket_hash_equal(std::size_t /*hash*/) const noexcept { return true; }
+
+ truncated_hash_type truncated_bucket_hash() const noexcept { return 0; }
+
+ protected:
+ void copy_hash(const hopscotch_bucket_hash&) noexcept {}
+
+ void set_hash(truncated_hash_type /*hash*/) noexcept {}
+};
+
+template <>
+class hopscotch_bucket_hash<true> {
+ public:
+ bool bucket_hash_equal(std::size_t hash) const noexcept {
+ return m_hash == truncated_hash_type(hash);
+ }
+
+ truncated_hash_type truncated_bucket_hash() const noexcept { return m_hash; }
+
+ protected:
+ void copy_hash(const hopscotch_bucket_hash& bucket) noexcept {
+ m_hash = bucket.m_hash;
+ }
+
+ void set_hash(truncated_hash_type hash) noexcept { m_hash = hash; }
+
+ private:
+ truncated_hash_type m_hash;
+};
+
+template <typename ValueType, unsigned int NeighborhoodSize, bool StoreHash>
+class hopscotch_bucket : public hopscotch_bucket_hash<StoreHash> {
+ private:
+ static const std::size_t MIN_NEIGHBORHOOD_SIZE = 4;
+ static const std::size_t MAX_NEIGHBORHOOD_SIZE =
+ SMALLEST_TYPE_MAX_BITS_SUPPORTED - NB_RESERVED_BITS_IN_NEIGHBORHOOD;
+
+ static_assert(NeighborhoodSize >= 4, "NeighborhoodSize should be >= 4.");
+ // We can't put a variable in the message, ensure coherence
+ static_assert(MIN_NEIGHBORHOOD_SIZE == 4, "");
+
+ static_assert(NeighborhoodSize <= 62, "NeighborhoodSize should be <= 62.");
+ // We can't put a variable in the message, ensure coherence
+ static_assert(MAX_NEIGHBORHOOD_SIZE == 62, "");
+
+ static_assert(!StoreHash || NeighborhoodSize <= 30,
+ "NeighborhoodSize should be <= 30 if StoreHash is true.");
+ // We can't put a variable in the message, ensure coherence
+ static_assert(MAX_NEIGHBORHOOD_SIZE - 32 == 30, "");
+
+ using bucket_hash = hopscotch_bucket_hash<StoreHash>;
+
+ public:
+ using value_type = ValueType;
+ using neighborhood_bitmap = typename smallest_type_for_min_bits<
+ NeighborhoodSize + NB_RESERVED_BITS_IN_NEIGHBORHOOD>::type;
+
+ hopscotch_bucket() noexcept : bucket_hash(), m_neighborhood_infos(0) {
+ tsl_hh_assert(empty());
+ }
+
+ hopscotch_bucket(const hopscotch_bucket& bucket) noexcept(
+ std::is_nothrow_copy_constructible<value_type>::value)
+ : bucket_hash(bucket), m_neighborhood_infos(0) {
+ if (!bucket.empty()) {
+ ::new (static_cast<void*>(std::addressof(m_value)))
+ value_type(bucket.value());
+ }
+
+ m_neighborhood_infos = bucket.m_neighborhood_infos;
+ }
+
+ hopscotch_bucket(hopscotch_bucket&& bucket) noexcept(
+ std::is_nothrow_move_constructible<value_type>::value)
+ : bucket_hash(std::move(bucket)), m_neighborhood_infos(0) {
+ if (!bucket.empty()) {
+ ::new (static_cast<void*>(std::addressof(m_value)))
+ value_type(std::move(bucket.value()));
+ }
+
+ m_neighborhood_infos = bucket.m_neighborhood_infos;
+ }
+
+ hopscotch_bucket& operator=(const hopscotch_bucket& bucket) noexcept(
+ std::is_nothrow_copy_constructible<value_type>::value) {
+ if (this != &bucket) {
+ remove_value();
+
+ bucket_hash::operator=(bucket);
+ if (!bucket.empty()) {
+ ::new (static_cast<void*>(std::addressof(m_value)))
+ value_type(bucket.value());
+ }
+
+ m_neighborhood_infos = bucket.m_neighborhood_infos;
+ }
+
+ return *this;
+ }
+
+ hopscotch_bucket& operator=(hopscotch_bucket&&) = delete;
+
+ ~hopscotch_bucket() noexcept {
+ if (!empty()) {
+ destroy_value();
+ }
+ }
+
+ neighborhood_bitmap neighborhood_infos() const noexcept {
+ return neighborhood_bitmap(m_neighborhood_infos >>
+ NB_RESERVED_BITS_IN_NEIGHBORHOOD);
+ }
+
+ void set_overflow(bool has_overflow) noexcept {
+ if (has_overflow) {
+ m_neighborhood_infos = neighborhood_bitmap(m_neighborhood_infos | 2);
+ } else {
+ m_neighborhood_infos = neighborhood_bitmap(m_neighborhood_infos & ~2);
+ }
+ }
+
+ bool has_overflow() const noexcept { return (m_neighborhood_infos & 2) != 0; }
+
+ bool empty() const noexcept { return (m_neighborhood_infos & 1) == 0; }
+
+ void toggle_neighbor_presence(std::size_t ineighbor) noexcept {
+ tsl_hh_assert(ineighbor <= NeighborhoodSize);
+ m_neighborhood_infos = neighborhood_bitmap(
+ m_neighborhood_infos ^
+ (1ull << (ineighbor + NB_RESERVED_BITS_IN_NEIGHBORHOOD)));
+ }
+
+ bool check_neighbor_presence(std::size_t ineighbor) const noexcept {
+ tsl_hh_assert(ineighbor <= NeighborhoodSize);
+ if (((m_neighborhood_infos >>
+ (ineighbor + NB_RESERVED_BITS_IN_NEIGHBORHOOD)) &
+ 1) == 1) {
+ return true;
+ }
+
+ return false;
+ }
+
+ value_type& value() noexcept {
+ tsl_hh_assert(!empty());
+ return *reinterpret_cast<value_type*>(std::addressof(m_value));
+ }
+
+ const value_type& value() const noexcept {
+ tsl_hh_assert(!empty());
+ return *reinterpret_cast<const value_type*>(std::addressof(m_value));
+ }
+
+ template <typename... Args>
+ void set_value_of_empty_bucket(truncated_hash_type hash,
+ Args&&... value_type_args) {
+ tsl_hh_assert(empty());
+
+ ::new (static_cast<void*>(std::addressof(m_value)))
+ value_type(std::forward<Args>(value_type_args)...);
+ set_empty(false);
+ this->set_hash(hash);
+ }
+
+ void swap_value_into_empty_bucket(hopscotch_bucket& empty_bucket) {
+ tsl_hh_assert(empty_bucket.empty());
+ if (!empty()) {
+ ::new (static_cast<void*>(std::addressof(empty_bucket.m_value)))
+ value_type(std::move(value()));
+ empty_bucket.copy_hash(*this);
+ empty_bucket.set_empty(false);
+
+ destroy_value();
+ set_empty(true);
+ }
+ }
+
+ void remove_value() noexcept {
+ if (!empty()) {
+ destroy_value();
+ set_empty(true);
+ }
+ }
+
+ void clear() noexcept {
+ if (!empty()) {
+ destroy_value();
+ }
+
+ m_neighborhood_infos = 0;
+ tsl_hh_assert(empty());
+ }
+
+ static truncated_hash_type truncate_hash(std::size_t hash) noexcept {
+ return truncated_hash_type(hash);
+ }
+
+ private:
+ void set_empty(bool is_empty) noexcept {
+ if (is_empty) {
+ m_neighborhood_infos = neighborhood_bitmap(m_neighborhood_infos & ~1);
+ } else {
+ m_neighborhood_infos = neighborhood_bitmap(m_neighborhood_infos | 1);
+ }
+ }
+
+ void destroy_value() noexcept {
+ tsl_hh_assert(!empty());
+ value().~value_type();
+ }
+
+ private:
+ using storage = typename std::aligned_storage<sizeof(value_type),
+ alignof(value_type)>::type;
+
+ neighborhood_bitmap m_neighborhood_infos;
+ storage m_value;
+};
+
+/**
+ * Internal common class used by (b)hopscotch_map and (b)hopscotch_set.
+ *
+ * ValueType is what will be stored by hopscotch_hash (usually std::pair<Key, T>
+ * for a map and Key for a set).
+ *
+ * KeySelect should be a FunctionObject which takes a ValueType in parameter and
+ * returns a reference to the key.
+ *
+ * ValueSelect should be a FunctionObject which takes a ValueType in parameter
+ * and returns a reference to the value. ValueSelect should be void if there is
+ * no value (in a set for example).
+ *
+ * OverflowContainer will be used as containers for overflown elements. Usually
+ * it should be a list<ValueType> or a set<Key>/map<Key, T>.
+ */
+template <class ValueType, class KeySelect, class ValueSelect, class Hash,
+ class KeyEqual, class Allocator, unsigned int NeighborhoodSize,
+ bool StoreHash, class GrowthPolicy, class OverflowContainer>
+class hopscotch_hash : private Hash, private KeyEqual, private GrowthPolicy {
+ private:
+ template <typename U>
+ using has_mapped_type =
+ typename std::integral_constant<bool, !std::is_same<U, void>::value>;
+
+ static_assert(
+ noexcept(std::declval<GrowthPolicy>().bucket_for_hash(std::size_t(0))),
+ "GrowthPolicy::bucket_for_hash must be noexcept.");
+ static_assert(noexcept(std::declval<GrowthPolicy>().clear()),
+ "GrowthPolicy::clear must be noexcept.");
+
+ public:
+ template <bool IsConst>
+ class hopscotch_iterator;
+
+ using key_type = typename KeySelect::key_type;
+ using value_type = ValueType;
+ using size_type = std::size_t;
+ using difference_type = std::ptrdiff_t;
+ using hasher = Hash;
+ using key_equal = KeyEqual;
+ using allocator_type = Allocator;
+ using reference = value_type&;
+ using const_reference = const value_type&;
+ using pointer = value_type*;
+ using const_pointer = const value_type*;
+ using iterator = hopscotch_iterator<false>;
+ using const_iterator = hopscotch_iterator<true>;
+
+ private:
+ using hopscotch_bucket =
+ tsl::detail_hopscotch_hash::hopscotch_bucket<ValueType, NeighborhoodSize,
+ StoreHash>;
+ using neighborhood_bitmap = typename hopscotch_bucket::neighborhood_bitmap;
+
+ using buckets_allocator = typename std::allocator_traits<
+ allocator_type>::template rebind_alloc<hopscotch_bucket>;
+ using buckets_container_type =
+ std::vector<hopscotch_bucket, buckets_allocator>;
+
+ using overflow_container_type = OverflowContainer;
+
+ static_assert(std::is_same<typename overflow_container_type::value_type,
+ ValueType>::value,
+ "OverflowContainer should have ValueType as type.");
+
+ static_assert(std::is_same<typename overflow_container_type::allocator_type,
+ Allocator>::value,
+ "Invalid allocator, not the same type as the value_type.");
+
+ using iterator_buckets = typename buckets_container_type::iterator;
+ using const_iterator_buckets =
+ typename buckets_container_type::const_iterator;
+
+ using iterator_overflow = typename overflow_container_type::iterator;
+ using const_iterator_overflow =
+ typename overflow_container_type::const_iterator;
+
+ public:
+ /**
+ * The `operator*()` and `operator->()` methods return a const reference and
+ * const pointer respectively to the stored value type.
+ *
+ * In case of a map, to get a modifiable reference to the value associated to
+ * a key (the `.second` in the stored pair), you have to call `value()`.
+ */
+ template <bool IsConst>
+ class hopscotch_iterator {
+ friend class hopscotch_hash;
+
+ private:
+ using iterator_bucket = typename std::conditional<
+ IsConst, typename hopscotch_hash::const_iterator_buckets,
+ typename hopscotch_hash::iterator_buckets>::type;
+ using iterator_overflow = typename std::conditional<
+ IsConst, typename hopscotch_hash::const_iterator_overflow,
+ typename hopscotch_hash::iterator_overflow>::type;
+
+ hopscotch_iterator(iterator_bucket buckets_iterator,
+ iterator_bucket buckets_end_iterator,
+ iterator_overflow overflow_iterator) noexcept
+ : m_buckets_iterator(buckets_iterator),
+ m_buckets_end_iterator(buckets_end_iterator),
+ m_overflow_iterator(overflow_iterator) {}
+
+ public:
+ using iterator_category = std::forward_iterator_tag;
+ using value_type = const typename hopscotch_hash::value_type;
+ using difference_type = std::ptrdiff_t;
+ using reference = value_type&;
+ using pointer = value_type*;
+
+ hopscotch_iterator() noexcept {}
+
+ // Copy constructor from iterator to const_iterator.
+ template <bool TIsConst = IsConst,
+ typename std::enable_if<TIsConst>::type* = nullptr>
+ hopscotch_iterator(const hopscotch_iterator<!TIsConst>& other) noexcept
+ : m_buckets_iterator(other.m_buckets_iterator),
+ m_buckets_end_iterator(other.m_buckets_end_iterator),
+ m_overflow_iterator(other.m_overflow_iterator) {}
+
+ hopscotch_iterator(const hopscotch_iterator& other) = default;
+ hopscotch_iterator(hopscotch_iterator&& other) = default;
+ hopscotch_iterator& operator=(const hopscotch_iterator& other) = default;
+ hopscotch_iterator& operator=(hopscotch_iterator&& other) = default;
+
+ const typename hopscotch_hash::key_type& key() const {
+ if (m_buckets_iterator != m_buckets_end_iterator) {
+ return KeySelect()(m_buckets_iterator->value());
+ }
+
+ return KeySelect()(*m_overflow_iterator);
+ }
+
+ template <
+ class U = ValueSelect,
+ typename std::enable_if<has_mapped_type<U>::value>::type* = nullptr>
+ typename std::conditional<IsConst, const typename U::value_type&,
+ typename U::value_type&>::type
+ value() const {
+ if (m_buckets_iterator != m_buckets_end_iterator) {
+ return U()(m_buckets_iterator->value());
+ }
+
+ return U()(*m_overflow_iterator);
+ }
+
+ reference operator*() const {
+ if (m_buckets_iterator != m_buckets_end_iterator) {
+ return m_buckets_iterator->value();
+ }
+
+ return *m_overflow_iterator;
+ }
+
+ pointer operator->() const {
+ if (m_buckets_iterator != m_buckets_end_iterator) {
+ return std::addressof(m_buckets_iterator->value());
+ }
+
+ return std::addressof(*m_overflow_iterator);
+ }
+
+ hopscotch_iterator& operator++() {
+ if (m_buckets_iterator == m_buckets_end_iterator) {
+ ++m_overflow_iterator;
+ return *this;
+ }
+
+ do {
+ ++m_buckets_iterator;
+ } while (m_buckets_iterator != m_buckets_end_iterator &&
+ m_buckets_iterator->empty());
+
+ return *this;
+ }
+
+ hopscotch_iterator operator++(int) {
+ hopscotch_iterator tmp(*this);
+ ++*this;
+
+ return tmp;
+ }
+
+ friend bool operator==(const hopscotch_iterator& lhs,
+ const hopscotch_iterator& rhs) {
+ return lhs.m_buckets_iterator == rhs.m_buckets_iterator &&
+ lhs.m_overflow_iterator == rhs.m_overflow_iterator;
+ }
+
+ friend bool operator!=(const hopscotch_iterator& lhs,
+ const hopscotch_iterator& rhs) {
+ return !(lhs == rhs);
+ }
+
+ private:
+ iterator_bucket m_buckets_iterator;
+ iterator_bucket m_buckets_end_iterator;
+ iterator_overflow m_overflow_iterator;
+ };
+
+ public:
+ template <
+ class OC = OverflowContainer,
+ typename std::enable_if<!has_key_compare<OC>::value>::type* = nullptr>
+ hopscotch_hash(size_type bucket_count, const Hash& hash,
+ const KeyEqual& equal, const Allocator& alloc,
+ float max_load_factor)
+ : Hash(hash),
+ KeyEqual(equal),
+ GrowthPolicy(bucket_count),
+ m_buckets_data(alloc),
+ m_overflow_elements(alloc),
+ m_buckets(static_empty_bucket_ptr()),
+ m_nb_elements(0) {
+ if (bucket_count > max_bucket_count()) {
+ TSL_HH_THROW_OR_TERMINATE(std::length_error,
+ "The map exceeds its maximum size.");
+ }
+
+ if (bucket_count > 0) {
+ static_assert(NeighborhoodSize - 1 > 0, "");
+
+ // Can't directly construct with the appropriate size in the initializer
+ // as m_buckets_data(bucket_count, alloc) is not supported by GCC 4.8
+ m_buckets_data.resize(bucket_count + NeighborhoodSize - 1);
+ m_buckets = m_buckets_data.data();
+ }
+
+ this->max_load_factor(max_load_factor);
+
+ // Check in the constructor instead of outside of a function to avoid
+ // compilation issues when value_type is not complete.
+ static_assert(std::is_nothrow_move_constructible<value_type>::value ||
+ std::is_copy_constructible<value_type>::value,
+ "value_type must be either copy constructible or nothrow "
+ "move constructible.");
+ }
+
+ template <
+ class OC = OverflowContainer,
+ typename std::enable_if<has_key_compare<OC>::value>::type* = nullptr>
+ hopscotch_hash(size_type bucket_count, const Hash& hash,
+ const KeyEqual& equal, const Allocator& alloc,
+ float max_load_factor, const typename OC::key_compare& comp)
+ : Hash(hash),
+ KeyEqual(equal),
+ GrowthPolicy(bucket_count),
+ m_buckets_data(alloc),
+ m_overflow_elements(comp, alloc),
+ m_buckets(static_empty_bucket_ptr()),
+ m_nb_elements(0) {
+ if (bucket_count > max_bucket_count()) {
+ TSL_HH_THROW_OR_TERMINATE(std::length_error,
+ "The map exceeds its maximum size.");
+ }
+
+ if (bucket_count > 0) {
+ static_assert(NeighborhoodSize - 1 > 0, "");
+
+ // Can't directly construct with the appropriate size in the initializer
+ // as m_buckets_data(bucket_count, alloc) is not supported by GCC 4.8
+ m_buckets_data.resize(bucket_count + NeighborhoodSize - 1);
+ m_buckets = m_buckets_data.data();
+ }
+
+ this->max_load_factor(max_load_factor);
+
+ // Check in the constructor instead of outside of a function to avoid
+ // compilation issues when value_type is not complete.
+ static_assert(std::is_nothrow_move_constructible<value_type>::value ||
+ std::is_copy_constructible<value_type>::value,
+ "value_type must be either copy constructible or nothrow "
+ "move constructible.");
+ }
+
+ hopscotch_hash(const hopscotch_hash& other)
+ : Hash(other),
+ KeyEqual(other),
+ GrowthPolicy(other),
+ m_buckets_data(other.m_buckets_data),
+ m_overflow_elements(other.m_overflow_elements),
+ m_buckets(m_buckets_data.empty() ? static_empty_bucket_ptr()
+ : m_buckets_data.data()),
+ m_nb_elements(other.m_nb_elements),
+ m_min_load_threshold_rehash(other.m_min_load_threshold_rehash),
+ m_max_load_threshold_rehash(other.m_max_load_threshold_rehash),
+ m_max_load_factor(other.m_max_load_factor) {}
+
+ hopscotch_hash(hopscotch_hash&& other) noexcept(
+ std::is_nothrow_move_constructible<Hash>::value&&
+ std::is_nothrow_move_constructible<KeyEqual>::value&&
+ std::is_nothrow_move_constructible<GrowthPolicy>::value&& std::
+ is_nothrow_move_constructible<buckets_container_type>::value&&
+ std::is_nothrow_move_constructible<
+ overflow_container_type>::value)
+ : Hash(std::move(static_cast<Hash&>(other))),
+ KeyEqual(std::move(static_cast<KeyEqual&>(other))),
+ GrowthPolicy(std::move(static_cast<GrowthPolicy&>(other))),
+ m_buckets_data(std::move(other.m_buckets_data)),
+ m_overflow_elements(std::move(other.m_overflow_elements)),
+ m_buckets(m_buckets_data.empty() ? static_empty_bucket_ptr()
+ : m_buckets_data.data()),
+ m_nb_elements(other.m_nb_elements),
+ m_min_load_threshold_rehash(other.m_min_load_threshold_rehash),
+ m_max_load_threshold_rehash(other.m_max_load_threshold_rehash),
+ m_max_load_factor(other.m_max_load_factor) {
+ other.GrowthPolicy::clear();
+ other.m_buckets_data.clear();
+ other.m_overflow_elements.clear();
+ other.m_buckets = static_empty_bucket_ptr();
+ other.m_nb_elements = 0;
+ other.m_min_load_threshold_rehash = 0;
+ other.m_max_load_threshold_rehash = 0;
+ }
+
+ hopscotch_hash& operator=(const hopscotch_hash& other) {
+ if (&other != this) {
+ Hash::operator=(other);
+ KeyEqual::operator=(other);
+ GrowthPolicy::operator=(other);
+
+ m_buckets_data = other.m_buckets_data;
+ m_overflow_elements = other.m_overflow_elements;
+ m_buckets = m_buckets_data.empty() ? static_empty_bucket_ptr()
+ : m_buckets_data.data();
+ m_nb_elements = other.m_nb_elements;
+
+ m_min_load_threshold_rehash = other.m_min_load_threshold_rehash;
+ m_max_load_threshold_rehash = other.m_max_load_threshold_rehash;
+ m_max_load_factor = other.m_max_load_factor;
+ }
+
+ return *this;
+ }
+
+ hopscotch_hash& operator=(hopscotch_hash&& other) {
+ other.swap(*this);
+ other.clear();
+
+ return *this;
+ }
+
+ allocator_type get_allocator() const {
+ return m_buckets_data.get_allocator();
+ }
+
+ /*
+ * Iterators
+ */
+ iterator begin() noexcept {
+ auto begin = m_buckets_data.begin();
+ while (begin != m_buckets_data.end() && begin->empty()) {
+ ++begin;
+ }
+
+ return iterator(begin, m_buckets_data.end(), m_overflow_elements.begin());
+ }
+
+ const_iterator begin() const noexcept { return cbegin(); }
+
+ const_iterator cbegin() const noexcept {
+ auto begin = m_buckets_data.cbegin();
+ while (begin != m_buckets_data.cend() && begin->empty()) {
+ ++begin;
+ }
+
+ return const_iterator(begin, m_buckets_data.cend(),
+ m_overflow_elements.cbegin());
+ }
+
+ iterator end() noexcept {
+ return iterator(m_buckets_data.end(), m_buckets_data.end(),
+ m_overflow_elements.end());
+ }
+
+ const_iterator end() const noexcept { return cend(); }
+
+ const_iterator cend() const noexcept {
+ return const_iterator(m_buckets_data.cend(), m_buckets_data.cend(),
+ m_overflow_elements.cend());
+ }
+
+ /*
+ * Capacity
+ */
+ bool empty() const noexcept { return m_nb_elements == 0; }
+
+ size_type size() const noexcept { return m_nb_elements; }
+
+ size_type max_size() const noexcept { return m_buckets_data.max_size(); }
+
+ /*
+ * Modifiers
+ */
+ void clear() noexcept {
+ for (auto& bucket : m_buckets_data) {
+ bucket.clear();
+ }
+
+ m_overflow_elements.clear();
+ m_nb_elements = 0;
+ }
+
+ std::pair<iterator, bool> insert(const value_type& value) {
+ return insert_impl(value);
+ }
+
+ template <class P, typename std::enable_if<std::is_constructible<
+ value_type, P&&>::value>::type* = nullptr>
+ std::pair<iterator, bool> insert(P&& value) {
+ return insert_impl(value_type(std::forward<P>(value)));
+ }
+
+ std::pair<iterator, bool> insert(value_type&& value) {
+ return insert_impl(std::move(value));
+ }
+
+ iterator insert(const_iterator hint, const value_type& value) {
+ if (hint != cend() &&
+ compare_keys(KeySelect()(*hint), KeySelect()(value))) {
+ return mutable_iterator(hint);
+ }
+
+ return insert(value).first;
+ }
+
+ template <class P, typename std::enable_if<std::is_constructible<
+ value_type, P&&>::value>::type* = nullptr>
+ iterator insert(const_iterator hint, P&& value) {
+ return emplace_hint(hint, std::forward<P>(value));
+ }
+
+ iterator insert(const_iterator hint, value_type&& value) {
+ if (hint != cend() &&
+ compare_keys(KeySelect()(*hint), KeySelect()(value))) {
+ return mutable_iterator(hint);
+ }
+
+ return insert(std::move(value)).first;
+ }
+
+ template <class InputIt>
+ void insert(InputIt first, InputIt last) {
+ if (std::is_base_of<
+ std::forward_iterator_tag,
+ typename std::iterator_traits<InputIt>::iterator_category>::value) {
+ const auto nb_elements_insert = std::distance(first, last);
+ const std::size_t nb_elements_in_buckets =
+ m_nb_elements - m_overflow_elements.size();
+ const std::size_t nb_free_buckets =
+ m_max_load_threshold_rehash - nb_elements_in_buckets;
+ tsl_hh_assert(m_nb_elements >= m_overflow_elements.size());
+ tsl_hh_assert(m_max_load_threshold_rehash >= nb_elements_in_buckets);
+
+ if (nb_elements_insert > 0 &&
+ nb_free_buckets < std::size_t(nb_elements_insert)) {
+ reserve(nb_elements_in_buckets + std::size_t(nb_elements_insert));
+ }
+ }
+
+ for (; first != last; ++first) {
+ insert(*first);
+ }
+ }
+
+ template <class M>
+ std::pair<iterator, bool> insert_or_assign(const key_type& k, M&& obj) {
+ return insert_or_assign_impl(k, std::forward<M>(obj));
+ }
+
+ template <class M>
+ std::pair<iterator, bool> insert_or_assign(key_type&& k, M&& obj) {
+ return insert_or_assign_impl(std::move(k), std::forward<M>(obj));
+ }
+
+ template <class M>
+ iterator insert_or_assign(const_iterator hint, const key_type& k, M&& obj) {
+ if (hint != cend() && compare_keys(KeySelect()(*hint), k)) {
+ auto it = mutable_iterator(hint);
+ it.value() = std::forward<M>(obj);
+
+ return it;
+ }
+
+ return insert_or_assign(k, std::forward<M>(obj)).first;
+ }
+
+ template <class M>
+ iterator insert_or_assign(const_iterator hint, key_type&& k, M&& obj) {
+ if (hint != cend() && compare_keys(KeySelect()(*hint), k)) {
+ auto it = mutable_iterator(hint);
+ it.value() = std::forward<M>(obj);
+
+ return it;
+ }
+
+ return insert_or_assign(std::move(k), std::forward<M>(obj)).first;
+ }
+
+ template <class... Args>
+ std::pair<iterator, bool> emplace(Args&&... args) {
+ return insert(value_type(std::forward<Args>(args)...));
+ }
+
+ template <class... Args>
+ iterator emplace_hint(const_iterator hint, Args&&... args) {
+ return insert(hint, value_type(std::forward<Args>(args)...));
+ }
+
+ template <class... Args>
+ std::pair<iterator, bool> try_emplace(const key_type& k, Args&&... args) {
+ return try_emplace_impl(k, std::forward<Args>(args)...);
+ }
+
+ template <class... Args>
+ std::pair<iterator, bool> try_emplace(key_type&& k, Args&&... args) {
+ return try_emplace_impl(std::move(k), std::forward<Args>(args)...);
+ }
+
+ template <class... Args>
+ iterator try_emplace(const_iterator hint, const key_type& k, Args&&... args) {
+ if (hint != cend() && compare_keys(KeySelect()(*hint), k)) {
+ return mutable_iterator(hint);
+ }
+
+ return try_emplace(k, std::forward<Args>(args)...).first;
+ }
+
+ template <class... Args>
+ iterator try_emplace(const_iterator hint, key_type&& k, Args&&... args) {
+ if (hint != cend() && compare_keys(KeySelect()(*hint), k)) {
+ return mutable_iterator(hint);
+ }
+
+ return try_emplace(std::move(k), std::forward<Args>(args)...).first;
+ }
+
+ /**
+ * Here to avoid `template<class K> size_type erase(const K& key)` being used
+ * when we use an iterator instead of a const_iterator.
+ */
+ iterator erase(iterator pos) { return erase(const_iterator(pos)); }
+
+ iterator erase(const_iterator pos) {
+ const std::size_t ibucket_for_hash = bucket_for_hash(hash_key(pos.key()));
+
+ if (pos.m_buckets_iterator != pos.m_buckets_end_iterator) {
+ auto it_bucket =
+ m_buckets_data.begin() +
+ std::distance(m_buckets_data.cbegin(), pos.m_buckets_iterator);
+ erase_from_bucket(*it_bucket, ibucket_for_hash);
+
+ return ++iterator(it_bucket, m_buckets_data.end(),
+ m_overflow_elements.begin());
+ } else {
+ auto it_next_overflow =
+ erase_from_overflow(pos.m_overflow_iterator, ibucket_for_hash);
+ return iterator(m_buckets_data.end(), m_buckets_data.end(),
+ it_next_overflow);
+ }
+ }
+
+ iterator erase(const_iterator first, const_iterator last) {
+ if (first == last) {
+ return mutable_iterator(first);
+ }
+
+ auto to_delete = erase(first);
+ while (to_delete != last) {
+ to_delete = erase(to_delete);
+ }
+
+ return to_delete;
+ }
+
+ template <class K>
+ size_type erase(const K& key) {
+ return erase(key, hash_key(key));
+ }
+
+ template <class K>
+ size_type erase(const K& key, std::size_t hash) {
+ const std::size_t ibucket_for_hash = bucket_for_hash(hash);
+
+ hopscotch_bucket* bucket_found =
+ find_in_buckets(key, hash, m_buckets + ibucket_for_hash);
+ if (bucket_found != nullptr) {
+ erase_from_bucket(*bucket_found, ibucket_for_hash);
+
+ return 1;
+ }
+
+ if (m_buckets[ibucket_for_hash].has_overflow()) {
+ auto it_overflow = find_in_overflow(key);
+ if (it_overflow != m_overflow_elements.end()) {
+ erase_from_overflow(it_overflow, ibucket_for_hash);
+
+ return 1;
+ }
+ }
+
+ return 0;
+ }
+
+ void swap(hopscotch_hash& other) {
+ using std::swap;
+
+ swap(static_cast<Hash&>(*this), static_cast<Hash&>(other));
+ swap(static_cast<KeyEqual&>(*this), static_cast<KeyEqual&>(other));
+ swap(static_cast<GrowthPolicy&>(*this), static_cast<GrowthPolicy&>(other));
+ swap(m_buckets_data, other.m_buckets_data);
+ swap(m_overflow_elements, other.m_overflow_elements);
+ swap(m_buckets, other.m_buckets);
+ swap(m_nb_elements, other.m_nb_elements);
+ swap(m_min_load_threshold_rehash, other.m_min_load_threshold_rehash);
+ swap(m_max_load_threshold_rehash, other.m_max_load_threshold_rehash);
+ swap(m_max_load_factor, other.m_max_load_factor);
+ }
+
+ /*
+ * Lookup
+ */
+ template <class K, class U = ValueSelect,
+ typename std::enable_if<has_mapped_type<U>::value>::type* = nullptr>
+ typename U::value_type& at(const K& key) {
+ return at(key, hash_key(key));
+ }
+
+ template <class K, class U = ValueSelect,
+ typename std::enable_if<has_mapped_type<U>::value>::type* = nullptr>
+ typename U::value_type& at(const K& key, std::size_t hash) {
+ return const_cast<typename U::value_type&>(
+ static_cast<const hopscotch_hash*>(this)->at(key, hash));
+ }
+
+ template <class K, class U = ValueSelect,
+ typename std::enable_if<has_mapped_type<U>::value>::type* = nullptr>
+ const typename U::value_type& at(const K& key) const {
+ return at(key, hash_key(key));
+ }
+
+ template <class K, class U = ValueSelect,
+ typename std::enable_if<has_mapped_type<U>::value>::type* = nullptr>
+ const typename U::value_type& at(const K& key, std::size_t hash) const {
+ using T = typename U::value_type;
+
+ const T* value =
+ find_value_impl(key, hash, m_buckets + bucket_for_hash(hash));
+ if (value == nullptr) {
+ TSL_HH_THROW_OR_TERMINATE(std::out_of_range, "Couldn't find key.");
+ } else {
+ return *value;
+ }
+ }
+
+ template <class K, class U = ValueSelect,
+ typename std::enable_if<has_mapped_type<U>::value>::type* = nullptr>
+ typename U::value_type& operator[](K&& key) {
+ using T = typename U::value_type;
+
+ const std::size_t hash = hash_key(key);
+ const std::size_t ibucket_for_hash = bucket_for_hash(hash);
+
+ T* value = find_value_impl(key, hash, m_buckets + ibucket_for_hash);
+ if (value != nullptr) {
+ return *value;
+ } else {
+ return insert_value(ibucket_for_hash, hash, std::piecewise_construct,
+ std::forward_as_tuple(std::forward<K>(key)),
+ std::forward_as_tuple())
+ .first.value();
+ }
+ }
+
+ template <class K>
+ size_type count(const K& key) const {
+ return count(key, hash_key(key));
+ }
+
+ template <class K>
+ size_type count(const K& key, std::size_t hash) const {
+ return count_impl(key, hash, m_buckets + bucket_for_hash(hash));
+ }
+
+ template <class K>
+ iterator find(const K& key) {
+ return find(key, hash_key(key));
+ }
+
+ template <class K>
+ iterator find(const K& key, std::size_t hash) {
+ return find_impl(key, hash, m_buckets + bucket_for_hash(hash));
+ }
+
+ template <class K>
+ const_iterator find(const K& key) const {
+ return find(key, hash_key(key));
+ }
+
+ template <class K>
+ const_iterator find(const K& key, std::size_t hash) const {
+ return find_impl(key, hash, m_buckets + bucket_for_hash(hash));
+ }
+
+ template <class K>
+ bool contains(const K& key) const {
+ return contains(key, hash_key(key));
+ }
+
+ template <class K>
+ bool contains(const K& key, std::size_t hash) const {
+ return count(key, hash) != 0;
+ }
+
+ template <class K>
+ std::pair<iterator, iterator> equal_range(const K& key) {
+ return equal_range(key, hash_key(key));
+ }
+
+ template <class K>
+ std::pair<iterator, iterator> equal_range(const K& key, std::size_t hash) {
+ iterator it = find(key, hash);
+ return std::make_pair(it, (it == end()) ? it : std::next(it));
+ }
+
+ template <class K>
+ std::pair<const_iterator, const_iterator> equal_range(const K& key) const {
+ return equal_range(key, hash_key(key));
+ }
+
+ template <class K>
+ std::pair<const_iterator, const_iterator> equal_range(
+ const K& key, std::size_t hash) const {
+ const_iterator it = find(key, hash);
+ return std::make_pair(it, (it == cend()) ? it : std::next(it));
+ }
+
+ /*
+ * Bucket interface
+ */
+ size_type bucket_count() const {
+ /*
+ * So that the last bucket can have NeighborhoodSize neighbors, the size of
+ * the bucket array is a little bigger than the real number of buckets when
+ * not empty. We could use some of the buckets at the beginning, but it is
+ * faster this way as we avoid extra checks.
+ */
+ if (m_buckets_data.empty()) {
+ return 0;
+ }
+
+ return m_buckets_data.size() - NeighborhoodSize + 1;
+ }
+
+ size_type max_bucket_count() const {
+ const std::size_t max_bucket_count =
+ std::min(GrowthPolicy::max_bucket_count(), m_buckets_data.max_size());
+ return max_bucket_count - NeighborhoodSize + 1;
+ }
+
+ /*
+ * Hash policy
+ */
+ float load_factor() const {
+ if (bucket_count() == 0) {
+ return 0;
+ }
+
+ return float(m_nb_elements) / float(bucket_count());
+ }
+
+ float max_load_factor() const { return m_max_load_factor; }
+
+ void max_load_factor(float ml) {
+ m_max_load_factor = std::max(0.1f, std::min(ml, 0.95f));
+ m_min_load_threshold_rehash =
+ size_type(float(bucket_count()) * MIN_LOAD_FACTOR_FOR_REHASH);
+ m_max_load_threshold_rehash =
+ size_type(float(bucket_count()) * m_max_load_factor);
+ }
+
+ void rehash(size_type count_) {
+ count_ = std::max(count_,
+ size_type(std::ceil(float(size()) / max_load_factor())));
+ rehash_impl(count_);
+ }
+
+ void reserve(size_type count_) {
+ rehash(size_type(std::ceil(float(count_) / max_load_factor())));
+ }
+
+ /*
+ * Observers
+ */
+ hasher hash_function() const { return static_cast<const Hash&>(*this); }
+
+ key_equal key_eq() const { return static_cast<const KeyEqual&>(*this); }
+
+ /*
+ * Other
+ */
+ iterator mutable_iterator(const_iterator pos) {
+ if (pos.m_buckets_iterator != pos.m_buckets_end_iterator) {
+ // Get a non-const iterator
+ auto it = m_buckets_data.begin() +
+ std::distance(m_buckets_data.cbegin(), pos.m_buckets_iterator);
+ return iterator(it, m_buckets_data.end(), m_overflow_elements.begin());
+ } else {
+ // Get a non-const iterator
+ auto it = mutable_overflow_iterator(pos.m_overflow_iterator);
+ return iterator(m_buckets_data.end(), m_buckets_data.end(), it);
+ }
+ }
+
+ size_type overflow_size() const noexcept {
+ return m_overflow_elements.size();
+ }
+
+ template <class U = OverflowContainer,
+ typename std::enable_if<has_key_compare<U>::value>::type* = nullptr>
+ typename U::key_compare key_comp() const {
+ return m_overflow_elements.key_comp();
+ }
+
+ private:
+ template <class K>
+ std::size_t hash_key(const K& key) const {
+ return Hash::operator()(key);
+ }
+
+ template <class K1, class K2>
+ bool compare_keys(const K1& key1, const K2& key2) const {
+ return KeyEqual::operator()(key1, key2);
+ }
+
+ std::size_t bucket_for_hash(std::size_t hash) const {
+ const std::size_t bucket = GrowthPolicy::bucket_for_hash(hash);
+ tsl_hh_assert(bucket < m_buckets_data.size() ||
+ (bucket == 0 && m_buckets_data.empty()));
+
+ return bucket;
+ }
+
+ template <typename U = value_type,
+ typename std::enable_if<
+ std::is_nothrow_move_constructible<U>::value>::type* = nullptr>
+ void rehash_impl(size_type count_) {
+ hopscotch_hash new_map = new_hopscotch_hash(count_);
+
+ if (!m_overflow_elements.empty()) {
+ new_map.m_overflow_elements.swap(m_overflow_elements);
+ new_map.m_nb_elements += new_map.m_overflow_elements.size();
+
+ for (const value_type& value : new_map.m_overflow_elements) {
+ const std::size_t ibucket_for_hash =
+ new_map.bucket_for_hash(new_map.hash_key(KeySelect()(value)));
+ new_map.m_buckets[ibucket_for_hash].set_overflow(true);
+ }
+ }
+
+#ifndef TSL_HH_NO_EXCEPTIONS
+ try {
+#endif
+ const bool use_stored_hash =
+ USE_STORED_HASH_ON_REHASH(new_map.bucket_count());
+ for (auto it_bucket = m_buckets_data.begin();
+ it_bucket != m_buckets_data.end(); ++it_bucket) {
+ if (it_bucket->empty()) {
+ continue;
+ }
+
+ const std::size_t hash =
+ use_stored_hash ? it_bucket->truncated_bucket_hash()
+ : new_map.hash_key(KeySelect()(it_bucket->value()));
+ const std::size_t ibucket_for_hash = new_map.bucket_for_hash(hash);
+
+ new_map.insert_value(ibucket_for_hash, hash,
+ std::move(it_bucket->value()));
+
+ erase_from_bucket(*it_bucket, bucket_for_hash(hash));
+ }
+#ifndef TSL_HH_NO_EXCEPTIONS
+ }
+ /*
+ * The call to insert_value may throw an exception if an element is added to
+ * the overflow list and the memory allocation fails. Rollback the elements
+ * in this case.
+ */
+ catch (...) {
+ m_overflow_elements.swap(new_map.m_overflow_elements);
+
+ const bool use_stored_hash =
+ USE_STORED_HASH_ON_REHASH(new_map.bucket_count());
+ for (auto it_bucket = new_map.m_buckets_data.begin();
+ it_bucket != new_map.m_buckets_data.end(); ++it_bucket) {
+ if (it_bucket->empty()) {
+ continue;
+ }
+
+ const std::size_t hash =
+ use_stored_hash ? it_bucket->truncated_bucket_hash()
+ : hash_key(KeySelect()(it_bucket->value()));
+ const std::size_t ibucket_for_hash = bucket_for_hash(hash);
+
+ // The elements we insert were not in the overflow list before the
+ // switch. They will not be go in the overflow list if we rollback the
+ // switch.
+ insert_value(ibucket_for_hash, hash, std::move(it_bucket->value()));
+ }
+
+ throw;
+ }
+#endif
+
+ new_map.swap(*this);
+ }
+
+ template <typename U = value_type,
+ typename std::enable_if<
+ std::is_copy_constructible<U>::value &&
+ !std::is_nothrow_move_constructible<U>::value>::type* = nullptr>
+ void rehash_impl(size_type count_) {
+ hopscotch_hash new_map = new_hopscotch_hash(count_);
+
+ const bool use_stored_hash =
+ USE_STORED_HASH_ON_REHASH(new_map.bucket_count());
+ for (const hopscotch_bucket& bucket : m_buckets_data) {
+ if (bucket.empty()) {
+ continue;
+ }
+
+ const std::size_t hash =
+ use_stored_hash ? bucket.truncated_bucket_hash()
+ : new_map.hash_key(KeySelect()(bucket.value()));
+ const std::size_t ibucket_for_hash = new_map.bucket_for_hash(hash);
+
+ new_map.insert_value(ibucket_for_hash, hash, bucket.value());
+ }
+
+ for (const value_type& value : m_overflow_elements) {
+ const std::size_t hash = new_map.hash_key(KeySelect()(value));
+ const std::size_t ibucket_for_hash = new_map.bucket_for_hash(hash);
+
+ new_map.insert_value(ibucket_for_hash, hash, value);
+ }
+
+ new_map.swap(*this);
+ }
+
+#ifdef TSL_HH_NO_RANGE_ERASE_WITH_CONST_ITERATOR
+ iterator_overflow mutable_overflow_iterator(const_iterator_overflow it) {
+ return std::next(m_overflow_elements.begin(),
+ std::distance(m_overflow_elements.cbegin(), it));
+ }
+#else
+ iterator_overflow mutable_overflow_iterator(const_iterator_overflow it) {
+ return m_overflow_elements.erase(it, it);
+ }
+#endif
+
+ // iterator is in overflow list
+ iterator_overflow erase_from_overflow(const_iterator_overflow pos,
+ std::size_t ibucket_for_hash) {
+#ifdef TSL_HH_NO_RANGE_ERASE_WITH_CONST_ITERATOR
+ auto it_next = m_overflow_elements.erase(mutable_overflow_iterator(pos));
+#else
+ auto it_next = m_overflow_elements.erase(pos);
+#endif
+ m_nb_elements--;
+
+ // Check if we can remove the overflow flag
+ tsl_hh_assert(m_buckets[ibucket_for_hash].has_overflow());
+ for (const value_type& value : m_overflow_elements) {
+ const std::size_t bucket_for_value =
+ bucket_for_hash(hash_key(KeySelect()(value)));
+ if (bucket_for_value == ibucket_for_hash) {
+ return it_next;
+ }
+ }
+
+ m_buckets[ibucket_for_hash].set_overflow(false);
+ return it_next;
+ }
+
+ /**
+ * bucket_for_value is the bucket in which the value is.
+ * ibucket_for_hash is the bucket where the value belongs.
+ */
+ void erase_from_bucket(hopscotch_bucket& bucket_for_value,
+ std::size_t ibucket_for_hash) noexcept {
+ const std::size_t ibucket_for_value =
+ std::distance(m_buckets_data.data(), &bucket_for_value);
+ tsl_hh_assert(ibucket_for_value >= ibucket_for_hash);
+
+ bucket_for_value.remove_value();
+ m_buckets[ibucket_for_hash].toggle_neighbor_presence(ibucket_for_value -
+ ibucket_for_hash);
+ m_nb_elements--;
+ }
+
+ template <class K, class M>
+ std::pair<iterator, bool> insert_or_assign_impl(K&& key, M&& obj) {
+ auto it = try_emplace_impl(std::forward<K>(key), std::forward<M>(obj));
+ if (!it.second) {
+ it.first.value() = std::forward<M>(obj);
+ }
+
+ return it;
+ }
+
+ template <typename P, class... Args>
+ std::pair<iterator, bool> try_emplace_impl(P&& key, Args&&... args_value) {
+ const std::size_t hash = hash_key(key);
+ const std::size_t ibucket_for_hash = bucket_for_hash(hash);
+
+ // Check if already presents
+ auto it_find = find_impl(key, hash, m_buckets + ibucket_for_hash);
+ if (it_find != end()) {
+ return std::make_pair(it_find, false);
+ }
+
+ return insert_value(
+ ibucket_for_hash, hash, std::piecewise_construct,
+ std::forward_as_tuple(std::forward<P>(key)),
+ std::forward_as_tuple(std::forward<Args>(args_value)...));
+ }
+
+ template <typename P>
+ std::pair<iterator, bool> insert_impl(P&& value) {
+ const std::size_t hash = hash_key(KeySelect()(value));
+ const std::size_t ibucket_for_hash = bucket_for_hash(hash);
+
+ // Check if already presents
+ auto it_find =
+ find_impl(KeySelect()(value), hash, m_buckets + ibucket_for_hash);
+ if (it_find != end()) {
+ return std::make_pair(it_find, false);
+ }
+
+ return insert_value(ibucket_for_hash, hash, std::forward<P>(value));
+ }
+
+ template <typename... Args>
+ std::pair<iterator, bool> insert_value(std::size_t ibucket_for_hash,
+ std::size_t hash,
+ Args&&... value_type_args) {
+ if ((m_nb_elements - m_overflow_elements.size()) >=
+ m_max_load_threshold_rehash) {
+ rehash(GrowthPolicy::next_bucket_count());
+ ibucket_for_hash = bucket_for_hash(hash);
+ }
+
+ std::size_t ibucket_empty = find_empty_bucket(ibucket_for_hash);
+ if (ibucket_empty < m_buckets_data.size()) {
+ do {
+ tsl_hh_assert(ibucket_empty >= ibucket_for_hash);
+
+ // Empty bucket is in range of NeighborhoodSize, use it
+ if (ibucket_empty - ibucket_for_hash < NeighborhoodSize) {
+ auto it = insert_in_bucket(ibucket_empty, ibucket_for_hash, hash,
+ std::forward<Args>(value_type_args)...);
+ return std::make_pair(
+ iterator(it, m_buckets_data.end(), m_overflow_elements.begin()),
+ true);
+ }
+ }
+ // else, try to swap values to get a closer empty bucket
+ while (swap_empty_bucket_closer(ibucket_empty));
+ }
+
+ // Load factor is too low or a rehash will not change the neighborhood, put
+ // the value in overflow list
+ if (size() < m_min_load_threshold_rehash ||
+ !will_neighborhood_change_on_rehash(ibucket_for_hash)) {
+ auto it = insert_in_overflow(ibucket_for_hash,
+ std::forward<Args>(value_type_args)...);
+ return std::make_pair(
+ iterator(m_buckets_data.end(), m_buckets_data.end(), it), true);
+ }
+
+ rehash(GrowthPolicy::next_bucket_count());
+ ibucket_for_hash = bucket_for_hash(hash);
+
+ return insert_value(ibucket_for_hash, hash,
+ std::forward<Args>(value_type_args)...);
+ }
+
+ /*
+ * Return true if a rehash will change the position of a key-value in the
+ * neighborhood of ibucket_neighborhood_check. In this case a rehash is needed
+ * instead of puting the value in overflow list.
+ */
+ bool will_neighborhood_change_on_rehash(
+ size_t ibucket_neighborhood_check) const {
+ std::size_t expand_bucket_count = GrowthPolicy::next_bucket_count();
+ GrowthPolicy expand_growth_policy(expand_bucket_count);
+
+ const bool use_stored_hash = USE_STORED_HASH_ON_REHASH(expand_bucket_count);
+ for (size_t ibucket = ibucket_neighborhood_check;
+ ibucket < m_buckets_data.size() &&
+ (ibucket - ibucket_neighborhood_check) < NeighborhoodSize;
+ ++ibucket) {
+ tsl_hh_assert(!m_buckets[ibucket].empty());
+
+ const size_t hash =
+ use_stored_hash ? m_buckets[ibucket].truncated_bucket_hash()
+ : hash_key(KeySelect()(m_buckets[ibucket].value()));
+ if (bucket_for_hash(hash) != expand_growth_policy.bucket_for_hash(hash)) {
+ return true;
+ }
+ }
+
+ return false;
+ }
+
+ /*
+ * Return the index of an empty bucket in m_buckets_data.
+ * If none, the returned index equals m_buckets_data.size()
+ */
+ std::size_t find_empty_bucket(std::size_t ibucket_start) const {
+ const std::size_t limit = std::min(
+ ibucket_start + MAX_PROBES_FOR_EMPTY_BUCKET, m_buckets_data.size());
+ for (; ibucket_start < limit; ibucket_start++) {
+ if (m_buckets[ibucket_start].empty()) {
+ return ibucket_start;
+ }
+ }
+
+ return m_buckets_data.size();
+ }
+
+ /*
+ * Insert value in ibucket_empty where value originally belongs to
+ * ibucket_for_hash
+ *
+ * Return bucket iterator to ibucket_empty
+ */
+ template <typename... Args>
+ iterator_buckets insert_in_bucket(std::size_t ibucket_empty,
+ std::size_t ibucket_for_hash,
+ std::size_t hash,
+ Args&&... value_type_args) {
+ tsl_hh_assert(ibucket_empty >= ibucket_for_hash);
+ tsl_hh_assert(m_buckets[ibucket_empty].empty());
+ m_buckets[ibucket_empty].set_value_of_empty_bucket(
+ hopscotch_bucket::truncate_hash(hash),
+ std::forward<Args>(value_type_args)...);
+
+ tsl_hh_assert(!m_buckets[ibucket_for_hash].empty());
+ m_buckets[ibucket_for_hash].toggle_neighbor_presence(ibucket_empty -
+ ibucket_for_hash);
+ m_nb_elements++;
+
+ return m_buckets_data.begin() + ibucket_empty;
+ }
+
+ template <
+ class... Args, class U = OverflowContainer,
+ typename std::enable_if<!has_key_compare<U>::value>::type* = nullptr>
+ iterator_overflow insert_in_overflow(std::size_t ibucket_for_hash,
+ Args&&... value_type_args) {
+ auto it = m_overflow_elements.emplace(
+ m_overflow_elements.end(), std::forward<Args>(value_type_args)...);
+
+ m_buckets[ibucket_for_hash].set_overflow(true);
+ m_nb_elements++;
+
+ return it;
+ }
+
+ template <class... Args, class U = OverflowContainer,
+ typename std::enable_if<has_key_compare<U>::value>::type* = nullptr>
+ iterator_overflow insert_in_overflow(std::size_t ibucket_for_hash,
+ Args&&... value_type_args) {
+ auto it =
+ m_overflow_elements.emplace(std::forward<Args>(value_type_args)...)
+ .first;
+
+ m_buckets[ibucket_for_hash].set_overflow(true);
+ m_nb_elements++;
+
+ return it;
+ }
+
+ /*
+ * Try to swap the bucket ibucket_empty_in_out with a bucket preceding it
+ * while keeping the neighborhood conditions correct.
+ *
+ * If a swap was possible, the position of ibucket_empty_in_out will be closer
+ * to 0 and true will re returned.
+ */
+ bool swap_empty_bucket_closer(std::size_t& ibucket_empty_in_out) {
+ tsl_hh_assert(ibucket_empty_in_out >= NeighborhoodSize);
+ const std::size_t neighborhood_start =
+ ibucket_empty_in_out - NeighborhoodSize + 1;
+
+ for (std::size_t to_check = neighborhood_start;
+ to_check < ibucket_empty_in_out; to_check++) {
+ neighborhood_bitmap neighborhood_infos =
+ m_buckets[to_check].neighborhood_infos();
+ std::size_t to_swap = to_check;
+
+ while (neighborhood_infos != 0 && to_swap < ibucket_empty_in_out) {
+ if ((neighborhood_infos & 1) == 1) {
+ tsl_hh_assert(m_buckets[ibucket_empty_in_out].empty());
+ tsl_hh_assert(!m_buckets[to_swap].empty());
+
+ m_buckets[to_swap].swap_value_into_empty_bucket(
+ m_buckets[ibucket_empty_in_out]);
+
+ tsl_hh_assert(!m_buckets[to_check].check_neighbor_presence(
+ ibucket_empty_in_out - to_check));
+ tsl_hh_assert(
+ m_buckets[to_check].check_neighbor_presence(to_swap - to_check));
+
+ m_buckets[to_check].toggle_neighbor_presence(ibucket_empty_in_out -
+ to_check);
+ m_buckets[to_check].toggle_neighbor_presence(to_swap - to_check);
+
+ ibucket_empty_in_out = to_swap;
+
+ return true;
+ }
+
+ to_swap++;
+ neighborhood_infos = neighborhood_bitmap(neighborhood_infos >> 1);
+ }
+ }
+
+ return false;
+ }
+
+ template <class K, class U = ValueSelect,
+ typename std::enable_if<has_mapped_type<U>::value>::type* = nullptr>
+ typename U::value_type* find_value_impl(const K& key, std::size_t hash,
+ hopscotch_bucket* bucket_for_hash) {
+ return const_cast<typename U::value_type*>(
+ static_cast<const hopscotch_hash*>(this)->find_value_impl(
+ key, hash, bucket_for_hash));
+ }
+
+ /*
+ * Avoid the creation of an iterator to just get the value for operator[] and
+ * at() in maps. Faster this way.
+ *
+ * Return null if no value for the key (TODO use std::optional when
+ * available).
+ */
+ template <class K, class U = ValueSelect,
+ typename std::enable_if<has_mapped_type<U>::value>::type* = nullptr>
+ const typename U::value_type* find_value_impl(
+ const K& key, std::size_t hash,
+ const hopscotch_bucket* bucket_for_hash) const {
+ const hopscotch_bucket* bucket_found =
+ find_in_buckets(key, hash, bucket_for_hash);
+ if (bucket_found != nullptr) {
+ return std::addressof(ValueSelect()(bucket_found->value()));
+ }
+
+ if (bucket_for_hash->has_overflow()) {
+ auto it_overflow = find_in_overflow(key);
+ if (it_overflow != m_overflow_elements.end()) {
+ return std::addressof(ValueSelect()(*it_overflow));
+ }
+ }
+
+ return nullptr;
+ }
+
+ template <class K>
+ size_type count_impl(const K& key, std::size_t hash,
+ const hopscotch_bucket* bucket_for_hash) const {
+ if (find_in_buckets(key, hash, bucket_for_hash) != nullptr) {
+ return 1;
+ } else if (bucket_for_hash->has_overflow() &&
+ find_in_overflow(key) != m_overflow_elements.cend()) {
+ return 1;
+ } else {
+ return 0;
+ }
+ }
+
+ template <class K>
+ iterator find_impl(const K& key, std::size_t hash,
+ hopscotch_bucket* bucket_for_hash) {
+ hopscotch_bucket* bucket_found =
+ find_in_buckets(key, hash, bucket_for_hash);
+ if (bucket_found != nullptr) {
+ return iterator(m_buckets_data.begin() +
+ std::distance(m_buckets_data.data(), bucket_found),
+ m_buckets_data.end(), m_overflow_elements.begin());
+ }
+
+ if (!bucket_for_hash->has_overflow()) {
+ return end();
+ }
+
+ return iterator(m_buckets_data.end(), m_buckets_data.end(),
+ find_in_overflow(key));
+ }
+
+ template <class K>
+ const_iterator find_impl(const K& key, std::size_t hash,
+ const hopscotch_bucket* bucket_for_hash) const {
+ const hopscotch_bucket* bucket_found =
+ find_in_buckets(key, hash, bucket_for_hash);
+ if (bucket_found != nullptr) {
+ return const_iterator(
+ m_buckets_data.cbegin() +
+ std::distance(m_buckets_data.data(), bucket_found),
+ m_buckets_data.cend(), m_overflow_elements.cbegin());
+ }
+
+ if (!bucket_for_hash->has_overflow()) {
+ return cend();
+ }
+
+ return const_iterator(m_buckets_data.cend(), m_buckets_data.cend(),
+ find_in_overflow(key));
+ }
+
+ template <class K>
+ hopscotch_bucket* find_in_buckets(const K& key, std::size_t hash,
+ hopscotch_bucket* bucket_for_hash) {
+ const hopscotch_bucket* bucket_found =
+ static_cast<const hopscotch_hash*>(this)->find_in_buckets(
+ key, hash, bucket_for_hash);
+ return const_cast<hopscotch_bucket*>(bucket_found);
+ }
+
+ /**
+ * Return a pointer to the bucket which has the value, nullptr otherwise.
+ */
+ template <class K>
+ const hopscotch_bucket* find_in_buckets(
+ const K& key, std::size_t hash,
+ const hopscotch_bucket* bucket_for_hash) const {
+ (void)hash; // Avoid warning of unused variable when StoreHash is false;
+
+ // TODO Try to optimize the function.
+ // I tried to use ffs and __builtin_ffs functions but I could not reduce
+ // the time the function takes with -march=native
+
+ neighborhood_bitmap neighborhood_infos =
+ bucket_for_hash->neighborhood_infos();
+ while (neighborhood_infos != 0) {
+ if ((neighborhood_infos & 1) == 1) {
+ // Check StoreHash before calling bucket_hash_equal. Functionally it
+ // doesn't change anythin. If StoreHash is false, bucket_hash_equal is a
+ // no-op. Avoiding the call is there to help GCC optimizes `hash`
+ // parameter away, it seems to not be able to do without this hint.
+ if ((!StoreHash || bucket_for_hash->bucket_hash_equal(hash)) &&
+ compare_keys(KeySelect()(bucket_for_hash->value()), key)) {
+ return bucket_for_hash;
+ }
+ }
+
+ ++bucket_for_hash;
+ neighborhood_infos = neighborhood_bitmap(neighborhood_infos >> 1);
+ }
+
+ return nullptr;
+ }
+
+ template <
+ class K, class U = OverflowContainer,
+ typename std::enable_if<!has_key_compare<U>::value>::type* = nullptr>
+ iterator_overflow find_in_overflow(const K& key) {
+ return std::find_if(m_overflow_elements.begin(), m_overflow_elements.end(),
+ [&](const value_type& value) {
+ return compare_keys(key, KeySelect()(value));
+ });
+ }
+
+ template <
+ class K, class U = OverflowContainer,
+ typename std::enable_if<!has_key_compare<U>::value>::type* = nullptr>
+ const_iterator_overflow find_in_overflow(const K& key) const {
+ return std::find_if(m_overflow_elements.cbegin(),
+ m_overflow_elements.cend(),
+ [&](const value_type& value) {
+ return compare_keys(key, KeySelect()(value));
+ });
+ }
+
+ template <class K, class U = OverflowContainer,
+ typename std::enable_if<has_key_compare<U>::value>::type* = nullptr>
+ iterator_overflow find_in_overflow(const K& key) {
+ return m_overflow_elements.find(key);
+ }
+
+ template <class K, class U = OverflowContainer,
+ typename std::enable_if<has_key_compare<U>::value>::type* = nullptr>
+ const_iterator_overflow find_in_overflow(const K& key) const {
+ return m_overflow_elements.find(key);
+ }
+
+ template <
+ class U = OverflowContainer,
+ typename std::enable_if<!has_key_compare<U>::value>::type* = nullptr>
+ hopscotch_hash new_hopscotch_hash(size_type bucket_count) {
+ return hopscotch_hash(bucket_count, static_cast<Hash&>(*this),
+ static_cast<KeyEqual&>(*this), get_allocator(),
+ m_max_load_factor);
+ }
+
+ template <class U = OverflowContainer,
+ typename std::enable_if<has_key_compare<U>::value>::type* = nullptr>
+ hopscotch_hash new_hopscotch_hash(size_type bucket_count) {
+ return hopscotch_hash(bucket_count, static_cast<Hash&>(*this),
+ static_cast<KeyEqual&>(*this), get_allocator(),
+ m_max_load_factor, m_overflow_elements.key_comp());
+ }
+
+ public:
+ static const size_type DEFAULT_INIT_BUCKETS_SIZE = 0;
+ static constexpr float DEFAULT_MAX_LOAD_FACTOR =
+ (NeighborhoodSize <= 30) ? 0.8f : 0.9f;
+
+ private:
+ static const std::size_t MAX_PROBES_FOR_EMPTY_BUCKET = 12 * NeighborhoodSize;
+ static constexpr float MIN_LOAD_FACTOR_FOR_REHASH = 0.1f;
+
+ /**
+ * We can only use the hash on rehash if the size of the hash type is the same
+ * as the stored one or if we use a power of two modulo. In the case of the
+ * power of two modulo, we just mask the least significant bytes, we just have
+ * to check that the truncated_hash_type didn't truncated too much bytes.
+ */
+ template <class T = size_type,
+ typename std::enable_if<
+ std::is_same<T, truncated_hash_type>::value>::type* = nullptr>
+ static bool USE_STORED_HASH_ON_REHASH(size_type /*bucket_count*/) {
+ return StoreHash;
+ }
+
+ template <class T = size_type,
+ typename std::enable_if<
+ !std::is_same<T, truncated_hash_type>::value>::type* = nullptr>
+ static bool USE_STORED_HASH_ON_REHASH(size_type bucket_count) {
+ (void)bucket_count;
+ if (StoreHash && is_power_of_two_policy<GrowthPolicy>::value) {
+ tsl_hh_assert(bucket_count > 0);
+ return (bucket_count - 1) <=
+ std::numeric_limits<truncated_hash_type>::max();
+ } else {
+ return false;
+ }
+ }
+
+ /**
+ * Return an always valid pointer to an static empty hopscotch_bucket.
+ */
+ hopscotch_bucket* static_empty_bucket_ptr() {
+ static hopscotch_bucket empty_bucket;
+ return &empty_bucket;
+ }
+
+ private:
+ buckets_container_type m_buckets_data;
+ overflow_container_type m_overflow_elements;
+
+ /**
+ * Points to m_buckets_data.data() if !m_buckets_data.empty() otherwise points
+ * to static_empty_bucket_ptr. This variable is useful to avoid the cost of
+ * checking if m_buckets_data is empty when trying to find an element.
+ *
+ * TODO Remove m_buckets_data and only use a pointer+size instead of a
+ * pointer+vector to save some space in the hopscotch_hash object.
+ */
+ hopscotch_bucket* m_buckets;
+
+ size_type m_nb_elements;
+
+ /**
+ * Min size of the hash table before a rehash can occurs automatically (except
+ * if m_max_load_threshold_rehash os reached). If the neighborhood of a bucket
+ * is full before the min is reacher, the elements are put into
+ * m_overflow_elements.
+ */
+ size_type m_min_load_threshold_rehash;
+
+ /**
+ * Max size of the hash table before a rehash occurs automatically to grow the
+ * table.
+ */
+ size_type m_max_load_threshold_rehash;
+
+ float m_max_load_factor;
+};
+
+} // end namespace detail_hopscotch_hash
+
+} // end namespace tsl
+
+#endif
diff --git a/benchmarks/others/tsl/hopscotch_map.h b/benchmarks/others/tsl/hopscotch_map.h
new file mode 100644
index 00000000..15c9e398
--- /dev/null
+++ b/benchmarks/others/tsl/hopscotch_map.h
@@ -0,0 +1,735 @@
+/**
+ * MIT License
+ *
+ * Copyright (c) 2017 Thibaut Goetghebuer-Planchon <[email protected]>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+#ifndef TSL_HOPSCOTCH_MAP_H
+#define TSL_HOPSCOTCH_MAP_H
+
+#include <algorithm>
+#include <cstddef>
+#include <functional>
+#include <initializer_list>
+#include <list>
+#include <memory>
+#include <type_traits>
+#include <utility>
+
+#include "hopscotch_hash.h"
+
+namespace tsl {
+
+/**
+ * Implementation of a hash map using the hopscotch hashing algorithm.
+ *
+ * The Key and the value T must be either nothrow move-constructible,
+ * copy-constructible or both.
+ *
+ * The size of the neighborhood (NeighborhoodSize) must be > 0 and <= 62 if
+ * StoreHash is false. When StoreHash is true, 32-bits of the hash will be
+ * stored alongside the neighborhood limiting the NeighborhoodSize to <= 30.
+ * There is no memory usage difference between 'NeighborhoodSize 62; StoreHash
+ * false' and 'NeighborhoodSize 30; StoreHash true'.
+ *
+ * Storing the hash may improve performance on insert during the rehash process
+ * if the hash takes time to compute. It may also improve read performance if
+ * the KeyEqual function takes time (or incurs a cache-miss). If used with
+ * simple Hash and KeyEqual it may slow things down.
+ *
+ * StoreHash can only be set if the GrowthPolicy is set to
+ * tsl::power_of_two_growth_policy.
+ *
+ * GrowthPolicy defines how the map grows and consequently how a hash value is
+ * mapped to a bucket. By default the map uses tsl::power_of_two_growth_policy.
+ * This policy keeps the number of buckets to a power of two and uses a mask to
+ * map the hash to a bucket instead of the slow modulo. You may define your own
+ * growth policy, check tsl::power_of_two_growth_policy for the interface.
+ *
+ * If the destructors of Key or T throw an exception, behaviour of the class is
+ * undefined.
+ *
+ * Iterators invalidation:
+ * - clear, operator=, reserve, rehash: always invalidate the iterators.
+ * - insert, emplace, emplace_hint, operator[]: if there is an effective
+ * insert, invalidate the iterators if a displacement is needed to resolve a
+ * collision (which mean that most of the time, insert will invalidate the
+ * iterators). Or if there is a rehash.
+ * - erase: iterator on the erased element is the only one which become
+ * invalid.
+ */
+template <class Key, class T, class Hash = std::hash<Key>,
+ class KeyEqual = std::equal_to<Key>,
+ class Allocator = std::allocator<std::pair<Key, T>>,
+ unsigned int NeighborhoodSize = 62, bool StoreHash = false,
+ class GrowthPolicy = tsl::hh::power_of_two_growth_policy<2>>
+class hopscotch_map {
+ private:
+ template <typename U>
+ using has_is_transparent = tsl::detail_hopscotch_hash::has_is_transparent<U>;
+
+ class KeySelect {
+ public:
+ using key_type = Key;
+
+ const key_type& operator()(const std::pair<Key, T>& key_value) const {
+ return key_value.first;
+ }
+
+ key_type& operator()(std::pair<Key, T>& key_value) {
+ return key_value.first;
+ }
+ };
+
+ class ValueSelect {
+ public:
+ using value_type = T;
+
+ const value_type& operator()(const std::pair<Key, T>& key_value) const {
+ return key_value.second;
+ }
+
+ value_type& operator()(std::pair<Key, T>& key_value) {
+ return key_value.second;
+ }
+ };
+
+ using overflow_container_type = std::list<std::pair<Key, T>, Allocator>;
+ using ht = detail_hopscotch_hash::hopscotch_hash<
+ std::pair<Key, T>, KeySelect, ValueSelect, Hash, KeyEqual, Allocator,
+ NeighborhoodSize, StoreHash, GrowthPolicy, overflow_container_type>;
+
+ public:
+ using key_type = typename ht::key_type;
+ using mapped_type = T;
+ using value_type = typename ht::value_type;
+ using size_type = typename ht::size_type;
+ using difference_type = typename ht::difference_type;
+ using hasher = typename ht::hasher;
+ using key_equal = typename ht::key_equal;
+ using allocator_type = typename ht::allocator_type;
+ using reference = typename ht::reference;
+ using const_reference = typename ht::const_reference;
+ using pointer = typename ht::pointer;
+ using const_pointer = typename ht::const_pointer;
+ using iterator = typename ht::iterator;
+ using const_iterator = typename ht::const_iterator;
+
+ /*
+ * Constructors
+ */
+ hopscotch_map() : hopscotch_map(ht::DEFAULT_INIT_BUCKETS_SIZE) {}
+
+ explicit hopscotch_map(size_type bucket_count, const Hash& hash = Hash(),
+ const KeyEqual& equal = KeyEqual(),
+ const Allocator& alloc = Allocator())
+ : m_ht(bucket_count, hash, equal, alloc, ht::DEFAULT_MAX_LOAD_FACTOR) {}
+
+ hopscotch_map(size_type bucket_count, const Allocator& alloc)
+ : hopscotch_map(bucket_count, Hash(), KeyEqual(), alloc) {}
+
+ hopscotch_map(size_type bucket_count, const Hash& hash,
+ const Allocator& alloc)
+ : hopscotch_map(bucket_count, hash, KeyEqual(), alloc) {}
+
+ explicit hopscotch_map(const Allocator& alloc)
+ : hopscotch_map(ht::DEFAULT_INIT_BUCKETS_SIZE, alloc) {}
+
+ template <class InputIt>
+ hopscotch_map(InputIt first, InputIt last,
+ size_type bucket_count = ht::DEFAULT_INIT_BUCKETS_SIZE,
+ const Hash& hash = Hash(), const KeyEqual& equal = KeyEqual(),
+ const Allocator& alloc = Allocator())
+ : hopscotch_map(bucket_count, hash, equal, alloc) {
+ insert(first, last);
+ }
+
+ template <class InputIt>
+ hopscotch_map(InputIt first, InputIt last, size_type bucket_count,
+ const Allocator& alloc)
+ : hopscotch_map(first, last, bucket_count, Hash(), KeyEqual(), alloc) {}
+
+ template <class InputIt>
+ hopscotch_map(InputIt first, InputIt last, size_type bucket_count,
+ const Hash& hash, const Allocator& alloc)
+ : hopscotch_map(first, last, bucket_count, hash, KeyEqual(), alloc) {}
+
+ hopscotch_map(std::initializer_list<value_type> init,
+ size_type bucket_count = ht::DEFAULT_INIT_BUCKETS_SIZE,
+ const Hash& hash = Hash(), const KeyEqual& equal = KeyEqual(),
+ const Allocator& alloc = Allocator())
+ : hopscotch_map(init.begin(), init.end(), bucket_count, hash, equal,
+ alloc) {}
+
+ hopscotch_map(std::initializer_list<value_type> init, size_type bucket_count,
+ const Allocator& alloc)
+ : hopscotch_map(init.begin(), init.end(), bucket_count, Hash(),
+ KeyEqual(), alloc) {}
+
+ hopscotch_map(std::initializer_list<value_type> init, size_type bucket_count,
+ const Hash& hash, const Allocator& alloc)
+ : hopscotch_map(init.begin(), init.end(), bucket_count, hash, KeyEqual(),
+ alloc) {}
+
+ hopscotch_map& operator=(std::initializer_list<value_type> ilist) {
+ m_ht.clear();
+
+ m_ht.reserve(ilist.size());
+ m_ht.insert(ilist.begin(), ilist.end());
+
+ return *this;
+ }
+
+ allocator_type get_allocator() const { return m_ht.get_allocator(); }
+
+ /*
+ * Iterators
+ */
+ iterator begin() noexcept { return m_ht.begin(); }
+ const_iterator begin() const noexcept { return m_ht.begin(); }
+ const_iterator cbegin() const noexcept { return m_ht.cbegin(); }
+
+ iterator end() noexcept { return m_ht.end(); }
+ const_iterator end() const noexcept { return m_ht.end(); }
+ const_iterator cend() const noexcept { return m_ht.cend(); }
+
+ /*
+ * Capacity
+ */
+ bool empty() const noexcept { return m_ht.empty(); }
+ size_type size() const noexcept { return m_ht.size(); }
+ size_type max_size() const noexcept { return m_ht.max_size(); }
+
+ /*
+ * Modifiers
+ */
+ void clear() noexcept { m_ht.clear(); }
+
+ std::pair<iterator, bool> insert(const value_type& value) {
+ return m_ht.insert(value);
+ }
+
+ template <class P, typename std::enable_if<std::is_constructible<
+ value_type, P&&>::value>::type* = nullptr>
+ std::pair<iterator, bool> insert(P&& value) {
+ return m_ht.insert(std::forward<P>(value));
+ }
+
+ std::pair<iterator, bool> insert(value_type&& value) {
+ return m_ht.insert(std::move(value));
+ }
+
+ iterator insert(const_iterator hint, const value_type& value) {
+ return m_ht.insert(hint, value);
+ }
+
+ template <class P, typename std::enable_if<std::is_constructible<
+ value_type, P&&>::value>::type* = nullptr>
+ iterator insert(const_iterator hint, P&& value) {
+ return m_ht.insert(hint, std::forward<P>(value));
+ }
+
+ iterator insert(const_iterator hint, value_type&& value) {
+ return m_ht.insert(hint, std::move(value));
+ }
+
+ template <class InputIt>
+ void insert(InputIt first, InputIt last) {
+ m_ht.insert(first, last);
+ }
+
+ void insert(std::initializer_list<value_type> ilist) {
+ m_ht.insert(ilist.begin(), ilist.end());
+ }
+
+ template <class M>
+ std::pair<iterator, bool> insert_or_assign(const key_type& k, M&& obj) {
+ return m_ht.insert_or_assign(k, std::forward<M>(obj));
+ }
+
+ template <class M>
+ std::pair<iterator, bool> insert_or_assign(key_type&& k, M&& obj) {
+ return m_ht.insert_or_assign(std::move(k), std::forward<M>(obj));
+ }
+
+ template <class M>
+ iterator insert_or_assign(const_iterator hint, const key_type& k, M&& obj) {
+ return m_ht.insert_or_assign(hint, k, std::forward<M>(obj));
+ }
+
+ template <class M>
+ iterator insert_or_assign(const_iterator hint, key_type&& k, M&& obj) {
+ return m_ht.insert_or_assign(hint, std::move(k), std::forward<M>(obj));
+ }
+
+ /**
+ * Due to the way elements are stored, emplace will need to move or copy the
+ * key-value once. The method is equivalent to
+ * insert(value_type(std::forward<Args>(args)...));
+ *
+ * Mainly here for compatibility with the std::unordered_map interface.
+ */
+ template <class... Args>
+ std::pair<iterator, bool> emplace(Args&&... args) {
+ return m_ht.emplace(std::forward<Args>(args)...);
+ }
+
+ /**
+ * Due to the way elements are stored, emplace_hint will need to move or copy
+ * the key-value once. The method is equivalent to insert(hint,
+ * value_type(std::forward<Args>(args)...));
+ *
+ * Mainly here for compatibility with the std::unordered_map interface.
+ */
+ template <class... Args>
+ iterator emplace_hint(const_iterator hint, Args&&... args) {
+ return m_ht.emplace_hint(hint, std::forward<Args>(args)...);
+ }
+
+ template <class... Args>
+ std::pair<iterator, bool> try_emplace(const key_type& k, Args&&... args) {
+ return m_ht.try_emplace(k, std::forward<Args>(args)...);
+ }
+
+ template <class... Args>
+ std::pair<iterator, bool> try_emplace(key_type&& k, Args&&... args) {
+ return m_ht.try_emplace(std::move(k), std::forward<Args>(args)...);
+ }
+
+ template <class... Args>
+ iterator try_emplace(const_iterator hint, const key_type& k, Args&&... args) {
+ return m_ht.try_emplace(hint, k, std::forward<Args>(args)...);
+ }
+
+ template <class... Args>
+ iterator try_emplace(const_iterator hint, key_type&& k, Args&&... args) {
+ return m_ht.try_emplace(hint, std::move(k), std::forward<Args>(args)...);
+ }
+
+ iterator erase(iterator pos) { return m_ht.erase(pos); }
+ iterator erase(const_iterator pos) { return m_ht.erase(pos); }
+ iterator erase(const_iterator first, const_iterator last) {
+ return m_ht.erase(first, last);
+ }
+ size_type erase(const key_type& key) { return m_ht.erase(key); }
+
+ /**
+ * Use the hash value 'precalculated_hash' instead of hashing the key. The
+ * hash value should be the same as hash_function()(key). Useful to speed-up
+ * the lookup to the value if you already have the hash.
+ */
+ size_type erase(const key_type& key, std::size_t precalculated_hash) {
+ return m_ht.erase(key, precalculated_hash);
+ }
+
+ /**
+ * This overload only participates in the overload resolution if the typedef
+ * KeyEqual::is_transparent exists. If so, K must be hashable and comparable
+ * to Key.
+ */
+ template <
+ class K, class KE = KeyEqual,
+ typename std::enable_if<has_is_transparent<KE>::value>::type* = nullptr>
+ size_type erase(const K& key) {
+ return m_ht.erase(key);
+ }
+
+ /**
+ * @copydoc erase(const K& key)
+ *
+ * Use the hash value 'precalculated_hash' instead of hashing the key. The
+ * hash value should be the same as hash_function()(key). Useful to speed-up
+ * the lookup to the value if you already have the hash.
+ */
+ template <
+ class K, class KE = KeyEqual,
+ typename std::enable_if<has_is_transparent<KE>::value>::type* = nullptr>
+ size_type erase(const K& key, std::size_t precalculated_hash) {
+ return m_ht.erase(key, precalculated_hash);
+ }
+
+ void swap(hopscotch_map& other) { other.m_ht.swap(m_ht); }
+
+ /*
+ * Lookup
+ */
+ T& at(const Key& key) { return m_ht.at(key); }
+
+ /**
+ * Use the hash value 'precalculated_hash' instead of hashing the key. The
+ * hash value should be the same as hash_function()(key). Useful to speed-up
+ * the lookup if you already have the hash.
+ */
+ T& at(const Key& key, std::size_t precalculated_hash) {
+ return m_ht.at(key, precalculated_hash);
+ }
+
+ const T& at(const Key& key) const { return m_ht.at(key); }
+
+ /**
+ * @copydoc at(const Key& key, std::size_t precalculated_hash)
+ */
+ const T& at(const Key& key, std::size_t precalculated_hash) const {
+ return m_ht.at(key, precalculated_hash);
+ }
+
+ /**
+ * This overload only participates in the overload resolution if the typedef
+ * KeyEqual::is_transparent exists. If so, K must be hashable and comparable
+ * to Key.
+ */
+ template <
+ class K, class KE = KeyEqual,
+ typename std::enable_if<has_is_transparent<KE>::value>::type* = nullptr>
+ T& at(const K& key) {
+ return m_ht.at(key);
+ }
+
+ /**
+ * @copydoc at(const K& key)
+ *
+ * Use the hash value 'precalculated_hash' instead of hashing the key. The
+ * hash value should be the same as hash_function()(key). Useful to speed-up
+ * the lookup if you already have the hash.
+ */
+ template <
+ class K, class KE = KeyEqual,
+ typename std::enable_if<has_is_transparent<KE>::value>::type* = nullptr>
+ T& at(const K& key, std::size_t precalculated_hash) {
+ return m_ht.at(key, precalculated_hash);
+ }
+
+ /**
+ * @copydoc at(const K& key)
+ */
+ template <
+ class K, class KE = KeyEqual,
+ typename std::enable_if<has_is_transparent<KE>::value>::type* = nullptr>
+ const T& at(const K& key) const {
+ return m_ht.at(key);
+ }
+
+ /**
+ * @copydoc at(const K& key, std::size_t precalculated_hash)
+ */
+ template <
+ class K, class KE = KeyEqual,
+ typename std::enable_if<has_is_transparent<KE>::value>::type* = nullptr>
+ const T& at(const K& key, std::size_t precalculated_hash) const {
+ return m_ht.at(key, precalculated_hash);
+ }
+
+ T& operator[](const Key& key) { return m_ht[key]; }
+ T& operator[](Key&& key) { return m_ht[std::move(key)]; }
+
+ size_type count(const Key& key) const { return m_ht.count(key); }
+
+ /**
+ * Use the hash value 'precalculated_hash' instead of hashing the key. The
+ * hash value should be the same as hash_function()(key). Useful to speed-up
+ * the lookup if you already have the hash.
+ */
+ size_type count(const Key& key, std::size_t precalculated_hash) const {
+ return m_ht.count(key, precalculated_hash);
+ }
+
+ /**
+ * This overload only participates in the overload resolution if the typedef
+ * KeyEqual::is_transparent exists. If so, K must be hashable and comparable
+ * to Key.
+ */
+ template <
+ class K, class KE = KeyEqual,
+ typename std::enable_if<has_is_transparent<KE>::value>::type* = nullptr>
+ size_type count(const K& key) const {
+ return m_ht.count(key);
+ }
+
+ /**
+ * @copydoc count(const K& key) const
+ *
+ * Use the hash value 'precalculated_hash' instead of hashing the key. The
+ * hash value should be the same as hash_function()(key). Useful to speed-up
+ * the lookup if you already have the hash.
+ */
+ template <
+ class K, class KE = KeyEqual,
+ typename std::enable_if<has_is_transparent<KE>::value>::type* = nullptr>
+ size_type count(const K& key, std::size_t precalculated_hash) const {
+ return m_ht.count(key, precalculated_hash);
+ }
+
+ iterator find(const Key& key) { return m_ht.find(key); }
+
+ /**
+ * Use the hash value 'precalculated_hash' instead of hashing the key. The
+ * hash value should be the same as hash_function()(key). Useful to speed-up
+ * the lookup if you already have the hash.
+ */
+ iterator find(const Key& key, std::size_t precalculated_hash) {
+ return m_ht.find(key, precalculated_hash);
+ }
+
+ const_iterator find(const Key& key) const { return m_ht.find(key); }
+
+ /**
+ * @copydoc find(const Key& key, std::size_t precalculated_hash)
+ */
+ const_iterator find(const Key& key, std::size_t precalculated_hash) const {
+ return m_ht.find(key, precalculated_hash);
+ }
+
+ /**
+ * This overload only participates in the overload resolution if the typedef
+ * KeyEqual::is_transparent exists. If so, K must be hashable and comparable
+ * to Key.
+ */
+ template <
+ class K, class KE = KeyEqual,
+ typename std::enable_if<has_is_transparent<KE>::value>::type* = nullptr>
+ iterator find(const K& key) {
+ return m_ht.find(key);
+ }
+
+ /**
+ * @copydoc find(const K& key)
+ *
+ * Use the hash value 'precalculated_hash' instead of hashing the key. The
+ * hash value should be the same as hash_function()(key). Useful to speed-up
+ * the lookup if you already have the hash.
+ */
+ template <
+ class K, class KE = KeyEqual,
+ typename std::enable_if<has_is_transparent<KE>::value>::type* = nullptr>
+ iterator find(const K& key, std::size_t precalculated_hash) {
+ return m_ht.find(key, precalculated_hash);
+ }
+
+ /**
+ * @copydoc find(const K& key)
+ */
+ template <
+ class K, class KE = KeyEqual,
+ typename std::enable_if<has_is_transparent<KE>::value>::type* = nullptr>
+ const_iterator find(const K& key) const {
+ return m_ht.find(key);
+ }
+
+ /**
+ * @copydoc find(const K& key)
+ *
+ * Use the hash value 'precalculated_hash' instead of hashing the key. The
+ * hash value should be the same as hash_function()(key). Useful to speed-up
+ * the lookup if you already have the hash.
+ */
+ template <
+ class K, class KE = KeyEqual,
+ typename std::enable_if<has_is_transparent<KE>::value>::type* = nullptr>
+ const_iterator find(const K& key, std::size_t precalculated_hash) const {
+ return m_ht.find(key, precalculated_hash);
+ }
+
+ bool contains(const Key& key) const { return m_ht.contains(key); }
+
+ /**
+ * Use the hash value 'precalculated_hash' instead of hashing the key. The
+ * hash value should be the same as hash_function()(key). Useful to speed-up
+ * the lookup if you already have the hash.
+ */
+ bool contains(const Key& key, std::size_t precalculated_hash) const {
+ return m_ht.contains(key, precalculated_hash);
+ }
+
+ /**
+ * This overload only participates in the overload resolution if the typedef
+ * KeyEqual::is_transparent exists. If so, K must be hashable and comparable
+ * to Key.
+ */
+ template <
+ class K, class KE = KeyEqual,
+ typename std::enable_if<has_is_transparent<KE>::value>::type* = nullptr>
+ bool contains(const K& key) const {
+ return m_ht.contains(key);
+ }
+
+ /**
+ * @copydoc contains(const K& key) const
+ *
+ * Use the hash value 'precalculated_hash' instead of hashing the key. The
+ * hash value should be the same as hash_function()(key). Useful to speed-up
+ * the lookup if you already have the hash.
+ */
+ template <
+ class K, class KE = KeyEqual,
+ typename std::enable_if<has_is_transparent<KE>::value>::type* = nullptr>
+ bool contains(const K& key, std::size_t precalculated_hash) const {
+ return m_ht.contains(key, precalculated_hash);
+ }
+
+ std::pair<iterator, iterator> equal_range(const Key& key) {
+ return m_ht.equal_range(key);
+ }
+
+ /**
+ * Use the hash value 'precalculated_hash' instead of hashing the key. The
+ * hash value should be the same as hash_function()(key). Useful to speed-up
+ * the lookup if you already have the hash.
+ */
+ std::pair<iterator, iterator> equal_range(const Key& key,
+ std::size_t precalculated_hash) {
+ return m_ht.equal_range(key, precalculated_hash);
+ }
+
+ std::pair<const_iterator, const_iterator> equal_range(const Key& key) const {
+ return m_ht.equal_range(key);
+ }
+
+ /**
+ * @copydoc equal_range(const Key& key, std::size_t precalculated_hash)
+ */
+ std::pair<const_iterator, const_iterator> equal_range(
+ const Key& key, std::size_t precalculated_hash) const {
+ return m_ht.equal_range(key, precalculated_hash);
+ }
+
+ /**
+ * This overload only participates in the overload resolution if the typedef
+ * KeyEqual::is_transparent exists. If so, K must be hashable and comparable
+ * to Key.
+ */
+ template <
+ class K, class KE = KeyEqual,
+ typename std::enable_if<has_is_transparent<KE>::value>::type* = nullptr>
+ std::pair<iterator, iterator> equal_range(const K& key) {
+ return m_ht.equal_range(key);
+ }
+
+ /**
+ * @copydoc equal_range(const K& key)
+ *
+ * Use the hash value 'precalculated_hash' instead of hashing the key. The
+ * hash value should be the same as hash_function()(key). Useful to speed-up
+ * the lookup if you already have the hash.
+ */
+ template <
+ class K, class KE = KeyEqual,
+ typename std::enable_if<has_is_transparent<KE>::value>::type* = nullptr>
+ std::pair<iterator, iterator> equal_range(const K& key,
+ std::size_t precalculated_hash) {
+ return m_ht.equal_range(key, precalculated_hash);
+ }
+
+ /**
+ * @copydoc equal_range(const K& key)
+ */
+ template <
+ class K, class KE = KeyEqual,
+ typename std::enable_if<has_is_transparent<KE>::value>::type* = nullptr>
+ std::pair<const_iterator, const_iterator> equal_range(const K& key) const {
+ return m_ht.equal_range(key);
+ }
+
+ /**
+ * @copydoc equal_range(const K& key, std::size_t precalculated_hash)
+ */
+ template <
+ class K, class KE = KeyEqual,
+ typename std::enable_if<has_is_transparent<KE>::value>::type* = nullptr>
+ std::pair<const_iterator, const_iterator> equal_range(
+ const K& key, std::size_t precalculated_hash) const {
+ return m_ht.equal_range(key, precalculated_hash);
+ }
+
+ /*
+ * Bucket interface
+ */
+ size_type bucket_count() const { return m_ht.bucket_count(); }
+ size_type max_bucket_count() const { return m_ht.max_bucket_count(); }
+
+ /*
+ * Hash policy
+ */
+ float load_factor() const { return m_ht.load_factor(); }
+ float max_load_factor() const { return m_ht.max_load_factor(); }
+ void max_load_factor(float ml) { m_ht.max_load_factor(ml); }
+
+ void rehash(size_type count_) { m_ht.rehash(count_); }
+ void reserve(size_type count_) { m_ht.reserve(count_); }
+
+ /*
+ * Observers
+ */
+ hasher hash_function() const { return m_ht.hash_function(); }
+ key_equal key_eq() const { return m_ht.key_eq(); }
+
+ /*
+ * Other
+ */
+
+ /**
+ * Convert a const_iterator to an iterator.
+ */
+ iterator mutable_iterator(const_iterator pos) {
+ return m_ht.mutable_iterator(pos);
+ }
+
+ size_type overflow_size() const noexcept { return m_ht.overflow_size(); }
+
+ friend bool operator==(const hopscotch_map& lhs, const hopscotch_map& rhs) {
+ if (lhs.size() != rhs.size()) {
+ return false;
+ }
+
+ for (const auto& element_lhs : lhs) {
+ const auto it_element_rhs = rhs.find(element_lhs.first);
+ if (it_element_rhs == rhs.cend() ||
+ element_lhs.second != it_element_rhs->second) {
+ return false;
+ }
+ }
+
+ return true;
+ }
+
+ friend bool operator!=(const hopscotch_map& lhs, const hopscotch_map& rhs) {
+ return !operator==(lhs, rhs);
+ }
+
+ friend void swap(hopscotch_map& lhs, hopscotch_map& rhs) { lhs.swap(rhs); }
+
+ private:
+ ht m_ht;
+};
+
+/**
+ * Same as `tsl::hopscotch_map<Key, T, Hash, KeyEqual, Allocator,
+ * NeighborhoodSize, StoreHash, tsl::hh::prime_growth_policy>`.
+ */
+template <class Key, class T, class Hash = std::hash<Key>,
+ class KeyEqual = std::equal_to<Key>,
+ class Allocator = std::allocator<std::pair<Key, T>>,
+ unsigned int NeighborhoodSize = 62, bool StoreHash = false>
+using hopscotch_pg_map =
+ hopscotch_map<Key, T, Hash, KeyEqual, Allocator, NeighborhoodSize,
+ StoreHash, tsl::hh::prime_growth_policy>;
+
+} // end namespace tsl
+
+#endif
diff --git a/benchmarks/others/update.sh b/benchmarks/others/update.sh
new file mode 100644
index 00000000..6af246d7
--- /dev/null
+++ b/benchmarks/others/update.sh
@@ -0,0 +1,31 @@
+tsl_h="https://raw.github.com/Tessil/hopscotch-map/master/include/tsl/"
+tsl_r="https://raw.github.com/Tessil/robin-map/master/include/tsl/"
+greg="https://raw.github.com/greg7mdp/sparsepp/master/sparsepp/"
+martinus="https://raw.github.com/martinus/robin-hood-hashing/master/src/include/"
+skarupke="https://raw.github.com/skarupke/flat_hash_map/master/"
+
+mkdir -p tsl sparsepp skarupke
+
+wget $martinus"robin_hood.h" -O "robin_hood.h"
+
+wget $skarupke"bytell_hash_map.hpp" -O "skarupke/bytell_hash_map.hpp"
+wget $skarupke"flat_hash_map.hpp" -O "skarupke/flat_hash_map.hpp"
+
+exit
+wget $greg"spp.h" -O "sparsepp/spp.h"
+wget $greg"spp_config.h" -O "sparsepp/spp_config.h"
+wget $greg"spp_dlalloc.h" -O "sparsepp/spp_dlalloc.h"
+wget $greg"spp_memory.h" -O "sparsepp/spp_memory.h"
+wget $greg"spp_smartptr.h" -O "sparsepp/spp_smartptr.h"
+wget $greg"spp_stdint.h" -O "sparsepp/spp_stdint.h"
+wget $greg"spp_timer.h" -O "sparsepp/spp_timer.h"
+wget $greg"spp_traits.h" -O "sparsepp/spp_traits.h"
+wget $greg"spp_utils.h" -O "sparsepp/spp_utils.h"
+
+wget $tsl_h"hopscotch_growth_policy.h" -O "tsl/hopscotch_growth_policy.h"
+wget $tsl_h"hopscotch_hash.h" -O "tsl/hopscotch_hash.h"
+wget $tsl_h"hopscotch_map.h" -O "tsl/hopscotch_map.h"
+
+wget $tsl_r"robin_growth_policy.h" -O "tsl/robin_growth_policy.h"
+wget $tsl_r"robin_hash.h" -O "tsl/robin_hash.h"
+wget $tsl_r"robin_map.h" -O "tsl/robin_map.h"
diff --git a/benchmarks/shootout1_cmap.cpp b/benchmarks/shootout1_cmap.cpp
index e16d4f41..f88d5ad6 100644
--- a/benchmarks/shootout1_cmap.cpp
+++ b/benchmarks/shootout1_cmap.cpp
@@ -5,9 +5,10 @@
#include <string>
#include <unordered_map>
#include <stdexcept>
-#include "others/bytell_hash_map.hpp"
#include "others/robin_hood.hpp"
-#include "others/hopscotch_map.h"
+#include "others/skarupke/bytell_hash_map.hpp"
+#include "others/tsl/hopscotch_map.h"
+#include "others/tsl/robin_map.h"
#include "others/sparsepp/spp.h"
#define PICOBENCH_IMPLEMENT_WITH_MAIN
@@ -20,6 +21,7 @@ template <class K, class V> using umap = std::unordered_map<K, V>;
template <class K, class V> using bmap = ska::bytell_hash_map<K, V>;
template <class K, class V> using fmap = ska::flat_hash_map<K, V>;
template <class K, class V> using hmap = tsl::hopscotch_map<K, V>;
+template <class K, class V> using omap = tsl::robin_map<K, V>;
template <class K, class V> using smap = spp::sparse_hash_map<K, V>;
template <class K, class V> using rmap = robin_hood::unordered_flat_map<K, V, robin_hood::hash<K>,
std::equal_to<K>, MaxLoadFactor100>;
@@ -28,9 +30,11 @@ template <class K, class V> using rmap = robin_hood::unordered_flat_map<K, V, ro
using b##map = bmap __VA_ARGS__; \
using f##map = fmap __VA_ARGS__; \
using h##map = hmap __VA_ARGS__; \
+ using o##map = omap __VA_ARGS__; \
using s##map = smap __VA_ARGS__; \
using r##map = rmap __VA_ARGS__
+
DEFMAP(map_i, <int, int>);
DEFMAP(map_x, <uint64_t, uint64_t>);
DEFMAP(map_s, <std::string, std::string>);
@@ -39,54 +43,15 @@ using_cmap(i, int, int, c_default_equals, c_default_hash32);
using_cmap(x, uint64_t, uint64_t, c_default_equals, c_default_hash64);
using_cmap_str();
-PICOBENCH_SUITE("Map1");
-
-template <class MapInt>
-static void ctor_and_ins_one_i(picobench::state& s)
-{
- size_t result = 0;
-
- picobench::scope scope(s);
- c_forrange (n, s.iterations()) {
- MapInt map;
- map[n];
- result += map.size();
- }
- s.set_result(result);
-}
-
-static void ctor_and_ins_one_cmap_i(picobench::state& s)
-{
- size_t result = 0;
-
- picobench::scope scope(s);
- c_forrange (n, s.iterations()) {
- cmap_i map = cmap_i_init();
- cmap_i_emplace(&map, n, 0);
- result += cmap_i_size(map);
- cmap_i_del(&map);
- }
- s.set_result(result);
-}
-
-#define P samples(S1).iterations({N1})
-PICOBENCH(ctor_and_ins_one_i<umap_i>).P;
-PICOBENCH(ctor_and_ins_one_i<bmap_i>).P;
-PICOBENCH(ctor_and_ins_one_i<fmap_i>).P;
-PICOBENCH(ctor_and_ins_one_i<hmap_i>).P;
-PICOBENCH(ctor_and_ins_one_i<smap_i>).P;
-PICOBENCH(ctor_and_ins_one_i<rmap_i>).P;
-PICOBENCH(ctor_and_ins_one_cmap_i).P;
-#undef P
-PICOBENCH_SUITE("Map2");
+PICOBENCH_SUITE("Map1");
template <class MapInt>
static void ins_and_erase_i(picobench::state& s)
{
size_t result = 0;
MapInt map;
- map.max_load_factor(MaxLoadFactor100 / 100.0);
+ map.max_load_factor((int)MaxLoadFactor100 / 100.0);
stc64_srandom(seed);
picobench::scope scope(s);
@@ -105,7 +70,7 @@ static void ins_and_erase_i(picobench::state& s)
static void ins_and_erase_cmap_i(picobench::state& s)
{
cmap_i map = cmap_i_init();
- cmap_i_set_load_factors(&map, 0.0, MaxLoadFactor100 / 100.0);
+ cmap_i_set_load_factors(&map, 0.0, (int)MaxLoadFactor100 / 100.0);
stc64_srandom(seed);
picobench::scope scope(s);
@@ -125,7 +90,7 @@ static void ins_and_erase_cmap_i(picobench::state& s)
static void ins_and_erase_cmap_x(picobench::state& s)
{
cmap_x map = cmap_x_init();
- cmap_x_set_load_factors(&map, 0.0, MaxLoadFactor100 / 100.0);
+ cmap_x_set_load_factors(&map, 0.0, (int)MaxLoadFactor100 / 100.0);
stc64_srandom(seed);
picobench::scope scope(s);
@@ -147,12 +112,13 @@ PICOBENCH(ins_and_erase_i<umap_x>).P;
PICOBENCH(ins_and_erase_i<bmap_x>).P;
PICOBENCH(ins_and_erase_i<fmap_x>).P;
PICOBENCH(ins_and_erase_i<hmap_x>).P;
+PICOBENCH(ins_and_erase_i<omap_x>).P;
PICOBENCH(ins_and_erase_i<smap_x>).P;
PICOBENCH(ins_and_erase_i<rmap_x>).P;
PICOBENCH(ins_and_erase_cmap_x).P;
#undef P
-PICOBENCH_SUITE("Map3");
+PICOBENCH_SUITE("Map2");
template <class MapInt>
static void ins_and_access_i(picobench::state& s)
@@ -160,7 +126,7 @@ static void ins_and_access_i(picobench::state& s)
uint64_t mask = (1ull << s.arg()) - 1;
size_t result = 0;
MapInt map;
- map.max_load_factor(MaxLoadFactor100 / 100.0);
+ map.max_load_factor((int)MaxLoadFactor100 / 100.0);
stc64_srandom(seed);
picobench::scope scope(s);
@@ -174,7 +140,7 @@ static void ins_and_access_cmap_i(picobench::state& s)
uint64_t mask = (1ull << s.arg()) - 1;
size_t result = 0;
cmap_i map = cmap_i_init();
- cmap_i_set_load_factors(&map, 0.0, MaxLoadFactor100 / 100.0);
+ cmap_i_set_load_factors(&map, 0.0, (int)MaxLoadFactor100 / 100.0);
stc64_srandom(seed);
picobench::scope scope(s);
@@ -189,12 +155,13 @@ PICOBENCH(ins_and_access_i<umap_i>).P;
PICOBENCH(ins_and_access_i<bmap_i>).P;
PICOBENCH(ins_and_access_i<fmap_i>).P;
PICOBENCH(ins_and_access_i<hmap_i>).P;
+PICOBENCH(ins_and_access_i<omap_i>).P;
PICOBENCH(ins_and_access_i<smap_i>).P;
PICOBENCH(ins_and_access_i<rmap_i>).P;
PICOBENCH(ins_and_access_cmap_i).P;
#undef P
-PICOBENCH_SUITE("Map4");
+PICOBENCH_SUITE("Map3");
static void randomize(char* str, size_t len) {
union {uint64_t i; char c[8];} r = {.i = stc64_random()};
@@ -208,7 +175,7 @@ static void ins_and_access_s(picobench::state& s)
std::string str(s.arg(), 'x');
size_t result = 0;
MapStr map;
- map.max_load_factor(MaxLoadFactor100 / 100.0);
+ map.max_load_factor((int)MaxLoadFactor100 / 100.0);
stc64_srandom(seed);
picobench::scope scope(s);
@@ -226,7 +193,7 @@ static void ins_and_access_cmap_s(picobench::state& s)
cstr str = cstr_with_size(s.arg(), 'x');
size_t result = 0;
cmap_str map = cmap_str_init();
- cmap_str_set_load_factors(&map, 0.0, MaxLoadFactor100 / 100.0);
+ cmap_str_set_load_factors(&map, 0.0, (int)MaxLoadFactor100 / 100.0);
stc64_srandom(seed);
picobench::scope scope(s);
@@ -246,18 +213,19 @@ PICOBENCH(ins_and_access_s<umap_s>).P;
PICOBENCH(ins_and_access_s<bmap_s>).P;
PICOBENCH(ins_and_access_s<fmap_s>).P;
PICOBENCH(ins_and_access_s<hmap_s>).P;
+PICOBENCH(ins_and_access_s<omap_s>).P;
PICOBENCH(ins_and_access_s<smap_s>).P;
PICOBENCH(ins_and_access_s<rmap_s>).P;
PICOBENCH(ins_and_access_cmap_s).P;
#undef P
-PICOBENCH_SUITE("Map5");
+PICOBENCH_SUITE("Map4");
template <class MapX>
static void iterate_x(picobench::state& s)
{
MapX map;
- map.max_load_factor(MaxLoadFactor100 / 100.0);
+ map.max_load_factor((int)MaxLoadFactor100 / 100.0);
uint64_t K = (1ull << s.arg()) - 1;
picobench::scope scope(s);
@@ -286,7 +254,7 @@ static void iterate_x(picobench::state& s)
static void iterate_cmap_x(picobench::state& s)
{
cmap_x map = cmap_x_init();
- cmap_x_set_load_factors(&map, 0.3, MaxLoadFactor100 / 100.0);
+ cmap_x_set_load_factors(&map, 0.3, (int)MaxLoadFactor100 / 100.0);
uint64_t K = (1ull << s.arg()) - 1;
picobench::scope scope(s);
@@ -319,6 +287,7 @@ PICOBENCH(iterate_x<umap_x>).P;
PICOBENCH(iterate_x<bmap_x>).P;
PICOBENCH(iterate_x<fmap_x>).P;
PICOBENCH(iterate_x<hmap_x>).P;
+PICOBENCH(iterate_x<omap_x>).P;
PICOBENCH(iterate_x<smap_x>).P;
PICOBENCH(iterate_x<rmap_x>).P;
PICOBENCH(iterate_cmap_x).P;
diff --git a/benchmarks/shootout2_cmap.cpp b/benchmarks/shootout2_cmap.cpp
index 13905a3f..33d9dad8 100644
--- a/benchmarks/shootout2_cmap.cpp
+++ b/benchmarks/shootout2_cmap.cpp
@@ -7,21 +7,18 @@
#ifdef __cplusplus
#include <unordered_map>
-#include "others/bytell_hash_map.hpp"
#include "others/robin_hood.hpp"
-#include "others/hopscotch_map.h"
+#include "others/skarupke/bytell_hash_map.hpp"
+#include "others/tsl/hopscotch_map.h"
+#include "others/tsl/robin_map.h"
#include "others/sparsepp/spp.h"
template<typename C> inline void destroy_me(C& c) { C().swap(c); }
#endif
// Visual Studio: compile with -TP to force C++: cl -TP -EHsc -O2 benchmark.c
-static inline uint32_t fibonacci_hash(const void* data, size_t len) {
- return (uint32_t) (((*(const uint64_t *) data) * 11400714819323198485llu) >> 24);
-}
-
// cmap and khash template expansion
-using_cmap(ii, int64_t, int64_t, c_default_equals, fibonacci_hash); // c_default_hash);
+using_cmap(ii, int64_t, int64_t, c_default_equals, c_default_hash64); // c_default_hash);
KHASH_MAP_INIT_INT64(ii, int64_t)
@@ -104,6 +101,18 @@ stc64_t rng;
#define HMAP_CLEAR(X) UMAP_CLEAR(X)
#define HMAP_DTOR(X) UMAP_DTOR(X)
+#define OMAP_SETUP(X, Key, Value) tsl::robin_map<Key, Value> map; map.max_load_factor(max_load_factor)
+#define OMAP_PUT(X, key, val) UMAP_PUT(X, key, val)
+#define OMAP_EMPLACE(X, key, val) UMAP_EMPLACE(X, key, val)
+#define OMAP_FIND(X, key) UMAP_FIND(X, key)
+#define OMAP_ERASE(X, key) UMAP_ERASE(X, key)
+#define OMAP_FOR(X, i) UMAP_FOR(X, i)
+#define OMAP_ITEM(X, i) UMAP_ITEM(X, i)
+#define OMAP_SIZE(X) UMAP_SIZE(X)
+#define OMAP_BUCKETS(X) UMAP_BUCKETS(X)
+#define OMAP_CLEAR(X) UMAP_CLEAR(X)
+#define OMAP_DTOR(X) UMAP_DTOR(X)
+
#define RMAP_SETUP(X, Key, Value) robin_hood::unordered_map<Key, Value> map
#define RMAP_PUT(X, key, val) UMAP_PUT(X, key, val)
#define RMAP_EMPLACE(X, key, val) UMAP_EMPLACE(X, key, val)
diff --git a/stc/cmap.h b/stc/cmap.h
index c8649f28..6300a46a 100644
--- a/stc/cmap.h
+++ b/stc/cmap.h
@@ -352,7 +352,6 @@ STC_INLINE uint64_t c_default_hash64(const void* data, size_t ignored)
STC_INLINE size_t fastrange_uint64_t(uint64_t x, uint64_t n) {uint64_t l,h; c_umul128(x,n,&l,&h); return h;}
#endif
#define chash_index_(h, entryPtr) ((entryPtr) - (h).table)
-enum {chash_HASH_ = 0x7f, chash_USED_ = 0x80};
#define _implement_CHASH(X, C, Key, Mapped, keyEqualsRaw, keyHashRaw, \
mappedDel, mappedFromRaw, mappedToRaw, RawMapped, \
@@ -385,9 +384,9 @@ enum {chash_HASH_ = 0x7f, chash_USED_ = 0x80};
\
STC_DEF chash_bucket_t \
C##_##X##_bucket_(const C##_##X* self, const C##_##X##_rawkey_t* rkeyptr) { \
- const size_t hash = keyHashRaw(rkeyptr, sizeof(C##_##X##_rawkey_t)); \
+ const uint64_t hash = keyHashRaw(rkeyptr, sizeof(C##_##X##_rawkey_t)); \
uint_fast8_t sx; size_t cap = self->bucket_count; \
- chash_bucket_t b = {_c_SELECT(fastrange,CMAP_SIZE_T)(hash, cap), (hash & chash_HASH_) | chash_USED_}; \
+ chash_bucket_t b = {_c_SELECT(fastrange,CMAP_SIZE_T)(hash, cap), (uint_fast8_t)(hash | 0x80)}; \
const uint8_t* hashx = self->_hashx; \
while ((sx = hashx[b.idx])) { \
if (sx == b.hx) { \
@@ -418,7 +417,7 @@ enum {chash_HASH_ = 0x7f, chash_USED_ = 0x80};
chash_bucket_t b = C##_##X##_bucket_(self, &rkey); \
C##_##X##_result_t res = {&self->table[b.idx], !self->_hashx[b.idx]}; \
if (res.second) { \
- self->_hashx[b.idx] = (uint8_t) b.hx; \
+ self->_hashx[b.idx] = b.hx; \
++self->size; \
} \
return res; \