Repo created

This commit is contained in:
Fr4nz D13trich 2025-11-22 14:04:28 +01:00
parent 81b91f4139
commit f8c34fa5ee
22732 changed files with 4815320 additions and 2 deletions

View file

@ -0,0 +1,764 @@
// Copyright 2018 The Abseil Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include <stdint.h>
#include <algorithm>
#include <functional>
#include <map>
#include <numeric>
#include <random>
#include <set>
#include <string>
#include <type_traits>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#include "benchmark/benchmark.h"
#include "absl/algorithm/container.h"
#include "absl/base/internal/raw_logging.h"
#include "absl/container/btree_map.h"
#include "absl/container/btree_set.h"
#include "absl/container/btree_test.h"
#include "absl/container/flat_hash_map.h"
#include "absl/container/flat_hash_set.h"
#include "absl/container/internal/hashtable_debug.h"
#include "absl/hash/hash.h"
#include "absl/log/log.h"
#include "absl/memory/memory.h"
#include "absl/random/random.h"
#include "absl/strings/cord.h"
#include "absl/strings/str_format.h"
#include "absl/time/time.h"
namespace absl {
ABSL_NAMESPACE_BEGIN
namespace container_internal {
namespace {
constexpr size_t kBenchmarkValues = 1 << 20;
// How many times we add and remove sub-batches in one batch of *AddRem
// benchmarks.
constexpr size_t kAddRemBatchSize = 1 << 2;
// Generates n values in the range [0, 4 * n].
template <typename V>
std::vector<V> GenerateValues(int n) {
constexpr int kSeed = 23;
return GenerateValuesWithSeed<V>(n, 4 * n, kSeed);
}
// Benchmark insertion of values into a container.
template <typename T>
void BM_InsertImpl(benchmark::State& state, bool sorted) {
using V = typename remove_pair_const<typename T::value_type>::type;
typename KeyOfValue<typename T::key_type, V>::type key_of_value;
std::vector<V> values = GenerateValues<V>(kBenchmarkValues);
if (sorted) {
std::sort(values.begin(), values.end());
}
T container(values.begin(), values.end());
// Remove and re-insert 10% of the keys per batch.
const int batch_size = (kBenchmarkValues + 9) / 10;
while (state.KeepRunningBatch(batch_size)) {
state.PauseTiming();
const auto i = static_cast<int>(state.iterations());
for (int j = i; j < i + batch_size; j++) {
int x = j % kBenchmarkValues;
container.erase(key_of_value(values[x]));
}
state.ResumeTiming();
for (int j = i; j < i + batch_size; j++) {
int x = j % kBenchmarkValues;
container.insert(values[x]);
}
}
}
template <typename T>
void BM_Insert(benchmark::State& state) {
BM_InsertImpl<T>(state, false);
}
template <typename T>
void BM_InsertSorted(benchmark::State& state) {
BM_InsertImpl<T>(state, true);
}
// Benchmark inserting the first few elements in a container. In b-tree, this is
// when the root node grows.
template <typename T>
void BM_InsertSmall(benchmark::State& state) {
using V = typename remove_pair_const<typename T::value_type>::type;
const int kSize = 8;
std::vector<V> values = GenerateValues<V>(kSize);
T container;
while (state.KeepRunningBatch(kSize)) {
for (int i = 0; i < kSize; ++i) {
benchmark::DoNotOptimize(container.insert(values[i]));
}
state.PauseTiming();
// Do not measure the time it takes to clear the container.
container.clear();
state.ResumeTiming();
}
}
template <typename T>
void BM_LookupImpl(benchmark::State& state, bool sorted) {
using V = typename remove_pair_const<typename T::value_type>::type;
typename KeyOfValue<typename T::key_type, V>::type key_of_value;
std::vector<V> values = GenerateValues<V>(kBenchmarkValues);
if (sorted) {
std::sort(values.begin(), values.end());
}
T container(values.begin(), values.end());
while (state.KeepRunning()) {
int idx = state.iterations() % kBenchmarkValues;
benchmark::DoNotOptimize(container.find(key_of_value(values[idx])));
}
}
// Benchmark lookup of values in a container.
template <typename T>
void BM_Lookup(benchmark::State& state) {
BM_LookupImpl<T>(state, false);
}
// Benchmark lookup of values in a full container, meaning that values
// are inserted in-order to take advantage of biased insertion, which
// yields a full tree.
template <typename T>
void BM_FullLookup(benchmark::State& state) {
BM_LookupImpl<T>(state, true);
}
// Benchmark erasing values from a container.
template <typename T>
void BM_Erase(benchmark::State& state) {
using V = typename remove_pair_const<typename T::value_type>::type;
typename KeyOfValue<typename T::key_type, V>::type key_of_value;
std::vector<V> values = GenerateValues<V>(kBenchmarkValues);
T container(values.begin(), values.end());
// Remove and re-insert 10% of the keys per batch.
const int batch_size = (kBenchmarkValues + 9) / 10;
while (state.KeepRunningBatch(batch_size)) {
const int i = state.iterations();
for (int j = i; j < i + batch_size; j++) {
int x = j % kBenchmarkValues;
container.erase(key_of_value(values[x]));
}
state.PauseTiming();
for (int j = i; j < i + batch_size; j++) {
int x = j % kBenchmarkValues;
container.insert(values[x]);
}
state.ResumeTiming();
}
}
// Benchmark erasing multiple values from a container.
template <typename T>
void BM_EraseRange(benchmark::State& state) {
using V = typename remove_pair_const<typename T::value_type>::type;
typename KeyOfValue<typename T::key_type, V>::type key_of_value;
std::vector<V> values = GenerateValues<V>(kBenchmarkValues);
T container(values.begin(), values.end());
// Remove and re-insert 10% of the keys per batch.
const int batch_size = (kBenchmarkValues + 9) / 10;
while (state.KeepRunningBatch(batch_size)) {
const int i = state.iterations();
const int start_index = i % kBenchmarkValues;
state.PauseTiming();
{
std::vector<V> removed;
removed.reserve(batch_size);
auto itr = container.find(key_of_value(values[start_index]));
auto start = itr;
for (int j = 0; j < batch_size; j++) {
if (itr == container.end()) {
state.ResumeTiming();
container.erase(start, itr);
state.PauseTiming();
itr = container.begin();
start = itr;
}
removed.push_back(*itr++);
}
state.ResumeTiming();
container.erase(start, itr);
state.PauseTiming();
container.insert(removed.begin(), removed.end());
}
state.ResumeTiming();
}
}
// Predicate that erases every other element. We can't use a lambda because
// C++11 doesn't support generic lambdas.
// TODO(b/207389011): consider adding benchmarks that remove different fractions
// of keys (e.g. 10%, 90%).
struct EraseIfPred {
uint64_t i = 0;
template <typename T>
bool operator()(const T&) {
return ++i % 2;
}
};
// Benchmark erasing multiple values from a container with a predicate.
template <typename T>
void BM_EraseIf(benchmark::State& state) {
using V = typename remove_pair_const<typename T::value_type>::type;
std::vector<V> values = GenerateValues<V>(kBenchmarkValues);
// Removes half of the keys per batch.
const int batch_size = (kBenchmarkValues + 1) / 2;
EraseIfPred pred;
while (state.KeepRunningBatch(batch_size)) {
state.PauseTiming();
{
T container(values.begin(), values.end());
state.ResumeTiming();
erase_if(container, pred);
benchmark::DoNotOptimize(container);
state.PauseTiming();
}
state.ResumeTiming();
}
}
// Benchmark steady-state insert (into first half of range) and remove (from
// second half of range), treating the container approximately like a queue with
// log-time access for all elements. This benchmark does not test the case where
// insertion and removal happen in the same region of the tree. This benchmark
// counts two value constructors.
template <typename T>
void BM_QueueAddRem(benchmark::State& state) {
using V = typename remove_pair_const<typename T::value_type>::type;
typename KeyOfValue<typename T::key_type, V>::type key_of_value;
ABSL_RAW_CHECK(kBenchmarkValues % 2 == 0, "for performance");
T container;
const size_t half = kBenchmarkValues / 2;
std::vector<int> remove_keys(half);
std::vector<int> add_keys(half);
// We want to do the exact same work repeatedly, and the benchmark can end
// after a different number of iterations depending on the speed of the
// individual run so we use a large batch size here and ensure that we do
// deterministic work every batch.
while (state.KeepRunningBatch(half * kAddRemBatchSize)) {
state.PauseTiming();
container.clear();
for (size_t i = 0; i < half; ++i) {
remove_keys[i] = i;
add_keys[i] = i;
}
constexpr int kSeed = 5;
std::mt19937_64 rand(kSeed);
std::shuffle(remove_keys.begin(), remove_keys.end(), rand);
std::shuffle(add_keys.begin(), add_keys.end(), rand);
// Note needs lazy generation of values.
Generator<V> g(kBenchmarkValues * kAddRemBatchSize);
for (size_t i = 0; i < half; ++i) {
container.insert(g(add_keys[i]));
container.insert(g(half + remove_keys[i]));
}
// There are three parts each of size "half":
// 1 is being deleted from [offset - half, offset)
// 2 is standing [offset, offset + half)
// 3 is being inserted into [offset + half, offset + 2 * half)
size_t offset = 0;
for (size_t i = 0; i < kAddRemBatchSize; ++i) {
std::shuffle(remove_keys.begin(), remove_keys.end(), rand);
std::shuffle(add_keys.begin(), add_keys.end(), rand);
offset += half;
state.ResumeTiming();
for (size_t idx = 0; idx < half; ++idx) {
container.erase(key_of_value(g(offset - half + remove_keys[idx])));
container.insert(g(offset + half + add_keys[idx]));
}
state.PauseTiming();
}
state.ResumeTiming();
}
}
// Mixed insertion and deletion in the same range using pre-constructed values.
template <typename T>
void BM_MixedAddRem(benchmark::State& state) {
using V = typename remove_pair_const<typename T::value_type>::type;
typename KeyOfValue<typename T::key_type, V>::type key_of_value;
ABSL_RAW_CHECK(kBenchmarkValues % 2 == 0, "for performance");
T container;
// Create two random shuffles
std::vector<int> remove_keys(kBenchmarkValues);
std::vector<int> add_keys(kBenchmarkValues);
// We want to do the exact same work repeatedly, and the benchmark can end
// after a different number of iterations depending on the speed of the
// individual run so we use a large batch size here and ensure that we do
// deterministic work every batch.
while (state.KeepRunningBatch(kBenchmarkValues * kAddRemBatchSize)) {
state.PauseTiming();
container.clear();
constexpr int kSeed = 7;
std::mt19937_64 rand(kSeed);
std::vector<V> values = GenerateValues<V>(kBenchmarkValues * 2);
// Insert the first half of the values (already in random order)
container.insert(values.begin(), values.begin() + kBenchmarkValues);
// Insert the first half of the values (already in random order)
for (size_t i = 0; i < kBenchmarkValues; ++i) {
// remove_keys and add_keys will be swapped before each round,
// therefore fill add_keys here w/ the keys being inserted, so
// they'll be the first to be removed.
remove_keys[i] = i + kBenchmarkValues;
add_keys[i] = i;
}
for (size_t i = 0; i < kAddRemBatchSize; ++i) {
remove_keys.swap(add_keys);
std::shuffle(remove_keys.begin(), remove_keys.end(), rand);
std::shuffle(add_keys.begin(), add_keys.end(), rand);
state.ResumeTiming();
for (size_t idx = 0; idx < kBenchmarkValues; ++idx) {
container.erase(key_of_value(values[remove_keys[idx]]));
container.insert(values[add_keys[idx]]);
}
state.PauseTiming();
}
state.ResumeTiming();
}
}
// Insertion at end, removal from the beginning. This benchmark
// counts two value constructors.
// TODO(ezb): we could add a GenerateNext version of generator that could reduce
// noise for string-like types.
template <typename T>
void BM_Fifo(benchmark::State& state) {
using V = typename remove_pair_const<typename T::value_type>::type;
T container;
// Need lazy generation of values as state.max_iterations is large.
Generator<V> g(kBenchmarkValues + state.max_iterations);
for (int i = 0; i < kBenchmarkValues; i++) {
container.insert(g(i));
}
while (state.KeepRunning()) {
container.erase(container.begin());
container.insert(container.end(), g(state.iterations() + kBenchmarkValues));
}
}
// Iteration (forward) through the tree
template <typename T>
void BM_FwdIter(benchmark::State& state) {
using V = typename remove_pair_const<typename T::value_type>::type;
using R = typename T::value_type const*;
std::vector<V> values = GenerateValues<V>(kBenchmarkValues);
T container(values.begin(), values.end());
auto iter = container.end();
R r = nullptr;
while (state.KeepRunning()) {
if (iter == container.end()) iter = container.begin();
r = &(*iter);
++iter;
}
benchmark::DoNotOptimize(r);
}
// Benchmark random range-construction of a container.
template <typename T>
void BM_RangeConstructionImpl(benchmark::State& state, bool sorted) {
using V = typename remove_pair_const<typename T::value_type>::type;
std::vector<V> values = GenerateValues<V>(kBenchmarkValues);
if (sorted) {
std::sort(values.begin(), values.end());
}
{
T container(values.begin(), values.end());
}
while (state.KeepRunning()) {
T container(values.begin(), values.end());
benchmark::DoNotOptimize(container);
}
}
template <typename T>
void BM_InsertRangeRandom(benchmark::State& state) {
BM_RangeConstructionImpl<T>(state, false);
}
template <typename T>
void BM_InsertRangeSorted(benchmark::State& state) {
BM_RangeConstructionImpl<T>(state, true);
}
#define STL_ORDERED_TYPES(value) \
using stl_set_##value = std::set<value>; \
using stl_map_##value = std::map<value, intptr_t>; \
using stl_multiset_##value = std::multiset<value>; \
using stl_multimap_##value = std::multimap<value, intptr_t>
using StdString = std::string;
STL_ORDERED_TYPES(int32_t);
STL_ORDERED_TYPES(int64_t);
STL_ORDERED_TYPES(StdString);
STL_ORDERED_TYPES(Cord);
STL_ORDERED_TYPES(Time);
#define STL_UNORDERED_TYPES(value) \
using stl_unordered_set_##value = std::unordered_set<value>; \
using stl_unordered_map_##value = std::unordered_map<value, intptr_t>; \
using flat_hash_set_##value = flat_hash_set<value>; \
using flat_hash_map_##value = flat_hash_map<value, intptr_t>; \
using stl_unordered_multiset_##value = std::unordered_multiset<value>; \
using stl_unordered_multimap_##value = \
std::unordered_multimap<value, intptr_t>
#define STL_UNORDERED_TYPES_CUSTOM_HASH(value, hash) \
using stl_unordered_set_##value = std::unordered_set<value, hash>; \
using stl_unordered_map_##value = std::unordered_map<value, intptr_t, hash>; \
using flat_hash_set_##value = flat_hash_set<value, hash>; \
using flat_hash_map_##value = flat_hash_map<value, intptr_t, hash>; \
using stl_unordered_multiset_##value = std::unordered_multiset<value, hash>; \
using stl_unordered_multimap_##value = \
std::unordered_multimap<value, intptr_t, hash>
STL_UNORDERED_TYPES_CUSTOM_HASH(Cord, absl::Hash<absl::Cord>);
STL_UNORDERED_TYPES(int32_t);
STL_UNORDERED_TYPES(int64_t);
STL_UNORDERED_TYPES(StdString);
STL_UNORDERED_TYPES_CUSTOM_HASH(Time, absl::Hash<absl::Time>);
#define BTREE_TYPES(value) \
using btree_256_set_##value = \
btree_set<value, std::less<value>, std::allocator<value>>; \
using btree_256_map_##value = \
btree_map<value, intptr_t, std::less<value>, \
std::allocator<std::pair<const value, intptr_t>>>; \
using btree_256_multiset_##value = \
btree_multiset<value, std::less<value>, std::allocator<value>>; \
using btree_256_multimap_##value = \
btree_multimap<value, intptr_t, std::less<value>, \
std::allocator<std::pair<const value, intptr_t>>>
BTREE_TYPES(int32_t);
BTREE_TYPES(int64_t);
BTREE_TYPES(StdString);
BTREE_TYPES(Cord);
BTREE_TYPES(Time);
#define MY_BENCHMARK4(type, func) \
void BM_##type##_##func(benchmark::State& state) { BM_##func<type>(state); } \
BENCHMARK(BM_##type##_##func)
#define MY_BENCHMARK3_STL(type) \
MY_BENCHMARK4(type, Insert); \
MY_BENCHMARK4(type, InsertSorted); \
MY_BENCHMARK4(type, InsertSmall); \
MY_BENCHMARK4(type, Lookup); \
MY_BENCHMARK4(type, FullLookup); \
MY_BENCHMARK4(type, Erase); \
MY_BENCHMARK4(type, EraseRange); \
MY_BENCHMARK4(type, QueueAddRem); \
MY_BENCHMARK4(type, MixedAddRem); \
MY_BENCHMARK4(type, Fifo); \
MY_BENCHMARK4(type, FwdIter); \
MY_BENCHMARK4(type, InsertRangeRandom); \
MY_BENCHMARK4(type, InsertRangeSorted)
#define MY_BENCHMARK3(type) \
MY_BENCHMARK4(type, EraseIf); \
MY_BENCHMARK3_STL(type)
#define MY_BENCHMARK2_SUPPORTS_MULTI_ONLY(type) \
MY_BENCHMARK3_STL(stl_##type); \
MY_BENCHMARK3_STL(stl_unordered_##type); \
MY_BENCHMARK3(btree_256_##type)
#define MY_BENCHMARK2(type) \
MY_BENCHMARK2_SUPPORTS_MULTI_ONLY(type); \
MY_BENCHMARK3(flat_hash_##type)
// Define MULTI_TESTING to see benchmarks for multi-containers also.
//
// You can use --copt=-DMULTI_TESTING.
#ifdef MULTI_TESTING
#define MY_BENCHMARK(type) \
MY_BENCHMARK2(set_##type); \
MY_BENCHMARK2(map_##type); \
MY_BENCHMARK2_SUPPORTS_MULTI_ONLY(multiset_##type); \
MY_BENCHMARK2_SUPPORTS_MULTI_ONLY(multimap_##type)
#else
#define MY_BENCHMARK(type) \
MY_BENCHMARK2(set_##type); \
MY_BENCHMARK2(map_##type)
#endif
MY_BENCHMARK(int32_t);
MY_BENCHMARK(int64_t);
MY_BENCHMARK(StdString);
MY_BENCHMARK(Cord);
MY_BENCHMARK(Time);
// Define a type whose size and cost of moving are independently customizable.
// When sizeof(value_type) increases, we expect btree to no longer have as much
// cache-locality advantage over STL. When cost of moving increases, we expect
// btree to actually do more work than STL because it has to move values around
// and STL doesn't have to.
template <int Size, int Copies>
struct BigType {
BigType() : BigType(0) {}
explicit BigType(int x) { std::iota(values.begin(), values.end(), x); }
void Copy(const BigType& other) {
for (int i = 0; i < Size && i < Copies; ++i) values[i] = other.values[i];
// If Copies > Size, do extra copies.
for (int i = Size, idx = 0; i < Copies; ++i) {
int64_t tmp = other.values[idx];
benchmark::DoNotOptimize(tmp);
idx = idx + 1 == Size ? 0 : idx + 1;
}
}
BigType(const BigType& other) { Copy(other); }
BigType& operator=(const BigType& other) {
Copy(other);
return *this;
}
// Compare only the first Copies elements if Copies is less than Size.
bool operator<(const BigType& other) const {
return std::lexicographical_compare(
values.begin(), values.begin() + std::min(Size, Copies),
other.values.begin(), other.values.begin() + std::min(Size, Copies));
}
bool operator==(const BigType& other) const {
return std::equal(values.begin(), values.begin() + std::min(Size, Copies),
other.values.begin());
}
// Support absl::Hash.
template <typename State>
friend State AbslHashValue(State h, const BigType& b) {
for (int i = 0; i < Size && i < Copies; ++i)
h = State::combine(std::move(h), b.values[i]);
return h;
}
std::array<int64_t, Size> values;
};
#define BIG_TYPE_BENCHMARKS(SIZE, COPIES) \
using stl_set_size##SIZE##copies##COPIES = std::set<BigType<SIZE, COPIES>>; \
using stl_map_size##SIZE##copies##COPIES = \
std::map<BigType<SIZE, COPIES>, intptr_t>; \
using stl_multiset_size##SIZE##copies##COPIES = \
std::multiset<BigType<SIZE, COPIES>>; \
using stl_multimap_size##SIZE##copies##COPIES = \
std::multimap<BigType<SIZE, COPIES>, intptr_t>; \
using stl_unordered_set_size##SIZE##copies##COPIES = \
std::unordered_set<BigType<SIZE, COPIES>, \
absl::Hash<BigType<SIZE, COPIES>>>; \
using stl_unordered_map_size##SIZE##copies##COPIES = \
std::unordered_map<BigType<SIZE, COPIES>, intptr_t, \
absl::Hash<BigType<SIZE, COPIES>>>; \
using flat_hash_set_size##SIZE##copies##COPIES = \
flat_hash_set<BigType<SIZE, COPIES>>; \
using flat_hash_map_size##SIZE##copies##COPIES = \
flat_hash_map<BigType<SIZE, COPIES>, intptr_t>; \
using stl_unordered_multiset_size##SIZE##copies##COPIES = \
std::unordered_multiset<BigType<SIZE, COPIES>, \
absl::Hash<BigType<SIZE, COPIES>>>; \
using stl_unordered_multimap_size##SIZE##copies##COPIES = \
std::unordered_multimap<BigType<SIZE, COPIES>, intptr_t, \
absl::Hash<BigType<SIZE, COPIES>>>; \
using btree_256_set_size##SIZE##copies##COPIES = \
btree_set<BigType<SIZE, COPIES>>; \
using btree_256_map_size##SIZE##copies##COPIES = \
btree_map<BigType<SIZE, COPIES>, intptr_t>; \
using btree_256_multiset_size##SIZE##copies##COPIES = \
btree_multiset<BigType<SIZE, COPIES>>; \
using btree_256_multimap_size##SIZE##copies##COPIES = \
btree_multimap<BigType<SIZE, COPIES>, intptr_t>; \
MY_BENCHMARK(size##SIZE##copies##COPIES)
// Define BIG_TYPE_TESTING to see benchmarks for more big types.
//
// You can use --copt=-DBIG_TYPE_TESTING.
#ifndef NODESIZE_TESTING
#ifdef BIG_TYPE_TESTING
BIG_TYPE_BENCHMARKS(1, 4);
BIG_TYPE_BENCHMARKS(4, 1);
BIG_TYPE_BENCHMARKS(4, 4);
BIG_TYPE_BENCHMARKS(1, 8);
BIG_TYPE_BENCHMARKS(8, 1);
BIG_TYPE_BENCHMARKS(8, 8);
BIG_TYPE_BENCHMARKS(1, 16);
BIG_TYPE_BENCHMARKS(16, 1);
BIG_TYPE_BENCHMARKS(16, 16);
BIG_TYPE_BENCHMARKS(1, 32);
BIG_TYPE_BENCHMARKS(32, 1);
BIG_TYPE_BENCHMARKS(32, 32);
#else
BIG_TYPE_BENCHMARKS(32, 32);
#endif
#endif
// Benchmark using unique_ptrs to large value types. In order to be able to use
// the same benchmark code as the other types, use a type that holds a
// unique_ptr and has a copy constructor.
template <int Size>
struct BigTypePtr {
BigTypePtr() : BigTypePtr(0) {}
explicit BigTypePtr(int x) {
ptr = absl::make_unique<BigType<Size, Size>>(x);
}
BigTypePtr(const BigTypePtr& other) {
ptr = absl::make_unique<BigType<Size, Size>>(*other.ptr);
}
BigTypePtr(BigTypePtr&& other) noexcept = default;
BigTypePtr& operator=(const BigTypePtr& other) {
ptr = absl::make_unique<BigType<Size, Size>>(*other.ptr);
}
BigTypePtr& operator=(BigTypePtr&& other) noexcept = default;
bool operator<(const BigTypePtr& other) const { return *ptr < *other.ptr; }
bool operator==(const BigTypePtr& other) const { return *ptr == *other.ptr; }
std::unique_ptr<BigType<Size, Size>> ptr;
};
template <int Size>
double ContainerInfo(const btree_set<BigTypePtr<Size>>& b) {
const double bytes_used =
b.bytes_used() + b.size() * sizeof(BigType<Size, Size>);
const double bytes_per_value = bytes_used / b.size();
BtreeContainerInfoLog(b, bytes_used, bytes_per_value);
return bytes_per_value;
}
template <int Size>
double ContainerInfo(const btree_map<int, BigTypePtr<Size>>& b) {
const double bytes_used =
b.bytes_used() + b.size() * sizeof(BigType<Size, Size>);
const double bytes_per_value = bytes_used / b.size();
BtreeContainerInfoLog(b, bytes_used, bytes_per_value);
return bytes_per_value;
}
#define BIG_TYPE_PTR_BENCHMARKS(SIZE) \
using stl_set_size##SIZE##copies##SIZE##ptr = std::set<BigType<SIZE, SIZE>>; \
using stl_map_size##SIZE##copies##SIZE##ptr = \
std::map<int, BigType<SIZE, SIZE>>; \
using stl_unordered_set_size##SIZE##copies##SIZE##ptr = \
std::unordered_set<BigType<SIZE, SIZE>, \
absl::Hash<BigType<SIZE, SIZE>>>; \
using stl_unordered_map_size##SIZE##copies##SIZE##ptr = \
std::unordered_map<int, BigType<SIZE, SIZE>>; \
using flat_hash_set_size##SIZE##copies##SIZE##ptr = \
flat_hash_set<BigType<SIZE, SIZE>>; \
using flat_hash_map_size##SIZE##copies##SIZE##ptr = \
flat_hash_map<int, BigTypePtr<SIZE>>; \
using btree_256_set_size##SIZE##copies##SIZE##ptr = \
btree_set<BigTypePtr<SIZE>>; \
using btree_256_map_size##SIZE##copies##SIZE##ptr = \
btree_map<int, BigTypePtr<SIZE>>; \
MY_BENCHMARK3_STL(stl_set_size##SIZE##copies##SIZE##ptr); \
MY_BENCHMARK3_STL(stl_unordered_set_size##SIZE##copies##SIZE##ptr); \
MY_BENCHMARK3(flat_hash_set_size##SIZE##copies##SIZE##ptr); \
MY_BENCHMARK3(btree_256_set_size##SIZE##copies##SIZE##ptr); \
MY_BENCHMARK3_STL(stl_map_size##SIZE##copies##SIZE##ptr); \
MY_BENCHMARK3_STL(stl_unordered_map_size##SIZE##copies##SIZE##ptr); \
MY_BENCHMARK3(flat_hash_map_size##SIZE##copies##SIZE##ptr); \
MY_BENCHMARK3(btree_256_map_size##SIZE##copies##SIZE##ptr)
BIG_TYPE_PTR_BENCHMARKS(32);
void BM_BtreeSet_IteratorSubtraction(benchmark::State& state) {
absl::InsecureBitGen bitgen;
std::vector<int> vec;
// Randomize the set's insertion order so the nodes aren't all full.
vec.reserve(state.range(0));
for (int i = 0; i < state.range(0); ++i) vec.push_back(i);
absl::c_shuffle(vec, bitgen);
absl::btree_set<int> set;
for (int i : vec) set.insert(i);
size_t distance = absl::Uniform(bitgen, 0u, set.size());
while (state.KeepRunningBatch(distance)) {
size_t end = absl::Uniform(bitgen, distance, set.size());
size_t begin = end - distance;
benchmark::DoNotOptimize(set.find(static_cast<int>(end)) -
set.find(static_cast<int>(begin)));
distance = absl::Uniform(bitgen, 0u, set.size());
}
}
BENCHMARK(BM_BtreeSet_IteratorSubtraction)->Range(1 << 10, 1 << 20);
} // namespace
} // namespace container_internal
ABSL_NAMESPACE_END
} // namespace absl

View file

@ -0,0 +1,889 @@
// Copyright 2018 The Abseil Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// -----------------------------------------------------------------------------
// File: btree_map.h
// -----------------------------------------------------------------------------
//
// This header file defines B-tree maps: sorted associative containers mapping
// keys to values.
//
// * `absl::btree_map<>`
// * `absl::btree_multimap<>`
//
// These B-tree types are similar to the corresponding types in the STL
// (`std::map` and `std::multimap`) and generally conform to the STL interfaces
// of those types. However, because they are implemented using B-trees, they
// are more efficient in most situations.
//
// Unlike `std::map` and `std::multimap`, which are commonly implemented using
// red-black tree nodes, B-tree maps use more generic B-tree nodes able to hold
// multiple values per node. Holding multiple values per node often makes
// B-tree maps perform better than their `std::map` counterparts, because
// multiple entries can be checked within the same cache hit.
//
// However, these types should not be considered drop-in replacements for
// `std::map` and `std::multimap` as there are some API differences, which are
// noted in this header file. The most consequential differences with respect to
// migrating to b-tree from the STL types are listed in the next paragraph.
// Other API differences are minor.
//
// Importantly, insertions and deletions may invalidate outstanding iterators,
// pointers, and references to elements. Such invalidations are typically only
// an issue if insertion and deletion operations are interleaved with the use of
// more than one iterator, pointer, or reference simultaneously. For this
// reason, `insert()`, `erase()`, and `extract_and_get_next()` return a valid
// iterator at the current position. Another important difference is that
// key-types must be copy-constructible.
//
// Another API difference is that btree iterators can be subtracted, and this
// is faster than using std::distance.
//
// B-tree maps are not exception-safe.
#ifndef ABSL_CONTAINER_BTREE_MAP_H_
#define ABSL_CONTAINER_BTREE_MAP_H_
#include "absl/base/attributes.h"
#include "absl/container/internal/btree.h" // IWYU pragma: export
#include "absl/container/internal/btree_container.h" // IWYU pragma: export
namespace absl {
ABSL_NAMESPACE_BEGIN
namespace container_internal {
template <typename Key, typename Data, typename Compare, typename Alloc,
int TargetNodeSize, bool IsMulti>
struct map_params;
} // namespace container_internal
// absl::btree_map<>
//
// An `absl::btree_map<K, V>` is an ordered associative container of
// unique keys and associated values designed to be a more efficient replacement
// for `std::map` (in most cases).
//
// Keys are sorted using an (optional) comparison function, which defaults to
// `std::less<K>`.
//
// An `absl::btree_map<K, V>` uses a default allocator of
// `std::allocator<std::pair<const K, V>>` to allocate (and deallocate)
// nodes, and construct and destruct values within those nodes. You may
// instead specify a custom allocator `A` (which in turn requires specifying a
// custom comparator `C`) as in `absl::btree_map<K, V, C, A>`.
//
template <typename Key, typename Value, typename Compare = std::less<Key>,
typename Alloc = std::allocator<std::pair<const Key, Value>>>
class ABSL_ATTRIBUTE_OWNER btree_map
: public container_internal::btree_map_container<
container_internal::btree<container_internal::map_params<
Key, Value, Compare, Alloc, /*TargetNodeSize=*/256,
/*IsMulti=*/false>>> {
using Base = typename btree_map::btree_map_container;
public:
// Constructors and Assignment Operators
//
// A `btree_map` supports the same overload set as `std::map`
// for construction and assignment:
//
// * Default constructor
//
// absl::btree_map<int, std::string> map1;
//
// * Initializer List constructor
//
// absl::btree_map<int, std::string> map2 =
// {{1, "huey"}, {2, "dewey"}, {3, "louie"},};
//
// * Copy constructor
//
// absl::btree_map<int, std::string> map3(map2);
//
// * Copy assignment operator
//
// absl::btree_map<int, std::string> map4;
// map4 = map3;
//
// * Move constructor
//
// // Move is guaranteed efficient
// absl::btree_map<int, std::string> map5(std::move(map4));
//
// * Move assignment operator
//
// // May be efficient if allocators are compatible
// absl::btree_map<int, std::string> map6;
// map6 = std::move(map5);
//
// * Range constructor
//
// std::vector<std::pair<int, std::string>> v = {{1, "a"}, {2, "b"}};
// absl::btree_map<int, std::string> map7(v.begin(), v.end());
btree_map() {}
using Base::Base;
// btree_map::begin()
//
// Returns an iterator to the beginning of the `btree_map`.
using Base::begin;
// btree_map::cbegin()
//
// Returns a const iterator to the beginning of the `btree_map`.
using Base::cbegin;
// btree_map::end()
//
// Returns an iterator to the end of the `btree_map`.
using Base::end;
// btree_map::cend()
//
// Returns a const iterator to the end of the `btree_map`.
using Base::cend;
// btree_map::empty()
//
// Returns whether or not the `btree_map` is empty.
using Base::empty;
// btree_map::max_size()
//
// Returns the largest theoretical possible number of elements within a
// `btree_map` under current memory constraints. This value can be thought
// of as the largest value of `std::distance(begin(), end())` for a
// `btree_map<Key, T>`.
using Base::max_size;
// btree_map::size()
//
// Returns the number of elements currently within the `btree_map`.
using Base::size;
// btree_map::clear()
//
// Removes all elements from the `btree_map`. Invalidates any references,
// pointers, or iterators referring to contained elements.
using Base::clear;
// btree_map::erase()
//
// Erases elements within the `btree_map`. If an erase occurs, any references,
// pointers, or iterators are invalidated.
// Overloads are listed below.
//
// iterator erase(iterator position):
// iterator erase(const_iterator position):
//
// Erases the element at `position` of the `btree_map`, returning
// the iterator pointing to the element after the one that was erased
// (or end() if none exists).
//
// iterator erase(const_iterator first, const_iterator last):
//
// Erases the elements in the open interval [`first`, `last`), returning
// the iterator pointing to the element after the interval that was erased
// (or end() if none exists).
//
// template <typename K> size_type erase(const K& key):
//
// Erases the element with the matching key, if it exists, returning the
// number of elements erased (0 or 1).
using Base::erase;
// btree_map::insert()
//
// Inserts an element of the specified value into the `btree_map`,
// returning an iterator pointing to the newly inserted element, provided that
// an element with the given key does not already exist. If an insertion
// occurs, any references, pointers, or iterators are invalidated.
// Overloads are listed below.
//
// std::pair<iterator,bool> insert(const value_type& value):
//
// Inserts a value into the `btree_map`. Returns a pair consisting of an
// iterator to the inserted element (or to the element that prevented the
// insertion) and a bool denoting whether the insertion took place.
//
// std::pair<iterator,bool> insert(value_type&& value):
//
// Inserts a moveable value into the `btree_map`. Returns a pair
// consisting of an iterator to the inserted element (or to the element that
// prevented the insertion) and a bool denoting whether the insertion took
// place.
//
// iterator insert(const_iterator hint, const value_type& value):
// iterator insert(const_iterator hint, value_type&& value):
//
// Inserts a value, using the position of `hint` as a non-binding suggestion
// for where to begin the insertion search. Returns an iterator to the
// inserted element, or to the existing element that prevented the
// insertion.
//
// void insert(InputIterator first, InputIterator last):
//
// Inserts a range of values [`first`, `last`).
//
// void insert(std::initializer_list<init_type> ilist):
//
// Inserts the elements within the initializer list `ilist`.
using Base::insert;
// btree_map::insert_or_assign()
//
// Inserts an element of the specified value into the `btree_map` provided
// that a value with the given key does not already exist, or replaces the
// corresponding mapped type with the forwarded `obj` argument if a key for
// that value already exists, returning an iterator pointing to the newly
// inserted element. Overloads are listed below.
//
// pair<iterator, bool> insert_or_assign(const key_type& k, M&& obj):
// pair<iterator, bool> insert_or_assign(key_type&& k, M&& obj):
//
// Inserts/Assigns (or moves) the element of the specified key into the
// `btree_map`. If the returned bool is true, insertion took place, and if
// it's false, assignment took place.
//
// iterator insert_or_assign(const_iterator hint,
// const key_type& k, M&& obj):
// iterator insert_or_assign(const_iterator hint, key_type&& k, M&& obj):
//
// Inserts/Assigns (or moves) the element of the specified key into the
// `btree_map` using the position of `hint` as a non-binding suggestion
// for where to begin the insertion search.
using Base::insert_or_assign;
// btree_map::emplace()
//
// Inserts an element of the specified value by constructing it in-place
// within the `btree_map`, provided that no element with the given key
// already exists.
//
// The element may be constructed even if there already is an element with the
// key in the container, in which case the newly constructed element will be
// destroyed immediately. Prefer `try_emplace()` unless your key is not
// copyable or moveable.
//
// If an insertion occurs, any references, pointers, or iterators are
// invalidated.
using Base::emplace;
// btree_map::emplace_hint()
//
// Inserts an element of the specified value by constructing it in-place
// within the `btree_map`, using the position of `hint` as a non-binding
// suggestion for where to begin the insertion search, and only inserts
// provided that no element with the given key already exists.
//
// The element may be constructed even if there already is an element with the
// key in the container, in which case the newly constructed element will be
// destroyed immediately. Prefer `try_emplace()` unless your key is not
// copyable or moveable.
//
// If an insertion occurs, any references, pointers, or iterators are
// invalidated.
using Base::emplace_hint;
// btree_map::try_emplace()
//
// Inserts an element of the specified value by constructing it in-place
// within the `btree_map`, provided that no element with the given key
// already exists. Unlike `emplace()`, if an element with the given key
// already exists, we guarantee that no element is constructed.
//
// If an insertion occurs, any references, pointers, or iterators are
// invalidated.
//
// Overloads are listed below.
//
// std::pair<iterator, bool> try_emplace(const key_type& k, Args&&... args):
// std::pair<iterator, bool> try_emplace(key_type&& k, Args&&... args):
//
// Inserts (via copy or move) the element of the specified key into the
// `btree_map`.
//
// iterator try_emplace(const_iterator hint,
// const key_type& k, Args&&... args):
// iterator try_emplace(const_iterator hint, key_type&& k, Args&&... args):
//
// Inserts (via copy or move) the element of the specified key into the
// `btree_map` using the position of `hint` as a non-binding suggestion
// for where to begin the insertion search.
using Base::try_emplace;
// btree_map::extract()
//
// Extracts the indicated element, erasing it in the process, and returns it
// as a C++17-compatible node handle. Any references, pointers, or iterators
// are invalidated. Overloads are listed below.
//
// node_type extract(const_iterator position):
//
// Extracts the element at the indicated position and returns a node handle
// owning that extracted data.
//
// template <typename K> node_type extract(const K& k):
//
// Extracts the element with the key matching the passed key value and
// returns a node handle owning that extracted data. If the `btree_map`
// does not contain an element with a matching key, this function returns an
// empty node handle.
//
// NOTE: when compiled in an earlier version of C++ than C++17,
// `node_type::key()` returns a const reference to the key instead of a
// mutable reference. We cannot safely return a mutable reference without
// std::launder (which is not available before C++17).
//
// NOTE: In this context, `node_type` refers to the C++17 concept of a
// move-only type that owns and provides access to the elements in associative
// containers (https://en.cppreference.com/w/cpp/container/node_handle).
// It does NOT refer to the data layout of the underlying btree.
using Base::extract;
// btree_map::extract_and_get_next()
//
// Extracts the indicated element, erasing it in the process, and returns it
// as a C++17-compatible node handle along with an iterator to the next
// element.
//
// extract_and_get_next_return_type extract_and_get_next(
// const_iterator position):
//
// Extracts the element at the indicated position, returns a struct
// containing a member named `node`: a node handle owning that extracted
// data and a member named `next`: an iterator pointing to the next element
// in the btree.
using Base::extract_and_get_next;
// btree_map::merge()
//
// Extracts elements from a given `source` btree_map into this
// `btree_map`. If the destination `btree_map` already contains an
// element with an equivalent key, that element is not extracted.
using Base::merge;
// btree_map::swap(btree_map& other)
//
// Exchanges the contents of this `btree_map` with those of the `other`
// btree_map, avoiding invocation of any move, copy, or swap operations on
// individual elements.
//
// All iterators and references on the `btree_map` remain valid, excepting
// for the past-the-end iterator, which is invalidated.
using Base::swap;
// btree_map::at()
//
// Returns a reference to the mapped value of the element with key equivalent
// to the passed key.
using Base::at;
// btree_map::contains()
//
// template <typename K> bool contains(const K& key) const:
//
// Determines whether an element comparing equal to the given `key` exists
// within the `btree_map`, returning `true` if so or `false` otherwise.
//
// Supports heterogeneous lookup, provided that the map has a compatible
// heterogeneous comparator.
using Base::contains;
// btree_map::count()
//
// template <typename K> size_type count(const K& key) const:
//
// Returns the number of elements comparing equal to the given `key` within
// the `btree_map`. Note that this function will return either `1` or `0`
// since duplicate elements are not allowed within a `btree_map`.
//
// Supports heterogeneous lookup, provided that the map has a compatible
// heterogeneous comparator.
using Base::count;
// btree_map::equal_range()
//
// Returns a half-open range [first, last), defined by a `std::pair` of two
// iterators, containing all elements with the passed key in the `btree_map`.
using Base::equal_range;
// btree_map::find()
//
// template <typename K> iterator find(const K& key):
// template <typename K> const_iterator find(const K& key) const:
//
// Finds an element with the passed `key` within the `btree_map`.
//
// Supports heterogeneous lookup, provided that the map has a compatible
// heterogeneous comparator.
using Base::find;
// btree_map::lower_bound()
//
// template <typename K> iterator lower_bound(const K& key):
// template <typename K> const_iterator lower_bound(const K& key) const:
//
// Finds the first element with a key that is not less than `key` within the
// `btree_map`.
//
// Supports heterogeneous lookup, provided that the map has a compatible
// heterogeneous comparator.
using Base::lower_bound;
// btree_map::upper_bound()
//
// template <typename K> iterator upper_bound(const K& key):
// template <typename K> const_iterator upper_bound(const K& key) const:
//
// Finds the first element with a key that is greater than `key` within the
// `btree_map`.
//
// Supports heterogeneous lookup, provided that the map has a compatible
// heterogeneous comparator.
using Base::upper_bound;
// btree_map::operator[]()
//
// Returns a reference to the value mapped to the passed key within the
// `btree_map`, performing an `insert()` if the key does not already
// exist.
//
// If an insertion occurs, any references, pointers, or iterators are
// invalidated. Otherwise iterators are not affected and references are not
// invalidated. Overloads are listed below.
//
// T& operator[](key_type&& key):
// T& operator[](const key_type& key):
//
// Inserts a value_type object constructed in-place if the element with the
// given key does not exist.
using Base::operator[];
// btree_map::get_allocator()
//
// Returns the allocator function associated with this `btree_map`.
using Base::get_allocator;
// btree_map::key_comp();
//
// Returns the key comparator associated with this `btree_map`.
using Base::key_comp;
// btree_map::value_comp();
//
// Returns the value comparator associated with this `btree_map`.
using Base::value_comp;
};
// absl::swap(absl::btree_map<>, absl::btree_map<>)
//
// Swaps the contents of two `absl::btree_map` containers.
template <typename K, typename V, typename C, typename A>
void swap(btree_map<K, V, C, A> &x, btree_map<K, V, C, A> &y) {
return x.swap(y);
}
// absl::erase_if(absl::btree_map<>, Pred)
//
// Erases all elements that satisfy the predicate pred from the container.
// Returns the number of erased elements.
template <typename K, typename V, typename C, typename A, typename Pred>
typename btree_map<K, V, C, A>::size_type erase_if(
btree_map<K, V, C, A> &map, Pred pred) {
return container_internal::btree_access::erase_if(map, std::move(pred));
}
// absl::btree_multimap
//
// An `absl::btree_multimap<K, V>` is an ordered associative container of
// keys and associated values designed to be a more efficient replacement for
// `std::multimap` (in most cases). Unlike `absl::btree_map`, a B-tree multimap
// allows multiple elements with equivalent keys.
//
// Keys are sorted using an (optional) comparison function, which defaults to
// `std::less<K>`.
//
// An `absl::btree_multimap<K, V>` uses a default allocator of
// `std::allocator<std::pair<const K, V>>` to allocate (and deallocate)
// nodes, and construct and destruct values within those nodes. You may
// instead specify a custom allocator `A` (which in turn requires specifying a
// custom comparator `C`) as in `absl::btree_multimap<K, V, C, A>`.
//
template <typename Key, typename Value, typename Compare = std::less<Key>,
typename Alloc = std::allocator<std::pair<const Key, Value>>>
class ABSL_ATTRIBUTE_OWNER btree_multimap
: public container_internal::btree_multimap_container<
container_internal::btree<container_internal::map_params<
Key, Value, Compare, Alloc, /*TargetNodeSize=*/256,
/*IsMulti=*/true>>> {
using Base = typename btree_multimap::btree_multimap_container;
public:
// Constructors and Assignment Operators
//
// A `btree_multimap` supports the same overload set as `std::multimap`
// for construction and assignment:
//
// * Default constructor
//
// absl::btree_multimap<int, std::string> map1;
//
// * Initializer List constructor
//
// absl::btree_multimap<int, std::string> map2 =
// {{1, "huey"}, {2, "dewey"}, {3, "louie"},};
//
// * Copy constructor
//
// absl::btree_multimap<int, std::string> map3(map2);
//
// * Copy assignment operator
//
// absl::btree_multimap<int, std::string> map4;
// map4 = map3;
//
// * Move constructor
//
// // Move is guaranteed efficient
// absl::btree_multimap<int, std::string> map5(std::move(map4));
//
// * Move assignment operator
//
// // May be efficient if allocators are compatible
// absl::btree_multimap<int, std::string> map6;
// map6 = std::move(map5);
//
// * Range constructor
//
// std::vector<std::pair<int, std::string>> v = {{1, "a"}, {2, "b"}};
// absl::btree_multimap<int, std::string> map7(v.begin(), v.end());
btree_multimap() {}
using Base::Base;
// btree_multimap::begin()
//
// Returns an iterator to the beginning of the `btree_multimap`.
using Base::begin;
// btree_multimap::cbegin()
//
// Returns a const iterator to the beginning of the `btree_multimap`.
using Base::cbegin;
// btree_multimap::end()
//
// Returns an iterator to the end of the `btree_multimap`.
using Base::end;
// btree_multimap::cend()
//
// Returns a const iterator to the end of the `btree_multimap`.
using Base::cend;
// btree_multimap::empty()
//
// Returns whether or not the `btree_multimap` is empty.
using Base::empty;
// btree_multimap::max_size()
//
// Returns the largest theoretical possible number of elements within a
// `btree_multimap` under current memory constraints. This value can be
// thought of as the largest value of `std::distance(begin(), end())` for a
// `btree_multimap<Key, T>`.
using Base::max_size;
// btree_multimap::size()
//
// Returns the number of elements currently within the `btree_multimap`.
using Base::size;
// btree_multimap::clear()
//
// Removes all elements from the `btree_multimap`. Invalidates any references,
// pointers, or iterators referring to contained elements.
using Base::clear;
// btree_multimap::erase()
//
// Erases elements within the `btree_multimap`. If an erase occurs, any
// references, pointers, or iterators are invalidated.
// Overloads are listed below.
//
// iterator erase(iterator position):
// iterator erase(const_iterator position):
//
// Erases the element at `position` of the `btree_multimap`, returning
// the iterator pointing to the element after the one that was erased
// (or end() if none exists).
//
// iterator erase(const_iterator first, const_iterator last):
//
// Erases the elements in the open interval [`first`, `last`), returning
// the iterator pointing to the element after the interval that was erased
// (or end() if none exists).
//
// template <typename K> size_type erase(const K& key):
//
// Erases the elements matching the key, if any exist, returning the
// number of elements erased.
using Base::erase;
// btree_multimap::insert()
//
// Inserts an element of the specified value into the `btree_multimap`,
// returning an iterator pointing to the newly inserted element.
// Any references, pointers, or iterators are invalidated. Overloads are
// listed below.
//
// iterator insert(const value_type& value):
//
// Inserts a value into the `btree_multimap`, returning an iterator to the
// inserted element.
//
// iterator insert(value_type&& value):
//
// Inserts a moveable value into the `btree_multimap`, returning an iterator
// to the inserted element.
//
// iterator insert(const_iterator hint, const value_type& value):
// iterator insert(const_iterator hint, value_type&& value):
//
// Inserts a value, using the position of `hint` as a non-binding suggestion
// for where to begin the insertion search. Returns an iterator to the
// inserted element.
//
// void insert(InputIterator first, InputIterator last):
//
// Inserts a range of values [`first`, `last`).
//
// void insert(std::initializer_list<init_type> ilist):
//
// Inserts the elements within the initializer list `ilist`.
using Base::insert;
// btree_multimap::emplace()
//
// Inserts an element of the specified value by constructing it in-place
// within the `btree_multimap`. Any references, pointers, or iterators are
// invalidated.
using Base::emplace;
// btree_multimap::emplace_hint()
//
// Inserts an element of the specified value by constructing it in-place
// within the `btree_multimap`, using the position of `hint` as a non-binding
// suggestion for where to begin the insertion search.
//
// Any references, pointers, or iterators are invalidated.
using Base::emplace_hint;
// btree_multimap::extract()
//
// Extracts the indicated element, erasing it in the process, and returns it
// as a C++17-compatible node handle. Overloads are listed below.
//
// node_type extract(const_iterator position):
//
// Extracts the element at the indicated position and returns a node handle
// owning that extracted data.
//
// template <typename K> node_type extract(const K& k):
//
// Extracts the element with the key matching the passed key value and
// returns a node handle owning that extracted data. If the `btree_multimap`
// does not contain an element with a matching key, this function returns an
// empty node handle.
//
// NOTE: when compiled in an earlier version of C++ than C++17,
// `node_type::key()` returns a const reference to the key instead of a
// mutable reference. We cannot safely return a mutable reference without
// std::launder (which is not available before C++17).
//
// NOTE: In this context, `node_type` refers to the C++17 concept of a
// move-only type that owns and provides access to the elements in associative
// containers (https://en.cppreference.com/w/cpp/container/node_handle).
// It does NOT refer to the data layout of the underlying btree.
using Base::extract;
// btree_multimap::extract_and_get_next()
//
// Extracts the indicated element, erasing it in the process, and returns it
// as a C++17-compatible node handle along with an iterator to the next
// element.
//
// extract_and_get_next_return_type extract_and_get_next(
// const_iterator position):
//
// Extracts the element at the indicated position, returns a struct
// containing a member named `node`: a node handle owning that extracted
// data and a member named `next`: an iterator pointing to the next element
// in the btree.
using Base::extract_and_get_next;
// btree_multimap::merge()
//
// Extracts all elements from a given `source` btree_multimap into this
// `btree_multimap`.
using Base::merge;
// btree_multimap::swap(btree_multimap& other)
//
// Exchanges the contents of this `btree_multimap` with those of the `other`
// btree_multimap, avoiding invocation of any move, copy, or swap operations
// on individual elements.
//
// All iterators and references on the `btree_multimap` remain valid,
// excepting for the past-the-end iterator, which is invalidated.
using Base::swap;
// btree_multimap::contains()
//
// template <typename K> bool contains(const K& key) const:
//
// Determines whether an element comparing equal to the given `key` exists
// within the `btree_multimap`, returning `true` if so or `false` otherwise.
//
// Supports heterogeneous lookup, provided that the map has a compatible
// heterogeneous comparator.
using Base::contains;
// btree_multimap::count()
//
// template <typename K> size_type count(const K& key) const:
//
// Returns the number of elements comparing equal to the given `key` within
// the `btree_multimap`.
//
// Supports heterogeneous lookup, provided that the map has a compatible
// heterogeneous comparator.
using Base::count;
// btree_multimap::equal_range()
//
// Returns a half-open range [first, last), defined by a `std::pair` of two
// iterators, containing all elements with the passed key in the
// `btree_multimap`.
using Base::equal_range;
// btree_multimap::find()
//
// template <typename K> iterator find(const K& key):
// template <typename K> const_iterator find(const K& key) const:
//
// Finds an element with the passed `key` within the `btree_multimap`.
//
// Supports heterogeneous lookup, provided that the map has a compatible
// heterogeneous comparator.
using Base::find;
// btree_multimap::lower_bound()
//
// template <typename K> iterator lower_bound(const K& key):
// template <typename K> const_iterator lower_bound(const K& key) const:
//
// Finds the first element with a key that is not less than `key` within the
// `btree_multimap`.
//
// Supports heterogeneous lookup, provided that the map has a compatible
// heterogeneous comparator.
using Base::lower_bound;
// btree_multimap::upper_bound()
//
// template <typename K> iterator upper_bound(const K& key):
// template <typename K> const_iterator upper_bound(const K& key) const:
//
// Finds the first element with a key that is greater than `key` within the
// `btree_multimap`.
//
// Supports heterogeneous lookup, provided that the map has a compatible
// heterogeneous comparator.
using Base::upper_bound;
// btree_multimap::get_allocator()
//
// Returns the allocator function associated with this `btree_multimap`.
using Base::get_allocator;
// btree_multimap::key_comp();
//
// Returns the key comparator associated with this `btree_multimap`.
using Base::key_comp;
// btree_multimap::value_comp();
//
// Returns the value comparator associated with this `btree_multimap`.
using Base::value_comp;
};
// absl::swap(absl::btree_multimap<>, absl::btree_multimap<>)
//
// Swaps the contents of two `absl::btree_multimap` containers.
template <typename K, typename V, typename C, typename A>
void swap(btree_multimap<K, V, C, A> &x, btree_multimap<K, V, C, A> &y) {
return x.swap(y);
}
// absl::erase_if(absl::btree_multimap<>, Pred)
//
// Erases all elements that satisfy the predicate pred from the container.
// Returns the number of erased elements.
template <typename K, typename V, typename C, typename A, typename Pred>
typename btree_multimap<K, V, C, A>::size_type erase_if(
btree_multimap<K, V, C, A> &map, Pred pred) {
return container_internal::btree_access::erase_if(map, std::move(pred));
}
namespace container_internal {
// A parameters structure for holding the type parameters for a btree_map.
// Compare and Alloc should be nothrow copy-constructible.
template <typename Key, typename Data, typename Compare, typename Alloc,
int TargetNodeSize, bool IsMulti>
struct map_params : common_params<Key, Compare, Alloc, TargetNodeSize, IsMulti,
/*IsMap=*/true, map_slot_policy<Key, Data>> {
using super_type = typename map_params::common_params;
using mapped_type = Data;
// This type allows us to move keys when it is safe to do so. It is safe
// for maps in which value_type and mutable_value_type are layout compatible.
using slot_policy = typename super_type::slot_policy;
using slot_type = typename super_type::slot_type;
using value_type = typename super_type::value_type;
using init_type = typename super_type::init_type;
template <typename V>
static auto key(const V &value ABSL_ATTRIBUTE_LIFETIME_BOUND)
-> decltype((value.first)) {
return value.first;
}
static const Key &key(const slot_type *s) { return slot_policy::key(s); }
static const Key &key(slot_type *s) { return slot_policy::key(s); }
// For use in node handle.
static auto mutable_key(slot_type *s)
-> decltype(slot_policy::mutable_key(s)) {
return slot_policy::mutable_key(s);
}
static mapped_type &value(value_type *value) { return value->second; }
};
} // namespace container_internal
ABSL_NAMESPACE_END
} // namespace absl
#endif // ABSL_CONTAINER_BTREE_MAP_H_

View file

@ -0,0 +1,824 @@
// Copyright 2018 The Abseil Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// -----------------------------------------------------------------------------
// File: btree_set.h
// -----------------------------------------------------------------------------
//
// This header file defines B-tree sets: sorted associative containers of
// values.
//
// * `absl::btree_set<>`
// * `absl::btree_multiset<>`
//
// These B-tree types are similar to the corresponding types in the STL
// (`std::set` and `std::multiset`) and generally conform to the STL interfaces
// of those types. However, because they are implemented using B-trees, they
// are more efficient in most situations.
//
// Unlike `std::set` and `std::multiset`, which are commonly implemented using
// red-black tree nodes, B-tree sets use more generic B-tree nodes able to hold
// multiple values per node. Holding multiple values per node often makes
// B-tree sets perform better than their `std::set` counterparts, because
// multiple entries can be checked within the same cache hit.
//
// However, these types should not be considered drop-in replacements for
// `std::set` and `std::multiset` as there are some API differences, which are
// noted in this header file. The most consequential differences with respect to
// migrating to b-tree from the STL types are listed in the next paragraph.
// Other API differences are minor.
//
// Importantly, insertions and deletions may invalidate outstanding iterators,
// pointers, and references to elements. Such invalidations are typically only
// an issue if insertion and deletion operations are interleaved with the use of
// more than one iterator, pointer, or reference simultaneously. For this
// reason, `insert()`, `erase()`, and `extract_and_get_next()` return a valid
// iterator at the current position.
//
// Another API difference is that btree iterators can be subtracted, and this
// is faster than using std::distance.
//
// B-tree sets are not exception-safe.
#ifndef ABSL_CONTAINER_BTREE_SET_H_
#define ABSL_CONTAINER_BTREE_SET_H_
#include "absl/base/attributes.h"
#include "absl/container/internal/btree.h" // IWYU pragma: export
#include "absl/container/internal/btree_container.h" // IWYU pragma: export
namespace absl {
ABSL_NAMESPACE_BEGIN
namespace container_internal {
template <typename Key>
struct set_slot_policy;
template <typename Key, typename Compare, typename Alloc, int TargetNodeSize,
bool IsMulti>
struct set_params;
} // namespace container_internal
// absl::btree_set<>
//
// An `absl::btree_set<K>` is an ordered associative container of unique key
// values designed to be a more efficient replacement for `std::set` (in most
// cases).
//
// Keys are sorted using an (optional) comparison function, which defaults to
// `std::less<K>`.
//
// An `absl::btree_set<K>` uses a default allocator of `std::allocator<K>` to
// allocate (and deallocate) nodes, and construct and destruct values within
// those nodes. You may instead specify a custom allocator `A` (which in turn
// requires specifying a custom comparator `C`) as in
// `absl::btree_set<K, C, A>`.
//
template <typename Key, typename Compare = std::less<Key>,
typename Alloc = std::allocator<Key>>
class ABSL_ATTRIBUTE_OWNER btree_set
: public container_internal::btree_set_container<
container_internal::btree<container_internal::set_params<
Key, Compare, Alloc, /*TargetNodeSize=*/256,
/*IsMulti=*/false>>> {
using Base = typename btree_set::btree_set_container;
public:
// Constructors and Assignment Operators
//
// A `btree_set` supports the same overload set as `std::set`
// for construction and assignment:
//
// * Default constructor
//
// absl::btree_set<std::string> set1;
//
// * Initializer List constructor
//
// absl::btree_set<std::string> set2 =
// {{"huey"}, {"dewey"}, {"louie"},};
//
// * Copy constructor
//
// absl::btree_set<std::string> set3(set2);
//
// * Copy assignment operator
//
// absl::btree_set<std::string> set4;
// set4 = set3;
//
// * Move constructor
//
// // Move is guaranteed efficient
// absl::btree_set<std::string> set5(std::move(set4));
//
// * Move assignment operator
//
// // May be efficient if allocators are compatible
// absl::btree_set<std::string> set6;
// set6 = std::move(set5);
//
// * Range constructor
//
// std::vector<std::string> v = {"a", "b"};
// absl::btree_set<std::string> set7(v.begin(), v.end());
btree_set() {}
using Base::Base;
// btree_set::begin()
//
// Returns an iterator to the beginning of the `btree_set`.
using Base::begin;
// btree_set::cbegin()
//
// Returns a const iterator to the beginning of the `btree_set`.
using Base::cbegin;
// btree_set::end()
//
// Returns an iterator to the end of the `btree_set`.
using Base::end;
// btree_set::cend()
//
// Returns a const iterator to the end of the `btree_set`.
using Base::cend;
// btree_set::empty()
//
// Returns whether or not the `btree_set` is empty.
using Base::empty;
// btree_set::max_size()
//
// Returns the largest theoretical possible number of elements within a
// `btree_set` under current memory constraints. This value can be thought
// of as the largest value of `std::distance(begin(), end())` for a
// `btree_set<Key>`.
using Base::max_size;
// btree_set::size()
//
// Returns the number of elements currently within the `btree_set`.
using Base::size;
// btree_set::clear()
//
// Removes all elements from the `btree_set`. Invalidates any references,
// pointers, or iterators referring to contained elements.
using Base::clear;
// btree_set::erase()
//
// Erases elements within the `btree_set`. Overloads are listed below.
//
// iterator erase(iterator position):
// iterator erase(const_iterator position):
//
// Erases the element at `position` of the `btree_set`, returning
// the iterator pointing to the element after the one that was erased
// (or end() if none exists).
//
// iterator erase(const_iterator first, const_iterator last):
//
// Erases the elements in the open interval [`first`, `last`), returning
// the iterator pointing to the element after the interval that was erased
// (or end() if none exists).
//
// template <typename K> size_type erase(const K& key):
//
// Erases the element with the matching key, if it exists, returning the
// number of elements erased (0 or 1).
using Base::erase;
// btree_set::insert()
//
// Inserts an element of the specified value into the `btree_set`,
// returning an iterator pointing to the newly inserted element, provided that
// an element with the given key does not already exist. If an insertion
// occurs, any references, pointers, or iterators are invalidated.
// Overloads are listed below.
//
// std::pair<iterator,bool> insert(const value_type& value):
//
// Inserts a value into the `btree_set`. Returns a pair consisting of an
// iterator to the inserted element (or to the element that prevented the
// insertion) and a bool denoting whether the insertion took place.
//
// std::pair<iterator,bool> insert(value_type&& value):
//
// Inserts a moveable value into the `btree_set`. Returns a pair
// consisting of an iterator to the inserted element (or to the element that
// prevented the insertion) and a bool denoting whether the insertion took
// place.
//
// iterator insert(const_iterator hint, const value_type& value):
// iterator insert(const_iterator hint, value_type&& value):
//
// Inserts a value, using the position of `hint` as a non-binding suggestion
// for where to begin the insertion search. Returns an iterator to the
// inserted element, or to the existing element that prevented the
// insertion.
//
// void insert(InputIterator first, InputIterator last):
//
// Inserts a range of values [`first`, `last`).
//
// void insert(std::initializer_list<init_type> ilist):
//
// Inserts the elements within the initializer list `ilist`.
using Base::insert;
// btree_set::emplace()
//
// Inserts an element of the specified value by constructing it in-place
// within the `btree_set`, provided that no element with the given key
// already exists.
//
// The element may be constructed even if there already is an element with the
// key in the container, in which case the newly constructed element will be
// destroyed immediately.
//
// If an insertion occurs, any references, pointers, or iterators are
// invalidated.
using Base::emplace;
// btree_set::emplace_hint()
//
// Inserts an element of the specified value by constructing it in-place
// within the `btree_set`, using the position of `hint` as a non-binding
// suggestion for where to begin the insertion search, and only inserts
// provided that no element with the given key already exists.
//
// The element may be constructed even if there already is an element with the
// key in the container, in which case the newly constructed element will be
// destroyed immediately.
//
// If an insertion occurs, any references, pointers, or iterators are
// invalidated.
using Base::emplace_hint;
// btree_set::extract()
//
// Extracts the indicated element, erasing it in the process, and returns it
// as a C++17-compatible node handle. Any references, pointers, or iterators
// are invalidated. Overloads are listed below.
//
// node_type extract(const_iterator position):
//
// Extracts the element at the indicated position and returns a node handle
// owning that extracted data.
//
// template <typename K> node_type extract(const K& k):
//
// Extracts the element with the key matching the passed key value and
// returns a node handle owning that extracted data. If the `btree_set`
// does not contain an element with a matching key, this function returns an
// empty node handle.
//
// NOTE: In this context, `node_type` refers to the C++17 concept of a
// move-only type that owns and provides access to the elements in associative
// containers (https://en.cppreference.com/w/cpp/container/node_handle).
// It does NOT refer to the data layout of the underlying btree.
using Base::extract;
// btree_set::extract_and_get_next()
//
// Extracts the indicated element, erasing it in the process, and returns it
// as a C++17-compatible node handle along with an iterator to the next
// element.
//
// extract_and_get_next_return_type extract_and_get_next(
// const_iterator position):
//
// Extracts the element at the indicated position, returns a struct
// containing a member named `node`: a node handle owning that extracted
// data and a member named `next`: an iterator pointing to the next element
// in the btree.
using Base::extract_and_get_next;
// btree_set::merge()
//
// Extracts elements from a given `source` btree_set into this
// `btree_set`. If the destination `btree_set` already contains an
// element with an equivalent key, that element is not extracted.
using Base::merge;
// btree_set::swap(btree_set& other)
//
// Exchanges the contents of this `btree_set` with those of the `other`
// btree_set, avoiding invocation of any move, copy, or swap operations on
// individual elements.
//
// All iterators and references on the `btree_set` remain valid, excepting
// for the past-the-end iterator, which is invalidated.
using Base::swap;
// btree_set::contains()
//
// template <typename K> bool contains(const K& key) const:
//
// Determines whether an element comparing equal to the given `key` exists
// within the `btree_set`, returning `true` if so or `false` otherwise.
//
// Supports heterogeneous lookup, provided that the set has a compatible
// heterogeneous comparator.
using Base::contains;
// btree_set::count()
//
// template <typename K> size_type count(const K& key) const:
//
// Returns the number of elements comparing equal to the given `key` within
// the `btree_set`. Note that this function will return either `1` or `0`
// since duplicate elements are not allowed within a `btree_set`.
//
// Supports heterogeneous lookup, provided that the set has a compatible
// heterogeneous comparator.
using Base::count;
// btree_set::equal_range()
//
// Returns a closed range [first, last], defined by a `std::pair` of two
// iterators, containing all elements with the passed key in the
// `btree_set`.
using Base::equal_range;
// btree_set::find()
//
// template <typename K> iterator find(const K& key):
// template <typename K> const_iterator find(const K& key) const:
//
// Finds an element with the passed `key` within the `btree_set`.
//
// Supports heterogeneous lookup, provided that the set has a compatible
// heterogeneous comparator.
using Base::find;
// btree_set::lower_bound()
//
// template <typename K> iterator lower_bound(const K& key):
// template <typename K> const_iterator lower_bound(const K& key) const:
//
// Finds the first element that is not less than `key` within the `btree_set`.
//
// Supports heterogeneous lookup, provided that the set has a compatible
// heterogeneous comparator.
using Base::lower_bound;
// btree_set::upper_bound()
//
// template <typename K> iterator upper_bound(const K& key):
// template <typename K> const_iterator upper_bound(const K& key) const:
//
// Finds the first element that is greater than `key` within the `btree_set`.
//
// Supports heterogeneous lookup, provided that the set has a compatible
// heterogeneous comparator.
using Base::upper_bound;
// btree_set::get_allocator()
//
// Returns the allocator function associated with this `btree_set`.
using Base::get_allocator;
// btree_set::key_comp();
//
// Returns the key comparator associated with this `btree_set`.
using Base::key_comp;
// btree_set::value_comp();
//
// Returns the value comparator associated with this `btree_set`. The keys to
// sort the elements are the values themselves, therefore `value_comp` and its
// sibling member function `key_comp` are equivalent.
using Base::value_comp;
};
// absl::swap(absl::btree_set<>, absl::btree_set<>)
//
// Swaps the contents of two `absl::btree_set` containers.
template <typename K, typename C, typename A>
void swap(btree_set<K, C, A> &x, btree_set<K, C, A> &y) {
return x.swap(y);
}
// absl::erase_if(absl::btree_set<>, Pred)
//
// Erases all elements that satisfy the predicate pred from the container.
// Returns the number of erased elements.
template <typename K, typename C, typename A, typename Pred>
typename btree_set<K, C, A>::size_type erase_if(btree_set<K, C, A> &set,
Pred pred) {
return container_internal::btree_access::erase_if(set, std::move(pred));
}
// absl::btree_multiset<>
//
// An `absl::btree_multiset<K>` is an ordered associative container of
// keys and associated values designed to be a more efficient replacement
// for `std::multiset` (in most cases). Unlike `absl::btree_set`, a B-tree
// multiset allows equivalent elements.
//
// Keys are sorted using an (optional) comparison function, which defaults to
// `std::less<K>`.
//
// An `absl::btree_multiset<K>` uses a default allocator of `std::allocator<K>`
// to allocate (and deallocate) nodes, and construct and destruct values within
// those nodes. You may instead specify a custom allocator `A` (which in turn
// requires specifying a custom comparator `C`) as in
// `absl::btree_multiset<K, C, A>`.
//
template <typename Key, typename Compare = std::less<Key>,
typename Alloc = std::allocator<Key>>
class ABSL_ATTRIBUTE_OWNER btree_multiset
: public container_internal::btree_multiset_container<
container_internal::btree<container_internal::set_params<
Key, Compare, Alloc, /*TargetNodeSize=*/256,
/*IsMulti=*/true>>> {
using Base = typename btree_multiset::btree_multiset_container;
public:
// Constructors and Assignment Operators
//
// A `btree_multiset` supports the same overload set as `std::set`
// for construction and assignment:
//
// * Default constructor
//
// absl::btree_multiset<std::string> set1;
//
// * Initializer List constructor
//
// absl::btree_multiset<std::string> set2 =
// {{"huey"}, {"dewey"}, {"louie"},};
//
// * Copy constructor
//
// absl::btree_multiset<std::string> set3(set2);
//
// * Copy assignment operator
//
// absl::btree_multiset<std::string> set4;
// set4 = set3;
//
// * Move constructor
//
// // Move is guaranteed efficient
// absl::btree_multiset<std::string> set5(std::move(set4));
//
// * Move assignment operator
//
// // May be efficient if allocators are compatible
// absl::btree_multiset<std::string> set6;
// set6 = std::move(set5);
//
// * Range constructor
//
// std::vector<std::string> v = {"a", "b"};
// absl::btree_multiset<std::string> set7(v.begin(), v.end());
btree_multiset() {}
using Base::Base;
// btree_multiset::begin()
//
// Returns an iterator to the beginning of the `btree_multiset`.
using Base::begin;
// btree_multiset::cbegin()
//
// Returns a const iterator to the beginning of the `btree_multiset`.
using Base::cbegin;
// btree_multiset::end()
//
// Returns an iterator to the end of the `btree_multiset`.
using Base::end;
// btree_multiset::cend()
//
// Returns a const iterator to the end of the `btree_multiset`.
using Base::cend;
// btree_multiset::empty()
//
// Returns whether or not the `btree_multiset` is empty.
using Base::empty;
// btree_multiset::max_size()
//
// Returns the largest theoretical possible number of elements within a
// `btree_multiset` under current memory constraints. This value can be
// thought of as the largest value of `std::distance(begin(), end())` for a
// `btree_multiset<Key>`.
using Base::max_size;
// btree_multiset::size()
//
// Returns the number of elements currently within the `btree_multiset`.
using Base::size;
// btree_multiset::clear()
//
// Removes all elements from the `btree_multiset`. Invalidates any references,
// pointers, or iterators referring to contained elements.
using Base::clear;
// btree_multiset::erase()
//
// Erases elements within the `btree_multiset`. Overloads are listed below.
//
// iterator erase(iterator position):
// iterator erase(const_iterator position):
//
// Erases the element at `position` of the `btree_multiset`, returning
// the iterator pointing to the element after the one that was erased
// (or end() if none exists).
//
// iterator erase(const_iterator first, const_iterator last):
//
// Erases the elements in the open interval [`first`, `last`), returning
// the iterator pointing to the element after the interval that was erased
// (or end() if none exists).
//
// template <typename K> size_type erase(const K& key):
//
// Erases the elements matching the key, if any exist, returning the
// number of elements erased.
using Base::erase;
// btree_multiset::insert()
//
// Inserts an element of the specified value into the `btree_multiset`,
// returning an iterator pointing to the newly inserted element.
// Any references, pointers, or iterators are invalidated. Overloads are
// listed below.
//
// iterator insert(const value_type& value):
//
// Inserts a value into the `btree_multiset`, returning an iterator to the
// inserted element.
//
// iterator insert(value_type&& value):
//
// Inserts a moveable value into the `btree_multiset`, returning an iterator
// to the inserted element.
//
// iterator insert(const_iterator hint, const value_type& value):
// iterator insert(const_iterator hint, value_type&& value):
//
// Inserts a value, using the position of `hint` as a non-binding suggestion
// for where to begin the insertion search. Returns an iterator to the
// inserted element.
//
// void insert(InputIterator first, InputIterator last):
//
// Inserts a range of values [`first`, `last`).
//
// void insert(std::initializer_list<init_type> ilist):
//
// Inserts the elements within the initializer list `ilist`.
using Base::insert;
// btree_multiset::emplace()
//
// Inserts an element of the specified value by constructing it in-place
// within the `btree_multiset`. Any references, pointers, or iterators are
// invalidated.
using Base::emplace;
// btree_multiset::emplace_hint()
//
// Inserts an element of the specified value by constructing it in-place
// within the `btree_multiset`, using the position of `hint` as a non-binding
// suggestion for where to begin the insertion search.
//
// Any references, pointers, or iterators are invalidated.
using Base::emplace_hint;
// btree_multiset::extract()
//
// Extracts the indicated element, erasing it in the process, and returns it
// as a C++17-compatible node handle. Overloads are listed below.
//
// node_type extract(const_iterator position):
//
// Extracts the element at the indicated position and returns a node handle
// owning that extracted data.
//
// template <typename K> node_type extract(const K& k):
//
// Extracts the element with the key matching the passed key value and
// returns a node handle owning that extracted data. If the `btree_multiset`
// does not contain an element with a matching key, this function returns an
// empty node handle.
//
// NOTE: In this context, `node_type` refers to the C++17 concept of a
// move-only type that owns and provides access to the elements in associative
// containers (https://en.cppreference.com/w/cpp/container/node_handle).
// It does NOT refer to the data layout of the underlying btree.
using Base::extract;
// btree_multiset::extract_and_get_next()
//
// Extracts the indicated element, erasing it in the process, and returns it
// as a C++17-compatible node handle along with an iterator to the next
// element.
//
// extract_and_get_next_return_type extract_and_get_next(
// const_iterator position):
//
// Extracts the element at the indicated position, returns a struct
// containing a member named `node`: a node handle owning that extracted
// data and a member named `next`: an iterator pointing to the next element
// in the btree.
using Base::extract_and_get_next;
// btree_multiset::merge()
//
// Extracts all elements from a given `source` btree_multiset into this
// `btree_multiset`.
using Base::merge;
// btree_multiset::swap(btree_multiset& other)
//
// Exchanges the contents of this `btree_multiset` with those of the `other`
// btree_multiset, avoiding invocation of any move, copy, or swap operations
// on individual elements.
//
// All iterators and references on the `btree_multiset` remain valid,
// excepting for the past-the-end iterator, which is invalidated.
using Base::swap;
// btree_multiset::contains()
//
// template <typename K> bool contains(const K& key) const:
//
// Determines whether an element comparing equal to the given `key` exists
// within the `btree_multiset`, returning `true` if so or `false` otherwise.
//
// Supports heterogeneous lookup, provided that the set has a compatible
// heterogeneous comparator.
using Base::contains;
// btree_multiset::count()
//
// template <typename K> size_type count(const K& key) const:
//
// Returns the number of elements comparing equal to the given `key` within
// the `btree_multiset`.
//
// Supports heterogeneous lookup, provided that the set has a compatible
// heterogeneous comparator.
using Base::count;
// btree_multiset::equal_range()
//
// Returns a closed range [first, last], defined by a `std::pair` of two
// iterators, containing all elements with the passed key in the
// `btree_multiset`.
using Base::equal_range;
// btree_multiset::find()
//
// template <typename K> iterator find(const K& key):
// template <typename K> const_iterator find(const K& key) const:
//
// Finds an element with the passed `key` within the `btree_multiset`.
//
// Supports heterogeneous lookup, provided that the set has a compatible
// heterogeneous comparator.
using Base::find;
// btree_multiset::lower_bound()
//
// template <typename K> iterator lower_bound(const K& key):
// template <typename K> const_iterator lower_bound(const K& key) const:
//
// Finds the first element that is not less than `key` within the
// `btree_multiset`.
//
// Supports heterogeneous lookup, provided that the set has a compatible
// heterogeneous comparator.
using Base::lower_bound;
// btree_multiset::upper_bound()
//
// template <typename K> iterator upper_bound(const K& key):
// template <typename K> const_iterator upper_bound(const K& key) const:
//
// Finds the first element that is greater than `key` within the
// `btree_multiset`.
//
// Supports heterogeneous lookup, provided that the set has a compatible
// heterogeneous comparator.
using Base::upper_bound;
// btree_multiset::get_allocator()
//
// Returns the allocator function associated with this `btree_multiset`.
using Base::get_allocator;
// btree_multiset::key_comp();
//
// Returns the key comparator associated with this `btree_multiset`.
using Base::key_comp;
// btree_multiset::value_comp();
//
// Returns the value comparator associated with this `btree_multiset`. The
// keys to sort the elements are the values themselves, therefore `value_comp`
// and its sibling member function `key_comp` are equivalent.
using Base::value_comp;
};
// absl::swap(absl::btree_multiset<>, absl::btree_multiset<>)
//
// Swaps the contents of two `absl::btree_multiset` containers.
template <typename K, typename C, typename A>
void swap(btree_multiset<K, C, A> &x, btree_multiset<K, C, A> &y) {
return x.swap(y);
}
// absl::erase_if(absl::btree_multiset<>, Pred)
//
// Erases all elements that satisfy the predicate pred from the container.
// Returns the number of erased elements.
template <typename K, typename C, typename A, typename Pred>
typename btree_multiset<K, C, A>::size_type erase_if(
btree_multiset<K, C, A> & set, Pred pred) {
return container_internal::btree_access::erase_if(set, std::move(pred));
}
namespace container_internal {
// This type implements the necessary functions from the
// absl::container_internal::slot_type interface for btree_(multi)set.
template <typename Key>
struct set_slot_policy {
using slot_type = Key;
using value_type = Key;
using mutable_value_type = Key;
static value_type &element(slot_type *slot) { return *slot; }
static const value_type &element(const slot_type *slot) { return *slot; }
template <typename Alloc, class... Args>
static void construct(Alloc *alloc, slot_type *slot, Args &&...args) {
absl::allocator_traits<Alloc>::construct(*alloc, slot,
std::forward<Args>(args)...);
}
template <typename Alloc>
static void construct(Alloc *alloc, slot_type *slot, slot_type *other) {
absl::allocator_traits<Alloc>::construct(*alloc, slot, std::move(*other));
}
template <typename Alloc>
static void construct(Alloc *alloc, slot_type *slot, const slot_type *other) {
absl::allocator_traits<Alloc>::construct(*alloc, slot, *other);
}
template <typename Alloc>
static void destroy(Alloc *alloc, slot_type *slot) {
absl::allocator_traits<Alloc>::destroy(*alloc, slot);
}
};
// A parameters structure for holding the type parameters for a btree_set.
// Compare and Alloc should be nothrow copy-constructible.
template <typename Key, typename Compare, typename Alloc, int TargetNodeSize,
bool IsMulti>
struct set_params : common_params<Key, Compare, Alloc, TargetNodeSize, IsMulti,
/*IsMap=*/false, set_slot_policy<Key>> {
using value_type = Key;
using slot_type = typename set_params::common_params::slot_type;
template <typename V>
static const V &key(const V &value) {
return value;
}
static const Key &key(const slot_type *slot) { return *slot; }
static const Key &key(slot_type *slot) { return *slot; }
};
} // namespace container_internal
ABSL_NAMESPACE_END
} // namespace absl
#endif // ABSL_CONTAINER_BTREE_SET_H_

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,166 @@
// Copyright 2018 The Abseil Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef ABSL_CONTAINER_BTREE_TEST_H_
#define ABSL_CONTAINER_BTREE_TEST_H_
#include <algorithm>
#include <cassert>
#include <random>
#include <string>
#include <utility>
#include <vector>
#include "absl/container/btree_map.h"
#include "absl/container/btree_set.h"
#include "absl/container/flat_hash_set.h"
#include "absl/strings/cord.h"
#include "absl/time/time.h"
namespace absl {
ABSL_NAMESPACE_BEGIN
namespace container_internal {
// Like remove_const but propagates the removal through std::pair.
template <typename T>
struct remove_pair_const {
using type = typename std::remove_const<T>::type;
};
template <typename T, typename U>
struct remove_pair_const<std::pair<T, U> > {
using type = std::pair<typename remove_pair_const<T>::type,
typename remove_pair_const<U>::type>;
};
// Utility class to provide an accessor for a key given a value. The default
// behavior is to treat the value as a pair and return the first element.
template <typename K, typename V>
struct KeyOfValue {
struct type {
const K& operator()(const V& p) const { return p.first; }
};
};
// Partial specialization of KeyOfValue class for when the key and value are
// the same type such as in set<> and btree_set<>.
template <typename K>
struct KeyOfValue<K, K> {
struct type {
const K& operator()(const K& k) const { return k; }
};
};
inline char* GenerateDigits(char buf[16], unsigned val, unsigned maxval) {
assert(val <= maxval);
constexpr unsigned kBase = 64; // avoid integer division.
unsigned p = 15;
buf[p--] = 0;
while (maxval > 0) {
buf[p--] = ' ' + (val % kBase);
val /= kBase;
maxval /= kBase;
}
return buf + p + 1;
}
template <typename K>
struct Generator {
int maxval;
explicit Generator(int m) : maxval(m) {}
K operator()(int i) const {
assert(i <= maxval);
return K(i);
}
};
template <>
struct Generator<absl::Time> {
int maxval;
explicit Generator(int m) : maxval(m) {}
absl::Time operator()(int i) const { return absl::FromUnixMillis(i); }
};
template <>
struct Generator<std::string> {
int maxval;
explicit Generator(int m) : maxval(m) {}
std::string operator()(int i) const {
char buf[16];
return GenerateDigits(buf, i, maxval);
}
};
template <>
struct Generator<Cord> {
int maxval;
explicit Generator(int m) : maxval(m) {}
Cord operator()(int i) const {
char buf[16];
return Cord(GenerateDigits(buf, i, maxval));
}
};
template <typename T, typename U>
struct Generator<std::pair<T, U> > {
Generator<typename remove_pair_const<T>::type> tgen;
Generator<typename remove_pair_const<U>::type> ugen;
explicit Generator(int m) : tgen(m), ugen(m) {}
std::pair<T, U> operator()(int i) const {
return std::make_pair(tgen(i), ugen(i));
}
};
// Generate n values for our tests and benchmarks. Value range is [0, maxval].
inline std::vector<int> GenerateNumbersWithSeed(int n, int maxval, int seed) {
// NOTE: Some tests rely on generated numbers not changing between test runs.
// We use std::minstd_rand0 because it is well-defined, but don't use
// std::uniform_int_distribution because platforms use different algorithms.
std::minstd_rand0 rng(seed);
std::vector<int> values;
absl::flat_hash_set<int> unique_values;
if (values.size() < n) {
for (int i = values.size(); i < n; i++) {
int value;
do {
value = static_cast<int>(rng()) % (maxval + 1);
} while (!unique_values.insert(value).second);
values.push_back(value);
}
}
return values;
}
// Generates n values in the range [0, maxval].
template <typename V>
std::vector<V> GenerateValuesWithSeed(int n, int maxval, int seed) {
const std::vector<int> nums = GenerateNumbersWithSeed(n, maxval, seed);
Generator<V> gen(maxval);
std::vector<V> vec;
vec.reserve(n);
for (int i = 0; i < n; i++) {
vec.push_back(gen(nums[i]));
}
return vec;
}
} // namespace container_internal
ABSL_NAMESPACE_END
} // namespace absl
#endif // ABSL_CONTAINER_BTREE_TEST_H_

View file

@ -0,0 +1,557 @@
// Copyright 2018 The Abseil Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// -----------------------------------------------------------------------------
// File: fixed_array.h
// -----------------------------------------------------------------------------
//
// A `FixedArray<T>` represents a non-resizable array of `T` where the length of
// the array can be determined at run-time. It is a good replacement for
// non-standard and deprecated uses of `alloca()` and variable length arrays
// within the GCC extension. (See
// https://gcc.gnu.org/onlinedocs/gcc/Variable-Length.html).
//
// `FixedArray` allocates small arrays inline, keeping performance fast by
// avoiding heap operations. It also helps reduce the chances of
// accidentally overflowing your stack if large input is passed to
// your function.
#ifndef ABSL_CONTAINER_FIXED_ARRAY_H_
#define ABSL_CONTAINER_FIXED_ARRAY_H_
#include <algorithm>
#include <cassert>
#include <cstddef>
#include <initializer_list>
#include <iterator>
#include <limits>
#include <memory>
#include <new>
#include <type_traits>
#include "absl/algorithm/algorithm.h"
#include "absl/base/attributes.h"
#include "absl/base/config.h"
#include "absl/base/dynamic_annotations.h"
#include "absl/base/internal/throw_delegate.h"
#include "absl/base/macros.h"
#include "absl/base/optimization.h"
#include "absl/base/port.h"
#include "absl/container/internal/compressed_tuple.h"
#include "absl/memory/memory.h"
namespace absl {
ABSL_NAMESPACE_BEGIN
constexpr static auto kFixedArrayUseDefault = static_cast<size_t>(-1);
// -----------------------------------------------------------------------------
// FixedArray
// -----------------------------------------------------------------------------
//
// A `FixedArray` provides a run-time fixed-size array, allocating a small array
// inline for efficiency.
//
// Most users should not specify the `N` template parameter and let `FixedArray`
// automatically determine the number of elements to store inline based on
// `sizeof(T)`. If `N` is specified, the `FixedArray` implementation will use
// inline storage for arrays with a length <= `N`.
//
// Note that a `FixedArray` constructed with a `size_type` argument will
// default-initialize its values by leaving trivially constructible types
// uninitialized (e.g. int, int[4], double), and others default-constructed.
// This matches the behavior of c-style arrays and `std::array`, but not
// `std::vector`.
template <typename T, size_t N = kFixedArrayUseDefault,
typename A = std::allocator<T>>
class ABSL_ATTRIBUTE_WARN_UNUSED FixedArray {
static_assert(!std::is_array<T>::value || std::extent<T>::value > 0,
"Arrays with unknown bounds cannot be used with FixedArray.");
static constexpr size_t kInlineBytesDefault = 256;
using AllocatorTraits = std::allocator_traits<A>;
// std::iterator_traits isn't guaranteed to be SFINAE-friendly until C++17,
// but this seems to be mostly pedantic.
template <typename Iterator>
using EnableIfForwardIterator = absl::enable_if_t<std::is_convertible<
typename std::iterator_traits<Iterator>::iterator_category,
std::forward_iterator_tag>::value>;
static constexpr bool NoexceptCopyable() {
return std::is_nothrow_copy_constructible<StorageElement>::value &&
absl::allocator_is_nothrow<allocator_type>::value;
}
static constexpr bool NoexceptMovable() {
return std::is_nothrow_move_constructible<StorageElement>::value &&
absl::allocator_is_nothrow<allocator_type>::value;
}
static constexpr bool DefaultConstructorIsNonTrivial() {
return !absl::is_trivially_default_constructible<StorageElement>::value;
}
public:
using allocator_type = typename AllocatorTraits::allocator_type;
using value_type = typename AllocatorTraits::value_type;
using pointer = typename AllocatorTraits::pointer;
using const_pointer = typename AllocatorTraits::const_pointer;
using reference = value_type&;
using const_reference = const value_type&;
using size_type = typename AllocatorTraits::size_type;
using difference_type = typename AllocatorTraits::difference_type;
using iterator = pointer;
using const_iterator = const_pointer;
using reverse_iterator = std::reverse_iterator<iterator>;
using const_reverse_iterator = std::reverse_iterator<const_iterator>;
static constexpr size_type inline_elements =
(N == kFixedArrayUseDefault ? kInlineBytesDefault / sizeof(value_type)
: static_cast<size_type>(N));
FixedArray(const FixedArray& other) noexcept(NoexceptCopyable())
: FixedArray(other,
AllocatorTraits::select_on_container_copy_construction(
other.storage_.alloc())) {}
FixedArray(const FixedArray& other,
const allocator_type& a) noexcept(NoexceptCopyable())
: FixedArray(other.begin(), other.end(), a) {}
FixedArray(FixedArray&& other) noexcept(NoexceptMovable())
: FixedArray(std::move(other), other.storage_.alloc()) {}
FixedArray(FixedArray&& other,
const allocator_type& a) noexcept(NoexceptMovable())
: FixedArray(std::make_move_iterator(other.begin()),
std::make_move_iterator(other.end()), a) {}
// Creates an array object that can store `n` elements.
// Note that trivially constructible elements will be uninitialized.
explicit FixedArray(size_type n, const allocator_type& a = allocator_type())
: storage_(n, a) {
if (DefaultConstructorIsNonTrivial()) {
memory_internal::ConstructRange(storage_.alloc(), storage_.begin(),
storage_.end());
}
}
// Creates an array initialized with `n` copies of `val`.
FixedArray(size_type n, const value_type& val,
const allocator_type& a = allocator_type())
: storage_(n, a) {
memory_internal::ConstructRange(storage_.alloc(), storage_.begin(),
storage_.end(), val);
}
// Creates an array initialized with the size and contents of `init_list`.
FixedArray(std::initializer_list<value_type> init_list,
const allocator_type& a = allocator_type())
: FixedArray(init_list.begin(), init_list.end(), a) {}
// Creates an array initialized with the elements from the input
// range. The array's size will always be `std::distance(first, last)`.
// REQUIRES: Iterator must be a forward_iterator or better.
template <typename Iterator, EnableIfForwardIterator<Iterator>* = nullptr>
FixedArray(Iterator first, Iterator last,
const allocator_type& a = allocator_type())
: storage_(std::distance(first, last), a) {
memory_internal::CopyRange(storage_.alloc(), storage_.begin(), first, last);
}
~FixedArray() noexcept {
for (auto* cur = storage_.begin(); cur != storage_.end(); ++cur) {
AllocatorTraits::destroy(storage_.alloc(), cur);
}
}
// Assignments are deleted because they break the invariant that the size of a
// `FixedArray` never changes.
void operator=(FixedArray&&) = delete;
void operator=(const FixedArray&) = delete;
// FixedArray::size()
//
// Returns the length of the fixed array.
size_type size() const { return storage_.size(); }
// FixedArray::max_size()
//
// Returns the largest possible value of `std::distance(begin(), end())` for a
// `FixedArray<T>`. This is equivalent to the most possible addressable bytes
// over the number of bytes taken by T.
constexpr size_type max_size() const {
return (std::numeric_limits<difference_type>::max)() / sizeof(value_type);
}
// FixedArray::empty()
//
// Returns whether or not the fixed array is empty.
bool empty() const { return size() == 0; }
// FixedArray::memsize()
//
// Returns the memory size of the fixed array in bytes.
size_t memsize() const { return size() * sizeof(value_type); }
// FixedArray::data()
//
// Returns a const T* pointer to elements of the `FixedArray`. This pointer
// can be used to access (but not modify) the contained elements.
const_pointer data() const ABSL_ATTRIBUTE_LIFETIME_BOUND {
return AsValueType(storage_.begin());
}
// Overload of FixedArray::data() to return a T* pointer to elements of the
// fixed array. This pointer can be used to access and modify the contained
// elements.
pointer data() ABSL_ATTRIBUTE_LIFETIME_BOUND {
return AsValueType(storage_.begin());
}
// FixedArray::operator[]
//
// Returns a reference the ith element of the fixed array.
// REQUIRES: 0 <= i < size()
reference operator[](size_type i) ABSL_ATTRIBUTE_LIFETIME_BOUND {
ABSL_HARDENING_ASSERT(i < size());
return data()[i];
}
// Overload of FixedArray::operator()[] to return a const reference to the
// ith element of the fixed array.
// REQUIRES: 0 <= i < size()
const_reference operator[](size_type i) const ABSL_ATTRIBUTE_LIFETIME_BOUND {
ABSL_HARDENING_ASSERT(i < size());
return data()[i];
}
// FixedArray::at
//
// Bounds-checked access. Returns a reference to the ith element of the fixed
// array, or throws std::out_of_range
reference at(size_type i) ABSL_ATTRIBUTE_LIFETIME_BOUND {
if (ABSL_PREDICT_FALSE(i >= size())) {
base_internal::ThrowStdOutOfRange("FixedArray::at failed bounds check");
}
return data()[i];
}
// Overload of FixedArray::at() to return a const reference to the ith element
// of the fixed array.
const_reference at(size_type i) const ABSL_ATTRIBUTE_LIFETIME_BOUND {
if (ABSL_PREDICT_FALSE(i >= size())) {
base_internal::ThrowStdOutOfRange("FixedArray::at failed bounds check");
}
return data()[i];
}
// FixedArray::front()
//
// Returns a reference to the first element of the fixed array.
reference front() ABSL_ATTRIBUTE_LIFETIME_BOUND {
ABSL_HARDENING_ASSERT(!empty());
return data()[0];
}
// Overload of FixedArray::front() to return a reference to the first element
// of a fixed array of const values.
const_reference front() const ABSL_ATTRIBUTE_LIFETIME_BOUND {
ABSL_HARDENING_ASSERT(!empty());
return data()[0];
}
// FixedArray::back()
//
// Returns a reference to the last element of the fixed array.
reference back() ABSL_ATTRIBUTE_LIFETIME_BOUND {
ABSL_HARDENING_ASSERT(!empty());
return data()[size() - 1];
}
// Overload of FixedArray::back() to return a reference to the last element
// of a fixed array of const values.
const_reference back() const ABSL_ATTRIBUTE_LIFETIME_BOUND {
ABSL_HARDENING_ASSERT(!empty());
return data()[size() - 1];
}
// FixedArray::begin()
//
// Returns an iterator to the beginning of the fixed array.
iterator begin() ABSL_ATTRIBUTE_LIFETIME_BOUND { return data(); }
// Overload of FixedArray::begin() to return a const iterator to the
// beginning of the fixed array.
const_iterator begin() const ABSL_ATTRIBUTE_LIFETIME_BOUND { return data(); }
// FixedArray::cbegin()
//
// Returns a const iterator to the beginning of the fixed array.
const_iterator cbegin() const ABSL_ATTRIBUTE_LIFETIME_BOUND {
return begin();
}
// FixedArray::end()
//
// Returns an iterator to the end of the fixed array.
iterator end() ABSL_ATTRIBUTE_LIFETIME_BOUND { return data() + size(); }
// Overload of FixedArray::end() to return a const iterator to the end of the
// fixed array.
const_iterator end() const ABSL_ATTRIBUTE_LIFETIME_BOUND {
return data() + size();
}
// FixedArray::cend()
//
// Returns a const iterator to the end of the fixed array.
const_iterator cend() const ABSL_ATTRIBUTE_LIFETIME_BOUND { return end(); }
// FixedArray::rbegin()
//
// Returns a reverse iterator from the end of the fixed array.
reverse_iterator rbegin() ABSL_ATTRIBUTE_LIFETIME_BOUND {
return reverse_iterator(end());
}
// Overload of FixedArray::rbegin() to return a const reverse iterator from
// the end of the fixed array.
const_reverse_iterator rbegin() const ABSL_ATTRIBUTE_LIFETIME_BOUND {
return const_reverse_iterator(end());
}
// FixedArray::crbegin()
//
// Returns a const reverse iterator from the end of the fixed array.
const_reverse_iterator crbegin() const ABSL_ATTRIBUTE_LIFETIME_BOUND {
return rbegin();
}
// FixedArray::rend()
//
// Returns a reverse iterator from the beginning of the fixed array.
reverse_iterator rend() ABSL_ATTRIBUTE_LIFETIME_BOUND {
return reverse_iterator(begin());
}
// Overload of FixedArray::rend() for returning a const reverse iterator
// from the beginning of the fixed array.
const_reverse_iterator rend() const ABSL_ATTRIBUTE_LIFETIME_BOUND {
return const_reverse_iterator(begin());
}
// FixedArray::crend()
//
// Returns a reverse iterator from the beginning of the fixed array.
const_reverse_iterator crend() const ABSL_ATTRIBUTE_LIFETIME_BOUND {
return rend();
}
// FixedArray::fill()
//
// Assigns the given `value` to all elements in the fixed array.
void fill(const value_type& val) { std::fill(begin(), end(), val); }
// Relational operators. Equality operators are elementwise using
// `operator==`, while order operators order FixedArrays lexicographically.
friend bool operator==(const FixedArray& lhs, const FixedArray& rhs) {
return std::equal(lhs.begin(), lhs.end(), rhs.begin(), rhs.end());
}
friend bool operator!=(const FixedArray& lhs, const FixedArray& rhs) {
return !(lhs == rhs);
}
friend bool operator<(const FixedArray& lhs, const FixedArray& rhs) {
return std::lexicographical_compare(lhs.begin(), lhs.end(), rhs.begin(),
rhs.end());
}
friend bool operator>(const FixedArray& lhs, const FixedArray& rhs) {
return rhs < lhs;
}
friend bool operator<=(const FixedArray& lhs, const FixedArray& rhs) {
return !(rhs < lhs);
}
friend bool operator>=(const FixedArray& lhs, const FixedArray& rhs) {
return !(lhs < rhs);
}
template <typename H>
friend H AbslHashValue(H h, const FixedArray& v) {
return H::combine(H::combine_contiguous(std::move(h), v.data(), v.size()),
v.size());
}
private:
// StorageElement
//
// For FixedArrays with a C-style-array value_type, StorageElement is a POD
// wrapper struct called StorageElementWrapper that holds the value_type
// instance inside. This is needed for construction and destruction of the
// entire array regardless of how many dimensions it has. For all other cases,
// StorageElement is just an alias of value_type.
//
// Maintainer's Note: The simpler solution would be to simply wrap value_type
// in a struct whether it's an array or not. That causes some paranoid
// diagnostics to misfire, believing that 'data()' returns a pointer to a
// single element, rather than the packed array that it really is.
// e.g.:
//
// FixedArray<char> buf(1);
// sprintf(buf.data(), "foo");
//
// error: call to int __builtin___sprintf_chk(etc...)
// will always overflow destination buffer [-Werror]
//
template <typename OuterT, typename InnerT = absl::remove_extent_t<OuterT>,
size_t InnerN = std::extent<OuterT>::value>
struct StorageElementWrapper {
InnerT array[InnerN];
};
using StorageElement =
absl::conditional_t<std::is_array<value_type>::value,
StorageElementWrapper<value_type>, value_type>;
static pointer AsValueType(pointer ptr) { return ptr; }
static pointer AsValueType(StorageElementWrapper<value_type>* ptr) {
return std::addressof(ptr->array);
}
static_assert(sizeof(StorageElement) == sizeof(value_type), "");
static_assert(alignof(StorageElement) == alignof(value_type), "");
class NonEmptyInlinedStorage {
public:
StorageElement* data() { return reinterpret_cast<StorageElement*>(buff_); }
void AnnotateConstruct(size_type n);
void AnnotateDestruct(size_type n);
#ifdef ABSL_HAVE_ADDRESS_SANITIZER
void* RedzoneBegin() { return &redzone_begin_; }
void* RedzoneEnd() { return &redzone_end_ + 1; }
#endif // ABSL_HAVE_ADDRESS_SANITIZER
private:
ABSL_ADDRESS_SANITIZER_REDZONE(redzone_begin_);
alignas(StorageElement) char buff_[sizeof(StorageElement[inline_elements])];
ABSL_ADDRESS_SANITIZER_REDZONE(redzone_end_);
};
class EmptyInlinedStorage {
public:
StorageElement* data() { return nullptr; }
void AnnotateConstruct(size_type) {}
void AnnotateDestruct(size_type) {}
};
using InlinedStorage =
absl::conditional_t<inline_elements == 0, EmptyInlinedStorage,
NonEmptyInlinedStorage>;
// Storage
//
// An instance of Storage manages the inline and out-of-line memory for
// instances of FixedArray. This guarantees that even when construction of
// individual elements fails in the FixedArray constructor body, the
// destructor for Storage will still be called and out-of-line memory will be
// properly deallocated.
//
class Storage : public InlinedStorage {
public:
Storage(size_type n, const allocator_type& a)
: size_alloc_(n, a), data_(InitializeData()) {}
~Storage() noexcept {
if (UsingInlinedStorage(size())) {
InlinedStorage::AnnotateDestruct(size());
} else {
AllocatorTraits::deallocate(alloc(), AsValueType(begin()), size());
}
}
size_type size() const { return size_alloc_.template get<0>(); }
StorageElement* begin() const { return data_; }
StorageElement* end() const { return begin() + size(); }
allocator_type& alloc() { return size_alloc_.template get<1>(); }
const allocator_type& alloc() const {
return size_alloc_.template get<1>();
}
private:
static bool UsingInlinedStorage(size_type n) {
return n <= inline_elements;
}
#ifdef ABSL_HAVE_ADDRESS_SANITIZER
ABSL_ATTRIBUTE_NOINLINE
#endif // ABSL_HAVE_ADDRESS_SANITIZER
StorageElement* InitializeData() {
if (UsingInlinedStorage(size())) {
InlinedStorage::AnnotateConstruct(size());
return InlinedStorage::data();
} else {
return reinterpret_cast<StorageElement*>(
AllocatorTraits::allocate(alloc(), size()));
}
}
// `CompressedTuple` takes advantage of EBCO for stateless `allocator_type`s
container_internal::CompressedTuple<size_type, allocator_type> size_alloc_;
StorageElement* data_;
};
Storage storage_;
};
#ifdef ABSL_INTERNAL_NEED_REDUNDANT_CONSTEXPR_DECL
template <typename T, size_t N, typename A>
constexpr size_t FixedArray<T, N, A>::kInlineBytesDefault;
template <typename T, size_t N, typename A>
constexpr typename FixedArray<T, N, A>::size_type
FixedArray<T, N, A>::inline_elements;
#endif
template <typename T, size_t N, typename A>
void FixedArray<T, N, A>::NonEmptyInlinedStorage::AnnotateConstruct(
typename FixedArray<T, N, A>::size_type n) {
#ifdef ABSL_HAVE_ADDRESS_SANITIZER
if (!n) return;
ABSL_ANNOTATE_CONTIGUOUS_CONTAINER(data(), RedzoneEnd(), RedzoneEnd(),
data() + n);
ABSL_ANNOTATE_CONTIGUOUS_CONTAINER(RedzoneBegin(), data(), data(),
RedzoneBegin());
#endif // ABSL_HAVE_ADDRESS_SANITIZER
static_cast<void>(n); // Mark used when not in asan mode
}
template <typename T, size_t N, typename A>
void FixedArray<T, N, A>::NonEmptyInlinedStorage::AnnotateDestruct(
typename FixedArray<T, N, A>::size_type n) {
#ifdef ABSL_HAVE_ADDRESS_SANITIZER
if (!n) return;
ABSL_ANNOTATE_CONTIGUOUS_CONTAINER(data(), RedzoneEnd(), data() + n,
RedzoneEnd());
ABSL_ANNOTATE_CONTIGUOUS_CONTAINER(RedzoneBegin(), data(), RedzoneBegin(),
data());
#endif // ABSL_HAVE_ADDRESS_SANITIZER
static_cast<void>(n); // Mark used when not in asan mode
}
ABSL_NAMESPACE_END
} // namespace absl
#endif // ABSL_CONTAINER_FIXED_ARRAY_H_

View file

@ -0,0 +1,67 @@
// Copyright 2019 The Abseil Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include <stddef.h>
#include <string>
#include "absl/container/fixed_array.h"
#include "benchmark/benchmark.h"
namespace {
// For benchmarking -- simple class with constructor and destructor that
// set an int to a constant..
class SimpleClass {
public:
SimpleClass() : i(3) {}
~SimpleClass() { i = 0; }
private:
int i;
};
template <typename C, size_t stack_size>
void BM_FixedArray(benchmark::State& state) {
const int size = state.range(0);
for (auto _ : state) {
absl::FixedArray<C, stack_size> fa(size);
benchmark::DoNotOptimize(fa.data());
}
}
BENCHMARK_TEMPLATE(BM_FixedArray, char, absl::kFixedArrayUseDefault)
->Range(0, 1 << 16);
BENCHMARK_TEMPLATE(BM_FixedArray, char, 0)->Range(0, 1 << 16);
BENCHMARK_TEMPLATE(BM_FixedArray, char, 1)->Range(0, 1 << 16);
BENCHMARK_TEMPLATE(BM_FixedArray, char, 16)->Range(0, 1 << 16);
BENCHMARK_TEMPLATE(BM_FixedArray, char, 256)->Range(0, 1 << 16);
BENCHMARK_TEMPLATE(BM_FixedArray, char, 65536)->Range(0, 1 << 16);
BENCHMARK_TEMPLATE(BM_FixedArray, SimpleClass, absl::kFixedArrayUseDefault)
->Range(0, 1 << 16);
BENCHMARK_TEMPLATE(BM_FixedArray, SimpleClass, 0)->Range(0, 1 << 16);
BENCHMARK_TEMPLATE(BM_FixedArray, SimpleClass, 1)->Range(0, 1 << 16);
BENCHMARK_TEMPLATE(BM_FixedArray, SimpleClass, 16)->Range(0, 1 << 16);
BENCHMARK_TEMPLATE(BM_FixedArray, SimpleClass, 256)->Range(0, 1 << 16);
BENCHMARK_TEMPLATE(BM_FixedArray, SimpleClass, 65536)->Range(0, 1 << 16);
BENCHMARK_TEMPLATE(BM_FixedArray, std::string, absl::kFixedArrayUseDefault)
->Range(0, 1 << 16);
BENCHMARK_TEMPLATE(BM_FixedArray, std::string, 0)->Range(0, 1 << 16);
BENCHMARK_TEMPLATE(BM_FixedArray, std::string, 1)->Range(0, 1 << 16);
BENCHMARK_TEMPLATE(BM_FixedArray, std::string, 16)->Range(0, 1 << 16);
BENCHMARK_TEMPLATE(BM_FixedArray, std::string, 256)->Range(0, 1 << 16);
BENCHMARK_TEMPLATE(BM_FixedArray, std::string, 65536)->Range(0, 1 << 16);
} // namespace

View file

@ -0,0 +1,201 @@
// Copyright 2019 The Abseil Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "absl/base/config.h"
#include "absl/container/fixed_array.h"
#ifdef ABSL_HAVE_EXCEPTIONS
#include <initializer_list>
#include "gtest/gtest.h"
#include "absl/base/internal/exception_safety_testing.h"
namespace absl {
ABSL_NAMESPACE_BEGIN
namespace {
constexpr size_t kInlined = 25;
constexpr size_t kSmallSize = kInlined / 2;
constexpr size_t kLargeSize = kInlined * 2;
constexpr int kInitialValue = 5;
constexpr int kUpdatedValue = 10;
using ::testing::TestThrowingCtor;
using Thrower = testing::ThrowingValue<testing::TypeSpec::kEverythingThrows>;
using ThrowAlloc =
testing::ThrowingAllocator<Thrower, testing::AllocSpec::kEverythingThrows>;
using MoveThrower = testing::ThrowingValue<testing::TypeSpec::kNoThrowMove>;
using MoveThrowAlloc =
testing::ThrowingAllocator<MoveThrower,
testing::AllocSpec::kEverythingThrows>;
using FixedArr = absl::FixedArray<Thrower, kInlined>;
using FixedArrWithAlloc = absl::FixedArray<Thrower, kInlined, ThrowAlloc>;
using MoveFixedArr = absl::FixedArray<MoveThrower, kInlined>;
using MoveFixedArrWithAlloc =
absl::FixedArray<MoveThrower, kInlined, MoveThrowAlloc>;
TEST(FixedArrayExceptionSafety, CopyConstructor) {
auto small = FixedArr(kSmallSize);
TestThrowingCtor<FixedArr>(small);
auto large = FixedArr(kLargeSize);
TestThrowingCtor<FixedArr>(large);
}
TEST(FixedArrayExceptionSafety, CopyConstructorWithAlloc) {
auto small = FixedArrWithAlloc(kSmallSize);
TestThrowingCtor<FixedArrWithAlloc>(small);
auto large = FixedArrWithAlloc(kLargeSize);
TestThrowingCtor<FixedArrWithAlloc>(large);
}
TEST(FixedArrayExceptionSafety, MoveConstructor) {
TestThrowingCtor<FixedArr>(FixedArr(kSmallSize));
TestThrowingCtor<FixedArr>(FixedArr(kLargeSize));
// TypeSpec::kNoThrowMove
TestThrowingCtor<MoveFixedArr>(MoveFixedArr(kSmallSize));
TestThrowingCtor<MoveFixedArr>(MoveFixedArr(kLargeSize));
}
TEST(FixedArrayExceptionSafety, MoveConstructorWithAlloc) {
TestThrowingCtor<FixedArrWithAlloc>(FixedArrWithAlloc(kSmallSize));
TestThrowingCtor<FixedArrWithAlloc>(FixedArrWithAlloc(kLargeSize));
// TypeSpec::kNoThrowMove
TestThrowingCtor<MoveFixedArrWithAlloc>(MoveFixedArrWithAlloc(kSmallSize));
TestThrowingCtor<MoveFixedArrWithAlloc>(MoveFixedArrWithAlloc(kLargeSize));
}
TEST(FixedArrayExceptionSafety, SizeConstructor) {
TestThrowingCtor<FixedArr>(kSmallSize);
TestThrowingCtor<FixedArr>(kLargeSize);
}
TEST(FixedArrayExceptionSafety, SizeConstructorWithAlloc) {
TestThrowingCtor<FixedArrWithAlloc>(kSmallSize);
TestThrowingCtor<FixedArrWithAlloc>(kLargeSize);
}
TEST(FixedArrayExceptionSafety, SizeValueConstructor) {
TestThrowingCtor<FixedArr>(kSmallSize, Thrower());
TestThrowingCtor<FixedArr>(kLargeSize, Thrower());
}
TEST(FixedArrayExceptionSafety, SizeValueConstructorWithAlloc) {
TestThrowingCtor<FixedArrWithAlloc>(kSmallSize, Thrower());
TestThrowingCtor<FixedArrWithAlloc>(kLargeSize, Thrower());
}
TEST(FixedArrayExceptionSafety, IteratorConstructor) {
auto small = FixedArr(kSmallSize);
TestThrowingCtor<FixedArr>(small.begin(), small.end());
auto large = FixedArr(kLargeSize);
TestThrowingCtor<FixedArr>(large.begin(), large.end());
}
TEST(FixedArrayExceptionSafety, IteratorConstructorWithAlloc) {
auto small = FixedArrWithAlloc(kSmallSize);
TestThrowingCtor<FixedArrWithAlloc>(small.begin(), small.end());
auto large = FixedArrWithAlloc(kLargeSize);
TestThrowingCtor<FixedArrWithAlloc>(large.begin(), large.end());
}
TEST(FixedArrayExceptionSafety, InitListConstructor) {
constexpr int small_inlined = 3;
using SmallFixedArr = absl::FixedArray<Thrower, small_inlined>;
TestThrowingCtor<SmallFixedArr>(std::initializer_list<Thrower>{});
// Test inlined allocation
TestThrowingCtor<SmallFixedArr>(
std::initializer_list<Thrower>{Thrower{}, Thrower{}});
// Test out of line allocation
TestThrowingCtor<SmallFixedArr>(std::initializer_list<Thrower>{
Thrower{}, Thrower{}, Thrower{}, Thrower{}, Thrower{}});
}
TEST(FixedArrayExceptionSafety, InitListConstructorWithAlloc) {
constexpr int small_inlined = 3;
using SmallFixedArrWithAlloc =
absl::FixedArray<Thrower, small_inlined, ThrowAlloc>;
TestThrowingCtor<SmallFixedArrWithAlloc>(std::initializer_list<Thrower>{});
// Test inlined allocation
TestThrowingCtor<SmallFixedArrWithAlloc>(
std::initializer_list<Thrower>{Thrower{}, Thrower{}});
// Test out of line allocation
TestThrowingCtor<SmallFixedArrWithAlloc>(std::initializer_list<Thrower>{
Thrower{}, Thrower{}, Thrower{}, Thrower{}, Thrower{}});
}
template <typename FixedArrT>
testing::AssertionResult ReadMemory(FixedArrT* fixed_arr) {
int sum = 0;
for (const auto& thrower : *fixed_arr) {
sum += thrower.Get();
}
return testing::AssertionSuccess() << "Values sum to [" << sum << "]";
}
TEST(FixedArrayExceptionSafety, Fill) {
auto test_fill = testing::MakeExceptionSafetyTester()
.WithContracts(ReadMemory<FixedArr>)
.WithOperation([&](FixedArr* fixed_arr_ptr) {
auto thrower =
Thrower(kUpdatedValue, testing::nothrow_ctor);
fixed_arr_ptr->fill(thrower);
});
EXPECT_TRUE(
test_fill.WithInitialValue(FixedArr(kSmallSize, Thrower(kInitialValue)))
.Test());
EXPECT_TRUE(
test_fill.WithInitialValue(FixedArr(kLargeSize, Thrower(kInitialValue)))
.Test());
}
TEST(FixedArrayExceptionSafety, FillWithAlloc) {
auto test_fill = testing::MakeExceptionSafetyTester()
.WithContracts(ReadMemory<FixedArrWithAlloc>)
.WithOperation([&](FixedArrWithAlloc* fixed_arr_ptr) {
auto thrower =
Thrower(kUpdatedValue, testing::nothrow_ctor);
fixed_arr_ptr->fill(thrower);
});
EXPECT_TRUE(test_fill
.WithInitialValue(
FixedArrWithAlloc(kSmallSize, Thrower(kInitialValue)))
.Test());
EXPECT_TRUE(test_fill
.WithInitialValue(
FixedArrWithAlloc(kLargeSize, Thrower(kInitialValue)))
.Test());
}
} // namespace
ABSL_NAMESPACE_END
} // namespace absl
#endif // ABSL_HAVE_EXCEPTIONS

View file

@ -0,0 +1,857 @@
// Copyright 2019 The Abseil Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "absl/container/fixed_array.h"
#include <stdio.h>
#include <cstring>
#include <list>
#include <memory>
#include <numeric>
#include <scoped_allocator>
#include <stdexcept>
#include <string>
#include <vector>
#include "gmock/gmock.h"
#include "gtest/gtest.h"
#include "absl/base/config.h"
#include "absl/base/internal/exception_testing.h"
#include "absl/base/options.h"
#include "absl/container/internal/test_allocator.h"
#include "absl/hash/hash_testing.h"
#include "absl/memory/memory.h"
using ::testing::ElementsAreArray;
namespace {
// Helper routine to determine if a absl::FixedArray used stack allocation.
template <typename ArrayType>
static bool IsOnStack(const ArrayType& a) {
return a.size() <= ArrayType::inline_elements;
}
class ConstructionTester {
public:
ConstructionTester() : self_ptr_(this), value_(0) { constructions++; }
~ConstructionTester() {
assert(self_ptr_ == this);
self_ptr_ = nullptr;
destructions++;
}
// These are incremented as elements are constructed and destructed so we can
// be sure all elements are properly cleaned up.
static int constructions;
static int destructions;
void CheckConstructed() { assert(self_ptr_ == this); }
void set(int value) { value_ = value; }
int get() { return value_; }
private:
// self_ptr_ should always point to 'this' -- that's how we can be sure the
// constructor has been called.
ConstructionTester* self_ptr_;
int value_;
};
int ConstructionTester::constructions = 0;
int ConstructionTester::destructions = 0;
// ThreeInts will initialize its three ints to the value stored in
// ThreeInts::counter. The constructor increments counter so that each object
// in an array of ThreeInts will have different values.
class ThreeInts {
public:
ThreeInts() {
x_ = counter;
y_ = counter;
z_ = counter;
++counter;
}
static int counter;
int x_, y_, z_;
};
int ThreeInts::counter = 0;
TEST(FixedArrayTest, CopyCtor) {
absl::FixedArray<int, 10> on_stack(5);
std::iota(on_stack.begin(), on_stack.end(), 0);
absl::FixedArray<int, 10> stack_copy = on_stack;
EXPECT_THAT(stack_copy, ElementsAreArray(on_stack));
EXPECT_TRUE(IsOnStack(stack_copy));
absl::FixedArray<int, 10> allocated(15);
std::iota(allocated.begin(), allocated.end(), 0);
absl::FixedArray<int, 10> alloced_copy = allocated;
EXPECT_THAT(alloced_copy, ElementsAreArray(allocated));
EXPECT_FALSE(IsOnStack(alloced_copy));
}
TEST(FixedArrayTest, MoveCtor) {
absl::FixedArray<std::unique_ptr<int>, 10> on_stack(5);
for (int i = 0; i < 5; ++i) {
on_stack[i] = absl::make_unique<int>(i);
}
absl::FixedArray<std::unique_ptr<int>, 10> stack_copy = std::move(on_stack);
for (int i = 0; i < 5; ++i) EXPECT_EQ(*(stack_copy[i]), i);
EXPECT_EQ(stack_copy.size(), on_stack.size());
absl::FixedArray<std::unique_ptr<int>, 10> allocated(15);
for (int i = 0; i < 15; ++i) {
allocated[i] = absl::make_unique<int>(i);
}
absl::FixedArray<std::unique_ptr<int>, 10> alloced_copy =
std::move(allocated);
for (int i = 0; i < 15; ++i) EXPECT_EQ(*(alloced_copy[i]), i);
EXPECT_EQ(allocated.size(), alloced_copy.size());
}
TEST(FixedArrayTest, SmallObjects) {
// Small object arrays
{
// Short arrays should be on the stack
absl::FixedArray<int> array(4);
EXPECT_TRUE(IsOnStack(array));
}
{
// Large arrays should be on the heap
absl::FixedArray<int> array(1048576);
EXPECT_FALSE(IsOnStack(array));
}
{
// Arrays of <= default size should be on the stack
absl::FixedArray<int, 100> array(100);
EXPECT_TRUE(IsOnStack(array));
}
{
// Arrays of > default size should be on the heap
absl::FixedArray<int, 100> array(101);
EXPECT_FALSE(IsOnStack(array));
}
{
// Arrays with different size elements should use approximately
// same amount of stack space
absl::FixedArray<int> array1(0);
absl::FixedArray<char> array2(0);
EXPECT_LE(sizeof(array1), sizeof(array2) + 100);
EXPECT_LE(sizeof(array2), sizeof(array1) + 100);
}
{
// Ensure that vectors are properly constructed inside a fixed array.
absl::FixedArray<std::vector<int>> array(2);
EXPECT_EQ(0, array[0].size());
EXPECT_EQ(0, array[1].size());
}
{
// Regardless of absl::FixedArray implementation, check that a type with a
// low alignment requirement and a non power-of-two size is initialized
// correctly.
ThreeInts::counter = 1;
absl::FixedArray<ThreeInts> array(2);
EXPECT_EQ(1, array[0].x_);
EXPECT_EQ(1, array[0].y_);
EXPECT_EQ(1, array[0].z_);
EXPECT_EQ(2, array[1].x_);
EXPECT_EQ(2, array[1].y_);
EXPECT_EQ(2, array[1].z_);
}
}
TEST(FixedArrayTest, AtThrows) {
absl::FixedArray<int> a = {1, 2, 3};
EXPECT_EQ(a.at(2), 3);
ABSL_BASE_INTERNAL_EXPECT_FAIL(a.at(3), std::out_of_range,
"failed bounds check");
}
TEST(FixedArrayTest, Hardened) {
#if !defined(NDEBUG) || ABSL_OPTION_HARDENED
absl::FixedArray<int> a = {1, 2, 3};
EXPECT_EQ(a[2], 3);
EXPECT_DEATH_IF_SUPPORTED(a[3], "");
EXPECT_DEATH_IF_SUPPORTED(a[-1], "");
absl::FixedArray<int> empty(0);
EXPECT_DEATH_IF_SUPPORTED(empty[0], "");
EXPECT_DEATH_IF_SUPPORTED(empty[-1], "");
EXPECT_DEATH_IF_SUPPORTED(empty.front(), "");
EXPECT_DEATH_IF_SUPPORTED(empty.back(), "");
#endif
}
TEST(FixedArrayRelationalsTest, EqualArrays) {
for (int i = 0; i < 10; ++i) {
absl::FixedArray<int, 5> a1(i);
std::iota(a1.begin(), a1.end(), 0);
absl::FixedArray<int, 5> a2(a1.begin(), a1.end());
EXPECT_TRUE(a1 == a2);
EXPECT_FALSE(a1 != a2);
EXPECT_TRUE(a2 == a1);
EXPECT_FALSE(a2 != a1);
EXPECT_FALSE(a1 < a2);
EXPECT_FALSE(a1 > a2);
EXPECT_FALSE(a2 < a1);
EXPECT_FALSE(a2 > a1);
EXPECT_TRUE(a1 <= a2);
EXPECT_TRUE(a1 >= a2);
EXPECT_TRUE(a2 <= a1);
EXPECT_TRUE(a2 >= a1);
}
}
TEST(FixedArrayRelationalsTest, UnequalArrays) {
for (int i = 1; i < 10; ++i) {
absl::FixedArray<int, 5> a1(i);
std::iota(a1.begin(), a1.end(), 0);
absl::FixedArray<int, 5> a2(a1.begin(), a1.end());
--a2[i / 2];
EXPECT_FALSE(a1 == a2);
EXPECT_TRUE(a1 != a2);
EXPECT_FALSE(a2 == a1);
EXPECT_TRUE(a2 != a1);
EXPECT_FALSE(a1 < a2);
EXPECT_TRUE(a1 > a2);
EXPECT_TRUE(a2 < a1);
EXPECT_FALSE(a2 > a1);
EXPECT_FALSE(a1 <= a2);
EXPECT_TRUE(a1 >= a2);
EXPECT_TRUE(a2 <= a1);
EXPECT_FALSE(a2 >= a1);
}
}
template <int stack_elements>
static void TestArray(int n) {
SCOPED_TRACE(n);
SCOPED_TRACE(stack_elements);
ConstructionTester::constructions = 0;
ConstructionTester::destructions = 0;
{
absl::FixedArray<ConstructionTester, stack_elements> array(n);
EXPECT_THAT(array.size(), n);
EXPECT_THAT(array.memsize(), sizeof(ConstructionTester) * n);
EXPECT_THAT(array.begin() + n, array.end());
// Check that all elements were constructed
for (int i = 0; i < n; i++) {
array[i].CheckConstructed();
}
// Check that no other elements were constructed
EXPECT_THAT(ConstructionTester::constructions, n);
// Test operator[]
for (int i = 0; i < n; i++) {
array[i].set(i);
}
for (int i = 0; i < n; i++) {
EXPECT_THAT(array[i].get(), i);
EXPECT_THAT(array.data()[i].get(), i);
}
// Test data()
for (int i = 0; i < n; i++) {
array.data()[i].set(i + 1);
}
for (int i = 0; i < n; i++) {
EXPECT_THAT(array[i].get(), i + 1);
EXPECT_THAT(array.data()[i].get(), i + 1);
}
} // Close scope containing 'array'.
// Check that all constructed elements were destructed.
EXPECT_EQ(ConstructionTester::constructions,
ConstructionTester::destructions);
}
template <int elements_per_inner_array, int inline_elements>
static void TestArrayOfArrays(int n) {
SCOPED_TRACE(n);
SCOPED_TRACE(inline_elements);
SCOPED_TRACE(elements_per_inner_array);
ConstructionTester::constructions = 0;
ConstructionTester::destructions = 0;
{
using InnerArray = ConstructionTester[elements_per_inner_array];
// Heap-allocate the FixedArray to avoid blowing the stack frame.
auto array_ptr =
absl::make_unique<absl::FixedArray<InnerArray, inline_elements>>(n);
auto& array = *array_ptr;
ASSERT_EQ(array.size(), n);
ASSERT_EQ(array.memsize(),
sizeof(ConstructionTester) * elements_per_inner_array * n);
ASSERT_EQ(array.begin() + n, array.end());
// Check that all elements were constructed
for (int i = 0; i < n; i++) {
for (int j = 0; j < elements_per_inner_array; j++) {
(array[i])[j].CheckConstructed();
}
}
// Check that no other elements were constructed
ASSERT_EQ(ConstructionTester::constructions, n * elements_per_inner_array);
// Test operator[]
for (int i = 0; i < n; i++) {
for (int j = 0; j < elements_per_inner_array; j++) {
(array[i])[j].set(i * elements_per_inner_array + j);
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < elements_per_inner_array; j++) {
ASSERT_EQ((array[i])[j].get(), i * elements_per_inner_array + j);
ASSERT_EQ((array.data()[i])[j].get(), i * elements_per_inner_array + j);
}
}
// Test data()
for (int i = 0; i < n; i++) {
for (int j = 0; j < elements_per_inner_array; j++) {
(array.data()[i])[j].set((i + 1) * elements_per_inner_array + j);
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < elements_per_inner_array; j++) {
ASSERT_EQ((array[i])[j].get(), (i + 1) * elements_per_inner_array + j);
ASSERT_EQ((array.data()[i])[j].get(),
(i + 1) * elements_per_inner_array + j);
}
}
} // Close scope containing 'array'.
// Check that all constructed elements were destructed.
EXPECT_EQ(ConstructionTester::constructions,
ConstructionTester::destructions);
}
TEST(IteratorConstructorTest, NonInline) {
int const kInput[] = {2, 3, 5, 7, 11, 13, 17};
absl::FixedArray<int, ABSL_ARRAYSIZE(kInput) - 1> const fixed(
kInput, kInput + ABSL_ARRAYSIZE(kInput));
ASSERT_EQ(ABSL_ARRAYSIZE(kInput), fixed.size());
for (size_t i = 0; i < ABSL_ARRAYSIZE(kInput); ++i) {
ASSERT_EQ(kInput[i], fixed[i]);
}
}
TEST(IteratorConstructorTest, Inline) {
int const kInput[] = {2, 3, 5, 7, 11, 13, 17};
absl::FixedArray<int, ABSL_ARRAYSIZE(kInput)> const fixed(
kInput, kInput + ABSL_ARRAYSIZE(kInput));
ASSERT_EQ(ABSL_ARRAYSIZE(kInput), fixed.size());
for (size_t i = 0; i < ABSL_ARRAYSIZE(kInput); ++i) {
ASSERT_EQ(kInput[i], fixed[i]);
}
}
TEST(IteratorConstructorTest, NonPod) {
char const* kInput[] = {"red", "orange", "yellow", "green",
"blue", "indigo", "violet"};
absl::FixedArray<std::string> const fixed(kInput,
kInput + ABSL_ARRAYSIZE(kInput));
ASSERT_EQ(ABSL_ARRAYSIZE(kInput), fixed.size());
for (size_t i = 0; i < ABSL_ARRAYSIZE(kInput); ++i) {
ASSERT_EQ(kInput[i], fixed[i]);
}
}
TEST(IteratorConstructorTest, FromEmptyVector) {
std::vector<int> const empty;
absl::FixedArray<int> const fixed(empty.begin(), empty.end());
EXPECT_EQ(0, fixed.size());
EXPECT_EQ(empty.size(), fixed.size());
}
TEST(IteratorConstructorTest, FromNonEmptyVector) {
int const kInput[] = {2, 3, 5, 7, 11, 13, 17};
std::vector<int> const items(kInput, kInput + ABSL_ARRAYSIZE(kInput));
absl::FixedArray<int> const fixed(items.begin(), items.end());
ASSERT_EQ(items.size(), fixed.size());
for (size_t i = 0; i < items.size(); ++i) {
ASSERT_EQ(items[i], fixed[i]);
}
}
TEST(IteratorConstructorTest, FromBidirectionalIteratorRange) {
int const kInput[] = {2, 3, 5, 7, 11, 13, 17};
std::list<int> const items(kInput, kInput + ABSL_ARRAYSIZE(kInput));
absl::FixedArray<int> const fixed(items.begin(), items.end());
EXPECT_THAT(fixed, testing::ElementsAreArray(kInput));
}
TEST(InitListConstructorTest, InitListConstruction) {
absl::FixedArray<int> fixed = {1, 2, 3};
EXPECT_THAT(fixed, testing::ElementsAreArray({1, 2, 3}));
}
TEST(FillConstructorTest, NonEmptyArrays) {
absl::FixedArray<int> stack_array(4, 1);
EXPECT_THAT(stack_array, testing::ElementsAreArray({1, 1, 1, 1}));
absl::FixedArray<int, 0> heap_array(4, 1);
EXPECT_THAT(heap_array, testing::ElementsAreArray({1, 1, 1, 1}));
}
TEST(FillConstructorTest, EmptyArray) {
absl::FixedArray<int> empty_fill(0, 1);
absl::FixedArray<int> empty_size(0);
EXPECT_EQ(empty_fill, empty_size);
}
TEST(FillConstructorTest, NotTriviallyCopyable) {
std::string str = "abcd";
absl::FixedArray<std::string> strings = {str, str, str, str};
absl::FixedArray<std::string> array(4, str);
EXPECT_EQ(array, strings);
}
TEST(FillConstructorTest, Disambiguation) {
absl::FixedArray<size_t> a(1, 2);
EXPECT_THAT(a, testing::ElementsAre(2));
}
TEST(FixedArrayTest, ManySizedArrays) {
std::vector<int> sizes;
for (int i = 1; i < 100; i++) sizes.push_back(i);
for (int i = 100; i <= 1000; i += 100) sizes.push_back(i);
for (int n : sizes) {
TestArray<0>(n);
TestArray<1>(n);
TestArray<64>(n);
TestArray<1000>(n);
}
}
TEST(FixedArrayTest, ManySizedArraysOfArraysOf1) {
for (int n = 1; n < 1000; n++) {
ASSERT_NO_FATAL_FAILURE((TestArrayOfArrays<1, 0>(n)));
ASSERT_NO_FATAL_FAILURE((TestArrayOfArrays<1, 1>(n)));
ASSERT_NO_FATAL_FAILURE((TestArrayOfArrays<1, 64>(n)));
ASSERT_NO_FATAL_FAILURE((TestArrayOfArrays<1, 1000>(n)));
}
}
TEST(FixedArrayTest, ManySizedArraysOfArraysOf2) {
for (int n = 1; n < 1000; n++) {
TestArrayOfArrays<2, 0>(n);
TestArrayOfArrays<2, 1>(n);
TestArrayOfArrays<2, 64>(n);
TestArrayOfArrays<2, 1000>(n);
}
}
// If value_type is put inside of a struct container,
// we might evoke this error in a hardened build unless data() is carefully
// written, so check on that.
// error: call to int __builtin___sprintf_chk(etc...)
// will always overflow destination buffer [-Werror]
TEST(FixedArrayTest, AvoidParanoidDiagnostics) {
absl::FixedArray<char, 32> buf(32);
sprintf(buf.data(), "foo"); // NOLINT(runtime/printf)
}
TEST(FixedArrayTest, TooBigInlinedSpace) {
struct TooBig {
char c[1 << 20];
}; // too big for even one on the stack
// Simulate the data members of absl::FixedArray, a pointer and a size_t.
struct Data {
TooBig* p;
size_t size;
};
// Make sure TooBig objects are not inlined for 0 or default size.
static_assert(sizeof(absl::FixedArray<TooBig, 0>) == sizeof(Data),
"0-sized absl::FixedArray should have same size as Data.");
static_assert(alignof(absl::FixedArray<TooBig, 0>) == alignof(Data),
"0-sized absl::FixedArray should have same alignment as Data.");
static_assert(sizeof(absl::FixedArray<TooBig>) == sizeof(Data),
"default-sized absl::FixedArray should have same size as Data");
static_assert(
alignof(absl::FixedArray<TooBig>) == alignof(Data),
"default-sized absl::FixedArray should have same alignment as Data.");
}
// PickyDelete EXPECTs its class-scope deallocation funcs are unused.
struct PickyDelete {
PickyDelete() {}
~PickyDelete() {}
void operator delete(void* p) {
EXPECT_TRUE(false) << __FUNCTION__;
::operator delete(p);
}
void operator delete[](void* p) {
EXPECT_TRUE(false) << __FUNCTION__;
::operator delete[](p);
}
};
TEST(FixedArrayTest, UsesGlobalAlloc) {
absl::FixedArray<PickyDelete, 0> a(5);
EXPECT_EQ(a.size(), 5);
}
TEST(FixedArrayTest, Data) {
static const int kInput[] = {2, 3, 5, 7, 11, 13, 17};
absl::FixedArray<int> fa(std::begin(kInput), std::end(kInput));
EXPECT_EQ(fa.data(), &*fa.begin());
EXPECT_EQ(fa.data(), &fa[0]);
const absl::FixedArray<int>& cfa = fa;
EXPECT_EQ(cfa.data(), &*cfa.begin());
EXPECT_EQ(cfa.data(), &cfa[0]);
}
TEST(FixedArrayTest, Empty) {
absl::FixedArray<int> empty(0);
absl::FixedArray<int> inline_filled(1);
absl::FixedArray<int, 0> heap_filled(1);
EXPECT_TRUE(empty.empty());
EXPECT_FALSE(inline_filled.empty());
EXPECT_FALSE(heap_filled.empty());
}
TEST(FixedArrayTest, FrontAndBack) {
absl::FixedArray<int, 3 * sizeof(int)> inlined = {1, 2, 3};
EXPECT_EQ(inlined.front(), 1);
EXPECT_EQ(inlined.back(), 3);
absl::FixedArray<int, 0> allocated = {1, 2, 3};
EXPECT_EQ(allocated.front(), 1);
EXPECT_EQ(allocated.back(), 3);
absl::FixedArray<int> one_element = {1};
EXPECT_EQ(one_element.front(), one_element.back());
}
TEST(FixedArrayTest, ReverseIteratorInlined) {
absl::FixedArray<int, 5 * sizeof(int)> a = {0, 1, 2, 3, 4};
int counter = 5;
for (absl::FixedArray<int>::reverse_iterator iter = a.rbegin();
iter != a.rend(); ++iter) {
counter--;
EXPECT_EQ(counter, *iter);
}
EXPECT_EQ(counter, 0);
counter = 5;
for (absl::FixedArray<int>::const_reverse_iterator iter = a.rbegin();
iter != a.rend(); ++iter) {
counter--;
EXPECT_EQ(counter, *iter);
}
EXPECT_EQ(counter, 0);
counter = 5;
for (auto iter = a.crbegin(); iter != a.crend(); ++iter) {
counter--;
EXPECT_EQ(counter, *iter);
}
EXPECT_EQ(counter, 0);
}
TEST(FixedArrayTest, ReverseIteratorAllocated) {
absl::FixedArray<int, 0> a = {0, 1, 2, 3, 4};
int counter = 5;
for (absl::FixedArray<int>::reverse_iterator iter = a.rbegin();
iter != a.rend(); ++iter) {
counter--;
EXPECT_EQ(counter, *iter);
}
EXPECT_EQ(counter, 0);
counter = 5;
for (absl::FixedArray<int>::const_reverse_iterator iter = a.rbegin();
iter != a.rend(); ++iter) {
counter--;
EXPECT_EQ(counter, *iter);
}
EXPECT_EQ(counter, 0);
counter = 5;
for (auto iter = a.crbegin(); iter != a.crend(); ++iter) {
counter--;
EXPECT_EQ(counter, *iter);
}
EXPECT_EQ(counter, 0);
}
TEST(FixedArrayTest, Fill) {
absl::FixedArray<int, 5 * sizeof(int)> inlined(5);
int fill_val = 42;
inlined.fill(fill_val);
for (int i : inlined) EXPECT_EQ(i, fill_val);
absl::FixedArray<int, 0> allocated(5);
allocated.fill(fill_val);
for (int i : allocated) EXPECT_EQ(i, fill_val);
// It doesn't do anything, just make sure this compiles.
absl::FixedArray<int> empty(0);
empty.fill(fill_val);
}
#ifndef __GNUC__
TEST(FixedArrayTest, DefaultCtorDoesNotValueInit) {
using T = char;
constexpr auto capacity = 10;
using FixedArrType = absl::FixedArray<T, capacity>;
constexpr auto scrubbed_bits = 0x95;
constexpr auto length = capacity / 2;
alignas(FixedArrType) unsigned char buff[sizeof(FixedArrType)];
std::memset(std::addressof(buff), scrubbed_bits, sizeof(FixedArrType));
FixedArrType* arr =
::new (static_cast<void*>(std::addressof(buff))) FixedArrType(length);
EXPECT_THAT(*arr, testing::Each(scrubbed_bits));
arr->~FixedArrType();
}
#endif // __GNUC__
TEST(AllocatorSupportTest, CountInlineAllocations) {
constexpr size_t inlined_size = 4;
using Alloc = absl::container_internal::CountingAllocator<int>;
using AllocFxdArr = absl::FixedArray<int, inlined_size, Alloc>;
int64_t allocated = 0;
int64_t active_instances = 0;
{
const int ia[] = {0, 1, 2, 3, 4, 5, 6, 7};
Alloc alloc(&allocated, &active_instances);
AllocFxdArr arr(ia, ia + inlined_size, alloc);
static_cast<void>(arr);
}
EXPECT_EQ(allocated, 0);
EXPECT_EQ(active_instances, 0);
}
TEST(AllocatorSupportTest, CountOutoflineAllocations) {
constexpr size_t inlined_size = 4;
using Alloc = absl::container_internal::CountingAllocator<int>;
using AllocFxdArr = absl::FixedArray<int, inlined_size, Alloc>;
int64_t allocated = 0;
int64_t active_instances = 0;
{
const int ia[] = {0, 1, 2, 3, 4, 5, 6, 7};
Alloc alloc(&allocated, &active_instances);
AllocFxdArr arr(ia, ia + ABSL_ARRAYSIZE(ia), alloc);
EXPECT_EQ(allocated, arr.size() * sizeof(int));
static_cast<void>(arr);
}
EXPECT_EQ(active_instances, 0);
}
TEST(AllocatorSupportTest, CountCopyInlineAllocations) {
constexpr size_t inlined_size = 4;
using Alloc = absl::container_internal::CountingAllocator<int>;
using AllocFxdArr = absl::FixedArray<int, inlined_size, Alloc>;
int64_t allocated1 = 0;
int64_t allocated2 = 0;
int64_t active_instances = 0;
Alloc alloc(&allocated1, &active_instances);
Alloc alloc2(&allocated2, &active_instances);
{
int initial_value = 1;
AllocFxdArr arr1(inlined_size / 2, initial_value, alloc);
EXPECT_EQ(allocated1, 0);
AllocFxdArr arr2(arr1, alloc2);
EXPECT_EQ(allocated2, 0);
static_cast<void>(arr1);
static_cast<void>(arr2);
}
EXPECT_EQ(active_instances, 0);
}
TEST(AllocatorSupportTest, CountCopyOutoflineAllocations) {
constexpr size_t inlined_size = 4;
using Alloc = absl::container_internal::CountingAllocator<int>;
using AllocFxdArr = absl::FixedArray<int, inlined_size, Alloc>;
int64_t allocated1 = 0;
int64_t allocated2 = 0;
int64_t active_instances = 0;
Alloc alloc(&allocated1, &active_instances);
Alloc alloc2(&allocated2, &active_instances);
{
int initial_value = 1;
AllocFxdArr arr1(inlined_size * 2, initial_value, alloc);
EXPECT_EQ(allocated1, arr1.size() * sizeof(int));
AllocFxdArr arr2(arr1, alloc2);
EXPECT_EQ(allocated2, inlined_size * 2 * sizeof(int));
static_cast<void>(arr1);
static_cast<void>(arr2);
}
EXPECT_EQ(active_instances, 0);
}
TEST(AllocatorSupportTest, SizeValAllocConstructor) {
using testing::AllOf;
using testing::Each;
using testing::SizeIs;
constexpr size_t inlined_size = 4;
using Alloc = absl::container_internal::CountingAllocator<int>;
using AllocFxdArr = absl::FixedArray<int, inlined_size, Alloc>;
{
auto len = inlined_size / 2;
auto val = 0;
int64_t allocated = 0;
AllocFxdArr arr(len, val, Alloc(&allocated));
EXPECT_EQ(allocated, 0);
EXPECT_THAT(arr, AllOf(SizeIs(len), Each(0)));
}
{
auto len = inlined_size * 2;
auto val = 0;
int64_t allocated = 0;
AllocFxdArr arr(len, val, Alloc(&allocated));
EXPECT_EQ(allocated, len * sizeof(int));
EXPECT_THAT(arr, AllOf(SizeIs(len), Each(0)));
}
}
TEST(AllocatorSupportTest, PropagatesStatefulAllocator) {
constexpr size_t inlined_size = 4;
using Alloc = absl::container_internal::CountingAllocator<int>;
using AllocFxdArr = absl::FixedArray<int, inlined_size, Alloc>;
auto len = inlined_size * 2;
auto val = 0;
int64_t allocated = 0;
AllocFxdArr arr(len, val, Alloc(&allocated));
EXPECT_EQ(allocated, len * sizeof(int));
AllocFxdArr copy = arr;
EXPECT_EQ(allocated, len * sizeof(int) * 2);
EXPECT_EQ(copy, arr);
}
#ifdef ABSL_HAVE_ADDRESS_SANITIZER
TEST(FixedArrayTest, AddressSanitizerAnnotations1) {
absl::FixedArray<int, 32> a(10);
int* raw = a.data();
raw[0] = 0;
raw[9] = 0;
EXPECT_DEATH_IF_SUPPORTED(raw[-2] = 0, "container-overflow");
EXPECT_DEATH_IF_SUPPORTED(raw[-1] = 0, "container-overflow");
EXPECT_DEATH_IF_SUPPORTED(raw[10] = 0, "container-overflow");
EXPECT_DEATH_IF_SUPPORTED(raw[31] = 0, "container-overflow");
}
TEST(FixedArrayTest, AddressSanitizerAnnotations2) {
absl::FixedArray<char, 17> a(12);
char* raw = a.data();
raw[0] = 0;
raw[11] = 0;
EXPECT_DEATH_IF_SUPPORTED(raw[-7] = 0, "container-overflow");
EXPECT_DEATH_IF_SUPPORTED(raw[-1] = 0, "container-overflow");
EXPECT_DEATH_IF_SUPPORTED(raw[12] = 0, "container-overflow");
EXPECT_DEATH_IF_SUPPORTED(raw[17] = 0, "container-overflow");
}
TEST(FixedArrayTest, AddressSanitizerAnnotations3) {
absl::FixedArray<uint64_t, 20> a(20);
uint64_t* raw = a.data();
raw[0] = 0;
raw[19] = 0;
EXPECT_DEATH_IF_SUPPORTED(raw[-1] = 0, "container-overflow");
EXPECT_DEATH_IF_SUPPORTED(raw[20] = 0, "container-overflow");
}
TEST(FixedArrayTest, AddressSanitizerAnnotations4) {
absl::FixedArray<ThreeInts> a(10);
ThreeInts* raw = a.data();
raw[0] = ThreeInts();
raw[9] = ThreeInts();
// Note: raw[-1] is pointing to 12 bytes before the container range. However,
// there is only a 8-byte red zone before the container range, so we only
// access the last 4 bytes of the struct to make sure it stays within the red
// zone.
EXPECT_DEATH_IF_SUPPORTED(raw[-1].z_ = 0, "container-overflow");
EXPECT_DEATH_IF_SUPPORTED(raw[10] = ThreeInts(), "container-overflow");
// The actual size of storage is kDefaultBytes=256, 21*12 = 252,
// so reading raw[21] should still trigger the correct warning.
EXPECT_DEATH_IF_SUPPORTED(raw[21] = ThreeInts(), "container-overflow");
}
#endif // ABSL_HAVE_ADDRESS_SANITIZER
TEST(FixedArrayTest, AbslHashValueWorks) {
using V = absl::FixedArray<int>;
std::vector<V> cases;
// Generate a variety of vectors some of these are small enough for the inline
// space but are stored out of line.
for (int i = 0; i < 10; ++i) {
V v(i);
for (int j = 0; j < i; ++j) {
v[j] = j;
}
cases.push_back(v);
}
EXPECT_TRUE(absl::VerifyTypeImplementsAbslHashCorrectly(cases));
}
} // namespace

View file

@ -0,0 +1,687 @@
// Copyright 2018 The Abseil Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// -----------------------------------------------------------------------------
// File: flat_hash_map.h
// -----------------------------------------------------------------------------
//
// An `absl::flat_hash_map<K, V>` is an unordered associative container of
// unique keys and associated values designed to be a more efficient replacement
// for `std::unordered_map`. Like `unordered_map`, search, insertion, and
// deletion of map elements can be done as an `O(1)` operation. However,
// `flat_hash_map` (and other unordered associative containers known as the
// collection of Abseil "Swiss tables") contain other optimizations that result
// in both memory and computation advantages.
//
// In most cases, your default choice for a hash map should be a map of type
// `flat_hash_map`.
//
// `flat_hash_map` is not exception-safe.
#ifndef ABSL_CONTAINER_FLAT_HASH_MAP_H_
#define ABSL_CONTAINER_FLAT_HASH_MAP_H_
#include <cstddef>
#include <memory>
#include <type_traits>
#include <utility>
#include "absl/algorithm/container.h"
#include "absl/base/attributes.h"
#include "absl/base/macros.h"
#include "absl/container/hash_container_defaults.h"
#include "absl/container/internal/container_memory.h"
#include "absl/container/internal/raw_hash_map.h" // IWYU pragma: export
#include "absl/meta/type_traits.h"
namespace absl {
ABSL_NAMESPACE_BEGIN
namespace container_internal {
template <class K, class V>
struct FlatHashMapPolicy;
} // namespace container_internal
// -----------------------------------------------------------------------------
// absl::flat_hash_map
// -----------------------------------------------------------------------------
//
// An `absl::flat_hash_map<K, V>` is an unordered associative container which
// has been optimized for both speed and memory footprint in most common use
// cases. Its interface is similar to that of `std::unordered_map<K, V>` with
// the following notable differences:
//
// * Requires keys that are CopyConstructible
// * Requires values that are MoveConstructible
// * Supports heterogeneous lookup, through `find()`, `operator[]()` and
// `insert()`, provided that the map is provided a compatible heterogeneous
// hashing function and equality operator. See below for details.
// * Invalidates any references and pointers to elements within the table after
// `rehash()` and when the table is moved.
// * Contains a `capacity()` member function indicating the number of element
// slots (open, deleted, and empty) within the hash map.
// * Returns `void` from the `erase(iterator)` overload.
//
// By default, `flat_hash_map` uses the `absl::Hash` hashing framework.
// All fundamental and Abseil types that support the `absl::Hash` framework have
// a compatible equality operator for comparing insertions into `flat_hash_map`.
// If your type is not yet supported by the `absl::Hash` framework, see
// absl/hash/hash.h for information on extending Abseil hashing to user-defined
// types.
//
// Using `absl::flat_hash_map` at interface boundaries in dynamically loaded
// libraries (e.g. .dll, .so) is unsupported due to way `absl::Hash` values may
// be randomized across dynamically loaded libraries.
//
// To achieve heterogeneous lookup for custom types either `Hash` and `Eq` type
// parameters can be used or `T` should have public inner types
// `absl_container_hash` and (optionally) `absl_container_eq`. In either case,
// `typename Hash::is_transparent` and `typename Eq::is_transparent` should be
// well-formed. Both types are basically functors:
// * `Hash` should support `size_t operator()(U val) const` that returns a hash
// for the given `val`.
// * `Eq` should support `bool operator()(U lhs, V rhs) const` that returns true
// if `lhs` is equal to `rhs`.
//
// In most cases `T` needs only to provide the `absl_container_hash`. In this
// case `std::equal_to<void>` will be used instead of `eq` part.
//
// NOTE: A `flat_hash_map` stores its value types directly inside its
// implementation array to avoid memory indirection. Because a `flat_hash_map`
// is designed to move data when rehashed, map values will not retain pointer
// stability. If you require pointer stability, or if your values are large,
// consider using `absl::flat_hash_map<Key, std::unique_ptr<Value>>` instead.
// If your types are not moveable or you require pointer stability for keys,
// consider `absl::node_hash_map`.
//
// Example:
//
// // Create a flat hash map of three strings (that map to strings)
// absl::flat_hash_map<std::string, std::string> ducks =
// {{"a", "huey"}, {"b", "dewey"}, {"c", "louie"}};
//
// // Insert a new element into the flat hash map
// ducks.insert({"d", "donald"});
//
// // Force a rehash of the flat hash map
// ducks.rehash(0);
//
// // Find the element with the key "b"
// std::string search_key = "b";
// auto result = ducks.find(search_key);
// if (result != ducks.end()) {
// std::cout << "Result: " << result->second << std::endl;
// }
template <class K, class V, class Hash = DefaultHashContainerHash<K>,
class Eq = DefaultHashContainerEq<K>,
class Allocator = std::allocator<std::pair<const K, V>>>
class ABSL_ATTRIBUTE_OWNER flat_hash_map
: public absl::container_internal::raw_hash_map<
absl::container_internal::FlatHashMapPolicy<K, V>, Hash, Eq,
Allocator> {
using Base = typename flat_hash_map::raw_hash_map;
public:
// Constructors and Assignment Operators
//
// A flat_hash_map supports the same overload set as `std::unordered_map`
// for construction and assignment:
//
// * Default constructor
//
// // No allocation for the table's elements is made.
// absl::flat_hash_map<int, std::string> map1;
//
// * Initializer List constructor
//
// absl::flat_hash_map<int, std::string> map2 =
// {{1, "huey"}, {2, "dewey"}, {3, "louie"},};
//
// * Copy constructor
//
// absl::flat_hash_map<int, std::string> map3(map2);
//
// * Copy assignment operator
//
// // Hash functor and Comparator are copied as well
// absl::flat_hash_map<int, std::string> map4;
// map4 = map3;
//
// * Move constructor
//
// // Move is guaranteed efficient
// absl::flat_hash_map<int, std::string> map5(std::move(map4));
//
// * Move assignment operator
//
// // May be efficient if allocators are compatible
// absl::flat_hash_map<int, std::string> map6;
// map6 = std::move(map5);
//
// * Range constructor
//
// std::vector<std::pair<int, std::string>> v = {{1, "a"}, {2, "b"}};
// absl::flat_hash_map<int, std::string> map7(v.begin(), v.end());
flat_hash_map() {}
using Base::Base;
// flat_hash_map::begin()
//
// Returns an iterator to the beginning of the `flat_hash_map`.
using Base::begin;
// flat_hash_map::cbegin()
//
// Returns a const iterator to the beginning of the `flat_hash_map`.
using Base::cbegin;
// flat_hash_map::cend()
//
// Returns a const iterator to the end of the `flat_hash_map`.
using Base::cend;
// flat_hash_map::end()
//
// Returns an iterator to the end of the `flat_hash_map`.
using Base::end;
// flat_hash_map::capacity()
//
// Returns the number of element slots (assigned, deleted, and empty)
// available within the `flat_hash_map`.
//
// NOTE: this member function is particular to `absl::flat_hash_map` and is
// not provided in the `std::unordered_map` API.
using Base::capacity;
// flat_hash_map::empty()
//
// Returns whether or not the `flat_hash_map` is empty.
using Base::empty;
// flat_hash_map::max_size()
//
// Returns the largest theoretical possible number of elements within a
// `flat_hash_map` under current memory constraints. This value can be thought
// of the largest value of `std::distance(begin(), end())` for a
// `flat_hash_map<K, V>`.
using Base::max_size;
// flat_hash_map::size()
//
// Returns the number of elements currently within the `flat_hash_map`.
using Base::size;
// flat_hash_map::clear()
//
// Removes all elements from the `flat_hash_map`. Invalidates any references,
// pointers, or iterators referring to contained elements.
//
// NOTE: this operation may shrink the underlying buffer. To avoid shrinking
// the underlying buffer call `erase(begin(), end())`.
using Base::clear;
// flat_hash_map::erase()
//
// Erases elements within the `flat_hash_map`. Erasing does not trigger a
// rehash. Overloads are listed below.
//
// void erase(const_iterator pos):
//
// Erases the element at `position` of the `flat_hash_map`, returning
// `void`.
//
// NOTE: returning `void` in this case is different than that of STL
// containers in general and `std::unordered_map` in particular (which
// return an iterator to the element following the erased element). If that
// iterator is needed, simply post increment the iterator:
//
// map.erase(it++);
//
// iterator erase(const_iterator first, const_iterator last):
//
// Erases the elements in the open interval [`first`, `last`), returning an
// iterator pointing to `last`. The special case of calling
// `erase(begin(), end())` resets the reserved growth such that if
// `reserve(N)` has previously been called and there has been no intervening
// call to `clear()`, then after calling `erase(begin(), end())`, it is safe
// to assume that inserting N elements will not cause a rehash.
//
// size_type erase(const key_type& key):
//
// Erases the element with the matching key, if it exists, returning the
// number of elements erased (0 or 1).
using Base::erase;
// flat_hash_map::insert()
//
// Inserts an element of the specified value into the `flat_hash_map`,
// returning an iterator pointing to the newly inserted element, provided that
// an element with the given key does not already exist. If rehashing occurs
// due to the insertion, all iterators are invalidated. Overloads are listed
// below.
//
// std::pair<iterator,bool> insert(const init_type& value):
//
// Inserts a value into the `flat_hash_map`. Returns a pair consisting of an
// iterator to the inserted element (or to the element that prevented the
// insertion) and a bool denoting whether the insertion took place.
//
// std::pair<iterator,bool> insert(T&& value):
// std::pair<iterator,bool> insert(init_type&& value):
//
// Inserts a moveable value into the `flat_hash_map`. Returns a pair
// consisting of an iterator to the inserted element (or to the element that
// prevented the insertion) and a bool denoting whether the insertion took
// place.
//
// iterator insert(const_iterator hint, const init_type& value):
// iterator insert(const_iterator hint, T&& value):
// iterator insert(const_iterator hint, init_type&& value);
//
// Inserts a value, using the position of `hint` as a non-binding suggestion
// for where to begin the insertion search. Returns an iterator to the
// inserted element, or to the existing element that prevented the
// insertion.
//
// void insert(InputIterator first, InputIterator last):
//
// Inserts a range of values [`first`, `last`).
//
// NOTE: Although the STL does not specify which element may be inserted if
// multiple keys compare equivalently, for `flat_hash_map` we guarantee the
// first match is inserted.
//
// void insert(std::initializer_list<init_type> ilist):
//
// Inserts the elements within the initializer list `ilist`.
//
// NOTE: Although the STL does not specify which element may be inserted if
// multiple keys compare equivalently within the initializer list, for
// `flat_hash_map` we guarantee the first match is inserted.
using Base::insert;
// flat_hash_map::insert_or_assign()
//
// Inserts an element of the specified value into the `flat_hash_map` provided
// that a value with the given key does not already exist, or replaces it with
// the element value if a key for that value already exists, returning an
// iterator pointing to the newly inserted element. If rehashing occurs due
// to the insertion, all existing iterators are invalidated. Overloads are
// listed below.
//
// pair<iterator, bool> insert_or_assign(const init_type& k, T&& obj):
// pair<iterator, bool> insert_or_assign(init_type&& k, T&& obj):
//
// Inserts/Assigns (or moves) the element of the specified key into the
// `flat_hash_map`.
//
// iterator insert_or_assign(const_iterator hint,
// const init_type& k, T&& obj):
// iterator insert_or_assign(const_iterator hint, init_type&& k, T&& obj):
//
// Inserts/Assigns (or moves) the element of the specified key into the
// `flat_hash_map` using the position of `hint` as a non-binding suggestion
// for where to begin the insertion search.
using Base::insert_or_assign;
// flat_hash_map::emplace()
//
// Inserts an element of the specified value by constructing it in-place
// within the `flat_hash_map`, provided that no element with the given key
// already exists.
//
// The element may be constructed even if there already is an element with the
// key in the container, in which case the newly constructed element will be
// destroyed immediately. Prefer `try_emplace()` unless your key is not
// copyable or moveable.
//
// If rehashing occurs due to the insertion, all iterators are invalidated.
using Base::emplace;
// flat_hash_map::emplace_hint()
//
// Inserts an element of the specified value by constructing it in-place
// within the `flat_hash_map`, using the position of `hint` as a non-binding
// suggestion for where to begin the insertion search, and only inserts
// provided that no element with the given key already exists.
//
// The element may be constructed even if there already is an element with the
// key in the container, in which case the newly constructed element will be
// destroyed immediately. Prefer `try_emplace()` unless your key is not
// copyable or moveable.
//
// If rehashing occurs due to the insertion, all iterators are invalidated.
using Base::emplace_hint;
// flat_hash_map::try_emplace()
//
// Inserts an element of the specified value by constructing it in-place
// within the `flat_hash_map`, provided that no element with the given key
// already exists. Unlike `emplace()`, if an element with the given key
// already exists, we guarantee that no element is constructed.
//
// If rehashing occurs due to the insertion, all iterators are invalidated.
// Overloads are listed below.
//
// pair<iterator, bool> try_emplace(const key_type& k, Args&&... args):
// pair<iterator, bool> try_emplace(key_type&& k, Args&&... args):
//
// Inserts (via copy or move) the element of the specified key into the
// `flat_hash_map`.
//
// iterator try_emplace(const_iterator hint,
// const key_type& k, Args&&... args):
// iterator try_emplace(const_iterator hint, key_type&& k, Args&&... args):
//
// Inserts (via copy or move) the element of the specified key into the
// `flat_hash_map` using the position of `hint` as a non-binding suggestion
// for where to begin the insertion search.
//
// All `try_emplace()` overloads make the same guarantees regarding rvalue
// arguments as `std::unordered_map::try_emplace()`, namely that these
// functions will not move from rvalue arguments if insertions do not happen.
using Base::try_emplace;
// flat_hash_map::extract()
//
// Extracts the indicated element, erasing it in the process, and returns it
// as a C++17-compatible node handle. Overloads are listed below.
//
// node_type extract(const_iterator position):
//
// Extracts the key,value pair of the element at the indicated position and
// returns a node handle owning that extracted data.
//
// node_type extract(const key_type& x):
//
// Extracts the key,value pair of the element with a key matching the passed
// key value and returns a node handle owning that extracted data. If the
// `flat_hash_map` does not contain an element with a matching key, this
// function returns an empty node handle.
//
// NOTE: when compiled in an earlier version of C++ than C++17,
// `node_type::key()` returns a const reference to the key instead of a
// mutable reference. We cannot safely return a mutable reference without
// std::launder (which is not available before C++17).
using Base::extract;
// flat_hash_map::merge()
//
// Extracts elements from a given `source` flat hash map into this
// `flat_hash_map`. If the destination `flat_hash_map` already contains an
// element with an equivalent key, that element is not extracted.
using Base::merge;
// flat_hash_map::swap(flat_hash_map& other)
//
// Exchanges the contents of this `flat_hash_map` with those of the `other`
// flat hash map.
//
// All iterators and references on the `flat_hash_map` remain valid, excepting
// for the past-the-end iterator, which is invalidated.
//
// `swap()` requires that the flat hash map's hashing and key equivalence
// functions be Swappable, and are exchanged using unqualified calls to
// non-member `swap()`. If the map's allocator has
// `std::allocator_traits<allocator_type>::propagate_on_container_swap::value`
// set to `true`, the allocators are also exchanged using an unqualified call
// to non-member `swap()`; otherwise, the allocators are not swapped.
using Base::swap;
// flat_hash_map::rehash(count)
//
// Rehashes the `flat_hash_map`, setting the number of slots to be at least
// the passed value. If the new number of slots increases the load factor more
// than the current maximum load factor
// (`count` < `size()` / `max_load_factor()`), then the new number of slots
// will be at least `size()` / `max_load_factor()`.
//
// To force a rehash, pass rehash(0).
//
// NOTE: unlike behavior in `std::unordered_map`, references are also
// invalidated upon a `rehash()`.
using Base::rehash;
// flat_hash_map::reserve(count)
//
// Sets the number of slots in the `flat_hash_map` to the number needed to
// accommodate at least `count` total elements without exceeding the current
// maximum load factor, and may rehash the container if needed.
using Base::reserve;
// flat_hash_map::at()
//
// Returns a reference to the mapped value of the element with key equivalent
// to the passed key.
using Base::at;
// flat_hash_map::contains()
//
// Determines whether an element with a key comparing equal to the given `key`
// exists within the `flat_hash_map`, returning `true` if so or `false`
// otherwise.
using Base::contains;
// flat_hash_map::count(const Key& key) const
//
// Returns the number of elements with a key comparing equal to the given
// `key` within the `flat_hash_map`. note that this function will return
// either `1` or `0` since duplicate keys are not allowed within a
// `flat_hash_map`.
using Base::count;
// flat_hash_map::equal_range()
//
// Returns a closed range [first, last], defined by a `std::pair` of two
// iterators, containing all elements with the passed key in the
// `flat_hash_map`.
using Base::equal_range;
// flat_hash_map::find()
//
// Finds an element with the passed `key` within the `flat_hash_map`.
using Base::find;
// flat_hash_map::operator[]()
//
// Returns a reference to the value mapped to the passed key within the
// `flat_hash_map`, performing an `insert()` if the key does not already
// exist.
//
// If an insertion occurs and results in a rehashing of the container, all
// iterators are invalidated. Otherwise iterators are not affected and
// references are not invalidated. Overloads are listed below.
//
// T& operator[](const Key& key):
//
// Inserts an init_type object constructed in-place if the element with the
// given key does not exist.
//
// T& operator[](Key&& key):
//
// Inserts an init_type object constructed in-place provided that an element
// with the given key does not exist.
using Base::operator[];
// flat_hash_map::bucket_count()
//
// Returns the number of "buckets" within the `flat_hash_map`. Note that
// because a flat hash map contains all elements within its internal storage,
// this value simply equals the current capacity of the `flat_hash_map`.
using Base::bucket_count;
// flat_hash_map::load_factor()
//
// Returns the current load factor of the `flat_hash_map` (the average number
// of slots occupied with a value within the hash map).
using Base::load_factor;
// flat_hash_map::max_load_factor()
//
// Manages the maximum load factor of the `flat_hash_map`. Overloads are
// listed below.
//
// float flat_hash_map::max_load_factor()
//
// Returns the current maximum load factor of the `flat_hash_map`.
//
// void flat_hash_map::max_load_factor(float ml)
//
// Sets the maximum load factor of the `flat_hash_map` to the passed value.
//
// NOTE: This overload is provided only for API compatibility with the STL;
// `flat_hash_map` will ignore any set load factor and manage its rehashing
// internally as an implementation detail.
using Base::max_load_factor;
// flat_hash_map::get_allocator()
//
// Returns the allocator function associated with this `flat_hash_map`.
using Base::get_allocator;
// flat_hash_map::hash_function()
//
// Returns the hashing function used to hash the keys within this
// `flat_hash_map`.
using Base::hash_function;
// flat_hash_map::key_eq()
//
// Returns the function used for comparing keys equality.
using Base::key_eq;
};
// erase_if(flat_hash_map<>, Pred)
//
// Erases all elements that satisfy the predicate `pred` from the container `c`.
// Returns the number of erased elements.
template <typename K, typename V, typename H, typename E, typename A,
typename Predicate>
typename flat_hash_map<K, V, H, E, A>::size_type erase_if(
flat_hash_map<K, V, H, E, A>& c, Predicate pred) {
return container_internal::EraseIf(pred, &c);
}
// swap(flat_hash_map<>, flat_hash_map<>)
//
// Swaps the contents of two `flat_hash_map` containers.
//
// NOTE: we need to define this function template in order for
// `flat_hash_set::swap` to be called instead of `std::swap`. Even though we
// have `swap(raw_hash_set&, raw_hash_set&)` defined, that function requires a
// derived-to-base conversion, whereas `std::swap` is a function template so
// `std::swap` will be preferred by compiler.
template <typename K, typename V, typename H, typename E, typename A>
void swap(flat_hash_map<K, V, H, E, A>& x,
flat_hash_map<K, V, H, E, A>& y) noexcept(noexcept(x.swap(y))) {
x.swap(y);
}
namespace container_internal {
// c_for_each_fast(flat_hash_map<>, Function)
//
// Container-based version of the <algorithm> `std::for_each()` function to
// apply a function to a container's elements.
// There is no guarantees on the order of the function calls.
// Erasure and/or insertion of elements in the function is not allowed.
template <typename K, typename V, typename H, typename E, typename A,
typename Function>
decay_t<Function> c_for_each_fast(const flat_hash_map<K, V, H, E, A>& c,
Function&& f) {
container_internal::ForEach(f, &c);
return f;
}
template <typename K, typename V, typename H, typename E, typename A,
typename Function>
decay_t<Function> c_for_each_fast(flat_hash_map<K, V, H, E, A>& c,
Function&& f) {
container_internal::ForEach(f, &c);
return f;
}
template <typename K, typename V, typename H, typename E, typename A,
typename Function>
decay_t<Function> c_for_each_fast(flat_hash_map<K, V, H, E, A>&& c,
Function&& f) {
container_internal::ForEach(f, &c);
return f;
}
} // namespace container_internal
namespace container_internal {
template <class K, class V>
struct FlatHashMapPolicy {
using slot_policy = container_internal::map_slot_policy<K, V>;
using slot_type = typename slot_policy::slot_type;
using key_type = K;
using mapped_type = V;
using init_type = std::pair</*non const*/ key_type, mapped_type>;
template <class Allocator, class... Args>
static void construct(Allocator* alloc, slot_type* slot, Args&&... args) {
slot_policy::construct(alloc, slot, std::forward<Args>(args)...);
}
// Returns std::true_type in case destroy is trivial.
template <class Allocator>
static auto destroy(Allocator* alloc, slot_type* slot) {
return slot_policy::destroy(alloc, slot);
}
template <class Allocator>
static auto transfer(Allocator* alloc, slot_type* new_slot,
slot_type* old_slot) {
return slot_policy::transfer(alloc, new_slot, old_slot);
}
template <class F, class... Args>
static decltype(absl::container_internal::DecomposePair(
std::declval<F>(), std::declval<Args>()...))
apply(F&& f, Args&&... args) {
return absl::container_internal::DecomposePair(std::forward<F>(f),
std::forward<Args>(args)...);
}
template <class Hash>
static constexpr HashSlotFn get_hash_slot_fn() {
return memory_internal::IsLayoutCompatible<K, V>::value
? &TypeErasedApplyToSlotFn<Hash, K>
: nullptr;
}
static size_t space_used(const slot_type*) { return 0; }
static std::pair<const K, V>& element(slot_type* slot) { return slot->value; }
static V& value(std::pair<const K, V>* kv) { return kv->second; }
static const V& value(const std::pair<const K, V>* kv) { return kv->second; }
};
} // namespace container_internal
namespace container_algorithm_internal {
// Specialization of trait in absl/algorithm/container.h
template <class Key, class T, class Hash, class KeyEqual, class Allocator>
struct IsUnorderedContainer<
absl::flat_hash_map<Key, T, Hash, KeyEqual, Allocator>> : std::true_type {};
} // namespace container_algorithm_internal
ABSL_NAMESPACE_END
} // namespace absl
#endif // ABSL_CONTAINER_FLAT_HASH_MAP_H_

View file

@ -0,0 +1,458 @@
// Copyright 2018 The Abseil Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "absl/container/flat_hash_map.h"
#include <cstddef>
#include <memory>
#include <string>
#include <type_traits>
#include <utility>
#include <vector>
#include "gmock/gmock.h"
#include "gtest/gtest.h"
#include "absl/base/config.h"
#include "absl/container/internal/hash_generator_testing.h"
#include "absl/container/internal/hash_policy_testing.h"
#include "absl/container/internal/test_allocator.h"
#include "absl/container/internal/unordered_map_constructor_test.h"
#include "absl/container/internal/unordered_map_lookup_test.h"
#include "absl/container/internal/unordered_map_members_test.h"
#include "absl/container/internal/unordered_map_modifiers_test.h"
#include "absl/log/check.h"
#include "absl/meta/type_traits.h"
#include "absl/types/any.h"
namespace absl {
ABSL_NAMESPACE_BEGIN
namespace container_internal {
namespace {
using ::absl::container_internal::hash_internal::Enum;
using ::absl::container_internal::hash_internal::EnumClass;
using ::testing::_;
using ::testing::IsEmpty;
using ::testing::Pair;
using ::testing::UnorderedElementsAre;
using ::testing::UnorderedElementsAreArray;
// Check that absl::flat_hash_map works in a global constructor.
struct BeforeMain {
BeforeMain() {
absl::flat_hash_map<int, int> x;
x.insert({1, 1});
CHECK(x.find(0) == x.end()) << "x should not contain 0";
auto it = x.find(1);
CHECK(it != x.end()) << "x should contain 1";
CHECK(it->second) << "1 should map to 1";
}
};
const BeforeMain before_main;
template <class K, class V>
using Map = flat_hash_map<K, V, StatefulTestingHash, StatefulTestingEqual,
Alloc<std::pair<const K, V>>>;
static_assert(!std::is_standard_layout<NonStandardLayout>(), "");
using MapTypes =
::testing::Types<Map<int, int>, Map<std::string, int>,
Map<Enum, std::string>, Map<EnumClass, int>,
Map<int, NonStandardLayout>, Map<NonStandardLayout, int>>;
INSTANTIATE_TYPED_TEST_SUITE_P(FlatHashMap, ConstructorTest, MapTypes);
INSTANTIATE_TYPED_TEST_SUITE_P(FlatHashMap, LookupTest, MapTypes);
INSTANTIATE_TYPED_TEST_SUITE_P(FlatHashMap, MembersTest, MapTypes);
INSTANTIATE_TYPED_TEST_SUITE_P(FlatHashMap, ModifiersTest, MapTypes);
using UniquePtrMapTypes = ::testing::Types<Map<int, std::unique_ptr<int>>>;
INSTANTIATE_TYPED_TEST_SUITE_P(FlatHashMap, UniquePtrModifiersTest,
UniquePtrMapTypes);
TEST(FlatHashMap, StandardLayout) {
struct Int {
explicit Int(size_t value) : value(value) {}
Int() : value(0) { ADD_FAILURE(); }
Int(const Int& other) : value(other.value) { ADD_FAILURE(); }
Int(Int&&) = default;
bool operator==(const Int& other) const { return value == other.value; }
size_t value;
};
static_assert(std::is_standard_layout<Int>(), "");
struct Hash {
size_t operator()(const Int& obj) const { return obj.value; }
};
// Verify that neither the key nor the value get default-constructed or
// copy-constructed.
{
flat_hash_map<Int, Int, Hash> m;
m.try_emplace(Int(1), Int(2));
m.try_emplace(Int(3), Int(4));
m.erase(Int(1));
m.rehash(2 * m.bucket_count());
}
{
flat_hash_map<Int, Int, Hash> m;
m.try_emplace(Int(1), Int(2));
m.try_emplace(Int(3), Int(4));
m.erase(Int(1));
m.clear();
}
}
TEST(FlatHashMap, Relocatability) {
static_assert(absl::is_trivially_relocatable<int>::value, "");
static_assert(
absl::is_trivially_relocatable<std::pair<const int, int>>::value, "");
static_assert(
std::is_same<decltype(absl::container_internal::FlatHashMapPolicy<
int, int>::transfer<std::allocator<char>>(nullptr,
nullptr,
nullptr)),
std::true_type>::value,
"");
struct NonRelocatable {
NonRelocatable() = default;
NonRelocatable(NonRelocatable&&) {}
NonRelocatable& operator=(NonRelocatable&&) { return *this; }
void* self = nullptr;
};
EXPECT_FALSE(absl::is_trivially_relocatable<NonRelocatable>::value);
EXPECT_TRUE(
(std::is_same<decltype(absl::container_internal::FlatHashMapPolicy<
int, NonRelocatable>::
transfer<std::allocator<char>>(nullptr, nullptr,
nullptr)),
std::false_type>::value));
}
// gcc becomes unhappy if this is inside the method, so pull it out here.
struct balast {};
TEST(FlatHashMap, IteratesMsan) {
// Because SwissTable randomizes on pointer addresses, we keep old tables
// around to ensure we don't reuse old memory.
std::vector<absl::flat_hash_map<int, balast>> garbage;
for (int i = 0; i < 100; ++i) {
absl::flat_hash_map<int, balast> t;
for (int j = 0; j < 100; ++j) {
t[j];
for (const auto& p : t) EXPECT_THAT(p, Pair(_, _));
}
garbage.push_back(std::move(t));
}
}
// Demonstration of the "Lazy Key" pattern. This uses heterogeneous insert to
// avoid creating expensive key elements when the item is already present in the
// map.
struct LazyInt {
explicit LazyInt(size_t value, int* tracker)
: value(value), tracker(tracker) {}
explicit operator size_t() const {
++*tracker;
return value;
}
size_t value;
int* tracker;
};
struct Hash {
using is_transparent = void;
int* tracker;
size_t operator()(size_t obj) const {
++*tracker;
return obj;
}
size_t operator()(const LazyInt& obj) const {
++*tracker;
return obj.value;
}
};
struct Eq {
using is_transparent = void;
bool operator()(size_t lhs, size_t rhs) const { return lhs == rhs; }
bool operator()(size_t lhs, const LazyInt& rhs) const {
return lhs == rhs.value;
}
};
TEST(FlatHashMap, LazyKeyPattern) {
// hashes are only guaranteed in opt mode, we use assertions to track internal
// state that can cause extra calls to hash.
int conversions = 0;
int hashes = 0;
flat_hash_map<size_t, size_t, Hash, Eq> m(0, Hash{&hashes});
m.reserve(3);
m[LazyInt(1, &conversions)] = 1;
EXPECT_THAT(m, UnorderedElementsAre(Pair(1, 1)));
EXPECT_EQ(conversions, 1);
#ifdef NDEBUG
EXPECT_EQ(hashes, 1);
#endif
m[LazyInt(1, &conversions)] = 2;
EXPECT_THAT(m, UnorderedElementsAre(Pair(1, 2)));
EXPECT_EQ(conversions, 1);
#ifdef NDEBUG
EXPECT_EQ(hashes, 2);
#endif
m.try_emplace(LazyInt(2, &conversions), 3);
EXPECT_THAT(m, UnorderedElementsAre(Pair(1, 2), Pair(2, 3)));
EXPECT_EQ(conversions, 2);
#ifdef NDEBUG
EXPECT_EQ(hashes, 3);
#endif
m.try_emplace(LazyInt(2, &conversions), 4);
EXPECT_THAT(m, UnorderedElementsAre(Pair(1, 2), Pair(2, 3)));
EXPECT_EQ(conversions, 2);
#ifdef NDEBUG
EXPECT_EQ(hashes, 4);
#endif
}
TEST(FlatHashMap, BitfieldArgument) {
union {
int n : 1;
};
n = 0;
flat_hash_map<int, int> m;
m.erase(n);
m.count(n);
m.prefetch(n);
m.find(n);
m.contains(n);
m.equal_range(n);
m.insert_or_assign(n, n);
m.insert_or_assign(m.end(), n, n);
m.try_emplace(n);
m.try_emplace(m.end(), n);
m.at(n);
m[n];
}
TEST(FlatHashMap, MergeExtractInsert) {
// We can't test mutable keys, or non-copyable keys with flat_hash_map.
// Test that the nodes have the proper API.
absl::flat_hash_map<int, int> m = {{1, 7}, {2, 9}};
auto node = m.extract(1);
EXPECT_TRUE(node);
EXPECT_EQ(node.key(), 1);
EXPECT_EQ(node.mapped(), 7);
EXPECT_THAT(m, UnorderedElementsAre(Pair(2, 9)));
node.mapped() = 17;
m.insert(std::move(node));
EXPECT_THAT(m, UnorderedElementsAre(Pair(1, 17), Pair(2, 9)));
}
bool FirstIsEven(std::pair<const int, int> p) { return p.first % 2 == 0; }
TEST(FlatHashMap, EraseIf) {
// Erase all elements.
{
flat_hash_map<int, int> s = {{1, 1}, {2, 2}, {3, 3}, {4, 4}, {5, 5}};
EXPECT_EQ(erase_if(s, [](std::pair<const int, int>) { return true; }), 5);
EXPECT_THAT(s, IsEmpty());
}
// Erase no elements.
{
flat_hash_map<int, int> s = {{1, 1}, {2, 2}, {3, 3}, {4, 4}, {5, 5}};
EXPECT_EQ(erase_if(s, [](std::pair<const int, int>) { return false; }), 0);
EXPECT_THAT(s, UnorderedElementsAre(Pair(1, 1), Pair(2, 2), Pair(3, 3),
Pair(4, 4), Pair(5, 5)));
}
// Erase specific elements.
{
flat_hash_map<int, int> s = {{1, 1}, {2, 2}, {3, 3}, {4, 4}, {5, 5}};
EXPECT_EQ(erase_if(s,
[](std::pair<const int, int> kvp) {
return kvp.first % 2 == 1;
}),
3);
EXPECT_THAT(s, UnorderedElementsAre(Pair(2, 2), Pair(4, 4)));
}
// Predicate is function reference.
{
flat_hash_map<int, int> s = {{1, 1}, {2, 2}, {3, 3}, {4, 4}, {5, 5}};
EXPECT_EQ(erase_if(s, FirstIsEven), 2);
EXPECT_THAT(s, UnorderedElementsAre(Pair(1, 1), Pair(3, 3), Pair(5, 5)));
}
// Predicate is function pointer.
{
flat_hash_map<int, int> s = {{1, 1}, {2, 2}, {3, 3}, {4, 4}, {5, 5}};
EXPECT_EQ(erase_if(s, &FirstIsEven), 2);
EXPECT_THAT(s, UnorderedElementsAre(Pair(1, 1), Pair(3, 3), Pair(5, 5)));
}
}
TEST(FlatHashMap, CForEach) {
flat_hash_map<int, int> m;
std::vector<std::pair<int, int>> expected;
for (int i = 0; i < 100; ++i) {
{
SCOPED_TRACE("mutable object iteration");
std::vector<std::pair<int, int>> v;
absl::container_internal::c_for_each_fast(
m, [&v](std::pair<const int, int>& p) { v.push_back(p); });
EXPECT_THAT(v, UnorderedElementsAreArray(expected));
}
{
SCOPED_TRACE("const object iteration");
std::vector<std::pair<int, int>> v;
const flat_hash_map<int, int>& cm = m;
absl::container_internal::c_for_each_fast(
cm, [&v](const std::pair<const int, int>& p) { v.push_back(p); });
EXPECT_THAT(v, UnorderedElementsAreArray(expected));
}
{
SCOPED_TRACE("const object iteration");
std::vector<std::pair<int, int>> v;
absl::container_internal::c_for_each_fast(
flat_hash_map<int, int>(m),
[&v](std::pair<const int, int>& p) { v.push_back(p); });
EXPECT_THAT(v, UnorderedElementsAreArray(expected));
}
m[i] = i;
expected.emplace_back(i, i);
}
}
TEST(FlatHashMap, CForEachMutate) {
flat_hash_map<int, int> s;
std::vector<std::pair<int, int>> expected;
for (int i = 0; i < 100; ++i) {
std::vector<std::pair<int, int>> v;
absl::container_internal::c_for_each_fast(
s, [&v](std::pair<const int, int>& p) {
v.push_back(p);
p.second++;
});
EXPECT_THAT(v, UnorderedElementsAreArray(expected));
for (auto& p : expected) {
p.second++;
}
EXPECT_THAT(s, UnorderedElementsAreArray(expected));
s[i] = i;
expected.emplace_back(i, i);
}
}
// This test requires std::launder for mutable key access in node handles.
#if defined(__cpp_lib_launder) && __cpp_lib_launder >= 201606
TEST(FlatHashMap, NodeHandleMutableKeyAccess) {
flat_hash_map<std::string, std::string> map;
map["key1"] = "mapped";
auto nh = map.extract(map.begin());
nh.key().resize(3);
map.insert(std::move(nh));
EXPECT_THAT(map, testing::ElementsAre(Pair("key", "mapped")));
}
#endif
TEST(FlatHashMap, Reserve) {
// Verify that if we reserve(size() + n) then we can perform n insertions
// without a rehash, i.e., without invalidating any references.
for (size_t trial = 0; trial < 20; ++trial) {
for (size_t initial = 3; initial < 100; ++initial) {
// Fill in `initial` entries, then erase 2 of them, then reserve space for
// two inserts and check for reference stability while doing the inserts.
flat_hash_map<size_t, size_t> map;
for (size_t i = 0; i < initial; ++i) {
map[i] = i;
}
map.erase(0);
map.erase(1);
map.reserve(map.size() + 2);
size_t& a2 = map[2];
// In the event of a failure, asan will complain in one of these two
// assignments.
map[initial] = a2;
map[initial + 1] = a2;
// Fail even when not under asan:
size_t& a2new = map[2];
EXPECT_EQ(&a2, &a2new);
}
}
}
TEST(FlatHashMap, RecursiveTypeCompiles) {
struct RecursiveType {
flat_hash_map<int, RecursiveType> m;
};
RecursiveType t;
t.m[0] = RecursiveType{};
}
TEST(FlatHashMap, FlatHashMapPolicyDestroyReturnsTrue) {
EXPECT_TRUE(
(decltype(FlatHashMapPolicy<int, char>::destroy<std::allocator<char>>(
nullptr, nullptr))()));
EXPECT_FALSE(
(decltype(FlatHashMapPolicy<int, char>::destroy<CountingAllocator<char>>(
nullptr, nullptr))()));
EXPECT_FALSE((decltype(FlatHashMapPolicy<int, std::unique_ptr<int>>::destroy<
std::allocator<char>>(nullptr, nullptr))()));
}
struct InconsistentHashEqType {
InconsistentHashEqType(int v1, int v2) : v1(v1), v2(v2) {}
template <typename H>
friend H AbslHashValue(H h, InconsistentHashEqType t) {
return H::combine(std::move(h), t.v1);
}
bool operator==(InconsistentHashEqType t) const { return v2 == t.v2; }
int v1, v2;
};
TEST(Iterator, InconsistentHashEqFunctorsValidation) {
if (!IsAssertEnabled()) GTEST_SKIP() << "Assertions not enabled.";
absl::flat_hash_map<InconsistentHashEqType, int> m;
for (int i = 0; i < 10; ++i) m[{i, i}] = 1;
// We need to insert multiple times to guarantee that we get the assertion
// because it's possible for the hash to collide with the inserted element
// that has v2==0. In those cases, the new element won't be inserted.
auto insert_conflicting_elems = [&] {
for (int i = 100; i < 20000; ++i) {
EXPECT_EQ((m[{i, 0}]), 1);
}
};
const char* crash_message = "hash/eq functors are inconsistent.";
#if defined(__arm__) || defined(__aarch64__)
// On ARM, the crash message is garbled so don't expect a specific message.
crash_message = "";
#endif
EXPECT_DEATH_IF_SUPPORTED(insert_conflicting_elems(), crash_message);
}
} // namespace
} // namespace container_internal
ABSL_NAMESPACE_END
} // namespace absl

View file

@ -0,0 +1,575 @@
// Copyright 2018 The Abseil Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// -----------------------------------------------------------------------------
// File: flat_hash_set.h
// -----------------------------------------------------------------------------
//
// An `absl::flat_hash_set<T>` is an unordered associative container designed to
// be a more efficient replacement for `std::unordered_set`. Like
// `unordered_set`, search, insertion, and deletion of set elements can be done
// as an `O(1)` operation. However, `flat_hash_set` (and other unordered
// associative containers known as the collection of Abseil "Swiss tables")
// contain other optimizations that result in both memory and computation
// advantages.
//
// In most cases, your default choice for a hash set should be a set of type
// `flat_hash_set`.
//
// `flat_hash_set` is not exception-safe.
#ifndef ABSL_CONTAINER_FLAT_HASH_SET_H_
#define ABSL_CONTAINER_FLAT_HASH_SET_H_
#include <cstddef>
#include <memory>
#include <type_traits>
#include <utility>
#include "absl/algorithm/container.h"
#include "absl/base/attributes.h"
#include "absl/base/macros.h"
#include "absl/container/hash_container_defaults.h"
#include "absl/container/internal/container_memory.h"
#include "absl/container/internal/raw_hash_set.h" // IWYU pragma: export
#include "absl/memory/memory.h"
#include "absl/meta/type_traits.h"
namespace absl {
ABSL_NAMESPACE_BEGIN
namespace container_internal {
template <typename T>
struct FlatHashSetPolicy;
} // namespace container_internal
// -----------------------------------------------------------------------------
// absl::flat_hash_set
// -----------------------------------------------------------------------------
//
// An `absl::flat_hash_set<T>` is an unordered associative container which has
// been optimized for both speed and memory footprint in most common use cases.
// Its interface is similar to that of `std::unordered_set<T>` with the
// following notable differences:
//
// * Requires keys that are CopyConstructible
// * Supports heterogeneous lookup, through `find()` and `insert()`, provided
// that the set is provided a compatible heterogeneous hashing function and
// equality operator. See below for details.
// * Invalidates any references and pointers to elements within the table after
// `rehash()` and when the table is moved.
// * Contains a `capacity()` member function indicating the number of element
// slots (open, deleted, and empty) within the hash set.
// * Returns `void` from the `erase(iterator)` overload.
//
// By default, `flat_hash_set` uses the `absl::Hash` hashing framework. All
// fundamental and Abseil types that support the `absl::Hash` framework have a
// compatible equality operator for comparing insertions into `flat_hash_set`.
// If your type is not yet supported by the `absl::Hash` framework, see
// absl/hash/hash.h for information on extending Abseil hashing to user-defined
// types.
//
// Using `absl::flat_hash_set` at interface boundaries in dynamically loaded
// libraries (e.g. .dll, .so) is unsupported due to way `absl::Hash` values may
// be randomized across dynamically loaded libraries.
//
// To achieve heterogeneous lookup for custom types either `Hash` and `Eq` type
// parameters can be used or `T` should have public inner types
// `absl_container_hash` and (optionally) `absl_container_eq`. In either case,
// `typename Hash::is_transparent` and `typename Eq::is_transparent` should be
// well-formed. Both types are basically functors:
// * `Hash` should support `size_t operator()(U val) const` that returns a hash
// for the given `val`.
// * `Eq` should support `bool operator()(U lhs, V rhs) const` that returns true
// if `lhs` is equal to `rhs`.
//
// In most cases `T` needs only to provide the `absl_container_hash`. In this
// case `std::equal_to<void>` will be used instead of `eq` part.
//
// NOTE: A `flat_hash_set` stores its keys directly inside its implementation
// array to avoid memory indirection. Because a `flat_hash_set` is designed to
// move data when rehashed, set keys will not retain pointer stability. If you
// require pointer stability, consider using
// `absl::flat_hash_set<std::unique_ptr<T>>`. If your type is not moveable and
// you require pointer stability, consider `absl::node_hash_set` instead.
//
// Example:
//
// // Create a flat hash set of three strings
// absl::flat_hash_set<std::string> ducks =
// {"huey", "dewey", "louie"};
//
// // Insert a new element into the flat hash set
// ducks.insert("donald");
//
// // Force a rehash of the flat hash set
// ducks.rehash(0);
//
// // See if "dewey" is present
// if (ducks.contains("dewey")) {
// std::cout << "We found dewey!" << std::endl;
// }
template <class T, class Hash = DefaultHashContainerHash<T>,
class Eq = DefaultHashContainerEq<T>,
class Allocator = std::allocator<T>>
class ABSL_ATTRIBUTE_OWNER flat_hash_set
: public absl::container_internal::raw_hash_set<
absl::container_internal::FlatHashSetPolicy<T>, Hash, Eq, Allocator> {
using Base = typename flat_hash_set::raw_hash_set;
public:
// Constructors and Assignment Operators
//
// A flat_hash_set supports the same overload set as `std::unordered_set`
// for construction and assignment:
//
// * Default constructor
//
// // No allocation for the table's elements is made.
// absl::flat_hash_set<std::string> set1;
//
// * Initializer List constructor
//
// absl::flat_hash_set<std::string> set2 =
// {{"huey"}, {"dewey"}, {"louie"},};
//
// * Copy constructor
//
// absl::flat_hash_set<std::string> set3(set2);
//
// * Copy assignment operator
//
// // Hash functor and Comparator are copied as well
// absl::flat_hash_set<std::string> set4;
// set4 = set3;
//
// * Move constructor
//
// // Move is guaranteed efficient
// absl::flat_hash_set<std::string> set5(std::move(set4));
//
// * Move assignment operator
//
// // May be efficient if allocators are compatible
// absl::flat_hash_set<std::string> set6;
// set6 = std::move(set5);
//
// * Range constructor
//
// std::vector<std::string> v = {"a", "b"};
// absl::flat_hash_set<std::string> set7(v.begin(), v.end());
flat_hash_set() {}
using Base::Base;
// flat_hash_set::begin()
//
// Returns an iterator to the beginning of the `flat_hash_set`.
using Base::begin;
// flat_hash_set::cbegin()
//
// Returns a const iterator to the beginning of the `flat_hash_set`.
using Base::cbegin;
// flat_hash_set::cend()
//
// Returns a const iterator to the end of the `flat_hash_set`.
using Base::cend;
// flat_hash_set::end()
//
// Returns an iterator to the end of the `flat_hash_set`.
using Base::end;
// flat_hash_set::capacity()
//
// Returns the number of element slots (assigned, deleted, and empty)
// available within the `flat_hash_set`.
//
// NOTE: this member function is particular to `absl::flat_hash_set` and is
// not provided in the `std::unordered_set` API.
using Base::capacity;
// flat_hash_set::empty()
//
// Returns whether or not the `flat_hash_set` is empty.
using Base::empty;
// flat_hash_set::max_size()
//
// Returns the largest theoretical possible number of elements within a
// `flat_hash_set` under current memory constraints. This value can be thought
// of the largest value of `std::distance(begin(), end())` for a
// `flat_hash_set<T>`.
using Base::max_size;
// flat_hash_set::size()
//
// Returns the number of elements currently within the `flat_hash_set`.
using Base::size;
// flat_hash_set::clear()
//
// Removes all elements from the `flat_hash_set`. Invalidates any references,
// pointers, or iterators referring to contained elements.
//
// NOTE: this operation may shrink the underlying buffer. To avoid shrinking
// the underlying buffer call `erase(begin(), end())`.
using Base::clear;
// flat_hash_set::erase()
//
// Erases elements within the `flat_hash_set`. Erasing does not trigger a
// rehash. Overloads are listed below.
//
// void erase(const_iterator pos):
//
// Erases the element at `position` of the `flat_hash_set`, returning
// `void`.
//
// NOTE: returning `void` in this case is different than that of STL
// containers in general and `std::unordered_set` in particular (which
// return an iterator to the element following the erased element). If that
// iterator is needed, simply post increment the iterator:
//
// set.erase(it++);
//
// iterator erase(const_iterator first, const_iterator last):
//
// Erases the elements in the open interval [`first`, `last`), returning an
// iterator pointing to `last`. The special case of calling
// `erase(begin(), end())` resets the reserved growth such that if
// `reserve(N)` has previously been called and there has been no intervening
// call to `clear()`, then after calling `erase(begin(), end())`, it is safe
// to assume that inserting N elements will not cause a rehash.
//
// size_type erase(const key_type& key):
//
// Erases the element with the matching key, if it exists, returning the
// number of elements erased (0 or 1).
using Base::erase;
// flat_hash_set::insert()
//
// Inserts an element of the specified value into the `flat_hash_set`,
// returning an iterator pointing to the newly inserted element, provided that
// an element with the given key does not already exist. If rehashing occurs
// due to the insertion, all iterators are invalidated. Overloads are listed
// below.
//
// std::pair<iterator,bool> insert(const T& value):
//
// Inserts a value into the `flat_hash_set`. Returns a pair consisting of an
// iterator to the inserted element (or to the element that prevented the
// insertion) and a bool denoting whether the insertion took place.
//
// std::pair<iterator,bool> insert(T&& value):
//
// Inserts a moveable value into the `flat_hash_set`. Returns a pair
// consisting of an iterator to the inserted element (or to the element that
// prevented the insertion) and a bool denoting whether the insertion took
// place.
//
// iterator insert(const_iterator hint, const T& value):
// iterator insert(const_iterator hint, T&& value):
//
// Inserts a value, using the position of `hint` as a non-binding suggestion
// for where to begin the insertion search. Returns an iterator to the
// inserted element, or to the existing element that prevented the
// insertion.
//
// void insert(InputIterator first, InputIterator last):
//
// Inserts a range of values [`first`, `last`).
//
// NOTE: Although the STL does not specify which element may be inserted if
// multiple keys compare equivalently, for `flat_hash_set` we guarantee the
// first match is inserted.
//
// void insert(std::initializer_list<T> ilist):
//
// Inserts the elements within the initializer list `ilist`.
//
// NOTE: Although the STL does not specify which element may be inserted if
// multiple keys compare equivalently within the initializer list, for
// `flat_hash_set` we guarantee the first match is inserted.
using Base::insert;
// flat_hash_set::emplace()
//
// Inserts an element of the specified value by constructing it in-place
// within the `flat_hash_set`, provided that no element with the given key
// already exists.
//
// The element may be constructed even if there already is an element with the
// key in the container, in which case the newly constructed element will be
// destroyed immediately.
//
// If rehashing occurs due to the insertion, all iterators are invalidated.
using Base::emplace;
// flat_hash_set::emplace_hint()
//
// Inserts an element of the specified value by constructing it in-place
// within the `flat_hash_set`, using the position of `hint` as a non-binding
// suggestion for where to begin the insertion search, and only inserts
// provided that no element with the given key already exists.
//
// The element may be constructed even if there already is an element with the
// key in the container, in which case the newly constructed element will be
// destroyed immediately.
//
// If rehashing occurs due to the insertion, all iterators are invalidated.
using Base::emplace_hint;
// flat_hash_set::extract()
//
// Extracts the indicated element, erasing it in the process, and returns it
// as a C++17-compatible node handle. Overloads are listed below.
//
// node_type extract(const_iterator position):
//
// Extracts the element at the indicated position and returns a node handle
// owning that extracted data.
//
// node_type extract(const key_type& x):
//
// Extracts the element with the key matching the passed key value and
// returns a node handle owning that extracted data. If the `flat_hash_set`
// does not contain an element with a matching key, this function returns an
// empty node handle.
using Base::extract;
// flat_hash_set::merge()
//
// Extracts elements from a given `source` flat hash set into this
// `flat_hash_set`. If the destination `flat_hash_set` already contains an
// element with an equivalent key, that element is not extracted.
using Base::merge;
// flat_hash_set::swap(flat_hash_set& other)
//
// Exchanges the contents of this `flat_hash_set` with those of the `other`
// flat hash set.
//
// All iterators and references on the `flat_hash_set` remain valid, excepting
// for the past-the-end iterator, which is invalidated.
//
// `swap()` requires that the flat hash set's hashing and key equivalence
// functions be Swappable, and are exchanged using unqualified calls to
// non-member `swap()`. If the set's allocator has
// `std::allocator_traits<allocator_type>::propagate_on_container_swap::value`
// set to `true`, the allocators are also exchanged using an unqualified call
// to non-member `swap()`; otherwise, the allocators are not swapped.
using Base::swap;
// flat_hash_set::rehash(count)
//
// Rehashes the `flat_hash_set`, setting the number of slots to be at least
// the passed value. If the new number of slots increases the load factor more
// than the current maximum load factor
// (`count` < `size()` / `max_load_factor()`), then the new number of slots
// will be at least `size()` / `max_load_factor()`.
//
// To force a rehash, pass rehash(0).
//
// NOTE: unlike behavior in `std::unordered_set`, references are also
// invalidated upon a `rehash()`.
using Base::rehash;
// flat_hash_set::reserve(count)
//
// Sets the number of slots in the `flat_hash_set` to the number needed to
// accommodate at least `count` total elements without exceeding the current
// maximum load factor, and may rehash the container if needed.
using Base::reserve;
// flat_hash_set::contains()
//
// Determines whether an element comparing equal to the given `key` exists
// within the `flat_hash_set`, returning `true` if so or `false` otherwise.
using Base::contains;
// flat_hash_set::count(const Key& key) const
//
// Returns the number of elements comparing equal to the given `key` within
// the `flat_hash_set`. note that this function will return either `1` or `0`
// since duplicate elements are not allowed within a `flat_hash_set`.
using Base::count;
// flat_hash_set::equal_range()
//
// Returns a closed range [first, last], defined by a `std::pair` of two
// iterators, containing all elements with the passed key in the
// `flat_hash_set`.
using Base::equal_range;
// flat_hash_set::find()
//
// Finds an element with the passed `key` within the `flat_hash_set`.
using Base::find;
// flat_hash_set::bucket_count()
//
// Returns the number of "buckets" within the `flat_hash_set`. Note that
// because a flat hash set contains all elements within its internal storage,
// this value simply equals the current capacity of the `flat_hash_set`.
using Base::bucket_count;
// flat_hash_set::load_factor()
//
// Returns the current load factor of the `flat_hash_set` (the average number
// of slots occupied with a value within the hash set).
using Base::load_factor;
// flat_hash_set::max_load_factor()
//
// Manages the maximum load factor of the `flat_hash_set`. Overloads are
// listed below.
//
// float flat_hash_set::max_load_factor()
//
// Returns the current maximum load factor of the `flat_hash_set`.
//
// void flat_hash_set::max_load_factor(float ml)
//
// Sets the maximum load factor of the `flat_hash_set` to the passed value.
//
// NOTE: This overload is provided only for API compatibility with the STL;
// `flat_hash_set` will ignore any set load factor and manage its rehashing
// internally as an implementation detail.
using Base::max_load_factor;
// flat_hash_set::get_allocator()
//
// Returns the allocator function associated with this `flat_hash_set`.
using Base::get_allocator;
// flat_hash_set::hash_function()
//
// Returns the hashing function used to hash the keys within this
// `flat_hash_set`.
using Base::hash_function;
// flat_hash_set::key_eq()
//
// Returns the function used for comparing keys equality.
using Base::key_eq;
};
// erase_if(flat_hash_set<>, Pred)
//
// Erases all elements that satisfy the predicate `pred` from the container `c`.
// Returns the number of erased elements.
template <typename T, typename H, typename E, typename A, typename Predicate>
typename flat_hash_set<T, H, E, A>::size_type erase_if(
flat_hash_set<T, H, E, A>& c, Predicate pred) {
return container_internal::EraseIf(pred, &c);
}
// swap(flat_hash_set<>, flat_hash_set<>)
//
// Swaps the contents of two `flat_hash_set` containers.
//
// NOTE: we need to define this function template in order for
// `flat_hash_set::swap` to be called instead of `std::swap`. Even though we
// have `swap(raw_hash_set&, raw_hash_set&)` defined, that function requires a
// derived-to-base conversion, whereas `std::swap` is a function template so
// `std::swap` will be preferred by compiler.
template <typename T, typename H, typename E, typename A>
void swap(flat_hash_set<T, H, E, A>& x,
flat_hash_set<T, H, E, A>& y) noexcept(noexcept(x.swap(y))) {
return x.swap(y);
}
namespace container_internal {
// c_for_each_fast(flat_hash_set<>, Function)
//
// Container-based version of the <algorithm> `std::for_each()` function to
// apply a function to a container's elements.
// There is no guarantees on the order of the function calls.
// Erasure and/or insertion of elements in the function is not allowed.
template <typename T, typename H, typename E, typename A, typename Function>
decay_t<Function> c_for_each_fast(const flat_hash_set<T, H, E, A>& c,
Function&& f) {
container_internal::ForEach(f, &c);
return f;
}
template <typename T, typename H, typename E, typename A, typename Function>
decay_t<Function> c_for_each_fast(flat_hash_set<T, H, E, A>& c, Function&& f) {
container_internal::ForEach(f, &c);
return f;
}
template <typename T, typename H, typename E, typename A, typename Function>
decay_t<Function> c_for_each_fast(flat_hash_set<T, H, E, A>&& c, Function&& f) {
container_internal::ForEach(f, &c);
return f;
}
} // namespace container_internal
namespace container_internal {
template <class T>
struct FlatHashSetPolicy {
using slot_type = T;
using key_type = T;
using init_type = T;
using constant_iterators = std::true_type;
template <class Allocator, class... Args>
static void construct(Allocator* alloc, slot_type* slot, Args&&... args) {
absl::allocator_traits<Allocator>::construct(*alloc, slot,
std::forward<Args>(args)...);
}
// Return std::true_type in case destroy is trivial.
template <class Allocator>
static auto destroy(Allocator* alloc, slot_type* slot) {
absl::allocator_traits<Allocator>::destroy(*alloc, slot);
return IsDestructionTrivial<Allocator, slot_type>();
}
static T& element(slot_type* slot) { return *slot; }
template <class F, class... Args>
static decltype(absl::container_internal::DecomposeValue(
std::declval<F>(), std::declval<Args>()...))
apply(F&& f, Args&&... args) {
return absl::container_internal::DecomposeValue(
std::forward<F>(f), std::forward<Args>(args)...);
}
static size_t space_used(const T*) { return 0; }
template <class Hash>
static constexpr HashSlotFn get_hash_slot_fn() {
return &TypeErasedApplyToSlotFn<Hash, T>;
}
};
} // namespace container_internal
namespace container_algorithm_internal {
// Specialization of trait in absl/algorithm/container.h
template <class Key, class Hash, class KeyEqual, class Allocator>
struct IsUnorderedContainer<absl::flat_hash_set<Key, Hash, KeyEqual, Allocator>>
: std::true_type {};
} // namespace container_algorithm_internal
ABSL_NAMESPACE_END
} // namespace absl
#endif // ABSL_CONTAINER_FLAT_HASH_SET_H_

View file

@ -0,0 +1,357 @@
// Copyright 2018 The Abseil Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "absl/container/flat_hash_set.h"
#include <cstddef>
#include <cstdint>
#include <memory>
#include <string>
#include <type_traits>
#include <utility>
#include <vector>
#include "gmock/gmock.h"
#include "gtest/gtest.h"
#include "absl/base/config.h"
#include "absl/container/hash_container_defaults.h"
#include "absl/container/internal/container_memory.h"
#include "absl/container/internal/hash_generator_testing.h"
#include "absl/container/internal/test_allocator.h"
#include "absl/container/internal/unordered_set_constructor_test.h"
#include "absl/container/internal/unordered_set_lookup_test.h"
#include "absl/container/internal/unordered_set_members_test.h"
#include "absl/container/internal/unordered_set_modifiers_test.h"
#include "absl/hash/hash.h"
#include "absl/log/check.h"
#include "absl/memory/memory.h"
#include "absl/strings/string_view.h"
namespace absl {
ABSL_NAMESPACE_BEGIN
namespace container_internal {
namespace {
using ::absl::container_internal::hash_internal::Enum;
using ::absl::container_internal::hash_internal::EnumClass;
using ::testing::IsEmpty;
using ::testing::Pointee;
using ::testing::UnorderedElementsAre;
using ::testing::UnorderedElementsAreArray;
// Check that absl::flat_hash_set works in a global constructor.
struct BeforeMain {
BeforeMain() {
absl::flat_hash_set<int> x;
x.insert(1);
CHECK(!x.contains(0)) << "x should not contain 0";
CHECK(x.contains(1)) << "x should contain 1";
}
};
const BeforeMain before_main;
template <class T>
using Set =
absl::flat_hash_set<T, StatefulTestingHash, StatefulTestingEqual, Alloc<T>>;
using SetTypes =
::testing::Types<Set<int>, Set<std::string>, Set<Enum>, Set<EnumClass>>;
INSTANTIATE_TYPED_TEST_SUITE_P(FlatHashSet, ConstructorTest, SetTypes);
INSTANTIATE_TYPED_TEST_SUITE_P(FlatHashSet, LookupTest, SetTypes);
INSTANTIATE_TYPED_TEST_SUITE_P(FlatHashSet, MembersTest, SetTypes);
INSTANTIATE_TYPED_TEST_SUITE_P(FlatHashSet, ModifiersTest, SetTypes);
TEST(FlatHashSet, EmplaceString) {
std::vector<std::string> v = {"a", "b"};
absl::flat_hash_set<absl::string_view> hs(v.begin(), v.end());
EXPECT_THAT(hs, UnorderedElementsAreArray(v));
}
TEST(FlatHashSet, BitfieldArgument) {
union {
int n : 1;
};
n = 0;
absl::flat_hash_set<int> s = {n};
s.insert(n);
s.insert(s.end(), n);
s.insert({n});
s.erase(n);
s.count(n);
s.prefetch(n);
s.find(n);
s.contains(n);
s.equal_range(n);
}
TEST(FlatHashSet, MergeExtractInsert) {
struct Hash {
size_t operator()(const std::unique_ptr<int>& p) const { return *p; }
};
struct Eq {
bool operator()(const std::unique_ptr<int>& a,
const std::unique_ptr<int>& b) const {
return *a == *b;
}
};
absl::flat_hash_set<std::unique_ptr<int>, Hash, Eq> set1, set2;
set1.insert(absl::make_unique<int>(7));
set1.insert(absl::make_unique<int>(17));
set2.insert(absl::make_unique<int>(7));
set2.insert(absl::make_unique<int>(19));
EXPECT_THAT(set1, UnorderedElementsAre(Pointee(7), Pointee(17)));
EXPECT_THAT(set2, UnorderedElementsAre(Pointee(7), Pointee(19)));
set1.merge(set2);
EXPECT_THAT(set1, UnorderedElementsAre(Pointee(7), Pointee(17), Pointee(19)));
EXPECT_THAT(set2, UnorderedElementsAre(Pointee(7)));
auto node = set1.extract(absl::make_unique<int>(7));
EXPECT_TRUE(node);
EXPECT_THAT(node.value(), Pointee(7));
EXPECT_THAT(set1, UnorderedElementsAre(Pointee(17), Pointee(19)));
auto insert_result = set2.insert(std::move(node));
EXPECT_FALSE(node);
EXPECT_FALSE(insert_result.inserted);
EXPECT_TRUE(insert_result.node);
EXPECT_THAT(insert_result.node.value(), Pointee(7));
EXPECT_EQ(**insert_result.position, 7);
EXPECT_NE(insert_result.position->get(), insert_result.node.value().get());
EXPECT_THAT(set2, UnorderedElementsAre(Pointee(7)));
node = set1.extract(absl::make_unique<int>(17));
EXPECT_TRUE(node);
EXPECT_THAT(node.value(), Pointee(17));
EXPECT_THAT(set1, UnorderedElementsAre(Pointee(19)));
node.value() = absl::make_unique<int>(23);
insert_result = set2.insert(std::move(node));
EXPECT_FALSE(node);
EXPECT_TRUE(insert_result.inserted);
EXPECT_FALSE(insert_result.node);
EXPECT_EQ(**insert_result.position, 23);
EXPECT_THAT(set2, UnorderedElementsAre(Pointee(7), Pointee(23)));
}
bool IsEven(int k) { return k % 2 == 0; }
TEST(FlatHashSet, EraseIf) {
// Erase all elements.
{
flat_hash_set<int> s = {1, 2, 3, 4, 5};
EXPECT_EQ(erase_if(s, [](int) { return true; }), 5);
EXPECT_THAT(s, IsEmpty());
}
// Erase no elements.
{
flat_hash_set<int> s = {1, 2, 3, 4, 5};
EXPECT_EQ(erase_if(s, [](int) { return false; }), 0);
EXPECT_THAT(s, UnorderedElementsAre(1, 2, 3, 4, 5));
}
// Erase specific elements.
{
flat_hash_set<int> s = {1, 2, 3, 4, 5};
EXPECT_EQ(erase_if(s, [](int k) { return k % 2 == 1; }), 3);
EXPECT_THAT(s, UnorderedElementsAre(2, 4));
}
// Predicate is function reference.
{
flat_hash_set<int> s = {1, 2, 3, 4, 5};
EXPECT_EQ(erase_if(s, IsEven), 2);
EXPECT_THAT(s, UnorderedElementsAre(1, 3, 5));
}
// Predicate is function pointer.
{
flat_hash_set<int> s = {1, 2, 3, 4, 5};
EXPECT_EQ(erase_if(s, &IsEven), 2);
EXPECT_THAT(s, UnorderedElementsAre(1, 3, 5));
}
}
TEST(FlatHashSet, CForEach) {
using ValueType = std::pair<int, int>;
flat_hash_set<ValueType> s;
std::vector<ValueType> expected;
for (int i = 0; i < 100; ++i) {
{
SCOPED_TRACE("mutable object iteration");
std::vector<ValueType> v;
absl::container_internal::c_for_each_fast(
s, [&v](const ValueType& p) { v.push_back(p); });
ASSERT_THAT(v, UnorderedElementsAreArray(expected));
}
{
SCOPED_TRACE("const object iteration");
std::vector<ValueType> v;
const flat_hash_set<ValueType>& cs = s;
absl::container_internal::c_for_each_fast(
cs, [&v](const ValueType& p) { v.push_back(p); });
ASSERT_THAT(v, UnorderedElementsAreArray(expected));
}
{
SCOPED_TRACE("temporary object iteration");
std::vector<ValueType> v;
absl::container_internal::c_for_each_fast(
flat_hash_set<ValueType>(s),
[&v](const ValueType& p) { v.push_back(p); });
ASSERT_THAT(v, UnorderedElementsAreArray(expected));
}
s.emplace(i, i);
expected.emplace_back(i, i);
}
}
class PoisonSoo {
int64_t data_;
public:
explicit PoisonSoo(int64_t d) : data_(d) { SanitizerPoisonObject(&data_); }
PoisonSoo(const PoisonSoo& that) : PoisonSoo(*that) {}
~PoisonSoo() { SanitizerUnpoisonObject(&data_); }
int64_t operator*() const {
SanitizerUnpoisonObject(&data_);
const int64_t ret = data_;
SanitizerPoisonObject(&data_);
return ret;
}
template <typename H>
friend H AbslHashValue(H h, const PoisonSoo& pi) {
return H::combine(std::move(h), *pi);
}
bool operator==(const PoisonSoo& rhs) const { return **this == *rhs; }
};
TEST(FlatHashSet, PoisonSooBasic) {
PoisonSoo a(0), b(1);
flat_hash_set<PoisonSoo> set;
set.insert(a);
EXPECT_THAT(set, UnorderedElementsAre(a));
set.insert(b);
EXPECT_THAT(set, UnorderedElementsAre(a, b));
set.erase(a);
EXPECT_THAT(set, UnorderedElementsAre(b));
set.rehash(0); // Shrink to SOO.
EXPECT_THAT(set, UnorderedElementsAre(b));
}
TEST(FlatHashSet, PoisonSooMoveConstructSooToSoo) {
PoisonSoo a(0);
flat_hash_set<PoisonSoo> set;
set.insert(a);
flat_hash_set<PoisonSoo> set2(std::move(set));
EXPECT_THAT(set2, UnorderedElementsAre(a));
}
TEST(FlatHashSet, PoisonSooAllocMoveConstructSooToSoo) {
PoisonSoo a(0);
flat_hash_set<PoisonSoo> set;
set.insert(a);
flat_hash_set<PoisonSoo> set2(std::move(set), std::allocator<PoisonSoo>());
EXPECT_THAT(set2, UnorderedElementsAre(a));
}
TEST(FlatHashSet, PoisonSooMoveAssignFullSooToEmptySoo) {
PoisonSoo a(0);
flat_hash_set<PoisonSoo> set, set2;
set.insert(a);
set2 = std::move(set);
EXPECT_THAT(set2, UnorderedElementsAre(a));
}
TEST(FlatHashSet, PoisonSooMoveAssignFullSooToFullSoo) {
PoisonSoo a(0), b(1);
flat_hash_set<PoisonSoo> set, set2;
set.insert(a);
set2.insert(b);
set2 = std::move(set);
EXPECT_THAT(set2, UnorderedElementsAre(a));
}
TEST(FlatHashSet, FlatHashSetPolicyDestroyReturnsTrue) {
EXPECT_TRUE((decltype(FlatHashSetPolicy<int>::destroy<std::allocator<int>>(
nullptr, nullptr))()));
EXPECT_FALSE(
(decltype(FlatHashSetPolicy<int>::destroy<CountingAllocator<int>>(
nullptr, nullptr))()));
EXPECT_FALSE((decltype(FlatHashSetPolicy<std::unique_ptr<int>>::destroy<
std::allocator<int>>(nullptr, nullptr))()));
}
struct HashEqInvalidOnMove {
HashEqInvalidOnMove() = default;
HashEqInvalidOnMove(const HashEqInvalidOnMove& rhs) = default;
HashEqInvalidOnMove(HashEqInvalidOnMove&& rhs) { rhs.moved = true; }
HashEqInvalidOnMove& operator=(const HashEqInvalidOnMove& rhs) = default;
HashEqInvalidOnMove& operator=(HashEqInvalidOnMove&& rhs) {
rhs.moved = true;
return *this;
}
size_t operator()(int x) const {
CHECK(!moved);
return absl::HashOf(x);
}
bool operator()(int x, int y) const {
CHECK(!moved);
return x == y;
}
bool moved = false;
};
TEST(FlatHashSet, MovedFromCleared_HashMustBeValid) {
flat_hash_set<int, HashEqInvalidOnMove> s1, s2;
// Moving the hashtable must not move the hasher because we need to support
// this behavior.
s2 = std::move(s1);
s1.clear();
s1.insert(2);
EXPECT_THAT(s1, UnorderedElementsAre(2));
}
TEST(FlatHashSet, MovedFromCleared_EqMustBeValid) {
flat_hash_set<int, DefaultHashContainerHash<int>, HashEqInvalidOnMove> s1, s2;
// Moving the hashtable must not move the equality functor because we need to
// support this behavior.
s2 = std::move(s1);
s1.clear();
s1.insert(2);
EXPECT_THAT(s1, UnorderedElementsAre(2));
}
TEST(FlatHashSet, Equality) {
{
flat_hash_set<int> s1 = {1, 2, 3};
flat_hash_set<int> s2 = {1, 2, 3};
EXPECT_EQ(s1, s2);
}
{
flat_hash_set<std::string> s1 = {"a", "b", "c"};
flat_hash_set<std::string> s2 = {"a", "b", "c"};
EXPECT_EQ(s1, s2);
}
}
} // namespace
} // namespace container_internal
ABSL_NAMESPACE_END
} // namespace absl

View file

@ -0,0 +1,45 @@
// Copyright 2024 The Abseil Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef ABSL_CONTAINER_HASH_CONTAINER_DEFAULTS_H_
#define ABSL_CONTAINER_HASH_CONTAINER_DEFAULTS_H_
#include "absl/base/config.h"
#include "absl/container/internal/hash_function_defaults.h"
namespace absl {
ABSL_NAMESPACE_BEGIN
// DefaultHashContainerHash is a convenience alias for the functor that is used
// by default by Abseil hash-based (unordered) containers for hashing when
// `Hash` type argument is not explicitly specified.
//
// This type alias can be used by generic code that wants to provide more
// flexibility for defining underlying containers.
template <typename T>
using DefaultHashContainerHash = absl::container_internal::hash_default_hash<T>;
// DefaultHashContainerEq is a convenience alias for the functor that is used by
// default by Abseil hash-based (unordered) containers for equality check when
// `Eq` type argument is not explicitly specified.
//
// This type alias can be used by generic code that wants to provide more
// flexibility for defining underlying containers.
template <typename T>
using DefaultHashContainerEq = absl::container_internal::hash_default_eq<T>;
ABSL_NAMESPACE_END
} // namespace absl
#endif // ABSL_CONTAINER_HASH_CONTAINER_DEFAULTS_H_

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,829 @@
// Copyright 2019 The Abseil Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include <array>
#include <string>
#include <vector>
#include "absl/base/internal/raw_logging.h"
#include "absl/base/macros.h"
#include "absl/container/inlined_vector.h"
#include "absl/strings/str_cat.h"
#include "benchmark/benchmark.h"
namespace {
void BM_InlinedVectorFill(benchmark::State& state) {
const int len = state.range(0);
absl::InlinedVector<int, 8> v;
v.reserve(len);
for (auto _ : state) {
v.resize(0); // Use resize(0) as InlinedVector releases storage on clear().
for (int i = 0; i < len; ++i) {
v.push_back(i);
}
benchmark::DoNotOptimize(v);
}
}
BENCHMARK(BM_InlinedVectorFill)->Range(1, 256);
void BM_InlinedVectorFillRange(benchmark::State& state) {
const int len = state.range(0);
const std::vector<int> src(len, len);
absl::InlinedVector<int, 8> v;
v.reserve(len);
for (auto _ : state) {
benchmark::DoNotOptimize(src);
v.assign(src.begin(), src.end());
benchmark::DoNotOptimize(v);
}
}
BENCHMARK(BM_InlinedVectorFillRange)->Range(1, 256);
void BM_StdVectorFill(benchmark::State& state) {
const int len = state.range(0);
std::vector<int> v;
v.reserve(len);
for (auto _ : state) {
v.clear();
for (int i = 0; i < len; ++i) {
v.push_back(i);
}
benchmark::DoNotOptimize(v);
}
}
BENCHMARK(BM_StdVectorFill)->Range(1, 256);
// The purpose of the next two benchmarks is to verify that
// absl::InlinedVector is efficient when moving is more efficient than
// copying. To do so, we use strings that are larger than the short
// string optimization.
bool StringRepresentedInline(std::string s) {
const char* chars = s.data();
std::string s1 = std::move(s);
return s1.data() != chars;
}
int GetNonShortStringOptimizationSize() {
for (int i = 24; i <= 192; i *= 2) {
if (!StringRepresentedInline(std::string(i, 'A'))) {
return i;
}
}
ABSL_RAW_LOG(
FATAL,
"Failed to find a string larger than the short string optimization");
return -1;
}
void BM_InlinedVectorFillString(benchmark::State& state) {
const int len = state.range(0);
const int no_sso = GetNonShortStringOptimizationSize();
std::string strings[4] = {std::string(no_sso, 'A'), std::string(no_sso, 'B'),
std::string(no_sso, 'C'), std::string(no_sso, 'D')};
for (auto _ : state) {
absl::InlinedVector<std::string, 8> v;
for (int i = 0; i < len; i++) {
v.push_back(strings[i & 3]);
}
}
state.SetItemsProcessed(static_cast<int64_t>(state.iterations()) * len);
}
BENCHMARK(BM_InlinedVectorFillString)->Range(0, 1024);
void BM_StdVectorFillString(benchmark::State& state) {
const int len = state.range(0);
const int no_sso = GetNonShortStringOptimizationSize();
std::string strings[4] = {std::string(no_sso, 'A'), std::string(no_sso, 'B'),
std::string(no_sso, 'C'), std::string(no_sso, 'D')};
for (auto _ : state) {
std::vector<std::string> v;
for (int i = 0; i < len; i++) {
v.push_back(strings[i & 3]);
}
}
state.SetItemsProcessed(static_cast<int64_t>(state.iterations()) * len);
}
BENCHMARK(BM_StdVectorFillString)->Range(0, 1024);
struct Buffer { // some arbitrary structure for benchmarking.
char* base;
int length;
int capacity;
void* user_data;
};
void BM_InlinedVectorAssignments(benchmark::State& state) {
const int len = state.range(0);
using BufferVec = absl::InlinedVector<Buffer, 2>;
BufferVec src;
src.resize(len);
BufferVec dst;
for (auto _ : state) {
benchmark::DoNotOptimize(dst);
benchmark::DoNotOptimize(src);
dst = src;
}
}
BENCHMARK(BM_InlinedVectorAssignments)
->Arg(0)
->Arg(1)
->Arg(2)
->Arg(3)
->Arg(4)
->Arg(20);
void BM_CreateFromContainer(benchmark::State& state) {
for (auto _ : state) {
absl::InlinedVector<int, 4> src{1, 2, 3};
benchmark::DoNotOptimize(src);
absl::InlinedVector<int, 4> dst(std::move(src));
benchmark::DoNotOptimize(dst);
}
}
BENCHMARK(BM_CreateFromContainer);
struct LargeCopyableOnly {
LargeCopyableOnly() : d(1024, 17) {}
LargeCopyableOnly(const LargeCopyableOnly& o) = default;
LargeCopyableOnly& operator=(const LargeCopyableOnly& o) = default;
std::vector<int> d;
};
struct LargeCopyableSwappable {
LargeCopyableSwappable() : d(1024, 17) {}
LargeCopyableSwappable(const LargeCopyableSwappable& o) = default;
LargeCopyableSwappable& operator=(LargeCopyableSwappable o) {
using std::swap;
swap(*this, o);
return *this;
}
friend void swap(LargeCopyableSwappable& a, LargeCopyableSwappable& b) {
using std::swap;
swap(a.d, b.d);
}
std::vector<int> d;
};
struct LargeCopyableMovable {
LargeCopyableMovable() : d(1024, 17) {}
// Use implicitly defined copy and move.
std::vector<int> d;
};
struct LargeCopyableMovableSwappable {
LargeCopyableMovableSwappable() : d(1024, 17) {}
LargeCopyableMovableSwappable(const LargeCopyableMovableSwappable& o) =
default;
LargeCopyableMovableSwappable(LargeCopyableMovableSwappable&& o) = default;
LargeCopyableMovableSwappable& operator=(LargeCopyableMovableSwappable o) {
using std::swap;
swap(*this, o);
return *this;
}
LargeCopyableMovableSwappable& operator=(LargeCopyableMovableSwappable&& o) =
default;
friend void swap(LargeCopyableMovableSwappable& a,
LargeCopyableMovableSwappable& b) {
using std::swap;
swap(a.d, b.d);
}
std::vector<int> d;
};
template <typename ElementType>
void BM_SwapElements(benchmark::State& state) {
const int len = state.range(0);
using Vec = absl::InlinedVector<ElementType, 32>;
Vec a(len);
Vec b;
for (auto _ : state) {
using std::swap;
benchmark::DoNotOptimize(a);
benchmark::DoNotOptimize(b);
swap(a, b);
}
}
BENCHMARK_TEMPLATE(BM_SwapElements, LargeCopyableOnly)->Range(0, 1024);
BENCHMARK_TEMPLATE(BM_SwapElements, LargeCopyableSwappable)->Range(0, 1024);
BENCHMARK_TEMPLATE(BM_SwapElements, LargeCopyableMovable)->Range(0, 1024);
BENCHMARK_TEMPLATE(BM_SwapElements, LargeCopyableMovableSwappable)
->Range(0, 1024);
// The following benchmark is meant to track the efficiency of the vector size
// as a function of stored type via the benchmark label. It is not meant to
// output useful sizeof operator performance. The loop is a dummy operation
// to fulfill the requirement of running the benchmark.
template <typename VecType>
void BM_Sizeof(benchmark::State& state) {
int size = 0;
for (auto _ : state) {
VecType vec;
size = sizeof(vec);
}
state.SetLabel(absl::StrCat("sz=", size));
}
BENCHMARK_TEMPLATE(BM_Sizeof, absl::InlinedVector<char, 1>);
BENCHMARK_TEMPLATE(BM_Sizeof, absl::InlinedVector<char, 4>);
BENCHMARK_TEMPLATE(BM_Sizeof, absl::InlinedVector<char, 7>);
BENCHMARK_TEMPLATE(BM_Sizeof, absl::InlinedVector<char, 8>);
BENCHMARK_TEMPLATE(BM_Sizeof, absl::InlinedVector<int, 1>);
BENCHMARK_TEMPLATE(BM_Sizeof, absl::InlinedVector<int, 4>);
BENCHMARK_TEMPLATE(BM_Sizeof, absl::InlinedVector<int, 7>);
BENCHMARK_TEMPLATE(BM_Sizeof, absl::InlinedVector<int, 8>);
BENCHMARK_TEMPLATE(BM_Sizeof, absl::InlinedVector<void*, 1>);
BENCHMARK_TEMPLATE(BM_Sizeof, absl::InlinedVector<void*, 4>);
BENCHMARK_TEMPLATE(BM_Sizeof, absl::InlinedVector<void*, 7>);
BENCHMARK_TEMPLATE(BM_Sizeof, absl::InlinedVector<void*, 8>);
BENCHMARK_TEMPLATE(BM_Sizeof, absl::InlinedVector<std::string, 1>);
BENCHMARK_TEMPLATE(BM_Sizeof, absl::InlinedVector<std::string, 4>);
BENCHMARK_TEMPLATE(BM_Sizeof, absl::InlinedVector<std::string, 7>);
BENCHMARK_TEMPLATE(BM_Sizeof, absl::InlinedVector<std::string, 8>);
void BM_InlinedVectorIndexInlined(benchmark::State& state) {
absl::InlinedVector<int, 8> v = {1, 2, 3, 4, 5, 6, 7};
for (auto _ : state) {
benchmark::DoNotOptimize(v);
benchmark::DoNotOptimize(v[4]);
}
}
BENCHMARK(BM_InlinedVectorIndexInlined);
void BM_InlinedVectorIndexExternal(benchmark::State& state) {
absl::InlinedVector<int, 8> v = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
for (auto _ : state) {
benchmark::DoNotOptimize(v);
benchmark::DoNotOptimize(v[4]);
}
}
BENCHMARK(BM_InlinedVectorIndexExternal);
void BM_StdVectorIndex(benchmark::State& state) {
std::vector<int> v = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
for (auto _ : state) {
benchmark::DoNotOptimize(v);
benchmark::DoNotOptimize(v[4]);
}
}
BENCHMARK(BM_StdVectorIndex);
void BM_InlinedVectorDataInlined(benchmark::State& state) {
absl::InlinedVector<int, 8> v = {1, 2, 3, 4, 5, 6, 7};
for (auto _ : state) {
benchmark::DoNotOptimize(v);
benchmark::DoNotOptimize(v.data());
}
}
BENCHMARK(BM_InlinedVectorDataInlined);
void BM_InlinedVectorDataExternal(benchmark::State& state) {
absl::InlinedVector<int, 8> v = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
for (auto _ : state) {
benchmark::DoNotOptimize(v);
benchmark::DoNotOptimize(v.data());
}
state.SetItemsProcessed(16 * static_cast<int64_t>(state.iterations()));
}
BENCHMARK(BM_InlinedVectorDataExternal);
void BM_StdVectorData(benchmark::State& state) {
std::vector<int> v = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
for (auto _ : state) {
benchmark::DoNotOptimize(v);
benchmark::DoNotOptimize(v.data());
}
state.SetItemsProcessed(16 * static_cast<int64_t>(state.iterations()));
}
BENCHMARK(BM_StdVectorData);
void BM_InlinedVectorSizeInlined(benchmark::State& state) {
absl::InlinedVector<int, 8> v = {1, 2, 3, 4, 5, 6, 7};
for (auto _ : state) {
benchmark::DoNotOptimize(v);
benchmark::DoNotOptimize(v.size());
}
}
BENCHMARK(BM_InlinedVectorSizeInlined);
void BM_InlinedVectorSizeExternal(benchmark::State& state) {
absl::InlinedVector<int, 8> v = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
for (auto _ : state) {
benchmark::DoNotOptimize(v);
benchmark::DoNotOptimize(v.size());
}
}
BENCHMARK(BM_InlinedVectorSizeExternal);
void BM_StdVectorSize(benchmark::State& state) {
std::vector<int> v = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
for (auto _ : state) {
benchmark::DoNotOptimize(v);
benchmark::DoNotOptimize(v.size());
}
}
BENCHMARK(BM_StdVectorSize);
void BM_InlinedVectorEmptyInlined(benchmark::State& state) {
absl::InlinedVector<int, 8> v = {1, 2, 3, 4, 5, 6, 7};
for (auto _ : state) {
benchmark::DoNotOptimize(v);
benchmark::DoNotOptimize(v.empty());
}
}
BENCHMARK(BM_InlinedVectorEmptyInlined);
void BM_InlinedVectorEmptyExternal(benchmark::State& state) {
absl::InlinedVector<int, 8> v = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
for (auto _ : state) {
benchmark::DoNotOptimize(v);
benchmark::DoNotOptimize(v.empty());
}
}
BENCHMARK(BM_InlinedVectorEmptyExternal);
void BM_StdVectorEmpty(benchmark::State& state) {
std::vector<int> v = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
for (auto _ : state) {
benchmark::DoNotOptimize(v);
benchmark::DoNotOptimize(v.empty());
}
}
BENCHMARK(BM_StdVectorEmpty);
constexpr size_t kInlinedCapacity = 4;
constexpr size_t kLargeSize = kInlinedCapacity * 2;
constexpr size_t kSmallSize = kInlinedCapacity / 2;
constexpr size_t kBatchSize = 100;
#define ABSL_INTERNAL_BENCHMARK_ONE_SIZE(BM_FunctionTemplate, T) \
BENCHMARK_TEMPLATE(BM_FunctionTemplate, T, kLargeSize); \
BENCHMARK_TEMPLATE(BM_FunctionTemplate, T, kSmallSize)
#define ABSL_INTERNAL_BENCHMARK_TWO_SIZE(BM_FunctionTemplate, T) \
BENCHMARK_TEMPLATE(BM_FunctionTemplate, T, kLargeSize, kLargeSize); \
BENCHMARK_TEMPLATE(BM_FunctionTemplate, T, kLargeSize, kSmallSize); \
BENCHMARK_TEMPLATE(BM_FunctionTemplate, T, kSmallSize, kLargeSize); \
BENCHMARK_TEMPLATE(BM_FunctionTemplate, T, kSmallSize, kSmallSize)
template <typename T>
using InlVec = absl::InlinedVector<T, kInlinedCapacity>;
struct TrivialType {
size_t val;
};
class NontrivialType {
public:
ABSL_ATTRIBUTE_NOINLINE NontrivialType() : val_() {
benchmark::DoNotOptimize(*this);
}
ABSL_ATTRIBUTE_NOINLINE NontrivialType(const NontrivialType& other)
: val_(other.val_) {
benchmark::DoNotOptimize(*this);
}
ABSL_ATTRIBUTE_NOINLINE NontrivialType& operator=(
const NontrivialType& other) {
val_ = other.val_;
benchmark::DoNotOptimize(*this);
return *this;
}
ABSL_ATTRIBUTE_NOINLINE ~NontrivialType() noexcept {
benchmark::DoNotOptimize(*this);
}
private:
size_t val_;
};
template <typename T, typename PrepareVecFn, typename TestVecFn>
void BatchedBenchmark(benchmark::State& state, PrepareVecFn prepare_vec,
TestVecFn test_vec) {
std::array<InlVec<T>, kBatchSize> vector_batch{};
while (state.KeepRunningBatch(kBatchSize)) {
// Prepare batch
state.PauseTiming();
for (size_t i = 0; i < kBatchSize; ++i) {
prepare_vec(vector_batch.data() + i, i);
}
benchmark::DoNotOptimize(vector_batch);
state.ResumeTiming();
// Test batch
for (size_t i = 0; i < kBatchSize; ++i) {
test_vec(vector_batch.data() + i, i);
}
}
}
template <typename T, size_t ToSize>
void BM_ConstructFromSize(benchmark::State& state) {
using VecT = InlVec<T>;
auto size = ToSize;
BatchedBenchmark<T>(
state,
/* prepare_vec = */ [](InlVec<T>* vec, size_t) { vec->~VecT(); },
/* test_vec = */
[&](void* ptr, size_t) {
benchmark::DoNotOptimize(size);
::new (ptr) VecT(size);
});
}
ABSL_INTERNAL_BENCHMARK_ONE_SIZE(BM_ConstructFromSize, TrivialType);
ABSL_INTERNAL_BENCHMARK_ONE_SIZE(BM_ConstructFromSize, NontrivialType);
template <typename T, size_t ToSize>
void BM_ConstructFromSizeRef(benchmark::State& state) {
using VecT = InlVec<T>;
auto size = ToSize;
auto ref = T();
BatchedBenchmark<T>(
state,
/* prepare_vec = */ [](InlVec<T>* vec, size_t) { vec->~VecT(); },
/* test_vec = */
[&](void* ptr, size_t) {
benchmark::DoNotOptimize(size);
benchmark::DoNotOptimize(ref);
::new (ptr) VecT(size, ref);
});
}
ABSL_INTERNAL_BENCHMARK_ONE_SIZE(BM_ConstructFromSizeRef, TrivialType);
ABSL_INTERNAL_BENCHMARK_ONE_SIZE(BM_ConstructFromSizeRef, NontrivialType);
template <typename T, size_t ToSize>
void BM_ConstructFromRange(benchmark::State& state) {
using VecT = InlVec<T>;
std::array<T, ToSize> arr{};
BatchedBenchmark<T>(
state,
/* prepare_vec = */ [](InlVec<T>* vec, size_t) { vec->~VecT(); },
/* test_vec = */
[&](void* ptr, size_t) {
benchmark::DoNotOptimize(arr);
::new (ptr) VecT(arr.begin(), arr.end());
});
}
ABSL_INTERNAL_BENCHMARK_ONE_SIZE(BM_ConstructFromRange, TrivialType);
ABSL_INTERNAL_BENCHMARK_ONE_SIZE(BM_ConstructFromRange, NontrivialType);
template <typename T, size_t ToSize>
void BM_ConstructFromCopy(benchmark::State& state) {
using VecT = InlVec<T>;
VecT other_vec(ToSize);
BatchedBenchmark<T>(
state,
/* prepare_vec = */
[](InlVec<T>* vec, size_t) { vec->~VecT(); },
/* test_vec = */
[&](void* ptr, size_t) {
benchmark::DoNotOptimize(other_vec);
::new (ptr) VecT(other_vec);
});
}
ABSL_INTERNAL_BENCHMARK_ONE_SIZE(BM_ConstructFromCopy, TrivialType);
ABSL_INTERNAL_BENCHMARK_ONE_SIZE(BM_ConstructFromCopy, NontrivialType);
template <typename T, size_t ToSize>
void BM_ConstructFromMove(benchmark::State& state) {
using VecT = InlVec<T>;
std::array<VecT, kBatchSize> vector_batch{};
BatchedBenchmark<T>(
state,
/* prepare_vec = */
[&](InlVec<T>* vec, size_t i) {
vector_batch[i].clear();
vector_batch[i].resize(ToSize);
vec->~VecT();
},
/* test_vec = */
[&](void* ptr, size_t i) {
benchmark::DoNotOptimize(vector_batch[i]);
::new (ptr) VecT(std::move(vector_batch[i]));
});
}
ABSL_INTERNAL_BENCHMARK_ONE_SIZE(BM_ConstructFromMove, TrivialType);
ABSL_INTERNAL_BENCHMARK_ONE_SIZE(BM_ConstructFromMove, NontrivialType);
// Measure cost of copy-constructor+destructor.
void BM_CopyTrivial(benchmark::State& state) {
const int n = state.range(0);
InlVec<int64_t> src(n);
for (auto s : state) {
InlVec<int64_t> copy(src);
benchmark::DoNotOptimize(copy);
}
}
BENCHMARK(BM_CopyTrivial)->Arg(0)->Arg(1)->Arg(kLargeSize);
// Measure cost of copy-constructor+destructor.
void BM_CopyNonTrivial(benchmark::State& state) {
const int n = state.range(0);
InlVec<InlVec<int64_t>> src(n);
for (auto s : state) {
InlVec<InlVec<int64_t>> copy(src);
benchmark::DoNotOptimize(copy);
}
}
BENCHMARK(BM_CopyNonTrivial)->Arg(0)->Arg(1)->Arg(kLargeSize);
template <typename T, size_t FromSize, size_t ToSize>
void BM_AssignSizeRef(benchmark::State& state) {
auto size = ToSize;
auto ref = T();
BatchedBenchmark<T>(
state,
/* prepare_vec = */ [](InlVec<T>* vec, size_t) { vec->resize(FromSize); },
/* test_vec = */
[&](InlVec<T>* vec, size_t) {
benchmark::DoNotOptimize(size);
benchmark::DoNotOptimize(ref);
vec->assign(size, ref);
});
}
ABSL_INTERNAL_BENCHMARK_TWO_SIZE(BM_AssignSizeRef, TrivialType);
ABSL_INTERNAL_BENCHMARK_TWO_SIZE(BM_AssignSizeRef, NontrivialType);
template <typename T, size_t FromSize, size_t ToSize>
void BM_AssignRange(benchmark::State& state) {
std::array<T, ToSize> arr{};
BatchedBenchmark<T>(
state,
/* prepare_vec = */ [](InlVec<T>* vec, size_t) { vec->resize(FromSize); },
/* test_vec = */
[&](InlVec<T>* vec, size_t) {
benchmark::DoNotOptimize(arr);
vec->assign(arr.begin(), arr.end());
});
}
ABSL_INTERNAL_BENCHMARK_TWO_SIZE(BM_AssignRange, TrivialType);
ABSL_INTERNAL_BENCHMARK_TWO_SIZE(BM_AssignRange, NontrivialType);
template <typename T, size_t FromSize, size_t ToSize>
void BM_AssignFromCopy(benchmark::State& state) {
InlVec<T> other_vec(ToSize);
BatchedBenchmark<T>(
state,
/* prepare_vec = */ [](InlVec<T>* vec, size_t) { vec->resize(FromSize); },
/* test_vec = */
[&](InlVec<T>* vec, size_t) {
benchmark::DoNotOptimize(other_vec);
*vec = other_vec;
});
}
ABSL_INTERNAL_BENCHMARK_TWO_SIZE(BM_AssignFromCopy, TrivialType);
ABSL_INTERNAL_BENCHMARK_TWO_SIZE(BM_AssignFromCopy, NontrivialType);
template <typename T, size_t FromSize, size_t ToSize>
void BM_AssignFromMove(benchmark::State& state) {
using VecT = InlVec<T>;
std::array<VecT, kBatchSize> vector_batch{};
BatchedBenchmark<T>(
state,
/* prepare_vec = */
[&](InlVec<T>* vec, size_t i) {
vector_batch[i].clear();
vector_batch[i].resize(ToSize);
vec->resize(FromSize);
},
/* test_vec = */
[&](InlVec<T>* vec, size_t i) {
benchmark::DoNotOptimize(vector_batch[i]);
*vec = std::move(vector_batch[i]);
});
}
ABSL_INTERNAL_BENCHMARK_TWO_SIZE(BM_AssignFromMove, TrivialType);
ABSL_INTERNAL_BENCHMARK_TWO_SIZE(BM_AssignFromMove, NontrivialType);
template <typename T, size_t FromSize, size_t ToSize>
void BM_ResizeSize(benchmark::State& state) {
BatchedBenchmark<T>(
state,
/* prepare_vec = */
[](InlVec<T>* vec, size_t) {
vec->clear();
vec->resize(FromSize);
},
/* test_vec = */
[](InlVec<T>* vec, size_t) { vec->resize(ToSize); });
}
ABSL_INTERNAL_BENCHMARK_TWO_SIZE(BM_ResizeSize, TrivialType);
ABSL_INTERNAL_BENCHMARK_TWO_SIZE(BM_ResizeSize, NontrivialType);
template <typename T, size_t FromSize, size_t ToSize>
void BM_ResizeSizeRef(benchmark::State& state) {
auto t = T();
BatchedBenchmark<T>(
state,
/* prepare_vec = */
[](InlVec<T>* vec, size_t) {
vec->clear();
vec->resize(FromSize);
},
/* test_vec = */
[&](InlVec<T>* vec, size_t) {
benchmark::DoNotOptimize(t);
vec->resize(ToSize, t);
});
}
ABSL_INTERNAL_BENCHMARK_TWO_SIZE(BM_ResizeSizeRef, TrivialType);
ABSL_INTERNAL_BENCHMARK_TWO_SIZE(BM_ResizeSizeRef, NontrivialType);
template <typename T, size_t FromSize, size_t ToSize>
void BM_InsertSizeRef(benchmark::State& state) {
auto t = T();
BatchedBenchmark<T>(
state,
/* prepare_vec = */
[](InlVec<T>* vec, size_t) {
vec->clear();
vec->resize(FromSize);
},
/* test_vec = */
[&](InlVec<T>* vec, size_t) {
benchmark::DoNotOptimize(t);
auto* pos = vec->data() + (vec->size() / 2);
vec->insert(pos, t);
});
}
ABSL_INTERNAL_BENCHMARK_TWO_SIZE(BM_InsertSizeRef, TrivialType);
ABSL_INTERNAL_BENCHMARK_TWO_SIZE(BM_InsertSizeRef, NontrivialType);
template <typename T, size_t FromSize, size_t ToSize>
void BM_InsertRange(benchmark::State& state) {
InlVec<T> other_vec(ToSize);
BatchedBenchmark<T>(
state,
/* prepare_vec = */
[](InlVec<T>* vec, size_t) {
vec->clear();
vec->resize(FromSize);
},
/* test_vec = */
[&](InlVec<T>* vec, size_t) {
benchmark::DoNotOptimize(other_vec);
auto* pos = vec->data() + (vec->size() / 2);
vec->insert(pos, other_vec.begin(), other_vec.end());
});
}
ABSL_INTERNAL_BENCHMARK_TWO_SIZE(BM_InsertRange, TrivialType);
ABSL_INTERNAL_BENCHMARK_TWO_SIZE(BM_InsertRange, NontrivialType);
template <typename T, size_t FromSize>
void BM_EmplaceBack(benchmark::State& state) {
BatchedBenchmark<T>(
state,
/* prepare_vec = */
[](InlVec<T>* vec, size_t) {
vec->clear();
vec->resize(FromSize);
},
/* test_vec = */
[](InlVec<T>* vec, size_t) { vec->emplace_back(); });
}
ABSL_INTERNAL_BENCHMARK_ONE_SIZE(BM_EmplaceBack, TrivialType);
ABSL_INTERNAL_BENCHMARK_ONE_SIZE(BM_EmplaceBack, NontrivialType);
template <typename T, size_t FromSize>
void BM_PopBack(benchmark::State& state) {
BatchedBenchmark<T>(
state,
/* prepare_vec = */
[](InlVec<T>* vec, size_t) {
vec->clear();
vec->resize(FromSize);
},
/* test_vec = */
[](InlVec<T>* vec, size_t) { vec->pop_back(); });
}
ABSL_INTERNAL_BENCHMARK_ONE_SIZE(BM_PopBack, TrivialType);
ABSL_INTERNAL_BENCHMARK_ONE_SIZE(BM_PopBack, NontrivialType);
template <typename T, size_t FromSize>
void BM_EraseOne(benchmark::State& state) {
BatchedBenchmark<T>(
state,
/* prepare_vec = */
[](InlVec<T>* vec, size_t) {
vec->clear();
vec->resize(FromSize);
},
/* test_vec = */
[](InlVec<T>* vec, size_t) {
auto* pos = vec->data() + (vec->size() / 2);
vec->erase(pos);
});
}
ABSL_INTERNAL_BENCHMARK_ONE_SIZE(BM_EraseOne, TrivialType);
ABSL_INTERNAL_BENCHMARK_ONE_SIZE(BM_EraseOne, NontrivialType);
template <typename T, size_t FromSize>
void BM_EraseRange(benchmark::State& state) {
BatchedBenchmark<T>(
state,
/* prepare_vec = */
[](InlVec<T>* vec, size_t) {
vec->clear();
vec->resize(FromSize);
},
/* test_vec = */
[](InlVec<T>* vec, size_t) {
auto* pos = vec->data() + (vec->size() / 2);
vec->erase(pos, pos + 1);
});
}
ABSL_INTERNAL_BENCHMARK_ONE_SIZE(BM_EraseRange, TrivialType);
ABSL_INTERNAL_BENCHMARK_ONE_SIZE(BM_EraseRange, NontrivialType);
template <typename T, size_t FromSize>
void BM_Clear(benchmark::State& state) {
BatchedBenchmark<T>(
state,
/* prepare_vec = */ [](InlVec<T>* vec, size_t) { vec->resize(FromSize); },
/* test_vec = */ [](InlVec<T>* vec, size_t) { vec->clear(); });
}
ABSL_INTERNAL_BENCHMARK_ONE_SIZE(BM_Clear, TrivialType);
ABSL_INTERNAL_BENCHMARK_ONE_SIZE(BM_Clear, NontrivialType);
template <typename T, size_t FromSize, size_t ToCapacity>
void BM_Reserve(benchmark::State& state) {
BatchedBenchmark<T>(
state,
/* prepare_vec = */
[](InlVec<T>* vec, size_t) {
vec->clear();
vec->resize(FromSize);
},
/* test_vec = */
[](InlVec<T>* vec, size_t) { vec->reserve(ToCapacity); });
}
ABSL_INTERNAL_BENCHMARK_TWO_SIZE(BM_Reserve, TrivialType);
ABSL_INTERNAL_BENCHMARK_TWO_SIZE(BM_Reserve, NontrivialType);
template <typename T, size_t FromCapacity, size_t ToCapacity>
void BM_ShrinkToFit(benchmark::State& state) {
BatchedBenchmark<T>(
state,
/* prepare_vec = */
[](InlVec<T>* vec, size_t) {
vec->clear();
vec->resize(ToCapacity);
vec->reserve(FromCapacity);
},
/* test_vec = */ [](InlVec<T>* vec, size_t) { vec->shrink_to_fit(); });
}
ABSL_INTERNAL_BENCHMARK_TWO_SIZE(BM_ShrinkToFit, TrivialType);
ABSL_INTERNAL_BENCHMARK_TWO_SIZE(BM_ShrinkToFit, NontrivialType);
template <typename T, size_t FromSize, size_t ToSize>
void BM_Swap(benchmark::State& state) {
using VecT = InlVec<T>;
std::array<VecT, kBatchSize> vector_batch{};
BatchedBenchmark<T>(
state,
/* prepare_vec = */
[&](InlVec<T>* vec, size_t i) {
vector_batch[i].clear();
vector_batch[i].resize(ToSize);
vec->resize(FromSize);
},
/* test_vec = */
[&](InlVec<T>* vec, size_t i) {
using std::swap;
benchmark::DoNotOptimize(vector_batch[i]);
swap(*vec, vector_batch[i]);
});
}
ABSL_INTERNAL_BENCHMARK_TWO_SIZE(BM_Swap, TrivialType);
ABSL_INTERNAL_BENCHMARK_TWO_SIZE(BM_Swap, NontrivialType);
} // namespace

View file

@ -0,0 +1,508 @@
// Copyright 2019 The Abseil Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "absl/container/inlined_vector.h"
#include "absl/base/config.h"
#if defined(ABSL_HAVE_EXCEPTIONS)
#include <array>
#include <initializer_list>
#include <iterator>
#include <memory>
#include <utility>
#include "gtest/gtest.h"
#include "absl/base/internal/exception_safety_testing.h"
namespace {
constexpr size_t kInlinedCapacity = 4;
constexpr size_t kLargeSize = kInlinedCapacity * 2;
constexpr size_t kSmallSize = kInlinedCapacity / 2;
using Thrower = testing::ThrowingValue<>;
using MovableThrower = testing::ThrowingValue<testing::TypeSpec::kNoThrowMove>;
using ThrowAlloc = testing::ThrowingAllocator<Thrower>;
using ThrowerVec = absl::InlinedVector<Thrower, kInlinedCapacity>;
using MovableThrowerVec = absl::InlinedVector<MovableThrower, kInlinedCapacity>;
using ThrowAllocThrowerVec =
absl::InlinedVector<Thrower, kInlinedCapacity, ThrowAlloc>;
using ThrowAllocMovableThrowerVec =
absl::InlinedVector<MovableThrower, kInlinedCapacity, ThrowAlloc>;
// In GCC, if an element of a `std::initializer_list` throws during construction
// the elements that were constructed before it are not destroyed. This causes
// incorrect exception safety test failures. Thus, `testing::nothrow_ctor` is
// required. See: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=66139
#define ABSL_INTERNAL_MAKE_INIT_LIST(T, N) \
(N > kInlinedCapacity \
? std::initializer_list<T>{T(0, testing::nothrow_ctor), \
T(1, testing::nothrow_ctor), \
T(2, testing::nothrow_ctor), \
T(3, testing::nothrow_ctor), \
T(4, testing::nothrow_ctor), \
T(5, testing::nothrow_ctor), \
T(6, testing::nothrow_ctor), \
T(7, testing::nothrow_ctor)} \
\
: std::initializer_list<T>{T(0, testing::nothrow_ctor), \
T(1, testing::nothrow_ctor)})
static_assert(kLargeSize == 8, "Must update ABSL_INTERNAL_MAKE_INIT_LIST(...)");
static_assert(kSmallSize == 2, "Must update ABSL_INTERNAL_MAKE_INIT_LIST(...)");
template <typename TheVecT, size_t... TheSizes>
class TestParams {
public:
using VecT = TheVecT;
constexpr static size_t GetSizeAt(size_t i) { return kSizes[1 + i]; }
private:
constexpr static size_t kSizes[1 + sizeof...(TheSizes)] = {1, TheSizes...};
};
using NoSizeTestParams =
::testing::Types<TestParams<ThrowerVec>, TestParams<MovableThrowerVec>,
TestParams<ThrowAllocThrowerVec>,
TestParams<ThrowAllocMovableThrowerVec>>;
using OneSizeTestParams =
::testing::Types<TestParams<ThrowerVec, kLargeSize>,
TestParams<ThrowerVec, kSmallSize>,
TestParams<MovableThrowerVec, kLargeSize>,
TestParams<MovableThrowerVec, kSmallSize>,
TestParams<ThrowAllocThrowerVec, kLargeSize>,
TestParams<ThrowAllocThrowerVec, kSmallSize>,
TestParams<ThrowAllocMovableThrowerVec, kLargeSize>,
TestParams<ThrowAllocMovableThrowerVec, kSmallSize>>;
using TwoSizeTestParams = ::testing::Types<
TestParams<ThrowerVec, kLargeSize, kLargeSize>,
TestParams<ThrowerVec, kLargeSize, kSmallSize>,
TestParams<ThrowerVec, kSmallSize, kLargeSize>,
TestParams<ThrowerVec, kSmallSize, kSmallSize>,
TestParams<MovableThrowerVec, kLargeSize, kLargeSize>,
TestParams<MovableThrowerVec, kLargeSize, kSmallSize>,
TestParams<MovableThrowerVec, kSmallSize, kLargeSize>,
TestParams<MovableThrowerVec, kSmallSize, kSmallSize>,
TestParams<ThrowAllocThrowerVec, kLargeSize, kLargeSize>,
TestParams<ThrowAllocThrowerVec, kLargeSize, kSmallSize>,
TestParams<ThrowAllocThrowerVec, kSmallSize, kLargeSize>,
TestParams<ThrowAllocThrowerVec, kSmallSize, kSmallSize>,
TestParams<ThrowAllocMovableThrowerVec, kLargeSize, kLargeSize>,
TestParams<ThrowAllocMovableThrowerVec, kLargeSize, kSmallSize>,
TestParams<ThrowAllocMovableThrowerVec, kSmallSize, kLargeSize>,
TestParams<ThrowAllocMovableThrowerVec, kSmallSize, kSmallSize>>;
template <typename>
struct NoSizeTest : ::testing::Test {};
TYPED_TEST_SUITE(NoSizeTest, NoSizeTestParams);
template <typename>
struct OneSizeTest : ::testing::Test {};
TYPED_TEST_SUITE(OneSizeTest, OneSizeTestParams);
template <typename>
struct TwoSizeTest : ::testing::Test {};
TYPED_TEST_SUITE(TwoSizeTest, TwoSizeTestParams);
template <typename VecT>
bool InlinedVectorInvariants(VecT* vec) {
if (*vec != *vec) return false;
if (vec->size() > vec->capacity()) return false;
if (vec->size() > vec->max_size()) return false;
if (vec->capacity() > vec->max_size()) return false;
if (vec->data() != std::addressof(vec->at(0))) return false;
if (vec->data() != vec->begin()) return false;
if (*vec->data() != *vec->begin()) return false;
if (vec->begin() > vec->end()) return false;
if ((vec->end() - vec->begin()) != vec->size()) return false;
if (std::distance(vec->begin(), vec->end()) != vec->size()) return false;
return true;
}
// Function that always returns false is correct, but refactoring is required
// for clarity. It's needed to express that, as a contract, certain operations
// should not throw at all. Execution of this function means an exception was
// thrown and thus the test should fail.
// TODO(johnsoncj): Add `testing::NoThrowGuarantee` to the framework
template <typename VecT>
bool NoThrowGuarantee(VecT* /* vec */) {
return false;
}
TYPED_TEST(NoSizeTest, DefaultConstructor) {
using VecT = typename TypeParam::VecT;
using allocator_type = typename VecT::allocator_type;
testing::TestThrowingCtor<VecT>();
testing::TestThrowingCtor<VecT>(allocator_type{});
}
TYPED_TEST(OneSizeTest, SizeConstructor) {
using VecT = typename TypeParam::VecT;
using allocator_type = typename VecT::allocator_type;
constexpr static auto size = TypeParam::GetSizeAt(0);
testing::TestThrowingCtor<VecT>(size);
testing::TestThrowingCtor<VecT>(size, allocator_type{});
}
TYPED_TEST(OneSizeTest, SizeRefConstructor) {
using VecT = typename TypeParam::VecT;
using value_type = typename VecT::value_type;
using allocator_type = typename VecT::allocator_type;
constexpr static auto size = TypeParam::GetSizeAt(0);
testing::TestThrowingCtor<VecT>(size, value_type{});
testing::TestThrowingCtor<VecT>(size, value_type{}, allocator_type{});
}
TYPED_TEST(OneSizeTest, InitializerListConstructor) {
using VecT = typename TypeParam::VecT;
using value_type = typename VecT::value_type;
using allocator_type = typename VecT::allocator_type;
constexpr static auto size = TypeParam::GetSizeAt(0);
testing::TestThrowingCtor<VecT>(
ABSL_INTERNAL_MAKE_INIT_LIST(value_type, size));
testing::TestThrowingCtor<VecT>(
ABSL_INTERNAL_MAKE_INIT_LIST(value_type, size), allocator_type{});
}
TYPED_TEST(OneSizeTest, RangeConstructor) {
using VecT = typename TypeParam::VecT;
using value_type = typename VecT::value_type;
using allocator_type = typename VecT::allocator_type;
constexpr static auto size = TypeParam::GetSizeAt(0);
std::array<value_type, size> arr{};
testing::TestThrowingCtor<VecT>(arr.begin(), arr.end());
testing::TestThrowingCtor<VecT>(arr.begin(), arr.end(), allocator_type{});
}
TYPED_TEST(OneSizeTest, CopyConstructor) {
using VecT = typename TypeParam::VecT;
using allocator_type = typename VecT::allocator_type;
constexpr static auto size = TypeParam::GetSizeAt(0);
VecT other_vec{size};
testing::TestThrowingCtor<VecT>(other_vec);
testing::TestThrowingCtor<VecT>(other_vec, allocator_type{});
}
TYPED_TEST(OneSizeTest, MoveConstructor) {
using VecT = typename TypeParam::VecT;
using allocator_type = typename VecT::allocator_type;
constexpr static auto size = TypeParam::GetSizeAt(0);
if (!absl::allocator_is_nothrow<allocator_type>::value) {
testing::TestThrowingCtor<VecT>(VecT{size});
testing::TestThrowingCtor<VecT>(VecT{size}, allocator_type{});
}
}
TYPED_TEST(TwoSizeTest, Assign) {
using VecT = typename TypeParam::VecT;
using value_type = typename VecT::value_type;
constexpr static auto from_size = TypeParam::GetSizeAt(0);
constexpr static auto to_size = TypeParam::GetSizeAt(1);
auto tester = testing::MakeExceptionSafetyTester()
.WithInitialValue(VecT{from_size})
.WithContracts(InlinedVectorInvariants<VecT>);
EXPECT_TRUE(tester.Test([](VecT* vec) {
*vec = ABSL_INTERNAL_MAKE_INIT_LIST(value_type, to_size);
}));
EXPECT_TRUE(tester.Test([](VecT* vec) {
VecT other_vec{to_size};
*vec = other_vec;
}));
EXPECT_TRUE(tester.Test([](VecT* vec) {
VecT other_vec{to_size};
*vec = std::move(other_vec);
}));
EXPECT_TRUE(tester.Test([](VecT* vec) {
value_type val{};
vec->assign(to_size, val);
}));
EXPECT_TRUE(tester.Test([](VecT* vec) {
vec->assign(ABSL_INTERNAL_MAKE_INIT_LIST(value_type, to_size));
}));
EXPECT_TRUE(tester.Test([](VecT* vec) {
std::array<value_type, to_size> arr{};
vec->assign(arr.begin(), arr.end());
}));
}
TYPED_TEST(TwoSizeTest, Resize) {
using VecT = typename TypeParam::VecT;
using value_type = typename VecT::value_type;
constexpr static auto from_size = TypeParam::GetSizeAt(0);
constexpr static auto to_size = TypeParam::GetSizeAt(1);
auto tester = testing::MakeExceptionSafetyTester()
.WithInitialValue(VecT{from_size})
.WithContracts(InlinedVectorInvariants<VecT>,
testing::strong_guarantee);
EXPECT_TRUE(tester.Test([](VecT* vec) {
vec->resize(to_size); //
}));
EXPECT_TRUE(tester.Test([](VecT* vec) {
vec->resize(to_size, value_type{}); //
}));
}
TYPED_TEST(OneSizeTest, Insert) {
using VecT = typename TypeParam::VecT;
using value_type = typename VecT::value_type;
constexpr static auto from_size = TypeParam::GetSizeAt(0);
auto tester = testing::MakeExceptionSafetyTester()
.WithInitialValue(VecT{from_size})
.WithContracts(InlinedVectorInvariants<VecT>);
EXPECT_TRUE(tester.Test([](VecT* vec) {
auto it = vec->begin();
vec->insert(it, value_type{});
}));
EXPECT_TRUE(tester.Test([](VecT* vec) {
auto it = vec->begin() + (vec->size() / 2);
vec->insert(it, value_type{});
}));
EXPECT_TRUE(tester.Test([](VecT* vec) {
auto it = vec->end();
vec->insert(it, value_type{});
}));
}
TYPED_TEST(TwoSizeTest, Insert) {
using VecT = typename TypeParam::VecT;
using value_type = typename VecT::value_type;
constexpr static auto from_size = TypeParam::GetSizeAt(0);
constexpr static auto count = TypeParam::GetSizeAt(1);
auto tester = testing::MakeExceptionSafetyTester()
.WithInitialValue(VecT{from_size})
.WithContracts(InlinedVectorInvariants<VecT>);
EXPECT_TRUE(tester.Test([](VecT* vec) {
auto it = vec->begin();
vec->insert(it, count, value_type{});
}));
EXPECT_TRUE(tester.Test([](VecT* vec) {
auto it = vec->begin() + (vec->size() / 2);
vec->insert(it, count, value_type{});
}));
EXPECT_TRUE(tester.Test([](VecT* vec) {
auto it = vec->end();
vec->insert(it, count, value_type{});
}));
EXPECT_TRUE(tester.Test([](VecT* vec) {
auto it = vec->begin();
vec->insert(it, ABSL_INTERNAL_MAKE_INIT_LIST(value_type, count));
}));
EXPECT_TRUE(tester.Test([](VecT* vec) {
auto it = vec->begin() + (vec->size() / 2);
vec->insert(it, ABSL_INTERNAL_MAKE_INIT_LIST(value_type, count));
}));
EXPECT_TRUE(tester.Test([](VecT* vec) {
auto it = vec->end();
vec->insert(it, ABSL_INTERNAL_MAKE_INIT_LIST(value_type, count));
}));
EXPECT_TRUE(tester.Test([](VecT* vec) {
auto it = vec->begin();
std::array<value_type, count> arr{};
vec->insert(it, arr.begin(), arr.end());
}));
EXPECT_TRUE(tester.Test([](VecT* vec) {
auto it = vec->begin() + (vec->size() / 2);
std::array<value_type, count> arr{};
vec->insert(it, arr.begin(), arr.end());
}));
EXPECT_TRUE(tester.Test([](VecT* vec) {
auto it = vec->end();
std::array<value_type, count> arr{};
vec->insert(it, arr.begin(), arr.end());
}));
}
TYPED_TEST(OneSizeTest, EmplaceBack) {
using VecT = typename TypeParam::VecT;
constexpr static auto size = TypeParam::GetSizeAt(0);
// For testing calls to `emplace_back(...)` that reallocate.
VecT full_vec{size};
full_vec.resize(full_vec.capacity());
// For testing calls to `emplace_back(...)` that don't reallocate.
VecT nonfull_vec{size};
nonfull_vec.reserve(size + 1);
auto tester = testing::MakeExceptionSafetyTester().WithContracts(
InlinedVectorInvariants<VecT>);
EXPECT_TRUE(tester.WithInitialValue(nonfull_vec).Test([](VecT* vec) {
vec->emplace_back();
}));
EXPECT_TRUE(tester.WithInitialValue(full_vec).Test(
[](VecT* vec) { vec->emplace_back(); }));
}
TYPED_TEST(OneSizeTest, PopBack) {
using VecT = typename TypeParam::VecT;
constexpr static auto size = TypeParam::GetSizeAt(0);
auto tester = testing::MakeExceptionSafetyTester()
.WithInitialValue(VecT{size})
.WithContracts(NoThrowGuarantee<VecT>);
EXPECT_TRUE(tester.Test([](VecT* vec) {
vec->pop_back(); //
}));
}
TYPED_TEST(OneSizeTest, Erase) {
using VecT = typename TypeParam::VecT;
constexpr static auto size = TypeParam::GetSizeAt(0);
auto tester = testing::MakeExceptionSafetyTester()
.WithInitialValue(VecT{size})
.WithContracts(InlinedVectorInvariants<VecT>);
EXPECT_TRUE(tester.Test([](VecT* vec) {
auto it = vec->begin();
vec->erase(it);
}));
EXPECT_TRUE(tester.Test([](VecT* vec) {
auto it = vec->begin() + (vec->size() / 2);
vec->erase(it);
}));
EXPECT_TRUE(tester.Test([](VecT* vec) {
auto it = vec->begin() + (vec->size() - 1);
vec->erase(it);
}));
EXPECT_TRUE(tester.Test([](VecT* vec) {
auto it = vec->begin();
vec->erase(it, it);
}));
EXPECT_TRUE(tester.Test([](VecT* vec) {
auto it = vec->begin() + (vec->size() / 2);
vec->erase(it, it);
}));
EXPECT_TRUE(tester.Test([](VecT* vec) {
auto it = vec->begin() + (vec->size() - 1);
vec->erase(it, it);
}));
EXPECT_TRUE(tester.Test([](VecT* vec) {
auto it = vec->begin();
vec->erase(it, it + 1);
}));
EXPECT_TRUE(tester.Test([](VecT* vec) {
auto it = vec->begin() + (vec->size() / 2);
vec->erase(it, it + 1);
}));
EXPECT_TRUE(tester.Test([](VecT* vec) {
auto it = vec->begin() + (vec->size() - 1);
vec->erase(it, it + 1);
}));
}
TYPED_TEST(OneSizeTest, Clear) {
using VecT = typename TypeParam::VecT;
constexpr static auto size = TypeParam::GetSizeAt(0);
auto tester = testing::MakeExceptionSafetyTester()
.WithInitialValue(VecT{size})
.WithContracts(NoThrowGuarantee<VecT>);
EXPECT_TRUE(tester.Test([](VecT* vec) {
vec->clear(); //
}));
}
TYPED_TEST(TwoSizeTest, Reserve) {
using VecT = typename TypeParam::VecT;
constexpr static auto from_size = TypeParam::GetSizeAt(0);
constexpr static auto to_capacity = TypeParam::GetSizeAt(1);
auto tester = testing::MakeExceptionSafetyTester()
.WithInitialValue(VecT{from_size})
.WithContracts(InlinedVectorInvariants<VecT>);
EXPECT_TRUE(tester.Test([](VecT* vec) { vec->reserve(to_capacity); }));
}
TYPED_TEST(OneSizeTest, ShrinkToFit) {
using VecT = typename TypeParam::VecT;
constexpr static auto size = TypeParam::GetSizeAt(0);
auto tester = testing::MakeExceptionSafetyTester()
.WithInitialValue(VecT{size})
.WithContracts(InlinedVectorInvariants<VecT>);
EXPECT_TRUE(tester.Test([](VecT* vec) {
vec->shrink_to_fit(); //
}));
}
TYPED_TEST(TwoSizeTest, Swap) {
using VecT = typename TypeParam::VecT;
constexpr static auto from_size = TypeParam::GetSizeAt(0);
constexpr static auto to_size = TypeParam::GetSizeAt(1);
auto tester = testing::MakeExceptionSafetyTester()
.WithInitialValue(VecT{from_size})
.WithContracts(InlinedVectorInvariants<VecT>);
EXPECT_TRUE(tester.Test([](VecT* vec) {
VecT other_vec{to_size};
vec->swap(other_vec);
}));
EXPECT_TRUE(tester.Test([](VecT* vec) {
using std::swap;
VecT other_vec{to_size};
swap(*vec, other_vec);
}));
}
} // namespace
#endif // defined(ABSL_HAVE_EXCEPTIONS)

File diff suppressed because it is too large Load diff

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,763 @@
// Copyright 2018 The Abseil Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef ABSL_CONTAINER_INTERNAL_BTREE_CONTAINER_H_
#define ABSL_CONTAINER_INTERNAL_BTREE_CONTAINER_H_
#include <algorithm>
#include <initializer_list>
#include <iterator>
#include <utility>
#include "absl/base/attributes.h"
#include "absl/base/internal/throw_delegate.h"
#include "absl/container/internal/btree.h" // IWYU pragma: export
#include "absl/container/internal/common.h"
#include "absl/memory/memory.h"
#include "absl/meta/type_traits.h"
namespace absl {
ABSL_NAMESPACE_BEGIN
namespace container_internal {
// A common base class for btree_set, btree_map, btree_multiset, and
// btree_multimap.
template <typename Tree>
class btree_container {
using params_type = typename Tree::params_type;
protected:
// Alias used for heterogeneous lookup functions.
// `key_arg<K>` evaluates to `K` when the functors are transparent and to
// `key_type` otherwise. It permits template argument deduction on `K` for the
// transparent case.
template <class K>
using key_arg =
typename KeyArg<params_type::kIsKeyCompareTransparent>::template type<
K, typename Tree::key_type>;
public:
using key_type = typename Tree::key_type;
using value_type = typename Tree::value_type;
using size_type = typename Tree::size_type;
using difference_type = typename Tree::difference_type;
using key_compare = typename Tree::original_key_compare;
using value_compare = typename Tree::value_compare;
using allocator_type = typename Tree::allocator_type;
using reference = typename Tree::reference;
using const_reference = typename Tree::const_reference;
using pointer = typename Tree::pointer;
using const_pointer = typename Tree::const_pointer;
using iterator = typename Tree::iterator;
using const_iterator = typename Tree::const_iterator;
using reverse_iterator = typename Tree::reverse_iterator;
using const_reverse_iterator = typename Tree::const_reverse_iterator;
using node_type = typename Tree::node_handle_type;
struct extract_and_get_next_return_type {
node_type node;
iterator next;
};
// Constructors/assignments.
btree_container() : tree_(key_compare(), allocator_type()) {}
explicit btree_container(const key_compare &comp,
const allocator_type &alloc = allocator_type())
: tree_(comp, alloc) {}
explicit btree_container(const allocator_type &alloc)
: tree_(key_compare(), alloc) {}
btree_container(const btree_container &other)
: btree_container(other, absl::allocator_traits<allocator_type>::
select_on_container_copy_construction(
other.get_allocator())) {}
btree_container(const btree_container &other, const allocator_type &alloc)
: tree_(other.tree_, alloc) {}
btree_container(btree_container &&other) noexcept(
std::is_nothrow_move_constructible<Tree>::value) = default;
btree_container(btree_container &&other, const allocator_type &alloc)
: tree_(std::move(other.tree_), alloc) {}
btree_container &operator=(const btree_container &other) = default;
btree_container &operator=(btree_container &&other) noexcept(
std::is_nothrow_move_assignable<Tree>::value) = default;
// Iterator routines.
iterator begin() ABSL_ATTRIBUTE_LIFETIME_BOUND { return tree_.begin(); }
const_iterator begin() const ABSL_ATTRIBUTE_LIFETIME_BOUND {
return tree_.begin();
}
const_iterator cbegin() const ABSL_ATTRIBUTE_LIFETIME_BOUND {
return tree_.begin();
}
iterator end() ABSL_ATTRIBUTE_LIFETIME_BOUND { return tree_.end(); }
const_iterator end() const ABSL_ATTRIBUTE_LIFETIME_BOUND {
return tree_.end();
}
const_iterator cend() const ABSL_ATTRIBUTE_LIFETIME_BOUND {
return tree_.end();
}
reverse_iterator rbegin() ABSL_ATTRIBUTE_LIFETIME_BOUND {
return tree_.rbegin();
}
const_reverse_iterator rbegin() const ABSL_ATTRIBUTE_LIFETIME_BOUND {
return tree_.rbegin();
}
const_reverse_iterator crbegin() const ABSL_ATTRIBUTE_LIFETIME_BOUND {
return tree_.rbegin();
}
reverse_iterator rend() ABSL_ATTRIBUTE_LIFETIME_BOUND { return tree_.rend(); }
const_reverse_iterator rend() const ABSL_ATTRIBUTE_LIFETIME_BOUND {
return tree_.rend();
}
const_reverse_iterator crend() const ABSL_ATTRIBUTE_LIFETIME_BOUND {
return tree_.rend();
}
// Lookup routines.
template <typename K = key_type>
size_type count(const key_arg<K> &key) const {
auto equal_range = this->equal_range(key);
return equal_range.second - equal_range.first;
}
template <typename K = key_type>
iterator find(const key_arg<K> &key) ABSL_ATTRIBUTE_LIFETIME_BOUND {
return tree_.find(key);
}
template <typename K = key_type>
const_iterator find(const key_arg<K> &key) const
ABSL_ATTRIBUTE_LIFETIME_BOUND {
return tree_.find(key);
}
template <typename K = key_type>
bool contains(const key_arg<K> &key) const {
return find(key) != end();
}
template <typename K = key_type>
iterator lower_bound(const key_arg<K> &key) ABSL_ATTRIBUTE_LIFETIME_BOUND {
return tree_.lower_bound(key);
}
template <typename K = key_type>
const_iterator lower_bound(const key_arg<K> &key) const
ABSL_ATTRIBUTE_LIFETIME_BOUND {
return tree_.lower_bound(key);
}
template <typename K = key_type>
iterator upper_bound(const key_arg<K> &key) ABSL_ATTRIBUTE_LIFETIME_BOUND {
return tree_.upper_bound(key);
}
template <typename K = key_type>
const_iterator upper_bound(const key_arg<K> &key) const
ABSL_ATTRIBUTE_LIFETIME_BOUND {
return tree_.upper_bound(key);
}
template <typename K = key_type>
std::pair<iterator, iterator> equal_range(const key_arg<K> &key)
ABSL_ATTRIBUTE_LIFETIME_BOUND {
return tree_.equal_range(key);
}
template <typename K = key_type>
std::pair<const_iterator, const_iterator> equal_range(
const key_arg<K> &key) const ABSL_ATTRIBUTE_LIFETIME_BOUND {
return tree_.equal_range(key);
}
// Deletion routines. Note that there is also a deletion routine that is
// specific to btree_set_container/btree_multiset_container.
// Erase the specified iterator from the btree. The iterator must be valid
// (i.e. not equal to end()). Return an iterator pointing to the node after
// the one that was erased (or end() if none exists).
iterator erase(const_iterator iter) ABSL_ATTRIBUTE_LIFETIME_BOUND {
return tree_.erase(iterator(iter));
}
iterator erase(iterator iter) ABSL_ATTRIBUTE_LIFETIME_BOUND {
return tree_.erase(iter);
}
iterator erase(const_iterator first,
const_iterator last) ABSL_ATTRIBUTE_LIFETIME_BOUND {
return tree_.erase_range(iterator(first), iterator(last)).second;
}
template <typename K = key_type>
size_type erase(const key_arg<K> &key) {
auto equal_range = this->equal_range(key);
return tree_.erase_range(equal_range.first, equal_range.second).first;
}
// Extract routines.
extract_and_get_next_return_type extract_and_get_next(const_iterator position)
ABSL_ATTRIBUTE_LIFETIME_BOUND {
// Use Construct instead of Transfer because the rebalancing code will
// destroy the slot later.
// Note: we rely on erase() taking place after Construct().
return {CommonAccess::Construct<node_type>(get_allocator(),
iterator(position).slot()),
erase(position)};
}
node_type extract(iterator position) {
// Use Construct instead of Transfer because the rebalancing code will
// destroy the slot later.
auto node =
CommonAccess::Construct<node_type>(get_allocator(), position.slot());
erase(position);
return node;
}
node_type extract(const_iterator position) {
return extract(iterator(position));
}
// Utility routines.
ABSL_ATTRIBUTE_REINITIALIZES void clear() { tree_.clear(); }
void swap(btree_container &other) { tree_.swap(other.tree_); }
void verify() const { tree_.verify(); }
// Size routines.
size_type size() const { return tree_.size(); }
size_type max_size() const { return tree_.max_size(); }
bool empty() const { return tree_.empty(); }
friend bool operator==(const btree_container &x, const btree_container &y) {
if (x.size() != y.size()) return false;
return std::equal(x.begin(), x.end(), y.begin());
}
friend bool operator!=(const btree_container &x, const btree_container &y) {
return !(x == y);
}
friend bool operator<(const btree_container &x, const btree_container &y) {
return std::lexicographical_compare(x.begin(), x.end(), y.begin(), y.end());
}
friend bool operator>(const btree_container &x, const btree_container &y) {
return y < x;
}
friend bool operator<=(const btree_container &x, const btree_container &y) {
return !(y < x);
}
friend bool operator>=(const btree_container &x, const btree_container &y) {
return !(x < y);
}
// The allocator used by the btree.
allocator_type get_allocator() const { return tree_.get_allocator(); }
// The key comparator used by the btree.
key_compare key_comp() const { return key_compare(tree_.key_comp()); }
value_compare value_comp() const { return tree_.value_comp(); }
// Support absl::Hash.
template <typename State>
friend State AbslHashValue(State h, const btree_container &b) {
for (const auto &v : b) {
h = State::combine(std::move(h), v);
}
return State::combine(std::move(h), b.size());
}
protected:
friend struct btree_access;
Tree tree_;
};
// A common base class for btree_set and btree_map.
template <typename Tree>
class btree_set_container : public btree_container<Tree> {
using super_type = btree_container<Tree>;
using params_type = typename Tree::params_type;
using init_type = typename params_type::init_type;
using is_key_compare_to = typename params_type::is_key_compare_to;
friend class BtreeNodePeer;
protected:
template <class K>
using key_arg = typename super_type::template key_arg<K>;
public:
using key_type = typename Tree::key_type;
using value_type = typename Tree::value_type;
using size_type = typename Tree::size_type;
using key_compare = typename Tree::original_key_compare;
using allocator_type = typename Tree::allocator_type;
using iterator = typename Tree::iterator;
using const_iterator = typename Tree::const_iterator;
using node_type = typename super_type::node_type;
using insert_return_type = InsertReturnType<iterator, node_type>;
// Inherit constructors.
using super_type::super_type;
btree_set_container() {}
// Range constructors.
template <class InputIterator>
btree_set_container(InputIterator b, InputIterator e,
const key_compare &comp = key_compare(),
const allocator_type &alloc = allocator_type())
: super_type(comp, alloc) {
insert(b, e);
}
template <class InputIterator>
btree_set_container(InputIterator b, InputIterator e,
const allocator_type &alloc)
: btree_set_container(b, e, key_compare(), alloc) {}
// Initializer list constructors.
btree_set_container(std::initializer_list<init_type> init,
const key_compare &comp = key_compare(),
const allocator_type &alloc = allocator_type())
: btree_set_container(init.begin(), init.end(), comp, alloc) {}
btree_set_container(std::initializer_list<init_type> init,
const allocator_type &alloc)
: btree_set_container(init.begin(), init.end(), alloc) {}
// Insertion routines.
std::pair<iterator, bool> insert(const value_type &v)
ABSL_ATTRIBUTE_LIFETIME_BOUND {
return this->tree_.insert_unique(params_type::key(v), v);
}
std::pair<iterator, bool> insert(value_type &&v)
ABSL_ATTRIBUTE_LIFETIME_BOUND {
return this->tree_.insert_unique(params_type::key(v), std::move(v));
}
template <typename... Args>
std::pair<iterator, bool> emplace(Args &&...args)
ABSL_ATTRIBUTE_LIFETIME_BOUND {
// Use a node handle to manage a temp slot.
auto node = CommonAccess::Construct<node_type>(this->get_allocator(),
std::forward<Args>(args)...);
auto *slot = CommonAccess::GetSlot(node);
return this->tree_.insert_unique(params_type::key(slot), slot);
}
iterator insert(const_iterator hint,
const value_type &v) ABSL_ATTRIBUTE_LIFETIME_BOUND {
return this->tree_
.insert_hint_unique(iterator(hint), params_type::key(v), v)
.first;
}
iterator insert(const_iterator hint,
value_type &&v) ABSL_ATTRIBUTE_LIFETIME_BOUND {
return this->tree_
.insert_hint_unique(iterator(hint), params_type::key(v), std::move(v))
.first;
}
template <typename... Args>
iterator emplace_hint(const_iterator hint,
Args &&...args) ABSL_ATTRIBUTE_LIFETIME_BOUND {
// Use a node handle to manage a temp slot.
auto node = CommonAccess::Construct<node_type>(this->get_allocator(),
std::forward<Args>(args)...);
auto *slot = CommonAccess::GetSlot(node);
return this->tree_
.insert_hint_unique(iterator(hint), params_type::key(slot), slot)
.first;
}
template <typename InputIterator>
void insert(InputIterator b, InputIterator e) {
this->tree_.insert_iterator_unique(b, e, 0);
}
void insert(std::initializer_list<init_type> init) {
this->tree_.insert_iterator_unique(init.begin(), init.end(), 0);
}
insert_return_type insert(node_type &&node) ABSL_ATTRIBUTE_LIFETIME_BOUND {
if (!node) return {this->end(), false, node_type()};
std::pair<iterator, bool> res =
this->tree_.insert_unique(params_type::key(CommonAccess::GetSlot(node)),
CommonAccess::GetSlot(node));
if (res.second) {
CommonAccess::Destroy(&node);
return {res.first, true, node_type()};
} else {
return {res.first, false, std::move(node)};
}
}
iterator insert(const_iterator hint,
node_type &&node) ABSL_ATTRIBUTE_LIFETIME_BOUND {
if (!node) return this->end();
std::pair<iterator, bool> res = this->tree_.insert_hint_unique(
iterator(hint), params_type::key(CommonAccess::GetSlot(node)),
CommonAccess::GetSlot(node));
if (res.second) CommonAccess::Destroy(&node);
return res.first;
}
// Node extraction routines.
template <typename K = key_type>
node_type extract(const key_arg<K> &key) {
const std::pair<iterator, bool> lower_and_equal =
this->tree_.lower_bound_equal(key);
return lower_and_equal.second ? extract(lower_and_equal.first)
: node_type();
}
using super_type::extract;
// Merge routines.
// Moves elements from `src` into `this`. If the element already exists in
// `this`, it is left unmodified in `src`.
template <
typename T,
typename absl::enable_if_t<
absl::conjunction<
std::is_same<value_type, typename T::value_type>,
std::is_same<allocator_type, typename T::allocator_type>,
std::is_same<typename params_type::is_map_container,
typename T::params_type::is_map_container>>::value,
int> = 0>
void merge(btree_container<T> &src) { // NOLINT
for (auto src_it = src.begin(); src_it != src.end();) {
if (insert(std::move(params_type::element(src_it.slot()))).second) {
src_it = src.erase(src_it);
} else {
++src_it;
}
}
}
template <
typename T,
typename absl::enable_if_t<
absl::conjunction<
std::is_same<value_type, typename T::value_type>,
std::is_same<allocator_type, typename T::allocator_type>,
std::is_same<typename params_type::is_map_container,
typename T::params_type::is_map_container>>::value,
int> = 0>
void merge(btree_container<T> &&src) {
merge(src);
}
};
// Base class for btree_map.
template <typename Tree>
class btree_map_container : public btree_set_container<Tree> {
using super_type = btree_set_container<Tree>;
using params_type = typename Tree::params_type;
friend class BtreeNodePeer;
private:
template <class K>
using key_arg = typename super_type::template key_arg<K>;
public:
using key_type = typename Tree::key_type;
using mapped_type = typename params_type::mapped_type;
using value_type = typename Tree::value_type;
using key_compare = typename Tree::original_key_compare;
using allocator_type = typename Tree::allocator_type;
using iterator = typename Tree::iterator;
using const_iterator = typename Tree::const_iterator;
// Inherit constructors.
using super_type::super_type;
btree_map_container() {}
// Insertion routines.
// Note: the nullptr template arguments and extra `const M&` overloads allow
// for supporting bitfield arguments.
template <typename K = key_type, class M>
std::pair<iterator, bool> insert_or_assign(const key_arg<K> &k, const M &obj)
ABSL_ATTRIBUTE_LIFETIME_BOUND {
return insert_or_assign_impl(k, obj);
}
template <typename K = key_type, class M, K * = nullptr>
std::pair<iterator, bool> insert_or_assign(key_arg<K> &&k, const M &obj)
ABSL_ATTRIBUTE_LIFETIME_BOUND {
return insert_or_assign_impl(std::forward<K>(k), obj);
}
template <typename K = key_type, class M, M * = nullptr>
std::pair<iterator, bool> insert_or_assign(const key_arg<K> &k, M &&obj)
ABSL_ATTRIBUTE_LIFETIME_BOUND {
return insert_or_assign_impl(k, std::forward<M>(obj));
}
template <typename K = key_type, class M, K * = nullptr, M * = nullptr>
std::pair<iterator, bool> insert_or_assign(key_arg<K> &&k, M &&obj)
ABSL_ATTRIBUTE_LIFETIME_BOUND {
return insert_or_assign_impl(std::forward<K>(k), std::forward<M>(obj));
}
template <typename K = key_type, class M>
iterator insert_or_assign(const_iterator hint, const key_arg<K> &k,
const M &obj) ABSL_ATTRIBUTE_LIFETIME_BOUND {
return insert_or_assign_hint_impl(hint, k, obj);
}
template <typename K = key_type, class M, K * = nullptr>
iterator insert_or_assign(const_iterator hint, key_arg<K> &&k,
const M &obj) ABSL_ATTRIBUTE_LIFETIME_BOUND {
return insert_or_assign_hint_impl(hint, std::forward<K>(k), obj);
}
template <typename K = key_type, class M, M * = nullptr>
iterator insert_or_assign(const_iterator hint, const key_arg<K> &k,
M &&obj) ABSL_ATTRIBUTE_LIFETIME_BOUND {
return insert_or_assign_hint_impl(hint, k, std::forward<M>(obj));
}
template <typename K = key_type, class M, K * = nullptr, M * = nullptr>
iterator insert_or_assign(const_iterator hint, key_arg<K> &&k,
M &&obj) ABSL_ATTRIBUTE_LIFETIME_BOUND {
return insert_or_assign_hint_impl(hint, std::forward<K>(k),
std::forward<M>(obj));
}
template <typename K = key_type, typename... Args,
typename absl::enable_if_t<
!std::is_convertible<K, const_iterator>::value, int> = 0>
std::pair<iterator, bool> try_emplace(const key_arg<K> &k, Args &&...args)
ABSL_ATTRIBUTE_LIFETIME_BOUND {
return try_emplace_impl(k, std::forward<Args>(args)...);
}
template <typename K = key_type, typename... Args,
typename absl::enable_if_t<
!std::is_convertible<K, const_iterator>::value, int> = 0>
std::pair<iterator, bool> try_emplace(key_arg<K> &&k, Args &&...args)
ABSL_ATTRIBUTE_LIFETIME_BOUND {
return try_emplace_impl(std::forward<K>(k), std::forward<Args>(args)...);
}
template <typename K = key_type, typename... Args>
iterator try_emplace(const_iterator hint, const key_arg<K> &k,
Args &&...args) ABSL_ATTRIBUTE_LIFETIME_BOUND {
return try_emplace_hint_impl(hint, k, std::forward<Args>(args)...);
}
template <typename K = key_type, typename... Args>
iterator try_emplace(const_iterator hint, key_arg<K> &&k,
Args &&...args) ABSL_ATTRIBUTE_LIFETIME_BOUND {
return try_emplace_hint_impl(hint, std::forward<K>(k),
std::forward<Args>(args)...);
}
template <typename K = key_type>
mapped_type &operator[](const key_arg<K> &k) ABSL_ATTRIBUTE_LIFETIME_BOUND {
return try_emplace(k).first->second;
}
template <typename K = key_type>
mapped_type &operator[](key_arg<K> &&k) ABSL_ATTRIBUTE_LIFETIME_BOUND {
return try_emplace(std::forward<K>(k)).first->second;
}
template <typename K = key_type>
mapped_type &at(const key_arg<K> &key) ABSL_ATTRIBUTE_LIFETIME_BOUND {
auto it = this->find(key);
if (it == this->end())
base_internal::ThrowStdOutOfRange("absl::btree_map::at");
return it->second;
}
template <typename K = key_type>
const mapped_type &at(const key_arg<K> &key) const
ABSL_ATTRIBUTE_LIFETIME_BOUND {
auto it = this->find(key);
if (it == this->end())
base_internal::ThrowStdOutOfRange("absl::btree_map::at");
return it->second;
}
private:
// Note: when we call `std::forward<M>(obj)` twice, it's safe because
// insert_unique/insert_hint_unique are guaranteed to not consume `obj` when
// `ret.second` is false.
template <class K, class M>
std::pair<iterator, bool> insert_or_assign_impl(K &&k, M &&obj) {
const std::pair<iterator, bool> ret =
this->tree_.insert_unique(k, std::forward<K>(k), std::forward<M>(obj));
if (!ret.second) ret.first->second = std::forward<M>(obj);
return ret;
}
template <class K, class M>
iterator insert_or_assign_hint_impl(const_iterator hint, K &&k, M &&obj) {
const std::pair<iterator, bool> ret = this->tree_.insert_hint_unique(
iterator(hint), k, std::forward<K>(k), std::forward<M>(obj));
if (!ret.second) ret.first->second = std::forward<M>(obj);
return ret.first;
}
template <class K, class... Args>
std::pair<iterator, bool> try_emplace_impl(K &&k, Args &&... args) {
return this->tree_.insert_unique(
k, std::piecewise_construct, std::forward_as_tuple(std::forward<K>(k)),
std::forward_as_tuple(std::forward<Args>(args)...));
}
template <class K, class... Args>
iterator try_emplace_hint_impl(const_iterator hint, K &&k, Args &&... args) {
return this->tree_
.insert_hint_unique(iterator(hint), k, std::piecewise_construct,
std::forward_as_tuple(std::forward<K>(k)),
std::forward_as_tuple(std::forward<Args>(args)...))
.first;
}
};
// A common base class for btree_multiset and btree_multimap.
template <typename Tree>
class btree_multiset_container : public btree_container<Tree> {
using super_type = btree_container<Tree>;
using params_type = typename Tree::params_type;
using init_type = typename params_type::init_type;
using is_key_compare_to = typename params_type::is_key_compare_to;
friend class BtreeNodePeer;
template <class K>
using key_arg = typename super_type::template key_arg<K>;
public:
using key_type = typename Tree::key_type;
using value_type = typename Tree::value_type;
using size_type = typename Tree::size_type;
using key_compare = typename Tree::original_key_compare;
using allocator_type = typename Tree::allocator_type;
using iterator = typename Tree::iterator;
using const_iterator = typename Tree::const_iterator;
using node_type = typename super_type::node_type;
// Inherit constructors.
using super_type::super_type;
btree_multiset_container() {}
// Range constructors.
template <class InputIterator>
btree_multiset_container(InputIterator b, InputIterator e,
const key_compare &comp = key_compare(),
const allocator_type &alloc = allocator_type())
: super_type(comp, alloc) {
insert(b, e);
}
template <class InputIterator>
btree_multiset_container(InputIterator b, InputIterator e,
const allocator_type &alloc)
: btree_multiset_container(b, e, key_compare(), alloc) {}
// Initializer list constructors.
btree_multiset_container(std::initializer_list<init_type> init,
const key_compare &comp = key_compare(),
const allocator_type &alloc = allocator_type())
: btree_multiset_container(init.begin(), init.end(), comp, alloc) {}
btree_multiset_container(std::initializer_list<init_type> init,
const allocator_type &alloc)
: btree_multiset_container(init.begin(), init.end(), alloc) {}
// Insertion routines.
iterator insert(const value_type &v) ABSL_ATTRIBUTE_LIFETIME_BOUND {
return this->tree_.insert_multi(v);
}
iterator insert(value_type &&v) ABSL_ATTRIBUTE_LIFETIME_BOUND {
return this->tree_.insert_multi(std::move(v));
}
iterator insert(const_iterator hint,
const value_type &v) ABSL_ATTRIBUTE_LIFETIME_BOUND {
return this->tree_.insert_hint_multi(iterator(hint), v);
}
iterator insert(const_iterator hint,
value_type &&v) ABSL_ATTRIBUTE_LIFETIME_BOUND {
return this->tree_.insert_hint_multi(iterator(hint), std::move(v));
}
template <typename InputIterator>
void insert(InputIterator b, InputIterator e) {
this->tree_.insert_iterator_multi(b, e);
}
void insert(std::initializer_list<init_type> init) {
this->tree_.insert_iterator_multi(init.begin(), init.end());
}
template <typename... Args>
iterator emplace(Args &&...args) ABSL_ATTRIBUTE_LIFETIME_BOUND {
// Use a node handle to manage a temp slot.
auto node = CommonAccess::Construct<node_type>(this->get_allocator(),
std::forward<Args>(args)...);
return this->tree_.insert_multi(CommonAccess::GetSlot(node));
}
template <typename... Args>
iterator emplace_hint(const_iterator hint,
Args &&...args) ABSL_ATTRIBUTE_LIFETIME_BOUND {
// Use a node handle to manage a temp slot.
auto node = CommonAccess::Construct<node_type>(this->get_allocator(),
std::forward<Args>(args)...);
return this->tree_.insert_hint_multi(iterator(hint),
CommonAccess::GetSlot(node));
}
iterator insert(node_type &&node) ABSL_ATTRIBUTE_LIFETIME_BOUND {
if (!node) return this->end();
iterator res =
this->tree_.insert_multi(params_type::key(CommonAccess::GetSlot(node)),
CommonAccess::GetSlot(node));
CommonAccess::Destroy(&node);
return res;
}
iterator insert(const_iterator hint,
node_type &&node) ABSL_ATTRIBUTE_LIFETIME_BOUND {
if (!node) return this->end();
iterator res = this->tree_.insert_hint_multi(
iterator(hint),
std::move(params_type::element(CommonAccess::GetSlot(node))));
CommonAccess::Destroy(&node);
return res;
}
// Node extraction routines.
template <typename K = key_type>
node_type extract(const key_arg<K> &key) {
const std::pair<iterator, bool> lower_and_equal =
this->tree_.lower_bound_equal(key);
return lower_and_equal.second ? extract(lower_and_equal.first)
: node_type();
}
using super_type::extract;
// Merge routines.
// Moves all elements from `src` into `this`.
template <
typename T,
typename absl::enable_if_t<
absl::conjunction<
std::is_same<value_type, typename T::value_type>,
std::is_same<allocator_type, typename T::allocator_type>,
std::is_same<typename params_type::is_map_container,
typename T::params_type::is_map_container>>::value,
int> = 0>
void merge(btree_container<T> &src) { // NOLINT
for (auto src_it = src.begin(), end = src.end(); src_it != end; ++src_it) {
insert(std::move(params_type::element(src_it.slot())));
}
src.clear();
}
template <
typename T,
typename absl::enable_if_t<
absl::conjunction<
std::is_same<value_type, typename T::value_type>,
std::is_same<allocator_type, typename T::allocator_type>,
std::is_same<typename params_type::is_map_container,
typename T::params_type::is_map_container>>::value,
int> = 0>
void merge(btree_container<T> &&src) {
merge(src);
}
};
// A base class for btree_multimap.
template <typename Tree>
class btree_multimap_container : public btree_multiset_container<Tree> {
using super_type = btree_multiset_container<Tree>;
using params_type = typename Tree::params_type;
friend class BtreeNodePeer;
public:
using mapped_type = typename params_type::mapped_type;
// Inherit constructors.
using super_type::super_type;
btree_multimap_container() {}
};
} // namespace container_internal
ABSL_NAMESPACE_END
} // namespace absl
#endif // ABSL_CONTAINER_INTERNAL_BTREE_CONTAINER_H_

View file

@ -0,0 +1,207 @@
// Copyright 2018 The Abseil Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef ABSL_CONTAINER_INTERNAL_COMMON_H_
#define ABSL_CONTAINER_INTERNAL_COMMON_H_
#include <cassert>
#include <type_traits>
#include "absl/meta/type_traits.h"
#include "absl/types/optional.h"
namespace absl {
ABSL_NAMESPACE_BEGIN
namespace container_internal {
template <class, class = void>
struct IsTransparent : std::false_type {};
template <class T>
struct IsTransparent<T, absl::void_t<typename T::is_transparent>>
: std::true_type {};
template <bool is_transparent>
struct KeyArg {
// Transparent. Forward `K`.
template <typename K, typename key_type>
using type = K;
};
template <>
struct KeyArg<false> {
// Not transparent. Always use `key_type`.
template <typename K, typename key_type>
using type = key_type;
};
// The node_handle concept from C++17.
// We specialize node_handle for sets and maps. node_handle_base holds the
// common API of both.
template <typename PolicyTraits, typename Alloc>
class node_handle_base {
protected:
using slot_type = typename PolicyTraits::slot_type;
public:
using allocator_type = Alloc;
constexpr node_handle_base() = default;
node_handle_base(node_handle_base&& other) noexcept {
*this = std::move(other);
}
~node_handle_base() { destroy(); }
node_handle_base& operator=(node_handle_base&& other) noexcept {
destroy();
if (!other.empty()) {
alloc_ = other.alloc_;
PolicyTraits::transfer(alloc(), slot(), other.slot());
other.reset();
}
return *this;
}
bool empty() const noexcept { return !alloc_; }
explicit operator bool() const noexcept { return !empty(); }
allocator_type get_allocator() const { return *alloc_; }
protected:
friend struct CommonAccess;
struct transfer_tag_t {};
node_handle_base(transfer_tag_t, const allocator_type& a, slot_type* s)
: alloc_(a) {
PolicyTraits::transfer(alloc(), slot(), s);
}
struct construct_tag_t {};
template <typename... Args>
node_handle_base(construct_tag_t, const allocator_type& a, Args&&... args)
: alloc_(a) {
PolicyTraits::construct(alloc(), slot(), std::forward<Args>(args)...);
}
void destroy() {
if (!empty()) {
PolicyTraits::destroy(alloc(), slot());
reset();
}
}
void reset() {
assert(alloc_.has_value());
alloc_ = absl::nullopt;
}
slot_type* slot() const {
assert(!empty());
return reinterpret_cast<slot_type*>(std::addressof(slot_space_));
}
allocator_type* alloc() { return std::addressof(*alloc_); }
private:
absl::optional<allocator_type> alloc_ = {};
alignas(slot_type) mutable unsigned char slot_space_[sizeof(slot_type)] = {};
};
// For sets.
template <typename Policy, typename PolicyTraits, typename Alloc,
typename = void>
class node_handle : public node_handle_base<PolicyTraits, Alloc> {
using Base = node_handle_base<PolicyTraits, Alloc>;
public:
using value_type = typename PolicyTraits::value_type;
constexpr node_handle() {}
value_type& value() const { return PolicyTraits::element(this->slot()); }
private:
friend struct CommonAccess;
using Base::Base;
};
// For maps.
template <typename Policy, typename PolicyTraits, typename Alloc>
class node_handle<Policy, PolicyTraits, Alloc,
absl::void_t<typename Policy::mapped_type>>
: public node_handle_base<PolicyTraits, Alloc> {
using Base = node_handle_base<PolicyTraits, Alloc>;
using slot_type = typename PolicyTraits::slot_type;
public:
using key_type = typename Policy::key_type;
using mapped_type = typename Policy::mapped_type;
constexpr node_handle() {}
// When C++17 is available, we can use std::launder to provide mutable
// access to the key. Otherwise, we provide const access.
auto key() const
-> decltype(PolicyTraits::mutable_key(std::declval<slot_type*>())) {
return PolicyTraits::mutable_key(this->slot());
}
mapped_type& mapped() const {
return PolicyTraits::value(&PolicyTraits::element(this->slot()));
}
private:
friend struct CommonAccess;
using Base::Base;
};
// Provide access to non-public node-handle functions.
struct CommonAccess {
template <typename Node>
static auto GetSlot(const Node& node) -> decltype(node.slot()) {
return node.slot();
}
template <typename Node>
static void Destroy(Node* node) {
node->destroy();
}
template <typename Node>
static void Reset(Node* node) {
node->reset();
}
template <typename T, typename... Args>
static T Transfer(Args&&... args) {
return T(typename T::transfer_tag_t{}, std::forward<Args>(args)...);
}
template <typename T, typename... Args>
static T Construct(Args&&... args) {
return T(typename T::construct_tag_t{}, std::forward<Args>(args)...);
}
};
// Implement the insert_return_type<> concept of C++17.
template <class Iterator, class NodeType>
struct InsertReturnType {
Iterator position;
bool inserted;
NodeType node;
};
} // namespace container_internal
ABSL_NAMESPACE_END
} // namespace absl
#endif // ABSL_CONTAINER_INTERNAL_COMMON_H_

View file

@ -0,0 +1,152 @@
// Copyright 2022 The Abseil Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef ABSL_CONTAINER_INTERNAL_COMMON_POLICY_TRAITS_H_
#define ABSL_CONTAINER_INTERNAL_COMMON_POLICY_TRAITS_H_
#include <cstddef>
#include <cstring>
#include <memory>
#include <new>
#include <type_traits>
#include <utility>
#include "absl/meta/type_traits.h"
namespace absl {
ABSL_NAMESPACE_BEGIN
namespace container_internal {
template <class Policy, class = void>
struct policy_trait_element_is_owner : std::false_type {};
template <class Policy>
struct policy_trait_element_is_owner<
Policy,
std::enable_if_t<!std::is_void<typename Policy::element_is_owner>::value>>
: Policy::element_is_owner {};
// Defines how slots are initialized/destroyed/moved.
template <class Policy, class = void>
struct common_policy_traits {
// The actual object stored in the container.
using slot_type = typename Policy::slot_type;
using reference = decltype(Policy::element(std::declval<slot_type*>()));
using value_type = typename std::remove_reference<reference>::type;
// PRECONDITION: `slot` is UNINITIALIZED
// POSTCONDITION: `slot` is INITIALIZED
template <class Alloc, class... Args>
static void construct(Alloc* alloc, slot_type* slot, Args&&... args) {
Policy::construct(alloc, slot, std::forward<Args>(args)...);
}
// PRECONDITION: `slot` is INITIALIZED
// POSTCONDITION: `slot` is UNINITIALIZED
// Returns std::true_type in case destroy is trivial.
template <class Alloc>
static auto destroy(Alloc* alloc, slot_type* slot) {
return Policy::destroy(alloc, slot);
}
// Transfers the `old_slot` to `new_slot`. Any memory allocated by the
// allocator inside `old_slot` to `new_slot` can be transferred.
//
// OPTIONAL: defaults to:
//
// clone(new_slot, std::move(*old_slot));
// destroy(old_slot);
//
// PRECONDITION: `new_slot` is UNINITIALIZED and `old_slot` is INITIALIZED
// POSTCONDITION: `new_slot` is INITIALIZED and `old_slot` is
// UNINITIALIZED
template <class Alloc>
static void transfer(Alloc* alloc, slot_type* new_slot, slot_type* old_slot) {
transfer_impl(alloc, new_slot, old_slot, Rank2{});
}
// PRECONDITION: `slot` is INITIALIZED
// POSTCONDITION: `slot` is INITIALIZED
// Note: we use remove_const_t so that the two overloads have different args
// in the case of sets with explicitly const value_types.
template <class P = Policy>
static auto element(absl::remove_const_t<slot_type>* slot)
-> decltype(P::element(slot)) {
return P::element(slot);
}
template <class P = Policy>
static auto element(const slot_type* slot) -> decltype(P::element(slot)) {
return P::element(slot);
}
static constexpr bool transfer_uses_memcpy() {
return std::is_same<decltype(transfer_impl<std::allocator<char>>(
nullptr, nullptr, nullptr, Rank2{})),
std::true_type>::value;
}
// Returns true if destroy is trivial and can be omitted.
template <class Alloc>
static constexpr bool destroy_is_trivial() {
return std::is_same<decltype(destroy<Alloc>(nullptr, nullptr)),
std::true_type>::value;
}
private:
// Use go/ranked-overloads for dispatching.
struct Rank0 {};
struct Rank1 : Rank0 {};
struct Rank2 : Rank1 {};
// Use auto -> decltype as an enabler.
// P::transfer returns std::true_type if transfer uses memcpy (e.g. in
// node_slot_policy).
template <class Alloc, class P = Policy>
static auto transfer_impl(Alloc* alloc, slot_type* new_slot,
slot_type* old_slot,
Rank2) -> decltype(P::transfer(alloc, new_slot,
old_slot)) {
return P::transfer(alloc, new_slot, old_slot);
}
#if defined(__cpp_lib_launder) && __cpp_lib_launder >= 201606
// This overload returns true_type for the trait below.
// The conditional_t is to make the enabler type dependent.
template <class Alloc,
typename = std::enable_if_t<absl::is_trivially_relocatable<
std::conditional_t<false, Alloc, value_type>>::value>>
static std::true_type transfer_impl(Alloc*, slot_type* new_slot,
slot_type* old_slot, Rank1) {
// TODO(b/247130232): remove casts after fixing warnings.
// TODO(b/251814870): remove casts after fixing warnings.
std::memcpy(
static_cast<void*>(std::launder(
const_cast<std::remove_const_t<value_type>*>(&element(new_slot)))),
static_cast<const void*>(&element(old_slot)), sizeof(value_type));
return {};
}
#endif
template <class Alloc>
static void transfer_impl(Alloc* alloc, slot_type* new_slot,
slot_type* old_slot, Rank0) {
construct(alloc, new_slot, std::move(element(old_slot)));
destroy(alloc, old_slot);
}
};
} // namespace container_internal
ABSL_NAMESPACE_END
} // namespace absl
#endif // ABSL_CONTAINER_INTERNAL_COMMON_POLICY_TRAITS_H_

View file

@ -0,0 +1,157 @@
// Copyright 2022 The Abseil Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "absl/container/internal/common_policy_traits.h"
#include <functional>
#include <memory>
#include <type_traits>
#include <utility>
#include "gmock/gmock.h"
#include "gtest/gtest.h"
#include "absl/base/config.h"
namespace absl {
ABSL_NAMESPACE_BEGIN
namespace container_internal {
namespace {
using ::testing::MockFunction;
using ::testing::AnyNumber;
using ::testing::ReturnRef;
using Slot = int;
struct PolicyWithoutOptionalOps {
using slot_type = Slot;
using key_type = Slot;
using init_type = Slot;
struct PolicyFunctions {
std::function<void(void*, Slot*, Slot)> construct;
std::function<void(void*, Slot*)> destroy;
std::function<Slot&(Slot*)> element;
};
static PolicyFunctions* functions() {
static PolicyFunctions* functions = new PolicyFunctions();
return functions;
}
static void construct(void* a, Slot* b, Slot c) {
functions()->construct(a, b, c);
}
static void destroy(void* a, Slot* b) { functions()->destroy(a, b); }
static Slot& element(Slot* b) { return functions()->element(b); }
};
struct PolicyWithOptionalOps : PolicyWithoutOptionalOps {
struct TransferFunctions {
std::function<void(void*, Slot*, Slot*)> transfer;
};
static TransferFunctions* transfer_fn() {
static TransferFunctions* transfer_fn = new TransferFunctions();
return transfer_fn;
}
static void transfer(void* a, Slot* b, Slot* c) {
transfer_fn()->transfer(a, b, c);
}
};
struct PolicyWithMemcpyTransferAndTrivialDestroy : PolicyWithoutOptionalOps {
static std::true_type transfer(void*, Slot*, Slot*) { return {}; }
static std::true_type destroy(void*, Slot*) { return {}; }
};
struct Test : ::testing::Test {
Test() {
PolicyWithoutOptionalOps::functions()->construct = [&](void* a1, Slot* a2,
Slot a3) {
construct.Call(a1, a2, std::move(a3));
};
PolicyWithoutOptionalOps::functions()->destroy = [&](void* a1, Slot* a2) {
destroy.Call(a1, a2);
};
PolicyWithoutOptionalOps::functions()->element = [&](Slot* a1) -> Slot& {
return element.Call(a1);
};
PolicyWithOptionalOps::transfer_fn()->transfer =
[&](void* a1, Slot* a2, Slot* a3) { return transfer.Call(a1, a2, a3); };
}
std::allocator<Slot> alloc;
int a = 53;
MockFunction<void(void*, Slot*, Slot)> construct;
MockFunction<void(void*, Slot*)> destroy;
MockFunction<Slot&(Slot*)> element;
MockFunction<void(void*, Slot*, Slot*)> transfer;
};
TEST_F(Test, construct) {
EXPECT_CALL(construct, Call(&alloc, &a, 53));
common_policy_traits<PolicyWithoutOptionalOps>::construct(&alloc, &a, 53);
}
TEST_F(Test, destroy) {
EXPECT_CALL(destroy, Call(&alloc, &a));
common_policy_traits<PolicyWithoutOptionalOps>::destroy(&alloc, &a);
}
TEST_F(Test, element) {
int b = 0;
EXPECT_CALL(element, Call(&a)).WillOnce(ReturnRef(b));
EXPECT_EQ(&b, &common_policy_traits<PolicyWithoutOptionalOps>::element(&a));
}
TEST_F(Test, without_transfer) {
int b = 42;
EXPECT_CALL(element, Call(&a)).Times(AnyNumber()).WillOnce(ReturnRef(a));
EXPECT_CALL(element, Call(&b)).WillOnce(ReturnRef(b));
EXPECT_CALL(construct, Call(&alloc, &a, b)).Times(AnyNumber());
EXPECT_CALL(destroy, Call(&alloc, &b)).Times(AnyNumber());
common_policy_traits<PolicyWithoutOptionalOps>::transfer(&alloc, &a, &b);
}
TEST_F(Test, with_transfer) {
int b = 42;
EXPECT_CALL(transfer, Call(&alloc, &a, &b));
common_policy_traits<PolicyWithOptionalOps>::transfer(&alloc, &a, &b);
}
TEST(TransferUsesMemcpy, Basic) {
EXPECT_FALSE(
common_policy_traits<PolicyWithOptionalOps>::transfer_uses_memcpy());
EXPECT_TRUE(
common_policy_traits<
PolicyWithMemcpyTransferAndTrivialDestroy>::transfer_uses_memcpy());
}
TEST(DestroyIsTrivial, Basic) {
EXPECT_FALSE(common_policy_traits<PolicyWithOptionalOps>::destroy_is_trivial<
std::allocator<char>>());
EXPECT_TRUE(common_policy_traits<PolicyWithMemcpyTransferAndTrivialDestroy>::
destroy_is_trivial<std::allocator<char>>());
}
} // namespace
} // namespace container_internal
ABSL_NAMESPACE_END
} // namespace absl

View file

@ -0,0 +1,271 @@
// Copyright 2018 The Abseil Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// Helper class to perform the Empty Base Optimization.
// Ts can contain classes and non-classes, empty or not. For the ones that
// are empty classes, we perform the optimization. If all types in Ts are empty
// classes, then CompressedTuple<Ts...> is itself an empty class.
//
// To access the members, use member get<N>() function.
//
// Eg:
// absl::container_internal::CompressedTuple<int, T1, T2, T3> value(7, t1, t2,
// t3);
// assert(value.get<0>() == 7);
// T1& t1 = value.get<1>();
// const T2& t2 = value.get<2>();
// ...
//
// https://en.cppreference.com/w/cpp/language/ebo
#ifndef ABSL_CONTAINER_INTERNAL_COMPRESSED_TUPLE_H_
#define ABSL_CONTAINER_INTERNAL_COMPRESSED_TUPLE_H_
#include <initializer_list>
#include <tuple>
#include <type_traits>
#include <utility>
#include "absl/utility/utility.h"
#if defined(_MSC_VER) && !defined(__NVCC__)
// We need to mark these classes with this declspec to ensure that
// CompressedTuple happens.
#define ABSL_INTERNAL_COMPRESSED_TUPLE_DECLSPEC __declspec(empty_bases)
#else
#define ABSL_INTERNAL_COMPRESSED_TUPLE_DECLSPEC
#endif
namespace absl {
ABSL_NAMESPACE_BEGIN
namespace container_internal {
template <typename... Ts>
class CompressedTuple;
namespace internal_compressed_tuple {
template <typename D, size_t I>
struct Elem;
template <typename... B, size_t I>
struct Elem<CompressedTuple<B...>, I>
: std::tuple_element<I, std::tuple<B...>> {};
template <typename D, size_t I>
using ElemT = typename Elem<D, I>::type;
// We can't use EBCO on other CompressedTuples because that would mean that we
// derive from multiple Storage<> instantiations with the same I parameter,
// and potentially from multiple identical Storage<> instantiations. So anytime
// we use type inheritance rather than encapsulation, we mark
// CompressedTupleImpl, to make this easy to detect.
struct uses_inheritance {};
template <typename T>
constexpr bool ShouldUseBase() {
return std::is_class<T>::value && std::is_empty<T>::value &&
!std::is_final<T>::value &&
!std::is_base_of<uses_inheritance, T>::value;
}
// The storage class provides two specializations:
// - For empty classes, it stores T as a base class.
// - For everything else, it stores T as a member.
template <typename T, size_t I, bool UseBase = ShouldUseBase<T>()>
struct Storage {
T value;
constexpr Storage() = default;
template <typename V>
explicit constexpr Storage(absl::in_place_t, V&& v)
: value(std::forward<V>(v)) {}
constexpr const T& get() const& { return value; }
constexpr T& get() & { return value; }
constexpr const T&& get() const&& { return std::move(*this).value; }
constexpr T&& get() && { return std::move(*this).value; }
};
template <typename T, size_t I>
struct ABSL_INTERNAL_COMPRESSED_TUPLE_DECLSPEC Storage<T, I, true> : T {
constexpr Storage() = default;
template <typename V>
explicit constexpr Storage(absl::in_place_t, V&& v) : T(std::forward<V>(v)) {}
constexpr const T& get() const& { return *this; }
constexpr T& get() & { return *this; }
constexpr const T&& get() const&& { return std::move(*this); }
constexpr T&& get() && { return std::move(*this); }
};
template <typename D, typename I, bool ShouldAnyUseBase>
struct ABSL_INTERNAL_COMPRESSED_TUPLE_DECLSPEC CompressedTupleImpl;
template <typename... Ts, size_t... I, bool ShouldAnyUseBase>
struct ABSL_INTERNAL_COMPRESSED_TUPLE_DECLSPEC CompressedTupleImpl<
CompressedTuple<Ts...>, absl::index_sequence<I...>, ShouldAnyUseBase>
// We use the dummy identity function through std::integral_constant to
// convince MSVC of accepting and expanding I in that context. Without it
// you would get:
// error C3548: 'I': parameter pack cannot be used in this context
: uses_inheritance,
Storage<Ts, std::integral_constant<size_t, I>::value>... {
constexpr CompressedTupleImpl() = default;
template <typename... Vs>
explicit constexpr CompressedTupleImpl(absl::in_place_t, Vs&&... args)
: Storage<Ts, I>(absl::in_place, std::forward<Vs>(args))... {}
friend CompressedTuple<Ts...>;
};
template <typename... Ts, size_t... I>
struct ABSL_INTERNAL_COMPRESSED_TUPLE_DECLSPEC CompressedTupleImpl<
CompressedTuple<Ts...>, absl::index_sequence<I...>, false>
// We use the dummy identity function as above...
: Storage<Ts, std::integral_constant<size_t, I>::value, false>... {
constexpr CompressedTupleImpl() = default;
template <typename... Vs>
explicit constexpr CompressedTupleImpl(absl::in_place_t, Vs&&... args)
: Storage<Ts, I, false>(absl::in_place, std::forward<Vs>(args))... {}
friend CompressedTuple<Ts...>;
};
std::false_type Or(std::initializer_list<std::false_type>);
std::true_type Or(std::initializer_list<bool>);
// MSVC requires this to be done separately rather than within the declaration
// of CompressedTuple below.
template <typename... Ts>
constexpr bool ShouldAnyUseBase() {
return decltype(
Or({std::integral_constant<bool, ShouldUseBase<Ts>()>()...})){};
}
template <typename T, typename V>
using TupleElementMoveConstructible =
typename std::conditional<std::is_reference<T>::value,
std::is_convertible<V, T>,
std::is_constructible<T, V&&>>::type;
template <bool SizeMatches, class T, class... Vs>
struct TupleMoveConstructible : std::false_type {};
template <class... Ts, class... Vs>
struct TupleMoveConstructible<true, CompressedTuple<Ts...>, Vs...>
: std::integral_constant<
bool, absl::conjunction<
TupleElementMoveConstructible<Ts, Vs&&>...>::value> {};
template <typename T>
struct compressed_tuple_size;
template <typename... Es>
struct compressed_tuple_size<CompressedTuple<Es...>>
: public std::integral_constant<std::size_t, sizeof...(Es)> {};
template <class T, class... Vs>
struct TupleItemsMoveConstructible
: std::integral_constant<
bool, TupleMoveConstructible<compressed_tuple_size<T>::value ==
sizeof...(Vs),
T, Vs...>::value> {};
} // namespace internal_compressed_tuple
// Helper class to perform the Empty Base Class Optimization.
// Ts can contain classes and non-classes, empty or not. For the ones that
// are empty classes, we perform the CompressedTuple. If all types in Ts are
// empty classes, then CompressedTuple<Ts...> is itself an empty class. (This
// does not apply when one or more of those empty classes is itself an empty
// CompressedTuple.)
//
// To access the members, use member .get<N>() function.
//
// Eg:
// absl::container_internal::CompressedTuple<int, T1, T2, T3> value(7, t1, t2,
// t3);
// assert(value.get<0>() == 7);
// T1& t1 = value.get<1>();
// const T2& t2 = value.get<2>();
// ...
//
// https://en.cppreference.com/w/cpp/language/ebo
template <typename... Ts>
class ABSL_INTERNAL_COMPRESSED_TUPLE_DECLSPEC CompressedTuple
: private internal_compressed_tuple::CompressedTupleImpl<
CompressedTuple<Ts...>, absl::index_sequence_for<Ts...>,
internal_compressed_tuple::ShouldAnyUseBase<Ts...>()> {
private:
template <int I>
using ElemT = internal_compressed_tuple::ElemT<CompressedTuple, I>;
template <int I>
using StorageT = internal_compressed_tuple::Storage<ElemT<I>, I>;
public:
// There seems to be a bug in MSVC dealing in which using '=default' here will
// cause the compiler to ignore the body of other constructors. The work-
// around is to explicitly implement the default constructor.
#if defined(_MSC_VER)
constexpr CompressedTuple() : CompressedTuple::CompressedTupleImpl() {}
#else
constexpr CompressedTuple() = default;
#endif
explicit constexpr CompressedTuple(const Ts&... base)
: CompressedTuple::CompressedTupleImpl(absl::in_place, base...) {}
template <typename First, typename... Vs,
absl::enable_if_t<
absl::conjunction<
// Ensure we are not hiding default copy/move constructors.
absl::negation<std::is_same<void(CompressedTuple),
void(absl::decay_t<First>)>>,
internal_compressed_tuple::TupleItemsMoveConstructible<
CompressedTuple<Ts...>, First, Vs...>>::value,
bool> = true>
explicit constexpr CompressedTuple(First&& first, Vs&&... base)
: CompressedTuple::CompressedTupleImpl(absl::in_place,
std::forward<First>(first),
std::forward<Vs>(base)...) {}
template <int I>
constexpr ElemT<I>& get() & {
return StorageT<I>::get();
}
template <int I>
constexpr const ElemT<I>& get() const& {
return StorageT<I>::get();
}
template <int I>
constexpr ElemT<I>&& get() && {
return std::move(*this).StorageT<I>::get();
}
template <int I>
constexpr const ElemT<I>&& get() const&& {
return std::move(*this).StorageT<I>::get();
}
};
// Explicit specialization for a zero-element tuple
// (needed to avoid ambiguous overloads for the default constructor).
template <>
class ABSL_INTERNAL_COMPRESSED_TUPLE_DECLSPEC CompressedTuple<> {};
} // namespace container_internal
ABSL_NAMESPACE_END
} // namespace absl
#undef ABSL_INTERNAL_COMPRESSED_TUPLE_DECLSPEC
#endif // ABSL_CONTAINER_INTERNAL_COMPRESSED_TUPLE_H_

View file

@ -0,0 +1,482 @@
// Copyright 2018 The Abseil Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "absl/container/internal/compressed_tuple.h"
#include <memory>
#include <set>
#include <string>
#include <type_traits>
#include <utility>
#include <vector>
#include "gmock/gmock.h"
#include "gtest/gtest.h"
#include "absl/container/internal/test_instance_tracker.h"
#include "absl/memory/memory.h"
#include "absl/types/any.h"
#include "absl/types/optional.h"
#include "absl/utility/utility.h"
// These are declared at global scope purely so that error messages
// are smaller and easier to understand.
enum class CallType { kMutableRef, kConstRef, kMutableMove, kConstMove };
template <int>
struct Empty {
constexpr CallType value() & { return CallType::kMutableRef; }
constexpr CallType value() const& { return CallType::kConstRef; }
constexpr CallType value() && { return CallType::kMutableMove; }
constexpr CallType value() const&& { return CallType::kConstMove; }
};
// Unconditionally return an lvalue reference to `t`.
template <typename T>
constexpr T& AsLValue(T&& t) {
return t;
}
template <typename T>
struct NotEmpty {
T value;
};
template <typename T, typename U>
struct TwoValues {
T value1;
U value2;
};
namespace absl {
ABSL_NAMESPACE_BEGIN
namespace container_internal {
namespace {
using absl::test_internal::CopyableMovableInstance;
using absl::test_internal::InstanceTracker;
using ::testing::Each;
TEST(CompressedTupleTest, Sizeof) {
EXPECT_EQ(sizeof(int), sizeof(CompressedTuple<int>));
EXPECT_EQ(sizeof(int), sizeof(CompressedTuple<int, Empty<0>>));
EXPECT_EQ(sizeof(int), sizeof(CompressedTuple<int, Empty<0>, Empty<1>>));
EXPECT_EQ(sizeof(int),
sizeof(CompressedTuple<int, Empty<0>, Empty<1>, Empty<2>>));
EXPECT_EQ(sizeof(TwoValues<int, double>),
sizeof(CompressedTuple<int, NotEmpty<double>>));
EXPECT_EQ(sizeof(TwoValues<int, double>),
sizeof(CompressedTuple<int, Empty<0>, NotEmpty<double>>));
EXPECT_EQ(sizeof(TwoValues<int, double>),
sizeof(CompressedTuple<int, Empty<0>, NotEmpty<double>, Empty<1>>));
}
TEST(CompressedTupleTest, PointerToEmpty) {
auto to_void_ptrs = [](const auto&... objs) {
return std::vector<const void*>{static_cast<const void*>(&objs)...};
};
{
using Tuple = CompressedTuple<int, Empty<0>>;
EXPECT_EQ(sizeof(int), sizeof(Tuple));
Tuple t;
EXPECT_THAT(to_void_ptrs(t.get<1>()), Each(&t));
}
{
using Tuple = CompressedTuple<int, Empty<0>, Empty<1>>;
EXPECT_EQ(sizeof(int), sizeof(Tuple));
Tuple t;
EXPECT_THAT(to_void_ptrs(t.get<1>(), t.get<2>()), Each(&t));
}
{
using Tuple = CompressedTuple<int, Empty<0>, Empty<1>, Empty<2>>;
EXPECT_EQ(sizeof(int), sizeof(Tuple));
Tuple t;
EXPECT_THAT(to_void_ptrs(t.get<1>(), t.get<2>(), t.get<3>()), Each(&t));
}
}
TEST(CompressedTupleTest, OneMoveOnRValueConstructionTemp) {
InstanceTracker tracker;
CompressedTuple<CopyableMovableInstance> x1(CopyableMovableInstance(1));
EXPECT_EQ(tracker.instances(), 1);
EXPECT_EQ(tracker.copies(), 0);
EXPECT_LE(tracker.moves(), 1);
EXPECT_EQ(x1.get<0>().value(), 1);
}
TEST(CompressedTupleTest, OneMoveOnRValueConstructionMove) {
InstanceTracker tracker;
CopyableMovableInstance i1(1);
CompressedTuple<CopyableMovableInstance> x1(std::move(i1));
EXPECT_EQ(tracker.instances(), 2);
EXPECT_EQ(tracker.copies(), 0);
EXPECT_LE(tracker.moves(), 1);
EXPECT_EQ(x1.get<0>().value(), 1);
}
TEST(CompressedTupleTest, OneMoveOnRValueConstructionMixedTypes) {
InstanceTracker tracker;
CopyableMovableInstance i1(1);
CopyableMovableInstance i2(2);
Empty<0> empty;
CompressedTuple<CopyableMovableInstance, CopyableMovableInstance&, Empty<0>>
x1(std::move(i1), i2, empty);
EXPECT_EQ(x1.get<0>().value(), 1);
EXPECT_EQ(x1.get<1>().value(), 2);
EXPECT_EQ(tracker.copies(), 0);
EXPECT_EQ(tracker.moves(), 1);
}
struct IncompleteType;
CompressedTuple<CopyableMovableInstance, IncompleteType&, Empty<0>>
MakeWithIncomplete(CopyableMovableInstance i1,
IncompleteType& t, // NOLINT
Empty<0> empty) {
return CompressedTuple<CopyableMovableInstance, IncompleteType&, Empty<0>>{
std::move(i1), t, empty};
}
struct IncompleteType {};
TEST(CompressedTupleTest, OneMoveOnRValueConstructionWithIncompleteType) {
InstanceTracker tracker;
CopyableMovableInstance i1(1);
Empty<0> empty;
struct DerivedType : IncompleteType {int value = 0;};
DerivedType fd;
fd.value = 7;
CompressedTuple<CopyableMovableInstance, IncompleteType&, Empty<0>> x1 =
MakeWithIncomplete(std::move(i1), fd, empty);
EXPECT_EQ(x1.get<0>().value(), 1);
EXPECT_EQ(static_cast<DerivedType&>(x1.get<1>()).value, 7);
EXPECT_EQ(tracker.copies(), 0);
EXPECT_EQ(tracker.moves(), 2);
}
TEST(CompressedTupleTest,
OneMoveOnRValueConstructionMixedTypes_BraceInitPoisonPillExpected) {
InstanceTracker tracker;
CopyableMovableInstance i1(1);
CopyableMovableInstance i2(2);
CompressedTuple<CopyableMovableInstance, CopyableMovableInstance&, Empty<0>>
x1(std::move(i1), i2, {}); // NOLINT
EXPECT_EQ(x1.get<0>().value(), 1);
EXPECT_EQ(x1.get<1>().value(), 2);
EXPECT_EQ(tracker.instances(), 3);
// We are forced into the `const Ts&...` constructor (invoking copies)
// because we need it to deduce the type of `{}`.
// std::tuple also has this behavior.
// Note, this test is proof that this is expected behavior, but it is not
// _desired_ behavior.
EXPECT_EQ(tracker.copies(), 1);
EXPECT_EQ(tracker.moves(), 0);
}
TEST(CompressedTupleTest, OneCopyOnLValueConstruction) {
InstanceTracker tracker;
CopyableMovableInstance i1(1);
CompressedTuple<CopyableMovableInstance> x1(i1);
EXPECT_EQ(tracker.copies(), 1);
EXPECT_EQ(tracker.moves(), 0);
tracker.ResetCopiesMovesSwaps();
CopyableMovableInstance i2(2);
const CopyableMovableInstance& i2_ref = i2;
CompressedTuple<CopyableMovableInstance> x2(i2_ref);
EXPECT_EQ(tracker.copies(), 1);
EXPECT_EQ(tracker.moves(), 0);
}
TEST(CompressedTupleTest, OneMoveOnRValueAccess) {
InstanceTracker tracker;
CopyableMovableInstance i1(1);
CompressedTuple<CopyableMovableInstance> x(std::move(i1));
tracker.ResetCopiesMovesSwaps();
CopyableMovableInstance i2 = std::move(x).get<0>();
EXPECT_EQ(tracker.copies(), 0);
EXPECT_EQ(tracker.moves(), 1);
}
TEST(CompressedTupleTest, OneCopyOnLValueAccess) {
InstanceTracker tracker;
CompressedTuple<CopyableMovableInstance> x(CopyableMovableInstance(0));
EXPECT_EQ(tracker.copies(), 0);
EXPECT_EQ(tracker.moves(), 1);
CopyableMovableInstance t = x.get<0>();
EXPECT_EQ(tracker.copies(), 1);
EXPECT_EQ(tracker.moves(), 1);
}
TEST(CompressedTupleTest, ZeroCopyOnRefAccess) {
InstanceTracker tracker;
CompressedTuple<CopyableMovableInstance> x(CopyableMovableInstance(0));
EXPECT_EQ(tracker.copies(), 0);
EXPECT_EQ(tracker.moves(), 1);
CopyableMovableInstance& t1 = x.get<0>();
const CopyableMovableInstance& t2 = x.get<0>();
EXPECT_EQ(tracker.copies(), 0);
EXPECT_EQ(tracker.moves(), 1);
EXPECT_EQ(t1.value(), 0);
EXPECT_EQ(t2.value(), 0);
}
TEST(CompressedTupleTest, Access) {
struct S {
std::string x;
};
CompressedTuple<int, Empty<0>, S> x(7, {}, S{"ABC"});
EXPECT_EQ(sizeof(x), sizeof(TwoValues<int, S>));
EXPECT_EQ(7, x.get<0>());
EXPECT_EQ("ABC", x.get<2>().x);
}
TEST(CompressedTupleTest, NonClasses) {
CompressedTuple<int, const char*> x(7, "ABC");
EXPECT_EQ(7, x.get<0>());
EXPECT_STREQ("ABC", x.get<1>());
}
TEST(CompressedTupleTest, MixClassAndNonClass) {
CompressedTuple<int, const char*, Empty<0>, NotEmpty<double>> x(7, "ABC", {},
{1.25});
struct Mock {
int v;
const char* p;
double d;
};
EXPECT_EQ(sizeof(x), sizeof(Mock));
EXPECT_EQ(7, x.get<0>());
EXPECT_STREQ("ABC", x.get<1>());
EXPECT_EQ(1.25, x.get<3>().value);
}
TEST(CompressedTupleTest, Nested) {
CompressedTuple<int, CompressedTuple<int>,
CompressedTuple<int, CompressedTuple<int>>>
x(1, CompressedTuple<int>(2),
CompressedTuple<int, CompressedTuple<int>>(3, CompressedTuple<int>(4)));
EXPECT_EQ(1, x.get<0>());
EXPECT_EQ(2, x.get<1>().get<0>());
EXPECT_EQ(3, x.get<2>().get<0>());
EXPECT_EQ(4, x.get<2>().get<1>().get<0>());
CompressedTuple<Empty<0>, Empty<0>,
CompressedTuple<Empty<0>, CompressedTuple<Empty<0>>>>
y;
std::set<Empty<0>*> empties{&y.get<0>(), &y.get<1>(), &y.get<2>().get<0>(),
&y.get<2>().get<1>().get<0>()};
#ifdef _MSC_VER
// MSVC has a bug where many instances of the same base class are layed out in
// the same address when using __declspec(empty_bases).
// This will be fixed in a future version of MSVC.
int expected = 1;
#else
int expected = 4;
#endif
EXPECT_EQ(expected, sizeof(y));
EXPECT_EQ(expected, empties.size());
EXPECT_EQ(sizeof(y), sizeof(Empty<0>) * empties.size());
EXPECT_EQ(4 * sizeof(char),
sizeof(CompressedTuple<CompressedTuple<char, char>,
CompressedTuple<char, char>>));
EXPECT_TRUE((std::is_empty<CompressedTuple<Empty<0>, Empty<1>>>::value));
// Make sure everything still works when things are nested.
struct CT_Empty : CompressedTuple<Empty<0>> {};
CompressedTuple<Empty<0>, CT_Empty> nested_empty;
auto contained = nested_empty.get<0>();
auto nested = nested_empty.get<1>().get<0>();
EXPECT_TRUE((std::is_same<decltype(contained), decltype(nested)>::value));
}
TEST(CompressedTupleTest, Reference) {
int i = 7;
std::string s = "Very long string that goes in the heap";
CompressedTuple<int, int&, std::string, std::string&> x(i, i, s, s);
// Sanity check. We should have not moved from `s`
EXPECT_EQ(s, "Very long string that goes in the heap");
EXPECT_EQ(x.get<0>(), x.get<1>());
EXPECT_NE(&x.get<0>(), &x.get<1>());
EXPECT_EQ(&x.get<1>(), &i);
EXPECT_EQ(x.get<2>(), x.get<3>());
EXPECT_NE(&x.get<2>(), &x.get<3>());
EXPECT_EQ(&x.get<3>(), &s);
}
TEST(CompressedTupleTest, NoElements) {
CompressedTuple<> x;
static_cast<void>(x); // Silence -Wunused-variable.
EXPECT_TRUE(std::is_empty<CompressedTuple<>>::value);
}
TEST(CompressedTupleTest, MoveOnlyElements) {
CompressedTuple<std::unique_ptr<std::string>> str_tup(
absl::make_unique<std::string>("str"));
CompressedTuple<CompressedTuple<std::unique_ptr<std::string>>,
std::unique_ptr<int>>
x(std::move(str_tup), absl::make_unique<int>(5));
EXPECT_EQ(*x.get<0>().get<0>(), "str");
EXPECT_EQ(*x.get<1>(), 5);
std::unique_ptr<std::string> x0 = std::move(x.get<0>()).get<0>();
std::unique_ptr<int> x1 = std::move(x).get<1>();
EXPECT_EQ(*x0, "str");
EXPECT_EQ(*x1, 5);
}
TEST(CompressedTupleTest, MoveConstructionMoveOnlyElements) {
CompressedTuple<std::unique_ptr<std::string>> base(
absl::make_unique<std::string>("str"));
EXPECT_EQ(*base.get<0>(), "str");
CompressedTuple<std::unique_ptr<std::string>> copy(std::move(base));
EXPECT_EQ(*copy.get<0>(), "str");
}
TEST(CompressedTupleTest, AnyElements) {
any a(std::string("str"));
CompressedTuple<any, any&> x(any(5), a);
EXPECT_EQ(absl::any_cast<int>(x.get<0>()), 5);
EXPECT_EQ(absl::any_cast<std::string>(x.get<1>()), "str");
a = 0.5f;
EXPECT_EQ(absl::any_cast<float>(x.get<1>()), 0.5);
}
TEST(CompressedTupleTest, Constexpr) {
struct NonTrivialStruct {
constexpr NonTrivialStruct() = default;
constexpr int value() const { return v; }
int v = 5;
};
struct TrivialStruct {
TrivialStruct() = default;
constexpr int value() const { return v; }
int v;
};
using Tuple = CompressedTuple<int, double, CompressedTuple<int>, Empty<0>>;
constexpr int r0 =
AsLValue(Tuple(1, 0.75, CompressedTuple<int>(9), {})).get<0>();
constexpr double r1 =
AsLValue(Tuple(1, 0.75, CompressedTuple<int>(9), {})).get<1>();
constexpr int r2 =
AsLValue(Tuple(1, 0.75, CompressedTuple<int>(9), {})).get<2>().get<0>();
constexpr CallType r3 =
AsLValue(Tuple(1, 0.75, CompressedTuple<int>(9), {})).get<3>().value();
EXPECT_EQ(r0, 1);
EXPECT_EQ(r1, 0.75);
EXPECT_EQ(r2, 9);
EXPECT_EQ(r3, CallType::kMutableRef);
constexpr Tuple x(7, 1.25, CompressedTuple<int>(5), {});
constexpr int x0 = x.get<0>();
constexpr double x1 = x.get<1>();
constexpr int x2 = x.get<2>().get<0>();
constexpr CallType x3 = x.get<3>().value();
EXPECT_EQ(x0, 7);
EXPECT_EQ(x1, 1.25);
EXPECT_EQ(x2, 5);
EXPECT_EQ(x3, CallType::kConstRef);
constexpr int m0 = Tuple(5, 0.25, CompressedTuple<int>(3), {}).get<0>();
constexpr double m1 = Tuple(5, 0.25, CompressedTuple<int>(3), {}).get<1>();
constexpr int m2 =
Tuple(5, 0.25, CompressedTuple<int>(3), {}).get<2>().get<0>();
constexpr CallType m3 =
Tuple(5, 0.25, CompressedTuple<int>(3), {}).get<3>().value();
EXPECT_EQ(m0, 5);
EXPECT_EQ(m1, 0.25);
EXPECT_EQ(m2, 3);
EXPECT_EQ(m3, CallType::kMutableMove);
constexpr CompressedTuple<Empty<0>, TrivialStruct, int> trivial = {};
constexpr CallType trivial0 = trivial.get<0>().value();
constexpr int trivial1 = trivial.get<1>().value();
constexpr int trivial2 = trivial.get<2>();
EXPECT_EQ(trivial0, CallType::kConstRef);
EXPECT_EQ(trivial1, 0);
EXPECT_EQ(trivial2, 0);
constexpr CompressedTuple<Empty<0>, NonTrivialStruct, absl::optional<int>>
non_trivial = {};
constexpr CallType non_trivial0 = non_trivial.get<0>().value();
constexpr int non_trivial1 = non_trivial.get<1>().value();
constexpr absl::optional<int> non_trivial2 = non_trivial.get<2>();
EXPECT_EQ(non_trivial0, CallType::kConstRef);
EXPECT_EQ(non_trivial1, 5);
EXPECT_EQ(non_trivial2, absl::nullopt);
static constexpr char data[] = "DEF";
constexpr CompressedTuple<const char*> z(data);
constexpr const char* z1 = z.get<0>();
EXPECT_EQ(std::string(z1), std::string(data));
#if defined(__clang__)
// An apparent bug in earlier versions of gcc claims these are ambiguous.
constexpr int x2m = std::move(x.get<2>()).get<0>();
constexpr CallType x3m = std::move(x).get<3>().value();
EXPECT_EQ(x2m, 5);
EXPECT_EQ(x3m, CallType::kConstMove);
#endif
}
#if defined(__clang__) || defined(__GNUC__)
TEST(CompressedTupleTest, EmptyFinalClass) {
struct S final {
int f() const { return 5; }
};
CompressedTuple<S> x;
EXPECT_EQ(x.get<0>().f(), 5);
}
#endif
// TODO(b/214288561): enable this test.
TEST(CompressedTupleTest, DISABLED_NestedEbo) {
struct Empty1 {};
struct Empty2 {};
CompressedTuple<Empty1, CompressedTuple<Empty2>, int> x;
CompressedTuple<Empty1, Empty2, int> y;
// Currently fails with sizeof(x) == 8, sizeof(y) == 4.
EXPECT_EQ(sizeof(x), sizeof(y));
}
} // namespace
} // namespace container_internal
ABSL_NAMESPACE_END
} // namespace absl

View file

@ -0,0 +1,492 @@
// Copyright 2018 The Abseil Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef ABSL_CONTAINER_INTERNAL_CONTAINER_MEMORY_H_
#define ABSL_CONTAINER_INTERNAL_CONTAINER_MEMORY_H_
#include <cassert>
#include <cstddef>
#include <cstdint>
#include <cstring>
#include <memory>
#include <new>
#include <tuple>
#include <type_traits>
#include <utility>
#include "absl/base/config.h"
#include "absl/memory/memory.h"
#include "absl/meta/type_traits.h"
#include "absl/utility/utility.h"
#ifdef ABSL_HAVE_ADDRESS_SANITIZER
#include <sanitizer/asan_interface.h>
#endif
#ifdef ABSL_HAVE_MEMORY_SANITIZER
#include <sanitizer/msan_interface.h>
#endif
namespace absl {
ABSL_NAMESPACE_BEGIN
namespace container_internal {
template <size_t Alignment>
struct alignas(Alignment) AlignedType {};
// Allocates at least n bytes aligned to the specified alignment.
// Alignment must be a power of 2. It must be positive.
//
// Note that many allocators don't honor alignment requirements above certain
// threshold (usually either alignof(std::max_align_t) or alignof(void*)).
// Allocate() doesn't apply alignment corrections. If the underlying allocator
// returns insufficiently alignment pointer, that's what you are going to get.
template <size_t Alignment, class Alloc>
void* Allocate(Alloc* alloc, size_t n) {
static_assert(Alignment > 0, "");
assert(n && "n must be positive");
using M = AlignedType<Alignment>;
using A = typename absl::allocator_traits<Alloc>::template rebind_alloc<M>;
using AT = typename absl::allocator_traits<Alloc>::template rebind_traits<M>;
// On macOS, "mem_alloc" is a #define with one argument defined in
// rpc/types.h, so we can't name the variable "mem_alloc" and initialize it
// with the "foo(bar)" syntax.
A my_mem_alloc(*alloc);
void* p = AT::allocate(my_mem_alloc, (n + sizeof(M) - 1) / sizeof(M));
assert(reinterpret_cast<uintptr_t>(p) % Alignment == 0 &&
"allocator does not respect alignment");
return p;
}
// Returns true if the destruction of the value with given Allocator will be
// trivial.
template <class Allocator, class ValueType>
constexpr auto IsDestructionTrivial() {
constexpr bool result =
std::is_trivially_destructible<ValueType>::value &&
std::is_same<typename absl::allocator_traits<
Allocator>::template rebind_alloc<char>,
std::allocator<char>>::value;
return std::integral_constant<bool, result>();
}
// The pointer must have been previously obtained by calling
// Allocate<Alignment>(alloc, n).
template <size_t Alignment, class Alloc>
void Deallocate(Alloc* alloc, void* p, size_t n) {
static_assert(Alignment > 0, "");
assert(n && "n must be positive");
using M = AlignedType<Alignment>;
using A = typename absl::allocator_traits<Alloc>::template rebind_alloc<M>;
using AT = typename absl::allocator_traits<Alloc>::template rebind_traits<M>;
// On macOS, "mem_alloc" is a #define with one argument defined in
// rpc/types.h, so we can't name the variable "mem_alloc" and initialize it
// with the "foo(bar)" syntax.
A my_mem_alloc(*alloc);
AT::deallocate(my_mem_alloc, static_cast<M*>(p),
(n + sizeof(M) - 1) / sizeof(M));
}
namespace memory_internal {
// Constructs T into uninitialized storage pointed by `ptr` using the args
// specified in the tuple.
template <class Alloc, class T, class Tuple, size_t... I>
void ConstructFromTupleImpl(Alloc* alloc, T* ptr, Tuple&& t,
absl::index_sequence<I...>) {
absl::allocator_traits<Alloc>::construct(
*alloc, ptr, std::get<I>(std::forward<Tuple>(t))...);
}
template <class T, class F>
struct WithConstructedImplF {
template <class... Args>
decltype(std::declval<F>()(std::declval<T>())) operator()(
Args&&... args) const {
return std::forward<F>(f)(T(std::forward<Args>(args)...));
}
F&& f;
};
template <class T, class Tuple, size_t... Is, class F>
decltype(std::declval<F>()(std::declval<T>())) WithConstructedImpl(
Tuple&& t, absl::index_sequence<Is...>, F&& f) {
return WithConstructedImplF<T, F>{std::forward<F>(f)}(
std::get<Is>(std::forward<Tuple>(t))...);
}
template <class T, size_t... Is>
auto TupleRefImpl(T&& t, absl::index_sequence<Is...>)
-> decltype(std::forward_as_tuple(std::get<Is>(std::forward<T>(t))...)) {
return std::forward_as_tuple(std::get<Is>(std::forward<T>(t))...);
}
// Returns a tuple of references to the elements of the input tuple. T must be a
// tuple.
template <class T>
auto TupleRef(T&& t) -> decltype(TupleRefImpl(
std::forward<T>(t),
absl::make_index_sequence<
std::tuple_size<typename std::decay<T>::type>::value>())) {
return TupleRefImpl(
std::forward<T>(t),
absl::make_index_sequence<
std::tuple_size<typename std::decay<T>::type>::value>());
}
template <class F, class K, class V>
decltype(std::declval<F>()(std::declval<const K&>(), std::piecewise_construct,
std::declval<std::tuple<K>>(), std::declval<V>()))
DecomposePairImpl(F&& f, std::pair<std::tuple<K>, V> p) {
const auto& key = std::get<0>(p.first);
return std::forward<F>(f)(key, std::piecewise_construct, std::move(p.first),
std::move(p.second));
}
} // namespace memory_internal
// Constructs T into uninitialized storage pointed by `ptr` using the args
// specified in the tuple.
template <class Alloc, class T, class Tuple>
void ConstructFromTuple(Alloc* alloc, T* ptr, Tuple&& t) {
memory_internal::ConstructFromTupleImpl(
alloc, ptr, std::forward<Tuple>(t),
absl::make_index_sequence<
std::tuple_size<typename std::decay<Tuple>::type>::value>());
}
// Constructs T using the args specified in the tuple and calls F with the
// constructed value.
template <class T, class Tuple, class F>
decltype(std::declval<F>()(std::declval<T>())) WithConstructed(Tuple&& t,
F&& f) {
return memory_internal::WithConstructedImpl<T>(
std::forward<Tuple>(t),
absl::make_index_sequence<
std::tuple_size<typename std::decay<Tuple>::type>::value>(),
std::forward<F>(f));
}
// Given arguments of an std::pair's constructor, PairArgs() returns a pair of
// tuples with references to the passed arguments. The tuples contain
// constructor arguments for the first and the second elements of the pair.
//
// The following two snippets are equivalent.
//
// 1. std::pair<F, S> p(args...);
//
// 2. auto a = PairArgs(args...);
// std::pair<F, S> p(std::piecewise_construct,
// std::move(a.first), std::move(a.second));
inline std::pair<std::tuple<>, std::tuple<>> PairArgs() { return {}; }
template <class F, class S>
std::pair<std::tuple<F&&>, std::tuple<S&&>> PairArgs(F&& f, S&& s) {
return {std::piecewise_construct, std::forward_as_tuple(std::forward<F>(f)),
std::forward_as_tuple(std::forward<S>(s))};
}
template <class F, class S>
std::pair<std::tuple<const F&>, std::tuple<const S&>> PairArgs(
const std::pair<F, S>& p) {
return PairArgs(p.first, p.second);
}
template <class F, class S>
std::pair<std::tuple<F&&>, std::tuple<S&&>> PairArgs(std::pair<F, S>&& p) {
return PairArgs(std::forward<F>(p.first), std::forward<S>(p.second));
}
template <class F, class S>
auto PairArgs(std::piecewise_construct_t, F&& f, S&& s)
-> decltype(std::make_pair(memory_internal::TupleRef(std::forward<F>(f)),
memory_internal::TupleRef(std::forward<S>(s)))) {
return std::make_pair(memory_internal::TupleRef(std::forward<F>(f)),
memory_internal::TupleRef(std::forward<S>(s)));
}
// A helper function for implementing apply() in map policies.
template <class F, class... Args>
auto DecomposePair(F&& f, Args&&... args)
-> decltype(memory_internal::DecomposePairImpl(
std::forward<F>(f), PairArgs(std::forward<Args>(args)...))) {
return memory_internal::DecomposePairImpl(
std::forward<F>(f), PairArgs(std::forward<Args>(args)...));
}
// A helper function for implementing apply() in set policies.
template <class F, class Arg>
decltype(std::declval<F>()(std::declval<const Arg&>(), std::declval<Arg>()))
DecomposeValue(F&& f, Arg&& arg) {
const auto& key = arg;
return std::forward<F>(f)(key, std::forward<Arg>(arg));
}
// Helper functions for asan and msan.
inline void SanitizerPoisonMemoryRegion(const void* m, size_t s) {
#ifdef ABSL_HAVE_ADDRESS_SANITIZER
ASAN_POISON_MEMORY_REGION(m, s);
#endif
#ifdef ABSL_HAVE_MEMORY_SANITIZER
__msan_poison(m, s);
#endif
(void)m;
(void)s;
}
inline void SanitizerUnpoisonMemoryRegion(const void* m, size_t s) {
#ifdef ABSL_HAVE_ADDRESS_SANITIZER
ASAN_UNPOISON_MEMORY_REGION(m, s);
#endif
#ifdef ABSL_HAVE_MEMORY_SANITIZER
__msan_unpoison(m, s);
#endif
(void)m;
(void)s;
}
template <typename T>
inline void SanitizerPoisonObject(const T* object) {
SanitizerPoisonMemoryRegion(object, sizeof(T));
}
template <typename T>
inline void SanitizerUnpoisonObject(const T* object) {
SanitizerUnpoisonMemoryRegion(object, sizeof(T));
}
namespace memory_internal {
// If Pair is a standard-layout type, OffsetOf<Pair>::kFirst and
// OffsetOf<Pair>::kSecond are equivalent to offsetof(Pair, first) and
// offsetof(Pair, second) respectively. Otherwise they are -1.
//
// The purpose of OffsetOf is to avoid calling offsetof() on non-standard-layout
// type, which is non-portable.
template <class Pair, class = std::true_type>
struct OffsetOf {
static constexpr size_t kFirst = static_cast<size_t>(-1);
static constexpr size_t kSecond = static_cast<size_t>(-1);
};
template <class Pair>
struct OffsetOf<Pair, typename std::is_standard_layout<Pair>::type> {
static constexpr size_t kFirst = offsetof(Pair, first);
static constexpr size_t kSecond = offsetof(Pair, second);
};
template <class K, class V>
struct IsLayoutCompatible {
private:
struct Pair {
K first;
V second;
};
// Is P layout-compatible with Pair?
template <class P>
static constexpr bool LayoutCompatible() {
return std::is_standard_layout<P>() && sizeof(P) == sizeof(Pair) &&
alignof(P) == alignof(Pair) &&
memory_internal::OffsetOf<P>::kFirst ==
memory_internal::OffsetOf<Pair>::kFirst &&
memory_internal::OffsetOf<P>::kSecond ==
memory_internal::OffsetOf<Pair>::kSecond;
}
public:
// Whether pair<const K, V> and pair<K, V> are layout-compatible. If they are,
// then it is safe to store them in a union and read from either.
static constexpr bool value = std::is_standard_layout<K>() &&
std::is_standard_layout<Pair>() &&
memory_internal::OffsetOf<Pair>::kFirst == 0 &&
LayoutCompatible<std::pair<K, V>>() &&
LayoutCompatible<std::pair<const K, V>>();
};
} // namespace memory_internal
// The internal storage type for key-value containers like flat_hash_map.
//
// It is convenient for the value_type of a flat_hash_map<K, V> to be
// pair<const K, V>; the "const K" prevents accidental modification of the key
// when dealing with the reference returned from find() and similar methods.
// However, this creates other problems; we want to be able to emplace(K, V)
// efficiently with move operations, and similarly be able to move a
// pair<K, V> in insert().
//
// The solution is this union, which aliases the const and non-const versions
// of the pair. This also allows flat_hash_map<const K, V> to work, even though
// that has the same efficiency issues with move in emplace() and insert() -
// but people do it anyway.
//
// If kMutableKeys is false, only the value member can be accessed.
//
// If kMutableKeys is true, key can be accessed through all slots while value
// and mutable_value must be accessed only via INITIALIZED slots. Slots are
// created and destroyed via mutable_value so that the key can be moved later.
//
// Accessing one of the union fields while the other is active is safe as
// long as they are layout-compatible, which is guaranteed by the definition of
// kMutableKeys. For C++11, the relevant section of the standard is
// https://timsong-cpp.github.io/cppwp/n3337/class.mem#19 (9.2.19)
template <class K, class V>
union map_slot_type {
map_slot_type() {}
~map_slot_type() = delete;
using value_type = std::pair<const K, V>;
using mutable_value_type =
std::pair<absl::remove_const_t<K>, absl::remove_const_t<V>>;
value_type value;
mutable_value_type mutable_value;
absl::remove_const_t<K> key;
};
template <class K, class V>
struct map_slot_policy {
using slot_type = map_slot_type<K, V>;
using value_type = std::pair<const K, V>;
using mutable_value_type =
std::pair<absl::remove_const_t<K>, absl::remove_const_t<V>>;
private:
static void emplace(slot_type* slot) {
// The construction of union doesn't do anything at runtime but it allows us
// to access its members without violating aliasing rules.
new (slot) slot_type;
}
// If pair<const K, V> and pair<K, V> are layout-compatible, we can accept one
// or the other via slot_type. We are also free to access the key via
// slot_type::key in this case.
using kMutableKeys = memory_internal::IsLayoutCompatible<K, V>;
public:
static value_type& element(slot_type* slot) { return slot->value; }
static const value_type& element(const slot_type* slot) {
return slot->value;
}
// When C++17 is available, we can use std::launder to provide mutable
// access to the key for use in node handle.
#if defined(__cpp_lib_launder) && __cpp_lib_launder >= 201606
static K& mutable_key(slot_type* slot) {
// Still check for kMutableKeys so that we can avoid calling std::launder
// unless necessary because it can interfere with optimizations.
return kMutableKeys::value ? slot->key
: *std::launder(const_cast<K*>(
std::addressof(slot->value.first)));
}
#else // !(defined(__cpp_lib_launder) && __cpp_lib_launder >= 201606)
static const K& mutable_key(slot_type* slot) { return key(slot); }
#endif
static const K& key(const slot_type* slot) {
return kMutableKeys::value ? slot->key : slot->value.first;
}
template <class Allocator, class... Args>
static void construct(Allocator* alloc, slot_type* slot, Args&&... args) {
emplace(slot);
if (kMutableKeys::value) {
absl::allocator_traits<Allocator>::construct(*alloc, &slot->mutable_value,
std::forward<Args>(args)...);
} else {
absl::allocator_traits<Allocator>::construct(*alloc, &slot->value,
std::forward<Args>(args)...);
}
}
// Construct this slot by moving from another slot.
template <class Allocator>
static void construct(Allocator* alloc, slot_type* slot, slot_type* other) {
emplace(slot);
if (kMutableKeys::value) {
absl::allocator_traits<Allocator>::construct(
*alloc, &slot->mutable_value, std::move(other->mutable_value));
} else {
absl::allocator_traits<Allocator>::construct(*alloc, &slot->value,
std::move(other->value));
}
}
// Construct this slot by copying from another slot.
template <class Allocator>
static void construct(Allocator* alloc, slot_type* slot,
const slot_type* other) {
emplace(slot);
absl::allocator_traits<Allocator>::construct(*alloc, &slot->value,
other->value);
}
template <class Allocator>
static auto destroy(Allocator* alloc, slot_type* slot) {
if (kMutableKeys::value) {
absl::allocator_traits<Allocator>::destroy(*alloc, &slot->mutable_value);
} else {
absl::allocator_traits<Allocator>::destroy(*alloc, &slot->value);
}
return IsDestructionTrivial<Allocator, value_type>();
}
template <class Allocator>
static auto transfer(Allocator* alloc, slot_type* new_slot,
slot_type* old_slot) {
auto is_relocatable =
typename absl::is_trivially_relocatable<value_type>::type();
emplace(new_slot);
#if defined(__cpp_lib_launder) && __cpp_lib_launder >= 201606
if (is_relocatable) {
// TODO(b/247130232,b/251814870): remove casts after fixing warnings.
std::memcpy(static_cast<void*>(std::launder(&new_slot->value)),
static_cast<const void*>(&old_slot->value),
sizeof(value_type));
return is_relocatable;
}
#endif
if (kMutableKeys::value) {
absl::allocator_traits<Allocator>::construct(
*alloc, &new_slot->mutable_value, std::move(old_slot->mutable_value));
} else {
absl::allocator_traits<Allocator>::construct(*alloc, &new_slot->value,
std::move(old_slot->value));
}
destroy(alloc, old_slot);
return is_relocatable;
}
};
// Type erased function for computing hash of the slot.
using HashSlotFn = size_t (*)(const void* hash_fn, void* slot);
// Type erased function to apply `Fn` to data inside of the `slot`.
// The data is expected to have type `T`.
template <class Fn, class T>
size_t TypeErasedApplyToSlotFn(const void* fn, void* slot) {
const auto* f = static_cast<const Fn*>(fn);
return (*f)(*static_cast<const T*>(slot));
}
// Type erased function to apply `Fn` to data inside of the `*slot_ptr`.
// The data is expected to have type `T`.
template <class Fn, class T>
size_t TypeErasedDerefAndApplyToSlotFn(const void* fn, void* slot_ptr) {
const auto* f = static_cast<const Fn*>(fn);
const T* slot = *static_cast<const T**>(slot_ptr);
return (*f)(*slot);
}
} // namespace container_internal
ABSL_NAMESPACE_END
} // namespace absl
#endif // ABSL_CONTAINER_INTERNAL_CONTAINER_MEMORY_H_

View file

@ -0,0 +1,318 @@
// Copyright 2018 The Abseil Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "absl/container/internal/container_memory.h"
#include <cstddef>
#include <cstdint>
#include <memory>
#include <tuple>
#include <type_traits>
#include <typeindex>
#include <typeinfo>
#include <utility>
#include "gmock/gmock.h"
#include "gtest/gtest.h"
#include "absl/base/no_destructor.h"
#include "absl/container/internal/test_instance_tracker.h"
#include "absl/meta/type_traits.h"
#include "absl/strings/string_view.h"
namespace absl {
ABSL_NAMESPACE_BEGIN
namespace container_internal {
namespace {
using ::absl::test_internal::CopyableMovableInstance;
using ::absl::test_internal::InstanceTracker;
using ::testing::_;
using ::testing::ElementsAre;
using ::testing::Gt;
using ::testing::Pair;
TEST(Memory, AlignmentLargerThanBase) {
std::allocator<int8_t> alloc;
void* mem = Allocate<2>(&alloc, 3);
EXPECT_EQ(0, reinterpret_cast<uintptr_t>(mem) % 2);
memcpy(mem, "abc", 3);
Deallocate<2>(&alloc, mem, 3);
}
TEST(Memory, AlignmentSmallerThanBase) {
std::allocator<int64_t> alloc;
void* mem = Allocate<2>(&alloc, 3);
EXPECT_EQ(0, reinterpret_cast<uintptr_t>(mem) % 2);
memcpy(mem, "abc", 3);
Deallocate<2>(&alloc, mem, 3);
}
std::map<std::type_index, int>& AllocationMap() {
static absl::NoDestructor<std::map<std::type_index, int>> map;
return *map;
}
template <typename T>
struct TypeCountingAllocator {
TypeCountingAllocator() = default;
template <typename U>
TypeCountingAllocator(const TypeCountingAllocator<U>&) {} // NOLINT
using value_type = T;
T* allocate(size_t n, const void* = nullptr) {
AllocationMap()[typeid(T)] += n;
return std::allocator<T>().allocate(n);
}
void deallocate(T* p, std::size_t n) {
AllocationMap()[typeid(T)] -= n;
return std::allocator<T>().deallocate(p, n);
}
};
TEST(Memory, AllocateDeallocateMatchType) {
TypeCountingAllocator<int> alloc;
void* mem = Allocate<1>(&alloc, 1);
// Verify that it was allocated
EXPECT_THAT(AllocationMap(), ElementsAre(Pair(_, Gt(0))));
Deallocate<1>(&alloc, mem, 1);
// Verify that the deallocation matched.
EXPECT_THAT(AllocationMap(), ElementsAre(Pair(_, 0)));
}
class Fixture : public ::testing::Test {
using Alloc = std::allocator<std::string>;
public:
Fixture() { ptr_ = std::allocator_traits<Alloc>::allocate(*alloc(), 1); }
~Fixture() override {
std::allocator_traits<Alloc>::destroy(*alloc(), ptr_);
std::allocator_traits<Alloc>::deallocate(*alloc(), ptr_, 1);
}
std::string* ptr() { return ptr_; }
Alloc* alloc() { return &alloc_; }
private:
Alloc alloc_;
std::string* ptr_;
};
TEST_F(Fixture, ConstructNoArgs) {
ConstructFromTuple(alloc(), ptr(), std::forward_as_tuple());
EXPECT_EQ(*ptr(), "");
}
TEST_F(Fixture, ConstructOneArg) {
ConstructFromTuple(alloc(), ptr(), std::forward_as_tuple("abcde"));
EXPECT_EQ(*ptr(), "abcde");
}
TEST_F(Fixture, ConstructTwoArg) {
ConstructFromTuple(alloc(), ptr(), std::forward_as_tuple(5, 'a'));
EXPECT_EQ(*ptr(), "aaaaa");
}
TEST(PairArgs, NoArgs) {
EXPECT_THAT(PairArgs(),
Pair(std::forward_as_tuple(), std::forward_as_tuple()));
}
TEST(PairArgs, TwoArgs) {
EXPECT_EQ(
std::make_pair(std::forward_as_tuple(1), std::forward_as_tuple('A')),
PairArgs(1, 'A'));
}
TEST(PairArgs, Pair) {
EXPECT_EQ(
std::make_pair(std::forward_as_tuple(1), std::forward_as_tuple('A')),
PairArgs(std::make_pair(1, 'A')));
}
TEST(PairArgs, Piecewise) {
EXPECT_EQ(
std::make_pair(std::forward_as_tuple(1), std::forward_as_tuple('A')),
PairArgs(std::piecewise_construct, std::forward_as_tuple(1),
std::forward_as_tuple('A')));
}
TEST(WithConstructed, Simple) {
EXPECT_EQ(1, WithConstructed<absl::string_view>(
std::make_tuple(std::string("a")),
[](absl::string_view str) { return str.size(); }));
}
template <class F, class Arg>
decltype(DecomposeValue(std::declval<F>(), std::declval<Arg>()))
DecomposeValueImpl(int, F&& f, Arg&& arg) {
return DecomposeValue(std::forward<F>(f), std::forward<Arg>(arg));
}
template <class F, class Arg>
const char* DecomposeValueImpl(char, F&& f, Arg&& arg) {
return "not decomposable";
}
template <class F, class Arg>
decltype(DecomposeValueImpl(0, std::declval<F>(), std::declval<Arg>()))
TryDecomposeValue(F&& f, Arg&& arg) {
return DecomposeValueImpl(0, std::forward<F>(f), std::forward<Arg>(arg));
}
TEST(DecomposeValue, Decomposable) {
auto f = [](const int& x, int&& y) { // NOLINT
EXPECT_EQ(&x, &y);
EXPECT_EQ(42, x);
return 'A';
};
EXPECT_EQ('A', TryDecomposeValue(f, 42));
}
TEST(DecomposeValue, NotDecomposable) {
auto f = [](void*) {
ADD_FAILURE() << "Must not be called";
return 'A';
};
EXPECT_STREQ("not decomposable", TryDecomposeValue(f, 42));
}
template <class F, class... Args>
decltype(DecomposePair(std::declval<F>(), std::declval<Args>()...))
DecomposePairImpl(int, F&& f, Args&&... args) {
return DecomposePair(std::forward<F>(f), std::forward<Args>(args)...);
}
template <class F, class... Args>
const char* DecomposePairImpl(char, F&& f, Args&&... args) {
return "not decomposable";
}
template <class F, class... Args>
decltype(DecomposePairImpl(0, std::declval<F>(), std::declval<Args>()...))
TryDecomposePair(F&& f, Args&&... args) {
return DecomposePairImpl(0, std::forward<F>(f), std::forward<Args>(args)...);
}
TEST(DecomposePair, Decomposable) {
auto f = [](const int& x, // NOLINT
std::piecewise_construct_t, std::tuple<int&&> k,
std::tuple<double>&& v) {
EXPECT_EQ(&x, &std::get<0>(k));
EXPECT_EQ(42, x);
EXPECT_EQ(0.5, std::get<0>(v));
return 'A';
};
EXPECT_EQ('A', TryDecomposePair(f, 42, 0.5));
EXPECT_EQ('A', TryDecomposePair(f, std::make_pair(42, 0.5)));
EXPECT_EQ('A', TryDecomposePair(f, std::piecewise_construct,
std::make_tuple(42), std::make_tuple(0.5)));
}
TEST(DecomposePair, NotDecomposable) {
auto f = [](...) {
ADD_FAILURE() << "Must not be called";
return 'A';
};
EXPECT_STREQ("not decomposable", TryDecomposePair(f));
EXPECT_STREQ("not decomposable",
TryDecomposePair(f, std::piecewise_construct, std::make_tuple(),
std::make_tuple(0.5)));
}
TEST(MapSlotPolicy, ConstKeyAndValue) {
using slot_policy = map_slot_policy<const CopyableMovableInstance,
const CopyableMovableInstance>;
using slot_type = typename slot_policy::slot_type;
union Slots {
Slots() {}
~Slots() {}
slot_type slots[100];
} slots;
std::allocator<
std::pair<const CopyableMovableInstance, const CopyableMovableInstance>>
alloc;
InstanceTracker tracker;
slot_policy::construct(&alloc, &slots.slots[0], CopyableMovableInstance(1),
CopyableMovableInstance(1));
for (int i = 0; i < 99; ++i) {
slot_policy::transfer(&alloc, &slots.slots[i + 1], &slots.slots[i]);
}
slot_policy::destroy(&alloc, &slots.slots[99]);
EXPECT_EQ(tracker.copies(), 0);
}
TEST(MapSlotPolicy, TransferReturnsTrue) {
{
using slot_policy = map_slot_policy<int, float>;
EXPECT_TRUE(
(std::is_same<decltype(slot_policy::transfer<std::allocator<char>>(
nullptr, nullptr, nullptr)),
std::true_type>::value));
}
{
struct NonRelocatable {
NonRelocatable() = default;
NonRelocatable(NonRelocatable&&) {}
NonRelocatable& operator=(NonRelocatable&&) { return *this; }
void* self = nullptr;
};
EXPECT_FALSE(absl::is_trivially_relocatable<NonRelocatable>::value);
using slot_policy = map_slot_policy<int, NonRelocatable>;
EXPECT_TRUE(
(std::is_same<decltype(slot_policy::transfer<std::allocator<char>>(
nullptr, nullptr, nullptr)),
std::false_type>::value));
}
}
TEST(MapSlotPolicy, DestroyReturnsTrue) {
{
using slot_policy = map_slot_policy<int, float>;
EXPECT_TRUE(
(std::is_same<decltype(slot_policy::destroy<std::allocator<char>>(
nullptr, nullptr)),
std::true_type>::value));
}
{
EXPECT_FALSE(std::is_trivially_destructible<std::unique_ptr<int>>::value);
using slot_policy = map_slot_policy<int, std::unique_ptr<int>>;
EXPECT_TRUE(
(std::is_same<decltype(slot_policy::destroy<std::allocator<char>>(
nullptr, nullptr)),
std::false_type>::value));
}
}
TEST(ApplyTest, TypeErasedApplyToSlotFn) {
size_t x = 7;
auto fn = [](size_t v) { return v * 2; };
EXPECT_EQ((TypeErasedApplyToSlotFn<decltype(fn), size_t>(&fn, &x)), 14);
}
TEST(ApplyTest, TypeErasedDerefAndApplyToSlotFn) {
size_t x = 7;
auto fn = [](size_t v) { return v * 2; };
size_t* x_ptr = &x;
EXPECT_EQ(
(TypeErasedDerefAndApplyToSlotFn<decltype(fn), size_t>(&fn, &x_ptr)), 14);
}
} // namespace
} // namespace container_internal
ABSL_NAMESPACE_END
} // namespace absl

View file

@ -0,0 +1,276 @@
// Copyright 2018 The Abseil Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// Define the default Hash and Eq functions for SwissTable containers.
//
// std::hash<T> and std::equal_to<T> are not appropriate hash and equal
// functions for SwissTable containers. There are two reasons for this.
//
// SwissTable containers are power of 2 sized containers:
//
// This means they use the lower bits of the hash value to find the slot for
// each entry. The typical hash function for integral types is the identity.
// This is a very weak hash function for SwissTable and any power of 2 sized
// hashtable implementation which will lead to excessive collisions. For
// SwissTable we use murmur3 style mixing to reduce collisions to a minimum.
//
// SwissTable containers support heterogeneous lookup:
//
// In order to make heterogeneous lookup work, hash and equal functions must be
// polymorphic. At the same time they have to satisfy the same requirements the
// C++ standard imposes on hash functions and equality operators. That is:
//
// if hash_default_eq<T>(a, b) returns true for any a and b of type T, then
// hash_default_hash<T>(a) must equal hash_default_hash<T>(b)
//
// For SwissTable containers this requirement is relaxed to allow a and b of
// any and possibly different types. Note that like the standard the hash and
// equal functions are still bound to T. This is important because some type U
// can be hashed by/tested for equality differently depending on T. A notable
// example is `const char*`. `const char*` is treated as a c-style string when
// the hash function is hash<std::string> but as a pointer when the hash
// function is hash<void*>.
//
#ifndef ABSL_CONTAINER_INTERNAL_HASH_FUNCTION_DEFAULTS_H_
#define ABSL_CONTAINER_INTERNAL_HASH_FUNCTION_DEFAULTS_H_
#include <cstddef>
#include <functional>
#include <memory>
#include <string>
#include <type_traits>
#include "absl/base/config.h"
#include "absl/container/internal/common.h"
#include "absl/hash/hash.h"
#include "absl/meta/type_traits.h"
#include "absl/strings/cord.h"
#include "absl/strings/string_view.h"
#ifdef ABSL_HAVE_STD_STRING_VIEW
#include <string_view>
#endif
namespace absl {
ABSL_NAMESPACE_BEGIN
namespace container_internal {
// The hash of an object of type T is computed by using absl::Hash.
template <class T, class E = void>
struct HashEq {
using Hash = absl::Hash<T>;
using Eq = std::equal_to<T>;
};
struct StringHash {
using is_transparent = void;
size_t operator()(absl::string_view v) const {
return absl::Hash<absl::string_view>{}(v);
}
size_t operator()(const absl::Cord& v) const {
return absl::Hash<absl::Cord>{}(v);
}
};
struct StringEq {
using is_transparent = void;
bool operator()(absl::string_view lhs, absl::string_view rhs) const {
return lhs == rhs;
}
bool operator()(const absl::Cord& lhs, const absl::Cord& rhs) const {
return lhs == rhs;
}
bool operator()(const absl::Cord& lhs, absl::string_view rhs) const {
return lhs == rhs;
}
bool operator()(absl::string_view lhs, const absl::Cord& rhs) const {
return lhs == rhs;
}
};
// Supports heterogeneous lookup for string-like elements.
struct StringHashEq {
using Hash = StringHash;
using Eq = StringEq;
};
template <>
struct HashEq<std::string> : StringHashEq {};
template <>
struct HashEq<absl::string_view> : StringHashEq {};
template <>
struct HashEq<absl::Cord> : StringHashEq {};
#ifdef ABSL_HAVE_STD_STRING_VIEW
template <typename TChar>
struct BasicStringHash {
using is_transparent = void;
size_t operator()(std::basic_string_view<TChar> v) const {
return absl::Hash<std::basic_string_view<TChar>>{}(v);
}
};
template <typename TChar>
struct BasicStringEq {
using is_transparent = void;
bool operator()(std::basic_string_view<TChar> lhs,
std::basic_string_view<TChar> rhs) const {
return lhs == rhs;
}
};
// Supports heterogeneous lookup for w/u16/u32 string + string_view + char*.
template <typename TChar>
struct BasicStringHashEq {
using Hash = BasicStringHash<TChar>;
using Eq = BasicStringEq<TChar>;
};
template <>
struct HashEq<std::wstring> : BasicStringHashEq<wchar_t> {};
template <>
struct HashEq<std::wstring_view> : BasicStringHashEq<wchar_t> {};
template <>
struct HashEq<std::u16string> : BasicStringHashEq<char16_t> {};
template <>
struct HashEq<std::u16string_view> : BasicStringHashEq<char16_t> {};
template <>
struct HashEq<std::u32string> : BasicStringHashEq<char32_t> {};
template <>
struct HashEq<std::u32string_view> : BasicStringHashEq<char32_t> {};
#endif // ABSL_HAVE_STD_STRING_VIEW
// Supports heterogeneous lookup for pointers and smart pointers.
template <class T>
struct HashEq<T*> {
struct Hash {
using is_transparent = void;
template <class U>
size_t operator()(const U& ptr) const {
return absl::Hash<const T*>{}(HashEq::ToPtr(ptr));
}
};
struct Eq {
using is_transparent = void;
template <class A, class B>
bool operator()(const A& a, const B& b) const {
return HashEq::ToPtr(a) == HashEq::ToPtr(b);
}
};
private:
static const T* ToPtr(const T* ptr) { return ptr; }
template <class U, class D>
static const T* ToPtr(const std::unique_ptr<U, D>& ptr) {
return ptr.get();
}
template <class U>
static const T* ToPtr(const std::shared_ptr<U>& ptr) {
return ptr.get();
}
};
template <class T, class D>
struct HashEq<std::unique_ptr<T, D>> : HashEq<T*> {};
template <class T>
struct HashEq<std::shared_ptr<T>> : HashEq<T*> {};
template <typename T, typename E = void>
struct HasAbslContainerHash : std::false_type {};
template <typename T>
struct HasAbslContainerHash<T, absl::void_t<typename T::absl_container_hash>>
: std::true_type {};
template <typename T, typename E = void>
struct HasAbslContainerEq : std::false_type {};
template <typename T>
struct HasAbslContainerEq<T, absl::void_t<typename T::absl_container_eq>>
: std::true_type {};
template <typename T, typename E = void>
struct AbslContainerEq {
using type = std::equal_to<>;
};
template <typename T>
struct AbslContainerEq<
T, typename std::enable_if_t<HasAbslContainerEq<T>::value>> {
using type = typename T::absl_container_eq;
};
template <typename T, typename E = void>
struct AbslContainerHash {
using type = void;
};
template <typename T>
struct AbslContainerHash<
T, typename std::enable_if_t<HasAbslContainerHash<T>::value>> {
using type = typename T::absl_container_hash;
};
// HashEq specialization for user types that provide `absl_container_hash` and
// (optionally) `absl_container_eq`. This specialization allows user types to
// provide heterogeneous lookup without requiring to explicitly specify Hash/Eq
// type arguments in unordered Abseil containers.
//
// Both `absl_container_hash` and `absl_container_eq` should be transparent
// (have inner is_transparent type). While there is no technical reason to
// restrict to transparent-only types, there is also no feasible use case when
// it shouldn't be transparent - it is easier to relax the requirement later if
// such a case arises rather than restricting it.
//
// If type provides only `absl_container_hash` then `eq` part will be
// `std::equal_to<void>`.
//
// User types are not allowed to provide only a `Eq` part as there is no
// feasible use case for this behavior - if Hash should be a default one then Eq
// should be an equivalent to the `std::equal_to<T>`.
template <typename T>
struct HashEq<T, typename std::enable_if_t<HasAbslContainerHash<T>::value>> {
using Hash = typename AbslContainerHash<T>::type;
using Eq = typename AbslContainerEq<T>::type;
static_assert(IsTransparent<Hash>::value,
"absl_container_hash must be transparent. To achieve it add a "
"`using is_transparent = void;` clause to this type.");
static_assert(IsTransparent<Eq>::value,
"absl_container_eq must be transparent. To achieve it add a "
"`using is_transparent = void;` clause to this type.");
};
// This header's visibility is restricted. If you need to access the default
// hasher please use the container's ::hasher alias instead.
//
// Example: typename Hash = typename absl::flat_hash_map<K, V>::hasher
template <class T>
using hash_default_hash = typename container_internal::HashEq<T>::Hash;
// This header's visibility is restricted. If you need to access the default
// key equal please use the container's ::key_equal alias instead.
//
// Example: typename Eq = typename absl::flat_hash_map<K, V, Hash>::key_equal
template <class T>
using hash_default_eq = typename container_internal::HashEq<T>::Eq;
} // namespace container_internal
ABSL_NAMESPACE_END
} // namespace absl
#endif // ABSL_CONTAINER_INTERNAL_HASH_FUNCTION_DEFAULTS_H_

View file

@ -0,0 +1,684 @@
// Copyright 2018 The Abseil Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "absl/container/internal/hash_function_defaults.h"
#include <cstddef>
#include <functional>
#include <type_traits>
#include <utility>
#include "gtest/gtest.h"
#include "absl/container/flat_hash_map.h"
#include "absl/container/flat_hash_set.h"
#include "absl/hash/hash.h"
#include "absl/random/random.h"
#include "absl/strings/cord.h"
#include "absl/strings/cord_test_helpers.h"
#include "absl/strings/string_view.h"
#ifdef ABSL_HAVE_STD_STRING_VIEW
#include <string_view>
#endif
namespace absl {
ABSL_NAMESPACE_BEGIN
namespace container_internal {
namespace {
using ::testing::Types;
TEST(Eq, Int32) {
hash_default_eq<int32_t> eq;
EXPECT_TRUE(eq(1, 1u));
EXPECT_TRUE(eq(1, char{1}));
EXPECT_TRUE(eq(1, true));
EXPECT_TRUE(eq(1, double{1.1}));
EXPECT_FALSE(eq(1, char{2}));
EXPECT_FALSE(eq(1, 2u));
EXPECT_FALSE(eq(1, false));
EXPECT_FALSE(eq(1, 2.));
}
TEST(Hash, Int32) {
hash_default_hash<int32_t> hash;
auto h = hash(1);
EXPECT_EQ(h, hash(1u));
EXPECT_EQ(h, hash(char{1}));
EXPECT_EQ(h, hash(true));
EXPECT_EQ(h, hash(double{1.1}));
EXPECT_NE(h, hash(2u));
EXPECT_NE(h, hash(char{2}));
EXPECT_NE(h, hash(false));
EXPECT_NE(h, hash(2.));
}
enum class MyEnum { A, B, C, D };
TEST(Eq, Enum) {
hash_default_eq<MyEnum> eq;
EXPECT_TRUE(eq(MyEnum::A, MyEnum::A));
EXPECT_FALSE(eq(MyEnum::A, MyEnum::B));
}
TEST(Hash, Enum) {
hash_default_hash<MyEnum> hash;
for (MyEnum e : {MyEnum::A, MyEnum::B, MyEnum::C}) {
auto h = hash(e);
EXPECT_EQ(h, hash_default_hash<int>{}(static_cast<int>(e)));
EXPECT_NE(h, hash(MyEnum::D));
}
}
using StringTypes = ::testing::Types<std::string, absl::string_view>;
template <class T>
struct EqString : ::testing::Test {
hash_default_eq<T> key_eq;
};
TYPED_TEST_SUITE(EqString, StringTypes);
template <class T>
struct HashString : ::testing::Test {
hash_default_hash<T> hasher;
};
TYPED_TEST_SUITE(HashString, StringTypes);
TYPED_TEST(EqString, Works) {
auto eq = this->key_eq;
EXPECT_TRUE(eq("a", "a"));
EXPECT_TRUE(eq("a", absl::string_view("a")));
EXPECT_TRUE(eq("a", std::string("a")));
EXPECT_FALSE(eq("a", "b"));
EXPECT_FALSE(eq("a", absl::string_view("b")));
EXPECT_FALSE(eq("a", std::string("b")));
}
TYPED_TEST(HashString, Works) {
auto hash = this->hasher;
auto h = hash("a");
EXPECT_EQ(h, hash(absl::string_view("a")));
EXPECT_EQ(h, hash(std::string("a")));
EXPECT_NE(h, hash(absl::string_view("b")));
EXPECT_NE(h, hash(std::string("b")));
}
TEST(BasicStringViewTest, WStringEqWorks) {
#ifndef ABSL_HAVE_STD_STRING_VIEW
GTEST_SKIP();
#else
hash_default_eq<std::wstring> eq;
EXPECT_TRUE(eq(L"a", L"a"));
EXPECT_TRUE(eq(L"a", std::wstring_view(L"a")));
EXPECT_TRUE(eq(L"a", std::wstring(L"a")));
EXPECT_FALSE(eq(L"a", L"b"));
EXPECT_FALSE(eq(L"a", std::wstring_view(L"b")));
EXPECT_FALSE(eq(L"a", std::wstring(L"b")));
#endif
}
TEST(BasicStringViewTest, WStringViewEqWorks) {
#ifndef ABSL_HAVE_STD_STRING_VIEW
GTEST_SKIP();
#else
hash_default_eq<std::wstring_view> eq;
EXPECT_TRUE(eq(L"a", L"a"));
EXPECT_TRUE(eq(L"a", std::wstring_view(L"a")));
EXPECT_TRUE(eq(L"a", std::wstring(L"a")));
EXPECT_FALSE(eq(L"a", L"b"));
EXPECT_FALSE(eq(L"a", std::wstring_view(L"b")));
EXPECT_FALSE(eq(L"a", std::wstring(L"b")));
#endif
}
TEST(BasicStringViewTest, U16StringEqWorks) {
#ifndef ABSL_HAVE_STD_STRING_VIEW
GTEST_SKIP();
#else
hash_default_eq<std::u16string> eq;
EXPECT_TRUE(eq(u"a", u"a"));
EXPECT_TRUE(eq(u"a", std::u16string_view(u"a")));
EXPECT_TRUE(eq(u"a", std::u16string(u"a")));
EXPECT_FALSE(eq(u"a", u"b"));
EXPECT_FALSE(eq(u"a", std::u16string_view(u"b")));
EXPECT_FALSE(eq(u"a", std::u16string(u"b")));
#endif
}
TEST(BasicStringViewTest, U16StringViewEqWorks) {
#ifndef ABSL_HAVE_STD_STRING_VIEW
GTEST_SKIP();
#else
hash_default_eq<std::u16string_view> eq;
EXPECT_TRUE(eq(u"a", u"a"));
EXPECT_TRUE(eq(u"a", std::u16string_view(u"a")));
EXPECT_TRUE(eq(u"a", std::u16string(u"a")));
EXPECT_FALSE(eq(u"a", u"b"));
EXPECT_FALSE(eq(u"a", std::u16string_view(u"b")));
EXPECT_FALSE(eq(u"a", std::u16string(u"b")));
#endif
}
TEST(BasicStringViewTest, U32StringEqWorks) {
#ifndef ABSL_HAVE_STD_STRING_VIEW
GTEST_SKIP();
#else
hash_default_eq<std::u32string> eq;
EXPECT_TRUE(eq(U"a", U"a"));
EXPECT_TRUE(eq(U"a", std::u32string_view(U"a")));
EXPECT_TRUE(eq(U"a", std::u32string(U"a")));
EXPECT_FALSE(eq(U"a", U"b"));
EXPECT_FALSE(eq(U"a", std::u32string_view(U"b")));
EXPECT_FALSE(eq(U"a", std::u32string(U"b")));
#endif
}
TEST(BasicStringViewTest, U32StringViewEqWorks) {
#ifndef ABSL_HAVE_STD_STRING_VIEW
GTEST_SKIP();
#else
hash_default_eq<std::u32string_view> eq;
EXPECT_TRUE(eq(U"a", U"a"));
EXPECT_TRUE(eq(U"a", std::u32string_view(U"a")));
EXPECT_TRUE(eq(U"a", std::u32string(U"a")));
EXPECT_FALSE(eq(U"a", U"b"));
EXPECT_FALSE(eq(U"a", std::u32string_view(U"b")));
EXPECT_FALSE(eq(U"a", std::u32string(U"b")));
#endif
}
TEST(BasicStringViewTest, WStringHashWorks) {
#ifndef ABSL_HAVE_STD_STRING_VIEW
GTEST_SKIP();
#else
hash_default_hash<std::wstring> hash;
auto h = hash(L"a");
EXPECT_EQ(h, hash(std::wstring_view(L"a")));
EXPECT_EQ(h, hash(std::wstring(L"a")));
EXPECT_NE(h, hash(std::wstring_view(L"b")));
EXPECT_NE(h, hash(std::wstring(L"b")));
#endif
}
TEST(BasicStringViewTest, WStringViewHashWorks) {
#ifndef ABSL_HAVE_STD_STRING_VIEW
GTEST_SKIP();
#else
hash_default_hash<std::wstring_view> hash;
auto h = hash(L"a");
EXPECT_EQ(h, hash(std::wstring_view(L"a")));
EXPECT_EQ(h, hash(std::wstring(L"a")));
EXPECT_NE(h, hash(std::wstring_view(L"b")));
EXPECT_NE(h, hash(std::wstring(L"b")));
#endif
}
TEST(BasicStringViewTest, U16StringHashWorks) {
#ifndef ABSL_HAVE_STD_STRING_VIEW
GTEST_SKIP();
#else
hash_default_hash<std::u16string> hash;
auto h = hash(u"a");
EXPECT_EQ(h, hash(std::u16string_view(u"a")));
EXPECT_EQ(h, hash(std::u16string(u"a")));
EXPECT_NE(h, hash(std::u16string_view(u"b")));
EXPECT_NE(h, hash(std::u16string(u"b")));
#endif
}
TEST(BasicStringViewTest, U16StringViewHashWorks) {
#ifndef ABSL_HAVE_STD_STRING_VIEW
GTEST_SKIP();
#else
hash_default_hash<std::u16string_view> hash;
auto h = hash(u"a");
EXPECT_EQ(h, hash(std::u16string_view(u"a")));
EXPECT_EQ(h, hash(std::u16string(u"a")));
EXPECT_NE(h, hash(std::u16string_view(u"b")));
EXPECT_NE(h, hash(std::u16string(u"b")));
#endif
}
TEST(BasicStringViewTest, U32StringHashWorks) {
#ifndef ABSL_HAVE_STD_STRING_VIEW
GTEST_SKIP();
#else
hash_default_hash<std::u32string> hash;
auto h = hash(U"a");
EXPECT_EQ(h, hash(std::u32string_view(U"a")));
EXPECT_EQ(h, hash(std::u32string(U"a")));
EXPECT_NE(h, hash(std::u32string_view(U"b")));
EXPECT_NE(h, hash(std::u32string(U"b")));
#endif
}
TEST(BasicStringViewTest, U32StringViewHashWorks) {
#ifndef ABSL_HAVE_STD_STRING_VIEW
GTEST_SKIP();
#else
hash_default_hash<std::u32string_view> hash;
auto h = hash(U"a");
EXPECT_EQ(h, hash(std::u32string_view(U"a")));
EXPECT_EQ(h, hash(std::u32string(U"a")));
EXPECT_NE(h, hash(std::u32string_view(U"b")));
EXPECT_NE(h, hash(std::u32string(U"b")));
#endif
}
struct NoDeleter {
template <class T>
void operator()(const T* ptr) const {}
};
using PointerTypes =
::testing::Types<const int*, int*, std::unique_ptr<const int>,
std::unique_ptr<const int, NoDeleter>,
std::unique_ptr<int>, std::unique_ptr<int, NoDeleter>,
std::shared_ptr<const int>, std::shared_ptr<int>>;
template <class T>
struct EqPointer : ::testing::Test {
hash_default_eq<T> key_eq;
};
TYPED_TEST_SUITE(EqPointer, PointerTypes);
template <class T>
struct HashPointer : ::testing::Test {
hash_default_hash<T> hasher;
};
TYPED_TEST_SUITE(HashPointer, PointerTypes);
TYPED_TEST(EqPointer, Works) {
int dummy;
auto eq = this->key_eq;
auto sptr = std::make_shared<int>();
std::shared_ptr<const int> csptr = sptr;
int* ptr = sptr.get();
const int* cptr = ptr;
std::unique_ptr<int, NoDeleter> uptr(ptr);
std::unique_ptr<const int, NoDeleter> cuptr(ptr);
EXPECT_TRUE(eq(ptr, cptr));
EXPECT_TRUE(eq(ptr, sptr));
EXPECT_TRUE(eq(ptr, uptr));
EXPECT_TRUE(eq(ptr, csptr));
EXPECT_TRUE(eq(ptr, cuptr));
EXPECT_FALSE(eq(&dummy, cptr));
EXPECT_FALSE(eq(&dummy, sptr));
EXPECT_FALSE(eq(&dummy, uptr));
EXPECT_FALSE(eq(&dummy, csptr));
EXPECT_FALSE(eq(&dummy, cuptr));
}
TEST(Hash, DerivedAndBase) {
struct Base {};
struct Derived : Base {};
hash_default_hash<Base*> hasher;
Base base;
Derived derived;
EXPECT_NE(hasher(&base), hasher(&derived));
EXPECT_EQ(hasher(static_cast<Base*>(&derived)), hasher(&derived));
auto dp = std::make_shared<Derived>();
EXPECT_EQ(hasher(static_cast<Base*>(dp.get())), hasher(dp));
}
TEST(Hash, FunctionPointer) {
using Func = int (*)();
hash_default_hash<Func> hasher;
hash_default_eq<Func> eq;
Func p1 = [] { return 1; }, p2 = [] { return 2; };
EXPECT_EQ(hasher(p1), hasher(p1));
EXPECT_TRUE(eq(p1, p1));
EXPECT_NE(hasher(p1), hasher(p2));
EXPECT_FALSE(eq(p1, p2));
}
TYPED_TEST(HashPointer, Works) {
int dummy;
auto hash = this->hasher;
auto sptr = std::make_shared<int>();
std::shared_ptr<const int> csptr = sptr;
int* ptr = sptr.get();
const int* cptr = ptr;
std::unique_ptr<int, NoDeleter> uptr(ptr);
std::unique_ptr<const int, NoDeleter> cuptr(ptr);
EXPECT_EQ(hash(ptr), hash(cptr));
EXPECT_EQ(hash(ptr), hash(sptr));
EXPECT_EQ(hash(ptr), hash(uptr));
EXPECT_EQ(hash(ptr), hash(csptr));
EXPECT_EQ(hash(ptr), hash(cuptr));
EXPECT_NE(hash(&dummy), hash(cptr));
EXPECT_NE(hash(&dummy), hash(sptr));
EXPECT_NE(hash(&dummy), hash(uptr));
EXPECT_NE(hash(&dummy), hash(csptr));
EXPECT_NE(hash(&dummy), hash(cuptr));
}
TEST(EqCord, Works) {
hash_default_eq<absl::Cord> eq;
const absl::string_view a_string_view = "a";
const absl::Cord a_cord(a_string_view);
const absl::string_view b_string_view = "b";
const absl::Cord b_cord(b_string_view);
EXPECT_TRUE(eq(a_cord, a_cord));
EXPECT_TRUE(eq(a_cord, a_string_view));
EXPECT_TRUE(eq(a_string_view, a_cord));
EXPECT_FALSE(eq(a_cord, b_cord));
EXPECT_FALSE(eq(a_cord, b_string_view));
EXPECT_FALSE(eq(b_string_view, a_cord));
}
TEST(HashCord, Works) {
hash_default_hash<absl::Cord> hash;
const absl::string_view a_string_view = "a";
const absl::Cord a_cord(a_string_view);
const absl::string_view b_string_view = "b";
const absl::Cord b_cord(b_string_view);
EXPECT_EQ(hash(a_cord), hash(a_cord));
EXPECT_EQ(hash(b_cord), hash(b_cord));
EXPECT_EQ(hash(a_string_view), hash(a_cord));
EXPECT_EQ(hash(b_string_view), hash(b_cord));
EXPECT_EQ(hash(absl::Cord("")), hash(""));
EXPECT_EQ(hash(absl::Cord()), hash(absl::string_view()));
EXPECT_NE(hash(a_cord), hash(b_cord));
EXPECT_NE(hash(a_cord), hash(b_string_view));
EXPECT_NE(hash(a_string_view), hash(b_cord));
EXPECT_NE(hash(a_string_view), hash(b_string_view));
}
void NoOpReleaser(absl::string_view data, void* arg) {}
TEST(HashCord, FragmentedCordWorks) {
hash_default_hash<absl::Cord> hash;
absl::Cord c = absl::MakeFragmentedCord({"a", "b", "c"});
EXPECT_FALSE(c.TryFlat().has_value());
EXPECT_EQ(hash(c), hash("abc"));
}
TEST(HashCord, FragmentedLongCordWorks) {
hash_default_hash<absl::Cord> hash;
// Crete some large strings which do not fit on the stack.
std::string a(65536, 'a');
std::string b(65536, 'b');
absl::Cord c = absl::MakeFragmentedCord({a, b});
EXPECT_FALSE(c.TryFlat().has_value());
EXPECT_EQ(hash(c), hash(a + b));
}
TEST(HashCord, RandomCord) {
hash_default_hash<absl::Cord> hash;
auto bitgen = absl::BitGen();
for (int i = 0; i < 1000; ++i) {
const int number_of_segments = absl::Uniform(bitgen, 0, 10);
std::vector<std::string> pieces;
for (size_t s = 0; s < number_of_segments; ++s) {
std::string str;
str.resize(absl::Uniform(bitgen, 0, 4096));
// MSVC needed the explicit return type in the lambda.
std::generate(str.begin(), str.end(), [&]() -> char {
return static_cast<char>(absl::Uniform<unsigned char>(bitgen));
});
pieces.push_back(str);
}
absl::Cord c = absl::MakeFragmentedCord(pieces);
EXPECT_EQ(hash(c), hash(std::string(c)));
}
}
// Cartesian product of (std::string, absl::string_view)
// with (std::string, absl::string_view, const char*, absl::Cord).
using StringTypesCartesianProduct = Types<
// clang-format off
std::pair<absl::Cord, std::string>,
std::pair<absl::Cord, absl::string_view>,
std::pair<absl::Cord, absl::Cord>,
std::pair<absl::Cord, const char*>,
std::pair<std::string, absl::Cord>,
std::pair<absl::string_view, absl::Cord>,
std::pair<absl::string_view, std::string>,
std::pair<absl::string_view, absl::string_view>,
std::pair<absl::string_view, const char*>>;
// clang-format on
constexpr char kFirstString[] = "abc123";
constexpr char kSecondString[] = "ijk456";
template <typename T>
struct StringLikeTest : public ::testing::Test {
typename T::first_type a1{kFirstString};
typename T::second_type b1{kFirstString};
typename T::first_type a2{kSecondString};
typename T::second_type b2{kSecondString};
hash_default_eq<typename T::first_type> eq;
hash_default_hash<typename T::first_type> hash;
};
TYPED_TEST_SUITE(StringLikeTest, StringTypesCartesianProduct);
TYPED_TEST(StringLikeTest, Eq) {
EXPECT_TRUE(this->eq(this->a1, this->b1));
EXPECT_TRUE(this->eq(this->b1, this->a1));
}
TYPED_TEST(StringLikeTest, NotEq) {
EXPECT_FALSE(this->eq(this->a1, this->b2));
EXPECT_FALSE(this->eq(this->b2, this->a1));
}
TYPED_TEST(StringLikeTest, HashEq) {
EXPECT_EQ(this->hash(this->a1), this->hash(this->b1));
EXPECT_EQ(this->hash(this->a2), this->hash(this->b2));
// It would be a poor hash function which collides on these strings.
EXPECT_NE(this->hash(this->a1), this->hash(this->b2));
}
struct TypeWithAbslContainerHash {
struct absl_container_hash {
using is_transparent = void;
size_t operator()(const TypeWithAbslContainerHash& foo) const {
return absl::HashOf(foo.value);
}
// Extra overload to test that heterogeneity works for this hasher.
size_t operator()(int value) const { return absl::HashOf(value); }
};
friend bool operator==(const TypeWithAbslContainerHash& lhs,
const TypeWithAbslContainerHash& rhs) {
return lhs.value == rhs.value;
}
friend bool operator==(const TypeWithAbslContainerHash& lhs, int rhs) {
return lhs.value == rhs;
}
int value;
int noise;
};
struct TypeWithAbslContainerHashAndEq {
struct absl_container_hash {
using is_transparent = void;
size_t operator()(const TypeWithAbslContainerHashAndEq& foo) const {
return absl::HashOf(foo.value);
}
// Extra overload to test that heterogeneity works for this hasher.
size_t operator()(int value) const { return absl::HashOf(value); }
};
struct absl_container_eq {
using is_transparent = void;
bool operator()(const TypeWithAbslContainerHashAndEq& lhs,
const TypeWithAbslContainerHashAndEq& rhs) const {
return lhs.value == rhs.value;
}
// Extra overload to test that heterogeneity works for this eq.
bool operator()(const TypeWithAbslContainerHashAndEq& lhs, int rhs) const {
return lhs.value == rhs;
}
};
template <typename T>
bool operator==(T&& other) const = delete;
int value;
int noise;
};
using AbslContainerHashTypes =
Types<TypeWithAbslContainerHash, TypeWithAbslContainerHashAndEq>;
template <typename T>
using AbslContainerHashTest = ::testing::Test;
TYPED_TEST_SUITE(AbslContainerHashTest, AbslContainerHashTypes);
TYPED_TEST(AbslContainerHashTest, HasherWorks) {
hash_default_hash<TypeParam> hasher;
TypeParam foo1{/*value=*/1, /*noise=*/100};
TypeParam foo1_copy{/*value=*/1, /*noise=*/20};
TypeParam foo2{/*value=*/2, /*noise=*/100};
EXPECT_EQ(hasher(foo1), absl::HashOf(1));
EXPECT_EQ(hasher(foo2), absl::HashOf(2));
EXPECT_EQ(hasher(foo1), hasher(foo1_copy));
// Heterogeneity works.
EXPECT_EQ(hasher(foo1), hasher(1));
EXPECT_EQ(hasher(foo2), hasher(2));
}
TYPED_TEST(AbslContainerHashTest, EqWorks) {
hash_default_eq<TypeParam> eq;
TypeParam foo1{/*value=*/1, /*noise=*/100};
TypeParam foo1_copy{/*value=*/1, /*noise=*/20};
TypeParam foo2{/*value=*/2, /*noise=*/100};
EXPECT_TRUE(eq(foo1, foo1_copy));
EXPECT_FALSE(eq(foo1, foo2));
// Heterogeneity works.
EXPECT_TRUE(eq(foo1, 1));
EXPECT_FALSE(eq(foo1, 2));
}
TYPED_TEST(AbslContainerHashTest, HeterogeneityInMapWorks) {
absl::flat_hash_map<TypeParam, int> map;
TypeParam foo1{/*value=*/1, /*noise=*/100};
TypeParam foo1_copy{/*value=*/1, /*noise=*/20};
TypeParam foo2{/*value=*/2, /*noise=*/100};
TypeParam foo3{/*value=*/3, /*noise=*/100};
map[foo1] = 1;
map[foo2] = 2;
EXPECT_TRUE(map.contains(foo1_copy));
EXPECT_EQ(map.at(foo1_copy), 1);
EXPECT_TRUE(map.contains(1));
EXPECT_EQ(map.at(1), 1);
EXPECT_TRUE(map.contains(2));
EXPECT_EQ(map.at(2), 2);
EXPECT_FALSE(map.contains(foo3));
EXPECT_FALSE(map.contains(3));
}
TYPED_TEST(AbslContainerHashTest, HeterogeneityInSetWorks) {
absl::flat_hash_set<TypeParam> set;
TypeParam foo1{/*value=*/1, /*noise=*/100};
TypeParam foo1_copy{/*value=*/1, /*noise=*/20};
TypeParam foo2{/*value=*/2, /*noise=*/100};
set.insert(foo1);
EXPECT_TRUE(set.contains(foo1_copy));
EXPECT_TRUE(set.contains(1));
EXPECT_FALSE(set.contains(foo2));
EXPECT_FALSE(set.contains(2));
}
} // namespace
} // namespace container_internal
ABSL_NAMESPACE_END
} // namespace absl
enum Hash : size_t {
kStd = 0x1, // std::hash
#ifdef _MSC_VER
kExtension = kStd, // In MSVC, std::hash == ::hash
#else // _MSC_VER
kExtension = 0x2, // ::hash (GCC extension)
#endif // _MSC_VER
};
// H is a bitmask of Hash enumerations.
// Hashable<H> is hashable via all means specified in H.
template <int H>
struct Hashable {
static constexpr bool HashableBy(Hash h) { return h & H; }
};
namespace std {
template <int H>
struct hash<Hashable<H>> {
template <class E = Hashable<H>,
class = typename std::enable_if<E::HashableBy(kStd)>::type>
size_t operator()(E) const {
return kStd;
}
};
} // namespace std
namespace absl {
ABSL_NAMESPACE_BEGIN
namespace container_internal {
namespace {
template <class T>
size_t Hash(const T& v) {
return hash_default_hash<T>()(v);
}
TEST(Delegate, HashDispatch) {
EXPECT_EQ(Hash(kStd), Hash(Hashable<kStd>()));
}
} // namespace
} // namespace container_internal
ABSL_NAMESPACE_END
} // namespace absl

View file

@ -0,0 +1,78 @@
// Copyright 2018 The Abseil Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "absl/container/internal/hash_generator_testing.h"
#include <deque>
#include "absl/base/no_destructor.h"
namespace absl {
ABSL_NAMESPACE_BEGIN
namespace container_internal {
namespace hash_internal {
namespace {
class RandomDeviceSeedSeq {
public:
using result_type = typename std::random_device::result_type;
template <class Iterator>
void generate(Iterator start, Iterator end) {
while (start != end) {
*start = gen_();
++start;
}
}
private:
std::random_device gen_;
};
} // namespace
std::mt19937_64* GetSharedRng() {
static absl::NoDestructor<std::mt19937_64> rng([] {
RandomDeviceSeedSeq seed_seq;
return std::mt19937_64(seed_seq);
}());
return rng.get();
}
std::string Generator<std::string>::operator()() const {
// NOLINTNEXTLINE(runtime/int)
std::uniform_int_distribution<short> chars(0x20, 0x7E);
std::string res;
res.resize(32);
std::generate(res.begin(), res.end(),
[&]() { return chars(*GetSharedRng()); });
return res;
}
absl::string_view Generator<absl::string_view>::operator()() const {
static absl::NoDestructor<std::deque<std::string>> arena;
// NOLINTNEXTLINE(runtime/int)
std::uniform_int_distribution<short> chars(0x20, 0x7E);
arena->emplace_back();
auto& res = arena->back();
res.resize(32);
std::generate(res.begin(), res.end(),
[&]() { return chars(*GetSharedRng()); });
return res;
}
} // namespace hash_internal
} // namespace container_internal
ABSL_NAMESPACE_END
} // namespace absl

View file

@ -0,0 +1,182 @@
// Copyright 2018 The Abseil Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// Generates random values for testing. Specialized only for the few types we
// care about.
#ifndef ABSL_CONTAINER_INTERNAL_HASH_GENERATOR_TESTING_H_
#define ABSL_CONTAINER_INTERNAL_HASH_GENERATOR_TESTING_H_
#include <stdint.h>
#include <algorithm>
#include <cassert>
#include <iosfwd>
#include <random>
#include <tuple>
#include <type_traits>
#include <utility>
#include <vector>
#include "absl/container/internal/hash_policy_testing.h"
#include "absl/memory/memory.h"
#include "absl/meta/type_traits.h"
#include "absl/strings/string_view.h"
namespace absl {
ABSL_NAMESPACE_BEGIN
namespace container_internal {
namespace hash_internal {
namespace generator_internal {
template <class Container, class = void>
struct IsMap : std::false_type {};
template <class Map>
struct IsMap<Map, absl::void_t<typename Map::mapped_type>> : std::true_type {};
} // namespace generator_internal
std::mt19937_64* GetSharedRng();
enum Enum {
kEnumEmpty,
kEnumDeleted,
};
enum class EnumClass : uint64_t {
kEmpty,
kDeleted,
};
inline std::ostream& operator<<(std::ostream& o, const EnumClass& ec) {
return o << static_cast<uint64_t>(ec);
}
template <class T, class E = void>
struct Generator;
template <class T>
struct Generator<T, typename std::enable_if<std::is_integral<T>::value>::type> {
T operator()() const {
std::uniform_int_distribution<T> dist;
return dist(*GetSharedRng());
}
};
template <>
struct Generator<Enum> {
Enum operator()() const {
std::uniform_int_distribution<typename std::underlying_type<Enum>::type>
dist;
while (true) {
auto variate = dist(*GetSharedRng());
if (variate != kEnumEmpty && variate != kEnumDeleted)
return static_cast<Enum>(variate);
}
}
};
template <>
struct Generator<EnumClass> {
EnumClass operator()() const {
std::uniform_int_distribution<
typename std::underlying_type<EnumClass>::type>
dist;
while (true) {
EnumClass variate = static_cast<EnumClass>(dist(*GetSharedRng()));
if (variate != EnumClass::kEmpty && variate != EnumClass::kDeleted)
return static_cast<EnumClass>(variate);
}
}
};
template <>
struct Generator<std::string> {
std::string operator()() const;
};
template <>
struct Generator<absl::string_view> {
absl::string_view operator()() const;
};
template <>
struct Generator<NonStandardLayout> {
NonStandardLayout operator()() const {
return NonStandardLayout(Generator<std::string>()());
}
};
template <class K, class V>
struct Generator<std::pair<K, V>> {
std::pair<K, V> operator()() const {
return std::pair<K, V>(Generator<typename std::decay<K>::type>()(),
Generator<typename std::decay<V>::type>()());
}
};
template <class... Ts>
struct Generator<std::tuple<Ts...>> {
std::tuple<Ts...> operator()() const {
return std::tuple<Ts...>(Generator<typename std::decay<Ts>::type>()()...);
}
};
template <class T>
struct Generator<std::unique_ptr<T>> {
std::unique_ptr<T> operator()() const {
return absl::make_unique<T>(Generator<T>()());
}
};
template <class U>
struct Generator<U, absl::void_t<decltype(std::declval<U&>().key()),
decltype(std::declval<U&>().value())>>
: Generator<std::pair<
typename std::decay<decltype(std::declval<U&>().key())>::type,
typename std::decay<decltype(std::declval<U&>().value())>::type>> {};
template <class Container>
using GeneratedType = decltype(
std::declval<const Generator<
typename std::conditional<generator_internal::IsMap<Container>::value,
typename Container::value_type,
typename Container::key_type>::type>&>()());
// Naive wrapper that performs a linear search of previous values.
// Beware this is O(SQR), which is reasonable for smaller kMaxValues.
template <class T, size_t kMaxValues = 64, class E = void>
struct UniqueGenerator {
Generator<T, E> gen;
std::vector<T> values;
T operator()() {
assert(values.size() < kMaxValues);
for (;;) {
T value = gen();
if (std::find(values.begin(), values.end(), value) == values.end()) {
values.push_back(value);
return value;
}
}
}
};
} // namespace hash_internal
} // namespace container_internal
ABSL_NAMESPACE_END
} // namespace absl
#endif // ABSL_CONTAINER_INTERNAL_HASH_GENERATOR_TESTING_H_

View file

@ -0,0 +1,183 @@
// Copyright 2018 The Abseil Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// Utilities to help tests verify that hash tables properly handle stateful
// allocators and hash functions.
#ifndef ABSL_CONTAINER_INTERNAL_HASH_POLICY_TESTING_H_
#define ABSL_CONTAINER_INTERNAL_HASH_POLICY_TESTING_H_
#include <cstdlib>
#include <limits>
#include <memory>
#include <ostream>
#include <type_traits>
#include <utility>
#include <vector>
#include "absl/hash/hash.h"
#include "absl/strings/string_view.h"
namespace absl {
ABSL_NAMESPACE_BEGIN
namespace container_internal {
namespace hash_testing_internal {
template <class Derived>
struct WithId {
WithId() : id_(next_id<Derived>()) {}
WithId(const WithId& that) : id_(that.id_) {}
WithId(WithId&& that) : id_(that.id_) { that.id_ = 0; }
WithId& operator=(const WithId& that) {
id_ = that.id_;
return *this;
}
WithId& operator=(WithId&& that) {
id_ = that.id_;
that.id_ = 0;
return *this;
}
size_t id() const { return id_; }
friend bool operator==(const WithId& a, const WithId& b) {
return a.id_ == b.id_;
}
friend bool operator!=(const WithId& a, const WithId& b) { return !(a == b); }
protected:
explicit WithId(size_t id) : id_(id) {}
private:
size_t id_;
template <class T>
static size_t next_id() {
// 0 is reserved for moved from state.
static size_t gId = 1;
return gId++;
}
};
} // namespace hash_testing_internal
struct NonStandardLayout {
NonStandardLayout() {}
explicit NonStandardLayout(std::string s) : value(std::move(s)) {}
virtual ~NonStandardLayout() {}
friend bool operator==(const NonStandardLayout& a,
const NonStandardLayout& b) {
return a.value == b.value;
}
friend bool operator!=(const NonStandardLayout& a,
const NonStandardLayout& b) {
return a.value != b.value;
}
template <typename H>
friend H AbslHashValue(H h, const NonStandardLayout& v) {
return H::combine(std::move(h), v.value);
}
std::string value;
};
struct StatefulTestingHash
: absl::container_internal::hash_testing_internal::WithId<
StatefulTestingHash> {
template <class T>
size_t operator()(const T& t) const {
return absl::Hash<T>{}(t);
}
};
struct StatefulTestingEqual
: absl::container_internal::hash_testing_internal::WithId<
StatefulTestingEqual> {
template <class T, class U>
bool operator()(const T& t, const U& u) const {
return t == u;
}
};
// It is expected that Alloc() == Alloc() for all allocators so we cannot use
// WithId base. We need to explicitly assign ids.
template <class T = int>
struct Alloc : std::allocator<T> {
using propagate_on_container_swap = std::true_type;
// Using old paradigm for this to ensure compatibility.
explicit Alloc(size_t id = 0) : id_(id) {}
Alloc(const Alloc&) = default;
Alloc& operator=(const Alloc&) = default;
template <class U>
Alloc(const Alloc<U>& that) : std::allocator<T>(that), id_(that.id()) {}
template <class U>
struct rebind {
using other = Alloc<U>;
};
size_t id() const { return id_; }
friend bool operator==(const Alloc& a, const Alloc& b) {
return a.id_ == b.id_;
}
friend bool operator!=(const Alloc& a, const Alloc& b) { return !(a == b); }
private:
size_t id_ = (std::numeric_limits<size_t>::max)();
};
template <class Map>
auto items(const Map& m) -> std::vector<
std::pair<typename Map::key_type, typename Map::mapped_type>> {
using std::get;
std::vector<std::pair<typename Map::key_type, typename Map::mapped_type>> res;
res.reserve(m.size());
for (const auto& v : m) res.emplace_back(get<0>(v), get<1>(v));
return res;
}
template <class Set>
auto keys(const Set& s)
-> std::vector<typename std::decay<typename Set::key_type>::type> {
std::vector<typename std::decay<typename Set::key_type>::type> res;
res.reserve(s.size());
for (const auto& v : s) res.emplace_back(v);
return res;
}
} // namespace container_internal
ABSL_NAMESPACE_END
} // namespace absl
// ABSL_UNORDERED_SUPPORTS_ALLOC_CTORS is false for glibcxx versions
// where the unordered containers are missing certain constructors that
// take allocator arguments. This test is defined ad-hoc for the platforms
// we care about (notably Crosstool 17) because libstdcxx's useless
// versioning scheme precludes a more principled solution.
// From GCC-4.9 Changelog: (src: https://gcc.gnu.org/gcc-4.9/changes.html)
// "the unordered associative containers in <unordered_map> and <unordered_set>
// meet the allocator-aware container requirements;"
#if defined(__GLIBCXX__) && __GLIBCXX__ <= 20140425
#define ABSL_UNORDERED_SUPPORTS_ALLOC_CTORS 0
#else
#define ABSL_UNORDERED_SUPPORTS_ALLOC_CTORS 1
#endif
#endif // ABSL_CONTAINER_INTERNAL_HASH_POLICY_TESTING_H_

View file

@ -0,0 +1,45 @@
// Copyright 2018 The Abseil Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "absl/container/internal/hash_policy_testing.h"
#include "gtest/gtest.h"
namespace absl {
ABSL_NAMESPACE_BEGIN
namespace container_internal {
namespace {
TEST(_, Hash) {
StatefulTestingHash h1;
EXPECT_EQ(1, h1.id());
StatefulTestingHash h2;
EXPECT_EQ(2, h2.id());
StatefulTestingHash h1c(h1);
EXPECT_EQ(1, h1c.id());
StatefulTestingHash h2m(std::move(h2));
EXPECT_EQ(2, h2m.id());
EXPECT_EQ(0, h2.id());
StatefulTestingHash h3;
EXPECT_EQ(3, h3.id());
h3 = StatefulTestingHash();
EXPECT_EQ(4, h3.id());
h3 = std::move(h1);
EXPECT_EQ(1, h3.id());
}
} // namespace
} // namespace container_internal
ABSL_NAMESPACE_END
} // namespace absl

View file

@ -0,0 +1,207 @@
// Copyright 2018 The Abseil Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef ABSL_CONTAINER_INTERNAL_HASH_POLICY_TRAITS_H_
#define ABSL_CONTAINER_INTERNAL_HASH_POLICY_TRAITS_H_
#include <cstddef>
#include <memory>
#include <new>
#include <type_traits>
#include <utility>
#include "absl/container/internal/common_policy_traits.h"
#include "absl/meta/type_traits.h"
namespace absl {
ABSL_NAMESPACE_BEGIN
namespace container_internal {
// Defines how slots are initialized/destroyed/moved.
template <class Policy, class = void>
struct hash_policy_traits : common_policy_traits<Policy> {
// The type of the keys stored in the hashtable.
using key_type = typename Policy::key_type;
private:
struct ReturnKey {
// When C++17 is available, we can use std::launder to provide mutable
// access to the key for use in node handle.
#if defined(__cpp_lib_launder) && __cpp_lib_launder >= 201606
template <class Key,
absl::enable_if_t<std::is_lvalue_reference<Key>::value, int> = 0>
static key_type& Impl(Key&& k, int) {
return *std::launder(
const_cast<key_type*>(std::addressof(std::forward<Key>(k))));
}
#endif
template <class Key>
static Key Impl(Key&& k, char) {
return std::forward<Key>(k);
}
// When Key=T&, we forward the lvalue reference.
// When Key=T, we return by value to avoid a dangling reference.
// eg, for string_hash_map.
template <class Key, class... Args>
auto operator()(Key&& k, const Args&...) const
-> decltype(Impl(std::forward<Key>(k), 0)) {
return Impl(std::forward<Key>(k), 0);
}
};
template <class P = Policy, class = void>
struct ConstantIteratorsImpl : std::false_type {};
template <class P>
struct ConstantIteratorsImpl<P, absl::void_t<typename P::constant_iterators>>
: P::constant_iterators {};
public:
// The actual object stored in the hash table.
using slot_type = typename Policy::slot_type;
// The argument type for insertions into the hashtable. This is different
// from value_type for increased performance. See initializer_list constructor
// and insert() member functions for more details.
using init_type = typename Policy::init_type;
using reference = decltype(Policy::element(std::declval<slot_type*>()));
using pointer = typename std::remove_reference<reference>::type*;
using value_type = typename std::remove_reference<reference>::type;
// Policies can set this variable to tell raw_hash_set that all iterators
// should be constant, even `iterator`. This is useful for set-like
// containers.
// Defaults to false if not provided by the policy.
using constant_iterators = ConstantIteratorsImpl<>;
// Returns the amount of memory owned by `slot`, exclusive of `sizeof(*slot)`.
//
// If `slot` is nullptr, returns the constant amount of memory owned by any
// full slot or -1 if slots own variable amounts of memory.
//
// PRECONDITION: `slot` is INITIALIZED or nullptr
template <class P = Policy>
static size_t space_used(const slot_type* slot) {
return P::space_used(slot);
}
// Provides generalized access to the key for elements, both for elements in
// the table and for elements that have not yet been inserted (or even
// constructed). We would like an API that allows us to say: `key(args...)`
// but we cannot do that for all cases, so we use this more general API that
// can be used for many things, including the following:
//
// - Given an element in a table, get its key.
// - Given an element initializer, get its key.
// - Given `emplace()` arguments, get the element key.
//
// Implementations of this must adhere to a very strict technical
// specification around aliasing and consuming arguments:
//
// Let `value_type` be the result type of `element()` without ref- and
// cv-qualifiers. The first argument is a functor, the rest are constructor
// arguments for `value_type`. Returns `std::forward<F>(f)(k, xs...)`, where
// `k` is the element key, and `xs...` are the new constructor arguments for
// `value_type`. It's allowed for `k` to alias `xs...`, and for both to alias
// `ts...`. The key won't be touched once `xs...` are used to construct an
// element; `ts...` won't be touched at all, which allows `apply()` to consume
// any rvalues among them.
//
// If `value_type` is constructible from `Ts&&...`, `Policy::apply()` must not
// trigger a hard compile error unless it originates from `f`. In other words,
// `Policy::apply()` must be SFINAE-friendly. If `value_type` is not
// constructible from `Ts&&...`, either SFINAE or a hard compile error is OK.
//
// If `Ts...` is `[cv] value_type[&]` or `[cv] init_type[&]`,
// `Policy::apply()` must work. A compile error is not allowed, SFINAE or not.
template <class F, class... Ts, class P = Policy>
static auto apply(F&& f, Ts&&... ts)
-> decltype(P::apply(std::forward<F>(f), std::forward<Ts>(ts)...)) {
return P::apply(std::forward<F>(f), std::forward<Ts>(ts)...);
}
// Returns the "key" portion of the slot.
// Used for node handle manipulation.
template <class P = Policy>
static auto mutable_key(slot_type* slot)
-> decltype(P::apply(ReturnKey(), hash_policy_traits::element(slot))) {
return P::apply(ReturnKey(), hash_policy_traits::element(slot));
}
// Returns the "value" (as opposed to the "key") portion of the element. Used
// by maps to implement `operator[]`, `at()` and `insert_or_assign()`.
template <class T, class P = Policy>
static auto value(T* elem) -> decltype(P::value(elem)) {
return P::value(elem);
}
using HashSlotFn = size_t (*)(const void* hash_fn, void* slot);
template <class Hash>
static constexpr HashSlotFn get_hash_slot_fn() {
// get_hash_slot_fn may return nullptr to signal that non type erased function
// should be used. GCC warns against comparing function address with nullptr.
#if defined(__GNUC__) && !defined(__clang__)
#pragma GCC diagnostic push
// silent error: the address of * will never be NULL [-Werror=address]
#pragma GCC diagnostic ignored "-Waddress"
#endif
return Policy::template get_hash_slot_fn<Hash>() == nullptr
? &hash_slot_fn_non_type_erased<Hash>
: Policy::template get_hash_slot_fn<Hash>();
#if defined(__GNUC__) && !defined(__clang__)
#pragma GCC diagnostic pop
#endif
}
// Whether small object optimization is enabled. True by default.
static constexpr bool soo_enabled() { return soo_enabled_impl(Rank1{}); }
private:
template <class Hash>
struct HashElement {
template <class K, class... Args>
size_t operator()(const K& key, Args&&...) const {
return h(key);
}
const Hash& h;
};
template <class Hash>
static size_t hash_slot_fn_non_type_erased(const void* hash_fn, void* slot) {
return Policy::apply(HashElement<Hash>{*static_cast<const Hash*>(hash_fn)},
Policy::element(static_cast<slot_type*>(slot)));
}
// Use go/ranked-overloads for dispatching. Rank1 is preferred.
struct Rank0 {};
struct Rank1 : Rank0 {};
// Use auto -> decltype as an enabler.
template <class P = Policy>
static constexpr auto soo_enabled_impl(Rank1) -> decltype(P::soo_enabled()) {
return P::soo_enabled();
}
static constexpr bool soo_enabled_impl(Rank0) { return true; }
};
} // namespace container_internal
ABSL_NAMESPACE_END
} // namespace absl
#endif // ABSL_CONTAINER_INTERNAL_HASH_POLICY_TRAITS_H_

View file

@ -0,0 +1,144 @@
// Copyright 2018 The Abseil Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "absl/container/internal/hash_policy_traits.h"
#include <cstddef>
#include <functional>
#include <memory>
#include <new>
#include "gmock/gmock.h"
#include "gtest/gtest.h"
#include "absl/container/internal/container_memory.h"
namespace absl {
ABSL_NAMESPACE_BEGIN
namespace container_internal {
namespace {
using ::testing::MockFunction;
using ::testing::Return;
using ::testing::ReturnRef;
using Alloc = std::allocator<int>;
using Slot = int;
struct PolicyWithoutOptionalOps {
using slot_type = Slot;
using key_type = Slot;
using init_type = Slot;
static std::function<Slot&(Slot*)> element;
static int apply(int v) { return apply_impl(v); }
static std::function<int(int)> apply_impl;
static std::function<Slot&(Slot*)> value;
template <class Hash>
static constexpr HashSlotFn get_hash_slot_fn() {
return nullptr;
}
};
std::function<int(int)> PolicyWithoutOptionalOps::apply_impl;
std::function<Slot&(Slot*)> PolicyWithoutOptionalOps::value;
struct Test : ::testing::Test {
Test() {
PolicyWithoutOptionalOps::apply_impl = [&](int a1) -> int {
return apply.Call(a1);
};
PolicyWithoutOptionalOps::value = [&](Slot* a1) -> Slot& {
return value.Call(a1);
};
}
std::allocator<int> alloc;
int a = 53;
MockFunction<int(int)> apply;
MockFunction<Slot&(Slot*)> value;
};
TEST_F(Test, apply) {
EXPECT_CALL(apply, Call(42)).WillOnce(Return(1337));
EXPECT_EQ(1337, (hash_policy_traits<PolicyWithoutOptionalOps>::apply(42)));
}
TEST_F(Test, value) {
int b = 0;
EXPECT_CALL(value, Call(&a)).WillOnce(ReturnRef(b));
EXPECT_EQ(&b, &hash_policy_traits<PolicyWithoutOptionalOps>::value(&a));
}
struct Hash {
size_t operator()(Slot a) const { return static_cast<size_t>(a) * 5; }
};
struct PolicyNoHashFn {
using slot_type = Slot;
using key_type = Slot;
using init_type = Slot;
static size_t* apply_called_count;
static Slot& element(Slot* slot) { return *slot; }
template <typename Fn>
static size_t apply(const Fn& fn, int v) {
++(*apply_called_count);
return fn(v);
}
template <class Hash>
static constexpr HashSlotFn get_hash_slot_fn() {
return nullptr;
}
};
size_t* PolicyNoHashFn::apply_called_count;
struct PolicyCustomHashFn : PolicyNoHashFn {
template <class Hash>
static constexpr HashSlotFn get_hash_slot_fn() {
return &TypeErasedApplyToSlotFn<Hash, int>;
}
};
TEST(HashTest, PolicyNoHashFn_get_hash_slot_fn) {
size_t apply_called_count = 0;
PolicyNoHashFn::apply_called_count = &apply_called_count;
Hash hasher;
Slot value = 7;
auto* fn = hash_policy_traits<PolicyNoHashFn>::get_hash_slot_fn<Hash>();
EXPECT_NE(fn, nullptr);
EXPECT_EQ(fn(&hasher, &value), hasher(value));
EXPECT_EQ(apply_called_count, 1);
}
TEST(HashTest, PolicyCustomHashFn_get_hash_slot_fn) {
size_t apply_called_count = 0;
PolicyNoHashFn::apply_called_count = &apply_called_count;
Hash hasher;
Slot value = 7;
auto* fn = hash_policy_traits<PolicyCustomHashFn>::get_hash_slot_fn<Hash>();
EXPECT_EQ(fn, PolicyCustomHashFn::get_hash_slot_fn<Hash>());
EXPECT_EQ(fn(&hasher, &value), hasher(value));
EXPECT_EQ(apply_called_count, 0);
}
} // namespace
} // namespace container_internal
ABSL_NAMESPACE_END
} // namespace absl

View file

@ -0,0 +1,102 @@
// Copyright 2018 The Abseil Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// This library provides APIs to debug the probing behavior of hash tables.
//
// In general, the probing behavior is a black box for users and only the
// side effects can be measured in the form of performance differences.
// These APIs give a glimpse on the actual behavior of the probing algorithms in
// these hashtables given a specified hash function and a set of elements.
//
// The probe count distribution can be used to assess the quality of the hash
// function for that particular hash table. Note that a hash function that
// performs well in one hash table implementation does not necessarily performs
// well in a different one.
//
// This library supports std::unordered_{set,map}, dense_hash_{set,map} and
// absl::{flat,node,string}_hash_{set,map}.
#ifndef ABSL_CONTAINER_INTERNAL_HASHTABLE_DEBUG_H_
#define ABSL_CONTAINER_INTERNAL_HASHTABLE_DEBUG_H_
#include <cstddef>
#include <algorithm>
#include <type_traits>
#include <vector>
#include "absl/container/internal/hashtable_debug_hooks.h"
namespace absl {
ABSL_NAMESPACE_BEGIN
namespace container_internal {
// Returns the number of probes required to lookup `key`. Returns 0 for a
// search with no collisions. Higher values mean more hash collisions occurred;
// however, the exact meaning of this number varies according to the container
// type.
template <typename C>
size_t GetHashtableDebugNumProbes(
const C& c, const typename C::key_type& key) {
return absl::container_internal::hashtable_debug_internal::
HashtableDebugAccess<C>::GetNumProbes(c, key);
}
// Gets a histogram of the number of probes for each elements in the container.
// The sum of all the values in the vector is equal to container.size().
template <typename C>
std::vector<size_t> GetHashtableDebugNumProbesHistogram(const C& container) {
std::vector<size_t> v;
for (auto it = container.begin(); it != container.end(); ++it) {
size_t num_probes = GetHashtableDebugNumProbes(
container,
absl::container_internal::hashtable_debug_internal::GetKey<C>(*it, 0));
v.resize((std::max)(v.size(), num_probes + 1));
v[num_probes]++;
}
return v;
}
struct HashtableDebugProbeSummary {
size_t total_elements;
size_t total_num_probes;
double mean;
};
// Gets a summary of the probe count distribution for the elements in the
// container.
template <typename C>
HashtableDebugProbeSummary GetHashtableDebugProbeSummary(const C& container) {
auto probes = GetHashtableDebugNumProbesHistogram(container);
HashtableDebugProbeSummary summary = {};
for (size_t i = 0; i < probes.size(); ++i) {
summary.total_elements += probes[i];
summary.total_num_probes += probes[i] * i;
}
summary.mean = 1.0 * summary.total_num_probes / summary.total_elements;
return summary;
}
// Returns the number of bytes requested from the allocator by the container
// and not freed.
template <typename C>
size_t AllocatedByteSize(const C& c) {
return absl::container_internal::hashtable_debug_internal::
HashtableDebugAccess<C>::AllocatedByteSize(c);
}
} // namespace container_internal
ABSL_NAMESPACE_END
} // namespace absl
#endif // ABSL_CONTAINER_INTERNAL_HASHTABLE_DEBUG_H_

View file

@ -0,0 +1,85 @@
// Copyright 2018 The Abseil Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// Provides the internal API for hashtable_debug.h.
#ifndef ABSL_CONTAINER_INTERNAL_HASHTABLE_DEBUG_HOOKS_H_
#define ABSL_CONTAINER_INTERNAL_HASHTABLE_DEBUG_HOOKS_H_
#include <cstddef>
#include <algorithm>
#include <type_traits>
#include <vector>
#include "absl/base/config.h"
namespace absl {
ABSL_NAMESPACE_BEGIN
namespace container_internal {
namespace hashtable_debug_internal {
// If it is a map, call get<0>().
using std::get;
template <typename T, typename = typename T::mapped_type>
auto GetKey(const typename T::value_type& pair, int) -> decltype(get<0>(pair)) {
return get<0>(pair);
}
// If it is not a map, return the value directly.
template <typename T>
const typename T::key_type& GetKey(const typename T::key_type& key, char) {
return key;
}
// Containers should specialize this to provide debug information for that
// container.
template <class Container, typename Enabler = void>
struct HashtableDebugAccess {
// Returns the number of probes required to find `key` in `c`. The "number of
// probes" is a concept that can vary by container. Implementations should
// return 0 when `key` was found in the minimum number of operations and
// should increment the result for each non-trivial operation required to find
// `key`.
//
// The default implementation uses the bucket api from the standard and thus
// works for `std::unordered_*` containers.
static size_t GetNumProbes(const Container& c,
const typename Container::key_type& key) {
if (!c.bucket_count()) return {};
size_t num_probes = 0;
size_t bucket = c.bucket(key);
for (auto it = c.begin(bucket), e = c.end(bucket);; ++it, ++num_probes) {
if (it == e) return num_probes;
if (c.key_eq()(key, GetKey<Container>(*it, 0))) return num_probes;
}
}
// Returns the number of bytes requested from the allocator by the container
// and not freed.
//
// static size_t AllocatedByteSize(const Container& c);
// Returns a tight lower bound for AllocatedByteSize(c) where `c` is of type
// `Container` and `c.size()` is equal to `num_elements`.
//
// static size_t LowerBoundAllocatedByteSize(size_t num_elements);
};
} // namespace hashtable_debug_internal
} // namespace container_internal
ABSL_NAMESPACE_END
} // namespace absl
#endif // ABSL_CONTAINER_INTERNAL_HASHTABLE_DEBUG_HOOKS_H_

View file

@ -0,0 +1,320 @@
// Copyright 2018 The Abseil Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "absl/container/internal/hashtablez_sampler.h"
#include <algorithm>
#include <atomic>
#include <cassert>
#include <cmath>
#include <cstddef>
#include <cstdint>
#include <functional>
#include <limits>
#include "absl/base/attributes.h"
#include "absl/base/config.h"
#include "absl/base/internal/per_thread_tls.h"
#include "absl/base/internal/raw_logging.h"
#include "absl/base/macros.h"
#include "absl/base/no_destructor.h"
#include "absl/base/optimization.h"
#include "absl/debugging/stacktrace.h"
#include "absl/memory/memory.h"
#include "absl/profiling/internal/exponential_biased.h"
#include "absl/profiling/internal/sample_recorder.h"
#include "absl/synchronization/mutex.h"
#include "absl/time/clock.h"
#include "absl/utility/utility.h"
namespace absl {
ABSL_NAMESPACE_BEGIN
namespace container_internal {
#ifdef ABSL_INTERNAL_NEED_REDUNDANT_CONSTEXPR_DECL
constexpr int HashtablezInfo::kMaxStackDepth;
#endif
namespace {
ABSL_CONST_INIT std::atomic<bool> g_hashtablez_enabled{
false
};
ABSL_CONST_INIT std::atomic<int32_t> g_hashtablez_sample_parameter{1 << 10};
std::atomic<HashtablezConfigListener> g_hashtablez_config_listener{nullptr};
#if defined(ABSL_INTERNAL_HASHTABLEZ_SAMPLE)
ABSL_PER_THREAD_TLS_KEYWORD absl::profiling_internal::ExponentialBiased
g_exponential_biased_generator;
#endif
void TriggerHashtablezConfigListener() {
auto* listener = g_hashtablez_config_listener.load(std::memory_order_acquire);
if (listener != nullptr) listener();
}
} // namespace
#if defined(ABSL_INTERNAL_HASHTABLEZ_SAMPLE)
ABSL_PER_THREAD_TLS_KEYWORD SamplingState global_next_sample = {0, 0};
#endif // defined(ABSL_INTERNAL_HASHTABLEZ_SAMPLE)
HashtablezSampler& GlobalHashtablezSampler() {
static absl::NoDestructor<HashtablezSampler> sampler;
return *sampler;
}
HashtablezInfo::HashtablezInfo() = default;
HashtablezInfo::~HashtablezInfo() = default;
void HashtablezInfo::PrepareForSampling(int64_t stride,
size_t inline_element_size_value,
size_t key_size_value,
size_t value_size_value,
uint16_t soo_capacity_value) {
capacity.store(0, std::memory_order_relaxed);
size.store(0, std::memory_order_relaxed);
num_erases.store(0, std::memory_order_relaxed);
num_rehashes.store(0, std::memory_order_relaxed);
max_probe_length.store(0, std::memory_order_relaxed);
total_probe_length.store(0, std::memory_order_relaxed);
hashes_bitwise_or.store(0, std::memory_order_relaxed);
hashes_bitwise_and.store(~size_t{}, std::memory_order_relaxed);
hashes_bitwise_xor.store(0, std::memory_order_relaxed);
max_reserve.store(0, std::memory_order_relaxed);
create_time = absl::Now();
weight = stride;
// The inliner makes hardcoded skip_count difficult (especially when combined
// with LTO). We use the ability to exclude stacks by regex when encoding
// instead.
depth = absl::GetStackTrace(stack, HashtablezInfo::kMaxStackDepth,
/* skip_count= */ 0);
inline_element_size = inline_element_size_value;
key_size = key_size_value;
value_size = value_size_value;
soo_capacity = soo_capacity_value;
}
static bool ShouldForceSampling() {
enum ForceState {
kDontForce,
kForce,
kUninitialized
};
ABSL_CONST_INIT static std::atomic<ForceState> global_state{
kUninitialized};
ForceState state = global_state.load(std::memory_order_relaxed);
if (ABSL_PREDICT_TRUE(state == kDontForce)) return false;
if (state == kUninitialized) {
state = ABSL_INTERNAL_C_SYMBOL(AbslContainerInternalSampleEverything)()
? kForce
: kDontForce;
global_state.store(state, std::memory_order_relaxed);
}
return state == kForce;
}
#if defined(ABSL_INTERNAL_HASHTABLEZ_SAMPLE)
HashtablezInfoHandle ForcedTrySample(size_t inline_element_size,
size_t key_size, size_t value_size,
uint16_t soo_capacity) {
return HashtablezInfoHandle(SampleSlow(global_next_sample,
inline_element_size, key_size,
value_size, soo_capacity));
}
void TestOnlyRefreshSamplingStateForCurrentThread() {
global_next_sample.next_sample =
g_hashtablez_sample_parameter.load(std::memory_order_relaxed);
global_next_sample.sample_stride = global_next_sample.next_sample;
}
#else
HashtablezInfoHandle ForcedTrySample(size_t, size_t, size_t, uint16_t) {
return HashtablezInfoHandle{nullptr};
}
void TestOnlyRefreshSamplingStateForCurrentThread() {}
#endif // ABSL_INTERNAL_HASHTABLEZ_SAMPLE
HashtablezInfo* SampleSlow(SamplingState& next_sample,
size_t inline_element_size, size_t key_size,
size_t value_size, uint16_t soo_capacity) {
if (ABSL_PREDICT_FALSE(ShouldForceSampling())) {
next_sample.next_sample = 1;
const int64_t old_stride = exchange(next_sample.sample_stride, 1);
HashtablezInfo* result = GlobalHashtablezSampler().Register(
old_stride, inline_element_size, key_size, value_size, soo_capacity);
return result;
}
#if !defined(ABSL_INTERNAL_HASHTABLEZ_SAMPLE)
next_sample = {
std::numeric_limits<int64_t>::max(),
std::numeric_limits<int64_t>::max(),
};
return nullptr;
#else
bool first = next_sample.next_sample < 0;
const int64_t next_stride = g_exponential_biased_generator.GetStride(
g_hashtablez_sample_parameter.load(std::memory_order_relaxed));
next_sample.next_sample = next_stride;
const int64_t old_stride = exchange(next_sample.sample_stride, next_stride);
// Small values of interval are equivalent to just sampling next time.
ABSL_ASSERT(next_stride >= 1);
// g_hashtablez_enabled can be dynamically flipped, we need to set a threshold
// low enough that we will start sampling in a reasonable time, so we just use
// the default sampling rate.
if (!g_hashtablez_enabled.load(std::memory_order_relaxed)) return nullptr;
// We will only be negative on our first count, so we should just retry in
// that case.
if (first) {
if (ABSL_PREDICT_TRUE(--next_sample.next_sample > 0)) return nullptr;
return SampleSlow(next_sample, inline_element_size, key_size, value_size,
soo_capacity);
}
return GlobalHashtablezSampler().Register(old_stride, inline_element_size,
key_size, value_size, soo_capacity);
#endif
}
void UnsampleSlow(HashtablezInfo* info) {
GlobalHashtablezSampler().Unregister(info);
}
void RecordRehashSlow(HashtablezInfo* info, size_t total_probe_length) {
#ifdef ABSL_INTERNAL_HAVE_SSE2
total_probe_length /= 16;
#else
total_probe_length /= 8;
#endif
info->total_probe_length.store(total_probe_length, std::memory_order_relaxed);
info->num_erases.store(0, std::memory_order_relaxed);
// There is only one concurrent writer, so `load` then `store` is sufficient
// instead of using `fetch_add`.
info->num_rehashes.store(
1 + info->num_rehashes.load(std::memory_order_relaxed),
std::memory_order_relaxed);
}
void RecordReservationSlow(HashtablezInfo* info, size_t target_capacity) {
info->max_reserve.store(
(std::max)(info->max_reserve.load(std::memory_order_relaxed),
target_capacity),
std::memory_order_relaxed);
}
void RecordClearedReservationSlow(HashtablezInfo* info) {
info->max_reserve.store(0, std::memory_order_relaxed);
}
void RecordStorageChangedSlow(HashtablezInfo* info, size_t size,
size_t capacity) {
info->size.store(size, std::memory_order_relaxed);
info->capacity.store(capacity, std::memory_order_relaxed);
if (size == 0) {
// This is a clear, reset the total/num_erases too.
info->total_probe_length.store(0, std::memory_order_relaxed);
info->num_erases.store(0, std::memory_order_relaxed);
}
}
void RecordInsertSlow(HashtablezInfo* info, size_t hash,
size_t distance_from_desired) {
// SwissTables probe in groups of 16, so scale this to count items probes and
// not offset from desired.
size_t probe_length = distance_from_desired;
#ifdef ABSL_INTERNAL_HAVE_SSE2
probe_length /= 16;
#else
probe_length /= 8;
#endif
info->hashes_bitwise_and.fetch_and(hash, std::memory_order_relaxed);
info->hashes_bitwise_or.fetch_or(hash, std::memory_order_relaxed);
info->hashes_bitwise_xor.fetch_xor(hash, std::memory_order_relaxed);
info->max_probe_length.store(
std::max(info->max_probe_length.load(std::memory_order_relaxed),
probe_length),
std::memory_order_relaxed);
info->total_probe_length.fetch_add(probe_length, std::memory_order_relaxed);
info->size.fetch_add(1, std::memory_order_relaxed);
}
void RecordEraseSlow(HashtablezInfo* info) {
info->size.fetch_sub(1, std::memory_order_relaxed);
// There is only one concurrent writer, so `load` then `store` is sufficient
// instead of using `fetch_add`.
info->num_erases.store(1 + info->num_erases.load(std::memory_order_relaxed),
std::memory_order_relaxed);
}
void SetHashtablezConfigListener(HashtablezConfigListener l) {
g_hashtablez_config_listener.store(l, std::memory_order_release);
}
bool IsHashtablezEnabled() {
return g_hashtablez_enabled.load(std::memory_order_acquire);
}
void SetHashtablezEnabled(bool enabled) {
SetHashtablezEnabledInternal(enabled);
TriggerHashtablezConfigListener();
}
void SetHashtablezEnabledInternal(bool enabled) {
g_hashtablez_enabled.store(enabled, std::memory_order_release);
}
int32_t GetHashtablezSampleParameter() {
return g_hashtablez_sample_parameter.load(std::memory_order_acquire);
}
void SetHashtablezSampleParameter(int32_t rate) {
SetHashtablezSampleParameterInternal(rate);
TriggerHashtablezConfigListener();
}
void SetHashtablezSampleParameterInternal(int32_t rate) {
if (rate > 0) {
g_hashtablez_sample_parameter.store(rate, std::memory_order_release);
} else {
ABSL_RAW_LOG(ERROR, "Invalid hashtablez sample rate: %lld",
static_cast<long long>(rate)); // NOLINT(runtime/int)
}
}
size_t GetHashtablezMaxSamples() {
return GlobalHashtablezSampler().GetMaxSamples();
}
void SetHashtablezMaxSamples(size_t max) {
SetHashtablezMaxSamplesInternal(max);
TriggerHashtablezConfigListener();
}
void SetHashtablezMaxSamplesInternal(size_t max) {
if (max > 0) {
GlobalHashtablezSampler().SetMaxSamples(max);
} else {
ABSL_RAW_LOG(ERROR, "Invalid hashtablez max samples: 0");
}
}
} // namespace container_internal
ABSL_NAMESPACE_END
} // namespace absl

View file

@ -0,0 +1,294 @@
// Copyright 2018 The Abseil Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// -----------------------------------------------------------------------------
// File: hashtablez_sampler.h
// -----------------------------------------------------------------------------
//
// This header file defines the API for a low level library to sample hashtables
// and collect runtime statistics about them.
//
// `HashtablezSampler` controls the lifecycle of `HashtablezInfo` objects which
// store information about a single sample.
//
// `Record*` methods store information into samples.
// `Sample()` and `Unsample()` make use of a single global sampler with
// properties controlled by the flags hashtablez_enabled,
// hashtablez_sample_rate, and hashtablez_max_samples.
//
// WARNING
//
// Using this sampling API may cause sampled Swiss tables to use the global
// allocator (operator `new`) in addition to any custom allocator. If you
// are using a table in an unusual circumstance where allocation or calling a
// linux syscall is unacceptable, this could interfere.
//
// This utility is internal-only. Use at your own risk.
#ifndef ABSL_CONTAINER_INTERNAL_HASHTABLEZ_SAMPLER_H_
#define ABSL_CONTAINER_INTERNAL_HASHTABLEZ_SAMPLER_H_
#include <atomic>
#include <cstddef>
#include <cstdint>
#include <functional>
#include <memory>
#include <vector>
#include "absl/base/attributes.h"
#include "absl/base/config.h"
#include "absl/base/internal/per_thread_tls.h"
#include "absl/base/optimization.h"
#include "absl/base/thread_annotations.h"
#include "absl/profiling/internal/sample_recorder.h"
#include "absl/synchronization/mutex.h"
#include "absl/time/time.h"
#include "absl/utility/utility.h"
namespace absl {
ABSL_NAMESPACE_BEGIN
namespace container_internal {
// Stores information about a sampled hashtable. All mutations to this *must*
// be made through `Record*` functions below. All reads from this *must* only
// occur in the callback to `HashtablezSampler::Iterate`.
struct HashtablezInfo : public profiling_internal::Sample<HashtablezInfo> {
// Constructs the object but does not fill in any fields.
HashtablezInfo();
~HashtablezInfo();
HashtablezInfo(const HashtablezInfo&) = delete;
HashtablezInfo& operator=(const HashtablezInfo&) = delete;
// Puts the object into a clean state, fills in the logically `const` members,
// blocking for any readers that are currently sampling the object.
void PrepareForSampling(int64_t stride, size_t inline_element_size_value,
size_t key_size, size_t value_size,
uint16_t soo_capacity_value)
ABSL_EXCLUSIVE_LOCKS_REQUIRED(init_mu);
// These fields are mutated by the various Record* APIs and need to be
// thread-safe.
std::atomic<size_t> capacity;
std::atomic<size_t> size;
std::atomic<size_t> num_erases;
std::atomic<size_t> num_rehashes;
std::atomic<size_t> max_probe_length;
std::atomic<size_t> total_probe_length;
std::atomic<size_t> hashes_bitwise_or;
std::atomic<size_t> hashes_bitwise_and;
std::atomic<size_t> hashes_bitwise_xor;
std::atomic<size_t> max_reserve;
// All of the fields below are set by `PrepareForSampling`, they must not be
// mutated in `Record*` functions. They are logically `const` in that sense.
// These are guarded by init_mu, but that is not externalized to clients,
// which can read them only during `SampleRecorder::Iterate` which will hold
// the lock.
static constexpr int kMaxStackDepth = 64;
absl::Time create_time;
int32_t depth;
// The SOO capacity for this table in elements (not bytes). Note that sampled
// tables are never SOO because we need to store the infoz handle on the heap.
// Tables that would be SOO if not sampled should have: soo_capacity > 0 &&
// size <= soo_capacity && max_reserve <= soo_capacity.
uint16_t soo_capacity;
void* stack[kMaxStackDepth];
size_t inline_element_size; // How big is the slot in bytes?
size_t key_size; // sizeof(key_type)
size_t value_size; // sizeof(value_type)
};
void RecordRehashSlow(HashtablezInfo* info, size_t total_probe_length);
void RecordReservationSlow(HashtablezInfo* info, size_t target_capacity);
void RecordClearedReservationSlow(HashtablezInfo* info);
void RecordStorageChangedSlow(HashtablezInfo* info, size_t size,
size_t capacity);
void RecordInsertSlow(HashtablezInfo* info, size_t hash,
size_t distance_from_desired);
void RecordEraseSlow(HashtablezInfo* info);
struct SamplingState {
int64_t next_sample;
// When we make a sampling decision, we record that distance so we can weight
// each sample.
int64_t sample_stride;
};
HashtablezInfo* SampleSlow(SamplingState& next_sample,
size_t inline_element_size, size_t key_size,
size_t value_size, uint16_t soo_capacity);
void UnsampleSlow(HashtablezInfo* info);
#if defined(ABSL_INTERNAL_HASHTABLEZ_SAMPLE)
#error ABSL_INTERNAL_HASHTABLEZ_SAMPLE cannot be directly set
#endif // defined(ABSL_INTERNAL_HASHTABLEZ_SAMPLE)
#if defined(ABSL_INTERNAL_HASHTABLEZ_SAMPLE)
class HashtablezInfoHandle {
public:
explicit HashtablezInfoHandle() : info_(nullptr) {}
explicit HashtablezInfoHandle(HashtablezInfo* info) : info_(info) {}
// We do not have a destructor. Caller is responsible for calling Unregister
// before destroying the handle.
void Unregister() {
if (ABSL_PREDICT_TRUE(info_ == nullptr)) return;
UnsampleSlow(info_);
}
inline bool IsSampled() const { return ABSL_PREDICT_FALSE(info_ != nullptr); }
inline void RecordStorageChanged(size_t size, size_t capacity) {
if (ABSL_PREDICT_TRUE(info_ == nullptr)) return;
RecordStorageChangedSlow(info_, size, capacity);
}
inline void RecordRehash(size_t total_probe_length) {
if (ABSL_PREDICT_TRUE(info_ == nullptr)) return;
RecordRehashSlow(info_, total_probe_length);
}
inline void RecordReservation(size_t target_capacity) {
if (ABSL_PREDICT_TRUE(info_ == nullptr)) return;
RecordReservationSlow(info_, target_capacity);
}
inline void RecordClearedReservation() {
if (ABSL_PREDICT_TRUE(info_ == nullptr)) return;
RecordClearedReservationSlow(info_);
}
inline void RecordInsert(size_t hash, size_t distance_from_desired) {
if (ABSL_PREDICT_TRUE(info_ == nullptr)) return;
RecordInsertSlow(info_, hash, distance_from_desired);
}
inline void RecordErase() {
if (ABSL_PREDICT_TRUE(info_ == nullptr)) return;
RecordEraseSlow(info_);
}
friend inline void swap(HashtablezInfoHandle& lhs,
HashtablezInfoHandle& rhs) {
std::swap(lhs.info_, rhs.info_);
}
private:
friend class HashtablezInfoHandlePeer;
HashtablezInfo* info_;
};
#else
// Ensure that when Hashtablez is turned off at compile time, HashtablezInfo can
// be removed by the linker, in order to reduce the binary size.
class HashtablezInfoHandle {
public:
explicit HashtablezInfoHandle() = default;
explicit HashtablezInfoHandle(std::nullptr_t) {}
inline void Unregister() {}
inline bool IsSampled() const { return false; }
inline void RecordStorageChanged(size_t /*size*/, size_t /*capacity*/) {}
inline void RecordRehash(size_t /*total_probe_length*/) {}
inline void RecordReservation(size_t /*target_capacity*/) {}
inline void RecordClearedReservation() {}
inline void RecordInsert(size_t /*hash*/, size_t /*distance_from_desired*/) {}
inline void RecordErase() {}
friend inline void swap(HashtablezInfoHandle& /*lhs*/,
HashtablezInfoHandle& /*rhs*/) {}
};
#endif // defined(ABSL_INTERNAL_HASHTABLEZ_SAMPLE)
#if defined(ABSL_INTERNAL_HASHTABLEZ_SAMPLE)
extern ABSL_PER_THREAD_TLS_KEYWORD SamplingState global_next_sample;
#endif // defined(ABSL_INTERNAL_HASHTABLEZ_SAMPLE)
// Returns true if the next table should be sampled.
// This function updates the global state.
// If the function returns true, actual sampling should be done by calling
// ForcedTrySample().
inline bool ShouldSampleNextTable() {
#if defined(ABSL_INTERNAL_HASHTABLEZ_SAMPLE)
if (ABSL_PREDICT_TRUE(--global_next_sample.next_sample > 0)) {
return false;
}
return true;
#else
return false;
#endif // ABSL_INTERNAL_HASHTABLEZ_SAMPLE
}
// Returns a sampling handle.
// Must be called only if HashSetShouldBeSampled() returned true.
// Returned handle still can be unsampled if sampling is not possible.
HashtablezInfoHandle ForcedTrySample(size_t inline_element_size,
size_t key_size, size_t value_size,
uint16_t soo_capacity);
// In case sampling needs to be disabled and re-enabled in tests, this function
// can be used to reset the sampling state for the current thread.
// It is useful to avoid sampling attempts and sampling delays in tests.
void TestOnlyRefreshSamplingStateForCurrentThread();
// Returns a sampling handle.
inline HashtablezInfoHandle Sample(size_t inline_element_size, size_t key_size,
size_t value_size, uint16_t soo_capacity) {
if (ABSL_PREDICT_TRUE(!ShouldSampleNextTable())) {
return HashtablezInfoHandle(nullptr);
}
return ForcedTrySample(inline_element_size, key_size, value_size,
soo_capacity);
}
using HashtablezSampler =
::absl::profiling_internal::SampleRecorder<HashtablezInfo>;
// Returns a global Sampler.
HashtablezSampler& GlobalHashtablezSampler();
using HashtablezConfigListener = void (*)();
void SetHashtablezConfigListener(HashtablezConfigListener l);
// Enables or disables sampling for Swiss tables.
bool IsHashtablezEnabled();
void SetHashtablezEnabled(bool enabled);
void SetHashtablezEnabledInternal(bool enabled);
// Sets the rate at which Swiss tables will be sampled.
int32_t GetHashtablezSampleParameter();
void SetHashtablezSampleParameter(int32_t rate);
void SetHashtablezSampleParameterInternal(int32_t rate);
// Sets a soft max for the number of samples that will be kept.
size_t GetHashtablezMaxSamples();
void SetHashtablezMaxSamples(size_t max);
void SetHashtablezMaxSamplesInternal(size_t max);
// Configuration override.
// This allows process-wide sampling without depending on order of
// initialization of static storage duration objects.
// The definition of this constant is weak, which allows us to inject a
// different value for it at link time.
extern "C" bool ABSL_INTERNAL_C_SYMBOL(AbslContainerInternalSampleEverything)();
} // namespace container_internal
ABSL_NAMESPACE_END
} // namespace absl
#endif // ABSL_CONTAINER_INTERNAL_HASHTABLEZ_SAMPLER_H_

View file

@ -0,0 +1,31 @@
// Copyright 2018 The Abseil Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "absl/container/internal/hashtablez_sampler.h"
#include "absl/base/attributes.h"
namespace absl {
ABSL_NAMESPACE_BEGIN
namespace container_internal {
// See hashtablez_sampler.h for details.
extern "C" ABSL_ATTRIBUTE_WEAK bool ABSL_INTERNAL_C_SYMBOL(
AbslContainerInternalSampleEverything)() {
return false;
}
} // namespace container_internal
ABSL_NAMESPACE_END
} // namespace absl

View file

@ -0,0 +1,518 @@
// Copyright 2018 The Abseil Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "absl/container/internal/hashtablez_sampler.h"
#include <atomic>
#include <cassert>
#include <cstddef>
#include <cstdint>
#include <limits>
#include <random>
#include <vector>
#include "gmock/gmock.h"
#include "gtest/gtest.h"
#include "absl/base/attributes.h"
#include "absl/base/config.h"
#include "absl/profiling/internal/sample_recorder.h"
#include "absl/synchronization/blocking_counter.h"
#include "absl/synchronization/internal/thread_pool.h"
#include "absl/synchronization/mutex.h"
#include "absl/synchronization/notification.h"
#include "absl/time/clock.h"
#include "absl/time/time.h"
#ifdef ABSL_INTERNAL_HAVE_SSE2
constexpr int kProbeLength = 16;
#else
constexpr int kProbeLength = 8;
#endif
namespace absl {
ABSL_NAMESPACE_BEGIN
namespace container_internal {
#if defined(ABSL_INTERNAL_HASHTABLEZ_SAMPLE)
class HashtablezInfoHandlePeer {
public:
static HashtablezInfo* GetInfo(HashtablezInfoHandle* h) { return h->info_; }
};
#else
class HashtablezInfoHandlePeer {
public:
static HashtablezInfo* GetInfo(HashtablezInfoHandle*) { return nullptr; }
};
#endif // defined(ABSL_INTERNAL_HASHTABLEZ_SAMPLE)
namespace {
using ::absl::synchronization_internal::ThreadPool;
using ::testing::IsEmpty;
using ::testing::UnorderedElementsAre;
std::vector<size_t> GetSizes(HashtablezSampler* s) {
std::vector<size_t> res;
s->Iterate([&](const HashtablezInfo& info) {
res.push_back(info.size.load(std::memory_order_acquire));
});
return res;
}
HashtablezInfo* Register(HashtablezSampler* s, size_t size) {
const int64_t test_stride = 123;
const size_t test_element_size = 17;
const size_t test_key_size = 3;
const size_t test_value_size = 5;
auto* info =
s->Register(test_stride, test_element_size, /*key_size=*/test_key_size,
/*value_size=*/test_value_size, /*soo_capacity=*/0);
assert(info != nullptr);
info->size.store(size);
return info;
}
TEST(HashtablezInfoTest, PrepareForSampling) {
absl::Time test_start = absl::Now();
const int64_t test_stride = 123;
const size_t test_element_size = 17;
const size_t test_key_size = 15;
const size_t test_value_size = 13;
HashtablezInfo info;
absl::MutexLock l(&info.init_mu);
info.PrepareForSampling(test_stride, test_element_size,
/*key_size=*/test_key_size,
/*value_size=*/test_value_size,
/*soo_capacity_value=*/1);
EXPECT_EQ(info.capacity.load(), 0);
EXPECT_EQ(info.size.load(), 0);
EXPECT_EQ(info.num_erases.load(), 0);
EXPECT_EQ(info.num_rehashes.load(), 0);
EXPECT_EQ(info.max_probe_length.load(), 0);
EXPECT_EQ(info.total_probe_length.load(), 0);
EXPECT_EQ(info.hashes_bitwise_or.load(), 0);
EXPECT_EQ(info.hashes_bitwise_and.load(), ~size_t{});
EXPECT_EQ(info.hashes_bitwise_xor.load(), 0);
EXPECT_EQ(info.max_reserve.load(), 0);
EXPECT_GE(info.create_time, test_start);
EXPECT_EQ(info.weight, test_stride);
EXPECT_EQ(info.inline_element_size, test_element_size);
EXPECT_EQ(info.key_size, test_key_size);
EXPECT_EQ(info.value_size, test_value_size);
EXPECT_EQ(info.soo_capacity, 1);
info.capacity.store(1, std::memory_order_relaxed);
info.size.store(1, std::memory_order_relaxed);
info.num_erases.store(1, std::memory_order_relaxed);
info.max_probe_length.store(1, std::memory_order_relaxed);
info.total_probe_length.store(1, std::memory_order_relaxed);
info.hashes_bitwise_or.store(1, std::memory_order_relaxed);
info.hashes_bitwise_and.store(1, std::memory_order_relaxed);
info.hashes_bitwise_xor.store(1, std::memory_order_relaxed);
info.max_reserve.store(1, std::memory_order_relaxed);
info.create_time = test_start - absl::Hours(20);
info.PrepareForSampling(test_stride * 2, test_element_size,
/*key_size=*/test_key_size,
/*value_size=*/test_value_size,
/*soo_capacity_value=*/0);
EXPECT_EQ(info.capacity.load(), 0);
EXPECT_EQ(info.size.load(), 0);
EXPECT_EQ(info.num_erases.load(), 0);
EXPECT_EQ(info.num_rehashes.load(), 0);
EXPECT_EQ(info.max_probe_length.load(), 0);
EXPECT_EQ(info.total_probe_length.load(), 0);
EXPECT_EQ(info.hashes_bitwise_or.load(), 0);
EXPECT_EQ(info.hashes_bitwise_and.load(), ~size_t{});
EXPECT_EQ(info.hashes_bitwise_xor.load(), 0);
EXPECT_EQ(info.max_reserve.load(), 0);
EXPECT_EQ(info.weight, 2 * test_stride);
EXPECT_EQ(info.inline_element_size, test_element_size);
EXPECT_EQ(info.key_size, test_key_size);
EXPECT_EQ(info.value_size, test_value_size);
EXPECT_GE(info.create_time, test_start);
EXPECT_EQ(info.soo_capacity, 0);
}
TEST(HashtablezInfoTest, RecordStorageChanged) {
HashtablezInfo info;
absl::MutexLock l(&info.init_mu);
const int64_t test_stride = 21;
const size_t test_element_size = 19;
const size_t test_key_size = 17;
const size_t test_value_size = 15;
info.PrepareForSampling(test_stride, test_element_size,
/*key_size=*/test_key_size,
/*value_size=*/test_value_size,
/*soo_capacity_value=*/0);
RecordStorageChangedSlow(&info, 17, 47);
EXPECT_EQ(info.size.load(), 17);
EXPECT_EQ(info.capacity.load(), 47);
RecordStorageChangedSlow(&info, 20, 20);
EXPECT_EQ(info.size.load(), 20);
EXPECT_EQ(info.capacity.load(), 20);
}
TEST(HashtablezInfoTest, RecordInsert) {
HashtablezInfo info;
absl::MutexLock l(&info.init_mu);
const int64_t test_stride = 25;
const size_t test_element_size = 23;
const size_t test_key_size = 21;
const size_t test_value_size = 19;
info.PrepareForSampling(test_stride, test_element_size,
/*key_size=*/test_key_size,
/*value_size=*/test_value_size,
/*soo_capacity_value=*/0);
EXPECT_EQ(info.max_probe_length.load(), 0);
RecordInsertSlow(&info, 0x0000FF00, 6 * kProbeLength);
EXPECT_EQ(info.max_probe_length.load(), 6);
EXPECT_EQ(info.hashes_bitwise_and.load(), 0x0000FF00);
EXPECT_EQ(info.hashes_bitwise_or.load(), 0x0000FF00);
EXPECT_EQ(info.hashes_bitwise_xor.load(), 0x0000FF00);
RecordInsertSlow(&info, 0x000FF000, 4 * kProbeLength);
EXPECT_EQ(info.max_probe_length.load(), 6);
EXPECT_EQ(info.hashes_bitwise_and.load(), 0x0000F000);
EXPECT_EQ(info.hashes_bitwise_or.load(), 0x000FFF00);
EXPECT_EQ(info.hashes_bitwise_xor.load(), 0x000F0F00);
RecordInsertSlow(&info, 0x00FF0000, 12 * kProbeLength);
EXPECT_EQ(info.max_probe_length.load(), 12);
EXPECT_EQ(info.hashes_bitwise_and.load(), 0x00000000);
EXPECT_EQ(info.hashes_bitwise_or.load(), 0x00FFFF00);
EXPECT_EQ(info.hashes_bitwise_xor.load(), 0x00F00F00);
}
TEST(HashtablezInfoTest, RecordErase) {
const int64_t test_stride = 31;
const size_t test_element_size = 29;
const size_t test_key_size = 27;
const size_t test_value_size = 25;
HashtablezInfo info;
absl::MutexLock l(&info.init_mu);
info.PrepareForSampling(test_stride, test_element_size,
/*key_size=*/test_key_size,
/*value_size=*/test_value_size,
/*soo_capacity_value=*/1);
EXPECT_EQ(info.num_erases.load(), 0);
EXPECT_EQ(info.size.load(), 0);
RecordInsertSlow(&info, 0x0000FF00, 6 * kProbeLength);
EXPECT_EQ(info.size.load(), 1);
RecordEraseSlow(&info);
EXPECT_EQ(info.size.load(), 0);
EXPECT_EQ(info.num_erases.load(), 1);
EXPECT_EQ(info.inline_element_size, test_element_size);
EXPECT_EQ(info.key_size, test_key_size);
EXPECT_EQ(info.value_size, test_value_size);
EXPECT_EQ(info.soo_capacity, 1);
}
TEST(HashtablezInfoTest, RecordRehash) {
const int64_t test_stride = 33;
const size_t test_element_size = 31;
const size_t test_key_size = 29;
const size_t test_value_size = 27;
HashtablezInfo info;
absl::MutexLock l(&info.init_mu);
info.PrepareForSampling(test_stride, test_element_size,
/*key_size=*/test_key_size,
/*value_size=*/test_value_size,
/*soo_capacity_value=*/0);
RecordInsertSlow(&info, 0x1, 0);
RecordInsertSlow(&info, 0x2, kProbeLength);
RecordInsertSlow(&info, 0x4, kProbeLength);
RecordInsertSlow(&info, 0x8, 2 * kProbeLength);
EXPECT_EQ(info.size.load(), 4);
EXPECT_EQ(info.total_probe_length.load(), 4);
RecordEraseSlow(&info);
RecordEraseSlow(&info);
EXPECT_EQ(info.size.load(), 2);
EXPECT_EQ(info.total_probe_length.load(), 4);
EXPECT_EQ(info.num_erases.load(), 2);
RecordRehashSlow(&info, 3 * kProbeLength);
EXPECT_EQ(info.size.load(), 2);
EXPECT_EQ(info.total_probe_length.load(), 3);
EXPECT_EQ(info.num_erases.load(), 0);
EXPECT_EQ(info.num_rehashes.load(), 1);
EXPECT_EQ(info.inline_element_size, test_element_size);
EXPECT_EQ(info.key_size, test_key_size);
EXPECT_EQ(info.value_size, test_value_size);
EXPECT_EQ(info.soo_capacity, 0);
}
TEST(HashtablezInfoTest, RecordReservation) {
HashtablezInfo info;
absl::MutexLock l(&info.init_mu);
const int64_t test_stride = 35;
const size_t test_element_size = 33;
const size_t test_key_size = 31;
const size_t test_value_size = 29;
info.PrepareForSampling(test_stride, test_element_size,
/*key_size=*/test_key_size,
/*value_size=*/test_value_size,
/*soo_capacity_value=*/0);
RecordReservationSlow(&info, 3);
EXPECT_EQ(info.max_reserve.load(), 3);
RecordReservationSlow(&info, 2);
// High watermark does not change
EXPECT_EQ(info.max_reserve.load(), 3);
RecordReservationSlow(&info, 10);
// High watermark does change
EXPECT_EQ(info.max_reserve.load(), 10);
}
#if defined(ABSL_INTERNAL_HASHTABLEZ_SAMPLE)
TEST(HashtablezSamplerTest, SmallSampleParameter) {
const size_t test_element_size = 31;
const size_t test_key_size = 33;
const size_t test_value_size = 35;
SetHashtablezEnabled(true);
SetHashtablezSampleParameter(100);
for (int i = 0; i < 1000; ++i) {
SamplingState next_sample = {0, 0};
HashtablezInfo* sample =
SampleSlow(next_sample, test_element_size,
/*key_size=*/test_key_size, /*value_size=*/test_value_size,
/*soo_capacity=*/0);
EXPECT_GT(next_sample.next_sample, 0);
EXPECT_EQ(next_sample.next_sample, next_sample.sample_stride);
EXPECT_NE(sample, nullptr);
UnsampleSlow(sample);
}
}
TEST(HashtablezSamplerTest, LargeSampleParameter) {
const size_t test_element_size = 31;
const size_t test_key_size = 33;
const size_t test_value_size = 35;
SetHashtablezEnabled(true);
SetHashtablezSampleParameter(std::numeric_limits<int32_t>::max());
for (int i = 0; i < 1000; ++i) {
SamplingState next_sample = {0, 0};
HashtablezInfo* sample =
SampleSlow(next_sample, test_element_size,
/*key_size=*/test_key_size, /*value_size=*/test_value_size,
/*soo_capacity=*/0);
EXPECT_GT(next_sample.next_sample, 0);
EXPECT_EQ(next_sample.next_sample, next_sample.sample_stride);
EXPECT_NE(sample, nullptr);
UnsampleSlow(sample);
}
}
TEST(HashtablezSamplerTest, Sample) {
const size_t test_element_size = 31;
const size_t test_key_size = 33;
const size_t test_value_size = 35;
SetHashtablezEnabled(true);
SetHashtablezSampleParameter(100);
int64_t num_sampled = 0;
int64_t total = 0;
double sample_rate = 0.0;
for (int i = 0; i < 1000000; ++i) {
HashtablezInfoHandle h =
Sample(test_element_size,
/*key_size=*/test_key_size, /*value_size=*/test_value_size,
/*soo_capacity=*/0);
++total;
if (h.IsSampled()) {
++num_sampled;
}
sample_rate = static_cast<double>(num_sampled) / total;
if (0.005 < sample_rate && sample_rate < 0.015) break;
}
EXPECT_NEAR(sample_rate, 0.01, 0.005);
}
TEST(HashtablezSamplerTest, Handle) {
auto& sampler = GlobalHashtablezSampler();
const int64_t test_stride = 41;
const size_t test_element_size = 39;
const size_t test_key_size = 37;
const size_t test_value_size = 35;
HashtablezInfoHandle h(sampler.Register(test_stride, test_element_size,
/*key_size=*/test_key_size,
/*value_size=*/test_value_size,
/*soo_capacity=*/0));
auto* info = HashtablezInfoHandlePeer::GetInfo(&h);
info->hashes_bitwise_and.store(0x12345678, std::memory_order_relaxed);
bool found = false;
sampler.Iterate([&](const HashtablezInfo& h) {
if (&h == info) {
EXPECT_EQ(h.weight, test_stride);
EXPECT_EQ(h.hashes_bitwise_and.load(), 0x12345678);
found = true;
}
});
EXPECT_TRUE(found);
h.Unregister();
h = HashtablezInfoHandle();
found = false;
sampler.Iterate([&](const HashtablezInfo& h) {
if (&h == info) {
// this will only happen if some other thread has resurrected the info
// the old handle was using.
if (h.hashes_bitwise_and.load() == 0x12345678) {
found = true;
}
}
});
EXPECT_FALSE(found);
}
#endif
TEST(HashtablezSamplerTest, Registration) {
HashtablezSampler sampler;
auto* info1 = Register(&sampler, 1);
EXPECT_THAT(GetSizes(&sampler), UnorderedElementsAre(1));
auto* info2 = Register(&sampler, 2);
EXPECT_THAT(GetSizes(&sampler), UnorderedElementsAre(1, 2));
info1->size.store(3);
EXPECT_THAT(GetSizes(&sampler), UnorderedElementsAre(3, 2));
sampler.Unregister(info1);
sampler.Unregister(info2);
}
TEST(HashtablezSamplerTest, Unregistration) {
HashtablezSampler sampler;
std::vector<HashtablezInfo*> infos;
for (size_t i = 0; i < 3; ++i) {
infos.push_back(Register(&sampler, i));
}
EXPECT_THAT(GetSizes(&sampler), UnorderedElementsAre(0, 1, 2));
sampler.Unregister(infos[1]);
EXPECT_THAT(GetSizes(&sampler), UnorderedElementsAre(0, 2));
infos.push_back(Register(&sampler, 3));
infos.push_back(Register(&sampler, 4));
EXPECT_THAT(GetSizes(&sampler), UnorderedElementsAre(0, 2, 3, 4));
sampler.Unregister(infos[3]);
EXPECT_THAT(GetSizes(&sampler), UnorderedElementsAre(0, 2, 4));
sampler.Unregister(infos[0]);
sampler.Unregister(infos[2]);
sampler.Unregister(infos[4]);
EXPECT_THAT(GetSizes(&sampler), IsEmpty());
}
TEST(HashtablezSamplerTest, MultiThreaded) {
HashtablezSampler sampler;
Notification stop;
ThreadPool pool(10);
for (int i = 0; i < 10; ++i) {
const int64_t sampling_stride = 11 + i % 3;
const size_t elt_size = 10 + i % 2;
const size_t key_size = 12 + i % 4;
const size_t value_size = 13 + i % 5;
pool.Schedule([&sampler, &stop, sampling_stride, elt_size, key_size,
value_size]() {
std::random_device rd;
std::mt19937 gen(rd());
std::vector<HashtablezInfo*> infoz;
while (!stop.HasBeenNotified()) {
if (infoz.empty()) {
infoz.push_back(sampler.Register(sampling_stride, elt_size,
/*key_size=*/key_size,
/*value_size=*/value_size,
/*soo_capacity=*/0));
}
switch (std::uniform_int_distribution<>(0, 2)(gen)) {
case 0: {
infoz.push_back(sampler.Register(sampling_stride, elt_size,
/*key_size=*/key_size,
/*value_size=*/value_size,
/*soo_capacity=*/0));
break;
}
case 1: {
size_t p =
std::uniform_int_distribution<>(0, infoz.size() - 1)(gen);
HashtablezInfo* info = infoz[p];
infoz[p] = infoz.back();
infoz.pop_back();
EXPECT_EQ(info->weight, sampling_stride);
sampler.Unregister(info);
break;
}
case 2: {
absl::Duration oldest = absl::ZeroDuration();
sampler.Iterate([&](const HashtablezInfo& info) {
oldest = std::max(oldest, absl::Now() - info.create_time);
});
ASSERT_GE(oldest, absl::ZeroDuration());
break;
}
}
}
});
}
// The threads will hammer away. Give it a little bit of time for tsan to
// spot errors.
absl::SleepFor(absl::Seconds(3));
stop.Notify();
}
TEST(HashtablezSamplerTest, Callback) {
HashtablezSampler sampler;
auto* info1 = Register(&sampler, 1);
auto* info2 = Register(&sampler, 2);
static const HashtablezInfo* expected;
auto callback = [](const HashtablezInfo& info) {
// We can't use `info` outside of this callback because the object will be
// disposed as soon as we return from here.
EXPECT_EQ(&info, expected);
};
// Set the callback.
EXPECT_EQ(sampler.SetDisposeCallback(callback), nullptr);
expected = info1;
sampler.Unregister(info1);
// Unset the callback.
EXPECT_EQ(callback, sampler.SetDisposeCallback(nullptr));
expected = nullptr; // no more calls.
sampler.Unregister(info2);
}
} // namespace
} // namespace container_internal
ABSL_NAMESPACE_END
} // namespace absl

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,844 @@
// Copyright 2018 The Abseil Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// MOTIVATION AND TUTORIAL
//
// If you want to put in a single heap allocation N doubles followed by M ints,
// it's easy if N and M are known at compile time.
//
// struct S {
// double a[N];
// int b[M];
// };
//
// S* p = new S;
//
// But what if N and M are known only in run time? Class template Layout to the
// rescue! It's a portable generalization of the technique known as struct hack.
//
// // This object will tell us everything we need to know about the memory
// // layout of double[N] followed by int[M]. It's structurally identical to
// // size_t[2] that stores N and M. It's very cheap to create.
// const Layout<double, int> layout(N, M);
//
// // Allocate enough memory for both arrays. `AllocSize()` tells us how much
// // memory is needed. We are free to use any allocation function we want as
// // long as it returns aligned memory.
// std::unique_ptr<unsigned char[]> p(new unsigned char[layout.AllocSize()]);
//
// // Obtain the pointer to the array of doubles.
// // Equivalent to `reinterpret_cast<double*>(p.get())`.
// //
// // We could have written layout.Pointer<0>(p) instead. If all the types are
// // unique you can use either form, but if some types are repeated you must
// // use the index form.
// double* a = layout.Pointer<double>(p.get());
//
// // Obtain the pointer to the array of ints.
// // Equivalent to `reinterpret_cast<int*>(p.get() + N * 8)`.
// int* b = layout.Pointer<int>(p);
//
// If we are unable to specify sizes of all fields, we can pass as many sizes as
// we can to `Partial()`. In return, it'll allow us to access the fields whose
// locations and sizes can be computed from the provided information.
// `Partial()` comes in handy when the array sizes are embedded into the
// allocation.
//
// // size_t[0] containing N, size_t[1] containing M, double[N], int[M].
// using L = Layout<size_t, size_t, double, int>;
//
// unsigned char* Allocate(size_t n, size_t m) {
// const L layout(1, 1, n, m);
// unsigned char* p = new unsigned char[layout.AllocSize()];
// *layout.Pointer<0>(p) = n;
// *layout.Pointer<1>(p) = m;
// return p;
// }
//
// void Use(unsigned char* p) {
// // First, extract N and M.
// // Specify that the first array has only one element. Using `prefix` we
// // can access the first two arrays but not more.
// constexpr auto prefix = L::Partial(1);
// size_t n = *prefix.Pointer<0>(p);
// size_t m = *prefix.Pointer<1>(p);
//
// // Now we can get pointers to the payload.
// const L layout(1, 1, n, m);
// double* a = layout.Pointer<double>(p);
// int* b = layout.Pointer<int>(p);
// }
//
// The layout we used above combines fixed-size with dynamically-sized fields.
// This is quite common. Layout is optimized for this use case and attempts to
// generate optimal code. To help the compiler do that in more cases, you can
// specify the fixed sizes using `WithStaticSizes`. This ensures that all
// computations that can be performed at compile time are indeed performed at
// compile time. Note that sometimes the `template` keyword is needed. E.g.:
//
// using SL = L::template WithStaticSizes<1, 1>;
//
// void Use(unsigned char* p) {
// // First, extract N and M.
// // Using `prefix` we can access the first three arrays but not more.
// //
// // More details: The first element always has offset 0. `SL`
// // has offsets for the second and third array based on sizes of
// // the first and second array, specified via `WithStaticSizes`.
// constexpr auto prefix = SL::Partial();
// size_t n = *prefix.Pointer<0>(p);
// size_t m = *prefix.Pointer<1>(p);
//
// // Now we can get a pointer to the final payload.
// const SL layout(n, m);
// double* a = layout.Pointer<double>(p);
// int* b = layout.Pointer<int>(p);
// }
//
// Efficiency tip: The order of fields matters. In `Layout<T1, ..., TN>` try to
// ensure that `alignof(T1) >= ... >= alignof(TN)`. This way you'll have no
// padding in between arrays.
//
// You can manually override the alignment of an array by wrapping the type in
// `Aligned<T, N>`. `Layout<..., Aligned<T, N>, ...>` has exactly the same API
// and behavior as `Layout<..., T, ...>` except that the first element of the
// array of `T` is aligned to `N` (the rest of the elements follow without
// padding). `N` cannot be less than `alignof(T)`.
//
// `AllocSize()` and `Pointer()` are the most basic methods for dealing with
// memory layouts. Check out the reference or code below to discover more.
//
// EXAMPLE
//
// // Immutable move-only string with sizeof equal to sizeof(void*). The
// // string size and the characters are kept in the same heap allocation.
// class CompactString {
// public:
// CompactString(const char* s = "") {
// const size_t size = strlen(s);
// // size_t[1] followed by char[size + 1].
// const L layout(size + 1);
// p_.reset(new unsigned char[layout.AllocSize()]);
// // If running under ASAN, mark the padding bytes, if any, to catch
// // memory errors.
// layout.PoisonPadding(p_.get());
// // Store the size in the allocation.
// *layout.Pointer<size_t>(p_.get()) = size;
// // Store the characters in the allocation.
// memcpy(layout.Pointer<char>(p_.get()), s, size + 1);
// }
//
// size_t size() const {
// // Equivalent to reinterpret_cast<size_t&>(*p).
// return *L::Partial().Pointer<size_t>(p_.get());
// }
//
// const char* c_str() const {
// // Equivalent to reinterpret_cast<char*>(p.get() + sizeof(size_t)).
// return L::Partial().Pointer<char>(p_.get());
// }
//
// private:
// // Our heap allocation contains a single size_t followed by an array of
// // chars.
// using L = Layout<size_t, char>::WithStaticSizes<1>;
// std::unique_ptr<unsigned char[]> p_;
// };
//
// int main() {
// CompactString s = "hello";
// assert(s.size() == 5);
// assert(strcmp(s.c_str(), "hello") == 0);
// }
//
// DOCUMENTATION
//
// The interface exported by this file consists of:
// - class `Layout<>` and its public members.
// - The public members of classes `internal_layout::LayoutWithStaticSizes<>`
// and `internal_layout::LayoutImpl<>`. Those classes aren't intended to be
// used directly, and their name and template parameter list are internal
// implementation details, but the classes themselves provide most of the
// functionality in this file. See comments on their members for detailed
// documentation.
//
// `Layout<T1,... Tn>::Partial(count1,..., countm)` (where `m` <= `n`) returns a
// `LayoutImpl<>` object. `Layout<T1,..., Tn> layout(count1,..., countn)`
// creates a `Layout` object, which exposes the same functionality by inheriting
// from `LayoutImpl<>`.
#ifndef ABSL_CONTAINER_INTERNAL_LAYOUT_H_
#define ABSL_CONTAINER_INTERNAL_LAYOUT_H_
#include <assert.h>
#include <stddef.h>
#include <stdint.h>
#include <array>
#include <string>
#include <tuple>
#include <type_traits>
#include <typeinfo>
#include <utility>
#include "absl/base/attributes.h"
#include "absl/base/config.h"
#include "absl/debugging/internal/demangle.h"
#include "absl/meta/type_traits.h"
#include "absl/strings/str_cat.h"
#include "absl/types/span.h"
#include "absl/utility/utility.h"
#ifdef ABSL_HAVE_ADDRESS_SANITIZER
#include <sanitizer/asan_interface.h>
#endif
namespace absl {
ABSL_NAMESPACE_BEGIN
namespace container_internal {
// A type wrapper that instructs `Layout` to use the specific alignment for the
// array. `Layout<..., Aligned<T, N>, ...>` has exactly the same API
// and behavior as `Layout<..., T, ...>` except that the first element of the
// array of `T` is aligned to `N` (the rest of the elements follow without
// padding).
//
// Requires: `N >= alignof(T)` and `N` is a power of 2.
template <class T, size_t N>
struct Aligned;
namespace internal_layout {
template <class T>
struct NotAligned {};
template <class T, size_t N>
struct NotAligned<const Aligned<T, N>> {
static_assert(sizeof(T) == 0, "Aligned<T, N> cannot be const-qualified");
};
template <size_t>
using IntToSize = size_t;
template <class T>
struct Type : NotAligned<T> {
using type = T;
};
template <class T, size_t N>
struct Type<Aligned<T, N>> {
using type = T;
};
template <class T>
struct SizeOf : NotAligned<T>, std::integral_constant<size_t, sizeof(T)> {};
template <class T, size_t N>
struct SizeOf<Aligned<T, N>> : std::integral_constant<size_t, sizeof(T)> {};
// Note: workaround for https://gcc.gnu.org/PR88115
template <class T>
struct AlignOf : NotAligned<T> {
static constexpr size_t value = alignof(T);
};
template <class T, size_t N>
struct AlignOf<Aligned<T, N>> {
static_assert(N % alignof(T) == 0,
"Custom alignment can't be lower than the type's alignment");
static constexpr size_t value = N;
};
// Does `Ts...` contain `T`?
template <class T, class... Ts>
using Contains = absl::disjunction<std::is_same<T, Ts>...>;
template <class From, class To>
using CopyConst =
typename std::conditional<std::is_const<From>::value, const To, To>::type;
// Note: We're not qualifying this with absl:: because it doesn't compile under
// MSVC.
template <class T>
using SliceType = Span<T>;
// This namespace contains no types. It prevents functions defined in it from
// being found by ADL.
namespace adl_barrier {
template <class Needle, class... Ts>
constexpr size_t Find(Needle, Needle, Ts...) {
static_assert(!Contains<Needle, Ts...>(), "Duplicate element type");
return 0;
}
template <class Needle, class T, class... Ts>
constexpr size_t Find(Needle, T, Ts...) {
return adl_barrier::Find(Needle(), Ts()...) + 1;
}
constexpr bool IsPow2(size_t n) { return !(n & (n - 1)); }
// Returns `q * m` for the smallest `q` such that `q * m >= n`.
// Requires: `m` is a power of two. It's enforced by IsLegalElementType below.
constexpr size_t Align(size_t n, size_t m) { return (n + m - 1) & ~(m - 1); }
constexpr size_t Min(size_t a, size_t b) { return b < a ? b : a; }
constexpr size_t Max(size_t a) { return a; }
template <class... Ts>
constexpr size_t Max(size_t a, size_t b, Ts... rest) {
return adl_barrier::Max(b < a ? a : b, rest...);
}
template <class T>
std::string TypeName() {
std::string out;
#ifdef ABSL_INTERNAL_HAS_RTTI
absl::StrAppend(&out, "<",
absl::debugging_internal::DemangleString(typeid(T).name()),
">");
#endif
return out;
}
} // namespace adl_barrier
template <bool C>
using EnableIf = typename std::enable_if<C, int>::type;
// Can `T` be a template argument of `Layout`?
template <class T>
using IsLegalElementType = std::integral_constant<
bool, !std::is_reference<T>::value && !std::is_volatile<T>::value &&
!std::is_reference<typename Type<T>::type>::value &&
!std::is_volatile<typename Type<T>::type>::value &&
adl_barrier::IsPow2(AlignOf<T>::value)>;
template <class Elements, class StaticSizeSeq, class RuntimeSizeSeq,
class SizeSeq, class OffsetSeq>
class LayoutImpl;
// Public base class of `Layout` and the result type of `Layout::Partial()`.
//
// `Elements...` contains all template arguments of `Layout` that created this
// instance.
//
// `StaticSizeSeq...` is an index_sequence containing the sizes specified at
// compile-time.
//
// `RuntimeSizeSeq...` is `[0, NumRuntimeSizes)`, where `NumRuntimeSizes` is the
// number of arguments passed to `Layout::Partial()` or `Layout::Layout()`.
//
// `SizeSeq...` is `[0, NumSizes)` where `NumSizes` is `NumRuntimeSizes` plus
// the number of sizes in `StaticSizeSeq`.
//
// `OffsetSeq...` is `[0, NumOffsets)` where `NumOffsets` is
// `Min(sizeof...(Elements), NumSizes + 1)` (the number of arrays for which we
// can compute offsets).
template <class... Elements, size_t... StaticSizeSeq, size_t... RuntimeSizeSeq,
size_t... SizeSeq, size_t... OffsetSeq>
class LayoutImpl<
std::tuple<Elements...>, absl::index_sequence<StaticSizeSeq...>,
absl::index_sequence<RuntimeSizeSeq...>, absl::index_sequence<SizeSeq...>,
absl::index_sequence<OffsetSeq...>> {
private:
static_assert(sizeof...(Elements) > 0, "At least one field is required");
static_assert(absl::conjunction<IsLegalElementType<Elements>...>::value,
"Invalid element type (see IsLegalElementType)");
static_assert(sizeof...(StaticSizeSeq) <= sizeof...(Elements),
"Too many static sizes specified");
enum {
NumTypes = sizeof...(Elements),
NumStaticSizes = sizeof...(StaticSizeSeq),
NumRuntimeSizes = sizeof...(RuntimeSizeSeq),
NumSizes = sizeof...(SizeSeq),
NumOffsets = sizeof...(OffsetSeq),
};
// These are guaranteed by `Layout`.
static_assert(NumStaticSizes + NumRuntimeSizes == NumSizes, "Internal error");
static_assert(NumSizes <= NumTypes, "Internal error");
static_assert(NumOffsets == adl_barrier::Min(NumTypes, NumSizes + 1),
"Internal error");
static_assert(NumTypes > 0, "Internal error");
static constexpr std::array<size_t, sizeof...(StaticSizeSeq)> kStaticSizes = {
StaticSizeSeq...};
// Returns the index of `T` in `Elements...`. Results in a compilation error
// if `Elements...` doesn't contain exactly one instance of `T`.
template <class T>
static constexpr size_t ElementIndex() {
static_assert(Contains<Type<T>, Type<typename Type<Elements>::type>...>(),
"Type not found");
return adl_barrier::Find(Type<T>(),
Type<typename Type<Elements>::type>()...);
}
template <size_t N>
using ElementAlignment =
AlignOf<typename std::tuple_element<N, std::tuple<Elements...>>::type>;
public:
// Element types of all arrays packed in a tuple.
using ElementTypes = std::tuple<typename Type<Elements>::type...>;
// Element type of the Nth array.
template <size_t N>
using ElementType = typename std::tuple_element<N, ElementTypes>::type;
constexpr explicit LayoutImpl(IntToSize<RuntimeSizeSeq>... sizes)
: size_{sizes...} {}
// Alignment of the layout, equal to the strictest alignment of all elements.
// All pointers passed to the methods of layout must be aligned to this value.
static constexpr size_t Alignment() {
return adl_barrier::Max(AlignOf<Elements>::value...);
}
// Offset in bytes of the Nth array.
//
// // int[3], 4 bytes of padding, double[4].
// Layout<int, double> x(3, 4);
// assert(x.Offset<0>() == 0); // The ints starts from 0.
// assert(x.Offset<1>() == 16); // The doubles starts from 16.
//
// Requires: `N <= NumSizes && N < sizeof...(Ts)`.
template <size_t N, EnableIf<N == 0> = 0>
constexpr size_t Offset() const {
return 0;
}
template <size_t N, EnableIf<N != 0> = 0>
constexpr size_t Offset() const {
static_assert(N < NumOffsets, "Index out of bounds");
return adl_barrier::Align(
Offset<N - 1>() + SizeOf<ElementType<N - 1>>::value * Size<N - 1>(),
ElementAlignment<N>::value);
}
// Offset in bytes of the array with the specified element type. There must
// be exactly one such array and its zero-based index must be at most
// `NumSizes`.
//
// // int[3], 4 bytes of padding, double[4].
// Layout<int, double> x(3, 4);
// assert(x.Offset<int>() == 0); // The ints starts from 0.
// assert(x.Offset<double>() == 16); // The doubles starts from 16.
template <class T>
constexpr size_t Offset() const {
return Offset<ElementIndex<T>()>();
}
// Offsets in bytes of all arrays for which the offsets are known.
constexpr std::array<size_t, NumOffsets> Offsets() const {
return {{Offset<OffsetSeq>()...}};
}
// The number of elements in the Nth array (zero-based).
//
// // int[3], 4 bytes of padding, double[4].
// Layout<int, double> x(3, 4);
// assert(x.Size<0>() == 3);
// assert(x.Size<1>() == 4);
//
// Requires: `N < NumSizes`.
template <size_t N, EnableIf<(N < NumStaticSizes)> = 0>
constexpr size_t Size() const {
return kStaticSizes[N];
}
template <size_t N, EnableIf<(N >= NumStaticSizes)> = 0>
constexpr size_t Size() const {
static_assert(N < NumSizes, "Index out of bounds");
return size_[N - NumStaticSizes];
}
// The number of elements in the array with the specified element type.
// There must be exactly one such array and its zero-based index must be
// at most `NumSizes`.
//
// // int[3], 4 bytes of padding, double[4].
// Layout<int, double> x(3, 4);
// assert(x.Size<int>() == 3);
// assert(x.Size<double>() == 4);
template <class T>
constexpr size_t Size() const {
return Size<ElementIndex<T>()>();
}
// The number of elements of all arrays for which they are known.
constexpr std::array<size_t, NumSizes> Sizes() const {
return {{Size<SizeSeq>()...}};
}
// Pointer to the beginning of the Nth array.
//
// `Char` must be `[const] [signed|unsigned] char`.
//
// // int[3], 4 bytes of padding, double[4].
// Layout<int, double> x(3, 4);
// unsigned char* p = new unsigned char[x.AllocSize()];
// int* ints = x.Pointer<0>(p);
// double* doubles = x.Pointer<1>(p);
//
// Requires: `N <= NumSizes && N < sizeof...(Ts)`.
// Requires: `p` is aligned to `Alignment()`.
template <size_t N, class Char>
CopyConst<Char, ElementType<N>>* Pointer(Char* p) const {
using C = typename std::remove_const<Char>::type;
static_assert(
std::is_same<C, char>() || std::is_same<C, unsigned char>() ||
std::is_same<C, signed char>(),
"The argument must be a pointer to [const] [signed|unsigned] char");
constexpr size_t alignment = Alignment();
(void)alignment;
assert(reinterpret_cast<uintptr_t>(p) % alignment == 0);
return reinterpret_cast<CopyConst<Char, ElementType<N>>*>(p + Offset<N>());
}
// Pointer to the beginning of the array with the specified element type.
// There must be exactly one such array and its zero-based index must be at
// most `NumSizes`.
//
// `Char` must be `[const] [signed|unsigned] char`.
//
// // int[3], 4 bytes of padding, double[4].
// Layout<int, double> x(3, 4);
// unsigned char* p = new unsigned char[x.AllocSize()];
// int* ints = x.Pointer<int>(p);
// double* doubles = x.Pointer<double>(p);
//
// Requires: `p` is aligned to `Alignment()`.
template <class T, class Char>
CopyConst<Char, T>* Pointer(Char* p) const {
return Pointer<ElementIndex<T>()>(p);
}
// Pointers to all arrays for which pointers are known.
//
// `Char` must be `[const] [signed|unsigned] char`.
//
// // int[3], 4 bytes of padding, double[4].
// Layout<int, double> x(3, 4);
// unsigned char* p = new unsigned char[x.AllocSize()];
//
// int* ints;
// double* doubles;
// std::tie(ints, doubles) = x.Pointers(p);
//
// Requires: `p` is aligned to `Alignment()`.
template <class Char>
auto Pointers(Char* p) const {
return std::tuple<CopyConst<Char, ElementType<OffsetSeq>>*...>(
Pointer<OffsetSeq>(p)...);
}
// The Nth array.
//
// `Char` must be `[const] [signed|unsigned] char`.
//
// // int[3], 4 bytes of padding, double[4].
// Layout<int, double> x(3, 4);
// unsigned char* p = new unsigned char[x.AllocSize()];
// Span<int> ints = x.Slice<0>(p);
// Span<double> doubles = x.Slice<1>(p);
//
// Requires: `N < NumSizes`.
// Requires: `p` is aligned to `Alignment()`.
template <size_t N, class Char>
SliceType<CopyConst<Char, ElementType<N>>> Slice(Char* p) const {
return SliceType<CopyConst<Char, ElementType<N>>>(Pointer<N>(p), Size<N>());
}
// The array with the specified element type. There must be exactly one
// such array and its zero-based index must be less than `NumSizes`.
//
// `Char` must be `[const] [signed|unsigned] char`.
//
// // int[3], 4 bytes of padding, double[4].
// Layout<int, double> x(3, 4);
// unsigned char* p = new unsigned char[x.AllocSize()];
// Span<int> ints = x.Slice<int>(p);
// Span<double> doubles = x.Slice<double>(p);
//
// Requires: `p` is aligned to `Alignment()`.
template <class T, class Char>
SliceType<CopyConst<Char, T>> Slice(Char* p) const {
return Slice<ElementIndex<T>()>(p);
}
// All arrays with known sizes.
//
// `Char` must be `[const] [signed|unsigned] char`.
//
// // int[3], 4 bytes of padding, double[4].
// Layout<int, double> x(3, 4);
// unsigned char* p = new unsigned char[x.AllocSize()];
//
// Span<int> ints;
// Span<double> doubles;
// std::tie(ints, doubles) = x.Slices(p);
//
// Requires: `p` is aligned to `Alignment()`.
//
// Note: We mark the parameter as unused because GCC detects it is not used
// when `SizeSeq` is empty [-Werror=unused-but-set-parameter].
template <class Char>
auto Slices(ABSL_ATTRIBUTE_UNUSED Char* p) const {
return std::tuple<SliceType<CopyConst<Char, ElementType<SizeSeq>>>...>(
Slice<SizeSeq>(p)...);
}
// The size of the allocation that fits all arrays.
//
// // int[3], 4 bytes of padding, double[4].
// Layout<int, double> x(3, 4);
// unsigned char* p = new unsigned char[x.AllocSize()]; // 48 bytes
//
// Requires: `NumSizes == sizeof...(Ts)`.
constexpr size_t AllocSize() const {
static_assert(NumTypes == NumSizes, "You must specify sizes of all fields");
return Offset<NumTypes - 1>() +
SizeOf<ElementType<NumTypes - 1>>::value * Size<NumTypes - 1>();
}
// If built with --config=asan, poisons padding bytes (if any) in the
// allocation. The pointer must point to a memory block at least
// `AllocSize()` bytes in length.
//
// `Char` must be `[const] [signed|unsigned] char`.
//
// Requires: `p` is aligned to `Alignment()`.
template <class Char, size_t N = NumOffsets - 1, EnableIf<N == 0> = 0>
void PoisonPadding(const Char* p) const {
Pointer<0>(p); // verify the requirements on `Char` and `p`
}
template <class Char, size_t N = NumOffsets - 1, EnableIf<N != 0> = 0>
void PoisonPadding(const Char* p) const {
static_assert(N < NumOffsets, "Index out of bounds");
(void)p;
#ifdef ABSL_HAVE_ADDRESS_SANITIZER
PoisonPadding<Char, N - 1>(p);
// The `if` is an optimization. It doesn't affect the observable behaviour.
if (ElementAlignment<N - 1>::value % ElementAlignment<N>::value) {
size_t start =
Offset<N - 1>() + SizeOf<ElementType<N - 1>>::value * Size<N - 1>();
ASAN_POISON_MEMORY_REGION(p + start, Offset<N>() - start);
}
#endif
}
// Human-readable description of the memory layout. Useful for debugging.
// Slow.
//
// // char[5], 3 bytes of padding, int[3], 4 bytes of padding, followed
// // by an unknown number of doubles.
// auto x = Layout<char, int, double>::Partial(5, 3);
// assert(x.DebugString() ==
// "@0<char>(1)[5]; @8<int>(4)[3]; @24<double>(8)");
//
// Each field is in the following format: @offset<type>(sizeof)[size] (<type>
// may be missing depending on the target platform). For example,
// @8<int>(4)[3] means that at offset 8 we have an array of ints, where each
// int is 4 bytes, and we have 3 of those ints. The size of the last field may
// be missing (as in the example above). Only fields with known offsets are
// described. Type names may differ across platforms: one compiler might
// produce "unsigned*" where another produces "unsigned int *".
std::string DebugString() const {
const auto offsets = Offsets();
const size_t sizes[] = {SizeOf<ElementType<OffsetSeq>>::value...};
const std::string types[] = {
adl_barrier::TypeName<ElementType<OffsetSeq>>()...};
std::string res = absl::StrCat("@0", types[0], "(", sizes[0], ")");
for (size_t i = 0; i != NumOffsets - 1; ++i) {
absl::StrAppend(&res, "[", DebugSize(i), "]; @", offsets[i + 1],
types[i + 1], "(", sizes[i + 1], ")");
}
// NumSizes is a constant that may be zero. Some compilers cannot see that
// inside the if statement "size_[NumSizes - 1]" must be valid.
int last = static_cast<int>(NumSizes) - 1;
if (NumTypes == NumSizes && last >= 0) {
absl::StrAppend(&res, "[", DebugSize(static_cast<size_t>(last)), "]");
}
return res;
}
private:
size_t DebugSize(size_t n) const {
if (n < NumStaticSizes) {
return kStaticSizes[n];
} else {
return size_[n - NumStaticSizes];
}
}
// Arguments of `Layout::Partial()` or `Layout::Layout()`.
size_t size_[NumRuntimeSizes > 0 ? NumRuntimeSizes : 1];
};
// Defining a constexpr static class member variable is redundant and deprecated
// in C++17, but required in C++14.
template <class... Elements, size_t... StaticSizeSeq, size_t... RuntimeSizeSeq,
size_t... SizeSeq, size_t... OffsetSeq>
constexpr std::array<size_t, sizeof...(StaticSizeSeq)> LayoutImpl<
std::tuple<Elements...>, absl::index_sequence<StaticSizeSeq...>,
absl::index_sequence<RuntimeSizeSeq...>, absl::index_sequence<SizeSeq...>,
absl::index_sequence<OffsetSeq...>>::kStaticSizes;
template <class StaticSizeSeq, size_t NumRuntimeSizes, class... Ts>
using LayoutType = LayoutImpl<
std::tuple<Ts...>, StaticSizeSeq,
absl::make_index_sequence<NumRuntimeSizes>,
absl::make_index_sequence<NumRuntimeSizes + StaticSizeSeq::size()>,
absl::make_index_sequence<adl_barrier::Min(
sizeof...(Ts), NumRuntimeSizes + StaticSizeSeq::size() + 1)>>;
template <class StaticSizeSeq, class... Ts>
class LayoutWithStaticSizes
: public LayoutType<StaticSizeSeq,
sizeof...(Ts) - adl_barrier::Min(sizeof...(Ts),
StaticSizeSeq::size()),
Ts...> {
private:
using Super =
LayoutType<StaticSizeSeq,
sizeof...(Ts) -
adl_barrier::Min(sizeof...(Ts), StaticSizeSeq::size()),
Ts...>;
public:
// The result type of `Partial()` with `NumSizes` arguments.
template <size_t NumSizes>
using PartialType =
internal_layout::LayoutType<StaticSizeSeq, NumSizes, Ts...>;
// `Layout` knows the element types of the arrays we want to lay out in
// memory but not the number of elements in each array.
// `Partial(size1, ..., sizeN)` allows us to specify the latter. The
// resulting immutable object can be used to obtain pointers to the
// individual arrays.
//
// It's allowed to pass fewer array sizes than the number of arrays. E.g.,
// if all you need is to the offset of the second array, you only need to
// pass one argument -- the number of elements in the first array.
//
// // int[3] followed by 4 bytes of padding and an unknown number of
// // doubles.
// auto x = Layout<int, double>::Partial(3);
// // doubles start at byte 16.
// assert(x.Offset<1>() == 16);
//
// If you know the number of elements in all arrays, you can still call
// `Partial()` but it's more convenient to use the constructor of `Layout`.
//
// Layout<int, double> x(3, 5);
//
// Note: The sizes of the arrays must be specified in number of elements,
// not in bytes.
//
// Requires: `sizeof...(Sizes) + NumStaticSizes <= sizeof...(Ts)`.
// Requires: all arguments are convertible to `size_t`.
template <class... Sizes>
static constexpr PartialType<sizeof...(Sizes)> Partial(Sizes&&... sizes) {
static_assert(sizeof...(Sizes) + StaticSizeSeq::size() <= sizeof...(Ts),
"");
return PartialType<sizeof...(Sizes)>(
static_cast<size_t>(std::forward<Sizes>(sizes))...);
}
// Inherit LayoutType's constructor.
//
// Creates a layout with the sizes of all arrays specified. If you know
// only the sizes of the first N arrays (where N can be zero), you can use
// `Partial()` defined above. The constructor is essentially equivalent to
// calling `Partial()` and passing in all array sizes; the constructor is
// provided as a convenient abbreviation.
//
// Note: The sizes of the arrays must be specified in number of elements,
// not in bytes.
//
// Implementation note: we do this via a `using` declaration instead of
// defining our own explicit constructor because the signature of LayoutType's
// constructor depends on RuntimeSizeSeq, which we don't have access to here.
// If we defined our own constructor here, it would have to use a parameter
// pack and then cast the arguments to size_t when calling the superclass
// constructor, similar to what Partial() does. But that would suffer from the
// same problem that Partial() has, which is that the parameter types are
// inferred from the arguments, which may be signed types, which must then be
// cast to size_t. This can lead to negative values being silently (i.e. with
// no compiler warnings) cast to an unsigned type. Having a constructor with
// size_t parameters helps the compiler generate better warnings about
// potential bad casts, while avoiding false warnings when positive literal
// arguments are used. If an argument is a positive literal integer (e.g.
// `1`), the compiler will understand that it can be safely converted to
// size_t, and hence not generate a warning. But if a negative literal (e.g.
// `-1`) or a variable with signed type is used, then it can generate a
// warning about a potentially unsafe implicit cast. It would be great if we
// could do this for Partial() too, but unfortunately as of C++23 there seems
// to be no way to define a function with a variable number of parameters of a
// certain type, a.k.a. homogeneous function parameter packs. So we're forced
// to choose between explicitly casting the arguments to size_t, which
// suppresses all warnings, even potentially valid ones, or implicitly casting
// them to size_t, which generates bogus warnings whenever literal arguments
// are used, even if they're positive.
using Super::Super;
};
} // namespace internal_layout
// Descriptor of arrays of various types and sizes laid out in memory one after
// another. See the top of the file for documentation.
//
// Check out the public API of internal_layout::LayoutWithStaticSizes and
// internal_layout::LayoutImpl above. Those types are internal to the library
// but their methods are public, and they are inherited by `Layout`.
template <class... Ts>
class Layout : public internal_layout::LayoutWithStaticSizes<
absl::make_index_sequence<0>, Ts...> {
private:
using Super =
internal_layout::LayoutWithStaticSizes<absl::make_index_sequence<0>,
Ts...>;
public:
// If you know the sizes of some or all of the arrays at compile time, you can
// use `WithStaticSizes` or `WithStaticSizeSequence` to create a `Layout` type
// with those sizes baked in. This can help the compiler generate optimal code
// for calculating array offsets and AllocSize().
//
// Like `Partial()`, the N sizes you specify are for the first N arrays, and
// they specify the number of elements in each array, not the number of bytes.
template <class StaticSizeSeq>
using WithStaticSizeSequence =
internal_layout::LayoutWithStaticSizes<StaticSizeSeq, Ts...>;
template <size_t... StaticSizes>
using WithStaticSizes =
WithStaticSizeSequence<std::index_sequence<StaticSizes...>>;
// Inherit LayoutWithStaticSizes's constructor, which requires you to specify
// all the array sizes.
using Super::Super;
};
} // namespace container_internal
ABSL_NAMESPACE_END
} // namespace absl
#endif // ABSL_CONTAINER_INTERNAL_LAYOUT_H_

View file

@ -0,0 +1,295 @@
// Copyright 2018 The Abseil Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// Every benchmark should have the same performance as the corresponding
// headroom benchmark.
#include <cstddef>
#include <cstdint>
#include "absl/base/internal/raw_logging.h"
#include "absl/container/internal/layout.h"
#include "benchmark/benchmark.h"
namespace absl {
ABSL_NAMESPACE_BEGIN
namespace container_internal {
namespace {
using ::benchmark::DoNotOptimize;
using Int128 = int64_t[2];
constexpr size_t MyAlign(size_t n, size_t m) { return (n + m - 1) & ~(m - 1); }
// This benchmark provides the upper bound on performance for BM_OffsetConstant.
template <size_t Offset, class... Ts>
void BM_OffsetConstantHeadroom(benchmark::State& state) {
for (auto _ : state) {
DoNotOptimize(Offset);
}
}
template <size_t Offset, class... Ts>
void BM_OffsetConstantStatic(benchmark::State& state) {
using L = typename Layout<Ts...>::template WithStaticSizes<3, 5, 7>;
ABSL_RAW_CHECK(L::Partial().template Offset<3>() == Offset, "Invalid offset");
for (auto _ : state) {
DoNotOptimize(L::Partial().template Offset<3>());
}
}
template <size_t Offset, class... Ts>
void BM_OffsetConstant(benchmark::State& state) {
using L = Layout<Ts...>;
ABSL_RAW_CHECK(L::Partial(3, 5, 7).template Offset<3>() == Offset,
"Invalid offset");
for (auto _ : state) {
DoNotOptimize(L::Partial(3, 5, 7).template Offset<3>());
}
}
template <size_t Offset, class... Ts>
void BM_OffsetConstantIndirect(benchmark::State& state) {
using L = Layout<Ts...>;
auto p = L::Partial(3, 5, 7);
ABSL_RAW_CHECK(p.template Offset<3>() == Offset, "Invalid offset");
for (auto _ : state) {
DoNotOptimize(p);
DoNotOptimize(p.template Offset<3>());
}
}
template <class... Ts>
size_t PartialOffset(size_t k);
template <>
size_t PartialOffset<int8_t, int16_t, int32_t, Int128>(size_t k) {
constexpr size_t o = MyAlign(MyAlign(3 * 1, 2) + 5 * 2, 4);
return MyAlign(o + k * 4, 8);
}
template <>
size_t PartialOffset<Int128, int32_t, int16_t, int8_t>(size_t k) {
// No alignment is necessary.
return 3 * 16 + 5 * 4 + k * 2;
}
// This benchmark provides the upper bound on performance for BM_OffsetVariable.
template <size_t Offset, class... Ts>
void BM_OffsetPartialHeadroom(benchmark::State& state) {
size_t k = 7;
ABSL_RAW_CHECK(PartialOffset<Ts...>(k) == Offset, "Invalid offset");
for (auto _ : state) {
DoNotOptimize(k);
DoNotOptimize(PartialOffset<Ts...>(k));
}
}
template <size_t Offset, class... Ts>
void BM_OffsetPartialStatic(benchmark::State& state) {
using L = typename Layout<Ts...>::template WithStaticSizes<3, 5>;
size_t k = 7;
ABSL_RAW_CHECK(L::Partial(k).template Offset<3>() == Offset,
"Invalid offset");
for (auto _ : state) {
DoNotOptimize(k);
DoNotOptimize(L::Partial(k).template Offset<3>());
}
}
template <size_t Offset, class... Ts>
void BM_OffsetPartial(benchmark::State& state) {
using L = Layout<Ts...>;
size_t k = 7;
ABSL_RAW_CHECK(L::Partial(3, 5, k).template Offset<3>() == Offset,
"Invalid offset");
for (auto _ : state) {
DoNotOptimize(k);
DoNotOptimize(L::Partial(3, 5, k).template Offset<3>());
}
}
template <class... Ts>
size_t VariableOffset(size_t n, size_t m, size_t k);
template <>
size_t VariableOffset<int8_t, int16_t, int32_t, Int128>(size_t n, size_t m,
size_t k) {
return MyAlign(MyAlign(MyAlign(n * 1, 2) + m * 2, 4) + k * 4, 8);
}
template <>
size_t VariableOffset<Int128, int32_t, int16_t, int8_t>(size_t n, size_t m,
size_t k) {
// No alignment is necessary.
return n * 16 + m * 4 + k * 2;
}
// This benchmark provides the upper bound on performance for BM_OffsetVariable.
template <size_t Offset, class... Ts>
void BM_OffsetVariableHeadroom(benchmark::State& state) {
size_t n = 3;
size_t m = 5;
size_t k = 7;
ABSL_RAW_CHECK(VariableOffset<Ts...>(n, m, k) == Offset, "Invalid offset");
for (auto _ : state) {
DoNotOptimize(n);
DoNotOptimize(m);
DoNotOptimize(k);
DoNotOptimize(VariableOffset<Ts...>(n, m, k));
}
}
template <size_t Offset, class... Ts>
void BM_OffsetVariable(benchmark::State& state) {
using L = Layout<Ts...>;
size_t n = 3;
size_t m = 5;
size_t k = 7;
ABSL_RAW_CHECK(L::Partial(n, m, k).template Offset<3>() == Offset,
"Invalid offset");
for (auto _ : state) {
DoNotOptimize(n);
DoNotOptimize(m);
DoNotOptimize(k);
DoNotOptimize(L::Partial(n, m, k).template Offset<3>());
}
}
template <class... Ts>
size_t AllocSize(size_t x);
template <>
size_t AllocSize<int8_t, int16_t, int32_t, Int128>(size_t x) {
constexpr size_t o =
Layout<int8_t, int16_t, int32_t, Int128>::Partial(3, 5, 7)
.template Offset<Int128>();
return o + sizeof(Int128) * x;
}
template <>
size_t AllocSize<Int128, int32_t, int16_t, int8_t>(size_t x) {
constexpr size_t o =
Layout<Int128, int32_t, int16_t, int8_t>::Partial(3, 5, 7)
.template Offset<int8_t>();
return o + sizeof(int8_t) * x;
}
// This benchmark provides the upper bound on performance for BM_AllocSize
template <size_t Size, class... Ts>
void BM_AllocSizeHeadroom(benchmark::State& state) {
size_t x = 9;
ABSL_RAW_CHECK(AllocSize<Ts...>(x) == Size, "Invalid size");
for (auto _ : state) {
DoNotOptimize(x);
DoNotOptimize(AllocSize<Ts...>(x));
}
}
template <size_t Size, class... Ts>
void BM_AllocSizeStatic(benchmark::State& state) {
using L = typename Layout<Ts...>::template WithStaticSizes<3, 5, 7>;
size_t x = 9;
ABSL_RAW_CHECK(L(x).AllocSize() == Size, "Invalid offset");
for (auto _ : state) {
DoNotOptimize(x);
DoNotOptimize(L(x).AllocSize());
}
}
template <size_t Size, class... Ts>
void BM_AllocSize(benchmark::State& state) {
using L = Layout<Ts...>;
size_t n = 3;
size_t m = 5;
size_t k = 7;
size_t x = 9;
ABSL_RAW_CHECK(L(n, m, k, x).AllocSize() == Size, "Invalid offset");
for (auto _ : state) {
DoNotOptimize(n);
DoNotOptimize(m);
DoNotOptimize(k);
DoNotOptimize(x);
DoNotOptimize(L(n, m, k, x).AllocSize());
}
}
template <size_t Size, class... Ts>
void BM_AllocSizeIndirect(benchmark::State& state) {
using L = Layout<Ts...>;
auto l = L(3, 5, 7, 9);
ABSL_RAW_CHECK(l.AllocSize() == Size, "Invalid offset");
for (auto _ : state) {
DoNotOptimize(l);
DoNotOptimize(l.AllocSize());
}
}
// Run all benchmarks in two modes:
//
// Layout with padding: int8_t[3], int16_t[5], int32_t[7], Int128[?].
// Layout without padding: Int128[3], int32_t[5], int16_t[7], int8_t[?].
#define OFFSET_BENCHMARK(NAME, OFFSET, T1, T2, T3, T4) \
auto& NAME##_##OFFSET##_##T1##_##T2##_##T3##_##T4 = \
NAME<OFFSET, T1, T2, T3, T4>; \
BENCHMARK(NAME##_##OFFSET##_##T1##_##T2##_##T3##_##T4)
OFFSET_BENCHMARK(BM_OffsetConstantHeadroom, 48, int8_t, int16_t, int32_t,
Int128);
OFFSET_BENCHMARK(BM_OffsetConstantStatic, 48, int8_t, int16_t, int32_t, Int128);
OFFSET_BENCHMARK(BM_OffsetConstant, 48, int8_t, int16_t, int32_t, Int128);
OFFSET_BENCHMARK(BM_OffsetConstantIndirect, 48, int8_t, int16_t, int32_t,
Int128);
OFFSET_BENCHMARK(BM_OffsetConstantHeadroom, 82, Int128, int32_t, int16_t,
int8_t);
OFFSET_BENCHMARK(BM_OffsetConstantStatic, 82, Int128, int32_t, int16_t, int8_t);
OFFSET_BENCHMARK(BM_OffsetConstant, 82, Int128, int32_t, int16_t, int8_t);
OFFSET_BENCHMARK(BM_OffsetConstantIndirect, 82, Int128, int32_t, int16_t,
int8_t);
OFFSET_BENCHMARK(BM_OffsetPartialHeadroom, 48, int8_t, int16_t, int32_t,
Int128);
OFFSET_BENCHMARK(BM_OffsetPartialStatic, 48, int8_t, int16_t, int32_t, Int128);
OFFSET_BENCHMARK(BM_OffsetPartial, 48, int8_t, int16_t, int32_t, Int128);
OFFSET_BENCHMARK(BM_OffsetPartialHeadroom, 82, Int128, int32_t, int16_t,
int8_t);
OFFSET_BENCHMARK(BM_OffsetPartialStatic, 82, Int128, int32_t, int16_t, int8_t);
OFFSET_BENCHMARK(BM_OffsetPartial, 82, Int128, int32_t, int16_t, int8_t);
OFFSET_BENCHMARK(BM_OffsetVariableHeadroom, 48, int8_t, int16_t, int32_t,
Int128);
OFFSET_BENCHMARK(BM_OffsetVariable, 48, int8_t, int16_t, int32_t, Int128);
OFFSET_BENCHMARK(BM_OffsetVariableHeadroom, 82, Int128, int32_t, int16_t,
int8_t);
OFFSET_BENCHMARK(BM_OffsetVariable, 82, Int128, int32_t, int16_t, int8_t);
OFFSET_BENCHMARK(BM_AllocSizeHeadroom, 192, int8_t, int16_t, int32_t, Int128);
OFFSET_BENCHMARK(BM_AllocSizeStatic, 192, int8_t, int16_t, int32_t, Int128);
OFFSET_BENCHMARK(BM_AllocSize, 192, int8_t, int16_t, int32_t, Int128);
OFFSET_BENCHMARK(BM_AllocSizeIndirect, 192, int8_t, int16_t, int32_t, Int128);
OFFSET_BENCHMARK(BM_AllocSizeHeadroom, 91, Int128, int32_t, int16_t, int8_t);
OFFSET_BENCHMARK(BM_AllocSizeStatic, 91, Int128, int32_t, int16_t, int8_t);
OFFSET_BENCHMARK(BM_AllocSize, 91, Int128, int32_t, int16_t, int8_t);
OFFSET_BENCHMARK(BM_AllocSizeIndirect, 91, Int128, int32_t, int16_t, int8_t);
} // namespace
} // namespace container_internal
ABSL_NAMESPACE_END
} // namespace absl

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,95 @@
// Copyright 2018 The Abseil Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// Adapts a policy for nodes.
//
// The node policy should model:
//
// struct Policy {
// // Returns a new node allocated and constructed using the allocator, using
// // the specified arguments.
// template <class Alloc, class... Args>
// value_type* new_element(Alloc* alloc, Args&&... args) const;
//
// // Destroys and deallocates node using the allocator.
// template <class Alloc>
// void delete_element(Alloc* alloc, value_type* node) const;
// };
//
// It may also optionally define `value()` and `apply()`. For documentation on
// these, see hash_policy_traits.h.
#ifndef ABSL_CONTAINER_INTERNAL_NODE_SLOT_POLICY_H_
#define ABSL_CONTAINER_INTERNAL_NODE_SLOT_POLICY_H_
#include <cassert>
#include <cstddef>
#include <memory>
#include <type_traits>
#include <utility>
#include "absl/base/config.h"
namespace absl {
ABSL_NAMESPACE_BEGIN
namespace container_internal {
template <class Reference, class Policy>
struct node_slot_policy {
static_assert(std::is_lvalue_reference<Reference>::value, "");
using slot_type = typename std::remove_cv<
typename std::remove_reference<Reference>::type>::type*;
template <class Alloc, class... Args>
static void construct(Alloc* alloc, slot_type* slot, Args&&... args) {
*slot = Policy::new_element(alloc, std::forward<Args>(args)...);
}
template <class Alloc>
static void destroy(Alloc* alloc, slot_type* slot) {
Policy::delete_element(alloc, *slot);
}
// Returns true_type to indicate that transfer can use memcpy.
template <class Alloc>
static std::true_type transfer(Alloc*, slot_type* new_slot,
slot_type* old_slot) {
*new_slot = *old_slot;
return {};
}
static size_t space_used(const slot_type* slot) {
if (slot == nullptr) return Policy::element_space_used(nullptr);
return Policy::element_space_used(*slot);
}
static Reference element(slot_type* slot) { return **slot; }
template <class T, class P = Policy>
static auto value(T* elem) -> decltype(P::value(elem)) {
return P::value(elem);
}
template <class... Ts, class P = Policy>
static auto apply(Ts&&... ts) -> decltype(P::apply(std::forward<Ts>(ts)...)) {
return P::apply(std::forward<Ts>(ts)...);
}
};
} // namespace container_internal
ABSL_NAMESPACE_END
} // namespace absl
#endif // ABSL_CONTAINER_INTERNAL_NODE_SLOT_POLICY_H_

View file

@ -0,0 +1,71 @@
// Copyright 2018 The Abseil Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "absl/container/internal/node_slot_policy.h"
#include <memory>
#include "gmock/gmock.h"
#include "gtest/gtest.h"
#include "absl/base/config.h"
#include "absl/container/internal/hash_policy_traits.h"
namespace absl {
ABSL_NAMESPACE_BEGIN
namespace container_internal {
namespace {
using ::testing::Pointee;
struct Policy : node_slot_policy<int&, Policy> {
using key_type = int;
using init_type = int;
template <class Alloc>
static int* new_element(Alloc* alloc, int value) {
return new int(value);
}
template <class Alloc>
static void delete_element(Alloc* alloc, int* elem) {
delete elem;
}
};
using NodePolicy = hash_policy_traits<Policy>;
struct NodeTest : ::testing::Test {
std::allocator<int> alloc;
int n = 53;
int* a = &n;
};
TEST_F(NodeTest, ConstructDestroy) {
NodePolicy::construct(&alloc, &a, 42);
EXPECT_THAT(a, Pointee(42));
NodePolicy::destroy(&alloc, &a);
}
TEST_F(NodeTest, transfer) {
int s = 42;
int* b = &s;
NodePolicy::transfer(&alloc, &a, &b);
EXPECT_EQ(&s, a);
EXPECT_TRUE(NodePolicy::transfer_uses_memcpy());
}
} // namespace
} // namespace container_internal
ABSL_NAMESPACE_END
} // namespace absl

View file

@ -0,0 +1,226 @@
// Copyright 2018 The Abseil Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef ABSL_CONTAINER_INTERNAL_RAW_HASH_MAP_H_
#define ABSL_CONTAINER_INTERNAL_RAW_HASH_MAP_H_
#include <tuple>
#include <type_traits>
#include <utility>
#include "absl/base/attributes.h"
#include "absl/base/config.h"
#include "absl/base/internal/throw_delegate.h"
#include "absl/container/internal/container_memory.h"
#include "absl/container/internal/raw_hash_set.h" // IWYU pragma: export
namespace absl {
ABSL_NAMESPACE_BEGIN
namespace container_internal {
template <class Policy, class Hash, class Eq, class Alloc>
class raw_hash_map : public raw_hash_set<Policy, Hash, Eq, Alloc> {
// P is Policy. It's passed as a template argument to support maps that have
// incomplete types as values, as in unordered_map<K, IncompleteType>.
// MappedReference<> may be a non-reference type.
template <class P>
using MappedReference = decltype(P::value(
std::addressof(std::declval<typename raw_hash_map::reference>())));
// MappedConstReference<> may be a non-reference type.
template <class P>
using MappedConstReference = decltype(P::value(
std::addressof(std::declval<typename raw_hash_map::const_reference>())));
using KeyArgImpl =
KeyArg<IsTransparent<Eq>::value && IsTransparent<Hash>::value>;
public:
using key_type = typename Policy::key_type;
using mapped_type = typename Policy::mapped_type;
template <class K>
using key_arg = typename KeyArgImpl::template type<K, key_type>;
static_assert(!std::is_reference<key_type>::value, "");
// TODO(b/187807849): Evaluate whether to support reference mapped_type and
// remove this assertion if/when it is supported.
static_assert(!std::is_reference<mapped_type>::value, "");
using iterator = typename raw_hash_map::raw_hash_set::iterator;
using const_iterator = typename raw_hash_map::raw_hash_set::const_iterator;
raw_hash_map() {}
using raw_hash_map::raw_hash_set::raw_hash_set;
// The last two template parameters ensure that both arguments are rvalues
// (lvalue arguments are handled by the overloads below). This is necessary
// for supporting bitfield arguments.
//
// union { int n : 1; };
// flat_hash_map<int, int> m;
// m.insert_or_assign(n, n);
template <class K = key_type, class V = mapped_type, K* = nullptr,
V* = nullptr>
std::pair<iterator, bool> insert_or_assign(key_arg<K>&& k, V&& v)
ABSL_ATTRIBUTE_LIFETIME_BOUND {
return insert_or_assign_impl(std::forward<K>(k), std::forward<V>(v));
}
template <class K = key_type, class V = mapped_type, K* = nullptr>
std::pair<iterator, bool> insert_or_assign(key_arg<K>&& k, const V& v)
ABSL_ATTRIBUTE_LIFETIME_BOUND {
return insert_or_assign_impl(std::forward<K>(k), v);
}
template <class K = key_type, class V = mapped_type, V* = nullptr>
std::pair<iterator, bool> insert_or_assign(const key_arg<K>& k, V&& v)
ABSL_ATTRIBUTE_LIFETIME_BOUND {
return insert_or_assign_impl(k, std::forward<V>(v));
}
template <class K = key_type, class V = mapped_type>
std::pair<iterator, bool> insert_or_assign(const key_arg<K>& k, const V& v)
ABSL_ATTRIBUTE_LIFETIME_BOUND {
return insert_or_assign_impl(k, v);
}
template <class K = key_type, class V = mapped_type, K* = nullptr,
V* = nullptr>
iterator insert_or_assign(const_iterator, key_arg<K>&& k,
V&& v) ABSL_ATTRIBUTE_LIFETIME_BOUND {
return insert_or_assign(std::forward<K>(k), std::forward<V>(v)).first;
}
template <class K = key_type, class V = mapped_type, K* = nullptr>
iterator insert_or_assign(const_iterator, key_arg<K>&& k,
const V& v) ABSL_ATTRIBUTE_LIFETIME_BOUND {
return insert_or_assign(std::forward<K>(k), v).first;
}
template <class K = key_type, class V = mapped_type, V* = nullptr>
iterator insert_or_assign(const_iterator, const key_arg<K>& k,
V&& v) ABSL_ATTRIBUTE_LIFETIME_BOUND {
return insert_or_assign(k, std::forward<V>(v)).first;
}
template <class K = key_type, class V = mapped_type>
iterator insert_or_assign(const_iterator, const key_arg<K>& k,
const V& v) ABSL_ATTRIBUTE_LIFETIME_BOUND {
return insert_or_assign(k, v).first;
}
// All `try_emplace()` overloads make the same guarantees regarding rvalue
// arguments as `std::unordered_map::try_emplace()`, namely that these
// functions will not move from rvalue arguments if insertions do not happen.
template <class K = key_type, class... Args,
typename std::enable_if<
!std::is_convertible<K, const_iterator>::value, int>::type = 0,
K* = nullptr>
std::pair<iterator, bool> try_emplace(key_arg<K>&& k, Args&&... args)
ABSL_ATTRIBUTE_LIFETIME_BOUND {
return try_emplace_impl(std::forward<K>(k), std::forward<Args>(args)...);
}
template <class K = key_type, class... Args,
typename std::enable_if<
!std::is_convertible<K, const_iterator>::value, int>::type = 0>
std::pair<iterator, bool> try_emplace(const key_arg<K>& k, Args&&... args)
ABSL_ATTRIBUTE_LIFETIME_BOUND {
return try_emplace_impl(k, std::forward<Args>(args)...);
}
template <class K = key_type, class... Args, K* = nullptr>
iterator try_emplace(const_iterator, key_arg<K>&& k,
Args&&... args) ABSL_ATTRIBUTE_LIFETIME_BOUND {
return try_emplace(std::forward<K>(k), std::forward<Args>(args)...).first;
}
template <class K = key_type, class... Args>
iterator try_emplace(const_iterator, const key_arg<K>& k,
Args&&... args) ABSL_ATTRIBUTE_LIFETIME_BOUND {
return try_emplace(k, std::forward<Args>(args)...).first;
}
template <class K = key_type, class P = Policy>
MappedReference<P> at(const key_arg<K>& key) ABSL_ATTRIBUTE_LIFETIME_BOUND {
auto it = this->find(key);
if (it == this->end()) {
base_internal::ThrowStdOutOfRange(
"absl::container_internal::raw_hash_map<>::at");
}
return Policy::value(&*it);
}
template <class K = key_type, class P = Policy>
MappedConstReference<P> at(const key_arg<K>& key) const
ABSL_ATTRIBUTE_LIFETIME_BOUND {
auto it = this->find(key);
if (it == this->end()) {
base_internal::ThrowStdOutOfRange(
"absl::container_internal::raw_hash_map<>::at");
}
return Policy::value(&*it);
}
template <class K = key_type, class P = Policy, K* = nullptr>
MappedReference<P> operator[](key_arg<K>&& key)
ABSL_ATTRIBUTE_LIFETIME_BOUND {
// It is safe to use unchecked_deref here because try_emplace
// will always return an iterator pointing to a valid item in the table,
// since it inserts if nothing is found for the given key.
return Policy::value(
&this->unchecked_deref(try_emplace(std::forward<K>(key)).first));
}
template <class K = key_type, class P = Policy>
MappedReference<P> operator[](const key_arg<K>& key)
ABSL_ATTRIBUTE_LIFETIME_BOUND {
// It is safe to use unchecked_deref here because try_emplace
// will always return an iterator pointing to a valid item in the table,
// since it inserts if nothing is found for the given key.
return Policy::value(&this->unchecked_deref(try_emplace(key).first));
}
private:
template <class K, class V>
std::pair<iterator, bool> insert_or_assign_impl(K&& k, V&& v)
ABSL_ATTRIBUTE_LIFETIME_BOUND {
auto res = this->find_or_prepare_insert(k);
if (res.second) {
this->emplace_at(res.first, std::forward<K>(k), std::forward<V>(v));
} else {
Policy::value(&*res.first) = std::forward<V>(v);
}
return res;
}
template <class K = key_type, class... Args>
std::pair<iterator, bool> try_emplace_impl(K&& k, Args&&... args)
ABSL_ATTRIBUTE_LIFETIME_BOUND {
auto res = this->find_or_prepare_insert(k);
if (res.second) {
this->emplace_at(res.first, std::piecewise_construct,
std::forward_as_tuple(std::forward<K>(k)),
std::forward_as_tuple(std::forward<Args>(args)...));
}
return res;
}
};
} // namespace container_internal
ABSL_NAMESPACE_END
} // namespace absl
#endif // ABSL_CONTAINER_INTERNAL_RAW_HASH_MAP_H_

View file

@ -0,0 +1,670 @@
// Copyright 2018 The Abseil Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "absl/container/internal/raw_hash_set.h"
#include <atomic>
#include <cassert>
#include <cstddef>
#include <cstdint>
#include <cstring>
#include "absl/base/attributes.h"
#include "absl/base/config.h"
#include "absl/base/dynamic_annotations.h"
#include "absl/base/internal/endian.h"
#include "absl/base/internal/raw_logging.h"
#include "absl/base/optimization.h"
#include "absl/container/internal/container_memory.h"
#include "absl/container/internal/hashtablez_sampler.h"
#include "absl/hash/hash.h"
#include "absl/numeric/bits.h"
namespace absl {
ABSL_NAMESPACE_BEGIN
namespace container_internal {
// Represents a control byte corresponding to a full slot with arbitrary hash.
constexpr ctrl_t ZeroCtrlT() { return static_cast<ctrl_t>(0); }
// We have space for `growth_info` before a single block of control bytes. A
// single block of empty control bytes for tables without any slots allocated.
// This enables removing a branch in the hot path of find(). In order to ensure
// that the control bytes are aligned to 16, we have 16 bytes before the control
// bytes even though growth_info only needs 8.
alignas(16) ABSL_CONST_INIT ABSL_DLL const ctrl_t kEmptyGroup[32] = {
ZeroCtrlT(), ZeroCtrlT(), ZeroCtrlT(), ZeroCtrlT(),
ZeroCtrlT(), ZeroCtrlT(), ZeroCtrlT(), ZeroCtrlT(),
ZeroCtrlT(), ZeroCtrlT(), ZeroCtrlT(), ZeroCtrlT(),
ZeroCtrlT(), ZeroCtrlT(), ZeroCtrlT(), ZeroCtrlT(),
ctrl_t::kSentinel, ctrl_t::kEmpty, ctrl_t::kEmpty, ctrl_t::kEmpty,
ctrl_t::kEmpty, ctrl_t::kEmpty, ctrl_t::kEmpty, ctrl_t::kEmpty,
ctrl_t::kEmpty, ctrl_t::kEmpty, ctrl_t::kEmpty, ctrl_t::kEmpty,
ctrl_t::kEmpty, ctrl_t::kEmpty, ctrl_t::kEmpty, ctrl_t::kEmpty};
// We need one full byte followed by a sentinel byte for iterator::operator++ to
// work. We have a full group after kSentinel to be safe (in case operator++ is
// changed to read a full group).
ABSL_CONST_INIT ABSL_DLL const ctrl_t kSooControl[17] = {
ZeroCtrlT(), ctrl_t::kSentinel, ZeroCtrlT(), ctrl_t::kEmpty,
ctrl_t::kEmpty, ctrl_t::kEmpty, ctrl_t::kEmpty, ctrl_t::kEmpty,
ctrl_t::kEmpty, ctrl_t::kEmpty, ctrl_t::kEmpty, ctrl_t::kEmpty,
ctrl_t::kEmpty, ctrl_t::kEmpty, ctrl_t::kEmpty, ctrl_t::kEmpty,
ctrl_t::kEmpty};
static_assert(NumControlBytes(SooCapacity()) <= 17,
"kSooControl capacity too small");
#ifdef ABSL_INTERNAL_NEED_REDUNDANT_CONSTEXPR_DECL
constexpr size_t Group::kWidth;
#endif
namespace {
// Returns "random" seed.
inline size_t RandomSeed() {
#ifdef ABSL_HAVE_THREAD_LOCAL
static thread_local size_t counter = 0;
// On Linux kernels >= 5.4 the MSAN runtime has a false-positive when
// accessing thread local storage data from loaded libraries
// (https://github.com/google/sanitizers/issues/1265), for this reason counter
// needs to be annotated as initialized.
ABSL_ANNOTATE_MEMORY_IS_INITIALIZED(&counter, sizeof(size_t));
size_t value = ++counter;
#else // ABSL_HAVE_THREAD_LOCAL
static std::atomic<size_t> counter(0);
size_t value = counter.fetch_add(1, std::memory_order_relaxed);
#endif // ABSL_HAVE_THREAD_LOCAL
return value ^ static_cast<size_t>(reinterpret_cast<uintptr_t>(&counter));
}
bool ShouldRehashForBugDetection(const ctrl_t* ctrl, size_t capacity) {
// Note: we can't use the abseil-random library because abseil-random
// depends on swisstable. We want to return true with probability
// `min(1, RehashProbabilityConstant() / capacity())`. In order to do this,
// we probe based on a random hash and see if the offset is less than
// RehashProbabilityConstant().
return probe(ctrl, capacity, absl::HashOf(RandomSeed())).offset() <
RehashProbabilityConstant();
}
// Find a non-deterministic hash for single group table.
// Last two bits are used to find a position for a newly inserted element after
// resize.
// This function is mixing all bits of hash and control pointer to maximize
// entropy.
size_t SingleGroupTableH1(size_t hash, ctrl_t* control) {
return static_cast<size_t>(absl::popcount(
hash ^ static_cast<size_t>(reinterpret_cast<uintptr_t>(control))));
}
} // namespace
GenerationType* EmptyGeneration() {
if (SwisstableGenerationsEnabled()) {
constexpr size_t kNumEmptyGenerations = 1024;
static constexpr GenerationType kEmptyGenerations[kNumEmptyGenerations]{};
return const_cast<GenerationType*>(
&kEmptyGenerations[RandomSeed() % kNumEmptyGenerations]);
}
return nullptr;
}
bool CommonFieldsGenerationInfoEnabled::
should_rehash_for_bug_detection_on_insert(const ctrl_t* ctrl,
size_t capacity) const {
if (reserved_growth_ == kReservedGrowthJustRanOut) return true;
if (reserved_growth_ > 0) return false;
return ShouldRehashForBugDetection(ctrl, capacity);
}
bool CommonFieldsGenerationInfoEnabled::should_rehash_for_bug_detection_on_move(
const ctrl_t* ctrl, size_t capacity) const {
return ShouldRehashForBugDetection(ctrl, capacity);
}
bool ShouldInsertBackwardsForDebug(size_t capacity, size_t hash,
const ctrl_t* ctrl) {
// To avoid problems with weak hashes and single bit tests, we use % 13.
// TODO(kfm,sbenza): revisit after we do unconditional mixing
return !is_small(capacity) && (H1(hash, ctrl) ^ RandomSeed()) % 13 > 6;
}
size_t PrepareInsertAfterSoo(size_t hash, size_t slot_size,
CommonFields& common) {
assert(common.capacity() == NextCapacity(SooCapacity()));
// After resize from capacity 1 to 3, we always have exactly the slot with
// index 1 occupied, so we need to insert either at index 0 or index 2.
static_assert(SooSlotIndex() == 1, "");
PrepareInsertCommon(common);
const size_t offset = SingleGroupTableH1(hash, common.control()) & 2;
common.growth_info().OverwriteEmptyAsFull();
SetCtrlInSingleGroupTable(common, offset, H2(hash), slot_size);
common.infoz().RecordInsert(hash, /*distance_from_desired=*/0);
return offset;
}
void ConvertDeletedToEmptyAndFullToDeleted(ctrl_t* ctrl, size_t capacity) {
assert(ctrl[capacity] == ctrl_t::kSentinel);
assert(IsValidCapacity(capacity));
for (ctrl_t* pos = ctrl; pos < ctrl + capacity; pos += Group::kWidth) {
Group{pos}.ConvertSpecialToEmptyAndFullToDeleted(pos);
}
// Copy the cloned ctrl bytes.
std::memcpy(ctrl + capacity + 1, ctrl, NumClonedBytes());
ctrl[capacity] = ctrl_t::kSentinel;
}
// Extern template instantiation for inline function.
template FindInfo find_first_non_full(const CommonFields&, size_t);
FindInfo find_first_non_full_outofline(const CommonFields& common,
size_t hash) {
return find_first_non_full(common, hash);
}
namespace {
// Returns the address of the slot just after slot assuming each slot has the
// specified size.
static inline void* NextSlot(void* slot, size_t slot_size) {
return reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(slot) + slot_size);
}
// Returns the address of the slot just before slot assuming each slot has the
// specified size.
static inline void* PrevSlot(void* slot, size_t slot_size) {
return reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(slot) - slot_size);
}
// Finds guaranteed to exists empty slot from the given position.
// NOTE: this function is almost never triggered inside of the
// DropDeletesWithoutResize, so we keep it simple.
// The table is rather sparse, so empty slot will be found very quickly.
size_t FindEmptySlot(size_t start, size_t end, const ctrl_t* ctrl) {
for (size_t i = start; i < end; ++i) {
if (IsEmpty(ctrl[i])) {
return i;
}
}
assert(false && "no empty slot");
return ~size_t{};
}
void DropDeletesWithoutResize(CommonFields& common,
const PolicyFunctions& policy) {
void* set = &common;
void* slot_array = common.slot_array();
const size_t capacity = common.capacity();
assert(IsValidCapacity(capacity));
assert(!is_small(capacity));
// Algorithm:
// - mark all DELETED slots as EMPTY
// - mark all FULL slots as DELETED
// - for each slot marked as DELETED
// hash = Hash(element)
// target = find_first_non_full(hash)
// if target is in the same group
// mark slot as FULL
// else if target is EMPTY
// transfer element to target
// mark slot as EMPTY
// mark target as FULL
// else if target is DELETED
// swap current element with target element
// mark target as FULL
// repeat procedure for current slot with moved from element (target)
ctrl_t* ctrl = common.control();
ConvertDeletedToEmptyAndFullToDeleted(ctrl, capacity);
const void* hash_fn = policy.hash_fn(common);
auto hasher = policy.hash_slot;
auto transfer = policy.transfer;
const size_t slot_size = policy.slot_size;
size_t total_probe_length = 0;
void* slot_ptr = SlotAddress(slot_array, 0, slot_size);
// The index of an empty slot that can be used as temporary memory for
// the swap operation.
constexpr size_t kUnknownId = ~size_t{};
size_t tmp_space_id = kUnknownId;
for (size_t i = 0; i != capacity;
++i, slot_ptr = NextSlot(slot_ptr, slot_size)) {
assert(slot_ptr == SlotAddress(slot_array, i, slot_size));
if (IsEmpty(ctrl[i])) {
tmp_space_id = i;
continue;
}
if (!IsDeleted(ctrl[i])) continue;
const size_t hash = (*hasher)(hash_fn, slot_ptr);
const FindInfo target = find_first_non_full(common, hash);
const size_t new_i = target.offset;
total_probe_length += target.probe_length;
// Verify if the old and new i fall within the same group wrt the hash.
// If they do, we don't need to move the object as it falls already in the
// best probe we can.
const size_t probe_offset = probe(common, hash).offset();
const auto probe_index = [probe_offset, capacity](size_t pos) {
return ((pos - probe_offset) & capacity) / Group::kWidth;
};
// Element doesn't move.
if (ABSL_PREDICT_TRUE(probe_index(new_i) == probe_index(i))) {
SetCtrl(common, i, H2(hash), slot_size);
continue;
}
void* new_slot_ptr = SlotAddress(slot_array, new_i, slot_size);
if (IsEmpty(ctrl[new_i])) {
// Transfer element to the empty spot.
// SetCtrl poisons/unpoisons the slots so we have to call it at the
// right time.
SetCtrl(common, new_i, H2(hash), slot_size);
(*transfer)(set, new_slot_ptr, slot_ptr);
SetCtrl(common, i, ctrl_t::kEmpty, slot_size);
// Initialize or change empty space id.
tmp_space_id = i;
} else {
assert(IsDeleted(ctrl[new_i]));
SetCtrl(common, new_i, H2(hash), slot_size);
// Until we are done rehashing, DELETED marks previously FULL slots.
if (tmp_space_id == kUnknownId) {
tmp_space_id = FindEmptySlot(i + 1, capacity, ctrl);
}
void* tmp_space = SlotAddress(slot_array, tmp_space_id, slot_size);
SanitizerUnpoisonMemoryRegion(tmp_space, slot_size);
// Swap i and new_i elements.
(*transfer)(set, tmp_space, new_slot_ptr);
(*transfer)(set, new_slot_ptr, slot_ptr);
(*transfer)(set, slot_ptr, tmp_space);
SanitizerPoisonMemoryRegion(tmp_space, slot_size);
// repeat the processing of the ith slot
--i;
slot_ptr = PrevSlot(slot_ptr, slot_size);
}
}
ResetGrowthLeft(common);
common.infoz().RecordRehash(total_probe_length);
}
static bool WasNeverFull(CommonFields& c, size_t index) {
if (is_single_group(c.capacity())) {
return true;
}
const size_t index_before = (index - Group::kWidth) & c.capacity();
const auto empty_after = Group(c.control() + index).MaskEmpty();
const auto empty_before = Group(c.control() + index_before).MaskEmpty();
// We count how many consecutive non empties we have to the right and to the
// left of `it`. If the sum is >= kWidth then there is at least one probe
// window that might have seen a full group.
return empty_before && empty_after &&
static_cast<size_t>(empty_after.TrailingZeros()) +
empty_before.LeadingZeros() <
Group::kWidth;
}
} // namespace
void EraseMetaOnly(CommonFields& c, size_t index, size_t slot_size) {
assert(IsFull(c.control()[index]) && "erasing a dangling iterator");
c.decrement_size();
c.infoz().RecordErase();
if (WasNeverFull(c, index)) {
SetCtrl(c, index, ctrl_t::kEmpty, slot_size);
c.growth_info().OverwriteFullAsEmpty();
return;
}
c.growth_info().OverwriteFullAsDeleted();
SetCtrl(c, index, ctrl_t::kDeleted, slot_size);
}
void ClearBackingArray(CommonFields& c, const PolicyFunctions& policy,
void* alloc, bool reuse, bool soo_enabled) {
c.set_size(0);
if (reuse) {
assert(!soo_enabled || c.capacity() > SooCapacity());
ResetCtrl(c, policy.slot_size);
ResetGrowthLeft(c);
c.infoz().RecordStorageChanged(0, c.capacity());
} else {
// We need to record infoz before calling dealloc, which will unregister
// infoz.
c.infoz().RecordClearedReservation();
c.infoz().RecordStorageChanged(0, soo_enabled ? SooCapacity() : 0);
c.infoz().Unregister();
(*policy.dealloc)(alloc, c.capacity(), c.control(), policy.slot_size,
policy.slot_align, c.has_infoz());
c = soo_enabled ? CommonFields{soo_tag_t{}} : CommonFields{non_soo_tag_t{}};
}
}
void HashSetResizeHelper::GrowIntoSingleGroupShuffleControlBytes(
ctrl_t* __restrict new_ctrl, size_t new_capacity) const {
assert(is_single_group(new_capacity));
constexpr size_t kHalfWidth = Group::kWidth / 2;
ABSL_ASSUME(old_capacity_ < kHalfWidth);
ABSL_ASSUME(old_capacity_ > 0);
static_assert(Group::kWidth == 8 || Group::kWidth == 16,
"Group size is not supported.");
// NOTE: operations are done with compile time known size = 8.
// Compiler optimizes that into single ASM operation.
// Load the bytes from old_capacity. This contains
// - the sentinel byte
// - all the old control bytes
// - the rest is filled with kEmpty bytes
// Example:
// old_ctrl = 012S012EEEEEEEEE...
// copied_bytes = S012EEEE
uint64_t copied_bytes =
absl::little_endian::Load64(old_ctrl() + old_capacity_);
// We change the sentinel byte to kEmpty before storing to both the start of
// the new_ctrl, and past the end of the new_ctrl later for the new cloned
// bytes. Note that this is faster than setting the sentinel byte to kEmpty
// after the copy directly in new_ctrl because we are limited on store
// bandwidth.
static constexpr uint64_t kEmptyXorSentinel =
static_cast<uint8_t>(ctrl_t::kEmpty) ^
static_cast<uint8_t>(ctrl_t::kSentinel);
// Replace the first byte kSentinel with kEmpty.
// Resulting bytes will be shifted by one byte old control blocks.
// Example:
// old_ctrl = 012S012EEEEEEEEE...
// before = S012EEEE
// after = E012EEEE
copied_bytes ^= kEmptyXorSentinel;
if (Group::kWidth == 8) {
// With group size 8, we can grow with two write operations.
assert(old_capacity_ < 8 && "old_capacity_ is too large for group size 8");
absl::little_endian::Store64(new_ctrl, copied_bytes);
static constexpr uint64_t kSentinal64 =
static_cast<uint8_t>(ctrl_t::kSentinel);
// Prepend kSentinel byte to the beginning of copied_bytes.
// We have maximum 3 non-empty bytes at the beginning of copied_bytes for
// group size 8.
// Example:
// old_ctrl = 012S012EEEE
// before = E012EEEE
// after = SE012EEE
copied_bytes = (copied_bytes << 8) ^ kSentinal64;
absl::little_endian::Store64(new_ctrl + new_capacity, copied_bytes);
// Example for capacity 3:
// old_ctrl = 012S012EEEE
// After the first store:
// >!
// new_ctrl = E012EEEE???????
// After the second store:
// >!
// new_ctrl = E012EEESE012EEE
return;
}
assert(Group::kWidth == 16);
// Fill the second half of the main control bytes with kEmpty.
// For small capacity that may write into mirrored control bytes.
// It is fine as we will overwrite all the bytes later.
std::memset(new_ctrl + kHalfWidth, static_cast<int8_t>(ctrl_t::kEmpty),
kHalfWidth);
// Fill the second half of the mirrored control bytes with kEmpty.
std::memset(new_ctrl + new_capacity + kHalfWidth,
static_cast<int8_t>(ctrl_t::kEmpty), kHalfWidth);
// Copy the first half of the non-mirrored control bytes.
absl::little_endian::Store64(new_ctrl, copied_bytes);
new_ctrl[new_capacity] = ctrl_t::kSentinel;
// Copy the first half of the mirrored control bytes.
absl::little_endian::Store64(new_ctrl + new_capacity + 1, copied_bytes);
// Example for growth capacity 1->3:
// old_ctrl = 0S0EEEEEEEEEEEEEE
// new_ctrl at the end = E0ESE0EEEEEEEEEEEEE
// >!
// new_ctrl after 1st memset = ????????EEEEEEEE???
// >!
// new_ctrl after 2nd memset = ????????EEEEEEEEEEE
// >!
// new_ctrl after 1st store = E0EEEEEEEEEEEEEEEEE
// new_ctrl after kSentinel = E0ESEEEEEEEEEEEEEEE
// >!
// new_ctrl after 2nd store = E0ESE0EEEEEEEEEEEEE
// Example for growth capacity 3->7:
// old_ctrl = 012S012EEEEEEEEEEEE
// new_ctrl at the end = E012EEESE012EEEEEEEEEEE
// >!
// new_ctrl after 1st memset = ????????EEEEEEEE???????
// >!
// new_ctrl after 2nd memset = ????????EEEEEEEEEEEEEEE
// >!
// new_ctrl after 1st store = E012EEEEEEEEEEEEEEEEEEE
// new_ctrl after kSentinel = E012EEESEEEEEEEEEEEEEEE
// >!
// new_ctrl after 2nd store = E012EEESE012EEEEEEEEEEE
// Example for growth capacity 7->15:
// old_ctrl = 0123456S0123456EEEEEEEE
// new_ctrl at the end = E0123456EEEEEEESE0123456EEEEEEE
// >!
// new_ctrl after 1st memset = ????????EEEEEEEE???????????????
// >!
// new_ctrl after 2nd memset = ????????EEEEEEEE???????EEEEEEEE
// >!
// new_ctrl after 1st store = E0123456EEEEEEEE???????EEEEEEEE
// new_ctrl after kSentinel = E0123456EEEEEEES???????EEEEEEEE
// >!
// new_ctrl after 2nd store = E0123456EEEEEEESE0123456EEEEEEE
}
void HashSetResizeHelper::GrowIntoSingleGroupShuffleTransferableSlots(
void* new_slots, size_t slot_size) const {
ABSL_ASSUME(old_capacity_ > 0);
SanitizerUnpoisonMemoryRegion(old_slots(), slot_size * old_capacity_);
std::memcpy(SlotAddress(new_slots, 1, slot_size), old_slots(),
slot_size * old_capacity_);
}
void HashSetResizeHelper::GrowSizeIntoSingleGroupTransferable(
CommonFields& c, size_t slot_size) {
assert(old_capacity_ < Group::kWidth / 2);
assert(is_single_group(c.capacity()));
assert(IsGrowingIntoSingleGroupApplicable(old_capacity_, c.capacity()));
GrowIntoSingleGroupShuffleControlBytes(c.control(), c.capacity());
GrowIntoSingleGroupShuffleTransferableSlots(c.slot_array(), slot_size);
// We poison since GrowIntoSingleGroupShuffleTransferableSlots
// may leave empty slots unpoisoned.
PoisonSingleGroupEmptySlots(c, slot_size);
}
void HashSetResizeHelper::InsertOldSooSlotAndInitializeControlBytesLarge(
CommonFields& c, size_t hash, ctrl_t* new_ctrl, void* new_slots,
const PolicyFunctions& policy) {
assert(was_soo_);
assert(had_soo_slot_);
size_t new_capacity = c.capacity();
size_t offset = probe(new_ctrl, new_capacity, hash).offset();
offset = offset == new_capacity ? 0 : offset;
SanitizerPoisonMemoryRegion(new_slots, policy.slot_size * new_capacity);
void* target_slot = SlotAddress(new_slots, offset, policy.slot_size);
SanitizerUnpoisonMemoryRegion(target_slot, policy.slot_size);
policy.transfer(&c, target_slot, c.soo_data());
c.set_control(new_ctrl);
c.set_slots(new_slots);
ResetCtrl(c, policy.slot_size);
SetCtrl(c, offset, H2(hash), policy.slot_size);
}
namespace {
// Called whenever the table needs to vacate empty slots either by removing
// tombstones via rehash or growth.
ABSL_ATTRIBUTE_NOINLINE
FindInfo FindInsertPositionWithGrowthOrRehash(CommonFields& common, size_t hash,
const PolicyFunctions& policy) {
const size_t cap = common.capacity();
if (cap > Group::kWidth &&
// Do these calculations in 64-bit to avoid overflow.
common.size() * uint64_t{32} <= cap * uint64_t{25}) {
// Squash DELETED without growing if there is enough capacity.
//
// Rehash in place if the current size is <= 25/32 of capacity.
// Rationale for such a high factor: 1) DropDeletesWithoutResize() is
// faster than resize, and 2) it takes quite a bit of work to add
// tombstones. In the worst case, seems to take approximately 4
// insert/erase pairs to create a single tombstone and so if we are
// rehashing because of tombstones, we can afford to rehash-in-place as
// long as we are reclaiming at least 1/8 the capacity without doing more
// than 2X the work. (Where "work" is defined to be size() for rehashing
// or rehashing in place, and 1 for an insert or erase.) But rehashing in
// place is faster per operation than inserting or even doubling the size
// of the table, so we actually afford to reclaim even less space from a
// resize-in-place. The decision is to rehash in place if we can reclaim
// at about 1/8th of the usable capacity (specifically 3/28 of the
// capacity) which means that the total cost of rehashing will be a small
// fraction of the total work.
//
// Here is output of an experiment using the BM_CacheInSteadyState
// benchmark running the old case (where we rehash-in-place only if we can
// reclaim at least 7/16*capacity) vs. this code (which rehashes in place
// if we can recover 3/32*capacity).
//
// Note that although in the worst-case number of rehashes jumped up from
// 15 to 190, but the number of operations per second is almost the same.
//
// Abridged output of running BM_CacheInSteadyState benchmark from
// raw_hash_set_benchmark. N is the number of insert/erase operations.
//
// | OLD (recover >= 7/16 | NEW (recover >= 3/32)
// size | N/s LoadFactor NRehashes | N/s LoadFactor NRehashes
// 448 | 145284 0.44 18 | 140118 0.44 19
// 493 | 152546 0.24 11 | 151417 0.48 28
// 538 | 151439 0.26 11 | 151152 0.53 38
// 583 | 151765 0.28 11 | 150572 0.57 50
// 628 | 150241 0.31 11 | 150853 0.61 66
// 672 | 149602 0.33 12 | 150110 0.66 90
// 717 | 149998 0.35 12 | 149531 0.70 129
// 762 | 149836 0.37 13 | 148559 0.74 190
// 807 | 149736 0.39 14 | 151107 0.39 14
// 852 | 150204 0.42 15 | 151019 0.42 15
DropDeletesWithoutResize(common, policy);
} else {
// Otherwise grow the container.
policy.resize(common, NextCapacity(cap), /*force_infoz=*/false);
}
// This function is typically called with tables containing deleted slots.
// The table will be big and `FindFirstNonFullAfterResize` will always
// fallback to `find_first_non_full`. So using `find_first_non_full` directly.
return find_first_non_full(common, hash);
}
} // namespace
const void* GetHashRefForEmptyHasher(const CommonFields& common) {
// Empty base optimization typically make the empty base class address to be
// the same as the first address of the derived class object.
// But we generally assume that for empty hasher we can return any valid
// pointer.
return &common;
}
FindInfo HashSetResizeHelper::FindFirstNonFullAfterResize(const CommonFields& c,
size_t old_capacity,
size_t hash) {
size_t new_capacity = c.capacity();
if (!IsGrowingIntoSingleGroupApplicable(old_capacity, new_capacity)) {
return find_first_non_full(c, hash);
}
// We put the new element either at the beginning or at the end of the table
// with approximately equal probability.
size_t offset =
SingleGroupTableH1(hash, c.control()) & 1 ? 0 : new_capacity - 1;
assert(IsEmpty(c.control()[offset]));
return FindInfo{offset, 0};
}
size_t PrepareInsertNonSoo(CommonFields& common, size_t hash, FindInfo target,
const PolicyFunctions& policy) {
// When there are no deleted slots in the table
// and growth_left is positive, we can insert at the first
// empty slot in the probe sequence (target).
const bool use_target_hint =
// Optimization is disabled when generations are enabled.
// We have to rehash even sparse tables randomly in such mode.
!SwisstableGenerationsEnabled() &&
common.growth_info().HasNoDeletedAndGrowthLeft();
if (ABSL_PREDICT_FALSE(!use_target_hint)) {
// Notes about optimized mode when generations are disabled:
// We do not enter this branch if table has no deleted slots
// and growth_left is positive.
// We enter this branch in the following cases listed in decreasing
// frequency:
// 1. Table without deleted slots (>95% cases) that needs to be resized.
// 2. Table with deleted slots that has space for the inserting element.
// 3. Table with deleted slots that needs to be rehashed or resized.
if (ABSL_PREDICT_TRUE(common.growth_info().HasNoGrowthLeftAndNoDeleted())) {
const size_t old_capacity = common.capacity();
policy.resize(common, NextCapacity(old_capacity), /*force_infoz=*/false);
target = HashSetResizeHelper::FindFirstNonFullAfterResize(
common, old_capacity, hash);
} else {
// Note: the table may have no deleted slots here when generations
// are enabled.
const bool rehash_for_bug_detection =
common.should_rehash_for_bug_detection_on_insert();
if (rehash_for_bug_detection) {
// Move to a different heap allocation in order to detect bugs.
const size_t cap = common.capacity();
policy.resize(common,
common.growth_left() > 0 ? cap : NextCapacity(cap),
/*force_infoz=*/false);
}
if (ABSL_PREDICT_TRUE(common.growth_left() > 0)) {
target = find_first_non_full(common, hash);
} else {
target = FindInsertPositionWithGrowthOrRehash(common, hash, policy);
}
}
}
PrepareInsertCommon(common);
common.growth_info().OverwriteControlAsFull(common.control()[target.offset]);
SetCtrl(common, target.offset, H2(hash), policy.slot_size);
common.infoz().RecordInsert(hash, target.probe_length);
return target.offset;
}
void HashTableSizeOverflow() {
ABSL_RAW_LOG(FATAL, "Hash table size overflow");
}
} // namespace container_internal
ABSL_NAMESPACE_END
} // namespace absl

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,520 @@
// Copyright 2018 The Abseil Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include <cstddef>
#include <cstdint>
#include <functional>
#include <limits>
#include <memory>
#include <ostream>
#include <set>
#include <type_traits>
#include <utility>
#include "gmock/gmock.h"
#include "gtest/gtest.h"
#include "absl/base/config.h"
#include "absl/container/internal/container_memory.h"
#include "absl/container/internal/raw_hash_set.h"
#include "absl/container/internal/tracked.h"
namespace absl {
ABSL_NAMESPACE_BEGIN
namespace container_internal {
namespace {
using ::testing::AnyOf;
enum AllocSpec {
kPropagateOnCopy = 1,
kPropagateOnMove = 2,
kPropagateOnSwap = 4,
};
struct AllocState {
size_t num_allocs = 0;
std::set<void*> owned;
};
template <class T,
int Spec = kPropagateOnCopy | kPropagateOnMove | kPropagateOnSwap>
class CheckedAlloc {
public:
template <class, int>
friend class CheckedAlloc;
using value_type = T;
CheckedAlloc() {}
explicit CheckedAlloc(size_t id) : id_(id) {}
CheckedAlloc(const CheckedAlloc&) = default;
CheckedAlloc& operator=(const CheckedAlloc&) = default;
template <class U>
CheckedAlloc(const CheckedAlloc<U, Spec>& that)
: id_(that.id_), state_(that.state_) {}
template <class U>
struct rebind {
using other = CheckedAlloc<U, Spec>;
};
using propagate_on_container_copy_assignment =
std::integral_constant<bool, (Spec & kPropagateOnCopy) != 0>;
using propagate_on_container_move_assignment =
std::integral_constant<bool, (Spec & kPropagateOnMove) != 0>;
using propagate_on_container_swap =
std::integral_constant<bool, (Spec & kPropagateOnSwap) != 0>;
CheckedAlloc select_on_container_copy_construction() const {
if (Spec & kPropagateOnCopy) return *this;
return {};
}
T* allocate(size_t n) {
T* ptr = std::allocator<T>().allocate(n);
track_alloc(ptr);
return ptr;
}
void deallocate(T* ptr, size_t n) {
memset(ptr, 0, n * sizeof(T)); // The freed memory must be unpoisoned.
track_dealloc(ptr);
return std::allocator<T>().deallocate(ptr, n);
}
friend bool operator==(const CheckedAlloc& a, const CheckedAlloc& b) {
return a.id_ == b.id_;
}
friend bool operator!=(const CheckedAlloc& a, const CheckedAlloc& b) {
return !(a == b);
}
size_t num_allocs() const { return state_->num_allocs; }
void swap(CheckedAlloc& that) {
using std::swap;
swap(id_, that.id_);
swap(state_, that.state_);
}
friend void swap(CheckedAlloc& a, CheckedAlloc& b) { a.swap(b); }
friend std::ostream& operator<<(std::ostream& o, const CheckedAlloc& a) {
return o << "alloc(" << a.id_ << ")";
}
private:
void track_alloc(void* ptr) {
AllocState* state = state_.get();
++state->num_allocs;
if (!state->owned.insert(ptr).second)
ADD_FAILURE() << *this << " got previously allocated memory: " << ptr;
}
void track_dealloc(void* ptr) {
if (state_->owned.erase(ptr) != 1)
ADD_FAILURE() << *this
<< " deleting memory owned by another allocator: " << ptr;
}
size_t id_ = std::numeric_limits<size_t>::max();
std::shared_ptr<AllocState> state_ = std::make_shared<AllocState>();
};
struct Identity {
size_t operator()(int32_t v) const { return static_cast<size_t>(v); }
};
struct Policy {
using slot_type = Tracked<int32_t>;
using init_type = Tracked<int32_t>;
using key_type = int32_t;
template <class allocator_type, class... Args>
static void construct(allocator_type* alloc, slot_type* slot,
Args&&... args) {
std::allocator_traits<allocator_type>::construct(
*alloc, slot, std::forward<Args>(args)...);
}
template <class allocator_type>
static void destroy(allocator_type* alloc, slot_type* slot) {
std::allocator_traits<allocator_type>::destroy(*alloc, slot);
}
template <class allocator_type>
static void transfer(allocator_type* alloc, slot_type* new_slot,
slot_type* old_slot) {
construct(alloc, new_slot, std::move(*old_slot));
destroy(alloc, old_slot);
}
template <class F>
static auto apply(F&& f, int32_t v) -> decltype(std::forward<F>(f)(v, v)) {
return std::forward<F>(f)(v, v);
}
template <class F>
static auto apply(F&& f, const slot_type& v)
-> decltype(std::forward<F>(f)(v.val(), v)) {
return std::forward<F>(f)(v.val(), v);
}
template <class F>
static auto apply(F&& f, slot_type&& v)
-> decltype(std::forward<F>(f)(v.val(), std::move(v))) {
return std::forward<F>(f)(v.val(), std::move(v));
}
static slot_type& element(slot_type* slot) { return *slot; }
template <class Hash>
static constexpr HashSlotFn get_hash_slot_fn() {
return nullptr;
}
};
template <int Spec>
struct PropagateTest : public ::testing::Test {
using Alloc = CheckedAlloc<Tracked<int32_t>, Spec>;
using Table = raw_hash_set<Policy, Identity, std::equal_to<int32_t>, Alloc>;
PropagateTest() {
EXPECT_EQ(a1, t1.get_allocator());
EXPECT_NE(a2, t1.get_allocator());
}
Alloc a1 = Alloc(1);
Table t1 = Table(0, a1);
Alloc a2 = Alloc(2);
};
using PropagateOnAll =
PropagateTest<kPropagateOnCopy | kPropagateOnMove | kPropagateOnSwap>;
using NoPropagateOnCopy = PropagateTest<kPropagateOnMove | kPropagateOnSwap>;
using NoPropagateOnMove = PropagateTest<kPropagateOnCopy | kPropagateOnSwap>;
TEST_F(PropagateOnAll, Empty) { EXPECT_EQ(0, a1.num_allocs()); }
TEST_F(PropagateOnAll, InsertAllocates) {
auto it = t1.insert(0).first;
EXPECT_EQ(1, a1.num_allocs());
EXPECT_EQ(0, it->num_moves());
EXPECT_EQ(0, it->num_copies());
}
TEST_F(PropagateOnAll, InsertDecomposes) {
auto it = t1.insert(0).first;
EXPECT_EQ(1, a1.num_allocs());
EXPECT_EQ(0, it->num_moves());
EXPECT_EQ(0, it->num_copies());
EXPECT_FALSE(t1.insert(0).second);
EXPECT_EQ(1, a1.num_allocs());
EXPECT_EQ(0, it->num_moves());
EXPECT_EQ(0, it->num_copies());
}
TEST_F(PropagateOnAll, RehashMoves) {
auto it = t1.insert(0).first;
EXPECT_EQ(0, it->num_moves());
t1.rehash(2 * t1.capacity());
EXPECT_EQ(2, a1.num_allocs());
it = t1.find(0);
EXPECT_EQ(1, it->num_moves());
EXPECT_EQ(0, it->num_copies());
}
TEST_F(PropagateOnAll, CopyConstructor) {
auto it = t1.insert(0).first;
Table u(t1);
EXPECT_EQ(2, a1.num_allocs());
EXPECT_EQ(0, it->num_moves());
EXPECT_EQ(1, it->num_copies());
}
TEST_F(NoPropagateOnCopy, CopyConstructor) {
auto it = t1.insert(0).first;
Table u(t1);
EXPECT_EQ(1, a1.num_allocs());
EXPECT_EQ(1, u.get_allocator().num_allocs());
EXPECT_EQ(0, it->num_moves());
EXPECT_EQ(1, it->num_copies());
}
TEST_F(PropagateOnAll, CopyConstructorWithSameAlloc) {
auto it = t1.insert(0).first;
Table u(t1, a1);
EXPECT_EQ(2, a1.num_allocs());
EXPECT_EQ(0, it->num_moves());
EXPECT_EQ(1, it->num_copies());
}
TEST_F(NoPropagateOnCopy, CopyConstructorWithSameAlloc) {
auto it = t1.insert(0).first;
Table u(t1, a1);
EXPECT_EQ(2, a1.num_allocs());
EXPECT_EQ(0, it->num_moves());
EXPECT_EQ(1, it->num_copies());
}
TEST_F(PropagateOnAll, CopyConstructorWithDifferentAlloc) {
auto it = t1.insert(0).first;
Table u(t1, a2);
EXPECT_EQ(a2, u.get_allocator());
EXPECT_EQ(1, a1.num_allocs());
EXPECT_EQ(1, a2.num_allocs());
EXPECT_EQ(0, it->num_moves());
EXPECT_EQ(1, it->num_copies());
}
TEST_F(NoPropagateOnCopy, CopyConstructorWithDifferentAlloc) {
auto it = t1.insert(0).first;
Table u(t1, a2);
EXPECT_EQ(a2, u.get_allocator());
EXPECT_EQ(1, a1.num_allocs());
EXPECT_EQ(1, a2.num_allocs());
EXPECT_EQ(0, it->num_moves());
EXPECT_EQ(1, it->num_copies());
}
TEST_F(PropagateOnAll, MoveConstructor) {
t1.insert(0);
Table u(std::move(t1));
auto it = u.begin();
EXPECT_THAT(a1.num_allocs(), AnyOf(1, 2));
EXPECT_THAT(it->num_moves(), AnyOf(0, 1));
EXPECT_EQ(0, it->num_copies());
}
TEST_F(NoPropagateOnMove, MoveConstructor) {
t1.insert(0);
Table u(std::move(t1));
auto it = u.begin();
EXPECT_THAT(a1.num_allocs(), AnyOf(1, 2));
EXPECT_THAT(it->num_moves(), AnyOf(0, 1));
EXPECT_EQ(0, it->num_copies());
}
TEST_F(PropagateOnAll, MoveConstructorWithSameAlloc) {
t1.insert(0);
Table u(std::move(t1), a1);
auto it = u.begin();
EXPECT_THAT(a1.num_allocs(), AnyOf(1, 2));
EXPECT_THAT(it->num_moves(), AnyOf(0, 1));
EXPECT_EQ(0, it->num_copies());
}
TEST_F(NoPropagateOnMove, MoveConstructorWithSameAlloc) {
t1.insert(0);
Table u(std::move(t1), a1);
auto it = u.begin();
EXPECT_THAT(a1.num_allocs(), AnyOf(1, 2));
EXPECT_THAT(it->num_moves(), AnyOf(0, 1));
EXPECT_EQ(0, it->num_copies());
}
TEST_F(PropagateOnAll, MoveConstructorWithDifferentAlloc) {
auto it = t1.insert(0).first;
Table u(std::move(t1), a2);
it = u.find(0);
EXPECT_EQ(a2, u.get_allocator());
EXPECT_EQ(1, a1.num_allocs());
EXPECT_THAT(a2.num_allocs(), AnyOf(1, 2));
EXPECT_THAT(it->num_moves(), AnyOf(1, 2));
EXPECT_EQ(0, it->num_copies());
}
TEST_F(NoPropagateOnMove, MoveConstructorWithDifferentAlloc) {
auto it = t1.insert(0).first;
Table u(std::move(t1), a2);
it = u.find(0);
EXPECT_EQ(a2, u.get_allocator());
EXPECT_EQ(1, a1.num_allocs());
EXPECT_THAT(a2.num_allocs(), AnyOf(1, 2));
EXPECT_THAT(it->num_moves(), AnyOf(1, 2));
EXPECT_EQ(0, it->num_copies());
}
TEST_F(PropagateOnAll, CopyAssignmentWithSameAlloc) {
auto it = t1.insert(0).first;
Table u(0, a1);
u = t1;
EXPECT_THAT(a1.num_allocs(), AnyOf(2, 3));
EXPECT_THAT(it->num_moves(), AnyOf(0, 1));
EXPECT_EQ(1, it->num_copies());
}
TEST_F(NoPropagateOnCopy, CopyAssignmentWithSameAlloc) {
auto it = t1.insert(0).first;
Table u(0, a1);
u = t1;
EXPECT_THAT(a1.num_allocs(), AnyOf(2, 3));
EXPECT_THAT(it->num_moves(), AnyOf(0, 1));
EXPECT_EQ(1, it->num_copies());
}
TEST_F(PropagateOnAll, CopyAssignmentWithDifferentAlloc) {
auto it = t1.insert(0).first;
Table u(0, a2);
u = t1;
EXPECT_EQ(a1, u.get_allocator());
EXPECT_THAT(a1.num_allocs(), AnyOf(2, 3));
EXPECT_EQ(0, a2.num_allocs());
EXPECT_THAT(it->num_moves(), AnyOf(0, 1));
EXPECT_EQ(1, it->num_copies());
}
TEST_F(NoPropagateOnCopy, CopyAssignmentWithDifferentAlloc) {
auto it = t1.insert(0).first;
Table u(0, a2);
u = t1;
EXPECT_EQ(a2, u.get_allocator());
EXPECT_EQ(1, a1.num_allocs());
EXPECT_THAT(a2.num_allocs(), AnyOf(1, 2));
EXPECT_THAT(it->num_moves(), AnyOf(0, 1));
EXPECT_EQ(1, it->num_copies());
}
TEST_F(PropagateOnAll, MoveAssignmentWithSameAlloc) {
t1.insert(0);
Table u(0, a1);
u = std::move(t1);
auto it = u.begin();
EXPECT_EQ(a1, u.get_allocator());
EXPECT_THAT(a1.num_allocs(), AnyOf(1, 2));
EXPECT_THAT(it->num_moves(), AnyOf(0, 1));
EXPECT_EQ(0, it->num_copies());
}
TEST_F(NoPropagateOnMove, MoveAssignmentWithSameAlloc) {
t1.insert(0);
Table u(0, a1);
u = std::move(t1);
auto it = u.begin();
EXPECT_EQ(a1, u.get_allocator());
EXPECT_THAT(a1.num_allocs(), AnyOf(1, 2));
EXPECT_THAT(it->num_moves(), AnyOf(0, 1));
EXPECT_EQ(0, it->num_copies());
}
TEST_F(PropagateOnAll, MoveAssignmentWithDifferentAlloc) {
t1.insert(0);
Table u(0, a2);
u = std::move(t1);
auto it = u.begin();
EXPECT_EQ(a1, u.get_allocator());
EXPECT_THAT(a1.num_allocs(), AnyOf(1, 2));
EXPECT_EQ(0, a2.num_allocs());
EXPECT_THAT(it->num_moves(), AnyOf(0, 1));
EXPECT_EQ(0, it->num_copies());
}
TEST_F(NoPropagateOnMove, MoveAssignmentWithDifferentAlloc) {
t1.insert(0);
Table u(0, a2);
u = std::move(t1);
auto it = u.find(0);
EXPECT_EQ(a2, u.get_allocator());
EXPECT_EQ(1, a1.num_allocs());
EXPECT_THAT(a2.num_allocs(), AnyOf(1, 2));
EXPECT_THAT(it->num_moves(), AnyOf(1, 2));
EXPECT_EQ(0, it->num_copies());
}
TEST_F(PropagateOnAll, Swap) {
auto it = t1.insert(0).first;
Table u(0, a2);
u.swap(t1);
EXPECT_EQ(a1, u.get_allocator());
EXPECT_EQ(a2, t1.get_allocator());
EXPECT_EQ(1, a1.num_allocs());
EXPECT_EQ(0, a2.num_allocs());
EXPECT_EQ(0, it->num_moves());
EXPECT_EQ(0, it->num_copies());
}
// This allocator is similar to std::pmr::polymorphic_allocator.
// Note the disabled assignment.
template <class T>
class PAlloc {
template <class>
friend class PAlloc;
public:
// types
using value_type = T;
PAlloc() noexcept = default;
explicit PAlloc(size_t id) noexcept : id_(id) {}
PAlloc(const PAlloc&) noexcept = default;
PAlloc& operator=(const PAlloc&) noexcept = delete;
template <class U>
PAlloc(const PAlloc<U>& that) noexcept : id_(that.id_) {} // NOLINT
template <class U>
struct rebind {
using other = PAlloc<U>;
};
constexpr PAlloc select_on_container_copy_construction() const { return {}; }
// public member functions
T* allocate(size_t) { return new T; }
void deallocate(T* p, size_t) { delete p; }
friend bool operator==(const PAlloc& a, const PAlloc& b) {
return a.id_ == b.id_;
}
friend bool operator!=(const PAlloc& a, const PAlloc& b) { return !(a == b); }
private:
size_t id_ = std::numeric_limits<size_t>::max();
};
TEST(NoPropagateDeletedAssignment, CopyConstruct) {
using PA = PAlloc<char>;
using Table = raw_hash_set<Policy, Identity, std::equal_to<int32_t>, PA>;
Table t1(PA{1}), t2(t1);
EXPECT_EQ(t1.get_allocator(), PA(1));
EXPECT_EQ(t2.get_allocator(), PA());
}
TEST(NoPropagateDeletedAssignment, CopyAssignment) {
using PA = PAlloc<char>;
using Table = raw_hash_set<Policy, Identity, std::equal_to<int32_t>, PA>;
Table t1(PA{1}), t2(PA{2});
t1 = t2;
EXPECT_EQ(t1.get_allocator(), PA(1));
EXPECT_EQ(t2.get_allocator(), PA(2));
}
TEST(NoPropagateDeletedAssignment, MoveAssignment) {
using PA = PAlloc<char>;
using Table = raw_hash_set<Policy, Identity, std::equal_to<int32_t>, PA>;
Table t1(PA{1}), t2(PA{2});
t1 = std::move(t2);
EXPECT_EQ(t1.get_allocator(), PA(1));
}
} // namespace
} // namespace container_internal
ABSL_NAMESPACE_END
} // namespace absl

View file

@ -0,0 +1,687 @@
// Copyright 2018 The Abseil Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include <algorithm>
#include <array>
#include <cmath>
#include <cstddef>
#include <cstdint>
#include <limits>
#include <numeric>
#include <random>
#include <string>
#include <tuple>
#include <utility>
#include <vector>
#include "absl/base/internal/raw_logging.h"
#include "absl/container/internal/container_memory.h"
#include "absl/container/internal/hash_function_defaults.h"
#include "absl/container/internal/raw_hash_set.h"
#include "absl/random/random.h"
#include "absl/strings/str_format.h"
#include "benchmark/benchmark.h"
namespace absl {
ABSL_NAMESPACE_BEGIN
namespace container_internal {
struct RawHashSetTestOnlyAccess {
template <typename C>
static auto GetSlots(const C& c) -> decltype(c.slots_) {
return c.slots_;
}
};
namespace {
struct IntPolicy {
using slot_type = int64_t;
using key_type = int64_t;
using init_type = int64_t;
static void construct(void*, int64_t* slot, int64_t v) { *slot = v; }
static void destroy(void*, int64_t*) {}
static void transfer(void*, int64_t* new_slot, int64_t* old_slot) {
*new_slot = *old_slot;
}
static int64_t& element(slot_type* slot) { return *slot; }
template <class F>
static auto apply(F&& f, int64_t x) -> decltype(std::forward<F>(f)(x, x)) {
return std::forward<F>(f)(x, x);
}
template <class Hash>
static constexpr HashSlotFn get_hash_slot_fn() {
return nullptr;
}
};
class StringPolicy {
template <class F, class K, class V,
class = typename std::enable_if<
std::is_convertible<const K&, absl::string_view>::value>::type>
decltype(std::declval<F>()(
std::declval<const absl::string_view&>(), std::piecewise_construct,
std::declval<std::tuple<K>>(),
std::declval<V>())) static apply_impl(F&& f,
std::pair<std::tuple<K>, V> p) {
const absl::string_view& key = std::get<0>(p.first);
return std::forward<F>(f)(key, std::piecewise_construct, std::move(p.first),
std::move(p.second));
}
public:
struct slot_type {
struct ctor {};
template <class... Ts>
slot_type(ctor, Ts&&... ts) : pair(std::forward<Ts>(ts)...) {}
std::pair<std::string, std::string> pair;
};
using key_type = std::string;
using init_type = std::pair<std::string, std::string>;
template <class allocator_type, class... Args>
static void construct(allocator_type* alloc, slot_type* slot, Args... args) {
std::allocator_traits<allocator_type>::construct(
*alloc, slot, typename slot_type::ctor(), std::forward<Args>(args)...);
}
template <class allocator_type>
static void destroy(allocator_type* alloc, slot_type* slot) {
std::allocator_traits<allocator_type>::destroy(*alloc, slot);
}
template <class allocator_type>
static void transfer(allocator_type* alloc, slot_type* new_slot,
slot_type* old_slot) {
construct(alloc, new_slot, std::move(old_slot->pair));
destroy(alloc, old_slot);
}
static std::pair<std::string, std::string>& element(slot_type* slot) {
return slot->pair;
}
template <class F, class... Args>
static auto apply(F&& f, Args&&... args)
-> decltype(apply_impl(std::forward<F>(f),
PairArgs(std::forward<Args>(args)...))) {
return apply_impl(std::forward<F>(f),
PairArgs(std::forward<Args>(args)...));
}
template <class Hash>
static constexpr HashSlotFn get_hash_slot_fn() {
return nullptr;
}
};
struct StringHash : container_internal::hash_default_hash<absl::string_view> {
using is_transparent = void;
};
struct StringEq : std::equal_to<absl::string_view> {
using is_transparent = void;
};
struct StringTable
: raw_hash_set<StringPolicy, StringHash, StringEq, std::allocator<int>> {
using Base = typename StringTable::raw_hash_set;
StringTable() {}
using Base::Base;
};
struct IntTable
: raw_hash_set<IntPolicy, container_internal::hash_default_hash<int64_t>,
std::equal_to<int64_t>, std::allocator<int64_t>> {
using Base = typename IntTable::raw_hash_set;
IntTable() {}
using Base::Base;
};
struct string_generator {
template <class RNG>
std::string operator()(RNG& rng) const {
std::string res;
res.resize(size);
std::uniform_int_distribution<uint32_t> printable_ascii(0x20, 0x7E);
std::generate(res.begin(), res.end(), [&] { return printable_ascii(rng); });
return res;
}
size_t size;
};
// Model a cache in steady state.
//
// On a table of size N, keep deleting the LRU entry and add a random one.
void BM_CacheInSteadyState(benchmark::State& state) {
std::random_device rd;
std::mt19937 rng(rd());
string_generator gen{12};
StringTable t;
std::deque<std::string> keys;
while (t.size() < state.range(0)) {
auto x = t.emplace(gen(rng), gen(rng));
if (x.second) keys.push_back(x.first->first);
}
ABSL_RAW_CHECK(state.range(0) >= 10, "");
while (state.KeepRunning()) {
// Some cache hits.
std::deque<std::string>::const_iterator it;
for (int i = 0; i != 90; ++i) {
if (i % 10 == 0) it = keys.end();
::benchmark::DoNotOptimize(t.find(*--it));
}
// Some cache misses.
for (int i = 0; i != 10; ++i) ::benchmark::DoNotOptimize(t.find(gen(rng)));
ABSL_RAW_CHECK(t.erase(keys.front()), keys.front().c_str());
keys.pop_front();
while (true) {
auto x = t.emplace(gen(rng), gen(rng));
if (x.second) {
keys.push_back(x.first->first);
break;
}
}
}
state.SetItemsProcessed(state.iterations());
state.SetLabel(absl::StrFormat("load_factor=%.2f", t.load_factor()));
}
template <typename Benchmark>
void CacheInSteadyStateArgs(Benchmark* bm) {
// The default.
const float max_load_factor = 0.875;
// When the cache is at the steady state, the probe sequence will equal
// capacity if there is no reclamation of deleted slots. Pick a number large
// enough to make the benchmark slow for that case.
const size_t capacity = 1 << 10;
// Check N data points to cover load factors in [0.4, 0.8).
const size_t kNumPoints = 10;
for (size_t i = 0; i != kNumPoints; ++i)
bm->Arg(std::ceil(
capacity * (max_load_factor + i * max_load_factor / kNumPoints) / 2));
}
BENCHMARK(BM_CacheInSteadyState)->Apply(CacheInSteadyStateArgs);
void BM_EraseEmplace(benchmark::State& state) {
IntTable t;
int64_t size = state.range(0);
for (int64_t i = 0; i < size; ++i) {
t.emplace(i);
}
while (state.KeepRunningBatch(size)) {
for (int64_t i = 0; i < size; ++i) {
benchmark::DoNotOptimize(t);
t.erase(i);
t.emplace(i);
}
}
}
BENCHMARK(BM_EraseEmplace)->Arg(1)->Arg(2)->Arg(4)->Arg(8)->Arg(16)->Arg(100);
void BM_EndComparison(benchmark::State& state) {
StringTable t = {{"a", "a"}, {"b", "b"}};
auto it = t.begin();
for (auto i : state) {
benchmark::DoNotOptimize(t);
benchmark::DoNotOptimize(it);
benchmark::DoNotOptimize(it != t.end());
}
}
BENCHMARK(BM_EndComparison);
void BM_Iteration(benchmark::State& state) {
std::random_device rd;
std::mt19937 rng(rd());
string_generator gen{12};
StringTable t;
size_t capacity = state.range(0);
size_t size = state.range(1);
t.reserve(capacity);
while (t.size() < size) {
t.emplace(gen(rng), gen(rng));
}
for (auto i : state) {
benchmark::DoNotOptimize(t);
for (auto it = t.begin(); it != t.end(); ++it) {
benchmark::DoNotOptimize(*it);
}
}
}
BENCHMARK(BM_Iteration)
->ArgPair(1, 1)
->ArgPair(2, 2)
->ArgPair(4, 4)
->ArgPair(7, 7)
->ArgPair(10, 10)
->ArgPair(15, 15)
->ArgPair(16, 16)
->ArgPair(54, 54)
->ArgPair(100, 100)
->ArgPair(400, 400)
// empty
->ArgPair(0, 0)
->ArgPair(10, 0)
->ArgPair(100, 0)
->ArgPair(1000, 0)
->ArgPair(10000, 0)
// sparse
->ArgPair(100, 1)
->ArgPair(1000, 10);
void BM_CopyCtorSparseInt(benchmark::State& state) {
std::random_device rd;
std::mt19937 rng(rd());
IntTable t;
std::uniform_int_distribution<uint64_t> dist(0, ~uint64_t{});
size_t size = state.range(0);
t.reserve(size * 10);
while (t.size() < size) {
t.emplace(dist(rng));
}
for (auto i : state) {
IntTable t2 = t;
benchmark::DoNotOptimize(t2);
}
}
BENCHMARK(BM_CopyCtorSparseInt)->Range(1, 4096);
void BM_CopyCtorInt(benchmark::State& state) {
std::random_device rd;
std::mt19937 rng(rd());
IntTable t;
std::uniform_int_distribution<uint64_t> dist(0, ~uint64_t{});
size_t size = state.range(0);
while (t.size() < size) {
t.emplace(dist(rng));
}
for (auto i : state) {
IntTable t2 = t;
benchmark::DoNotOptimize(t2);
}
}
BENCHMARK(BM_CopyCtorInt)->Range(0, 4096);
void BM_CopyCtorString(benchmark::State& state) {
std::random_device rd;
std::mt19937 rng(rd());
StringTable t;
std::uniform_int_distribution<uint64_t> dist(0, ~uint64_t{});
size_t size = state.range(0);
while (t.size() < size) {
t.emplace(std::to_string(dist(rng)), std::to_string(dist(rng)));
}
for (auto i : state) {
StringTable t2 = t;
benchmark::DoNotOptimize(t2);
}
}
BENCHMARK(BM_CopyCtorString)->Range(0, 4096);
void BM_CopyAssign(benchmark::State& state) {
std::random_device rd;
std::mt19937 rng(rd());
IntTable t;
std::uniform_int_distribution<uint64_t> dist(0, ~uint64_t{});
while (t.size() < state.range(0)) {
t.emplace(dist(rng));
}
IntTable t2;
for (auto _ : state) {
t2 = t;
benchmark::DoNotOptimize(t2);
}
}
BENCHMARK(BM_CopyAssign)->Range(128, 4096);
void BM_RangeCtor(benchmark::State& state) {
std::random_device rd;
std::mt19937 rng(rd());
std::uniform_int_distribution<uint64_t> dist(0, ~uint64_t{});
std::vector<int> values;
const size_t desired_size = state.range(0);
while (values.size() < desired_size) {
values.emplace_back(dist(rng));
}
for (auto unused : state) {
IntTable t{values.begin(), values.end()};
benchmark::DoNotOptimize(t);
}
}
BENCHMARK(BM_RangeCtor)->Range(128, 65536);
void BM_NoOpReserveIntTable(benchmark::State& state) {
IntTable t;
t.reserve(100000);
for (auto _ : state) {
benchmark::DoNotOptimize(t);
t.reserve(100000);
}
}
BENCHMARK(BM_NoOpReserveIntTable);
void BM_NoOpReserveStringTable(benchmark::State& state) {
StringTable t;
t.reserve(100000);
for (auto _ : state) {
benchmark::DoNotOptimize(t);
t.reserve(100000);
}
}
BENCHMARK(BM_NoOpReserveStringTable);
void BM_ReserveIntTable(benchmark::State& state) {
constexpr size_t kBatchSize = 1024;
size_t reserve_size = static_cast<size_t>(state.range(0));
std::vector<IntTable> tables;
while (state.KeepRunningBatch(kBatchSize)) {
state.PauseTiming();
tables.clear();
tables.resize(kBatchSize);
state.ResumeTiming();
for (auto& t : tables) {
benchmark::DoNotOptimize(t);
t.reserve(reserve_size);
benchmark::DoNotOptimize(t);
}
}
}
BENCHMARK(BM_ReserveIntTable)->Range(1, 64);
void BM_ReserveStringTable(benchmark::State& state) {
constexpr size_t kBatchSize = 1024;
size_t reserve_size = static_cast<size_t>(state.range(0));
std::vector<StringTable> tables;
while (state.KeepRunningBatch(kBatchSize)) {
state.PauseTiming();
tables.clear();
tables.resize(kBatchSize);
state.ResumeTiming();
for (auto& t : tables) {
benchmark::DoNotOptimize(t);
t.reserve(reserve_size);
benchmark::DoNotOptimize(t);
}
}
}
BENCHMARK(BM_ReserveStringTable)->Range(1, 64);
// Like std::iota, except that ctrl_t doesn't support operator++.
template <typename CtrlIter>
void Iota(CtrlIter begin, CtrlIter end, int value) {
for (; begin != end; ++begin, ++value) {
*begin = static_cast<ctrl_t>(value);
}
}
void BM_Group_Match(benchmark::State& state) {
std::array<ctrl_t, Group::kWidth> group;
Iota(group.begin(), group.end(), -4);
Group g{group.data()};
h2_t h = 1;
for (auto _ : state) {
::benchmark::DoNotOptimize(h);
::benchmark::DoNotOptimize(g);
::benchmark::DoNotOptimize(g.Match(h));
}
}
BENCHMARK(BM_Group_Match);
void BM_GroupPortable_Match(benchmark::State& state) {
std::array<ctrl_t, GroupPortableImpl::kWidth> group;
Iota(group.begin(), group.end(), -4);
GroupPortableImpl g{group.data()};
h2_t h = 1;
for (auto _ : state) {
::benchmark::DoNotOptimize(h);
::benchmark::DoNotOptimize(g);
::benchmark::DoNotOptimize(g.Match(h));
}
}
BENCHMARK(BM_GroupPortable_Match);
void BM_Group_MaskEmpty(benchmark::State& state) {
std::array<ctrl_t, Group::kWidth> group;
Iota(group.begin(), group.end(), -4);
Group g{group.data()};
for (auto _ : state) {
::benchmark::DoNotOptimize(g);
::benchmark::DoNotOptimize(g.MaskEmpty());
}
}
BENCHMARK(BM_Group_MaskEmpty);
void BM_Group_MaskEmptyOrDeleted(benchmark::State& state) {
std::array<ctrl_t, Group::kWidth> group;
Iota(group.begin(), group.end(), -4);
Group g{group.data()};
for (auto _ : state) {
::benchmark::DoNotOptimize(g);
::benchmark::DoNotOptimize(g.MaskEmptyOrDeleted());
}
}
BENCHMARK(BM_Group_MaskEmptyOrDeleted);
void BM_Group_MaskNonFull(benchmark::State& state) {
std::array<ctrl_t, Group::kWidth> group;
Iota(group.begin(), group.end(), -4);
Group g{group.data()};
for (auto _ : state) {
::benchmark::DoNotOptimize(g);
::benchmark::DoNotOptimize(g.MaskNonFull());
}
}
BENCHMARK(BM_Group_MaskNonFull);
void BM_Group_CountLeadingEmptyOrDeleted(benchmark::State& state) {
std::array<ctrl_t, Group::kWidth> group;
Iota(group.begin(), group.end(), -2);
Group g{group.data()};
for (auto _ : state) {
::benchmark::DoNotOptimize(g);
::benchmark::DoNotOptimize(g.CountLeadingEmptyOrDeleted());
}
}
BENCHMARK(BM_Group_CountLeadingEmptyOrDeleted);
void BM_Group_MatchFirstEmptyOrDeleted(benchmark::State& state) {
std::array<ctrl_t, Group::kWidth> group;
Iota(group.begin(), group.end(), -2);
Group g{group.data()};
for (auto _ : state) {
::benchmark::DoNotOptimize(g);
::benchmark::DoNotOptimize(g.MaskEmptyOrDeleted().LowestBitSet());
}
}
BENCHMARK(BM_Group_MatchFirstEmptyOrDeleted);
void BM_Group_MatchFirstNonFull(benchmark::State& state) {
std::array<ctrl_t, Group::kWidth> group;
Iota(group.begin(), group.end(), -2);
Group g{group.data()};
for (auto _ : state) {
::benchmark::DoNotOptimize(g);
::benchmark::DoNotOptimize(g.MaskNonFull().LowestBitSet());
}
}
BENCHMARK(BM_Group_MatchFirstNonFull);
void BM_DropDeletes(benchmark::State& state) {
constexpr size_t capacity = (1 << 20) - 1;
std::vector<ctrl_t> ctrl(capacity + 1 + Group::kWidth);
ctrl[capacity] = ctrl_t::kSentinel;
std::vector<ctrl_t> pattern = {ctrl_t::kEmpty, static_cast<ctrl_t>(2),
ctrl_t::kDeleted, static_cast<ctrl_t>(2),
ctrl_t::kEmpty, static_cast<ctrl_t>(1),
ctrl_t::kDeleted};
for (size_t i = 0; i != capacity; ++i) {
ctrl[i] = pattern[i % pattern.size()];
}
while (state.KeepRunning()) {
state.PauseTiming();
std::vector<ctrl_t> ctrl_copy = ctrl;
state.ResumeTiming();
ConvertDeletedToEmptyAndFullToDeleted(ctrl_copy.data(), capacity);
::benchmark::DoNotOptimize(ctrl_copy[capacity]);
}
}
BENCHMARK(BM_DropDeletes);
void BM_Resize(benchmark::State& state) {
// For now just measure a small cheap hash table since we
// are mostly interested in the overhead of type-erasure
// in resize().
constexpr int kElements = 64;
const int kCapacity = kElements * 2;
IntTable table;
for (int i = 0; i < kElements; i++) {
table.insert(i);
}
for (auto unused : state) {
table.rehash(0);
table.rehash(kCapacity);
}
}
BENCHMARK(BM_Resize);
void BM_EraseIf(benchmark::State& state) {
int64_t num_elements = state.range(0);
size_t num_erased = static_cast<size_t>(state.range(1));
constexpr size_t kRepetitions = 64;
absl::BitGen rng;
std::vector<std::vector<int64_t>> keys(kRepetitions);
std::vector<IntTable> tables;
std::vector<int64_t> threshold;
for (auto& k : keys) {
tables.push_back(IntTable());
auto& table = tables.back();
for (int64_t i = 0; i < num_elements; i++) {
// We use random keys to reduce noise.
k.push_back(
absl::Uniform<int64_t>(rng, 0, std::numeric_limits<int64_t>::max()));
if (!table.insert(k.back()).second) {
k.pop_back();
--i; // duplicated value, retrying
}
}
std::sort(k.begin(), k.end());
threshold.push_back(static_cast<int64_t>(num_erased) < num_elements
? k[num_erased]
: std::numeric_limits<int64_t>::max());
}
while (state.KeepRunningBatch(static_cast<int64_t>(kRepetitions) *
std::max(num_elements, int64_t{1}))) {
benchmark::DoNotOptimize(tables);
for (size_t t_id = 0; t_id < kRepetitions; t_id++) {
auto& table = tables[t_id];
benchmark::DoNotOptimize(num_erased);
auto pred = [t = threshold[t_id]](int64_t key) { return key < t; };
benchmark::DoNotOptimize(pred);
benchmark::DoNotOptimize(table);
absl::container_internal::EraseIf(pred, &table);
}
state.PauseTiming();
for (size_t t_id = 0; t_id < kRepetitions; t_id++) {
auto& k = keys[t_id];
auto& table = tables[t_id];
for (size_t i = 0; i < num_erased; i++) {
table.insert(k[i]);
}
}
state.ResumeTiming();
}
}
BENCHMARK(BM_EraseIf)
->ArgNames({"num_elements", "num_erased"})
->ArgPair(10, 0)
->ArgPair(1000, 0)
->ArgPair(10, 5)
->ArgPair(1000, 500)
->ArgPair(10, 10)
->ArgPair(1000, 1000);
} // namespace
} // namespace container_internal
ABSL_NAMESPACE_END
} // namespace absl
// These methods are here to make it easy to examine the assembly for targeted
// parts of the API.
auto CodegenAbslRawHashSetInt64Find(absl::container_internal::IntTable* table,
int64_t key) -> decltype(table->find(key)) {
return table->find(key);
}
bool CodegenAbslRawHashSetInt64FindNeEnd(
absl::container_internal::IntTable* table, int64_t key) {
return table->find(key) != table->end();
}
// This is useful because the find isn't inlined but the iterator comparison is.
bool CodegenAbslRawHashSetStringFindNeEnd(
absl::container_internal::StringTable* table, const std::string& key) {
return table->find(key) != table->end();
}
auto CodegenAbslRawHashSetInt64Insert(absl::container_internal::IntTable* table,
int64_t key)
-> decltype(table->insert(key)) {
return table->insert(key);
}
bool CodegenAbslRawHashSetInt64Contains(
absl::container_internal::IntTable* table, int64_t key) {
return table->contains(key);
}
void CodegenAbslRawHashSetInt64Iterate(
absl::container_internal::IntTable* table) {
for (auto x : *table) benchmark::DoNotOptimize(x);
}
int odr =
(::benchmark::DoNotOptimize(std::make_tuple(
&CodegenAbslRawHashSetInt64Find, &CodegenAbslRawHashSetInt64FindNeEnd,
&CodegenAbslRawHashSetStringFindNeEnd,
&CodegenAbslRawHashSetInt64Insert, &CodegenAbslRawHashSetInt64Contains,
&CodegenAbslRawHashSetInt64Iterate)),
1);

View file

@ -0,0 +1,597 @@
// Copyright 2018 The Abseil Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// Generates probe length statistics for many combinations of key types and key
// distributions, all using the default hash function for swisstable.
#include <memory>
#include <regex> // NOLINT
#include <vector>
#include "absl/base/no_destructor.h"
#include "absl/container/flat_hash_map.h"
#include "absl/container/internal/hash_function_defaults.h"
#include "absl/container/internal/hashtable_debug.h"
#include "absl/container/internal/raw_hash_set.h"
#include "absl/random/distributions.h"
#include "absl/random/random.h"
#include "absl/strings/str_cat.h"
#include "absl/strings/str_format.h"
#include "absl/strings/string_view.h"
#include "absl/strings/strip.h"
#include "absl/types/optional.h"
namespace {
enum class OutputStyle { kRegular, kBenchmark };
// The --benchmark command line flag.
// This is populated from main().
// When run in "benchmark" mode, we have different output. This allows
// A/B comparisons with tools like `benchy`.
absl::string_view benchmarks;
OutputStyle output() {
return !benchmarks.empty() ? OutputStyle::kBenchmark : OutputStyle::kRegular;
}
template <class T>
struct Policy {
using slot_type = T;
using key_type = T;
using init_type = T;
template <class allocator_type, class Arg>
static void construct(allocator_type* alloc, slot_type* slot,
const Arg& arg) {
std::allocator_traits<allocator_type>::construct(*alloc, slot, arg);
}
template <class allocator_type>
static void destroy(allocator_type* alloc, slot_type* slot) {
std::allocator_traits<allocator_type>::destroy(*alloc, slot);
}
static slot_type& element(slot_type* slot) { return *slot; }
template <class F, class... Args>
static auto apply(F&& f, const slot_type& arg)
-> decltype(std::forward<F>(f)(arg, arg)) {
return std::forward<F>(f)(arg, arg);
}
template <class Hash>
static constexpr auto get_hash_slot_fn() {
return nullptr;
}
};
absl::BitGen& GlobalBitGen() {
static absl::NoDestructor<absl::BitGen> value;
return *value;
}
// Keeps a pool of allocations and randomly gives one out.
// This introduces more randomization to the addresses given to swisstable and
// should help smooth out this factor from probe length calculation.
template <class T>
class RandomizedAllocator {
public:
using value_type = T;
RandomizedAllocator() = default;
template <typename U>
RandomizedAllocator(RandomizedAllocator<U>) {} // NOLINT
static T* allocate(size_t n) {
auto& pointers = GetPointers(n);
// Fill the pool
while (pointers.size() < kRandomPool) {
pointers.push_back(std::allocator<T>{}.allocate(n));
}
// Choose a random one.
size_t i = absl::Uniform<size_t>(GlobalBitGen(), 0, pointers.size());
T* result = pointers[i];
pointers[i] = pointers.back();
pointers.pop_back();
return result;
}
static void deallocate(T* p, size_t n) {
// Just put it back on the pool. No need to release the memory.
GetPointers(n).push_back(p);
}
private:
// We keep at least kRandomPool allocations for each size.
static constexpr size_t kRandomPool = 20;
static std::vector<T*>& GetPointers(size_t n) {
static absl::NoDestructor<absl::flat_hash_map<size_t, std::vector<T*>>> m;
return (*m)[n];
}
};
template <class T>
struct DefaultHash {
using type = absl::container_internal::hash_default_hash<T>;
};
template <class T>
using DefaultHashT = typename DefaultHash<T>::type;
template <class T>
struct Table : absl::container_internal::raw_hash_set<
Policy<T>, DefaultHashT<T>,
absl::container_internal::hash_default_eq<T>,
RandomizedAllocator<T>> {};
struct LoadSizes {
size_t min_load;
size_t max_load;
};
LoadSizes GetMinMaxLoadSizes() {
static const auto sizes = [] {
Table<int> t;
// First, fill enough to have a good distribution.
constexpr size_t kMinSize = 10000;
while (t.size() < kMinSize) t.insert(t.size());
const auto reach_min_load_factor = [&] {
const double lf = t.load_factor();
while (lf <= t.load_factor()) t.insert(t.size());
};
// Then, insert until we reach min load factor.
reach_min_load_factor();
const size_t min_load_size = t.size();
// Keep going until we hit min load factor again, then go back one.
t.insert(t.size());
reach_min_load_factor();
return LoadSizes{min_load_size, t.size() - 1};
}();
return sizes;
}
struct Ratios {
double min_load;
double avg_load;
double max_load;
};
// See absl/container/internal/hashtable_debug.h for details on
// probe length calculation.
template <class ElemFn>
Ratios CollectMeanProbeLengths() {
const auto min_max_sizes = GetMinMaxLoadSizes();
ElemFn elem;
using Key = decltype(elem());
Table<Key> t;
Ratios result;
while (t.size() < min_max_sizes.min_load) t.insert(elem());
result.min_load =
absl::container_internal::GetHashtableDebugProbeSummary(t).mean;
while (t.size() < (min_max_sizes.min_load + min_max_sizes.max_load) / 2)
t.insert(elem());
result.avg_load =
absl::container_internal::GetHashtableDebugProbeSummary(t).mean;
while (t.size() < min_max_sizes.max_load) t.insert(elem());
result.max_load =
absl::container_internal::GetHashtableDebugProbeSummary(t).mean;
return result;
}
template <int Align>
uintptr_t PointerForAlignment() {
alignas(Align) static constexpr uintptr_t kInitPointer = 0;
return reinterpret_cast<uintptr_t>(&kInitPointer);
}
// This incomplete type is used for testing hash of pointers of different
// alignments.
// NOTE: We are generating invalid pointer values on the fly with
// reinterpret_cast. There are not "safely derived" pointers so using them is
// technically UB. It is unlikely to be a problem, though.
template <int Align>
struct Ptr;
template <int Align>
Ptr<Align>* MakePtr(uintptr_t v) {
if (sizeof(v) == 8) {
constexpr int kCopyBits = 16;
// Ensure high bits are all the same.
v = static_cast<uintptr_t>(static_cast<intptr_t>(v << kCopyBits) >>
kCopyBits);
}
return reinterpret_cast<Ptr<Align>*>(v);
}
struct IntIdentity {
uint64_t i;
friend bool operator==(IntIdentity a, IntIdentity b) { return a.i == b.i; }
IntIdentity operator++(int) { return IntIdentity{i++}; }
};
template <int Align>
struct PtrIdentity {
explicit PtrIdentity(uintptr_t val = PointerForAlignment<Align>()) : i(val) {}
uintptr_t i;
friend bool operator==(PtrIdentity a, PtrIdentity b) { return a.i == b.i; }
PtrIdentity operator++(int) {
PtrIdentity p(i);
i += Align;
return p;
}
};
constexpr char kStringFormat[] = "/path/to/file/name-%07d-of-9999999.txt";
template <bool small>
struct String {
std::string value;
static std::string Make(uint32_t v) {
return {small ? absl::StrCat(v) : absl::StrFormat(kStringFormat, v)};
}
};
template <>
struct DefaultHash<IntIdentity> {
struct type {
size_t operator()(IntIdentity t) const { return t.i; }
};
};
template <int Align>
struct DefaultHash<PtrIdentity<Align>> {
struct type {
size_t operator()(PtrIdentity<Align> t) const { return t.i; }
};
};
template <class T>
struct Sequential {
T operator()() const { return current++; }
mutable T current{};
};
template <int Align>
struct Sequential<Ptr<Align>*> {
Ptr<Align>* operator()() const {
auto* result = MakePtr<Align>(current);
current += Align;
return result;
}
mutable uintptr_t current = PointerForAlignment<Align>();
};
template <bool small>
struct Sequential<String<small>> {
std::string operator()() const { return String<small>::Make(current++); }
mutable uint32_t current = 0;
};
template <class T, class U>
struct Sequential<std::pair<T, U>> {
mutable Sequential<T> tseq;
mutable Sequential<U> useq;
using RealT = decltype(tseq());
using RealU = decltype(useq());
mutable std::vector<RealT> ts;
mutable std::vector<RealU> us;
mutable size_t ti = 0, ui = 0;
std::pair<RealT, RealU> operator()() const {
std::pair<RealT, RealU> value{get_t(), get_u()};
if (ti == 0) {
ti = ui + 1;
ui = 0;
} else {
--ti;
++ui;
}
return value;
}
RealT get_t() const {
while (ti >= ts.size()) ts.push_back(tseq());
return ts[ti];
}
RealU get_u() const {
while (ui >= us.size()) us.push_back(useq());
return us[ui];
}
};
template <class T, int percent_skip>
struct AlmostSequential {
mutable Sequential<T> current;
auto operator()() const -> decltype(current()) {
while (absl::Uniform(GlobalBitGen(), 0.0, 1.0) <= percent_skip / 100.)
current();
return current();
}
};
struct Uniform {
template <typename T>
T operator()(T) const {
return absl::Uniform<T>(absl::IntervalClosed, GlobalBitGen(), T{0}, ~T{0});
}
};
struct Gaussian {
template <typename T>
T operator()(T) const {
double d;
do {
d = absl::Gaussian<double>(GlobalBitGen(), 1e6, 1e4);
} while (d <= 0 || d > std::numeric_limits<T>::max() / 2);
return static_cast<T>(d);
}
};
struct Zipf {
template <typename T>
T operator()(T) const {
return absl::Zipf<T>(GlobalBitGen(), std::numeric_limits<T>::max(), 1.6);
}
};
template <class T, class Dist>
struct Random {
T operator()() const { return Dist{}(T{}); }
};
template <class Dist, int Align>
struct Random<Ptr<Align>*, Dist> {
Ptr<Align>* operator()() const {
return MakePtr<Align>(Random<uintptr_t, Dist>{}() * Align);
}
};
template <class Dist>
struct Random<IntIdentity, Dist> {
IntIdentity operator()() const {
return IntIdentity{Random<uint64_t, Dist>{}()};
}
};
template <class Dist, int Align>
struct Random<PtrIdentity<Align>, Dist> {
PtrIdentity<Align> operator()() const {
return PtrIdentity<Align>{Random<uintptr_t, Dist>{}() * Align};
}
};
template <class Dist, bool small>
struct Random<String<small>, Dist> {
std::string operator()() const {
return String<small>::Make(Random<uint32_t, Dist>{}());
}
};
template <class T, class U, class Dist>
struct Random<std::pair<T, U>, Dist> {
auto operator()() const
-> decltype(std::make_pair(Random<T, Dist>{}(), Random<U, Dist>{}())) {
return std::make_pair(Random<T, Dist>{}(), Random<U, Dist>{}());
}
};
template <typename>
std::string Name();
std::string Name(uint32_t*) { return "u32"; }
std::string Name(uint64_t*) { return "u64"; }
std::string Name(IntIdentity*) { return "IntIdentity"; }
template <int Align>
std::string Name(Ptr<Align>**) {
return absl::StrCat("Ptr", Align);
}
template <int Align>
std::string Name(PtrIdentity<Align>*) {
return absl::StrCat("PtrIdentity", Align);
}
template <bool small>
std::string Name(String<small>*) {
return small ? "StrS" : "StrL";
}
template <class T, class U>
std::string Name(std::pair<T, U>*) {
if (output() == OutputStyle::kBenchmark)
return absl::StrCat("P_", Name<T>(), "_", Name<U>());
return absl::StrCat("P<", Name<T>(), ",", Name<U>(), ">");
}
template <class T>
std::string Name(Sequential<T>*) {
return "Sequential";
}
template <class T, int P>
std::string Name(AlmostSequential<T, P>*) {
return absl::StrCat("AlmostSeq_", P);
}
template <class T>
std::string Name(Random<T, Uniform>*) {
return "UnifRand";
}
template <class T>
std::string Name(Random<T, Gaussian>*) {
return "GausRand";
}
template <class T>
std::string Name(Random<T, Zipf>*) {
return "ZipfRand";
}
template <typename T>
std::string Name() {
return Name(static_cast<T*>(nullptr));
}
constexpr int kNameWidth = 15;
constexpr int kDistWidth = 16;
bool CanRunBenchmark(absl::string_view name) {
static const absl::NoDestructor<absl::optional<std::regex>> filter([] {
return benchmarks.empty() || benchmarks == "all"
? absl::nullopt
: absl::make_optional(std::regex(std::string(benchmarks)));
}());
return !filter->has_value() || std::regex_search(std::string(name), **filter);
}
struct Result {
std::string name;
std::string dist_name;
Ratios ratios;
};
template <typename T, typename Dist>
void RunForTypeAndDistribution(std::vector<Result>& results) {
std::string name = absl::StrCat(Name<T>(), "/", Name<Dist>());
// We have to check against all three names (min/avg/max) before we run it.
// If any of them is enabled, we run it.
if (!CanRunBenchmark(absl::StrCat(name, "/min")) &&
!CanRunBenchmark(absl::StrCat(name, "/avg")) &&
!CanRunBenchmark(absl::StrCat(name, "/max"))) {
return;
}
results.push_back({Name<T>(), Name<Dist>(), CollectMeanProbeLengths<Dist>()});
}
template <class T>
void RunForType(std::vector<Result>& results) {
RunForTypeAndDistribution<T, Sequential<T>>(results);
RunForTypeAndDistribution<T, AlmostSequential<T, 20>>(results);
RunForTypeAndDistribution<T, AlmostSequential<T, 50>>(results);
RunForTypeAndDistribution<T, Random<T, Uniform>>(results);
#ifdef NDEBUG
// Disable these in non-opt mode because they take too long.
RunForTypeAndDistribution<T, Random<T, Gaussian>>(results);
RunForTypeAndDistribution<T, Random<T, Zipf>>(results);
#endif // NDEBUG
}
} // namespace
int main(int argc, char** argv) {
// Parse the benchmark flags. Ignore all of them except the regex pattern.
for (int i = 1; i < argc; ++i) {
absl::string_view arg = argv[i];
const auto next = [&] { return argv[std::min(i + 1, argc - 1)]; };
if (absl::ConsumePrefix(&arg, "--benchmark_filter")) {
if (arg == "") {
// --benchmark_filter X
benchmarks = next();
} else if (absl::ConsumePrefix(&arg, "=")) {
// --benchmark_filter=X
benchmarks = arg;
}
}
// Any --benchmark flag turns on the mode.
if (absl::ConsumePrefix(&arg, "--benchmark")) {
if (benchmarks.empty()) benchmarks="all";
}
}
std::vector<Result> results;
RunForType<uint64_t>(results);
RunForType<IntIdentity>(results);
RunForType<Ptr<8>*>(results);
RunForType<Ptr<16>*>(results);
RunForType<Ptr<32>*>(results);
RunForType<Ptr<64>*>(results);
RunForType<PtrIdentity<8>>(results);
RunForType<PtrIdentity<16>>(results);
RunForType<PtrIdentity<32>>(results);
RunForType<PtrIdentity<64>>(results);
RunForType<std::pair<uint32_t, uint32_t>>(results);
RunForType<String<true>>(results);
RunForType<String<false>>(results);
RunForType<std::pair<uint64_t, String<true>>>(results);
RunForType<std::pair<String<true>, uint64_t>>(results);
RunForType<std::pair<uint64_t, String<false>>>(results);
RunForType<std::pair<String<false>, uint64_t>>(results);
switch (output()) {
case OutputStyle::kRegular:
absl::PrintF("%-*s%-*s Min Avg Max\n%s\n", kNameWidth,
"Type", kDistWidth, "Distribution",
std::string(kNameWidth + kDistWidth + 10 * 3, '-'));
for (const auto& result : results) {
absl::PrintF("%-*s%-*s %8.4f %8.4f %8.4f\n", kNameWidth, result.name,
kDistWidth, result.dist_name, result.ratios.min_load,
result.ratios.avg_load, result.ratios.max_load);
}
break;
case OutputStyle::kBenchmark: {
absl::PrintF("{\n");
absl::PrintF(" \"benchmarks\": [\n");
absl::string_view comma;
for (const auto& result : results) {
auto print = [&](absl::string_view stat, double Ratios::*val) {
std::string name =
absl::StrCat(result.name, "/", result.dist_name, "/", stat);
// Check the regex again. We might had have enabled only one of the
// stats for the benchmark.
if (!CanRunBenchmark(name)) return;
absl::PrintF(" %s{\n", comma);
absl::PrintF(" \"cpu_time\": %f,\n", 1e9 * result.ratios.*val);
absl::PrintF(" \"real_time\": %f,\n", 1e9 * result.ratios.*val);
absl::PrintF(" \"iterations\": 1,\n");
absl::PrintF(" \"name\": \"%s\",\n", name);
absl::PrintF(" \"time_unit\": \"ns\"\n");
absl::PrintF(" }\n");
comma = ",";
};
print("min", &Ratios::min_load);
print("avg", &Ratios::avg_load);
print("max", &Ratios::max_load);
}
absl::PrintF(" ],\n");
absl::PrintF(" \"context\": {\n");
absl::PrintF(" }\n");
absl::PrintF("}\n");
break;
}
}
return 0;
}

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,387 @@
// Copyright 2018 The Abseil Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef ABSL_CONTAINER_INTERNAL_TEST_ALLOCATOR_H_
#define ABSL_CONTAINER_INTERNAL_TEST_ALLOCATOR_H_
#include <cassert>
#include <cstddef>
#include <cstdint>
#include <memory>
#include <type_traits>
#include "gtest/gtest.h"
#include "absl/base/config.h"
namespace absl {
ABSL_NAMESPACE_BEGIN
namespace container_internal {
// This is a stateful allocator, but the state lives outside of the
// allocator (in whatever test is using the allocator). This is odd
// but helps in tests where the allocator is propagated into nested
// containers - that chain of allocators uses the same state and is
// thus easier to query for aggregate allocation information.
template <typename T>
class CountingAllocator {
public:
using Allocator = std::allocator<T>;
using AllocatorTraits = std::allocator_traits<Allocator>;
using value_type = typename AllocatorTraits::value_type;
using pointer = typename AllocatorTraits::pointer;
using const_pointer = typename AllocatorTraits::const_pointer;
using size_type = typename AllocatorTraits::size_type;
using difference_type = typename AllocatorTraits::difference_type;
CountingAllocator() = default;
explicit CountingAllocator(int64_t* bytes_used) : bytes_used_(bytes_used) {}
CountingAllocator(int64_t* bytes_used, int64_t* instance_count)
: bytes_used_(bytes_used), instance_count_(instance_count) {}
template <typename U>
CountingAllocator(const CountingAllocator<U>& x)
: bytes_used_(x.bytes_used_), instance_count_(x.instance_count_) {}
pointer allocate(
size_type n,
typename AllocatorTraits::const_void_pointer hint = nullptr) {
Allocator allocator;
pointer ptr = AllocatorTraits::allocate(allocator, n, hint);
if (bytes_used_ != nullptr) {
*bytes_used_ += n * sizeof(T);
}
return ptr;
}
void deallocate(pointer p, size_type n) {
Allocator allocator;
AllocatorTraits::deallocate(allocator, p, n);
if (bytes_used_ != nullptr) {
*bytes_used_ -= n * sizeof(T);
}
}
template <typename U, typename... Args>
void construct(U* p, Args&&... args) {
Allocator allocator;
AllocatorTraits::construct(allocator, p, std::forward<Args>(args)...);
if (instance_count_ != nullptr) {
*instance_count_ += 1;
}
}
template <typename U>
void destroy(U* p) {
Allocator allocator;
// Ignore GCC warning bug.
#if ABSL_INTERNAL_HAVE_MIN_GNUC_VERSION(12, 0)
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wuse-after-free"
#endif
AllocatorTraits::destroy(allocator, p);
#if ABSL_INTERNAL_HAVE_MIN_GNUC_VERSION(12, 0)
#pragma GCC diagnostic pop
#endif
if (instance_count_ != nullptr) {
*instance_count_ -= 1;
}
}
template <typename U>
class rebind {
public:
using other = CountingAllocator<U>;
};
friend bool operator==(const CountingAllocator& a,
const CountingAllocator& b) {
return a.bytes_used_ == b.bytes_used_ &&
a.instance_count_ == b.instance_count_;
}
friend bool operator!=(const CountingAllocator& a,
const CountingAllocator& b) {
return !(a == b);
}
int64_t* bytes_used_ = nullptr;
int64_t* instance_count_ = nullptr;
};
template <typename T>
struct CopyAssignPropagatingCountingAlloc : public CountingAllocator<T> {
using propagate_on_container_copy_assignment = std::true_type;
using Base = CountingAllocator<T>;
using Base::Base;
template <typename U>
explicit CopyAssignPropagatingCountingAlloc(
const CopyAssignPropagatingCountingAlloc<U>& other)
: Base(other.bytes_used_, other.instance_count_) {}
template <typename U>
struct rebind {
using other = CopyAssignPropagatingCountingAlloc<U>;
};
};
template <typename T>
struct MoveAssignPropagatingCountingAlloc : public CountingAllocator<T> {
using propagate_on_container_move_assignment = std::true_type;
using Base = CountingAllocator<T>;
using Base::Base;
template <typename U>
explicit MoveAssignPropagatingCountingAlloc(
const MoveAssignPropagatingCountingAlloc<U>& other)
: Base(other.bytes_used_, other.instance_count_) {}
template <typename U>
struct rebind {
using other = MoveAssignPropagatingCountingAlloc<U>;
};
};
template <typename T>
struct SwapPropagatingCountingAlloc : public CountingAllocator<T> {
using propagate_on_container_swap = std::true_type;
using Base = CountingAllocator<T>;
using Base::Base;
template <typename U>
explicit SwapPropagatingCountingAlloc(
const SwapPropagatingCountingAlloc<U>& other)
: Base(other.bytes_used_, other.instance_count_) {}
template <typename U>
struct rebind {
using other = SwapPropagatingCountingAlloc<U>;
};
};
// Tries to allocate memory at the minimum alignment even when the default
// allocator uses a higher alignment.
template <typename T>
struct MinimumAlignmentAlloc : std::allocator<T> {
MinimumAlignmentAlloc() = default;
template <typename U>
explicit MinimumAlignmentAlloc(const MinimumAlignmentAlloc<U>& /*other*/) {}
template <class U>
struct rebind {
using other = MinimumAlignmentAlloc<U>;
};
T* allocate(size_t n) {
T* ptr = std::allocator<T>::allocate(n + 1);
char* cptr = reinterpret_cast<char*>(ptr);
cptr += alignof(T);
return reinterpret_cast<T*>(cptr);
}
void deallocate(T* ptr, size_t n) {
char* cptr = reinterpret_cast<char*>(ptr);
cptr -= alignof(T);
std::allocator<T>::deallocate(reinterpret_cast<T*>(cptr), n + 1);
}
};
inline bool IsAssertEnabled() {
// Use an assert with side-effects to figure out if they are actually enabled.
bool assert_enabled = false;
assert([&]() { // NOLINT
assert_enabled = true;
return true;
}());
return assert_enabled;
}
template <template <class Alloc> class Container>
void TestCopyAssignAllocPropagation() {
int64_t bytes1 = 0, instances1 = 0, bytes2 = 0, instances2 = 0;
CopyAssignPropagatingCountingAlloc<int> allocator1(&bytes1, &instances1);
CopyAssignPropagatingCountingAlloc<int> allocator2(&bytes2, &instances2);
// Test propagating allocator_type.
{
Container<CopyAssignPropagatingCountingAlloc<int>> c1(allocator1);
Container<CopyAssignPropagatingCountingAlloc<int>> c2(allocator2);
for (int i = 0; i < 100; ++i) c1.insert(i);
EXPECT_NE(c2.get_allocator(), allocator1);
EXPECT_EQ(instances1, 100);
EXPECT_EQ(instances2, 0);
c2 = c1;
EXPECT_EQ(c2.get_allocator(), allocator1);
EXPECT_EQ(instances1, 200);
EXPECT_EQ(instances2, 0);
}
// Test non-propagating allocator_type with different allocators.
{
Container<CountingAllocator<int>> c1(allocator1), c2(allocator2);
for (int i = 0; i < 100; ++i) c1.insert(i);
EXPECT_EQ(c2.get_allocator(), allocator2);
EXPECT_EQ(instances1, 100);
EXPECT_EQ(instances2, 0);
c2 = c1;
EXPECT_EQ(c2.get_allocator(), allocator2);
EXPECT_EQ(instances1, 100);
EXPECT_EQ(instances2, 100);
}
EXPECT_EQ(bytes1, 0);
EXPECT_EQ(instances1, 0);
EXPECT_EQ(bytes2, 0);
EXPECT_EQ(instances2, 0);
}
template <template <class Alloc> class Container>
void TestMoveAssignAllocPropagation() {
int64_t bytes1 = 0, instances1 = 0, bytes2 = 0, instances2 = 0;
MoveAssignPropagatingCountingAlloc<int> allocator1(&bytes1, &instances1);
MoveAssignPropagatingCountingAlloc<int> allocator2(&bytes2, &instances2);
// Test propagating allocator_type.
{
Container<MoveAssignPropagatingCountingAlloc<int>> c1(allocator1);
Container<MoveAssignPropagatingCountingAlloc<int>> c2(allocator2);
for (int i = 0; i < 100; ++i) c1.insert(i);
EXPECT_NE(c2.get_allocator(), allocator1);
EXPECT_EQ(instances1, 100);
EXPECT_EQ(instances2, 0);
c2 = std::move(c1);
EXPECT_EQ(c2.get_allocator(), allocator1);
EXPECT_EQ(instances1, 100);
EXPECT_EQ(instances2, 0);
}
// Test non-propagating allocator_type with equal allocators.
{
Container<CountingAllocator<int>> c1(allocator1), c2(allocator1);
for (int i = 0; i < 100; ++i) c1.insert(i);
EXPECT_EQ(c2.get_allocator(), allocator1);
EXPECT_EQ(instances1, 100);
EXPECT_EQ(instances2, 0);
c2 = std::move(c1);
EXPECT_EQ(c2.get_allocator(), allocator1);
EXPECT_EQ(instances1, 100);
EXPECT_EQ(instances2, 0);
}
// Test non-propagating allocator_type with different allocators.
{
Container<CountingAllocator<int>> c1(allocator1), c2(allocator2);
for (int i = 0; i < 100; ++i) c1.insert(i);
EXPECT_NE(c2.get_allocator(), allocator1);
EXPECT_EQ(instances1, 100);
EXPECT_EQ(instances2, 0);
c2 = std::move(c1);
EXPECT_EQ(c2.get_allocator(), allocator2);
EXPECT_LE(instances1, 100); // The values in c1 may or may not have been
// destroyed at this point.
EXPECT_EQ(instances2, 100);
}
EXPECT_EQ(bytes1, 0);
EXPECT_EQ(instances1, 0);
EXPECT_EQ(bytes2, 0);
EXPECT_EQ(instances2, 0);
}
template <template <class Alloc> class Container>
void TestSwapAllocPropagation() {
int64_t bytes1 = 0, instances1 = 0, bytes2 = 0, instances2 = 0;
SwapPropagatingCountingAlloc<int> allocator1(&bytes1, &instances1);
SwapPropagatingCountingAlloc<int> allocator2(&bytes2, &instances2);
// Test propagating allocator_type.
{
Container<SwapPropagatingCountingAlloc<int>> c1(allocator1), c2(allocator2);
for (int i = 0; i < 100; ++i) c1.insert(i);
EXPECT_NE(c2.get_allocator(), allocator1);
EXPECT_EQ(instances1, 100);
EXPECT_EQ(instances2, 0);
c2.swap(c1);
EXPECT_EQ(c2.get_allocator(), allocator1);
EXPECT_EQ(instances1, 100);
EXPECT_EQ(instances2, 0);
}
// Test non-propagating allocator_type with equal allocators.
{
Container<CountingAllocator<int>> c1(allocator1), c2(allocator1);
for (int i = 0; i < 100; ++i) c1.insert(i);
EXPECT_EQ(c2.get_allocator(), allocator1);
EXPECT_EQ(instances1, 100);
EXPECT_EQ(instances2, 0);
c2.swap(c1);
EXPECT_EQ(c2.get_allocator(), allocator1);
EXPECT_EQ(instances1, 100);
EXPECT_EQ(instances2, 0);
}
// Test non-propagating allocator_type with different allocators.
{
Container<CountingAllocator<int>> c1(allocator1), c2(allocator2);
for (int i = 0; i < 100; ++i) c1.insert(i);
EXPECT_NE(c1.get_allocator(), c2.get_allocator());
if (IsAssertEnabled()) {
EXPECT_DEATH_IF_SUPPORTED(c2.swap(c1), "");
}
}
EXPECT_EQ(bytes1, 0);
EXPECT_EQ(instances1, 0);
EXPECT_EQ(bytes2, 0);
EXPECT_EQ(instances2, 0);
}
template <template <class Alloc> class Container>
void TestAllocPropagation() {
TestCopyAssignAllocPropagation<Container>();
TestMoveAssignAllocPropagation<Container>();
TestSwapAllocPropagation<Container>();
}
} // namespace container_internal
ABSL_NAMESPACE_END
} // namespace absl
#endif // ABSL_CONTAINER_INTERNAL_TEST_ALLOCATOR_H_

View file

@ -0,0 +1,29 @@
// Copyright 2017 The Abseil Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "absl/container/internal/test_instance_tracker.h"
namespace absl {
ABSL_NAMESPACE_BEGIN
namespace test_internal {
int BaseCountedInstance::num_instances_ = 0;
int BaseCountedInstance::num_live_instances_ = 0;
int BaseCountedInstance::num_moves_ = 0;
int BaseCountedInstance::num_copies_ = 0;
int BaseCountedInstance::num_swaps_ = 0;
int BaseCountedInstance::num_comparisons_ = 0;
} // namespace test_internal
ABSL_NAMESPACE_END
} // namespace absl

View file

@ -0,0 +1,274 @@
// Copyright 2017 The Abseil Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef ABSL_CONTAINER_INTERNAL_TEST_INSTANCE_TRACKER_H_
#define ABSL_CONTAINER_INTERNAL_TEST_INSTANCE_TRACKER_H_
#include <cstdlib>
#include <ostream>
#include "absl/types/compare.h"
namespace absl {
ABSL_NAMESPACE_BEGIN
namespace test_internal {
// A type that counts number of occurrences of the type, the live occurrences of
// the type, as well as the number of copies, moves, swaps, and comparisons that
// have occurred on the type. This is used as a base class for the copyable,
// copyable+movable, and movable types below that are used in actual tests. Use
// InstanceTracker in tests to track the number of instances.
class BaseCountedInstance {
public:
explicit BaseCountedInstance(int x) : value_(x) {
++num_instances_;
++num_live_instances_;
}
BaseCountedInstance(const BaseCountedInstance& x)
: value_(x.value_), is_live_(x.is_live_) {
++num_instances_;
if (is_live_) ++num_live_instances_;
++num_copies_;
}
BaseCountedInstance(BaseCountedInstance&& x)
: value_(x.value_), is_live_(x.is_live_) {
x.is_live_ = false;
++num_instances_;
++num_moves_;
}
~BaseCountedInstance() {
--num_instances_;
if (is_live_) --num_live_instances_;
}
BaseCountedInstance& operator=(const BaseCountedInstance& x) {
value_ = x.value_;
if (is_live_) --num_live_instances_;
is_live_ = x.is_live_;
if (is_live_) ++num_live_instances_;
++num_copies_;
return *this;
}
BaseCountedInstance& operator=(BaseCountedInstance&& x) {
value_ = x.value_;
if (is_live_) --num_live_instances_;
is_live_ = x.is_live_;
x.is_live_ = false;
++num_moves_;
return *this;
}
bool operator==(const BaseCountedInstance& x) const {
++num_comparisons_;
return value_ == x.value_;
}
bool operator!=(const BaseCountedInstance& x) const {
++num_comparisons_;
return value_ != x.value_;
}
bool operator<(const BaseCountedInstance& x) const {
++num_comparisons_;
return value_ < x.value_;
}
bool operator>(const BaseCountedInstance& x) const {
++num_comparisons_;
return value_ > x.value_;
}
bool operator<=(const BaseCountedInstance& x) const {
++num_comparisons_;
return value_ <= x.value_;
}
bool operator>=(const BaseCountedInstance& x) const {
++num_comparisons_;
return value_ >= x.value_;
}
absl::weak_ordering compare(const BaseCountedInstance& x) const {
++num_comparisons_;
return value_ < x.value_
? absl::weak_ordering::less
: value_ == x.value_ ? absl::weak_ordering::equivalent
: absl::weak_ordering::greater;
}
int value() const {
if (!is_live_) std::abort();
return value_;
}
friend std::ostream& operator<<(std::ostream& o,
const BaseCountedInstance& v) {
return o << "[value:" << v.value() << "]";
}
// Implementation of efficient swap() that counts swaps.
static void SwapImpl(
BaseCountedInstance& lhs, // NOLINT(runtime/references)
BaseCountedInstance& rhs) { // NOLINT(runtime/references)
using std::swap;
swap(lhs.value_, rhs.value_);
swap(lhs.is_live_, rhs.is_live_);
++BaseCountedInstance::num_swaps_;
}
private:
friend class InstanceTracker;
int value_;
// Indicates if the value is live, ie it hasn't been moved away from.
bool is_live_ = true;
// Number of instances.
static int num_instances_;
// Number of live instances (those that have not been moved away from.)
static int num_live_instances_;
// Number of times that BaseCountedInstance objects were moved.
static int num_moves_;
// Number of times that BaseCountedInstance objects were copied.
static int num_copies_;
// Number of times that BaseCountedInstance objects were swapped.
static int num_swaps_;
// Number of times that BaseCountedInstance objects were compared.
static int num_comparisons_;
};
// Helper to track the BaseCountedInstance instance counters. Expects that the
// number of instances and live_instances are the same when it is constructed
// and when it is destructed.
class InstanceTracker {
public:
InstanceTracker()
: start_instances_(BaseCountedInstance::num_instances_),
start_live_instances_(BaseCountedInstance::num_live_instances_) {
ResetCopiesMovesSwaps();
}
~InstanceTracker() {
if (instances() != 0) std::abort();
if (live_instances() != 0) std::abort();
}
// Returns the number of BaseCountedInstance instances both containing valid
// values and those moved away from compared to when the InstanceTracker was
// constructed
int instances() const {
return BaseCountedInstance::num_instances_ - start_instances_;
}
// Returns the number of live BaseCountedInstance instances compared to when
// the InstanceTracker was constructed
int live_instances() const {
return BaseCountedInstance::num_live_instances_ - start_live_instances_;
}
// Returns the number of moves on BaseCountedInstance objects since
// construction or since the last call to ResetCopiesMovesSwaps().
int moves() const { return BaseCountedInstance::num_moves_ - start_moves_; }
// Returns the number of copies on BaseCountedInstance objects since
// construction or the last call to ResetCopiesMovesSwaps().
int copies() const {
return BaseCountedInstance::num_copies_ - start_copies_;
}
// Returns the number of swaps on BaseCountedInstance objects since
// construction or the last call to ResetCopiesMovesSwaps().
int swaps() const { return BaseCountedInstance::num_swaps_ - start_swaps_; }
// Returns the number of comparisons on BaseCountedInstance objects since
// construction or the last call to ResetCopiesMovesSwaps().
int comparisons() const {
return BaseCountedInstance::num_comparisons_ - start_comparisons_;
}
// Resets the base values for moves, copies, comparisons, and swaps to the
// current values, so that subsequent Get*() calls for moves, copies,
// comparisons, and swaps will compare to the situation at the point of this
// call.
void ResetCopiesMovesSwaps() {
start_moves_ = BaseCountedInstance::num_moves_;
start_copies_ = BaseCountedInstance::num_copies_;
start_swaps_ = BaseCountedInstance::num_swaps_;
start_comparisons_ = BaseCountedInstance::num_comparisons_;
}
private:
int start_instances_;
int start_live_instances_;
int start_moves_;
int start_copies_;
int start_swaps_;
int start_comparisons_;
};
// Copyable, not movable.
class CopyableOnlyInstance : public BaseCountedInstance {
public:
explicit CopyableOnlyInstance(int x) : BaseCountedInstance(x) {}
CopyableOnlyInstance(const CopyableOnlyInstance& rhs) = default;
CopyableOnlyInstance& operator=(const CopyableOnlyInstance& rhs) = default;
friend void swap(CopyableOnlyInstance& lhs, CopyableOnlyInstance& rhs) {
BaseCountedInstance::SwapImpl(lhs, rhs);
}
static bool supports_move() { return false; }
};
// Copyable and movable.
class CopyableMovableInstance : public BaseCountedInstance {
public:
explicit CopyableMovableInstance(int x) : BaseCountedInstance(x) {}
CopyableMovableInstance(const CopyableMovableInstance& rhs) = default;
CopyableMovableInstance(CopyableMovableInstance&& rhs) = default;
CopyableMovableInstance& operator=(const CopyableMovableInstance& rhs) =
default;
CopyableMovableInstance& operator=(CopyableMovableInstance&& rhs) = default;
friend void swap(CopyableMovableInstance& lhs, CopyableMovableInstance& rhs) {
BaseCountedInstance::SwapImpl(lhs, rhs);
}
static bool supports_move() { return true; }
};
// Only movable, not default-constructible.
class MovableOnlyInstance : public BaseCountedInstance {
public:
explicit MovableOnlyInstance(int x) : BaseCountedInstance(x) {}
MovableOnlyInstance(MovableOnlyInstance&& other) = default;
MovableOnlyInstance& operator=(MovableOnlyInstance&& other) = default;
friend void swap(MovableOnlyInstance& lhs, MovableOnlyInstance& rhs) {
BaseCountedInstance::SwapImpl(lhs, rhs);
}
static bool supports_move() { return true; }
};
} // namespace test_internal
ABSL_NAMESPACE_END
} // namespace absl
#endif // ABSL_CONTAINER_INTERNAL_TEST_INSTANCE_TRACKER_H_

View file

@ -0,0 +1,184 @@
// Copyright 2017 The Abseil Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "absl/container/internal/test_instance_tracker.h"
#include "gtest/gtest.h"
namespace {
using absl::test_internal::CopyableMovableInstance;
using absl::test_internal::CopyableOnlyInstance;
using absl::test_internal::InstanceTracker;
using absl::test_internal::MovableOnlyInstance;
TEST(TestInstanceTracker, CopyableMovable) {
InstanceTracker tracker;
CopyableMovableInstance src(1);
EXPECT_EQ(1, src.value()) << src;
CopyableMovableInstance copy(src);
CopyableMovableInstance move(std::move(src));
EXPECT_EQ(1, tracker.copies());
EXPECT_EQ(1, tracker.moves());
EXPECT_EQ(0, tracker.swaps());
EXPECT_EQ(3, tracker.instances());
EXPECT_EQ(2, tracker.live_instances());
tracker.ResetCopiesMovesSwaps();
CopyableMovableInstance copy_assign(1);
copy_assign = copy;
CopyableMovableInstance move_assign(1);
move_assign = std::move(move);
EXPECT_EQ(1, tracker.copies());
EXPECT_EQ(1, tracker.moves());
EXPECT_EQ(0, tracker.swaps());
EXPECT_EQ(5, tracker.instances());
EXPECT_EQ(3, tracker.live_instances());
tracker.ResetCopiesMovesSwaps();
{
using std::swap;
swap(move_assign, copy);
swap(copy, move_assign);
EXPECT_EQ(2, tracker.swaps());
EXPECT_EQ(0, tracker.copies());
EXPECT_EQ(0, tracker.moves());
EXPECT_EQ(5, tracker.instances());
EXPECT_EQ(3, tracker.live_instances());
}
}
TEST(TestInstanceTracker, CopyableOnly) {
InstanceTracker tracker;
CopyableOnlyInstance src(1);
EXPECT_EQ(1, src.value()) << src;
CopyableOnlyInstance copy(src);
CopyableOnlyInstance copy2(std::move(src)); // NOLINT
EXPECT_EQ(2, tracker.copies());
EXPECT_EQ(0, tracker.moves());
EXPECT_EQ(3, tracker.instances());
EXPECT_EQ(3, tracker.live_instances());
tracker.ResetCopiesMovesSwaps();
CopyableOnlyInstance copy_assign(1);
copy_assign = copy;
CopyableOnlyInstance copy_assign2(1);
copy_assign2 = std::move(copy2); // NOLINT
EXPECT_EQ(2, tracker.copies());
EXPECT_EQ(0, tracker.moves());
EXPECT_EQ(5, tracker.instances());
EXPECT_EQ(5, tracker.live_instances());
tracker.ResetCopiesMovesSwaps();
{
using std::swap;
swap(src, copy);
swap(copy, src);
EXPECT_EQ(2, tracker.swaps());
EXPECT_EQ(0, tracker.copies());
EXPECT_EQ(0, tracker.moves());
EXPECT_EQ(5, tracker.instances());
EXPECT_EQ(5, tracker.live_instances());
}
}
TEST(TestInstanceTracker, MovableOnly) {
InstanceTracker tracker;
MovableOnlyInstance src(1);
EXPECT_EQ(1, src.value()) << src;
MovableOnlyInstance move(std::move(src));
MovableOnlyInstance move_assign(2);
move_assign = std::move(move);
EXPECT_EQ(3, tracker.instances());
EXPECT_EQ(1, tracker.live_instances());
EXPECT_EQ(2, tracker.moves());
EXPECT_EQ(0, tracker.copies());
tracker.ResetCopiesMovesSwaps();
{
using std::swap;
MovableOnlyInstance other(2);
swap(move_assign, other);
swap(other, move_assign);
EXPECT_EQ(2, tracker.swaps());
EXPECT_EQ(0, tracker.copies());
EXPECT_EQ(0, tracker.moves());
EXPECT_EQ(4, tracker.instances());
EXPECT_EQ(2, tracker.live_instances());
}
}
TEST(TestInstanceTracker, ExistingInstances) {
CopyableMovableInstance uncounted_instance(1);
CopyableMovableInstance uncounted_live_instance(
std::move(uncounted_instance));
InstanceTracker tracker;
EXPECT_EQ(0, tracker.instances());
EXPECT_EQ(0, tracker.live_instances());
EXPECT_EQ(0, tracker.copies());
{
CopyableMovableInstance instance1(1);
EXPECT_EQ(1, tracker.instances());
EXPECT_EQ(1, tracker.live_instances());
EXPECT_EQ(0, tracker.copies());
EXPECT_EQ(0, tracker.moves());
{
InstanceTracker tracker2;
CopyableMovableInstance instance2(instance1);
CopyableMovableInstance instance3(std::move(instance2));
EXPECT_EQ(3, tracker.instances());
EXPECT_EQ(2, tracker.live_instances());
EXPECT_EQ(1, tracker.copies());
EXPECT_EQ(1, tracker.moves());
EXPECT_EQ(2, tracker2.instances());
EXPECT_EQ(1, tracker2.live_instances());
EXPECT_EQ(1, tracker2.copies());
EXPECT_EQ(1, tracker2.moves());
}
EXPECT_EQ(1, tracker.instances());
EXPECT_EQ(1, tracker.live_instances());
EXPECT_EQ(1, tracker.copies());
EXPECT_EQ(1, tracker.moves());
}
EXPECT_EQ(0, tracker.instances());
EXPECT_EQ(0, tracker.live_instances());
EXPECT_EQ(1, tracker.copies());
EXPECT_EQ(1, tracker.moves());
}
TEST(TestInstanceTracker, Comparisons) {
InstanceTracker tracker;
MovableOnlyInstance one(1), two(2);
EXPECT_EQ(0, tracker.comparisons());
EXPECT_FALSE(one == two);
EXPECT_EQ(1, tracker.comparisons());
EXPECT_TRUE(one != two);
EXPECT_EQ(2, tracker.comparisons());
EXPECT_TRUE(one < two);
EXPECT_EQ(3, tracker.comparisons());
EXPECT_FALSE(one > two);
EXPECT_EQ(4, tracker.comparisons());
EXPECT_TRUE(one <= two);
EXPECT_EQ(5, tracker.comparisons());
EXPECT_FALSE(one >= two);
EXPECT_EQ(6, tracker.comparisons());
EXPECT_TRUE(one.compare(two) < 0); // NOLINT
EXPECT_EQ(7, tracker.comparisons());
tracker.ResetCopiesMovesSwaps();
EXPECT_EQ(0, tracker.comparisons());
}
} // namespace

View file

@ -0,0 +1,83 @@
// Copyright 2018 The Abseil Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef ABSL_CONTAINER_INTERNAL_TRACKED_H_
#define ABSL_CONTAINER_INTERNAL_TRACKED_H_
#include <stddef.h>
#include <memory>
#include <utility>
#include "absl/base/config.h"
namespace absl {
ABSL_NAMESPACE_BEGIN
namespace container_internal {
// A class that tracks its copies and moves so that it can be queried in tests.
template <class T>
class Tracked {
public:
Tracked() {}
// NOLINTNEXTLINE(runtime/explicit)
Tracked(const T& val) : val_(val) {}
Tracked(const Tracked& that)
: val_(that.val_),
num_moves_(that.num_moves_),
num_copies_(that.num_copies_) {
++(*num_copies_);
}
Tracked(Tracked&& that)
: val_(std::move(that.val_)),
num_moves_(std::move(that.num_moves_)),
num_copies_(std::move(that.num_copies_)) {
++(*num_moves_);
}
Tracked& operator=(const Tracked& that) {
val_ = that.val_;
num_moves_ = that.num_moves_;
num_copies_ = that.num_copies_;
++(*num_copies_);
}
Tracked& operator=(Tracked&& that) {
val_ = std::move(that.val_);
num_moves_ = std::move(that.num_moves_);
num_copies_ = std::move(that.num_copies_);
++(*num_moves_);
}
const T& val() const { return val_; }
friend bool operator==(const Tracked& a, const Tracked& b) {
return a.val_ == b.val_;
}
friend bool operator!=(const Tracked& a, const Tracked& b) {
return !(a == b);
}
size_t num_copies() { return *num_copies_; }
size_t num_moves() { return *num_moves_; }
private:
T val_;
std::shared_ptr<size_t> num_moves_ = std::make_shared<size_t>(0);
std::shared_ptr<size_t> num_copies_ = std::make_shared<size_t>(0);
};
} // namespace container_internal
ABSL_NAMESPACE_END
} // namespace absl
#endif // ABSL_CONTAINER_INTERNAL_TRACKED_H_

View file

@ -0,0 +1,494 @@
// Copyright 2018 The Abseil Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef ABSL_CONTAINER_INTERNAL_UNORDERED_MAP_CONSTRUCTOR_TEST_H_
#define ABSL_CONTAINER_INTERNAL_UNORDERED_MAP_CONSTRUCTOR_TEST_H_
#include <algorithm>
#include <unordered_map>
#include <vector>
#include "gmock/gmock.h"
#include "gtest/gtest.h"
#include "absl/container/internal/hash_generator_testing.h"
#include "absl/container/internal/hash_policy_testing.h"
namespace absl {
ABSL_NAMESPACE_BEGIN
namespace container_internal {
template <class UnordMap>
class ConstructorTest : public ::testing::Test {};
TYPED_TEST_SUITE_P(ConstructorTest);
TYPED_TEST_P(ConstructorTest, NoArgs) {
TypeParam m;
EXPECT_TRUE(m.empty());
EXPECT_THAT(m, ::testing::UnorderedElementsAre());
}
TYPED_TEST_P(ConstructorTest, BucketCount) {
TypeParam m(123);
EXPECT_TRUE(m.empty());
EXPECT_THAT(m, ::testing::UnorderedElementsAre());
EXPECT_GE(m.bucket_count(), 123);
}
TYPED_TEST_P(ConstructorTest, BucketCountHash) {
using H = typename TypeParam::hasher;
H hasher;
TypeParam m(123, hasher);
EXPECT_EQ(m.hash_function(), hasher);
EXPECT_TRUE(m.empty());
EXPECT_THAT(m, ::testing::UnorderedElementsAre());
EXPECT_GE(m.bucket_count(), 123);
}
TYPED_TEST_P(ConstructorTest, BucketCountHashEqual) {
using H = typename TypeParam::hasher;
using E = typename TypeParam::key_equal;
H hasher;
E equal;
TypeParam m(123, hasher, equal);
EXPECT_EQ(m.hash_function(), hasher);
EXPECT_EQ(m.key_eq(), equal);
EXPECT_TRUE(m.empty());
EXPECT_THAT(m, ::testing::UnorderedElementsAre());
EXPECT_GE(m.bucket_count(), 123);
}
TYPED_TEST_P(ConstructorTest, BucketCountHashEqualAlloc) {
using H = typename TypeParam::hasher;
using E = typename TypeParam::key_equal;
using A = typename TypeParam::allocator_type;
H hasher;
E equal;
A alloc(0);
TypeParam m(123, hasher, equal, alloc);
EXPECT_EQ(m.hash_function(), hasher);
EXPECT_EQ(m.key_eq(), equal);
EXPECT_EQ(m.get_allocator(), alloc);
EXPECT_TRUE(m.empty());
EXPECT_THAT(m, ::testing::UnorderedElementsAre());
EXPECT_GE(m.bucket_count(), 123);
}
template <typename T>
struct is_std_unordered_map : std::false_type {};
template <typename... T>
struct is_std_unordered_map<std::unordered_map<T...>> : std::true_type {};
#if defined(UNORDERED_MAP_CXX14) || defined(UNORDERED_MAP_CXX17)
using has_cxx14_std_apis = std::true_type;
#else
using has_cxx14_std_apis = std::false_type;
#endif
template <typename T>
using expect_cxx14_apis =
absl::disjunction<absl::negation<is_std_unordered_map<T>>,
has_cxx14_std_apis>;
template <typename TypeParam>
void BucketCountAllocTest(std::false_type) {}
template <typename TypeParam>
void BucketCountAllocTest(std::true_type) {
using A = typename TypeParam::allocator_type;
A alloc(0);
TypeParam m(123, alloc);
EXPECT_EQ(m.get_allocator(), alloc);
EXPECT_TRUE(m.empty());
EXPECT_THAT(m, ::testing::UnorderedElementsAre());
EXPECT_GE(m.bucket_count(), 123);
}
TYPED_TEST_P(ConstructorTest, BucketCountAlloc) {
BucketCountAllocTest<TypeParam>(expect_cxx14_apis<TypeParam>());
}
template <typename TypeParam>
void BucketCountHashAllocTest(std::false_type) {}
template <typename TypeParam>
void BucketCountHashAllocTest(std::true_type) {
using H = typename TypeParam::hasher;
using A = typename TypeParam::allocator_type;
H hasher;
A alloc(0);
TypeParam m(123, hasher, alloc);
EXPECT_EQ(m.hash_function(), hasher);
EXPECT_EQ(m.get_allocator(), alloc);
EXPECT_TRUE(m.empty());
EXPECT_THAT(m, ::testing::UnorderedElementsAre());
EXPECT_GE(m.bucket_count(), 123);
}
TYPED_TEST_P(ConstructorTest, BucketCountHashAlloc) {
BucketCountHashAllocTest<TypeParam>(expect_cxx14_apis<TypeParam>());
}
#if ABSL_UNORDERED_SUPPORTS_ALLOC_CTORS
using has_alloc_std_constructors = std::true_type;
#else
using has_alloc_std_constructors = std::false_type;
#endif
template <typename T>
using expect_alloc_constructors =
absl::disjunction<absl::negation<is_std_unordered_map<T>>,
has_alloc_std_constructors>;
template <typename TypeParam>
void AllocTest(std::false_type) {}
template <typename TypeParam>
void AllocTest(std::true_type) {
using A = typename TypeParam::allocator_type;
A alloc(0);
TypeParam m(alloc);
EXPECT_EQ(m.get_allocator(), alloc);
EXPECT_TRUE(m.empty());
EXPECT_THAT(m, ::testing::UnorderedElementsAre());
}
TYPED_TEST_P(ConstructorTest, Alloc) {
AllocTest<TypeParam>(expect_alloc_constructors<TypeParam>());
}
TYPED_TEST_P(ConstructorTest, InputIteratorBucketHashEqualAlloc) {
using T = hash_internal::GeneratedType<TypeParam>;
using H = typename TypeParam::hasher;
using E = typename TypeParam::key_equal;
using A = typename TypeParam::allocator_type;
H hasher;
E equal;
A alloc(0);
std::vector<T> values;
std::generate_n(std::back_inserter(values), 10,
hash_internal::UniqueGenerator<T>());
TypeParam m(values.begin(), values.end(), 123, hasher, equal, alloc);
EXPECT_EQ(m.hash_function(), hasher);
EXPECT_EQ(m.key_eq(), equal);
EXPECT_EQ(m.get_allocator(), alloc);
EXPECT_THAT(items(m), ::testing::UnorderedElementsAreArray(values));
EXPECT_GE(m.bucket_count(), 123);
}
template <typename TypeParam>
void InputIteratorBucketAllocTest(std::false_type) {}
template <typename TypeParam>
void InputIteratorBucketAllocTest(std::true_type) {
using T = hash_internal::GeneratedType<TypeParam>;
using A = typename TypeParam::allocator_type;
A alloc(0);
std::vector<T> values;
std::generate_n(std::back_inserter(values), 10,
hash_internal::UniqueGenerator<T>());
TypeParam m(values.begin(), values.end(), 123, alloc);
EXPECT_EQ(m.get_allocator(), alloc);
EXPECT_THAT(items(m), ::testing::UnorderedElementsAreArray(values));
EXPECT_GE(m.bucket_count(), 123);
}
TYPED_TEST_P(ConstructorTest, InputIteratorBucketAlloc) {
InputIteratorBucketAllocTest<TypeParam>(expect_cxx14_apis<TypeParam>());
}
template <typename TypeParam>
void InputIteratorBucketHashAllocTest(std::false_type) {}
template <typename TypeParam>
void InputIteratorBucketHashAllocTest(std::true_type) {
using T = hash_internal::GeneratedType<TypeParam>;
using H = typename TypeParam::hasher;
using A = typename TypeParam::allocator_type;
H hasher;
A alloc(0);
std::vector<T> values;
std::generate_n(std::back_inserter(values), 10,
hash_internal::UniqueGenerator<T>());
TypeParam m(values.begin(), values.end(), 123, hasher, alloc);
EXPECT_EQ(m.hash_function(), hasher);
EXPECT_EQ(m.get_allocator(), alloc);
EXPECT_THAT(items(m), ::testing::UnorderedElementsAreArray(values));
EXPECT_GE(m.bucket_count(), 123);
}
TYPED_TEST_P(ConstructorTest, InputIteratorBucketHashAlloc) {
InputIteratorBucketHashAllocTest<TypeParam>(expect_cxx14_apis<TypeParam>());
}
TYPED_TEST_P(ConstructorTest, CopyConstructor) {
using T = hash_internal::GeneratedType<TypeParam>;
using H = typename TypeParam::hasher;
using E = typename TypeParam::key_equal;
using A = typename TypeParam::allocator_type;
H hasher;
E equal;
A alloc(0);
hash_internal::UniqueGenerator<T> gen;
TypeParam m(123, hasher, equal, alloc);
for (size_t i = 0; i != 10; ++i) m.insert(gen());
TypeParam n(m);
EXPECT_EQ(m.hash_function(), n.hash_function());
EXPECT_EQ(m.key_eq(), n.key_eq());
EXPECT_EQ(m.get_allocator(), n.get_allocator());
EXPECT_EQ(m, n);
}
template <typename TypeParam>
void CopyConstructorAllocTest(std::false_type) {}
template <typename TypeParam>
void CopyConstructorAllocTest(std::true_type) {
using T = hash_internal::GeneratedType<TypeParam>;
using H = typename TypeParam::hasher;
using E = typename TypeParam::key_equal;
using A = typename TypeParam::allocator_type;
H hasher;
E equal;
A alloc(0);
hash_internal::UniqueGenerator<T> gen;
TypeParam m(123, hasher, equal, alloc);
for (size_t i = 0; i != 10; ++i) m.insert(gen());
TypeParam n(m, A(11));
EXPECT_EQ(m.hash_function(), n.hash_function());
EXPECT_EQ(m.key_eq(), n.key_eq());
EXPECT_NE(m.get_allocator(), n.get_allocator());
EXPECT_EQ(m, n);
}
TYPED_TEST_P(ConstructorTest, CopyConstructorAlloc) {
CopyConstructorAllocTest<TypeParam>(expect_alloc_constructors<TypeParam>());
}
// TODO(alkis): Test non-propagating allocators on copy constructors.
TYPED_TEST_P(ConstructorTest, MoveConstructor) {
using T = hash_internal::GeneratedType<TypeParam>;
using H = typename TypeParam::hasher;
using E = typename TypeParam::key_equal;
using A = typename TypeParam::allocator_type;
H hasher;
E equal;
A alloc(0);
hash_internal::UniqueGenerator<T> gen;
TypeParam m(123, hasher, equal, alloc);
for (size_t i = 0; i != 10; ++i) m.insert(gen());
TypeParam t(m);
TypeParam n(std::move(t));
EXPECT_EQ(m.hash_function(), n.hash_function());
EXPECT_EQ(m.key_eq(), n.key_eq());
EXPECT_EQ(m.get_allocator(), n.get_allocator());
EXPECT_EQ(m, n);
}
template <typename TypeParam>
void MoveConstructorAllocTest(std::false_type) {}
template <typename TypeParam>
void MoveConstructorAllocTest(std::true_type) {
using T = hash_internal::GeneratedType<TypeParam>;
using H = typename TypeParam::hasher;
using E = typename TypeParam::key_equal;
using A = typename TypeParam::allocator_type;
H hasher;
E equal;
A alloc(0);
hash_internal::UniqueGenerator<T> gen;
TypeParam m(123, hasher, equal, alloc);
for (size_t i = 0; i != 10; ++i) m.insert(gen());
TypeParam t(m);
TypeParam n(std::move(t), A(1));
EXPECT_EQ(m.hash_function(), n.hash_function());
EXPECT_EQ(m.key_eq(), n.key_eq());
EXPECT_NE(m.get_allocator(), n.get_allocator());
EXPECT_EQ(m, n);
}
TYPED_TEST_P(ConstructorTest, MoveConstructorAlloc) {
MoveConstructorAllocTest<TypeParam>(expect_alloc_constructors<TypeParam>());
}
// TODO(alkis): Test non-propagating allocators on move constructors.
TYPED_TEST_P(ConstructorTest, InitializerListBucketHashEqualAlloc) {
using T = hash_internal::GeneratedType<TypeParam>;
hash_internal::UniqueGenerator<T> gen;
std::initializer_list<T> values = {gen(), gen(), gen(), gen(), gen()};
using H = typename TypeParam::hasher;
using E = typename TypeParam::key_equal;
using A = typename TypeParam::allocator_type;
H hasher;
E equal;
A alloc(0);
TypeParam m(values, 123, hasher, equal, alloc);
EXPECT_EQ(m.hash_function(), hasher);
EXPECT_EQ(m.key_eq(), equal);
EXPECT_EQ(m.get_allocator(), alloc);
EXPECT_THAT(items(m), ::testing::UnorderedElementsAreArray(values));
EXPECT_GE(m.bucket_count(), 123);
}
template <typename TypeParam>
void InitializerListBucketAllocTest(std::false_type) {}
template <typename TypeParam>
void InitializerListBucketAllocTest(std::true_type) {
using T = hash_internal::GeneratedType<TypeParam>;
using A = typename TypeParam::allocator_type;
hash_internal::UniqueGenerator<T> gen;
std::initializer_list<T> values = {gen(), gen(), gen(), gen(), gen()};
A alloc(0);
TypeParam m(values, 123, alloc);
EXPECT_EQ(m.get_allocator(), alloc);
EXPECT_THAT(items(m), ::testing::UnorderedElementsAreArray(values));
EXPECT_GE(m.bucket_count(), 123);
}
TYPED_TEST_P(ConstructorTest, InitializerListBucketAlloc) {
InitializerListBucketAllocTest<TypeParam>(expect_cxx14_apis<TypeParam>());
}
template <typename TypeParam>
void InitializerListBucketHashAllocTest(std::false_type) {}
template <typename TypeParam>
void InitializerListBucketHashAllocTest(std::true_type) {
using T = hash_internal::GeneratedType<TypeParam>;
using H = typename TypeParam::hasher;
using A = typename TypeParam::allocator_type;
H hasher;
A alloc(0);
hash_internal::UniqueGenerator<T> gen;
std::initializer_list<T> values = {gen(), gen(), gen(), gen(), gen()};
TypeParam m(values, 123, hasher, alloc);
EXPECT_EQ(m.hash_function(), hasher);
EXPECT_EQ(m.get_allocator(), alloc);
EXPECT_THAT(items(m), ::testing::UnorderedElementsAreArray(values));
EXPECT_GE(m.bucket_count(), 123);
}
TYPED_TEST_P(ConstructorTest, InitializerListBucketHashAlloc) {
InitializerListBucketHashAllocTest<TypeParam>(expect_cxx14_apis<TypeParam>());
}
TYPED_TEST_P(ConstructorTest, Assignment) {
using T = hash_internal::GeneratedType<TypeParam>;
using H = typename TypeParam::hasher;
using E = typename TypeParam::key_equal;
using A = typename TypeParam::allocator_type;
H hasher;
E equal;
A alloc(0);
hash_internal::UniqueGenerator<T> gen;
TypeParam m({gen(), gen(), gen()}, 123, hasher, equal, alloc);
TypeParam n;
n = m;
EXPECT_EQ(m.hash_function(), n.hash_function());
EXPECT_EQ(m.key_eq(), n.key_eq());
EXPECT_EQ(m, n);
}
// TODO(alkis): Test [non-]propagating allocators on move/copy assignments
// (it depends on traits).
TYPED_TEST_P(ConstructorTest, MoveAssignment) {
using T = hash_internal::GeneratedType<TypeParam>;
using H = typename TypeParam::hasher;
using E = typename TypeParam::key_equal;
using A = typename TypeParam::allocator_type;
H hasher;
E equal;
A alloc(0);
hash_internal::UniqueGenerator<T> gen;
TypeParam m({gen(), gen(), gen()}, 123, hasher, equal, alloc);
TypeParam t(m);
TypeParam n;
n = std::move(t);
EXPECT_EQ(m.hash_function(), n.hash_function());
EXPECT_EQ(m.key_eq(), n.key_eq());
EXPECT_EQ(m, n);
}
TYPED_TEST_P(ConstructorTest, AssignmentFromInitializerList) {
using T = hash_internal::GeneratedType<TypeParam>;
hash_internal::UniqueGenerator<T> gen;
std::initializer_list<T> values = {gen(), gen(), gen(), gen(), gen()};
TypeParam m;
m = values;
EXPECT_THAT(items(m), ::testing::UnorderedElementsAreArray(values));
}
TYPED_TEST_P(ConstructorTest, AssignmentOverwritesExisting) {
using T = hash_internal::GeneratedType<TypeParam>;
hash_internal::UniqueGenerator<T> gen;
TypeParam m({gen(), gen(), gen()});
TypeParam n({gen()});
n = m;
EXPECT_EQ(m, n);
}
TYPED_TEST_P(ConstructorTest, MoveAssignmentOverwritesExisting) {
using T = hash_internal::GeneratedType<TypeParam>;
hash_internal::UniqueGenerator<T> gen;
TypeParam m({gen(), gen(), gen()});
TypeParam t(m);
TypeParam n({gen()});
n = std::move(t);
EXPECT_EQ(m, n);
}
TYPED_TEST_P(ConstructorTest, AssignmentFromInitializerListOverwritesExisting) {
using T = hash_internal::GeneratedType<TypeParam>;
hash_internal::UniqueGenerator<T> gen;
std::initializer_list<T> values = {gen(), gen(), gen(), gen(), gen()};
TypeParam m;
m = values;
EXPECT_THAT(items(m), ::testing::UnorderedElementsAreArray(values));
}
TYPED_TEST_P(ConstructorTest, AssignmentOnSelf) {
using T = hash_internal::GeneratedType<TypeParam>;
hash_internal::UniqueGenerator<T> gen;
std::initializer_list<T> values = {gen(), gen(), gen(), gen(), gen()};
TypeParam m(values);
m = *&m; // Avoid -Wself-assign
EXPECT_THAT(items(m), ::testing::UnorderedElementsAreArray(values));
}
// We cannot test self move as standard states that it leaves standard
// containers in unspecified state (and in practice in causes memory-leak
// according to heap-checker!).
REGISTER_TYPED_TEST_SUITE_P(
ConstructorTest, NoArgs, BucketCount, BucketCountHash, BucketCountHashEqual,
BucketCountHashEqualAlloc, BucketCountAlloc, BucketCountHashAlloc, Alloc,
InputIteratorBucketHashEqualAlloc, InputIteratorBucketAlloc,
InputIteratorBucketHashAlloc, CopyConstructor, CopyConstructorAlloc,
MoveConstructor, MoveConstructorAlloc, InitializerListBucketHashEqualAlloc,
InitializerListBucketAlloc, InitializerListBucketHashAlloc, Assignment,
MoveAssignment, AssignmentFromInitializerList, AssignmentOverwritesExisting,
MoveAssignmentOverwritesExisting,
AssignmentFromInitializerListOverwritesExisting, AssignmentOnSelf);
} // namespace container_internal
ABSL_NAMESPACE_END
} // namespace absl
#endif // ABSL_CONTAINER_INTERNAL_UNORDERED_MAP_CONSTRUCTOR_TEST_H_

View file

@ -0,0 +1,117 @@
// Copyright 2018 The Abseil Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef ABSL_CONTAINER_INTERNAL_UNORDERED_MAP_LOOKUP_TEST_H_
#define ABSL_CONTAINER_INTERNAL_UNORDERED_MAP_LOOKUP_TEST_H_
#include "gmock/gmock.h"
#include "gtest/gtest.h"
#include "absl/container/internal/hash_generator_testing.h"
#include "absl/container/internal/hash_policy_testing.h"
namespace absl {
ABSL_NAMESPACE_BEGIN
namespace container_internal {
template <class UnordMap>
class LookupTest : public ::testing::Test {};
TYPED_TEST_SUITE_P(LookupTest);
TYPED_TEST_P(LookupTest, At) {
using T = hash_internal::GeneratedType<TypeParam>;
std::vector<T> values;
std::generate_n(std::back_inserter(values), 10,
hash_internal::Generator<T>());
TypeParam m(values.begin(), values.end());
for (const auto& p : values) {
const auto& val = m.at(p.first);
EXPECT_EQ(p.second, val) << ::testing::PrintToString(p.first);
}
}
TYPED_TEST_P(LookupTest, OperatorBracket) {
using T = hash_internal::GeneratedType<TypeParam>;
using V = typename TypeParam::mapped_type;
std::vector<T> values;
std::generate_n(std::back_inserter(values), 10,
hash_internal::Generator<T>());
TypeParam m;
for (const auto& p : values) {
auto& val = m[p.first];
EXPECT_EQ(V(), val) << ::testing::PrintToString(p.first);
val = p.second;
}
for (const auto& p : values)
EXPECT_EQ(p.second, m[p.first]) << ::testing::PrintToString(p.first);
}
TYPED_TEST_P(LookupTest, Count) {
using T = hash_internal::GeneratedType<TypeParam>;
std::vector<T> values;
std::generate_n(std::back_inserter(values), 10,
hash_internal::Generator<T>());
TypeParam m;
for (const auto& p : values)
EXPECT_EQ(0, m.count(p.first)) << ::testing::PrintToString(p.first);
m.insert(values.begin(), values.end());
for (const auto& p : values)
EXPECT_EQ(1, m.count(p.first)) << ::testing::PrintToString(p.first);
}
TYPED_TEST_P(LookupTest, Find) {
using std::get;
using T = hash_internal::GeneratedType<TypeParam>;
std::vector<T> values;
std::generate_n(std::back_inserter(values), 10,
hash_internal::Generator<T>());
TypeParam m;
for (const auto& p : values)
EXPECT_TRUE(m.end() == m.find(p.first))
<< ::testing::PrintToString(p.first);
m.insert(values.begin(), values.end());
for (const auto& p : values) {
auto it = m.find(p.first);
EXPECT_TRUE(m.end() != it) << ::testing::PrintToString(p.first);
EXPECT_EQ(p.second, get<1>(*it)) << ::testing::PrintToString(p.first);
}
}
TYPED_TEST_P(LookupTest, EqualRange) {
using std::get;
using T = hash_internal::GeneratedType<TypeParam>;
std::vector<T> values;
std::generate_n(std::back_inserter(values), 10,
hash_internal::Generator<T>());
TypeParam m;
for (const auto& p : values) {
auto r = m.equal_range(p.first);
ASSERT_EQ(0, std::distance(r.first, r.second));
}
m.insert(values.begin(), values.end());
for (const auto& p : values) {
auto r = m.equal_range(p.first);
ASSERT_EQ(1, std::distance(r.first, r.second));
EXPECT_EQ(p.second, get<1>(*r.first)) << ::testing::PrintToString(p.first);
}
}
REGISTER_TYPED_TEST_SUITE_P(LookupTest, At, OperatorBracket, Count, Find,
EqualRange);
} // namespace container_internal
ABSL_NAMESPACE_END
} // namespace absl
#endif // ABSL_CONTAINER_INTERNAL_UNORDERED_MAP_LOOKUP_TEST_H_

View file

@ -0,0 +1,87 @@
// Copyright 2019 The Abseil Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef ABSL_CONTAINER_INTERNAL_UNORDERED_MAP_MEMBERS_TEST_H_
#define ABSL_CONTAINER_INTERNAL_UNORDERED_MAP_MEMBERS_TEST_H_
#include <type_traits>
#include "gmock/gmock.h"
#include "gtest/gtest.h"
#include "absl/meta/type_traits.h"
namespace absl {
ABSL_NAMESPACE_BEGIN
namespace container_internal {
template <class UnordMap>
class MembersTest : public ::testing::Test {};
TYPED_TEST_SUITE_P(MembersTest);
template <typename T>
void UseType() {}
TYPED_TEST_P(MembersTest, Typedefs) {
EXPECT_TRUE((std::is_same<std::pair<const typename TypeParam::key_type,
typename TypeParam::mapped_type>,
typename TypeParam::value_type>()));
EXPECT_TRUE((absl::conjunction<
absl::negation<std::is_signed<typename TypeParam::size_type>>,
std::is_integral<typename TypeParam::size_type>>()));
EXPECT_TRUE((absl::conjunction<
std::is_signed<typename TypeParam::difference_type>,
std::is_integral<typename TypeParam::difference_type>>()));
EXPECT_TRUE((std::is_convertible<
decltype(std::declval<const typename TypeParam::hasher&>()(
std::declval<const typename TypeParam::key_type&>())),
size_t>()));
EXPECT_TRUE((std::is_convertible<
decltype(std::declval<const typename TypeParam::key_equal&>()(
std::declval<const typename TypeParam::key_type&>(),
std::declval<const typename TypeParam::key_type&>())),
bool>()));
EXPECT_TRUE((std::is_same<typename TypeParam::allocator_type::value_type,
typename TypeParam::value_type>()));
EXPECT_TRUE((std::is_same<typename TypeParam::value_type&,
typename TypeParam::reference>()));
EXPECT_TRUE((std::is_same<const typename TypeParam::value_type&,
typename TypeParam::const_reference>()));
EXPECT_TRUE((std::is_same<typename std::allocator_traits<
typename TypeParam::allocator_type>::pointer,
typename TypeParam::pointer>()));
EXPECT_TRUE(
(std::is_same<typename std::allocator_traits<
typename TypeParam::allocator_type>::const_pointer,
typename TypeParam::const_pointer>()));
}
TYPED_TEST_P(MembersTest, SimpleFunctions) {
EXPECT_GT(TypeParam().max_size(), 0);
}
TYPED_TEST_P(MembersTest, BeginEnd) {
TypeParam t = {typename TypeParam::value_type{}};
EXPECT_EQ(t.begin(), t.cbegin());
EXPECT_EQ(t.end(), t.cend());
EXPECT_NE(t.begin(), t.end());
EXPECT_NE(t.cbegin(), t.cend());
}
REGISTER_TYPED_TEST_SUITE_P(MembersTest, Typedefs, SimpleFunctions, BeginEnd);
} // namespace container_internal
ABSL_NAMESPACE_END
} // namespace absl
#endif // ABSL_CONTAINER_INTERNAL_UNORDERED_MAP_MEMBERS_TEST_H_

View file

@ -0,0 +1,352 @@
// Copyright 2018 The Abseil Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef ABSL_CONTAINER_INTERNAL_UNORDERED_MAP_MODIFIERS_TEST_H_
#define ABSL_CONTAINER_INTERNAL_UNORDERED_MAP_MODIFIERS_TEST_H_
#include <memory>
#include "gmock/gmock.h"
#include "gtest/gtest.h"
#include "absl/container/internal/hash_generator_testing.h"
#include "absl/container/internal/hash_policy_testing.h"
namespace absl {
ABSL_NAMESPACE_BEGIN
namespace container_internal {
template <class UnordMap>
class ModifiersTest : public ::testing::Test {};
TYPED_TEST_SUITE_P(ModifiersTest);
TYPED_TEST_P(ModifiersTest, Clear) {
using T = hash_internal::GeneratedType<TypeParam>;
std::vector<T> values;
std::generate_n(std::back_inserter(values), 10,
hash_internal::Generator<T>());
TypeParam m(values.begin(), values.end());
ASSERT_THAT(items(m), ::testing::UnorderedElementsAreArray(values));
m.clear();
EXPECT_THAT(items(m), ::testing::UnorderedElementsAre());
EXPECT_TRUE(m.empty());
}
TYPED_TEST_P(ModifiersTest, Insert) {
using T = hash_internal::GeneratedType<TypeParam>;
using V = typename TypeParam::mapped_type;
T val = hash_internal::Generator<T>()();
TypeParam m;
auto p = m.insert(val);
EXPECT_TRUE(p.second);
EXPECT_EQ(val, *p.first);
T val2 = {val.first, hash_internal::Generator<V>()()};
p = m.insert(val2);
EXPECT_FALSE(p.second);
EXPECT_EQ(val, *p.first);
}
TYPED_TEST_P(ModifiersTest, InsertHint) {
using T = hash_internal::GeneratedType<TypeParam>;
using V = typename TypeParam::mapped_type;
T val = hash_internal::Generator<T>()();
TypeParam m;
auto it = m.insert(m.end(), val);
EXPECT_TRUE(it != m.end());
EXPECT_EQ(val, *it);
T val2 = {val.first, hash_internal::Generator<V>()()};
it = m.insert(it, val2);
EXPECT_TRUE(it != m.end());
EXPECT_EQ(val, *it);
}
TYPED_TEST_P(ModifiersTest, InsertRange) {
using T = hash_internal::GeneratedType<TypeParam>;
std::vector<T> values;
std::generate_n(std::back_inserter(values), 10,
hash_internal::Generator<T>());
TypeParam m;
m.insert(values.begin(), values.end());
ASSERT_THAT(items(m), ::testing::UnorderedElementsAreArray(values));
}
TYPED_TEST_P(ModifiersTest, InsertWithinCapacity) {
using T = hash_internal::GeneratedType<TypeParam>;
using V = typename TypeParam::mapped_type;
T val = hash_internal::Generator<T>()();
TypeParam m;
m.reserve(10);
const size_t original_capacity = m.bucket_count();
m.insert(val);
EXPECT_EQ(m.bucket_count(), original_capacity);
T val2 = {val.first, hash_internal::Generator<V>()()};
m.insert(val2);
EXPECT_EQ(m.bucket_count(), original_capacity);
}
TYPED_TEST_P(ModifiersTest, InsertRangeWithinCapacity) {
#if !defined(__GLIBCXX__)
using T = hash_internal::GeneratedType<TypeParam>;
std::vector<T> base_values;
std::generate_n(std::back_inserter(base_values), 10,
hash_internal::Generator<T>());
std::vector<T> values;
while (values.size() != 100) {
std::copy_n(base_values.begin(), 10, std::back_inserter(values));
}
TypeParam m;
m.reserve(10);
const size_t original_capacity = m.bucket_count();
m.insert(values.begin(), values.end());
EXPECT_EQ(m.bucket_count(), original_capacity);
#endif
}
TYPED_TEST_P(ModifiersTest, InsertOrAssign) {
#ifdef UNORDERED_MAP_CXX17
using std::get;
using K = typename TypeParam::key_type;
using V = typename TypeParam::mapped_type;
K k = hash_internal::Generator<K>()();
V val = hash_internal::Generator<V>()();
TypeParam m;
auto p = m.insert_or_assign(k, val);
EXPECT_TRUE(p.second);
EXPECT_EQ(k, get<0>(*p.first));
EXPECT_EQ(val, get<1>(*p.first));
V val2 = hash_internal::Generator<V>()();
p = m.insert_or_assign(k, val2);
EXPECT_FALSE(p.second);
EXPECT_EQ(k, get<0>(*p.first));
EXPECT_EQ(val2, get<1>(*p.first));
#endif
}
TYPED_TEST_P(ModifiersTest, InsertOrAssignHint) {
#ifdef UNORDERED_MAP_CXX17
using std::get;
using K = typename TypeParam::key_type;
using V = typename TypeParam::mapped_type;
K k = hash_internal::Generator<K>()();
V val = hash_internal::Generator<V>()();
TypeParam m;
auto it = m.insert_or_assign(m.end(), k, val);
EXPECT_TRUE(it != m.end());
EXPECT_EQ(k, get<0>(*it));
EXPECT_EQ(val, get<1>(*it));
V val2 = hash_internal::Generator<V>()();
it = m.insert_or_assign(it, k, val2);
EXPECT_EQ(k, get<0>(*it));
EXPECT_EQ(val2, get<1>(*it));
#endif
}
TYPED_TEST_P(ModifiersTest, Emplace) {
using T = hash_internal::GeneratedType<TypeParam>;
using V = typename TypeParam::mapped_type;
T val = hash_internal::Generator<T>()();
TypeParam m;
// TODO(alkis): We need a way to run emplace in a more meaningful way. Perhaps
// with test traits/policy.
auto p = m.emplace(val);
EXPECT_TRUE(p.second);
EXPECT_EQ(val, *p.first);
T val2 = {val.first, hash_internal::Generator<V>()()};
p = m.emplace(val2);
EXPECT_FALSE(p.second);
EXPECT_EQ(val, *p.first);
}
TYPED_TEST_P(ModifiersTest, EmplaceHint) {
using T = hash_internal::GeneratedType<TypeParam>;
using V = typename TypeParam::mapped_type;
T val = hash_internal::Generator<T>()();
TypeParam m;
// TODO(alkis): We need a way to run emplace in a more meaningful way. Perhaps
// with test traits/policy.
auto it = m.emplace_hint(m.end(), val);
EXPECT_EQ(val, *it);
T val2 = {val.first, hash_internal::Generator<V>()()};
it = m.emplace_hint(it, val2);
EXPECT_EQ(val, *it);
}
TYPED_TEST_P(ModifiersTest, TryEmplace) {
#ifdef UNORDERED_MAP_CXX17
using T = hash_internal::GeneratedType<TypeParam>;
using V = typename TypeParam::mapped_type;
T val = hash_internal::Generator<T>()();
TypeParam m;
// TODO(alkis): We need a way to run emplace in a more meaningful way. Perhaps
// with test traits/policy.
auto p = m.try_emplace(val.first, val.second);
EXPECT_TRUE(p.second);
EXPECT_EQ(val, *p.first);
T val2 = {val.first, hash_internal::Generator<V>()()};
p = m.try_emplace(val2.first, val2.second);
EXPECT_FALSE(p.second);
EXPECT_EQ(val, *p.first);
#endif
}
TYPED_TEST_P(ModifiersTest, TryEmplaceHint) {
#ifdef UNORDERED_MAP_CXX17
using T = hash_internal::GeneratedType<TypeParam>;
using V = typename TypeParam::mapped_type;
T val = hash_internal::Generator<T>()();
TypeParam m;
// TODO(alkis): We need a way to run emplace in a more meaningful way. Perhaps
// with test traits/policy.
auto it = m.try_emplace(m.end(), val.first, val.second);
EXPECT_EQ(val, *it);
T val2 = {val.first, hash_internal::Generator<V>()()};
it = m.try_emplace(it, val2.first, val2.second);
EXPECT_EQ(val, *it);
#endif
}
template <class V>
using IfNotVoid = typename std::enable_if<!std::is_void<V>::value, V>::type;
// In openmap we chose not to return the iterator from erase because that's
// more expensive. As such we adapt erase to return an iterator here.
struct EraseFirst {
template <class Map>
auto operator()(Map* m, int) const
-> IfNotVoid<decltype(m->erase(m->begin()))> {
return m->erase(m->begin());
}
template <class Map>
typename Map::iterator operator()(Map* m, ...) const {
auto it = m->begin();
m->erase(it++);
return it;
}
};
TYPED_TEST_P(ModifiersTest, Erase) {
using T = hash_internal::GeneratedType<TypeParam>;
using std::get;
std::vector<T> values;
std::generate_n(std::back_inserter(values), 10,
hash_internal::Generator<T>());
TypeParam m(values.begin(), values.end());
ASSERT_THAT(items(m), ::testing::UnorderedElementsAreArray(values));
auto& first = *m.begin();
std::vector<T> values2;
for (const auto& val : values)
if (get<0>(val) != get<0>(first)) values2.push_back(val);
auto it = EraseFirst()(&m, 0);
ASSERT_TRUE(it != m.end());
EXPECT_EQ(1, std::count(values2.begin(), values2.end(), *it));
EXPECT_THAT(items(m), ::testing::UnorderedElementsAreArray(values2.begin(),
values2.end()));
}
TYPED_TEST_P(ModifiersTest, EraseRange) {
using T = hash_internal::GeneratedType<TypeParam>;
std::vector<T> values;
std::generate_n(std::back_inserter(values), 10,
hash_internal::Generator<T>());
TypeParam m(values.begin(), values.end());
ASSERT_THAT(items(m), ::testing::UnorderedElementsAreArray(values));
auto it = m.erase(m.begin(), m.end());
EXPECT_THAT(items(m), ::testing::UnorderedElementsAre());
EXPECT_TRUE(it == m.end());
}
TYPED_TEST_P(ModifiersTest, EraseKey) {
using T = hash_internal::GeneratedType<TypeParam>;
std::vector<T> values;
std::generate_n(std::back_inserter(values), 10,
hash_internal::Generator<T>());
TypeParam m(values.begin(), values.end());
ASSERT_THAT(items(m), ::testing::UnorderedElementsAreArray(values));
EXPECT_EQ(1, m.erase(values[0].first));
EXPECT_EQ(0, std::count(m.begin(), m.end(), values[0]));
EXPECT_THAT(items(m), ::testing::UnorderedElementsAreArray(values.begin() + 1,
values.end()));
}
TYPED_TEST_P(ModifiersTest, Swap) {
using T = hash_internal::GeneratedType<TypeParam>;
std::vector<T> v1;
std::vector<T> v2;
std::generate_n(std::back_inserter(v1), 5, hash_internal::Generator<T>());
std::generate_n(std::back_inserter(v2), 5, hash_internal::Generator<T>());
TypeParam m1(v1.begin(), v1.end());
TypeParam m2(v2.begin(), v2.end());
EXPECT_THAT(items(m1), ::testing::UnorderedElementsAreArray(v1));
EXPECT_THAT(items(m2), ::testing::UnorderedElementsAreArray(v2));
m1.swap(m2);
EXPECT_THAT(items(m1), ::testing::UnorderedElementsAreArray(v2));
EXPECT_THAT(items(m2), ::testing::UnorderedElementsAreArray(v1));
}
// TODO(alkis): Write tests for extract.
// TODO(alkis): Write tests for merge.
REGISTER_TYPED_TEST_SUITE_P(ModifiersTest, Clear, Insert, InsertHint,
InsertRange, InsertWithinCapacity,
InsertRangeWithinCapacity, InsertOrAssign,
InsertOrAssignHint, Emplace, EmplaceHint,
TryEmplace, TryEmplaceHint, Erase, EraseRange,
EraseKey, Swap);
template <typename Type>
struct is_unique_ptr : std::false_type {};
template <typename Type>
struct is_unique_ptr<std::unique_ptr<Type>> : std::true_type {};
template <class UnordMap>
class UniquePtrModifiersTest : public ::testing::Test {
protected:
UniquePtrModifiersTest() {
static_assert(is_unique_ptr<typename UnordMap::mapped_type>::value,
"UniquePtrModifiersTyest may only be called with a "
"std::unique_ptr value type.");
}
};
GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(UniquePtrModifiersTest);
TYPED_TEST_SUITE_P(UniquePtrModifiersTest);
// Test that we do not move from rvalue arguments if an insertion does not
// happen.
TYPED_TEST_P(UniquePtrModifiersTest, TryEmplace) {
#ifdef UNORDERED_MAP_CXX17
using T = hash_internal::GeneratedType<TypeParam>;
using V = typename TypeParam::mapped_type;
T val = hash_internal::Generator<T>()();
TypeParam m;
auto p = m.try_emplace(val.first, std::move(val.second));
EXPECT_TRUE(p.second);
// A moved from std::unique_ptr is guaranteed to be nullptr.
EXPECT_EQ(val.second, nullptr);
T val2 = {val.first, hash_internal::Generator<V>()()};
p = m.try_emplace(val2.first, std::move(val2.second));
EXPECT_FALSE(p.second);
EXPECT_NE(val2.second, nullptr);
#endif
}
REGISTER_TYPED_TEST_SUITE_P(UniquePtrModifiersTest, TryEmplace);
} // namespace container_internal
ABSL_NAMESPACE_END
} // namespace absl
#endif // ABSL_CONTAINER_INTERNAL_UNORDERED_MAP_MODIFIERS_TEST_H_

View file

@ -0,0 +1,50 @@
// Copyright 2018 The Abseil Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include <memory>
#include <unordered_map>
#include "absl/container/internal/unordered_map_constructor_test.h"
#include "absl/container/internal/unordered_map_lookup_test.h"
#include "absl/container/internal/unordered_map_members_test.h"
#include "absl/container/internal/unordered_map_modifiers_test.h"
namespace absl {
ABSL_NAMESPACE_BEGIN
namespace container_internal {
namespace {
using MapTypes = ::testing::Types<
std::unordered_map<int, int, StatefulTestingHash, StatefulTestingEqual,
Alloc<std::pair<const int, int>>>,
std::unordered_map<std::string, std::string, StatefulTestingHash,
StatefulTestingEqual,
Alloc<std::pair<const std::string, std::string>>>>;
INSTANTIATE_TYPED_TEST_SUITE_P(UnorderedMap, ConstructorTest, MapTypes);
INSTANTIATE_TYPED_TEST_SUITE_P(UnorderedMap, LookupTest, MapTypes);
INSTANTIATE_TYPED_TEST_SUITE_P(UnorderedMap, MembersTest, MapTypes);
INSTANTIATE_TYPED_TEST_SUITE_P(UnorderedMap, ModifiersTest, MapTypes);
using UniquePtrMapTypes = ::testing::Types<std::unordered_map<
int, std::unique_ptr<int>, StatefulTestingHash, StatefulTestingEqual,
Alloc<std::pair<const int, std::unique_ptr<int>>>>>;
INSTANTIATE_TYPED_TEST_SUITE_P(UnorderedMap, UniquePtrModifiersTest,
UniquePtrMapTypes);
} // namespace
} // namespace container_internal
ABSL_NAMESPACE_END
} // namespace absl

View file

@ -0,0 +1,496 @@
// Copyright 2018 The Abseil Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef ABSL_CONTAINER_INTERNAL_UNORDERED_SET_CONSTRUCTOR_TEST_H_
#define ABSL_CONTAINER_INTERNAL_UNORDERED_SET_CONSTRUCTOR_TEST_H_
#include <algorithm>
#include <unordered_set>
#include <vector>
#include "gmock/gmock.h"
#include "gtest/gtest.h"
#include "absl/container/internal/hash_generator_testing.h"
#include "absl/container/internal/hash_policy_testing.h"
#include "absl/meta/type_traits.h"
namespace absl {
ABSL_NAMESPACE_BEGIN
namespace container_internal {
template <class UnordMap>
class ConstructorTest : public ::testing::Test {};
TYPED_TEST_SUITE_P(ConstructorTest);
TYPED_TEST_P(ConstructorTest, NoArgs) {
TypeParam m;
EXPECT_TRUE(m.empty());
EXPECT_THAT(keys(m), ::testing::UnorderedElementsAre());
}
TYPED_TEST_P(ConstructorTest, BucketCount) {
TypeParam m(123);
EXPECT_TRUE(m.empty());
EXPECT_THAT(keys(m), ::testing::UnorderedElementsAre());
EXPECT_GE(m.bucket_count(), 123);
}
TYPED_TEST_P(ConstructorTest, BucketCountHash) {
using H = typename TypeParam::hasher;
H hasher;
TypeParam m(123, hasher);
EXPECT_EQ(m.hash_function(), hasher);
EXPECT_TRUE(m.empty());
EXPECT_THAT(keys(m), ::testing::UnorderedElementsAre());
EXPECT_GE(m.bucket_count(), 123);
}
TYPED_TEST_P(ConstructorTest, BucketCountHashEqual) {
using H = typename TypeParam::hasher;
using E = typename TypeParam::key_equal;
H hasher;
E equal;
TypeParam m(123, hasher, equal);
EXPECT_EQ(m.hash_function(), hasher);
EXPECT_EQ(m.key_eq(), equal);
EXPECT_TRUE(m.empty());
EXPECT_THAT(keys(m), ::testing::UnorderedElementsAre());
EXPECT_GE(m.bucket_count(), 123);
}
TYPED_TEST_P(ConstructorTest, BucketCountHashEqualAlloc) {
using H = typename TypeParam::hasher;
using E = typename TypeParam::key_equal;
using A = typename TypeParam::allocator_type;
H hasher;
E equal;
A alloc(0);
TypeParam m(123, hasher, equal, alloc);
EXPECT_EQ(m.hash_function(), hasher);
EXPECT_EQ(m.key_eq(), equal);
EXPECT_EQ(m.get_allocator(), alloc);
EXPECT_TRUE(m.empty());
EXPECT_THAT(keys(m), ::testing::UnorderedElementsAre());
EXPECT_GE(m.bucket_count(), 123);
const auto& cm = m;
EXPECT_EQ(cm.hash_function(), hasher);
EXPECT_EQ(cm.key_eq(), equal);
EXPECT_EQ(cm.get_allocator(), alloc);
EXPECT_TRUE(cm.empty());
EXPECT_THAT(keys(cm), ::testing::UnorderedElementsAre());
EXPECT_GE(cm.bucket_count(), 123);
}
template <typename T>
struct is_std_unordered_set : std::false_type {};
template <typename... T>
struct is_std_unordered_set<std::unordered_set<T...>> : std::true_type {};
#if defined(UNORDERED_SET_CXX14) || defined(UNORDERED_SET_CXX17)
using has_cxx14_std_apis = std::true_type;
#else
using has_cxx14_std_apis = std::false_type;
#endif
template <typename T>
using expect_cxx14_apis =
absl::disjunction<absl::negation<is_std_unordered_set<T>>,
has_cxx14_std_apis>;
template <typename TypeParam>
void BucketCountAllocTest(std::false_type) {}
template <typename TypeParam>
void BucketCountAllocTest(std::true_type) {
using A = typename TypeParam::allocator_type;
A alloc(0);
TypeParam m(123, alloc);
EXPECT_EQ(m.get_allocator(), alloc);
EXPECT_TRUE(m.empty());
EXPECT_THAT(keys(m), ::testing::UnorderedElementsAre());
EXPECT_GE(m.bucket_count(), 123);
}
TYPED_TEST_P(ConstructorTest, BucketCountAlloc) {
BucketCountAllocTest<TypeParam>(expect_cxx14_apis<TypeParam>());
}
template <typename TypeParam>
void BucketCountHashAllocTest(std::false_type) {}
template <typename TypeParam>
void BucketCountHashAllocTest(std::true_type) {
using H = typename TypeParam::hasher;
using A = typename TypeParam::allocator_type;
H hasher;
A alloc(0);
TypeParam m(123, hasher, alloc);
EXPECT_EQ(m.hash_function(), hasher);
EXPECT_EQ(m.get_allocator(), alloc);
EXPECT_TRUE(m.empty());
EXPECT_THAT(keys(m), ::testing::UnorderedElementsAre());
EXPECT_GE(m.bucket_count(), 123);
}
TYPED_TEST_P(ConstructorTest, BucketCountHashAlloc) {
BucketCountHashAllocTest<TypeParam>(expect_cxx14_apis<TypeParam>());
}
#if ABSL_UNORDERED_SUPPORTS_ALLOC_CTORS
using has_alloc_std_constructors = std::true_type;
#else
using has_alloc_std_constructors = std::false_type;
#endif
template <typename T>
using expect_alloc_constructors =
absl::disjunction<absl::negation<is_std_unordered_set<T>>,
has_alloc_std_constructors>;
template <typename TypeParam>
void AllocTest(std::false_type) {}
template <typename TypeParam>
void AllocTest(std::true_type) {
using A = typename TypeParam::allocator_type;
A alloc(0);
TypeParam m(alloc);
EXPECT_EQ(m.get_allocator(), alloc);
EXPECT_TRUE(m.empty());
EXPECT_THAT(keys(m), ::testing::UnorderedElementsAre());
}
TYPED_TEST_P(ConstructorTest, Alloc) {
AllocTest<TypeParam>(expect_alloc_constructors<TypeParam>());
}
TYPED_TEST_P(ConstructorTest, InputIteratorBucketHashEqualAlloc) {
using T = hash_internal::GeneratedType<TypeParam>;
using H = typename TypeParam::hasher;
using E = typename TypeParam::key_equal;
using A = typename TypeParam::allocator_type;
H hasher;
E equal;
A alloc(0);
std::vector<T> values;
for (size_t i = 0; i != 10; ++i)
values.push_back(hash_internal::Generator<T>()());
TypeParam m(values.begin(), values.end(), 123, hasher, equal, alloc);
EXPECT_EQ(m.hash_function(), hasher);
EXPECT_EQ(m.key_eq(), equal);
EXPECT_EQ(m.get_allocator(), alloc);
EXPECT_THAT(keys(m), ::testing::UnorderedElementsAreArray(values));
EXPECT_GE(m.bucket_count(), 123);
}
template <typename TypeParam>
void InputIteratorBucketAllocTest(std::false_type) {}
template <typename TypeParam>
void InputIteratorBucketAllocTest(std::true_type) {
using T = hash_internal::GeneratedType<TypeParam>;
using A = typename TypeParam::allocator_type;
A alloc(0);
std::vector<T> values;
for (size_t i = 0; i != 10; ++i)
values.push_back(hash_internal::Generator<T>()());
TypeParam m(values.begin(), values.end(), 123, alloc);
EXPECT_EQ(m.get_allocator(), alloc);
EXPECT_THAT(keys(m), ::testing::UnorderedElementsAreArray(values));
EXPECT_GE(m.bucket_count(), 123);
}
TYPED_TEST_P(ConstructorTest, InputIteratorBucketAlloc) {
InputIteratorBucketAllocTest<TypeParam>(expect_cxx14_apis<TypeParam>());
}
template <typename TypeParam>
void InputIteratorBucketHashAllocTest(std::false_type) {}
template <typename TypeParam>
void InputIteratorBucketHashAllocTest(std::true_type) {
using T = hash_internal::GeneratedType<TypeParam>;
using H = typename TypeParam::hasher;
using A = typename TypeParam::allocator_type;
H hasher;
A alloc(0);
std::vector<T> values;
for (size_t i = 0; i != 10; ++i)
values.push_back(hash_internal::Generator<T>()());
TypeParam m(values.begin(), values.end(), 123, hasher, alloc);
EXPECT_EQ(m.hash_function(), hasher);
EXPECT_EQ(m.get_allocator(), alloc);
EXPECT_THAT(keys(m), ::testing::UnorderedElementsAreArray(values));
EXPECT_GE(m.bucket_count(), 123);
}
TYPED_TEST_P(ConstructorTest, InputIteratorBucketHashAlloc) {
InputIteratorBucketHashAllocTest<TypeParam>(expect_cxx14_apis<TypeParam>());
}
TYPED_TEST_P(ConstructorTest, CopyConstructor) {
using T = hash_internal::GeneratedType<TypeParam>;
using H = typename TypeParam::hasher;
using E = typename TypeParam::key_equal;
using A = typename TypeParam::allocator_type;
H hasher;
E equal;
A alloc(0);
TypeParam m(123, hasher, equal, alloc);
for (size_t i = 0; i != 10; ++i) m.insert(hash_internal::Generator<T>()());
TypeParam n(m);
EXPECT_EQ(m.hash_function(), n.hash_function());
EXPECT_EQ(m.key_eq(), n.key_eq());
EXPECT_EQ(m.get_allocator(), n.get_allocator());
EXPECT_EQ(m, n);
EXPECT_NE(TypeParam(0, hasher, equal, alloc), n);
}
template <typename TypeParam>
void CopyConstructorAllocTest(std::false_type) {}
template <typename TypeParam>
void CopyConstructorAllocTest(std::true_type) {
using T = hash_internal::GeneratedType<TypeParam>;
using H = typename TypeParam::hasher;
using E = typename TypeParam::key_equal;
using A = typename TypeParam::allocator_type;
H hasher;
E equal;
A alloc(0);
TypeParam m(123, hasher, equal, alloc);
for (size_t i = 0; i != 10; ++i) m.insert(hash_internal::Generator<T>()());
TypeParam n(m, A(11));
EXPECT_EQ(m.hash_function(), n.hash_function());
EXPECT_EQ(m.key_eq(), n.key_eq());
EXPECT_NE(m.get_allocator(), n.get_allocator());
EXPECT_EQ(m, n);
}
TYPED_TEST_P(ConstructorTest, CopyConstructorAlloc) {
CopyConstructorAllocTest<TypeParam>(expect_alloc_constructors<TypeParam>());
}
// TODO(alkis): Test non-propagating allocators on copy constructors.
TYPED_TEST_P(ConstructorTest, MoveConstructor) {
using T = hash_internal::GeneratedType<TypeParam>;
using H = typename TypeParam::hasher;
using E = typename TypeParam::key_equal;
using A = typename TypeParam::allocator_type;
H hasher;
E equal;
A alloc(0);
TypeParam m(123, hasher, equal, alloc);
for (size_t i = 0; i != 10; ++i) m.insert(hash_internal::Generator<T>()());
TypeParam t(m);
TypeParam n(std::move(t));
EXPECT_EQ(m.hash_function(), n.hash_function());
EXPECT_EQ(m.key_eq(), n.key_eq());
EXPECT_EQ(m.get_allocator(), n.get_allocator());
EXPECT_EQ(m, n);
}
template <typename TypeParam>
void MoveConstructorAllocTest(std::false_type) {}
template <typename TypeParam>
void MoveConstructorAllocTest(std::true_type) {
using T = hash_internal::GeneratedType<TypeParam>;
using H = typename TypeParam::hasher;
using E = typename TypeParam::key_equal;
using A = typename TypeParam::allocator_type;
H hasher;
E equal;
A alloc(0);
TypeParam m(123, hasher, equal, alloc);
for (size_t i = 0; i != 10; ++i) m.insert(hash_internal::Generator<T>()());
TypeParam t(m);
TypeParam n(std::move(t), A(1));
EXPECT_EQ(m.hash_function(), n.hash_function());
EXPECT_EQ(m.key_eq(), n.key_eq());
EXPECT_NE(m.get_allocator(), n.get_allocator());
EXPECT_EQ(m, n);
}
TYPED_TEST_P(ConstructorTest, MoveConstructorAlloc) {
MoveConstructorAllocTest<TypeParam>(expect_alloc_constructors<TypeParam>());
}
// TODO(alkis): Test non-propagating allocators on move constructors.
TYPED_TEST_P(ConstructorTest, InitializerListBucketHashEqualAlloc) {
using T = hash_internal::GeneratedType<TypeParam>;
hash_internal::Generator<T> gen;
std::initializer_list<T> values = {gen(), gen(), gen(), gen(), gen()};
using H = typename TypeParam::hasher;
using E = typename TypeParam::key_equal;
using A = typename TypeParam::allocator_type;
H hasher;
E equal;
A alloc(0);
TypeParam m(values, 123, hasher, equal, alloc);
EXPECT_EQ(m.hash_function(), hasher);
EXPECT_EQ(m.key_eq(), equal);
EXPECT_EQ(m.get_allocator(), alloc);
EXPECT_THAT(keys(m), ::testing::UnorderedElementsAreArray(values));
EXPECT_GE(m.bucket_count(), 123);
}
template <typename TypeParam>
void InitializerListBucketAllocTest(std::false_type) {}
template <typename TypeParam>
void InitializerListBucketAllocTest(std::true_type) {
using T = hash_internal::GeneratedType<TypeParam>;
using A = typename TypeParam::allocator_type;
hash_internal::Generator<T> gen;
std::initializer_list<T> values = {gen(), gen(), gen(), gen(), gen()};
A alloc(0);
TypeParam m(values, 123, alloc);
EXPECT_EQ(m.get_allocator(), alloc);
EXPECT_THAT(keys(m), ::testing::UnorderedElementsAreArray(values));
EXPECT_GE(m.bucket_count(), 123);
}
TYPED_TEST_P(ConstructorTest, InitializerListBucketAlloc) {
InitializerListBucketAllocTest<TypeParam>(expect_cxx14_apis<TypeParam>());
}
template <typename TypeParam>
void InitializerListBucketHashAllocTest(std::false_type) {}
template <typename TypeParam>
void InitializerListBucketHashAllocTest(std::true_type) {
using T = hash_internal::GeneratedType<TypeParam>;
using H = typename TypeParam::hasher;
using A = typename TypeParam::allocator_type;
H hasher;
A alloc(0);
hash_internal::Generator<T> gen;
std::initializer_list<T> values = {gen(), gen(), gen(), gen(), gen()};
TypeParam m(values, 123, hasher, alloc);
EXPECT_EQ(m.hash_function(), hasher);
EXPECT_EQ(m.get_allocator(), alloc);
EXPECT_THAT(keys(m), ::testing::UnorderedElementsAreArray(values));
EXPECT_GE(m.bucket_count(), 123);
}
TYPED_TEST_P(ConstructorTest, InitializerListBucketHashAlloc) {
InitializerListBucketHashAllocTest<TypeParam>(expect_cxx14_apis<TypeParam>());
}
TYPED_TEST_P(ConstructorTest, CopyAssignment) {
using T = hash_internal::GeneratedType<TypeParam>;
using H = typename TypeParam::hasher;
using E = typename TypeParam::key_equal;
using A = typename TypeParam::allocator_type;
H hasher;
E equal;
A alloc(0);
hash_internal::Generator<T> gen;
TypeParam m({gen(), gen(), gen()}, 123, hasher, equal, alloc);
TypeParam n;
n = m;
EXPECT_EQ(m.hash_function(), n.hash_function());
EXPECT_EQ(m.key_eq(), n.key_eq());
EXPECT_EQ(m, n);
}
// TODO(alkis): Test [non-]propagating allocators on move/copy assignments
// (it depends on traits).
TYPED_TEST_P(ConstructorTest, MoveAssignment) {
using T = hash_internal::GeneratedType<TypeParam>;
using H = typename TypeParam::hasher;
using E = typename TypeParam::key_equal;
using A = typename TypeParam::allocator_type;
H hasher;
E equal;
A alloc(0);
hash_internal::Generator<T> gen;
TypeParam m({gen(), gen(), gen()}, 123, hasher, equal, alloc);
TypeParam t(m);
TypeParam n;
n = std::move(t);
EXPECT_EQ(m.hash_function(), n.hash_function());
EXPECT_EQ(m.key_eq(), n.key_eq());
EXPECT_EQ(m, n);
}
TYPED_TEST_P(ConstructorTest, AssignmentFromInitializerList) {
using T = hash_internal::GeneratedType<TypeParam>;
hash_internal::Generator<T> gen;
std::initializer_list<T> values = {gen(), gen(), gen(), gen(), gen()};
TypeParam m;
m = values;
EXPECT_THAT(keys(m), ::testing::UnorderedElementsAreArray(values));
}
TYPED_TEST_P(ConstructorTest, AssignmentOverwritesExisting) {
using T = hash_internal::GeneratedType<TypeParam>;
hash_internal::Generator<T> gen;
TypeParam m({gen(), gen(), gen()});
TypeParam n({gen()});
n = m;
EXPECT_EQ(m, n);
}
TYPED_TEST_P(ConstructorTest, MoveAssignmentOverwritesExisting) {
using T = hash_internal::GeneratedType<TypeParam>;
hash_internal::Generator<T> gen;
TypeParam m({gen(), gen(), gen()});
TypeParam t(m);
TypeParam n({gen()});
n = std::move(t);
EXPECT_EQ(m, n);
}
TYPED_TEST_P(ConstructorTest, AssignmentFromInitializerListOverwritesExisting) {
using T = hash_internal::GeneratedType<TypeParam>;
hash_internal::Generator<T> gen;
std::initializer_list<T> values = {gen(), gen(), gen(), gen(), gen()};
TypeParam m;
m = values;
EXPECT_THAT(keys(m), ::testing::UnorderedElementsAreArray(values));
}
TYPED_TEST_P(ConstructorTest, AssignmentOnSelf) {
using T = hash_internal::GeneratedType<TypeParam>;
hash_internal::Generator<T> gen;
std::initializer_list<T> values = {gen(), gen(), gen(), gen(), gen()};
TypeParam m(values);
m = *&m; // Avoid -Wself-assign.
EXPECT_THAT(keys(m), ::testing::UnorderedElementsAreArray(values));
}
REGISTER_TYPED_TEST_SUITE_P(
ConstructorTest, NoArgs, BucketCount, BucketCountHash, BucketCountHashEqual,
BucketCountHashEqualAlloc, BucketCountAlloc, BucketCountHashAlloc, Alloc,
InputIteratorBucketHashEqualAlloc, InputIteratorBucketAlloc,
InputIteratorBucketHashAlloc, CopyConstructor, CopyConstructorAlloc,
MoveConstructor, MoveConstructorAlloc, InitializerListBucketHashEqualAlloc,
InitializerListBucketAlloc, InitializerListBucketHashAlloc, CopyAssignment,
MoveAssignment, AssignmentFromInitializerList, AssignmentOverwritesExisting,
MoveAssignmentOverwritesExisting,
AssignmentFromInitializerListOverwritesExisting, AssignmentOnSelf);
} // namespace container_internal
ABSL_NAMESPACE_END
} // namespace absl
#endif // ABSL_CONTAINER_INTERNAL_UNORDERED_SET_CONSTRUCTOR_TEST_H_

View file

@ -0,0 +1,91 @@
// Copyright 2018 The Abseil Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef ABSL_CONTAINER_INTERNAL_UNORDERED_SET_LOOKUP_TEST_H_
#define ABSL_CONTAINER_INTERNAL_UNORDERED_SET_LOOKUP_TEST_H_
#include "gmock/gmock.h"
#include "gtest/gtest.h"
#include "absl/container/internal/hash_generator_testing.h"
#include "absl/container/internal/hash_policy_testing.h"
namespace absl {
ABSL_NAMESPACE_BEGIN
namespace container_internal {
template <class UnordSet>
class LookupTest : public ::testing::Test {};
TYPED_TEST_SUITE_P(LookupTest);
TYPED_TEST_P(LookupTest, Count) {
using T = hash_internal::GeneratedType<TypeParam>;
std::vector<T> values;
std::generate_n(std::back_inserter(values), 10,
hash_internal::Generator<T>());
TypeParam m;
for (const auto& v : values)
EXPECT_EQ(0, m.count(v)) << ::testing::PrintToString(v);
m.insert(values.begin(), values.end());
for (const auto& v : values)
EXPECT_EQ(1, m.count(v)) << ::testing::PrintToString(v);
}
TYPED_TEST_P(LookupTest, Find) {
using T = hash_internal::GeneratedType<TypeParam>;
std::vector<T> values;
std::generate_n(std::back_inserter(values), 10,
hash_internal::Generator<T>());
TypeParam m;
for (const auto& v : values)
EXPECT_TRUE(m.end() == m.find(v)) << ::testing::PrintToString(v);
m.insert(values.begin(), values.end());
for (const auto& v : values) {
typename TypeParam::iterator it = m.find(v);
static_assert(std::is_same<const typename TypeParam::value_type&,
decltype(*it)>::value,
"");
static_assert(std::is_same<const typename TypeParam::value_type*,
decltype(it.operator->())>::value,
"");
EXPECT_TRUE(m.end() != it) << ::testing::PrintToString(v);
EXPECT_EQ(v, *it) << ::testing::PrintToString(v);
}
}
TYPED_TEST_P(LookupTest, EqualRange) {
using T = hash_internal::GeneratedType<TypeParam>;
std::vector<T> values;
std::generate_n(std::back_inserter(values), 10,
hash_internal::Generator<T>());
TypeParam m;
for (const auto& v : values) {
auto r = m.equal_range(v);
ASSERT_EQ(0, std::distance(r.first, r.second));
}
m.insert(values.begin(), values.end());
for (const auto& v : values) {
auto r = m.equal_range(v);
ASSERT_EQ(1, std::distance(r.first, r.second));
EXPECT_EQ(v, *r.first);
}
}
REGISTER_TYPED_TEST_SUITE_P(LookupTest, Count, Find, EqualRange);
} // namespace container_internal
ABSL_NAMESPACE_END
} // namespace absl
#endif // ABSL_CONTAINER_INTERNAL_UNORDERED_SET_LOOKUP_TEST_H_

View file

@ -0,0 +1,86 @@
// Copyright 2019 The Abseil Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef ABSL_CONTAINER_INTERNAL_UNORDERED_SET_MEMBERS_TEST_H_
#define ABSL_CONTAINER_INTERNAL_UNORDERED_SET_MEMBERS_TEST_H_
#include <type_traits>
#include "gmock/gmock.h"
#include "gtest/gtest.h"
#include "absl/meta/type_traits.h"
namespace absl {
ABSL_NAMESPACE_BEGIN
namespace container_internal {
template <class UnordSet>
class MembersTest : public ::testing::Test {};
TYPED_TEST_SUITE_P(MembersTest);
template <typename T>
void UseType() {}
TYPED_TEST_P(MembersTest, Typedefs) {
EXPECT_TRUE((std::is_same<typename TypeParam::key_type,
typename TypeParam::value_type>()));
EXPECT_TRUE((absl::conjunction<
absl::negation<std::is_signed<typename TypeParam::size_type>>,
std::is_integral<typename TypeParam::size_type>>()));
EXPECT_TRUE((absl::conjunction<
std::is_signed<typename TypeParam::difference_type>,
std::is_integral<typename TypeParam::difference_type>>()));
EXPECT_TRUE((std::is_convertible<
decltype(std::declval<const typename TypeParam::hasher&>()(
std::declval<const typename TypeParam::key_type&>())),
size_t>()));
EXPECT_TRUE((std::is_convertible<
decltype(std::declval<const typename TypeParam::key_equal&>()(
std::declval<const typename TypeParam::key_type&>(),
std::declval<const typename TypeParam::key_type&>())),
bool>()));
EXPECT_TRUE((std::is_same<typename TypeParam::allocator_type::value_type,
typename TypeParam::value_type>()));
EXPECT_TRUE((std::is_same<typename TypeParam::value_type&,
typename TypeParam::reference>()));
EXPECT_TRUE((std::is_same<const typename TypeParam::value_type&,
typename TypeParam::const_reference>()));
EXPECT_TRUE((std::is_same<typename std::allocator_traits<
typename TypeParam::allocator_type>::pointer,
typename TypeParam::pointer>()));
EXPECT_TRUE(
(std::is_same<typename std::allocator_traits<
typename TypeParam::allocator_type>::const_pointer,
typename TypeParam::const_pointer>()));
}
TYPED_TEST_P(MembersTest, SimpleFunctions) {
EXPECT_GT(TypeParam().max_size(), 0);
}
TYPED_TEST_P(MembersTest, BeginEnd) {
TypeParam t = {typename TypeParam::value_type{}};
EXPECT_EQ(t.begin(), t.cbegin());
EXPECT_EQ(t.end(), t.cend());
EXPECT_NE(t.begin(), t.end());
EXPECT_NE(t.cbegin(), t.cend());
}
REGISTER_TYPED_TEST_SUITE_P(MembersTest, Typedefs, SimpleFunctions, BeginEnd);
} // namespace container_internal
ABSL_NAMESPACE_END
} // namespace absl
#endif // ABSL_CONTAINER_INTERNAL_UNORDERED_SET_MEMBERS_TEST_H_

View file

@ -0,0 +1,221 @@
// Copyright 2018 The Abseil Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef ABSL_CONTAINER_INTERNAL_UNORDERED_SET_MODIFIERS_TEST_H_
#define ABSL_CONTAINER_INTERNAL_UNORDERED_SET_MODIFIERS_TEST_H_
#include "gmock/gmock.h"
#include "gtest/gtest.h"
#include "absl/container/internal/hash_generator_testing.h"
#include "absl/container/internal/hash_policy_testing.h"
namespace absl {
ABSL_NAMESPACE_BEGIN
namespace container_internal {
template <class UnordSet>
class ModifiersTest : public ::testing::Test {};
TYPED_TEST_SUITE_P(ModifiersTest);
TYPED_TEST_P(ModifiersTest, Clear) {
using T = hash_internal::GeneratedType<TypeParam>;
std::vector<T> values;
std::generate_n(std::back_inserter(values), 10,
hash_internal::Generator<T>());
TypeParam m(values.begin(), values.end());
ASSERT_THAT(keys(m), ::testing::UnorderedElementsAreArray(values));
m.clear();
EXPECT_THAT(keys(m), ::testing::UnorderedElementsAre());
EXPECT_TRUE(m.empty());
}
TYPED_TEST_P(ModifiersTest, Insert) {
using T = hash_internal::GeneratedType<TypeParam>;
T val = hash_internal::Generator<T>()();
TypeParam m;
auto p = m.insert(val);
EXPECT_TRUE(p.second);
EXPECT_EQ(val, *p.first);
p = m.insert(val);
EXPECT_FALSE(p.second);
}
TYPED_TEST_P(ModifiersTest, InsertHint) {
using T = hash_internal::GeneratedType<TypeParam>;
T val = hash_internal::Generator<T>()();
TypeParam m;
auto it = m.insert(m.end(), val);
EXPECT_TRUE(it != m.end());
EXPECT_EQ(val, *it);
it = m.insert(it, val);
EXPECT_TRUE(it != m.end());
EXPECT_EQ(val, *it);
}
TYPED_TEST_P(ModifiersTest, InsertRange) {
using T = hash_internal::GeneratedType<TypeParam>;
std::vector<T> values;
std::generate_n(std::back_inserter(values), 10,
hash_internal::Generator<T>());
TypeParam m;
m.insert(values.begin(), values.end());
ASSERT_THAT(keys(m), ::testing::UnorderedElementsAreArray(values));
}
TYPED_TEST_P(ModifiersTest, InsertWithinCapacity) {
using T = hash_internal::GeneratedType<TypeParam>;
T val = hash_internal::Generator<T>()();
TypeParam m;
m.reserve(10);
const size_t original_capacity = m.bucket_count();
m.insert(val);
EXPECT_EQ(m.bucket_count(), original_capacity);
m.insert(val);
EXPECT_EQ(m.bucket_count(), original_capacity);
}
TYPED_TEST_P(ModifiersTest, InsertRangeWithinCapacity) {
#if !defined(__GLIBCXX__)
using T = hash_internal::GeneratedType<TypeParam>;
std::vector<T> base_values;
std::generate_n(std::back_inserter(base_values), 10,
hash_internal::Generator<T>());
std::vector<T> values;
while (values.size() != 100) {
values.insert(values.end(), base_values.begin(), base_values.end());
}
TypeParam m;
m.reserve(10);
const size_t original_capacity = m.bucket_count();
m.insert(values.begin(), values.end());
EXPECT_EQ(m.bucket_count(), original_capacity);
#endif
}
TYPED_TEST_P(ModifiersTest, Emplace) {
using T = hash_internal::GeneratedType<TypeParam>;
T val = hash_internal::Generator<T>()();
TypeParam m;
// TODO(alkis): We need a way to run emplace in a more meaningful way. Perhaps
// with test traits/policy.
auto p = m.emplace(val);
EXPECT_TRUE(p.second);
EXPECT_EQ(val, *p.first);
p = m.emplace(val);
EXPECT_FALSE(p.second);
EXPECT_EQ(val, *p.first);
}
TYPED_TEST_P(ModifiersTest, EmplaceHint) {
using T = hash_internal::GeneratedType<TypeParam>;
T val = hash_internal::Generator<T>()();
TypeParam m;
// TODO(alkis): We need a way to run emplace in a more meaningful way. Perhaps
// with test traits/policy.
auto it = m.emplace_hint(m.end(), val);
EXPECT_EQ(val, *it);
it = m.emplace_hint(it, val);
EXPECT_EQ(val, *it);
}
template <class V>
using IfNotVoid = typename std::enable_if<!std::is_void<V>::value, V>::type;
// In openmap we chose not to return the iterator from erase because that's
// more expensive. As such we adapt erase to return an iterator here.
struct EraseFirst {
template <class Map>
auto operator()(Map* m, int) const
-> IfNotVoid<decltype(m->erase(m->begin()))> {
return m->erase(m->begin());
}
template <class Map>
typename Map::iterator operator()(Map* m, ...) const {
auto it = m->begin();
m->erase(it++);
return it;
}
};
TYPED_TEST_P(ModifiersTest, Erase) {
using T = hash_internal::GeneratedType<TypeParam>;
std::vector<T> values;
std::generate_n(std::back_inserter(values), 10,
hash_internal::Generator<T>());
TypeParam m(values.begin(), values.end());
ASSERT_THAT(keys(m), ::testing::UnorderedElementsAreArray(values));
std::vector<T> values2;
for (const auto& val : values)
if (val != *m.begin()) values2.push_back(val);
auto it = EraseFirst()(&m, 0);
ASSERT_TRUE(it != m.end());
EXPECT_EQ(1, std::count(values2.begin(), values2.end(), *it));
EXPECT_THAT(keys(m), ::testing::UnorderedElementsAreArray(values2.begin(),
values2.end()));
}
TYPED_TEST_P(ModifiersTest, EraseRange) {
using T = hash_internal::GeneratedType<TypeParam>;
std::vector<T> values;
std::generate_n(std::back_inserter(values), 10,
hash_internal::Generator<T>());
TypeParam m(values.begin(), values.end());
ASSERT_THAT(keys(m), ::testing::UnorderedElementsAreArray(values));
auto it = m.erase(m.begin(), m.end());
EXPECT_THAT(keys(m), ::testing::UnorderedElementsAre());
EXPECT_TRUE(it == m.end());
}
TYPED_TEST_P(ModifiersTest, EraseKey) {
using T = hash_internal::GeneratedType<TypeParam>;
std::vector<T> values;
std::generate_n(std::back_inserter(values), 10,
hash_internal::Generator<T>());
TypeParam m(values.begin(), values.end());
ASSERT_THAT(keys(m), ::testing::UnorderedElementsAreArray(values));
EXPECT_EQ(1, m.erase(values[0]));
EXPECT_EQ(0, std::count(m.begin(), m.end(), values[0]));
EXPECT_THAT(keys(m), ::testing::UnorderedElementsAreArray(values.begin() + 1,
values.end()));
}
TYPED_TEST_P(ModifiersTest, Swap) {
using T = hash_internal::GeneratedType<TypeParam>;
std::vector<T> v1;
std::vector<T> v2;
std::generate_n(std::back_inserter(v1), 5, hash_internal::Generator<T>());
std::generate_n(std::back_inserter(v2), 5, hash_internal::Generator<T>());
TypeParam m1(v1.begin(), v1.end());
TypeParam m2(v2.begin(), v2.end());
EXPECT_THAT(keys(m1), ::testing::UnorderedElementsAreArray(v1));
EXPECT_THAT(keys(m2), ::testing::UnorderedElementsAreArray(v2));
m1.swap(m2);
EXPECT_THAT(keys(m1), ::testing::UnorderedElementsAreArray(v2));
EXPECT_THAT(keys(m2), ::testing::UnorderedElementsAreArray(v1));
}
// TODO(alkis): Write tests for extract.
// TODO(alkis): Write tests for merge.
REGISTER_TYPED_TEST_SUITE_P(ModifiersTest, Clear, Insert, InsertHint,
InsertRange, InsertWithinCapacity,
InsertRangeWithinCapacity, Emplace, EmplaceHint,
Erase, EraseRange, EraseKey, Swap);
} // namespace container_internal
ABSL_NAMESPACE_END
} // namespace absl
#endif // ABSL_CONTAINER_INTERNAL_UNORDERED_SET_MODIFIERS_TEST_H_

View file

@ -0,0 +1,41 @@
// Copyright 2018 The Abseil Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include <unordered_set>
#include "absl/container/internal/unordered_set_constructor_test.h"
#include "absl/container/internal/unordered_set_lookup_test.h"
#include "absl/container/internal/unordered_set_members_test.h"
#include "absl/container/internal/unordered_set_modifiers_test.h"
namespace absl {
ABSL_NAMESPACE_BEGIN
namespace container_internal {
namespace {
using SetTypes = ::testing::Types<
std::unordered_set<int, StatefulTestingHash, StatefulTestingEqual,
Alloc<int>>,
std::unordered_set<std::string, StatefulTestingHash, StatefulTestingEqual,
Alloc<std::string>>>;
INSTANTIATE_TYPED_TEST_SUITE_P(UnorderedSet, ConstructorTest, SetTypes);
INSTANTIATE_TYPED_TEST_SUITE_P(UnorderedSet, LookupTest, SetTypes);
INSTANTIATE_TYPED_TEST_SUITE_P(UnorderedSet, MembersTest, SetTypes);
INSTANTIATE_TYPED_TEST_SUITE_P(UnorderedSet, ModifiersTest, SetTypes);
} // namespace
} // namespace container_internal
ABSL_NAMESPACE_END
} // namespace absl

View file

@ -0,0 +1,682 @@
// Copyright 2018 The Abseil Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// -----------------------------------------------------------------------------
// File: node_hash_map.h
// -----------------------------------------------------------------------------
//
// An `absl::node_hash_map<K, V>` is an unordered associative container of
// unique keys and associated values designed to be a more efficient replacement
// for `std::unordered_map`. Like `unordered_map`, search, insertion, and
// deletion of map elements can be done as an `O(1)` operation. However,
// `node_hash_map` (and other unordered associative containers known as the
// collection of Abseil "Swiss tables") contain other optimizations that result
// in both memory and computation advantages.
//
// In most cases, your default choice for a hash map should be a map of type
// `flat_hash_map`. However, if you need pointer stability and cannot store
// a `flat_hash_map` with `unique_ptr` elements, a `node_hash_map` may be a
// valid alternative. As well, if you are migrating your code from using
// `std::unordered_map`, a `node_hash_map` provides a more straightforward
// migration, because it guarantees pointer stability. Consider migrating to
// `node_hash_map` and perhaps converting to a more efficient `flat_hash_map`
// upon further review.
//
// `node_hash_map` is not exception-safe.
#ifndef ABSL_CONTAINER_NODE_HASH_MAP_H_
#define ABSL_CONTAINER_NODE_HASH_MAP_H_
#include <cstddef>
#include <memory>
#include <type_traits>
#include <utility>
#include "absl/algorithm/container.h"
#include "absl/base/attributes.h"
#include "absl/container/hash_container_defaults.h"
#include "absl/container/internal/container_memory.h"
#include "absl/container/internal/node_slot_policy.h"
#include "absl/container/internal/raw_hash_map.h" // IWYU pragma: export
#include "absl/memory/memory.h"
#include "absl/meta/type_traits.h"
namespace absl {
ABSL_NAMESPACE_BEGIN
namespace container_internal {
template <class Key, class Value>
class NodeHashMapPolicy;
} // namespace container_internal
// -----------------------------------------------------------------------------
// absl::node_hash_map
// -----------------------------------------------------------------------------
//
// An `absl::node_hash_map<K, V>` is an unordered associative container which
// has been optimized for both speed and memory footprint in most common use
// cases. Its interface is similar to that of `std::unordered_map<K, V>` with
// the following notable differences:
//
// * Supports heterogeneous lookup, through `find()`, `operator[]()` and
// `insert()`, provided that the map is provided a compatible heterogeneous
// hashing function and equality operator. See below for details.
// * Contains a `capacity()` member function indicating the number of element
// slots (open, deleted, and empty) within the hash map.
// * Returns `void` from the `erase(iterator)` overload.
//
// By default, `node_hash_map` uses the `absl::Hash` hashing framework.
// All fundamental and Abseil types that support the `absl::Hash` framework have
// a compatible equality operator for comparing insertions into `node_hash_map`.
// If your type is not yet supported by the `absl::Hash` framework, see
// absl/hash/hash.h for information on extending Abseil hashing to user-defined
// types.
//
// Using `absl::node_hash_map` at interface boundaries in dynamically loaded
// libraries (e.g. .dll, .so) is unsupported due to way `absl::Hash` values may
// be randomized across dynamically loaded libraries.
//
// To achieve heterogeneous lookup for custom types either `Hash` and `Eq` type
// parameters can be used or `T` should have public inner types
// `absl_container_hash` and (optionally) `absl_container_eq`. In either case,
// `typename Hash::is_transparent` and `typename Eq::is_transparent` should be
// well-formed. Both types are basically functors:
// * `Hash` should support `size_t operator()(U val) const` that returns a hash
// for the given `val`.
// * `Eq` should support `bool operator()(U lhs, V rhs) const` that returns true
// if `lhs` is equal to `rhs`.
//
// In most cases `T` needs only to provide the `absl_container_hash`. In this
// case `std::equal_to<void>` will be used instead of `eq` part.
//
// Example:
//
// // Create a node hash map of three strings (that map to strings)
// absl::node_hash_map<std::string, std::string> ducks =
// {{"a", "huey"}, {"b", "dewey"}, {"c", "louie"}};
//
// // Insert a new element into the node hash map
// ducks.insert({"d", "donald"}};
//
// // Force a rehash of the node hash map
// ducks.rehash(0);
//
// // Find the element with the key "b"
// std::string search_key = "b";
// auto result = ducks.find(search_key);
// if (result != ducks.end()) {
// std::cout << "Result: " << result->second << std::endl;
// }
template <class Key, class Value, class Hash = DefaultHashContainerHash<Key>,
class Eq = DefaultHashContainerEq<Key>,
class Alloc = std::allocator<std::pair<const Key, Value>>>
class ABSL_ATTRIBUTE_OWNER node_hash_map
: public absl::container_internal::raw_hash_map<
absl::container_internal::NodeHashMapPolicy<Key, Value>, Hash, Eq,
Alloc> {
using Base = typename node_hash_map::raw_hash_map;
public:
// Constructors and Assignment Operators
//
// A node_hash_map supports the same overload set as `std::unordered_map`
// for construction and assignment:
//
// * Default constructor
//
// // No allocation for the table's elements is made.
// absl::node_hash_map<int, std::string> map1;
//
// * Initializer List constructor
//
// absl::node_hash_map<int, std::string> map2 =
// {{1, "huey"}, {2, "dewey"}, {3, "louie"},};
//
// * Copy constructor
//
// absl::node_hash_map<int, std::string> map3(map2);
//
// * Copy assignment operator
//
// // Hash functor and Comparator are copied as well
// absl::node_hash_map<int, std::string> map4;
// map4 = map3;
//
// * Move constructor
//
// // Move is guaranteed efficient
// absl::node_hash_map<int, std::string> map5(std::move(map4));
//
// * Move assignment operator
//
// // May be efficient if allocators are compatible
// absl::node_hash_map<int, std::string> map6;
// map6 = std::move(map5);
//
// * Range constructor
//
// std::vector<std::pair<int, std::string>> v = {{1, "a"}, {2, "b"}};
// absl::node_hash_map<int, std::string> map7(v.begin(), v.end());
node_hash_map() {}
using Base::Base;
// node_hash_map::begin()
//
// Returns an iterator to the beginning of the `node_hash_map`.
using Base::begin;
// node_hash_map::cbegin()
//
// Returns a const iterator to the beginning of the `node_hash_map`.
using Base::cbegin;
// node_hash_map::cend()
//
// Returns a const iterator to the end of the `node_hash_map`.
using Base::cend;
// node_hash_map::end()
//
// Returns an iterator to the end of the `node_hash_map`.
using Base::end;
// node_hash_map::capacity()
//
// Returns the number of element slots (assigned, deleted, and empty)
// available within the `node_hash_map`.
//
// NOTE: this member function is particular to `absl::node_hash_map` and is
// not provided in the `std::unordered_map` API.
using Base::capacity;
// node_hash_map::empty()
//
// Returns whether or not the `node_hash_map` is empty.
using Base::empty;
// node_hash_map::max_size()
//
// Returns the largest theoretical possible number of elements within a
// `node_hash_map` under current memory constraints. This value can be thought
// of as the largest value of `std::distance(begin(), end())` for a
// `node_hash_map<K, V>`.
using Base::max_size;
// node_hash_map::size()
//
// Returns the number of elements currently within the `node_hash_map`.
using Base::size;
// node_hash_map::clear()
//
// Removes all elements from the `node_hash_map`. Invalidates any references,
// pointers, or iterators referring to contained elements.
//
// NOTE: this operation may shrink the underlying buffer. To avoid shrinking
// the underlying buffer call `erase(begin(), end())`.
using Base::clear;
// node_hash_map::erase()
//
// Erases elements within the `node_hash_map`. Erasing does not trigger a
// rehash. Overloads are listed below.
//
// void erase(const_iterator pos):
//
// Erases the element at `position` of the `node_hash_map`, returning
// `void`.
//
// NOTE: Returning `void` in this case is different than that of STL
// containers in general and `std::unordered_map` in particular (which
// return an iterator to the element following the erased element). If that
// iterator is needed, simply post increment the iterator:
//
// map.erase(it++);
//
//
// iterator erase(const_iterator first, const_iterator last):
//
// Erases the elements in the open interval [`first`, `last`), returning an
// iterator pointing to `last`. The special case of calling
// `erase(begin(), end())` resets the reserved growth such that if
// `reserve(N)` has previously been called and there has been no intervening
// call to `clear()`, then after calling `erase(begin(), end())`, it is safe
// to assume that inserting N elements will not cause a rehash.
//
// size_type erase(const key_type& key):
//
// Erases the element with the matching key, if it exists, returning the
// number of elements erased (0 or 1).
using Base::erase;
// node_hash_map::insert()
//
// Inserts an element of the specified value into the `node_hash_map`,
// returning an iterator pointing to the newly inserted element, provided that
// an element with the given key does not already exist. If rehashing occurs
// due to the insertion, all iterators are invalidated. Overloads are listed
// below.
//
// std::pair<iterator,bool> insert(const init_type& value):
//
// Inserts a value into the `node_hash_map`. Returns a pair consisting of an
// iterator to the inserted element (or to the element that prevented the
// insertion) and a `bool` denoting whether the insertion took place.
//
// std::pair<iterator,bool> insert(T&& value):
// std::pair<iterator,bool> insert(init_type&& value):
//
// Inserts a moveable value into the `node_hash_map`. Returns a `std::pair`
// consisting of an iterator to the inserted element (or to the element that
// prevented the insertion) and a `bool` denoting whether the insertion took
// place.
//
// iterator insert(const_iterator hint, const init_type& value):
// iterator insert(const_iterator hint, T&& value):
// iterator insert(const_iterator hint, init_type&& value);
//
// Inserts a value, using the position of `hint` as a non-binding suggestion
// for where to begin the insertion search. Returns an iterator to the
// inserted element, or to the existing element that prevented the
// insertion.
//
// void insert(InputIterator first, InputIterator last):
//
// Inserts a range of values [`first`, `last`).
//
// NOTE: Although the STL does not specify which element may be inserted if
// multiple keys compare equivalently, for `node_hash_map` we guarantee the
// first match is inserted.
//
// void insert(std::initializer_list<init_type> ilist):
//
// Inserts the elements within the initializer list `ilist`.
//
// NOTE: Although the STL does not specify which element may be inserted if
// multiple keys compare equivalently within the initializer list, for
// `node_hash_map` we guarantee the first match is inserted.
using Base::insert;
// node_hash_map::insert_or_assign()
//
// Inserts an element of the specified value into the `node_hash_map` provided
// that a value with the given key does not already exist, or replaces it with
// the element value if a key for that value already exists, returning an
// iterator pointing to the newly inserted element. If rehashing occurs due to
// the insertion, all iterators are invalidated. Overloads are listed
// below.
//
// std::pair<iterator, bool> insert_or_assign(const init_type& k, T&& obj):
// std::pair<iterator, bool> insert_or_assign(init_type&& k, T&& obj):
//
// Inserts/Assigns (or moves) the element of the specified key into the
// `node_hash_map`.
//
// iterator insert_or_assign(const_iterator hint,
// const init_type& k, T&& obj):
// iterator insert_or_assign(const_iterator hint, init_type&& k, T&& obj):
//
// Inserts/Assigns (or moves) the element of the specified key into the
// `node_hash_map` using the position of `hint` as a non-binding suggestion
// for where to begin the insertion search.
using Base::insert_or_assign;
// node_hash_map::emplace()
//
// Inserts an element of the specified value by constructing it in-place
// within the `node_hash_map`, provided that no element with the given key
// already exists.
//
// The element may be constructed even if there already is an element with the
// key in the container, in which case the newly constructed element will be
// destroyed immediately. Prefer `try_emplace()` unless your key is not
// copyable or moveable.
//
// If rehashing occurs due to the insertion, all iterators are invalidated.
using Base::emplace;
// node_hash_map::emplace_hint()
//
// Inserts an element of the specified value by constructing it in-place
// within the `node_hash_map`, using the position of `hint` as a non-binding
// suggestion for where to begin the insertion search, and only inserts
// provided that no element with the given key already exists.
//
// The element may be constructed even if there already is an element with the
// key in the container, in which case the newly constructed element will be
// destroyed immediately. Prefer `try_emplace()` unless your key is not
// copyable or moveable.
//
// If rehashing occurs due to the insertion, all iterators are invalidated.
using Base::emplace_hint;
// node_hash_map::try_emplace()
//
// Inserts an element of the specified value by constructing it in-place
// within the `node_hash_map`, provided that no element with the given key
// already exists. Unlike `emplace()`, if an element with the given key
// already exists, we guarantee that no element is constructed.
//
// If rehashing occurs due to the insertion, all iterators are invalidated.
// Overloads are listed below.
//
// std::pair<iterator, bool> try_emplace(const key_type& k, Args&&... args):
// std::pair<iterator, bool> try_emplace(key_type&& k, Args&&... args):
//
// Inserts (via copy or move) the element of the specified key into the
// `node_hash_map`.
//
// iterator try_emplace(const_iterator hint,
// const key_type& k, Args&&... args):
// iterator try_emplace(const_iterator hint, key_type&& k, Args&&... args):
//
// Inserts (via copy or move) the element of the specified key into the
// `node_hash_map` using the position of `hint` as a non-binding suggestion
// for where to begin the insertion search.
//
// All `try_emplace()` overloads make the same guarantees regarding rvalue
// arguments as `std::unordered_map::try_emplace()`, namely that these
// functions will not move from rvalue arguments if insertions do not happen.
using Base::try_emplace;
// node_hash_map::extract()
//
// Extracts the indicated element, erasing it in the process, and returns it
// as a C++17-compatible node handle. Overloads are listed below.
//
// node_type extract(const_iterator position):
//
// Extracts the key,value pair of the element at the indicated position and
// returns a node handle owning that extracted data.
//
// node_type extract(const key_type& x):
//
// Extracts the key,value pair of the element with a key matching the passed
// key value and returns a node handle owning that extracted data. If the
// `node_hash_map` does not contain an element with a matching key, this
// function returns an empty node handle.
//
// NOTE: when compiled in an earlier version of C++ than C++17,
// `node_type::key()` returns a const reference to the key instead of a
// mutable reference. We cannot safely return a mutable reference without
// std::launder (which is not available before C++17).
using Base::extract;
// node_hash_map::merge()
//
// Extracts elements from a given `source` node hash map into this
// `node_hash_map`. If the destination `node_hash_map` already contains an
// element with an equivalent key, that element is not extracted.
using Base::merge;
// node_hash_map::swap(node_hash_map& other)
//
// Exchanges the contents of this `node_hash_map` with those of the `other`
// node hash map.
//
// All iterators and references on the `node_hash_map` remain valid, excepting
// for the past-the-end iterator, which is invalidated.
//
// `swap()` requires that the node hash map's hashing and key equivalence
// functions be Swappable, and are exchanged using unqualified calls to
// non-member `swap()`. If the map's allocator has
// `std::allocator_traits<allocator_type>::propagate_on_container_swap::value`
// set to `true`, the allocators are also exchanged using an unqualified call
// to non-member `swap()`; otherwise, the allocators are not swapped.
using Base::swap;
// node_hash_map::rehash(count)
//
// Rehashes the `node_hash_map`, setting the number of slots to be at least
// the passed value. If the new number of slots increases the load factor more
// than the current maximum load factor
// (`count` < `size()` / `max_load_factor()`), then the new number of slots
// will be at least `size()` / `max_load_factor()`.
//
// To force a rehash, pass rehash(0).
using Base::rehash;
// node_hash_map::reserve(count)
//
// Sets the number of slots in the `node_hash_map` to the number needed to
// accommodate at least `count` total elements without exceeding the current
// maximum load factor, and may rehash the container if needed.
using Base::reserve;
// node_hash_map::at()
//
// Returns a reference to the mapped value of the element with key equivalent
// to the passed key.
using Base::at;
// node_hash_map::contains()
//
// Determines whether an element with a key comparing equal to the given `key`
// exists within the `node_hash_map`, returning `true` if so or `false`
// otherwise.
using Base::contains;
// node_hash_map::count(const Key& key) const
//
// Returns the number of elements with a key comparing equal to the given
// `key` within the `node_hash_map`. note that this function will return
// either `1` or `0` since duplicate keys are not allowed within a
// `node_hash_map`.
using Base::count;
// node_hash_map::equal_range()
//
// Returns a closed range [first, last], defined by a `std::pair` of two
// iterators, containing all elements with the passed key in the
// `node_hash_map`.
using Base::equal_range;
// node_hash_map::find()
//
// Finds an element with the passed `key` within the `node_hash_map`.
using Base::find;
// node_hash_map::operator[]()
//
// Returns a reference to the value mapped to the passed key within the
// `node_hash_map`, performing an `insert()` if the key does not already
// exist. If an insertion occurs and results in a rehashing of the container,
// all iterators are invalidated. Otherwise iterators are not affected and
// references are not invalidated. Overloads are listed below.
//
// T& operator[](const Key& key):
//
// Inserts an init_type object constructed in-place if the element with the
// given key does not exist.
//
// T& operator[](Key&& key):
//
// Inserts an init_type object constructed in-place provided that an element
// with the given key does not exist.
using Base::operator[];
// node_hash_map::bucket_count()
//
// Returns the number of "buckets" within the `node_hash_map`.
using Base::bucket_count;
// node_hash_map::load_factor()
//
// Returns the current load factor of the `node_hash_map` (the average number
// of slots occupied with a value within the hash map).
using Base::load_factor;
// node_hash_map::max_load_factor()
//
// Manages the maximum load factor of the `node_hash_map`. Overloads are
// listed below.
//
// float node_hash_map::max_load_factor()
//
// Returns the current maximum load factor of the `node_hash_map`.
//
// void node_hash_map::max_load_factor(float ml)
//
// Sets the maximum load factor of the `node_hash_map` to the passed value.
//
// NOTE: This overload is provided only for API compatibility with the STL;
// `node_hash_map` will ignore any set load factor and manage its rehashing
// internally as an implementation detail.
using Base::max_load_factor;
// node_hash_map::get_allocator()
//
// Returns the allocator function associated with this `node_hash_map`.
using Base::get_allocator;
// node_hash_map::hash_function()
//
// Returns the hashing function used to hash the keys within this
// `node_hash_map`.
using Base::hash_function;
// node_hash_map::key_eq()
//
// Returns the function used for comparing keys equality.
using Base::key_eq;
};
// erase_if(node_hash_map<>, Pred)
//
// Erases all elements that satisfy the predicate `pred` from the container `c`.
// Returns the number of erased elements.
template <typename K, typename V, typename H, typename E, typename A,
typename Predicate>
typename node_hash_map<K, V, H, E, A>::size_type erase_if(
node_hash_map<K, V, H, E, A>& c, Predicate pred) {
return container_internal::EraseIf(pred, &c);
}
// swap(node_hash_map<>, node_hash_map<>)
//
// Swaps the contents of two `node_hash_map` containers.
//
// NOTE: we need to define this function template in order for
// `flat_hash_set::swap` to be called instead of `std::swap`. Even though we
// have `swap(raw_hash_set&, raw_hash_set&)` defined, that function requires a
// derived-to-base conversion, whereas `std::swap` is a function template so
// `std::swap` will be preferred by compiler.
template <typename K, typename V, typename H, typename E, typename A>
void swap(node_hash_map<K, V, H, E, A>& x,
node_hash_map<K, V, H, E, A>& y) noexcept(noexcept(x.swap(y))) {
return x.swap(y);
}
namespace container_internal {
// c_for_each_fast(node_hash_map<>, Function)
//
// Container-based version of the <algorithm> `std::for_each()` function to
// apply a function to a container's elements.
// There is no guarantees on the order of the function calls.
// Erasure and/or insertion of elements in the function is not allowed.
template <typename K, typename V, typename H, typename E, typename A,
typename Function>
decay_t<Function> c_for_each_fast(const node_hash_map<K, V, H, E, A>& c,
Function&& f) {
container_internal::ForEach(f, &c);
return f;
}
template <typename K, typename V, typename H, typename E, typename A,
typename Function>
decay_t<Function> c_for_each_fast(node_hash_map<K, V, H, E, A>& c,
Function&& f) {
container_internal::ForEach(f, &c);
return f;
}
template <typename K, typename V, typename H, typename E, typename A,
typename Function>
decay_t<Function> c_for_each_fast(node_hash_map<K, V, H, E, A>&& c,
Function&& f) {
container_internal::ForEach(f, &c);
return f;
}
} // namespace container_internal
namespace container_internal {
template <class Key, class Value>
class NodeHashMapPolicy
: public absl::container_internal::node_slot_policy<
std::pair<const Key, Value>&, NodeHashMapPolicy<Key, Value>> {
using value_type = std::pair<const Key, Value>;
public:
using key_type = Key;
using mapped_type = Value;
using init_type = std::pair</*non const*/ key_type, mapped_type>;
template <class Allocator, class... Args>
static value_type* new_element(Allocator* alloc, Args&&... args) {
using PairAlloc = typename absl::allocator_traits<
Allocator>::template rebind_alloc<value_type>;
PairAlloc pair_alloc(*alloc);
value_type* res =
absl::allocator_traits<PairAlloc>::allocate(pair_alloc, 1);
absl::allocator_traits<PairAlloc>::construct(pair_alloc, res,
std::forward<Args>(args)...);
return res;
}
template <class Allocator>
static void delete_element(Allocator* alloc, value_type* pair) {
using PairAlloc = typename absl::allocator_traits<
Allocator>::template rebind_alloc<value_type>;
PairAlloc pair_alloc(*alloc);
absl::allocator_traits<PairAlloc>::destroy(pair_alloc, pair);
absl::allocator_traits<PairAlloc>::deallocate(pair_alloc, pair, 1);
}
template <class F, class... Args>
static decltype(absl::container_internal::DecomposePair(
std::declval<F>(), std::declval<Args>()...))
apply(F&& f, Args&&... args) {
return absl::container_internal::DecomposePair(std::forward<F>(f),
std::forward<Args>(args)...);
}
static size_t element_space_used(const value_type*) {
return sizeof(value_type);
}
static Value& value(value_type* elem) { return elem->second; }
static const Value& value(const value_type* elem) { return elem->second; }
template <class Hash>
static constexpr HashSlotFn get_hash_slot_fn() {
return memory_internal::IsLayoutCompatible<Key, Value>::value
? &TypeErasedDerefAndApplyToSlotFn<Hash, Key>
: nullptr;
}
};
} // namespace container_internal
namespace container_algorithm_internal {
// Specialization of trait in absl/algorithm/container.h
template <class Key, class T, class Hash, class KeyEqual, class Allocator>
struct IsUnorderedContainer<
absl::node_hash_map<Key, T, Hash, KeyEqual, Allocator>> : std::true_type {};
} // namespace container_algorithm_internal
ABSL_NAMESPACE_END
} // namespace absl
#endif // ABSL_CONTAINER_NODE_HASH_MAP_H_

View file

@ -0,0 +1,351 @@
// Copyright 2018 The Abseil Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "absl/container/node_hash_map.h"
#include <cstddef>
#include <new>
#include <string>
#include <tuple>
#include <type_traits>
#include <utility>
#include <vector>
#include "gmock/gmock.h"
#include "gtest/gtest.h"
#include "absl/base/config.h"
#include "absl/container/internal/hash_policy_testing.h"
#include "absl/container/internal/tracked.h"
#include "absl/container/internal/unordered_map_constructor_test.h"
#include "absl/container/internal/unordered_map_lookup_test.h"
#include "absl/container/internal/unordered_map_members_test.h"
#include "absl/container/internal/unordered_map_modifiers_test.h"
namespace absl {
ABSL_NAMESPACE_BEGIN
namespace container_internal {
namespace {
using ::testing::Field;
using ::testing::IsEmpty;
using ::testing::Pair;
using ::testing::UnorderedElementsAre;
using ::testing::UnorderedElementsAreArray;
using MapTypes = ::testing::Types<
absl::node_hash_map<int, int, StatefulTestingHash, StatefulTestingEqual,
Alloc<std::pair<const int, int>>>,
absl::node_hash_map<std::string, std::string, StatefulTestingHash,
StatefulTestingEqual,
Alloc<std::pair<const std::string, std::string>>>>;
INSTANTIATE_TYPED_TEST_SUITE_P(NodeHashMap, ConstructorTest, MapTypes);
INSTANTIATE_TYPED_TEST_SUITE_P(NodeHashMap, LookupTest, MapTypes);
INSTANTIATE_TYPED_TEST_SUITE_P(NodeHashMap, MembersTest, MapTypes);
INSTANTIATE_TYPED_TEST_SUITE_P(NodeHashMap, ModifiersTest, MapTypes);
using M = absl::node_hash_map<std::string, Tracked<int>>;
TEST(NodeHashMap, Emplace) {
M m;
Tracked<int> t(53);
m.emplace("a", t);
ASSERT_EQ(0, t.num_moves());
ASSERT_EQ(1, t.num_copies());
m.emplace(std::string("a"), t);
ASSERT_EQ(0, t.num_moves());
ASSERT_EQ(1, t.num_copies());
std::string a("a");
m.emplace(a, t);
ASSERT_EQ(0, t.num_moves());
ASSERT_EQ(1, t.num_copies());
const std::string ca("a");
m.emplace(a, t);
ASSERT_EQ(0, t.num_moves());
ASSERT_EQ(1, t.num_copies());
m.emplace(std::make_pair("a", t));
ASSERT_EQ(0, t.num_moves());
ASSERT_EQ(2, t.num_copies());
m.emplace(std::make_pair(std::string("a"), t));
ASSERT_EQ(0, t.num_moves());
ASSERT_EQ(3, t.num_copies());
std::pair<std::string, Tracked<int>> p("a", t);
ASSERT_EQ(0, t.num_moves());
ASSERT_EQ(4, t.num_copies());
m.emplace(p);
ASSERT_EQ(0, t.num_moves());
ASSERT_EQ(4, t.num_copies());
const std::pair<std::string, Tracked<int>> cp("a", t);
ASSERT_EQ(0, t.num_moves());
ASSERT_EQ(5, t.num_copies());
m.emplace(cp);
ASSERT_EQ(0, t.num_moves());
ASSERT_EQ(5, t.num_copies());
std::pair<const std::string, Tracked<int>> pc("a", t);
ASSERT_EQ(0, t.num_moves());
ASSERT_EQ(6, t.num_copies());
m.emplace(pc);
ASSERT_EQ(0, t.num_moves());
ASSERT_EQ(6, t.num_copies());
const std::pair<const std::string, Tracked<int>> cpc("a", t);
ASSERT_EQ(0, t.num_moves());
ASSERT_EQ(7, t.num_copies());
m.emplace(cpc);
ASSERT_EQ(0, t.num_moves());
ASSERT_EQ(7, t.num_copies());
m.emplace(std::piecewise_construct, std::forward_as_tuple("a"),
std::forward_as_tuple(t));
ASSERT_EQ(0, t.num_moves());
ASSERT_EQ(7, t.num_copies());
m.emplace(std::piecewise_construct, std::forward_as_tuple(std::string("a")),
std::forward_as_tuple(t));
ASSERT_EQ(0, t.num_moves());
ASSERT_EQ(7, t.num_copies());
}
TEST(NodeHashMap, AssignRecursive) {
struct Tree {
// Verify that unordered_map<K, IncompleteType> can be instantiated.
absl::node_hash_map<int, Tree> children;
};
Tree root;
const Tree& child = root.children.emplace().first->second;
// Verify that `lhs = rhs` doesn't read rhs after clearing lhs.
root = child;
}
TEST(FlatHashMap, MoveOnlyKey) {
struct Key {
Key() = default;
Key(Key&&) = default;
Key& operator=(Key&&) = default;
};
struct Eq {
bool operator()(const Key&, const Key&) const { return true; }
};
struct Hash {
size_t operator()(const Key&) const { return 0; }
};
absl::node_hash_map<Key, int, Hash, Eq> m;
m[Key()];
}
struct NonMovableKey {
explicit NonMovableKey(int i) : i(i) {}
NonMovableKey(NonMovableKey&&) = delete;
int i;
};
struct NonMovableKeyHash {
using is_transparent = void;
size_t operator()(const NonMovableKey& k) const { return k.i; }
size_t operator()(int k) const { return k; }
};
struct NonMovableKeyEq {
using is_transparent = void;
bool operator()(const NonMovableKey& a, const NonMovableKey& b) const {
return a.i == b.i;
}
bool operator()(const NonMovableKey& a, int b) const { return a.i == b; }
};
TEST(NodeHashMap, MergeExtractInsert) {
absl::node_hash_map<NonMovableKey, int, NonMovableKeyHash, NonMovableKeyEq>
set1, set2;
set1.emplace(std::piecewise_construct, std::make_tuple(7),
std::make_tuple(-7));
set1.emplace(std::piecewise_construct, std::make_tuple(17),
std::make_tuple(-17));
set2.emplace(std::piecewise_construct, std::make_tuple(7),
std::make_tuple(-70));
set2.emplace(std::piecewise_construct, std::make_tuple(19),
std::make_tuple(-190));
auto Elem = [](int key, int value) {
return Pair(Field(&NonMovableKey::i, key), value);
};
EXPECT_THAT(set1, UnorderedElementsAre(Elem(7, -7), Elem(17, -17)));
EXPECT_THAT(set2, UnorderedElementsAre(Elem(7, -70), Elem(19, -190)));
// NonMovableKey is neither copyable nor movable. We should still be able to
// move nodes around.
static_assert(!std::is_move_constructible<NonMovableKey>::value, "");
set1.merge(set2);
EXPECT_THAT(set1,
UnorderedElementsAre(Elem(7, -7), Elem(17, -17), Elem(19, -190)));
EXPECT_THAT(set2, UnorderedElementsAre(Elem(7, -70)));
auto node = set1.extract(7);
EXPECT_TRUE(node);
EXPECT_EQ(node.key().i, 7);
EXPECT_EQ(node.mapped(), -7);
EXPECT_THAT(set1, UnorderedElementsAre(Elem(17, -17), Elem(19, -190)));
auto insert_result = set2.insert(std::move(node));
EXPECT_FALSE(node);
EXPECT_FALSE(insert_result.inserted);
EXPECT_TRUE(insert_result.node);
EXPECT_EQ(insert_result.node.key().i, 7);
EXPECT_EQ(insert_result.node.mapped(), -7);
EXPECT_THAT(*insert_result.position, Elem(7, -70));
EXPECT_THAT(set2, UnorderedElementsAre(Elem(7, -70)));
node = set1.extract(17);
EXPECT_TRUE(node);
EXPECT_EQ(node.key().i, 17);
EXPECT_EQ(node.mapped(), -17);
EXPECT_THAT(set1, UnorderedElementsAre(Elem(19, -190)));
node.mapped() = 23;
insert_result = set2.insert(std::move(node));
EXPECT_FALSE(node);
EXPECT_TRUE(insert_result.inserted);
EXPECT_FALSE(insert_result.node);
EXPECT_THAT(*insert_result.position, Elem(17, 23));
EXPECT_THAT(set2, UnorderedElementsAre(Elem(7, -70), Elem(17, 23)));
}
bool FirstIsEven(std::pair<const int, int> p) { return p.first % 2 == 0; }
TEST(NodeHashMap, EraseIf) {
// Erase all elements.
{
node_hash_map<int, int> s = {{1, 1}, {2, 2}, {3, 3}, {4, 4}, {5, 5}};
EXPECT_EQ(erase_if(s, [](std::pair<const int, int>) { return true; }), 5);
EXPECT_THAT(s, IsEmpty());
}
// Erase no elements.
{
node_hash_map<int, int> s = {{1, 1}, {2, 2}, {3, 3}, {4, 4}, {5, 5}};
EXPECT_EQ(erase_if(s, [](std::pair<const int, int>) { return false; }), 0);
EXPECT_THAT(s, UnorderedElementsAre(Pair(1, 1), Pair(2, 2), Pair(3, 3),
Pair(4, 4), Pair(5, 5)));
}
// Erase specific elements.
{
node_hash_map<int, int> s = {{1, 1}, {2, 2}, {3, 3}, {4, 4}, {5, 5}};
EXPECT_EQ(erase_if(s,
[](std::pair<const int, int> kvp) {
return kvp.first % 2 == 1;
}),
3);
EXPECT_THAT(s, UnorderedElementsAre(Pair(2, 2), Pair(4, 4)));
}
// Predicate is function reference.
{
node_hash_map<int, int> s = {{1, 1}, {2, 2}, {3, 3}, {4, 4}, {5, 5}};
EXPECT_EQ(erase_if(s, FirstIsEven), 2);
EXPECT_THAT(s, UnorderedElementsAre(Pair(1, 1), Pair(3, 3), Pair(5, 5)));
}
// Predicate is function pointer.
{
node_hash_map<int, int> s = {{1, 1}, {2, 2}, {3, 3}, {4, 4}, {5, 5}};
EXPECT_EQ(erase_if(s, &FirstIsEven), 2);
EXPECT_THAT(s, UnorderedElementsAre(Pair(1, 1), Pair(3, 3), Pair(5, 5)));
}
}
TEST(NodeHashMap, CForEach) {
node_hash_map<int, int> m;
std::vector<std::pair<int, int>> expected;
for (int i = 0; i < 100; ++i) {
{
SCOPED_TRACE("mutable object iteration");
std::vector<std::pair<int, int>> v;
absl::container_internal::c_for_each_fast(
m, [&v](std::pair<const int, int>& p) { v.push_back(p); });
EXPECT_THAT(v, UnorderedElementsAreArray(expected));
}
{
SCOPED_TRACE("const object iteration");
std::vector<std::pair<int, int>> v;
const node_hash_map<int, int>& cm = m;
absl::container_internal::c_for_each_fast(
cm, [&v](const std::pair<const int, int>& p) { v.push_back(p); });
EXPECT_THAT(v, UnorderedElementsAreArray(expected));
}
{
SCOPED_TRACE("const object iteration");
std::vector<std::pair<int, int>> v;
absl::container_internal::c_for_each_fast(
node_hash_map<int, int>(m),
[&v](std::pair<const int, int>& p) { v.push_back(p); });
EXPECT_THAT(v, UnorderedElementsAreArray(expected));
}
m[i] = i;
expected.emplace_back(i, i);
}
}
TEST(NodeHashMap, CForEachMutate) {
node_hash_map<int, int> s;
std::vector<std::pair<int, int>> expected;
for (int i = 0; i < 100; ++i) {
std::vector<std::pair<int, int>> v;
absl::container_internal::c_for_each_fast(
s, [&v](std::pair<const int, int>& p) {
v.push_back(p);
p.second++;
});
EXPECT_THAT(v, UnorderedElementsAreArray(expected));
for (auto& p : expected) {
p.second++;
}
EXPECT_THAT(s, UnorderedElementsAreArray(expected));
s[i] = i;
expected.emplace_back(i, i);
}
}
// This test requires std::launder for mutable key access in node handles.
#if defined(__cpp_lib_launder) && __cpp_lib_launder >= 201606
TEST(NodeHashMap, NodeHandleMutableKeyAccess) {
node_hash_map<std::string, std::string> map;
map["key1"] = "mapped";
auto nh = map.extract(map.begin());
nh.key().resize(3);
map.insert(std::move(nh));
EXPECT_THAT(map, testing::ElementsAre(Pair("key", "mapped")));
}
#endif
TEST(NodeHashMap, RecursiveTypeCompiles) {
struct RecursiveType {
node_hash_map<int, RecursiveType> m;
};
RecursiveType t;
t.m[0] = RecursiveType{};
}
} // namespace
} // namespace container_internal
ABSL_NAMESPACE_END
} // namespace absl

View file

@ -0,0 +1,573 @@
// Copyright 2018 The Abseil Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// -----------------------------------------------------------------------------
// File: node_hash_set.h
// -----------------------------------------------------------------------------
//
// An `absl::node_hash_set<T>` is an unordered associative container designed to
// be a more efficient replacement for `std::unordered_set`. Like
// `unordered_set`, search, insertion, and deletion of set elements can be done
// as an `O(1)` operation. However, `node_hash_set` (and other unordered
// associative containers known as the collection of Abseil "Swiss tables")
// contain other optimizations that result in both memory and computation
// advantages.
//
// In most cases, your default choice for a hash table should be a map of type
// `flat_hash_map` or a set of type `flat_hash_set`. However, if you need
// pointer stability, a `node_hash_set` should be your preferred choice. As
// well, if you are migrating your code from using `std::unordered_set`, a
// `node_hash_set` should be an easy migration. Consider migrating to
// `node_hash_set` and perhaps converting to a more efficient `flat_hash_set`
// upon further review.
//
// `node_hash_set` is not exception-safe.
#ifndef ABSL_CONTAINER_NODE_HASH_SET_H_
#define ABSL_CONTAINER_NODE_HASH_SET_H_
#include <cstddef>
#include <memory>
#include <type_traits>
#include "absl/algorithm/container.h"
#include "absl/base/attributes.h"
#include "absl/container/hash_container_defaults.h"
#include "absl/container/internal/container_memory.h"
#include "absl/container/internal/node_slot_policy.h"
#include "absl/container/internal/raw_hash_set.h" // IWYU pragma: export
#include "absl/memory/memory.h"
#include "absl/meta/type_traits.h"
namespace absl {
ABSL_NAMESPACE_BEGIN
namespace container_internal {
template <typename T>
struct NodeHashSetPolicy;
} // namespace container_internal
// -----------------------------------------------------------------------------
// absl::node_hash_set
// -----------------------------------------------------------------------------
//
// An `absl::node_hash_set<T>` is an unordered associative container which
// has been optimized for both speed and memory footprint in most common use
// cases. Its interface is similar to that of `std::unordered_set<T>` with the
// following notable differences:
//
// * Supports heterogeneous lookup, through `find()`, `operator[]()` and
// `insert()`, provided that the set is provided a compatible heterogeneous
// hashing function and equality operator. See below for details.
// * Contains a `capacity()` member function indicating the number of element
// slots (open, deleted, and empty) within the hash set.
// * Returns `void` from the `erase(iterator)` overload.
//
// By default, `node_hash_set` uses the `absl::Hash` hashing framework.
// All fundamental and Abseil types that support the `absl::Hash` framework have
// a compatible equality operator for comparing insertions into `node_hash_set`.
// If your type is not yet supported by the `absl::Hash` framework, see
// absl/hash/hash.h for information on extending Abseil hashing to user-defined
// types.
//
// Using `absl::node_hash_set` at interface boundaries in dynamically loaded
// libraries (e.g. .dll, .so) is unsupported due to way `absl::Hash` values may
// be randomized across dynamically loaded libraries.
//
// To achieve heterogeneous lookup for custom types either `Hash` and `Eq` type
// parameters can be used or `T` should have public inner types
// `absl_container_hash` and (optionally) `absl_container_eq`. In either case,
// `typename Hash::is_transparent` and `typename Eq::is_transparent` should be
// well-formed. Both types are basically functors:
// * `Hash` should support `size_t operator()(U val) const` that returns a hash
// for the given `val`.
// * `Eq` should support `bool operator()(U lhs, V rhs) const` that returns true
// if `lhs` is equal to `rhs`.
//
// In most cases `T` needs only to provide the `absl_container_hash`. In this
// case `std::equal_to<void>` will be used instead of `eq` part.
//
// Example:
//
// // Create a node hash set of three strings
// absl::node_hash_set<std::string> ducks =
// {"huey", "dewey", "louie"};
//
// // Insert a new element into the node hash set
// ducks.insert("donald");
//
// // Force a rehash of the node hash set
// ducks.rehash(0);
//
// // See if "dewey" is present
// if (ducks.contains("dewey")) {
// std::cout << "We found dewey!" << std::endl;
// }
template <class T, class Hash = DefaultHashContainerHash<T>,
class Eq = DefaultHashContainerEq<T>, class Alloc = std::allocator<T>>
class ABSL_ATTRIBUTE_OWNER node_hash_set
: public absl::container_internal::raw_hash_set<
absl::container_internal::NodeHashSetPolicy<T>, Hash, Eq, Alloc> {
using Base = typename node_hash_set::raw_hash_set;
public:
// Constructors and Assignment Operators
//
// A node_hash_set supports the same overload set as `std::unordered_set`
// for construction and assignment:
//
// * Default constructor
//
// // No allocation for the table's elements is made.
// absl::node_hash_set<std::string> set1;
//
// * Initializer List constructor
//
// absl::node_hash_set<std::string> set2 =
// {{"huey"}, {"dewey"}, {"louie"}};
//
// * Copy constructor
//
// absl::node_hash_set<std::string> set3(set2);
//
// * Copy assignment operator
//
// // Hash functor and Comparator are copied as well
// absl::node_hash_set<std::string> set4;
// set4 = set3;
//
// * Move constructor
//
// // Move is guaranteed efficient
// absl::node_hash_set<std::string> set5(std::move(set4));
//
// * Move assignment operator
//
// // May be efficient if allocators are compatible
// absl::node_hash_set<std::string> set6;
// set6 = std::move(set5);
//
// * Range constructor
//
// std::vector<std::string> v = {"a", "b"};
// absl::node_hash_set<std::string> set7(v.begin(), v.end());
node_hash_set() {}
using Base::Base;
// node_hash_set::begin()
//
// Returns an iterator to the beginning of the `node_hash_set`.
using Base::begin;
// node_hash_set::cbegin()
//
// Returns a const iterator to the beginning of the `node_hash_set`.
using Base::cbegin;
// node_hash_set::cend()
//
// Returns a const iterator to the end of the `node_hash_set`.
using Base::cend;
// node_hash_set::end()
//
// Returns an iterator to the end of the `node_hash_set`.
using Base::end;
// node_hash_set::capacity()
//
// Returns the number of element slots (assigned, deleted, and empty)
// available within the `node_hash_set`.
//
// NOTE: this member function is particular to `absl::node_hash_set` and is
// not provided in the `std::unordered_set` API.
using Base::capacity;
// node_hash_set::empty()
//
// Returns whether or not the `node_hash_set` is empty.
using Base::empty;
// node_hash_set::max_size()
//
// Returns the largest theoretical possible number of elements within a
// `node_hash_set` under current memory constraints. This value can be thought
// of the largest value of `std::distance(begin(), end())` for a
// `node_hash_set<T>`.
using Base::max_size;
// node_hash_set::size()
//
// Returns the number of elements currently within the `node_hash_set`.
using Base::size;
// node_hash_set::clear()
//
// Removes all elements from the `node_hash_set`. Invalidates any references,
// pointers, or iterators referring to contained elements.
//
// NOTE: this operation may shrink the underlying buffer. To avoid shrinking
// the underlying buffer call `erase(begin(), end())`.
using Base::clear;
// node_hash_set::erase()
//
// Erases elements within the `node_hash_set`. Erasing does not trigger a
// rehash. Overloads are listed below.
//
// void erase(const_iterator pos):
//
// Erases the element at `position` of the `node_hash_set`, returning
// `void`.
//
// NOTE: Returning `void` in this case is different than that of STL
// containers in general and `std::unordered_map` in particular (which
// return an iterator to the element following the erased element). If that
// iterator is needed, simply post increment the iterator:
//
// map.erase(it++);
//
//
// iterator erase(const_iterator first, const_iterator last):
//
// Erases the elements in the open interval [`first`, `last`), returning an
// iterator pointing to `last`. The special case of calling
// `erase(begin(), end())` resets the reserved growth such that if
// `reserve(N)` has previously been called and there has been no intervening
// call to `clear()`, then after calling `erase(begin(), end())`, it is safe
// to assume that inserting N elements will not cause a rehash.
//
// size_type erase(const key_type& key):
//
// Erases the element with the matching key, if it exists, returning the
// number of elements erased (0 or 1).
using Base::erase;
// node_hash_set::insert()
//
// Inserts an element of the specified value into the `node_hash_set`,
// returning an iterator pointing to the newly inserted element, provided that
// an element with the given key does not already exist. If rehashing occurs
// due to the insertion, all iterators are invalidated. Overloads are listed
// below.
//
// std::pair<iterator,bool> insert(const T& value):
//
// Inserts a value into the `node_hash_set`. Returns a pair consisting of an
// iterator to the inserted element (or to the element that prevented the
// insertion) and a bool denoting whether the insertion took place.
//
// std::pair<iterator,bool> insert(T&& value):
//
// Inserts a moveable value into the `node_hash_set`. Returns a pair
// consisting of an iterator to the inserted element (or to the element that
// prevented the insertion) and a bool denoting whether the insertion took
// place.
//
// iterator insert(const_iterator hint, const T& value):
// iterator insert(const_iterator hint, T&& value):
//
// Inserts a value, using the position of `hint` as a non-binding suggestion
// for where to begin the insertion search. Returns an iterator to the
// inserted element, or to the existing element that prevented the
// insertion.
//
// void insert(InputIterator first, InputIterator last):
//
// Inserts a range of values [`first`, `last`).
//
// NOTE: Although the STL does not specify which element may be inserted if
// multiple keys compare equivalently, for `node_hash_set` we guarantee the
// first match is inserted.
//
// void insert(std::initializer_list<T> ilist):
//
// Inserts the elements within the initializer list `ilist`.
//
// NOTE: Although the STL does not specify which element may be inserted if
// multiple keys compare equivalently within the initializer list, for
// `node_hash_set` we guarantee the first match is inserted.
using Base::insert;
// node_hash_set::emplace()
//
// Inserts an element of the specified value by constructing it in-place
// within the `node_hash_set`, provided that no element with the given key
// already exists.
//
// The element may be constructed even if there already is an element with the
// key in the container, in which case the newly constructed element will be
// destroyed immediately.
//
// If rehashing occurs due to the insertion, all iterators are invalidated.
using Base::emplace;
// node_hash_set::emplace_hint()
//
// Inserts an element of the specified value by constructing it in-place
// within the `node_hash_set`, using the position of `hint` as a non-binding
// suggestion for where to begin the insertion search, and only inserts
// provided that no element with the given key already exists.
//
// The element may be constructed even if there already is an element with the
// key in the container, in which case the newly constructed element will be
// destroyed immediately.
//
// If rehashing occurs due to the insertion, all iterators are invalidated.
using Base::emplace_hint;
// node_hash_set::extract()
//
// Extracts the indicated element, erasing it in the process, and returns it
// as a C++17-compatible node handle. Overloads are listed below.
//
// node_type extract(const_iterator position):
//
// Extracts the element at the indicated position and returns a node handle
// owning that extracted data.
//
// node_type extract(const key_type& x):
//
// Extracts the element with the key matching the passed key value and
// returns a node handle owning that extracted data. If the `node_hash_set`
// does not contain an element with a matching key, this function returns an
// empty node handle.
using Base::extract;
// node_hash_set::merge()
//
// Extracts elements from a given `source` node hash set into this
// `node_hash_set`. If the destination `node_hash_set` already contains an
// element with an equivalent key, that element is not extracted.
using Base::merge;
// node_hash_set::swap(node_hash_set& other)
//
// Exchanges the contents of this `node_hash_set` with those of the `other`
// node hash set.
//
// All iterators and references on the `node_hash_set` remain valid, excepting
// for the past-the-end iterator, which is invalidated.
//
// `swap()` requires that the node hash set's hashing and key equivalence
// functions be Swappable, and are exchanged using unqualified calls to
// non-member `swap()`. If the set's allocator has
// `std::allocator_traits<allocator_type>::propagate_on_container_swap::value`
// set to `true`, the allocators are also exchanged using an unqualified call
// to non-member `swap()`; otherwise, the allocators are not swapped.
using Base::swap;
// node_hash_set::rehash(count)
//
// Rehashes the `node_hash_set`, setting the number of slots to be at least
// the passed value. If the new number of slots increases the load factor more
// than the current maximum load factor
// (`count` < `size()` / `max_load_factor()`), then the new number of slots
// will be at least `size()` / `max_load_factor()`.
//
// To force a rehash, pass rehash(0).
//
// NOTE: unlike behavior in `std::unordered_set`, references are also
// invalidated upon a `rehash()`.
using Base::rehash;
// node_hash_set::reserve(count)
//
// Sets the number of slots in the `node_hash_set` to the number needed to
// accommodate at least `count` total elements without exceeding the current
// maximum load factor, and may rehash the container if needed.
using Base::reserve;
// node_hash_set::contains()
//
// Determines whether an element comparing equal to the given `key` exists
// within the `node_hash_set`, returning `true` if so or `false` otherwise.
using Base::contains;
// node_hash_set::count(const Key& key) const
//
// Returns the number of elements comparing equal to the given `key` within
// the `node_hash_set`. note that this function will return either `1` or `0`
// since duplicate elements are not allowed within a `node_hash_set`.
using Base::count;
// node_hash_set::equal_range()
//
// Returns a closed range [first, last], defined by a `std::pair` of two
// iterators, containing all elements with the passed key in the
// `node_hash_set`.
using Base::equal_range;
// node_hash_set::find()
//
// Finds an element with the passed `key` within the `node_hash_set`.
using Base::find;
// node_hash_set::bucket_count()
//
// Returns the number of "buckets" within the `node_hash_set`. Note that
// because a node hash set contains all elements within its internal storage,
// this value simply equals the current capacity of the `node_hash_set`.
using Base::bucket_count;
// node_hash_set::load_factor()
//
// Returns the current load factor of the `node_hash_set` (the average number
// of slots occupied with a value within the hash set).
using Base::load_factor;
// node_hash_set::max_load_factor()
//
// Manages the maximum load factor of the `node_hash_set`. Overloads are
// listed below.
//
// float node_hash_set::max_load_factor()
//
// Returns the current maximum load factor of the `node_hash_set`.
//
// void node_hash_set::max_load_factor(float ml)
//
// Sets the maximum load factor of the `node_hash_set` to the passed value.
//
// NOTE: This overload is provided only for API compatibility with the STL;
// `node_hash_set` will ignore any set load factor and manage its rehashing
// internally as an implementation detail.
using Base::max_load_factor;
// node_hash_set::get_allocator()
//
// Returns the allocator function associated with this `node_hash_set`.
using Base::get_allocator;
// node_hash_set::hash_function()
//
// Returns the hashing function used to hash the keys within this
// `node_hash_set`.
using Base::hash_function;
// node_hash_set::key_eq()
//
// Returns the function used for comparing keys equality.
using Base::key_eq;
};
// erase_if(node_hash_set<>, Pred)
//
// Erases all elements that satisfy the predicate `pred` from the container `c`.
// Returns the number of erased elements.
template <typename T, typename H, typename E, typename A, typename Predicate>
typename node_hash_set<T, H, E, A>::size_type erase_if(
node_hash_set<T, H, E, A>& c, Predicate pred) {
return container_internal::EraseIf(pred, &c);
}
// swap(node_hash_set<>, node_hash_set<>)
//
// Swaps the contents of two `node_hash_set` containers.
//
// NOTE: we need to define this function template in order for
// `flat_hash_set::swap` to be called instead of `std::swap`. Even though we
// have `swap(raw_hash_set&, raw_hash_set&)` defined, that function requires a
// derived-to-base conversion, whereas `std::swap` is a function template so
// `std::swap` will be preferred by compiler.
template <typename T, typename H, typename E, typename A>
void swap(node_hash_set<T, H, E, A>& x,
node_hash_set<T, H, E, A>& y) noexcept(noexcept(x.swap(y))) {
return x.swap(y);
}
namespace container_internal {
// c_for_each_fast(node_hash_set<>, Function)
//
// Container-based version of the <algorithm> `std::for_each()` function to
// apply a function to a container's elements.
// There is no guarantees on the order of the function calls.
// Erasure and/or insertion of elements in the function is not allowed.
template <typename T, typename H, typename E, typename A, typename Function>
decay_t<Function> c_for_each_fast(const node_hash_set<T, H, E, A>& c,
Function&& f) {
container_internal::ForEach(f, &c);
return f;
}
template <typename T, typename H, typename E, typename A, typename Function>
decay_t<Function> c_for_each_fast(node_hash_set<T, H, E, A>& c, Function&& f) {
container_internal::ForEach(f, &c);
return f;
}
template <typename T, typename H, typename E, typename A, typename Function>
decay_t<Function> c_for_each_fast(node_hash_set<T, H, E, A>&& c, Function&& f) {
container_internal::ForEach(f, &c);
return f;
}
} // namespace container_internal
namespace container_internal {
template <class T>
struct NodeHashSetPolicy
: absl::container_internal::node_slot_policy<T&, NodeHashSetPolicy<T>> {
using key_type = T;
using init_type = T;
using constant_iterators = std::true_type;
template <class Allocator, class... Args>
static T* new_element(Allocator* alloc, Args&&... args) {
using ValueAlloc =
typename absl::allocator_traits<Allocator>::template rebind_alloc<T>;
ValueAlloc value_alloc(*alloc);
T* res = absl::allocator_traits<ValueAlloc>::allocate(value_alloc, 1);
absl::allocator_traits<ValueAlloc>::construct(value_alloc, res,
std::forward<Args>(args)...);
return res;
}
template <class Allocator>
static void delete_element(Allocator* alloc, T* elem) {
using ValueAlloc =
typename absl::allocator_traits<Allocator>::template rebind_alloc<T>;
ValueAlloc value_alloc(*alloc);
absl::allocator_traits<ValueAlloc>::destroy(value_alloc, elem);
absl::allocator_traits<ValueAlloc>::deallocate(value_alloc, elem, 1);
}
template <class F, class... Args>
static decltype(absl::container_internal::DecomposeValue(
std::declval<F>(), std::declval<Args>()...))
apply(F&& f, Args&&... args) {
return absl::container_internal::DecomposeValue(
std::forward<F>(f), std::forward<Args>(args)...);
}
static size_t element_space_used(const T*) { return sizeof(T); }
template <class Hash>
static constexpr HashSlotFn get_hash_slot_fn() {
return &TypeErasedDerefAndApplyToSlotFn<Hash, T>;
}
};
} // namespace container_internal
namespace container_algorithm_internal {
// Specialization of trait in absl/algorithm/container.h
template <class Key, class Hash, class KeyEqual, class Allocator>
struct IsUnorderedContainer<absl::node_hash_set<Key, Hash, KeyEqual, Allocator>>
: std::true_type {};
} // namespace container_algorithm_internal
ABSL_NAMESPACE_END
} // namespace absl
#endif // ABSL_CONTAINER_NODE_HASH_SET_H_

View file

@ -0,0 +1,189 @@
// Copyright 2018 The Abseil Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "absl/container/node_hash_set.h"
#include <cstddef>
#include <memory>
#include <string>
#include <utility>
#include <vector>
#include "gmock/gmock.h"
#include "gtest/gtest.h"
#include "absl/base/config.h"
#include "absl/container/internal/hash_generator_testing.h"
#include "absl/container/internal/hash_policy_testing.h"
#include "absl/container/internal/unordered_set_constructor_test.h"
#include "absl/container/internal/unordered_set_lookup_test.h"
#include "absl/container/internal/unordered_set_members_test.h"
#include "absl/container/internal/unordered_set_modifiers_test.h"
#include "absl/memory/memory.h"
namespace absl {
ABSL_NAMESPACE_BEGIN
namespace container_internal {
namespace {
using ::absl::container_internal::hash_internal::Enum;
using ::absl::container_internal::hash_internal::EnumClass;
using ::testing::IsEmpty;
using ::testing::Pointee;
using ::testing::UnorderedElementsAre;
using ::testing::UnorderedElementsAreArray;
using SetTypes = ::testing::Types<
node_hash_set<int, StatefulTestingHash, StatefulTestingEqual, Alloc<int>>,
node_hash_set<std::string, StatefulTestingHash, StatefulTestingEqual,
Alloc<std::string>>,
node_hash_set<Enum, StatefulTestingHash, StatefulTestingEqual, Alloc<Enum>>,
node_hash_set<EnumClass, StatefulTestingHash, StatefulTestingEqual,
Alloc<EnumClass>>>;
INSTANTIATE_TYPED_TEST_SUITE_P(NodeHashSet, ConstructorTest, SetTypes);
INSTANTIATE_TYPED_TEST_SUITE_P(NodeHashSet, LookupTest, SetTypes);
INSTANTIATE_TYPED_TEST_SUITE_P(NodeHashSet, MembersTest, SetTypes);
INSTANTIATE_TYPED_TEST_SUITE_P(NodeHashSet, ModifiersTest, SetTypes);
TEST(NodeHashSet, MoveableNotCopyableCompiles) {
node_hash_set<std::unique_ptr<void*>> t;
node_hash_set<std::unique_ptr<void*>> u;
u = std::move(t);
}
TEST(NodeHashSet, MergeExtractInsert) {
struct Hash {
size_t operator()(const std::unique_ptr<int>& p) const { return *p; }
};
struct Eq {
bool operator()(const std::unique_ptr<int>& a,
const std::unique_ptr<int>& b) const {
return *a == *b;
}
};
absl::node_hash_set<std::unique_ptr<int>, Hash, Eq> set1, set2;
set1.insert(absl::make_unique<int>(7));
set1.insert(absl::make_unique<int>(17));
set2.insert(absl::make_unique<int>(7));
set2.insert(absl::make_unique<int>(19));
EXPECT_THAT(set1, UnorderedElementsAre(Pointee(7), Pointee(17)));
EXPECT_THAT(set2, UnorderedElementsAre(Pointee(7), Pointee(19)));
set1.merge(set2);
EXPECT_THAT(set1, UnorderedElementsAre(Pointee(7), Pointee(17), Pointee(19)));
EXPECT_THAT(set2, UnorderedElementsAre(Pointee(7)));
auto node = set1.extract(absl::make_unique<int>(7));
EXPECT_TRUE(node);
EXPECT_THAT(node.value(), Pointee(7));
EXPECT_THAT(set1, UnorderedElementsAre(Pointee(17), Pointee(19)));
auto insert_result = set2.insert(std::move(node));
EXPECT_FALSE(node);
EXPECT_FALSE(insert_result.inserted);
EXPECT_TRUE(insert_result.node);
EXPECT_THAT(insert_result.node.value(), Pointee(7));
EXPECT_EQ(**insert_result.position, 7);
EXPECT_NE(insert_result.position->get(), insert_result.node.value().get());
EXPECT_THAT(set2, UnorderedElementsAre(Pointee(7)));
node = set1.extract(absl::make_unique<int>(17));
EXPECT_TRUE(node);
EXPECT_THAT(node.value(), Pointee(17));
EXPECT_THAT(set1, UnorderedElementsAre(Pointee(19)));
node.value() = absl::make_unique<int>(23);
insert_result = set2.insert(std::move(node));
EXPECT_FALSE(node);
EXPECT_TRUE(insert_result.inserted);
EXPECT_FALSE(insert_result.node);
EXPECT_EQ(**insert_result.position, 23);
EXPECT_THAT(set2, UnorderedElementsAre(Pointee(7), Pointee(23)));
}
bool IsEven(int k) { return k % 2 == 0; }
TEST(NodeHashSet, EraseIf) {
// Erase all elements.
{
node_hash_set<int> s = {1, 2, 3, 4, 5};
EXPECT_EQ(erase_if(s, [](int) { return true; }), 5);
EXPECT_THAT(s, IsEmpty());
}
// Erase no elements.
{
node_hash_set<int> s = {1, 2, 3, 4, 5};
EXPECT_EQ(erase_if(s, [](int) { return false; }), 0);
EXPECT_THAT(s, UnorderedElementsAre(1, 2, 3, 4, 5));
}
// Erase specific elements.
{
node_hash_set<int> s = {1, 2, 3, 4, 5};
EXPECT_EQ(erase_if(s, [](int k) { return k % 2 == 1; }), 3);
EXPECT_THAT(s, UnorderedElementsAre(2, 4));
}
// Predicate is function reference.
{
node_hash_set<int> s = {1, 2, 3, 4, 5};
EXPECT_EQ(erase_if(s, IsEven), 2);
EXPECT_THAT(s, UnorderedElementsAre(1, 3, 5));
}
// Predicate is function pointer.
{
node_hash_set<int> s = {1, 2, 3, 4, 5};
EXPECT_EQ(erase_if(s, &IsEven), 2);
EXPECT_THAT(s, UnorderedElementsAre(1, 3, 5));
}
}
TEST(NodeHashSet, CForEach) {
using ValueType = std::pair<int, int>;
node_hash_set<ValueType> s;
std::vector<ValueType> expected;
for (int i = 0; i < 100; ++i) {
{
SCOPED_TRACE("mutable object iteration");
std::vector<ValueType> v;
absl::container_internal::c_for_each_fast(
s, [&v](const ValueType& p) { v.push_back(p); });
ASSERT_THAT(v, UnorderedElementsAreArray(expected));
}
{
SCOPED_TRACE("const object iteration");
std::vector<ValueType> v;
const node_hash_set<ValueType>& cs = s;
absl::container_internal::c_for_each_fast(
cs, [&v](const ValueType& p) { v.push_back(p); });
ASSERT_THAT(v, UnorderedElementsAreArray(expected));
}
{
SCOPED_TRACE("temporary object iteration");
std::vector<ValueType> v;
absl::container_internal::c_for_each_fast(
node_hash_set<ValueType>(s),
[&v](const ValueType& p) { v.push_back(p); });
ASSERT_THAT(v, UnorderedElementsAreArray(expected));
}
s.emplace(i, i);
expected.emplace_back(i, i);
}
}
} // namespace
} // namespace container_internal
ABSL_NAMESPACE_END
} // namespace absl

View file

@ -0,0 +1,118 @@
// Copyright 2018 The Abseil Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include <cstddef>
#include <unordered_set>
#include <utility>
#include <vector>
#include "gmock/gmock.h"
#include "gtest/gtest.h"
#include "absl/container/flat_hash_map.h"
#include "absl/container/flat_hash_set.h"
#include "absl/container/internal/hashtablez_sampler.h"
#include "absl/container/node_hash_map.h"
#include "absl/container/node_hash_set.h"
namespace absl {
ABSL_NAMESPACE_BEGIN
namespace container_internal {
namespace {
#if defined(ABSL_INTERNAL_HASHTABLEZ_SAMPLE)
// Create some tables of type `Table`, then look at all the new
// `HashtablezInfo`s to make sure that the `inline_element_size ==
// expected_element_size`. The `inline_element_size` is the amount of memory
// allocated for each slot of a hash table, that is `sizeof(slot_type)`. Add
// the new `HashtablezInfo`s to `preexisting_info`. Store all the new tables
// into `tables`.
template <class Table>
void TestInlineElementSize(
HashtablezSampler& sampler,
// clang-tidy gives a false positive on this declaration. This unordered
// set cannot be flat_hash_set, however, since that would introduce a mutex
// deadlock.
std::unordered_set<const HashtablezInfo*>& preexisting_info, // NOLINT
std::vector<Table>& tables,
const std::vector<typename Table::value_type>& values,
size_t expected_element_size) {
EXPECT_GT(values.size(), 0);
for (int i = 0; i < 10; ++i) {
// We create a new table and must store it somewhere so that when we store
// a pointer to the resulting `HashtablezInfo` into `preexisting_info`
// that we aren't storing a dangling pointer.
tables.emplace_back();
// We must insert elements to get a hashtablez to instantiate.
tables.back().insert(values.begin(), values.end());
}
size_t new_count = 0;
sampler.Iterate([&](const HashtablezInfo& info) {
if (preexisting_info.insert(&info).second) {
EXPECT_EQ(info.inline_element_size, expected_element_size);
++new_count;
}
});
// Make sure we actually did get a new hashtablez.
EXPECT_GT(new_count, 0);
}
struct bigstruct {
char a[1000];
friend bool operator==(const bigstruct& x, const bigstruct& y) {
return memcmp(x.a, y.a, sizeof(x.a)) == 0;
}
template <typename H>
friend H AbslHashValue(H h, const bigstruct& c) {
return H::combine_contiguous(std::move(h), c.a, sizeof(c.a));
}
};
#endif
TEST(FlatHashMap, SampleElementSize) {
#if defined(ABSL_INTERNAL_HASHTABLEZ_SAMPLE)
// Enable sampling even if the prod default is off.
SetHashtablezEnabled(true);
SetHashtablezSampleParameter(1);
TestOnlyRefreshSamplingStateForCurrentThread();
auto& sampler = GlobalHashtablezSampler();
std::vector<flat_hash_map<int, bigstruct>> flat_map_tables;
std::vector<flat_hash_set<bigstruct>> flat_set_tables;
std::vector<node_hash_map<int, bigstruct>> node_map_tables;
std::vector<node_hash_set<bigstruct>> node_set_tables;
std::vector<bigstruct> set_values = {bigstruct{{0}}, bigstruct{{1}}};
std::vector<std::pair<const int, bigstruct>> map_values = {{0, bigstruct{}},
{1, bigstruct{}}};
// clang-tidy gives a false positive on this declaration. This unordered set
// cannot be a flat_hash_set, however, since that would introduce a mutex
// deadlock.
std::unordered_set<const HashtablezInfo*> preexisting_info; // NOLINT
sampler.Iterate(
[&](const HashtablezInfo& info) { preexisting_info.insert(&info); });
TestInlineElementSize(sampler, preexisting_info, flat_map_tables, map_values,
sizeof(int) + sizeof(bigstruct));
TestInlineElementSize(sampler, preexisting_info, node_map_tables, map_values,
sizeof(void*));
TestInlineElementSize(sampler, preexisting_info, flat_set_tables, set_values,
sizeof(bigstruct));
TestInlineElementSize(sampler, preexisting_info, node_set_tables, set_values,
sizeof(void*));
#endif
}
} // namespace
} // namespace container_internal
ABSL_NAMESPACE_END
} // namespace absl