From 5aa5d282eac56a21e74611c1cdbaa97bb5db2dca Mon Sep 17 00:00:00 2001 From: Vincent Ambo Date: Tue, 8 Feb 2022 02:05:36 +0300 Subject: chore(3p/abseil_cpp): unvendor abseil_cpp we weren't actually using these sources anymore, okay? Change-Id: If701571d9716de308d3512e1eb22c35db0877a66 Reviewed-on: https://cl.tvl.fyi/c/depot/+/5248 Tested-by: BuildkiteCI Reviewed-by: grfn Autosubmit: tazjin --- third_party/abseil_cpp/absl/container/BUILD.bazel | 921 ------- .../abseil_cpp/absl/container/CMakeLists.txt | 905 ------- .../abseil_cpp/absl/container/btree_benchmark.cc | 735 ----- third_party/abseil_cpp/absl/container/btree_map.h | 769 ------ third_party/abseil_cpp/absl/container/btree_set.h | 683 ----- .../abseil_cpp/absl/container/btree_test.cc | 2827 -------------------- third_party/abseil_cpp/absl/container/btree_test.h | 166 -- .../abseil_cpp/absl/container/fixed_array.h | 532 ---- .../absl/container/fixed_array_benchmark.cc | 67 - .../container/fixed_array_exception_safety_test.cc | 201 -- .../abseil_cpp/absl/container/fixed_array_test.cc | 837 ------ .../abseil_cpp/absl/container/flat_hash_map.h | 606 ----- .../absl/container/flat_hash_map_test.cc | 288 -- .../abseil_cpp/absl/container/flat_hash_set.h | 504 ---- .../absl/container/flat_hash_set_test.cc | 178 -- .../abseil_cpp/absl/container/inlined_vector.h | 845 ------ .../absl/container/inlined_vector_benchmark.cc | 807 ------ .../inlined_vector_exception_safety_test.cc | 508 ---- .../absl/container/inlined_vector_test.cc | 1815 ------------- .../abseil_cpp/absl/container/internal/btree.h | 2587 ------------------ .../absl/container/internal/btree_container.h | 683 ----- .../abseil_cpp/absl/container/internal/common.h | 206 -- .../absl/container/internal/compressed_tuple.h | 290 -- .../container/internal/compressed_tuple_test.cc | 409 --- .../absl/container/internal/container_memory.h | 460 ---- .../container/internal/container_memory_test.cc | 256 -- .../absl/container/internal/counting_allocator.h | 114 - .../container/internal/hash_function_defaults.h | 161 -- .../internal/hash_function_defaults_test.cc | 383 --- .../container/internal/hash_generator_testing.cc | 76 - .../container/internal/hash_generator_testing.h | 161 -- .../absl/container/internal/hash_policy_testing.h | 184 -- .../container/internal/hash_policy_testing_test.cc | 45 - .../absl/container/internal/hash_policy_traits.h | 208 -- .../container/internal/hash_policy_traits_test.cc | 144 - .../absl/container/internal/hashtable_debug.h | 110 - .../container/internal/hashtable_debug_hooks.h | 85 - .../absl/container/internal/hashtablez_sampler.cc | 270 -- .../absl/container/internal/hashtablez_sampler.h | 321 --- .../hashtablez_sampler_force_weak_definition.cc | 30 - .../container/internal/hashtablez_sampler_test.cc | 371 --- .../abseil_cpp/absl/container/internal/have_sse.h | 50 - .../absl/container/internal/inlined_vector.h | 895 ------- .../abseil_cpp/absl/container/internal/layout.h | 743 ----- .../absl/container/internal/layout_test.cc | 1635 ----------- .../absl/container/internal/node_hash_policy.h | 92 - .../container/internal/node_hash_policy_test.cc | 69 - .../absl/container/internal/raw_hash_map.h | 197 -- .../absl/container/internal/raw_hash_set.cc | 61 - .../absl/container/internal/raw_hash_set.h | 1903 ------------- .../internal/raw_hash_set_allocator_test.cc | 505 ---- .../absl/container/internal/raw_hash_set_test.cc | 1893 ------------- .../container/internal/test_instance_tracker.cc | 29 - .../container/internal/test_instance_tracker.h | 274 -- .../internal/test_instance_tracker_test.cc | 184 -- .../abseil_cpp/absl/container/internal/tracked.h | 83 - .../internal/unordered_map_constructor_test.h | 489 ---- .../container/internal/unordered_map_lookup_test.h | 117 - .../internal/unordered_map_members_test.h | 87 - .../internal/unordered_map_modifiers_test.h | 318 --- .../absl/container/internal/unordered_map_test.cc | 50 - .../internal/unordered_set_constructor_test.h | 496 ---- .../container/internal/unordered_set_lookup_test.h | 91 - .../internal/unordered_set_members_test.h | 86 - .../internal/unordered_set_modifiers_test.h | 190 -- .../absl/container/internal/unordered_set_test.cc | 41 - .../abseil_cpp/absl/container/node_hash_map.h | 597 ----- .../absl/container/node_hash_map_test.cc | 275 -- .../abseil_cpp/absl/container/node_hash_set.h | 493 ---- .../absl/container/node_hash_set_test.cc | 143 - 70 files changed, 33834 deletions(-) delete mode 100644 third_party/abseil_cpp/absl/container/BUILD.bazel delete mode 100644 third_party/abseil_cpp/absl/container/CMakeLists.txt delete mode 100644 third_party/abseil_cpp/absl/container/btree_benchmark.cc delete mode 100644 third_party/abseil_cpp/absl/container/btree_map.h delete mode 100644 third_party/abseil_cpp/absl/container/btree_set.h delete mode 100644 third_party/abseil_cpp/absl/container/btree_test.cc delete mode 100644 third_party/abseil_cpp/absl/container/btree_test.h delete mode 100644 third_party/abseil_cpp/absl/container/fixed_array.h delete mode 100644 third_party/abseil_cpp/absl/container/fixed_array_benchmark.cc delete mode 100644 third_party/abseil_cpp/absl/container/fixed_array_exception_safety_test.cc delete mode 100644 third_party/abseil_cpp/absl/container/fixed_array_test.cc delete mode 100644 third_party/abseil_cpp/absl/container/flat_hash_map.h delete mode 100644 third_party/abseil_cpp/absl/container/flat_hash_map_test.cc delete mode 100644 third_party/abseil_cpp/absl/container/flat_hash_set.h delete mode 100644 third_party/abseil_cpp/absl/container/flat_hash_set_test.cc delete mode 100644 third_party/abseil_cpp/absl/container/inlined_vector.h delete mode 100644 third_party/abseil_cpp/absl/container/inlined_vector_benchmark.cc delete mode 100644 third_party/abseil_cpp/absl/container/inlined_vector_exception_safety_test.cc delete mode 100644 third_party/abseil_cpp/absl/container/inlined_vector_test.cc delete mode 100644 third_party/abseil_cpp/absl/container/internal/btree.h delete mode 100644 third_party/abseil_cpp/absl/container/internal/btree_container.h delete mode 100644 third_party/abseil_cpp/absl/container/internal/common.h delete mode 100644 third_party/abseil_cpp/absl/container/internal/compressed_tuple.h delete mode 100644 third_party/abseil_cpp/absl/container/internal/compressed_tuple_test.cc delete mode 100644 third_party/abseil_cpp/absl/container/internal/container_memory.h delete mode 100644 third_party/abseil_cpp/absl/container/internal/container_memory_test.cc delete mode 100644 third_party/abseil_cpp/absl/container/internal/counting_allocator.h delete mode 100644 third_party/abseil_cpp/absl/container/internal/hash_function_defaults.h delete mode 100644 third_party/abseil_cpp/absl/container/internal/hash_function_defaults_test.cc delete mode 100644 third_party/abseil_cpp/absl/container/internal/hash_generator_testing.cc delete mode 100644 third_party/abseil_cpp/absl/container/internal/hash_generator_testing.h delete mode 100644 third_party/abseil_cpp/absl/container/internal/hash_policy_testing.h delete mode 100644 third_party/abseil_cpp/absl/container/internal/hash_policy_testing_test.cc delete mode 100644 third_party/abseil_cpp/absl/container/internal/hash_policy_traits.h delete mode 100644 third_party/abseil_cpp/absl/container/internal/hash_policy_traits_test.cc delete mode 100644 third_party/abseil_cpp/absl/container/internal/hashtable_debug.h delete mode 100644 third_party/abseil_cpp/absl/container/internal/hashtable_debug_hooks.h delete mode 100644 third_party/abseil_cpp/absl/container/internal/hashtablez_sampler.cc delete mode 100644 third_party/abseil_cpp/absl/container/internal/hashtablez_sampler.h delete mode 100644 third_party/abseil_cpp/absl/container/internal/hashtablez_sampler_force_weak_definition.cc delete mode 100644 third_party/abseil_cpp/absl/container/internal/hashtablez_sampler_test.cc delete mode 100644 third_party/abseil_cpp/absl/container/internal/have_sse.h delete mode 100644 third_party/abseil_cpp/absl/container/internal/inlined_vector.h delete mode 100644 third_party/abseil_cpp/absl/container/internal/layout.h delete mode 100644 third_party/abseil_cpp/absl/container/internal/layout_test.cc delete mode 100644 third_party/abseil_cpp/absl/container/internal/node_hash_policy.h delete mode 100644 third_party/abseil_cpp/absl/container/internal/node_hash_policy_test.cc delete mode 100644 third_party/abseil_cpp/absl/container/internal/raw_hash_map.h delete mode 100644 third_party/abseil_cpp/absl/container/internal/raw_hash_set.cc delete mode 100644 third_party/abseil_cpp/absl/container/internal/raw_hash_set.h delete mode 100644 third_party/abseil_cpp/absl/container/internal/raw_hash_set_allocator_test.cc delete mode 100644 third_party/abseil_cpp/absl/container/internal/raw_hash_set_test.cc delete mode 100644 third_party/abseil_cpp/absl/container/internal/test_instance_tracker.cc delete mode 100644 third_party/abseil_cpp/absl/container/internal/test_instance_tracker.h delete mode 100644 third_party/abseil_cpp/absl/container/internal/test_instance_tracker_test.cc delete mode 100644 third_party/abseil_cpp/absl/container/internal/tracked.h delete mode 100644 third_party/abseil_cpp/absl/container/internal/unordered_map_constructor_test.h delete mode 100644 third_party/abseil_cpp/absl/container/internal/unordered_map_lookup_test.h delete mode 100644 third_party/abseil_cpp/absl/container/internal/unordered_map_members_test.h delete mode 100644 third_party/abseil_cpp/absl/container/internal/unordered_map_modifiers_test.h delete mode 100644 third_party/abseil_cpp/absl/container/internal/unordered_map_test.cc delete mode 100644 third_party/abseil_cpp/absl/container/internal/unordered_set_constructor_test.h delete mode 100644 third_party/abseil_cpp/absl/container/internal/unordered_set_lookup_test.h delete mode 100644 third_party/abseil_cpp/absl/container/internal/unordered_set_members_test.h delete mode 100644 third_party/abseil_cpp/absl/container/internal/unordered_set_modifiers_test.h delete mode 100644 third_party/abseil_cpp/absl/container/internal/unordered_set_test.cc delete mode 100644 third_party/abseil_cpp/absl/container/node_hash_map.h delete mode 100644 third_party/abseil_cpp/absl/container/node_hash_map_test.cc delete mode 100644 third_party/abseil_cpp/absl/container/node_hash_set.h delete mode 100644 third_party/abseil_cpp/absl/container/node_hash_set_test.cc (limited to 'third_party/abseil_cpp/absl/container') diff --git a/third_party/abseil_cpp/absl/container/BUILD.bazel b/third_party/abseil_cpp/absl/container/BUILD.bazel deleted file mode 100644 index 8e72ad03547c..000000000000 --- a/third_party/abseil_cpp/absl/container/BUILD.bazel +++ /dev/null @@ -1,921 +0,0 @@ -# -# 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. -# - -load("@rules_cc//cc:defs.bzl", "cc_binary", "cc_library", "cc_test") -load( - "//absl:copts/configure_copts.bzl", - "ABSL_DEFAULT_COPTS", - "ABSL_DEFAULT_LINKOPTS", - "ABSL_TEST_COPTS", -) - -package(default_visibility = ["//visibility:public"]) - -licenses(["notice"]) - -cc_library( - name = "compressed_tuple", - hdrs = ["internal/compressed_tuple.h"], - copts = ABSL_DEFAULT_COPTS, - linkopts = ABSL_DEFAULT_LINKOPTS, - deps = [ - "//absl/utility", - ], -) - -cc_test( - name = "compressed_tuple_test", - srcs = ["internal/compressed_tuple_test.cc"], - copts = ABSL_TEST_COPTS, - linkopts = ABSL_DEFAULT_LINKOPTS, - deps = [ - ":compressed_tuple", - ":test_instance_tracker", - "//absl/memory", - "//absl/types:any", - "//absl/types:optional", - "//absl/utility", - "@com_google_googletest//:gtest_main", - ], -) - -cc_library( - name = "fixed_array", - hdrs = ["fixed_array.h"], - copts = ABSL_DEFAULT_COPTS, - linkopts = ABSL_DEFAULT_LINKOPTS, - deps = [ - ":compressed_tuple", - "//absl/algorithm", - "//absl/base:config", - "//absl/base:core_headers", - "//absl/base:dynamic_annotations", - "//absl/base:throw_delegate", - "//absl/memory", - ], -) - -cc_test( - name = "fixed_array_test", - srcs = ["fixed_array_test.cc"], - copts = ABSL_TEST_COPTS, - linkopts = ABSL_DEFAULT_LINKOPTS, - deps = [ - ":counting_allocator", - ":fixed_array", - "//absl/base:config", - "//absl/base:exception_testing", - "//absl/hash:hash_testing", - "//absl/memory", - "@com_google_googletest//:gtest_main", - ], -) - -cc_test( - name = "fixed_array_exception_safety_test", - srcs = ["fixed_array_exception_safety_test.cc"], - copts = ABSL_TEST_COPTS, - linkopts = ABSL_DEFAULT_LINKOPTS, - deps = [ - ":fixed_array", - "//absl/base:config", - "//absl/base:exception_safety_testing", - "@com_google_googletest//:gtest_main", - ], -) - -cc_test( - name = "fixed_array_benchmark", - srcs = ["fixed_array_benchmark.cc"], - copts = ABSL_TEST_COPTS + ["$(STACK_FRAME_UNLIMITED)"], - linkopts = ABSL_DEFAULT_LINKOPTS, - tags = ["benchmark"], - deps = [ - ":fixed_array", - "@com_github_google_benchmark//:benchmark_main", - ], -) - -cc_library( - name = "inlined_vector_internal", - hdrs = ["internal/inlined_vector.h"], - copts = ABSL_DEFAULT_COPTS, - linkopts = ABSL_DEFAULT_LINKOPTS, - deps = [ - ":compressed_tuple", - "//absl/base:core_headers", - "//absl/memory", - "//absl/meta:type_traits", - "//absl/types:span", - ], -) - -cc_library( - name = "inlined_vector", - hdrs = ["inlined_vector.h"], - copts = ABSL_DEFAULT_COPTS, - linkopts = ABSL_DEFAULT_LINKOPTS, - deps = [ - ":inlined_vector_internal", - "//absl/algorithm", - "//absl/base:core_headers", - "//absl/base:throw_delegate", - "//absl/memory", - ], -) - -cc_library( - name = "counting_allocator", - testonly = 1, - hdrs = ["internal/counting_allocator.h"], - copts = ABSL_DEFAULT_COPTS, - linkopts = ABSL_DEFAULT_LINKOPTS, - visibility = ["//visibility:private"], - deps = ["//absl/base:config"], -) - -cc_test( - name = "inlined_vector_test", - srcs = ["inlined_vector_test.cc"], - copts = ABSL_TEST_COPTS, - linkopts = ABSL_DEFAULT_LINKOPTS, - deps = [ - ":counting_allocator", - ":inlined_vector", - ":test_instance_tracker", - "//absl/base:config", - "//absl/base:core_headers", - "//absl/base:exception_testing", - "//absl/base:raw_logging_internal", - "//absl/hash:hash_testing", - "//absl/memory", - "//absl/strings", - "@com_google_googletest//:gtest_main", - ], -) - -cc_test( - name = "inlined_vector_benchmark", - srcs = ["inlined_vector_benchmark.cc"], - copts = ABSL_TEST_COPTS, - linkopts = ABSL_DEFAULT_LINKOPTS, - tags = ["benchmark"], - deps = [ - ":inlined_vector", - "//absl/base:core_headers", - "//absl/base:raw_logging_internal", - "//absl/strings", - "@com_github_google_benchmark//:benchmark_main", - ], -) - -cc_test( - name = "inlined_vector_exception_safety_test", - srcs = ["inlined_vector_exception_safety_test.cc"], - copts = ABSL_TEST_COPTS, - deps = [ - ":inlined_vector", - "//absl/base:config", - "//absl/base:exception_safety_testing", - "@com_google_googletest//:gtest_main", - ], -) - -cc_library( - name = "test_instance_tracker", - testonly = 1, - srcs = ["internal/test_instance_tracker.cc"], - hdrs = ["internal/test_instance_tracker.h"], - copts = ABSL_DEFAULT_COPTS, - linkopts = ABSL_DEFAULT_LINKOPTS, - visibility = [ - "//absl:__subpackages__", - ], - deps = ["//absl/types:compare"], -) - -cc_test( - name = "test_instance_tracker_test", - srcs = ["internal/test_instance_tracker_test.cc"], - copts = ABSL_TEST_COPTS, - linkopts = ABSL_DEFAULT_LINKOPTS, - deps = [ - ":test_instance_tracker", - "@com_google_googletest//:gtest_main", - ], -) - -NOTEST_TAGS_NONMOBILE = [ - "no_test_darwin_x86_64", - "no_test_loonix", -] - -NOTEST_TAGS_MOBILE = [ - "no_test_android_arm", - "no_test_android_arm64", - "no_test_android_x86", - "no_test_ios_x86_64", -] - -NOTEST_TAGS = NOTEST_TAGS_MOBILE + NOTEST_TAGS_NONMOBILE - -cc_library( - name = "flat_hash_map", - hdrs = ["flat_hash_map.h"], - copts = ABSL_DEFAULT_COPTS, - linkopts = ABSL_DEFAULT_LINKOPTS, - deps = [ - ":container_memory", - ":hash_function_defaults", - ":raw_hash_map", - "//absl/algorithm:container", - "//absl/memory", - ], -) - -cc_test( - name = "flat_hash_map_test", - srcs = ["flat_hash_map_test.cc"], - copts = ABSL_TEST_COPTS, - linkopts = ABSL_DEFAULT_LINKOPTS, - tags = NOTEST_TAGS_NONMOBILE, - deps = [ - ":flat_hash_map", - ":hash_generator_testing", - ":unordered_map_constructor_test", - ":unordered_map_lookup_test", - ":unordered_map_members_test", - ":unordered_map_modifiers_test", - "//absl/base:raw_logging_internal", - "//absl/types:any", - "@com_google_googletest//:gtest_main", - ], -) - -cc_library( - name = "flat_hash_set", - hdrs = ["flat_hash_set.h"], - copts = ABSL_DEFAULT_COPTS, - linkopts = ABSL_DEFAULT_LINKOPTS, - deps = [ - ":container_memory", - ":hash_function_defaults", - ":raw_hash_set", - "//absl/algorithm:container", - "//absl/base:core_headers", - "//absl/memory", - ], -) - -cc_test( - name = "flat_hash_set_test", - srcs = ["flat_hash_set_test.cc"], - copts = ABSL_TEST_COPTS + ["-DUNORDERED_SET_CXX17"], - linkopts = ABSL_DEFAULT_LINKOPTS, - tags = NOTEST_TAGS_NONMOBILE, - deps = [ - ":flat_hash_set", - ":hash_generator_testing", - ":unordered_set_constructor_test", - ":unordered_set_lookup_test", - ":unordered_set_members_test", - ":unordered_set_modifiers_test", - "//absl/base:raw_logging_internal", - "//absl/memory", - "//absl/strings", - "@com_google_googletest//:gtest_main", - ], -) - -cc_library( - name = "node_hash_map", - hdrs = ["node_hash_map.h"], - copts = ABSL_DEFAULT_COPTS, - linkopts = ABSL_DEFAULT_LINKOPTS, - deps = [ - ":container_memory", - ":hash_function_defaults", - ":node_hash_policy", - ":raw_hash_map", - "//absl/algorithm:container", - "//absl/memory", - ], -) - -cc_test( - name = "node_hash_map_test", - srcs = ["node_hash_map_test.cc"], - copts = ABSL_TEST_COPTS, - linkopts = ABSL_DEFAULT_LINKOPTS, - tags = NOTEST_TAGS_NONMOBILE, - deps = [ - ":hash_generator_testing", - ":node_hash_map", - ":tracked", - ":unordered_map_constructor_test", - ":unordered_map_lookup_test", - ":unordered_map_members_test", - ":unordered_map_modifiers_test", - "@com_google_googletest//:gtest_main", - ], -) - -cc_library( - name = "node_hash_set", - hdrs = ["node_hash_set.h"], - copts = ABSL_DEFAULT_COPTS, - linkopts = ABSL_DEFAULT_LINKOPTS, - deps = [ - ":hash_function_defaults", - ":node_hash_policy", - ":raw_hash_set", - "//absl/algorithm:container", - "//absl/memory", - ], -) - -cc_test( - name = "node_hash_set_test", - srcs = ["node_hash_set_test.cc"], - copts = ABSL_TEST_COPTS + ["-DUNORDERED_SET_CXX17"], - linkopts = ABSL_DEFAULT_LINKOPTS, - tags = NOTEST_TAGS_NONMOBILE, - deps = [ - ":node_hash_set", - ":unordered_set_constructor_test", - ":unordered_set_lookup_test", - ":unordered_set_members_test", - ":unordered_set_modifiers_test", - "@com_google_googletest//:gtest_main", - ], -) - -cc_library( - name = "container_memory", - hdrs = ["internal/container_memory.h"], - copts = ABSL_DEFAULT_COPTS, - linkopts = ABSL_DEFAULT_LINKOPTS, - deps = [ - "//absl/base:config", - "//absl/memory", - "//absl/meta:type_traits", - "//absl/utility", - ], -) - -cc_test( - name = "container_memory_test", - srcs = ["internal/container_memory_test.cc"], - copts = ABSL_TEST_COPTS, - linkopts = ABSL_DEFAULT_LINKOPTS, - tags = NOTEST_TAGS_NONMOBILE, - deps = [ - ":container_memory", - ":test_instance_tracker", - "//absl/strings", - "@com_google_googletest//:gtest_main", - ], -) - -cc_library( - name = "hash_function_defaults", - hdrs = ["internal/hash_function_defaults.h"], - copts = ABSL_DEFAULT_COPTS, - linkopts = ABSL_DEFAULT_LINKOPTS, - deps = [ - "//absl/base:config", - "//absl/hash", - "//absl/strings", - "//absl/strings:cord", - ], -) - -cc_test( - name = "hash_function_defaults_test", - srcs = ["internal/hash_function_defaults_test.cc"], - copts = ABSL_TEST_COPTS, - linkopts = ABSL_DEFAULT_LINKOPTS, - tags = NOTEST_TAGS, - deps = [ - ":hash_function_defaults", - "//absl/hash", - "//absl/random", - "//absl/strings", - "//absl/strings:cord", - "//absl/strings:cord_test_helpers", - "@com_google_googletest//:gtest_main", - ], -) - -cc_library( - name = "hash_generator_testing", - testonly = 1, - srcs = ["internal/hash_generator_testing.cc"], - hdrs = ["internal/hash_generator_testing.h"], - copts = ABSL_TEST_COPTS, - linkopts = ABSL_DEFAULT_LINKOPTS, - deps = [ - ":hash_policy_testing", - "//absl/memory", - "//absl/meta:type_traits", - "//absl/strings", - ], -) - -cc_library( - name = "hash_policy_testing", - testonly = 1, - hdrs = ["internal/hash_policy_testing.h"], - copts = ABSL_TEST_COPTS, - linkopts = ABSL_DEFAULT_LINKOPTS, - deps = [ - "//absl/hash", - "//absl/strings", - ], -) - -cc_test( - name = "hash_policy_testing_test", - srcs = ["internal/hash_policy_testing_test.cc"], - copts = ABSL_TEST_COPTS, - linkopts = ABSL_DEFAULT_LINKOPTS, - deps = [ - ":hash_policy_testing", - "@com_google_googletest//:gtest_main", - ], -) - -cc_library( - name = "hash_policy_traits", - hdrs = ["internal/hash_policy_traits.h"], - copts = ABSL_DEFAULT_COPTS, - linkopts = ABSL_DEFAULT_LINKOPTS, - deps = ["//absl/meta:type_traits"], -) - -cc_test( - name = "hash_policy_traits_test", - srcs = ["internal/hash_policy_traits_test.cc"], - copts = ABSL_TEST_COPTS, - linkopts = ABSL_DEFAULT_LINKOPTS, - deps = [ - ":hash_policy_traits", - "@com_google_googletest//:gtest_main", - ], -) - -cc_library( - name = "hashtable_debug", - hdrs = ["internal/hashtable_debug.h"], - copts = ABSL_DEFAULT_COPTS, - linkopts = ABSL_DEFAULT_LINKOPTS, - deps = [ - ":hashtable_debug_hooks", - ], -) - -cc_library( - name = "hashtable_debug_hooks", - hdrs = ["internal/hashtable_debug_hooks.h"], - copts = ABSL_DEFAULT_COPTS, - linkopts = ABSL_DEFAULT_LINKOPTS, - deps = [ - "//absl/base:config", - ], -) - -cc_library( - name = "hashtablez_sampler", - srcs = [ - "internal/hashtablez_sampler.cc", - "internal/hashtablez_sampler_force_weak_definition.cc", - ], - hdrs = ["internal/hashtablez_sampler.h"], - copts = ABSL_DEFAULT_COPTS, - linkopts = ABSL_DEFAULT_LINKOPTS, - deps = [ - ":have_sse", - "//absl/base", - "//absl/base:core_headers", - "//absl/base:exponential_biased", - "//absl/debugging:stacktrace", - "//absl/memory", - "//absl/synchronization", - "//absl/utility", - ], -) - -cc_test( - name = "hashtablez_sampler_test", - srcs = ["internal/hashtablez_sampler_test.cc"], - linkopts = ABSL_DEFAULT_LINKOPTS, - deps = [ - ":hashtablez_sampler", - ":have_sse", - "//absl/base:core_headers", - "//absl/synchronization", - "//absl/synchronization:thread_pool", - "//absl/time", - "@com_google_googletest//:gtest_main", - ], -) - -cc_library( - name = "node_hash_policy", - hdrs = ["internal/node_hash_policy.h"], - copts = ABSL_DEFAULT_COPTS, - linkopts = ABSL_DEFAULT_LINKOPTS, - deps = ["//absl/base:config"], -) - -cc_test( - name = "node_hash_policy_test", - srcs = ["internal/node_hash_policy_test.cc"], - copts = ABSL_TEST_COPTS, - linkopts = ABSL_DEFAULT_LINKOPTS, - deps = [ - ":hash_policy_traits", - ":node_hash_policy", - "@com_google_googletest//:gtest_main", - ], -) - -cc_library( - name = "raw_hash_map", - hdrs = ["internal/raw_hash_map.h"], - copts = ABSL_DEFAULT_COPTS, - linkopts = ABSL_DEFAULT_LINKOPTS, - deps = [ - ":container_memory", - ":raw_hash_set", - "//absl/base:throw_delegate", - ], -) - -cc_library( - name = "have_sse", - hdrs = ["internal/have_sse.h"], - copts = ABSL_DEFAULT_COPTS, - linkopts = ABSL_DEFAULT_LINKOPTS, - visibility = ["//visibility:private"], -) - -cc_library( - name = "common", - hdrs = ["internal/common.h"], - copts = ABSL_DEFAULT_COPTS, - linkopts = ABSL_DEFAULT_LINKOPTS, - deps = [ - "//absl/meta:type_traits", - "//absl/types:optional", - ], -) - -cc_library( - name = "raw_hash_set", - srcs = ["internal/raw_hash_set.cc"], - hdrs = ["internal/raw_hash_set.h"], - copts = ABSL_DEFAULT_COPTS, - linkopts = ABSL_DEFAULT_LINKOPTS, - deps = [ - ":common", - ":compressed_tuple", - ":container_memory", - ":hash_policy_traits", - ":hashtable_debug_hooks", - ":hashtablez_sampler", - ":have_sse", - ":layout", - "//absl/base:bits", - "//absl/base:config", - "//absl/base:core_headers", - "//absl/base:endian", - "//absl/memory", - "//absl/meta:type_traits", - "//absl/utility", - ], -) - -cc_test( - name = "raw_hash_set_test", - srcs = ["internal/raw_hash_set_test.cc"], - copts = ABSL_TEST_COPTS, - linkstatic = 1, - tags = NOTEST_TAGS, - deps = [ - ":container_memory", - ":hash_function_defaults", - ":hash_policy_testing", - ":hashtable_debug", - ":raw_hash_set", - "//absl/base", - "//absl/base:config", - "//absl/base:core_headers", - "//absl/base:raw_logging_internal", - "//absl/strings", - "@com_google_googletest//:gtest_main", - ], -) - -cc_test( - name = "raw_hash_set_allocator_test", - size = "small", - srcs = ["internal/raw_hash_set_allocator_test.cc"], - copts = ABSL_TEST_COPTS, - linkopts = ABSL_DEFAULT_LINKOPTS, - deps = [ - ":raw_hash_set", - ":tracked", - "//absl/base:core_headers", - "@com_google_googletest//:gtest_main", - ], -) - -cc_library( - name = "layout", - hdrs = ["internal/layout.h"], - copts = ABSL_DEFAULT_COPTS, - linkopts = ABSL_DEFAULT_LINKOPTS, - deps = [ - "//absl/base:config", - "//absl/base:core_headers", - "//absl/meta:type_traits", - "//absl/strings", - "//absl/types:span", - "//absl/utility", - ], -) - -cc_test( - name = "layout_test", - size = "small", - srcs = ["internal/layout_test.cc"], - copts = ABSL_TEST_COPTS, - linkopts = ABSL_DEFAULT_LINKOPTS, - tags = NOTEST_TAGS, - visibility = ["//visibility:private"], - deps = [ - ":layout", - "//absl/base:config", - "//absl/base:core_headers", - "//absl/base:raw_logging_internal", - "//absl/types:span", - "@com_google_googletest//:gtest_main", - ], -) - -cc_library( - name = "tracked", - testonly = 1, - hdrs = ["internal/tracked.h"], - copts = ABSL_TEST_COPTS, - linkopts = ABSL_DEFAULT_LINKOPTS, - deps = [ - "//absl/base:config", - ], -) - -cc_library( - name = "unordered_map_constructor_test", - testonly = 1, - hdrs = ["internal/unordered_map_constructor_test.h"], - copts = ABSL_TEST_COPTS, - linkopts = ABSL_DEFAULT_LINKOPTS, - deps = [ - ":hash_generator_testing", - ":hash_policy_testing", - "@com_google_googletest//:gtest", - ], -) - -cc_library( - name = "unordered_map_lookup_test", - testonly = 1, - hdrs = ["internal/unordered_map_lookup_test.h"], - copts = ABSL_TEST_COPTS, - linkopts = ABSL_DEFAULT_LINKOPTS, - deps = [ - ":hash_generator_testing", - ":hash_policy_testing", - "@com_google_googletest//:gtest", - ], -) - -cc_library( - name = "unordered_map_modifiers_test", - testonly = 1, - hdrs = ["internal/unordered_map_modifiers_test.h"], - copts = ABSL_TEST_COPTS, - linkopts = ABSL_DEFAULT_LINKOPTS, - deps = [ - ":hash_generator_testing", - ":hash_policy_testing", - "@com_google_googletest//:gtest", - ], -) - -cc_library( - name = "unordered_set_constructor_test", - testonly = 1, - hdrs = ["internal/unordered_set_constructor_test.h"], - copts = ABSL_TEST_COPTS, - linkopts = ABSL_DEFAULT_LINKOPTS, - deps = [ - ":hash_generator_testing", - ":hash_policy_testing", - "//absl/meta:type_traits", - "@com_google_googletest//:gtest", - ], -) - -cc_library( - name = "unordered_set_members_test", - testonly = 1, - hdrs = ["internal/unordered_set_members_test.h"], - copts = ABSL_TEST_COPTS, - linkopts = ABSL_DEFAULT_LINKOPTS, - deps = [ - "//absl/meta:type_traits", - "@com_google_googletest//:gtest", - ], -) - -cc_library( - name = "unordered_map_members_test", - testonly = 1, - hdrs = ["internal/unordered_map_members_test.h"], - copts = ABSL_TEST_COPTS, - linkopts = ABSL_DEFAULT_LINKOPTS, - deps = [ - "//absl/meta:type_traits", - "@com_google_googletest//:gtest", - ], -) - -cc_library( - name = "unordered_set_lookup_test", - testonly = 1, - hdrs = ["internal/unordered_set_lookup_test.h"], - copts = ABSL_TEST_COPTS, - linkopts = ABSL_DEFAULT_LINKOPTS, - deps = [ - ":hash_generator_testing", - ":hash_policy_testing", - "@com_google_googletest//:gtest", - ], -) - -cc_library( - name = "unordered_set_modifiers_test", - testonly = 1, - hdrs = ["internal/unordered_set_modifiers_test.h"], - copts = ABSL_TEST_COPTS, - linkopts = ABSL_DEFAULT_LINKOPTS, - deps = [ - ":hash_generator_testing", - ":hash_policy_testing", - "@com_google_googletest//:gtest", - ], -) - -cc_test( - name = "unordered_set_test", - srcs = ["internal/unordered_set_test.cc"], - copts = ABSL_TEST_COPTS, - linkopts = ABSL_DEFAULT_LINKOPTS, - tags = NOTEST_TAGS_NONMOBILE, - deps = [ - ":unordered_set_constructor_test", - ":unordered_set_lookup_test", - ":unordered_set_members_test", - ":unordered_set_modifiers_test", - "@com_google_googletest//:gtest_main", - ], -) - -cc_test( - name = "unordered_map_test", - srcs = ["internal/unordered_map_test.cc"], - copts = ABSL_TEST_COPTS, - linkopts = ABSL_DEFAULT_LINKOPTS, - tags = NOTEST_TAGS_NONMOBILE, - deps = [ - ":unordered_map_constructor_test", - ":unordered_map_lookup_test", - ":unordered_map_members_test", - ":unordered_map_modifiers_test", - "@com_google_googletest//:gtest_main", - ], -) - -cc_library( - name = "btree", - srcs = [ - "internal/btree.h", - "internal/btree_container.h", - ], - hdrs = [ - "btree_map.h", - "btree_set.h", - ], - copts = ABSL_DEFAULT_COPTS, - linkopts = ABSL_DEFAULT_LINKOPTS, - visibility = ["//visibility:public"], - deps = [ - ":common", - ":compressed_tuple", - ":container_memory", - ":layout", - "//absl/base:core_headers", - "//absl/base:throw_delegate", - "//absl/memory", - "//absl/meta:type_traits", - "//absl/strings", - "//absl/strings:cord", - "//absl/types:compare", - "//absl/utility", - ], -) - -cc_library( - name = "btree_test_common", - testonly = 1, - hdrs = ["btree_test.h"], - copts = ABSL_TEST_COPTS, - linkopts = ABSL_DEFAULT_LINKOPTS, - visibility = ["//visibility:private"], - deps = [ - ":btree", - ":flat_hash_set", - "//absl/strings", - "//absl/strings:cord", - "//absl/time", - ], -) - -cc_test( - name = "btree_test", - size = "large", - srcs = [ - "btree_test.cc", - ], - copts = ABSL_TEST_COPTS, - linkopts = ABSL_DEFAULT_LINKOPTS, - shard_count = 10, - visibility = ["//visibility:private"], - deps = [ - ":btree", - ":btree_test_common", - ":counting_allocator", - ":test_instance_tracker", - "//absl/base:core_headers", - "//absl/base:raw_logging_internal", - "//absl/flags:flag", - "//absl/hash:hash_testing", - "//absl/memory", - "//absl/meta:type_traits", - "//absl/strings", - "//absl/types:compare", - "@com_google_googletest//:gtest_main", - ], -) - -cc_binary( - name = "btree_benchmark", - testonly = 1, - srcs = [ - "btree_benchmark.cc", - ], - copts = ABSL_TEST_COPTS, - linkopts = ABSL_DEFAULT_LINKOPTS, - tags = ["benchmark"], - visibility = ["//visibility:private"], - deps = [ - ":btree", - ":btree_test_common", - ":flat_hash_map", - ":flat_hash_set", - ":hashtable_debug", - "//absl/base:raw_logging_internal", - "//absl/flags:flag", - "//absl/hash", - "//absl/memory", - "//absl/strings:cord", - "//absl/strings:str_format", - "//absl/time", - "@com_github_google_benchmark//:benchmark_main", - ], -) diff --git a/third_party/abseil_cpp/absl/container/CMakeLists.txt b/third_party/abseil_cpp/absl/container/CMakeLists.txt deleted file mode 100644 index eb202c459b27..000000000000 --- a/third_party/abseil_cpp/absl/container/CMakeLists.txt +++ /dev/null @@ -1,905 +0,0 @@ -# -# 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. -# - -# This is deprecated and will be removed in the future. It also doesn't do -# anything anyways. Prefer to use the library associated with the API you are -# using. -absl_cc_library( - NAME - container - PUBLIC -) - -absl_cc_library( - NAME - btree - HDRS - "btree_map.h" - "btree_set.h" - "internal/btree.h" - "internal/btree_container.h" - COPTS - ${ABSL_DEFAULT_COPTS} - LINKOPTS - ${ABSL_DEFAULT_LINKOPTS} - DEPS - absl::container_common - absl::compare - absl::compressed_tuple - absl::container_memory - absl::cord - absl::core_headers - absl::layout - absl::memory - absl::strings - absl::throw_delegate - absl::type_traits - absl::utility -) - -absl_cc_library( - NAME - btree_test_common - hdrs - "btree_test.h" - COPTS - ${ABSL_TEST_COPTS} - LINKOPTS - ${ABSL_DEFAULT_LINKOPTS} - DEPS - absl::btree - absl::cord - absl::flat_hash_set - absl::strings - absl::time - TESTONLY -) - -absl_cc_test( - NAME - btree_test - SRCS - "btree_test.cc" - COPTS - ${ABSL_TEST_COPTS} - LINKOPTS - ${ABSL_DEFAULT_LINKOPTS} - DEPS - absl::btree - absl::btree_test_common - absl::compare - absl::core_headers - absl::counting_allocator - absl::flags - absl::hash_testing - absl::raw_logging_internal - absl::strings - absl::test_instance_tracker - absl::type_traits - gmock_main -) - -absl_cc_library( - NAME - compressed_tuple - HDRS - "internal/compressed_tuple.h" - COPTS - ${ABSL_DEFAULT_COPTS} - DEPS - absl::utility - PUBLIC -) - -absl_cc_test( - NAME - compressed_tuple_test - SRCS - "internal/compressed_tuple_test.cc" - COPTS - ${ABSL_TEST_COPTS} - DEPS - absl::any - absl::compressed_tuple - absl::memory - absl::optional - absl::test_instance_tracker - absl::utility - gmock_main -) - -absl_cc_library( - NAME - fixed_array - HDRS - "fixed_array.h" - COPTS - ${ABSL_DEFAULT_COPTS} - DEPS - absl::compressed_tuple - absl::algorithm - absl::config - absl::core_headers - absl::dynamic_annotations - absl::throw_delegate - absl::memory - PUBLIC -) - -absl_cc_test( - NAME - fixed_array_test - SRCS - "fixed_array_test.cc" - COPTS - ${ABSL_TEST_COPTS} - DEPS - absl::fixed_array - absl::counting_allocator - absl::config - absl::exception_testing - absl::hash_testing - absl::memory - gmock_main -) - -absl_cc_test( - NAME - fixed_array_exception_safety_test - SRCS - "fixed_array_exception_safety_test.cc" - COPTS - ${ABSL_TEST_COPTS} - DEPS - absl::fixed_array - absl::config - absl::exception_safety_testing - gmock_main -) - -absl_cc_library( - NAME - inlined_vector_internal - HDRS - "internal/inlined_vector.h" - COPTS - ${ABSL_DEFAULT_COPTS} - DEPS - absl::compressed_tuple - absl::core_headers - absl::memory - absl::span - absl::type_traits - PUBLIC -) - -absl_cc_library( - NAME - inlined_vector - HDRS - "inlined_vector.h" - COPTS - ${ABSL_DEFAULT_COPTS} - DEPS - absl::algorithm - absl::core_headers - absl::inlined_vector_internal - absl::throw_delegate - absl::memory - PUBLIC -) - -absl_cc_library( - NAME - counting_allocator - HDRS - "internal/counting_allocator.h" - COPTS - ${ABSL_DEFAULT_COPTS} - DEPS - absl::config -) - -absl_cc_test( - NAME - inlined_vector_test - SRCS - "inlined_vector_test.cc" - COPTS - ${ABSL_TEST_COPTS} - DEPS - absl::counting_allocator - absl::inlined_vector - absl::test_instance_tracker - absl::config - absl::core_headers - absl::exception_testing - absl::hash_testing - absl::memory - absl::raw_logging_internal - absl::strings - gmock_main -) - -absl_cc_test( - NAME - inlined_vector_exception_safety_test - SRCS - "inlined_vector_exception_safety_test.cc" - COPTS - ${ABSL_TEST_COPTS} - DEPS - absl::inlined_vector - absl::config - absl::exception_safety_testing - gmock_main -) - -absl_cc_library( - NAME - test_instance_tracker - HDRS - "internal/test_instance_tracker.h" - SRCS - "internal/test_instance_tracker.cc" - COPTS - ${ABSL_DEFAULT_COPTS} - DEPS - absl::compare - TESTONLY -) - -absl_cc_test( - NAME - test_instance_tracker_test - SRCS - "internal/test_instance_tracker_test.cc" - COPTS - ${ABSL_TEST_COPTS} - DEPS - absl::test_instance_tracker - gmock_main -) - -absl_cc_library( - NAME - flat_hash_map - HDRS - "flat_hash_map.h" - COPTS - ${ABSL_DEFAULT_COPTS} - DEPS - absl::container_memory - absl::hash_function_defaults - absl::raw_hash_map - absl::algorithm_container - absl::memory - PUBLIC -) - -absl_cc_test( - NAME - flat_hash_map_test - SRCS - "flat_hash_map_test.cc" - COPTS - ${ABSL_TEST_COPTS} - DEPS - absl::flat_hash_map - absl::hash_generator_testing - absl::unordered_map_constructor_test - absl::unordered_map_lookup_test - absl::unordered_map_members_test - absl::unordered_map_modifiers_test - absl::any - absl::raw_logging_internal - gmock_main -) - -absl_cc_library( - NAME - flat_hash_set - HDRS - "flat_hash_set.h" - COPTS - ${ABSL_DEFAULT_COPTS} - DEPS - absl::container_memory - absl::hash_function_defaults - absl::raw_hash_set - absl::algorithm_container - absl::core_headers - absl::memory - PUBLIC -) - -absl_cc_test( - NAME - flat_hash_set_test - SRCS - "flat_hash_set_test.cc" - COPTS - ${ABSL_TEST_COPTS} - "-DUNORDERED_SET_CXX17" - DEPS - absl::flat_hash_set - absl::hash_generator_testing - absl::unordered_set_constructor_test - absl::unordered_set_lookup_test - absl::unordered_set_members_test - absl::unordered_set_modifiers_test - absl::memory - absl::raw_logging_internal - absl::strings - gmock_main -) - -absl_cc_library( - NAME - node_hash_map - HDRS - "node_hash_map.h" - COPTS - ${ABSL_DEFAULT_COPTS} - DEPS - absl::container_memory - absl::hash_function_defaults - absl::node_hash_policy - absl::raw_hash_map - absl::algorithm_container - absl::memory - PUBLIC -) - -absl_cc_test( - NAME - node_hash_map_test - SRCS - "node_hash_map_test.cc" - COPTS - ${ABSL_TEST_COPTS} - DEPS - absl::hash_generator_testing - absl::node_hash_map - absl::tracked - absl::unordered_map_constructor_test - absl::unordered_map_lookup_test - absl::unordered_map_members_test - absl::unordered_map_modifiers_test - gmock_main -) - -absl_cc_library( - NAME - node_hash_set - HDRS - "node_hash_set.h" - COPTS - ${ABSL_DEFAULT_COPTS} - DEPS - absl::hash_function_defaults - absl::node_hash_policy - absl::raw_hash_set - absl::algorithm_container - absl::memory - PUBLIC -) - -absl_cc_test( - NAME - node_hash_set_test - SRCS - "node_hash_set_test.cc" - COPTS - ${ABSL_TEST_COPTS} - "-DUNORDERED_SET_CXX17" - DEPS - absl::hash_generator_testing - absl::node_hash_set - absl::unordered_set_constructor_test - absl::unordered_set_lookup_test - absl::unordered_set_members_test - absl::unordered_set_modifiers_test - gmock_main -) - -absl_cc_library( - NAME - container_memory - HDRS - "internal/container_memory.h" - COPTS - ${ABSL_DEFAULT_COPTS} - DEPS - absl::config - absl::memory - absl::type_traits - absl::utility - PUBLIC -) - -absl_cc_test( - NAME - container_memory_test - SRCS - "internal/container_memory_test.cc" - COPTS - ${ABSL_TEST_COPTS} - DEPS - absl::container_memory - absl::strings - absl::test_instance_tracker - gmock_main -) - -absl_cc_library( - NAME - hash_function_defaults - HDRS - "internal/hash_function_defaults.h" - COPTS - ${ABSL_DEFAULT_COPTS} - DEPS - absl::config - absl::cord - absl::hash - absl::strings - PUBLIC -) - -absl_cc_test( - NAME - hash_function_defaults_test - SRCS - "internal/hash_function_defaults_test.cc" - COPTS - ${ABSL_TEST_COPTS} - DEPS - absl::cord - absl::cord_test_helpers - absl::hash_function_defaults - absl::hash - absl::random_random - absl::strings - gmock_main -) - -absl_cc_library( - NAME - hash_generator_testing - HDRS - "internal/hash_generator_testing.h" - SRCS - "internal/hash_generator_testing.cc" - COPTS - ${ABSL_TEST_COPTS} - DEPS - absl::hash_policy_testing - absl::memory - absl::meta - absl::strings - TESTONLY -) - -absl_cc_library( - NAME - hash_policy_testing - HDRS - "internal/hash_policy_testing.h" - COPTS - ${ABSL_TEST_COPTS} - DEPS - absl::hash - absl::strings - TESTONLY -) - -absl_cc_test( - NAME - hash_policy_testing_test - SRCS - "internal/hash_policy_testing_test.cc" - COPTS - ${ABSL_TEST_COPTS} - DEPS - absl::hash_policy_testing - gmock_main -) - -absl_cc_library( - NAME - hash_policy_traits - HDRS - "internal/hash_policy_traits.h" - COPTS - ${ABSL_DEFAULT_COPTS} - DEPS - absl::meta - PUBLIC -) - -absl_cc_test( - NAME - hash_policy_traits_test - SRCS - "internal/hash_policy_traits_test.cc" - COPTS - ${ABSL_TEST_COPTS} - DEPS - absl::hash_policy_traits - gmock_main -) - -absl_cc_library( - NAME - hashtablez_sampler - HDRS - "internal/hashtablez_sampler.h" - SRCS - "internal/hashtablez_sampler.cc" - "internal/hashtablez_sampler_force_weak_definition.cc" - COPTS - ${ABSL_DEFAULT_COPTS} - DEPS - absl::base - absl::exponential_biased - absl::have_sse - absl::synchronization -) - -absl_cc_test( - NAME - hashtablez_sampler_test - SRCS - "internal/hashtablez_sampler_test.cc" - COPTS - ${ABSL_TEST_COPTS} - DEPS - absl::hashtablez_sampler - absl::have_sse - gmock_main -) - -absl_cc_library( - NAME - hashtable_debug - HDRS - "internal/hashtable_debug.h" - COPTS - ${ABSL_DEFAULT_COPTS} - DEPS - absl::hashtable_debug_hooks -) - -absl_cc_library( - NAME - hashtable_debug_hooks - HDRS - "internal/hashtable_debug_hooks.h" - COPTS - ${ABSL_DEFAULT_COPTS} - DEPS - absl::config - PUBLIC -) - -absl_cc_library( - NAME - have_sse - HDRS - "internal/have_sse.h" - COPTS - ${ABSL_DEFAULT_COPTS} -) - -absl_cc_library( - NAME - node_hash_policy - HDRS - "internal/node_hash_policy.h" - COPTS - ${ABSL_DEFAULT_COPTS} - DEPS - absl::config - PUBLIC -) - -absl_cc_test( - NAME - node_hash_policy_test - SRCS - "internal/node_hash_policy_test.cc" - COPTS - ${ABSL_TEST_COPTS} - DEPS - absl::hash_policy_traits - absl::node_hash_policy - gmock_main -) - -absl_cc_library( - NAME - raw_hash_map - HDRS - "internal/raw_hash_map.h" - COPTS - ${ABSL_DEFAULT_COPTS} - DEPS - absl::container_memory - absl::raw_hash_set - absl::throw_delegate - PUBLIC -) - -absl_cc_library( - NAME - container_common - HDRS - "internal/common.h" - COPTS - ${ABSL_DEFAULT_COPTS} - DEPS - absl::type_traits -) - -absl_cc_library( - NAME - raw_hash_set - HDRS - "internal/raw_hash_set.h" - SRCS - "internal/raw_hash_set.cc" - COPTS - ${ABSL_DEFAULT_COPTS} - DEPS - absl::bits - absl::compressed_tuple - absl::config - absl::container_common - absl::container_memory - absl::core_headers - absl::endian - absl::hash_policy_traits - absl::hashtable_debug_hooks - absl::have_sse - absl::layout - absl::memory - absl::meta - absl::optional - absl::utility - absl::hashtablez_sampler - PUBLIC -) - -absl_cc_test( - NAME - raw_hash_set_test - SRCS - "internal/raw_hash_set_test.cc" - COPTS - ${ABSL_TEST_COPTS} - DEPS - absl::container_memory - absl::hash_function_defaults - absl::hash_policy_testing - absl::hashtable_debug - absl::raw_hash_set - absl::base - absl::config - absl::core_headers - absl::raw_logging_internal - absl::strings - gmock_main -) - -absl_cc_test( - NAME - raw_hash_set_allocator_test - SRCS - "internal/raw_hash_set_allocator_test.cc" - COPTS - ${ABSL_TEST_COPTS} - DEPS - absl::raw_hash_set - absl::tracked - absl::core_headers - gmock_main -) - -absl_cc_library( - NAME - layout - HDRS - "internal/layout.h" - COPTS - ${ABSL_DEFAULT_COPTS} - DEPS - absl::config - absl::core_headers - absl::meta - absl::strings - absl::span - absl::utility - PUBLIC -) - -absl_cc_test( - NAME - layout_test - SRCS - "internal/layout_test.cc" - COPTS - ${ABSL_TEST_COPTS} - DEPS - absl::layout - absl::config - absl::core_headers - absl::raw_logging_internal - absl::span - gmock_main -) - -absl_cc_library( - NAME - tracked - HDRS - "internal/tracked.h" - COPTS - ${ABSL_TEST_COPTS} - DEPS - absl::config - TESTONLY -) - -absl_cc_library( - NAME - unordered_map_constructor_test - HDRS - "internal/unordered_map_constructor_test.h" - COPTS - ${ABSL_TEST_COPTS} - DEPS - absl::hash_generator_testing - absl::hash_policy_testing - gmock - TESTONLY -) - -absl_cc_library( - NAME - unordered_map_lookup_test - HDRS - "internal/unordered_map_lookup_test.h" - COPTS - ${ABSL_TEST_COPTS} - DEPS - absl::hash_generator_testing - absl::hash_policy_testing - gmock - TESTONLY -) - -absl_cc_library( - NAME - unordered_map_members_test - HDRS - "internal/unordered_map_members_test.h" - COPTS - ${ABSL_TEST_COPTS} - DEPS - absl::type_traits - gmock - TESTONLY -) - -absl_cc_library( - NAME - unordered_map_modifiers_test - HDRS - "internal/unordered_map_modifiers_test.h" - COPTS - ${ABSL_TEST_COPTS} - DEPS - absl::hash_generator_testing - absl::hash_policy_testing - gmock - TESTONLY -) - -absl_cc_library( - NAME - unordered_set_constructor_test - HDRS - "internal/unordered_set_constructor_test.h" - COPTS - ${ABSL_TEST_COPTS} - DEPS - absl::hash_generator_testing - absl::hash_policy_testing - gmock - TESTONLY -) - -absl_cc_library( - NAME - unordered_set_lookup_test - HDRS - "internal/unordered_set_lookup_test.h" - COPTS - ${ABSL_TEST_COPTS} - DEPS - absl::hash_generator_testing - absl::hash_policy_testing - gmock - TESTONLY -) - -absl_cc_library( - NAME - unordered_set_members_test - HDRS - "internal/unordered_set_members_test.h" - COPTS - ${ABSL_TEST_COPTS} - DEPS - absl::type_traits - gmock - TESTONLY -) - -absl_cc_library( - NAME - unordered_set_modifiers_test - HDRS - "internal/unordered_set_modifiers_test.h" - COPTS - ${ABSL_TEST_COPTS} - DEPS - absl::hash_generator_testing - absl::hash_policy_testing - gmock - TESTONLY -) - -absl_cc_test( - NAME - unordered_set_test - SRCS - "internal/unordered_set_test.cc" - COPTS - ${ABSL_TEST_COPTS} - DEPS - absl::unordered_set_constructor_test - absl::unordered_set_lookup_test - absl::unordered_set_members_test - absl::unordered_set_modifiers_test - gmock_main -) - -absl_cc_test( - NAME - unordered_map_test - SRCS - "internal/unordered_map_test.cc" - COPTS - ${ABSL_TEST_COPTS} - DEPS - absl::unordered_map_constructor_test - absl::unordered_map_lookup_test - absl::unordered_map_members_test - absl::unordered_map_modifiers_test - gmock_main -) diff --git a/third_party/abseil_cpp/absl/container/btree_benchmark.cc b/third_party/abseil_cpp/absl/container/btree_benchmark.cc deleted file mode 100644 index 467986768aa1..000000000000 --- a/third_party/abseil_cpp/absl/container/btree_benchmark.cc +++ /dev/null @@ -1,735 +0,0 @@ -// 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 - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#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/flags/flag.h" -#include "absl/hash/hash.h" -#include "absl/memory/memory.h" -#include "absl/strings/cord.h" -#include "absl/strings/str_format.h" -#include "absl/time/time.h" -#include "benchmark/benchmark.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 -std::vector GenerateValues(int n) { - constexpr int kSeed = 23; - return GenerateValuesWithSeed(n, 4 * n, kSeed); -} - -// Benchmark insertion of values into a container. -template -void BM_InsertImpl(benchmark::State& state, bool sorted) { - using V = typename remove_pair_const::type; - typename KeyOfValue::type key_of_value; - - std::vector values = GenerateValues(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(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 -void BM_Insert(benchmark::State& state) { - BM_InsertImpl(state, false); -} - -template -void BM_InsertSorted(benchmark::State& state) { - BM_InsertImpl(state, true); -} - -// container::insert sometimes returns a pair and sometimes -// returns an iterator (for multi- containers). -template -Iter GetIterFromInsert(const std::pair& pair) { - return pair.first; -} -template -Iter GetIterFromInsert(const Iter iter) { - return iter; -} - -// Benchmark insertion of values into a container at the end. -template -void BM_InsertEnd(benchmark::State& state) { - using V = typename remove_pair_const::type; - typename KeyOfValue::type key_of_value; - - T container; - const int kSize = 10000; - for (int i = 0; i < kSize; ++i) { - container.insert(Generator(kSize)(i)); - } - V v = Generator(kSize)(kSize - 1); - typename T::key_type k = key_of_value(v); - - auto it = container.find(k); - while (state.KeepRunning()) { - // Repeatedly removing then adding v. - container.erase(it); - it = GetIterFromInsert(container.insert(v)); - } -} - -// Benchmark inserting the first few elements in a container. In b-tree, this is -// when the root node grows. -template -void BM_InsertSmall(benchmark::State& state) { - using V = typename remove_pair_const::type; - - const int kSize = 8; - std::vector values = GenerateValues(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 -void BM_LookupImpl(benchmark::State& state, bool sorted) { - using V = typename remove_pair_const::type; - typename KeyOfValue::type key_of_value; - - std::vector values = GenerateValues(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 -void BM_Lookup(benchmark::State& state) { - BM_LookupImpl(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 -void BM_FullLookup(benchmark::State& state) { - BM_LookupImpl(state, true); -} - -// Benchmark deletion of values from a container. -template -void BM_Delete(benchmark::State& state) { - using V = typename remove_pair_const::type; - typename KeyOfValue::type key_of_value; - std::vector values = GenerateValues(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 deletion of multiple values from a container. -template -void BM_DeleteRange(benchmark::State& state) { - using V = typename remove_pair_const::type; - typename KeyOfValue::type key_of_value; - std::vector values = GenerateValues(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 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(); - } -} - -// 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 -void BM_QueueAddRem(benchmark::State& state) { - using V = typename remove_pair_const::type; - typename KeyOfValue::type key_of_value; - - ABSL_RAW_CHECK(kBenchmarkValues % 2 == 0, "for performance"); - - T container; - - const size_t half = kBenchmarkValues / 2; - std::vector remove_keys(half); - std::vector 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 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 -void BM_MixedAddRem(benchmark::State& state) { - using V = typename remove_pair_const::type; - typename KeyOfValue::type key_of_value; - - ABSL_RAW_CHECK(kBenchmarkValues % 2 == 0, "for performance"); - - T container; - - // Create two random shuffles - std::vector remove_keys(kBenchmarkValues); - std::vector 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 values = GenerateValues(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 -void BM_Fifo(benchmark::State& state) { - using V = typename remove_pair_const::type; - - T container; - // Need lazy generation of values as state.max_iterations is large. - Generator 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 -void BM_FwdIter(benchmark::State& state) { - using V = typename remove_pair_const::type; - using R = typename T::value_type const*; - - std::vector values = GenerateValues(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 -void BM_RangeConstructionImpl(benchmark::State& state, bool sorted) { - using V = typename remove_pair_const::type; - - std::vector values = GenerateValues(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 -void BM_InsertRangeRandom(benchmark::State& state) { - BM_RangeConstructionImpl(state, false); -} - -template -void BM_InsertRangeSorted(benchmark::State& state) { - BM_RangeConstructionImpl(state, true); -} - -#define STL_ORDERED_TYPES(value) \ - using stl_set_##value = std::set; \ - using stl_map_##value = std::map; \ - using stl_multiset_##value = std::multiset; \ - using stl_multimap_##value = std::multimap - -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; \ - using stl_unordered_map_##value = std::unordered_map; \ - using flat_hash_set_##value = flat_hash_set; \ - using flat_hash_map_##value = flat_hash_map; \ - using stl_unordered_multiset_##value = std::unordered_multiset; \ - using stl_unordered_multimap_##value = \ - std::unordered_multimap - -#define STL_UNORDERED_TYPES_CUSTOM_HASH(value, hash) \ - using stl_unordered_set_##value = std::unordered_set; \ - using stl_unordered_map_##value = std::unordered_map; \ - using flat_hash_set_##value = flat_hash_set; \ - using flat_hash_map_##value = flat_hash_map; \ - using stl_unordered_multiset_##value = std::unordered_multiset; \ - using stl_unordered_multimap_##value = \ - std::unordered_multimap - -STL_UNORDERED_TYPES_CUSTOM_HASH(Cord, absl::Hash); - -STL_UNORDERED_TYPES(int32_t); -STL_UNORDERED_TYPES(int64_t); -STL_UNORDERED_TYPES(StdString); -STL_UNORDERED_TYPES_CUSTOM_HASH(Time, absl::Hash); - -#define BTREE_TYPES(value) \ - using btree_256_set_##value = \ - btree_set, std::allocator>; \ - using btree_256_map_##value = \ - btree_map, \ - std::allocator>>; \ - using btree_256_multiset_##value = \ - btree_multiset, std::allocator>; \ - using btree_256_multimap_##value = \ - btree_multimap, \ - std::allocator>> - -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(state); } \ - BENCHMARK(BM_##type##_##func) - -#define MY_BENCHMARK3(type) \ - MY_BENCHMARK4(type, Insert); \ - MY_BENCHMARK4(type, InsertSorted); \ - MY_BENCHMARK4(type, InsertEnd); \ - MY_BENCHMARK4(type, InsertSmall); \ - MY_BENCHMARK4(type, Lookup); \ - MY_BENCHMARK4(type, FullLookup); \ - MY_BENCHMARK4(type, Delete); \ - MY_BENCHMARK4(type, DeleteRange); \ - 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_BENCHMARK2_SUPPORTS_MULTI_ONLY(type) \ - MY_BENCHMARK3(stl_##type); \ - MY_BENCHMARK3(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 -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 - 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 values; -}; - -#define BIG_TYPE_BENCHMARKS(SIZE, COPIES) \ - using stl_set_size##SIZE##copies##COPIES = std::set>; \ - using stl_map_size##SIZE##copies##COPIES = \ - std::map, intptr_t>; \ - using stl_multiset_size##SIZE##copies##COPIES = \ - std::multiset>; \ - using stl_multimap_size##SIZE##copies##COPIES = \ - std::multimap, intptr_t>; \ - using stl_unordered_set_size##SIZE##copies##COPIES = \ - std::unordered_set, \ - absl::Hash>>; \ - using stl_unordered_map_size##SIZE##copies##COPIES = \ - std::unordered_map, intptr_t, \ - absl::Hash>>; \ - using flat_hash_set_size##SIZE##copies##COPIES = \ - flat_hash_set>; \ - using flat_hash_map_size##SIZE##copies##COPIES = \ - flat_hash_map, intptr_t>; \ - using stl_unordered_multiset_size##SIZE##copies##COPIES = \ - std::unordered_multiset, \ - absl::Hash>>; \ - using stl_unordered_multimap_size##SIZE##copies##COPIES = \ - std::unordered_multimap, intptr_t, \ - absl::Hash>>; \ - using btree_256_set_size##SIZE##copies##COPIES = \ - btree_set>; \ - using btree_256_map_size##SIZE##copies##COPIES = \ - btree_map, intptr_t>; \ - using btree_256_multiset_size##SIZE##copies##COPIES = \ - btree_multiset>; \ - using btree_256_multimap_size##SIZE##copies##COPIES = \ - btree_multimap, 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 -struct BigTypePtr { - BigTypePtr() : BigTypePtr(0) {} - explicit BigTypePtr(int x) { - ptr = absl::make_unique>(x); - } - BigTypePtr(const BigTypePtr& other) { - ptr = absl::make_unique>(*other.ptr); - } - BigTypePtr(BigTypePtr&& other) noexcept = default; - BigTypePtr& operator=(const BigTypePtr& other) { - ptr = absl::make_unique>(*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> ptr; -}; - -template -double ContainerInfo(const btree_set>& b) { - const double bytes_used = - b.bytes_used() + b.size() * sizeof(BigType); - const double bytes_per_value = bytes_used / b.size(); - BtreeContainerInfoLog(b, bytes_used, bytes_per_value); - return bytes_per_value; -} -template -double ContainerInfo(const btree_map>& b) { - const double bytes_used = - b.bytes_used() + b.size() * sizeof(BigType); - 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>; \ - using stl_map_size##SIZE##copies##SIZE##ptr = \ - std::map>; \ - using stl_unordered_set_size##SIZE##copies##SIZE##ptr = \ - std::unordered_set, \ - absl::Hash>>; \ - using stl_unordered_map_size##SIZE##copies##SIZE##ptr = \ - std::unordered_map>; \ - using flat_hash_set_size##SIZE##copies##SIZE##ptr = \ - flat_hash_set>; \ - using flat_hash_map_size##SIZE##copies##SIZE##ptr = \ - flat_hash_map>; \ - using btree_256_set_size##SIZE##copies##SIZE##ptr = \ - btree_set>; \ - using btree_256_map_size##SIZE##copies##SIZE##ptr = \ - btree_map>; \ - MY_BENCHMARK3(stl_set_size##SIZE##copies##SIZE##ptr); \ - MY_BENCHMARK3(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_map_size##SIZE##copies##SIZE##ptr); \ - MY_BENCHMARK3(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); - -} // namespace -} // namespace container_internal -ABSL_NAMESPACE_END -} // namespace absl diff --git a/third_party/abseil_cpp/absl/container/btree_map.h b/third_party/abseil_cpp/absl/container/btree_map.h deleted file mode 100644 index abc09b0ac068..000000000000 --- a/third_party/abseil_cpp/absl/container/btree_map.h +++ /dev/null @@ -1,769 +0,0 @@ -// 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. -// -// 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()` and `erase()` return a valid iterator at the current -// position. - -#ifndef ABSL_CONTAINER_BTREE_MAP_H_ -#define ABSL_CONTAINER_BTREE_MAP_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 - -// absl::btree_map<> -// -// An `absl::btree_map` 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`. -// -// An `absl::btree_map` uses a default allocator of -// `std::allocator>` 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`. -// -template , - typename Alloc = std::allocator>> -class btree_map - : public container_internal::btree_map_container< - container_internal::btree>> { - 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 map1; - // - // * Initializer List constructor - // - // absl::btree_map map2 = - // {{1, "huey"}, {2, "dewey"}, {3, "louie"},}; - // - // * Copy constructor - // - // absl::btree_map map3(map2); - // - // * Copy assignment operator - // - // absl::btree_map map4; - // map4 = map3; - // - // * Move constructor - // - // // Move is guaranteed efficient - // absl::btree_map map5(std::move(map4)); - // - // * Move assignment operator - // - // // May be efficient if allocators are compatible - // absl::btree_map map6; - // map6 = std::move(map5); - // - // * Range constructor - // - // std::vector> v = {{1, "a"}, {2, "b"}}; - // absl::btree_map 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`. - 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 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 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 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 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 insert_or_assign(const key_type& k, M&& obj): - // pair 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 try_emplace(const key_type& k, Args&&... args): - // std::pair 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. 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 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::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 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 is provided a - // compatible heterogeneous comparator. - using Base::contains; - - // btree_map::count() - // - // template 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 is provided a - // compatible heterogeneous comparator. - using Base::count; - - // btree_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 - // `btree_map`. - using Base::equal_range; - - // btree_map::find() - // - // template iterator find(const K& key): - // template 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 is provided a - // compatible heterogeneous comparator. - using Base::find; - - // 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 -void swap(btree_map &x, btree_map &y) { - return x.swap(y); -} - -// absl::erase_if(absl::btree_map<>, Pred) -// -// Erases all elements that satisfy the predicate pred from the container. -template -void erase_if(btree_map &map, Pred pred) { - for (auto it = map.begin(); it != map.end();) { - if (pred(*it)) { - it = map.erase(it); - } else { - ++it; - } - } -} - -// absl::btree_multimap -// -// An `absl::btree_multimap` 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`. -// -// An `absl::btree_multimap` uses a default allocator of -// `std::allocator>` 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`. -// -template , - typename Alloc = std::allocator>> -class btree_multimap - : public container_internal::btree_multimap_container< - container_internal::btree>> { - 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 map1; - // - // * Initializer List constructor - // - // absl::btree_multimap map2 = - // {{1, "huey"}, {2, "dewey"}, {3, "louie"},}; - // - // * Copy constructor - // - // absl::btree_multimap map3(map2); - // - // * Copy assignment operator - // - // absl::btree_multimap map4; - // map4 = map3; - // - // * Move constructor - // - // // Move is guaranteed efficient - // absl::btree_multimap map5(std::move(map4)); - // - // * Move assignment operator - // - // // May be efficient if allocators are compatible - // absl::btree_multimap map6; - // map6 = std::move(map5); - // - // * Range constructor - // - // std::vector> v = {{1, "a"}, {2, "b"}}; - // absl::btree_multimap 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`. - 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 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 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 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::merge() - // - // Extracts elements from a given `source` btree_multimap into this - // `btree_multimap`. If the destination `btree_multimap` already contains an - // element with an equivalent key, that element is not extracted. - 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 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 is provided a - // compatible heterogeneous comparator. - using Base::contains; - - // btree_multimap::count() - // - // template 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 is provided a - // compatible heterogeneous comparator. - using Base::count; - - // btree_multimap::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_multimap`. - using Base::equal_range; - - // btree_multimap::find() - // - // template iterator find(const K& key): - // template 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 is provided a - // compatible heterogeneous comparator. - using Base::find; - - // 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 -void swap(btree_multimap &x, btree_multimap &y) { - return x.swap(y); -} - -// absl::erase_if(absl::btree_multimap<>, Pred) -// -// Erases all elements that satisfy the predicate pred from the container. -template -void erase_if(btree_multimap &map, Pred pred) { - for (auto it = map.begin(); it != map.end();) { - if (pred(*it)) { - it = map.erase(it); - } else { - ++it; - } - } -} - -ABSL_NAMESPACE_END -} // namespace absl - -#endif // ABSL_CONTAINER_BTREE_MAP_H_ diff --git a/third_party/abseil_cpp/absl/container/btree_set.h b/third_party/abseil_cpp/absl/container/btree_set.h deleted file mode 100644 index 21ef0a032a85..000000000000 --- a/third_party/abseil_cpp/absl/container/btree_set.h +++ /dev/null @@ -1,683 +0,0 @@ -// 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. -// -// 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()` and `erase()` return a valid iterator at the current -// position. - -#ifndef ABSL_CONTAINER_BTREE_SET_H_ -#define ABSL_CONTAINER_BTREE_SET_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 - -// absl::btree_set<> -// -// An `absl::btree_set` 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`. -// -// An `absl::btree_set` uses a default allocator of `std::allocator` 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`. -// -template , - typename Alloc = std::allocator> -class btree_set - : public container_internal::btree_set_container< - container_internal::btree>> { - 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 set1; - // - // * Initializer List constructor - // - // absl::btree_set set2 = - // {{"huey"}, {"dewey"}, {"louie"},}; - // - // * Copy constructor - // - // absl::btree_set set3(set2); - // - // * Copy assignment operator - // - // absl::btree_set set4; - // set4 = set3; - // - // * Move constructor - // - // // Move is guaranteed efficient - // absl::btree_set set5(std::move(set4)); - // - // * Move assignment operator - // - // // May be efficient if allocators are compatible - // absl::btree_set set6; - // set6 = std::move(set5); - // - // * Range constructor - // - // std::vector v = {"a", "b"}; - // absl::btree_set 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`. - 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 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 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 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 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. 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 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::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 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 is provided a - // compatible heterogeneous comparator. - using Base::contains; - - // btree_set::count() - // - // template 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 is provided 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 iterator find(const K& key): - // template 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 is provided a - // compatible heterogeneous comparator. - using Base::find; - - // 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 -void swap(btree_set &x, btree_set &y) { - return x.swap(y); -} - -// absl::erase_if(absl::btree_set<>, Pred) -// -// Erases all elements that satisfy the predicate pred from the container. -template -void erase_if(btree_set &set, Pred pred) { - for (auto it = set.begin(); it != set.end();) { - if (pred(*it)) { - it = set.erase(it); - } else { - ++it; - } - } -} - -// absl::btree_multiset<> -// -// An `absl::btree_multiset` 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`. -// -// An `absl::btree_multiset` uses a default allocator of `std::allocator` -// 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`. -// -template , - typename Alloc = std::allocator> -class btree_multiset - : public container_internal::btree_multiset_container< - container_internal::btree>> { - 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 set1; - // - // * Initializer List constructor - // - // absl::btree_multiset set2 = - // {{"huey"}, {"dewey"}, {"louie"},}; - // - // * Copy constructor - // - // absl::btree_multiset set3(set2); - // - // * Copy assignment operator - // - // absl::btree_multiset set4; - // set4 = set3; - // - // * Move constructor - // - // // Move is guaranteed efficient - // absl::btree_multiset set5(std::move(set4)); - // - // * Move assignment operator - // - // // May be efficient if allocators are compatible - // absl::btree_multiset set6; - // set6 = std::move(set5); - // - // * Range constructor - // - // std::vector v = {"a", "b"}; - // absl::btree_multiset 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`. - 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 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 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 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::merge() - // - // Extracts elements from a given `source` btree_multiset into this - // `btree_multiset`. If the destination `btree_multiset` already contains an - // element with an equivalent key, that element is not extracted. - 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 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 is provided a - // compatible heterogeneous comparator. - using Base::contains; - - // btree_multiset::count() - // - // template 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 is provided 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 iterator find(const K& key): - // template 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 is provided a - // compatible heterogeneous comparator. - using Base::find; - - // 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 -void swap(btree_multiset &x, btree_multiset &y) { - return x.swap(y); -} - -// absl::erase_if(absl::btree_multiset<>, Pred) -// -// Erases all elements that satisfy the predicate pred from the container. -template -void erase_if(btree_multiset &set, Pred pred) { - for (auto it = set.begin(); it != set.end();) { - if (pred(*it)) { - it = set.erase(it); - } else { - ++it; - } - } -} - -ABSL_NAMESPACE_END -} // namespace absl - -#endif // ABSL_CONTAINER_BTREE_SET_H_ diff --git a/third_party/abseil_cpp/absl/container/btree_test.cc b/third_party/abseil_cpp/absl/container/btree_test.cc deleted file mode 100644 index 9b1b6436c7ca..000000000000 --- a/third_party/abseil_cpp/absl/container/btree_test.cc +++ /dev/null @@ -1,2827 +0,0 @@ -// 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/btree_test.h" - -#include -#include -#include -#include -#include -#include -#include -#include - -#include "gmock/gmock.h" -#include "gtest/gtest.h" -#include "absl/base/internal/raw_logging.h" -#include "absl/base/macros.h" -#include "absl/container/btree_map.h" -#include "absl/container/btree_set.h" -#include "absl/container/internal/counting_allocator.h" -#include "absl/container/internal/test_instance_tracker.h" -#include "absl/flags/flag.h" -#include "absl/hash/hash_testing.h" -#include "absl/memory/memory.h" -#include "absl/meta/type_traits.h" -#include "absl/strings/str_cat.h" -#include "absl/strings/str_split.h" -#include "absl/strings/string_view.h" -#include "absl/types/compare.h" - -ABSL_FLAG(int, test_values, 10000, "The number of values to use for tests"); - -namespace absl { -ABSL_NAMESPACE_BEGIN -namespace container_internal { -namespace { - -using ::absl::test_internal::CopyableMovableInstance; -using ::absl::test_internal::InstanceTracker; -using ::absl::test_internal::MovableOnlyInstance; -using ::testing::ElementsAre; -using ::testing::ElementsAreArray; -using ::testing::IsEmpty; -using ::testing::IsNull; -using ::testing::Pair; -using ::testing::SizeIs; - -template -void CheckPairEquals(const T &x, const U &y) { - ABSL_INTERNAL_CHECK(x == y, "Values are unequal."); -} - -template -void CheckPairEquals(const std::pair &x, const std::pair &y) { - CheckPairEquals(x.first, y.first); - CheckPairEquals(x.second, y.second); -} -} // namespace - -// The base class for a sorted associative container checker. TreeType is the -// container type to check and CheckerType is the container type to check -// against. TreeType is expected to be btree_{set,map,multiset,multimap} and -// CheckerType is expected to be {set,map,multiset,multimap}. -template -class base_checker { - public: - using key_type = typename TreeType::key_type; - using value_type = typename TreeType::value_type; - using key_compare = typename TreeType::key_compare; - using pointer = typename TreeType::pointer; - using const_pointer = typename TreeType::const_pointer; - using reference = typename TreeType::reference; - using const_reference = typename TreeType::const_reference; - using size_type = typename TreeType::size_type; - using difference_type = typename TreeType::difference_type; - using iterator = typename TreeType::iterator; - using const_iterator = typename TreeType::const_iterator; - using reverse_iterator = typename TreeType::reverse_iterator; - using const_reverse_iterator = typename TreeType::const_reverse_iterator; - - public: - base_checker() : const_tree_(tree_) {} - base_checker(const base_checker &other) - : tree_(other.tree_), const_tree_(tree_), checker_(other.checker_) {} - template - base_checker(InputIterator b, InputIterator e) - : tree_(b, e), const_tree_(tree_), checker_(b, e) {} - - iterator begin() { return tree_.begin(); } - const_iterator begin() const { return tree_.begin(); } - iterator end() { return tree_.end(); } - const_iterator end() const { return tree_.end(); } - reverse_iterator rbegin() { return tree_.rbegin(); } - const_reverse_iterator rbegin() const { return tree_.rbegin(); } - reverse_iterator rend() { return tree_.rend(); } - const_reverse_iterator rend() const { return tree_.rend(); } - - template - IterType iter_check(IterType tree_iter, CheckerIterType checker_iter) const { - if (tree_iter == tree_.end()) { - ABSL_INTERNAL_CHECK(checker_iter == checker_.end(), - "Checker iterator not at end."); - } else { - CheckPairEquals(*tree_iter, *checker_iter); - } - return tree_iter; - } - template - IterType riter_check(IterType tree_iter, CheckerIterType checker_iter) const { - if (tree_iter == tree_.rend()) { - ABSL_INTERNAL_CHECK(checker_iter == checker_.rend(), - "Checker iterator not at rend."); - } else { - CheckPairEquals(*tree_iter, *checker_iter); - } - return tree_iter; - } - void value_check(const value_type &v) { - typename KeyOfValue::type key_of_value; - const key_type &key = key_of_value(v); - CheckPairEquals(*find(key), v); - lower_bound(key); - upper_bound(key); - equal_range(key); - contains(key); - count(key); - } - void erase_check(const key_type &key) { - EXPECT_FALSE(tree_.contains(key)); - EXPECT_EQ(tree_.find(key), const_tree_.end()); - EXPECT_FALSE(const_tree_.contains(key)); - EXPECT_EQ(const_tree_.find(key), tree_.end()); - EXPECT_EQ(tree_.equal_range(key).first, - const_tree_.equal_range(key).second); - } - - iterator lower_bound(const key_type &key) { - return iter_check(tree_.lower_bound(key), checker_.lower_bound(key)); - } - const_iterator lower_bound(const key_type &key) const { - return iter_check(tree_.lower_bound(key), checker_.lower_bound(key)); - } - iterator upper_bound(const key_type &key) { - return iter_check(tree_.upper_bound(key), checker_.upper_bound(key)); - } - const_iterator upper_bound(const key_type &key) const { - return iter_check(tree_.upper_bound(key), checker_.upper_bound(key)); - } - std::pair equal_range(const key_type &key) { - std::pair - checker_res = checker_.equal_range(key); - std::pair tree_res = tree_.equal_range(key); - iter_check(tree_res.first, checker_res.first); - iter_check(tree_res.second, checker_res.second); - return tree_res; - } - std::pair equal_range( - const key_type &key) const { - std::pair - checker_res = checker_.equal_range(key); - std::pair tree_res = tree_.equal_range(key); - iter_check(tree_res.first, checker_res.first); - iter_check(tree_res.second, checker_res.second); - return tree_res; - } - iterator find(const key_type &key) { - return iter_check(tree_.find(key), checker_.find(key)); - } - const_iterator find(const key_type &key) const { - return iter_check(tree_.find(key), checker_.find(key)); - } - bool contains(const key_type &key) const { return find(key) != end(); } - size_type count(const key_type &key) const { - size_type res = checker_.count(key); - EXPECT_EQ(res, tree_.count(key)); - return res; - } - - base_checker &operator=(const base_checker &other) { - tree_ = other.tree_; - checker_ = other.checker_; - return *this; - } - - int erase(const key_type &key) { - int size = tree_.size(); - int res = checker_.erase(key); - EXPECT_EQ(res, tree_.count(key)); - EXPECT_EQ(res, tree_.erase(key)); - EXPECT_EQ(tree_.count(key), 0); - EXPECT_EQ(tree_.size(), size - res); - erase_check(key); - return res; - } - iterator erase(iterator iter) { - key_type key = iter.key(); - int size = tree_.size(); - int count = tree_.count(key); - auto checker_iter = checker_.lower_bound(key); - for (iterator tmp(tree_.lower_bound(key)); tmp != iter; ++tmp) { - ++checker_iter; - } - auto checker_next = checker_iter; - ++checker_next; - checker_.erase(checker_iter); - iter = tree_.erase(iter); - EXPECT_EQ(tree_.size(), checker_.size()); - EXPECT_EQ(tree_.size(), size - 1); - EXPECT_EQ(tree_.count(key), count - 1); - if (count == 1) { - erase_check(key); - } - return iter_check(iter, checker_next); - } - - void erase(iterator begin, iterator end) { - int size = tree_.size(); - int count = std::distance(begin, end); - auto checker_begin = checker_.lower_bound(begin.key()); - for (iterator tmp(tree_.lower_bound(begin.key())); tmp != begin; ++tmp) { - ++checker_begin; - } - auto checker_end = - end == tree_.end() ? checker_.end() : checker_.lower_bound(end.key()); - if (end != tree_.end()) { - for (iterator tmp(tree_.lower_bound(end.key())); tmp != end; ++tmp) { - ++checker_end; - } - } - const auto checker_ret = checker_.erase(checker_begin, checker_end); - const auto tree_ret = tree_.erase(begin, end); - EXPECT_EQ(std::distance(checker_.begin(), checker_ret), - std::distance(tree_.begin(), tree_ret)); - EXPECT_EQ(tree_.size(), checker_.size()); - EXPECT_EQ(tree_.size(), size - count); - } - - void clear() { - tree_.clear(); - checker_.clear(); - } - void swap(base_checker &other) { - tree_.swap(other.tree_); - checker_.swap(other.checker_); - } - - void verify() const { - tree_.verify(); - EXPECT_EQ(tree_.size(), checker_.size()); - - // Move through the forward iterators using increment. - auto checker_iter = checker_.begin(); - const_iterator tree_iter(tree_.begin()); - for (; tree_iter != tree_.end(); ++tree_iter, ++checker_iter) { - CheckPairEquals(*tree_iter, *checker_iter); - } - - // Move through the forward iterators using decrement. - for (int n = tree_.size() - 1; n >= 0; --n) { - iter_check(tree_iter, checker_iter); - --tree_iter; - --checker_iter; - } - EXPECT_EQ(tree_iter, tree_.begin()); - EXPECT_EQ(checker_iter, checker_.begin()); - - // Move through the reverse iterators using increment. - auto checker_riter = checker_.rbegin(); - const_reverse_iterator tree_riter(tree_.rbegin()); - for (; tree_riter != tree_.rend(); ++tree_riter, ++checker_riter) { - CheckPairEquals(*tree_riter, *checker_riter); - } - - // Move through the reverse iterators using decrement. - for (int n = tree_.size() - 1; n >= 0; --n) { - riter_check(tree_riter, checker_riter); - --tree_riter; - --checker_riter; - } - EXPECT_EQ(tree_riter, tree_.rbegin()); - EXPECT_EQ(checker_riter, checker_.rbegin()); - } - - const TreeType &tree() const { return tree_; } - - size_type size() const { - EXPECT_EQ(tree_.size(), checker_.size()); - return tree_.size(); - } - size_type max_size() const { return tree_.max_size(); } - bool empty() const { - EXPECT_EQ(tree_.empty(), checker_.empty()); - return tree_.empty(); - } - - protected: - TreeType tree_; - const TreeType &const_tree_; - CheckerType checker_; -}; - -namespace { -// A checker for unique sorted associative containers. TreeType is expected to -// be btree_{set,map} and CheckerType is expected to be {set,map}. -template -class unique_checker : public base_checker { - using super_type = base_checker; - - public: - using iterator = typename super_type::iterator; - using value_type = typename super_type::value_type; - - public: - unique_checker() : super_type() {} - unique_checker(const unique_checker &other) : super_type(other) {} - template - unique_checker(InputIterator b, InputIterator e) : super_type(b, e) {} - unique_checker &operator=(const unique_checker &) = default; - - // Insertion routines. - std::pair insert(const value_type &v) { - int size = this->tree_.size(); - std::pair checker_res = - this->checker_.insert(v); - std::pair tree_res = this->tree_.insert(v); - CheckPairEquals(*tree_res.first, *checker_res.first); - EXPECT_EQ(tree_res.second, checker_res.second); - EXPECT_EQ(this->tree_.size(), this->checker_.size()); - EXPECT_EQ(this->tree_.size(), size + tree_res.second); - return tree_res; - } - iterator insert(iterator position, const value_type &v) { - int size = this->tree_.size(); - std::pair checker_res = - this->checker_.insert(v); - iterator tree_res = this->tree_.insert(position, v); - CheckPairEquals(*tree_res, *checker_res.first); - EXPECT_EQ(this->tree_.size(), this->checker_.size()); - EXPECT_EQ(this->tree_.size(), size + checker_res.second); - return tree_res; - } - template - void insert(InputIterator b, InputIterator e) { - for (; b != e; ++b) { - insert(*b); - } - } -}; - -// A checker for multiple sorted associative containers. TreeType is expected -// to be btree_{multiset,multimap} and CheckerType is expected to be -// {multiset,multimap}. -template -class multi_checker : public base_checker { - using super_type = base_checker; - - public: - using iterator = typename super_type::iterator; - using value_type = typename super_type::value_type; - - public: - multi_checker() : super_type() {} - multi_checker(const multi_checker &other) : super_type(other) {} - template - multi_checker(InputIterator b, InputIterator e) : super_type(b, e) {} - multi_checker &operator=(const multi_checker &) = default; - - // Insertion routines. - iterator insert(const value_type &v) { - int size = this->tree_.size(); - auto checker_res = this->checker_.insert(v); - iterator tree_res = this->tree_.insert(v); - CheckPairEquals(*tree_res, *checker_res); - EXPECT_EQ(this->tree_.size(), this->checker_.size()); - EXPECT_EQ(this->tree_.size(), size + 1); - return tree_res; - } - iterator insert(iterator position, const value_type &v) { - int size = this->tree_.size(); - auto checker_res = this->checker_.insert(v); - iterator tree_res = this->tree_.insert(position, v); - CheckPairEquals(*tree_res, *checker_res); - EXPECT_EQ(this->tree_.size(), this->checker_.size()); - EXPECT_EQ(this->tree_.size(), size + 1); - return tree_res; - } - template - void insert(InputIterator b, InputIterator e) { - for (; b != e; ++b) { - insert(*b); - } - } -}; - -template -void DoTest(const char *name, T *b, const std::vector &values) { - typename KeyOfValue::type key_of_value; - - T &mutable_b = *b; - const T &const_b = *b; - - // Test insert. - for (int i = 0; i < values.size(); ++i) { - mutable_b.insert(values[i]); - mutable_b.value_check(values[i]); - } - ASSERT_EQ(mutable_b.size(), values.size()); - - const_b.verify(); - - // Test copy constructor. - T b_copy(const_b); - EXPECT_EQ(b_copy.size(), const_b.size()); - for (int i = 0; i < values.size(); ++i) { - CheckPairEquals(*b_copy.find(key_of_value(values[i])), values[i]); - } - - // Test range constructor. - T b_range(const_b.begin(), const_b.end()); - EXPECT_EQ(b_range.size(), const_b.size()); - for (int i = 0; i < values.size(); ++i) { - CheckPairEquals(*b_range.find(key_of_value(values[i])), values[i]); - } - - // Test range insertion for values that already exist. - b_range.insert(b_copy.begin(), b_copy.end()); - b_range.verify(); - - // Test range insertion for new values. - b_range.clear(); - b_range.insert(b_copy.begin(), b_copy.end()); - EXPECT_EQ(b_range.size(), b_copy.size()); - for (int i = 0; i < values.size(); ++i) { - CheckPairEquals(*b_range.find(key_of_value(values[i])), values[i]); - } - - // Test assignment to self. Nothing should change. - b_range.operator=(b_range); - EXPECT_EQ(b_range.size(), b_copy.size()); - - // Test assignment of new values. - b_range.clear(); - b_range = b_copy; - EXPECT_EQ(b_range.size(), b_copy.size()); - - // Test swap. - b_range.clear(); - b_range.swap(b_copy); - EXPECT_EQ(b_copy.size(), 0); - EXPECT_EQ(b_range.size(), const_b.size()); - for (int i = 0; i < values.size(); ++i) { - CheckPairEquals(*b_range.find(key_of_value(values[i])), values[i]); - } - b_range.swap(b_copy); - - // Test non-member function swap. - swap(b_range, b_copy); - EXPECT_EQ(b_copy.size(), 0); - EXPECT_EQ(b_range.size(), const_b.size()); - for (int i = 0; i < values.size(); ++i) { - CheckPairEquals(*b_range.find(key_of_value(values[i])), values[i]); - } - swap(b_range, b_copy); - - // Test erase via values. - for (int i = 0; i < values.size(); ++i) { - mutable_b.erase(key_of_value(values[i])); - // Erasing a non-existent key should have no effect. - ASSERT_EQ(mutable_b.erase(key_of_value(values[i])), 0); - } - - const_b.verify(); - EXPECT_EQ(const_b.size(), 0); - - // Test erase via iterators. - mutable_b = b_copy; - for (int i = 0; i < values.size(); ++i) { - mutable_b.erase(mutable_b.find(key_of_value(values[i]))); - } - - const_b.verify(); - EXPECT_EQ(const_b.size(), 0); - - // Test insert with hint. - for (int i = 0; i < values.size(); i++) { - mutable_b.insert(mutable_b.upper_bound(key_of_value(values[i])), values[i]); - } - - const_b.verify(); - - // Test range erase. - mutable_b.erase(mutable_b.begin(), mutable_b.end()); - EXPECT_EQ(mutable_b.size(), 0); - const_b.verify(); - - // First half. - mutable_b = b_copy; - typename T::iterator mutable_iter_end = mutable_b.begin(); - for (int i = 0; i < values.size() / 2; ++i) ++mutable_iter_end; - mutable_b.erase(mutable_b.begin(), mutable_iter_end); - EXPECT_EQ(mutable_b.size(), values.size() - values.size() / 2); - const_b.verify(); - - // Second half. - mutable_b = b_copy; - typename T::iterator mutable_iter_begin = mutable_b.begin(); - for (int i = 0; i < values.size() / 2; ++i) ++mutable_iter_begin; - mutable_b.erase(mutable_iter_begin, mutable_b.end()); - EXPECT_EQ(mutable_b.size(), values.size() / 2); - const_b.verify(); - - // Second quarter. - mutable_b = b_copy; - mutable_iter_begin = mutable_b.begin(); - for (int i = 0; i < values.size() / 4; ++i) ++mutable_iter_begin; - mutable_iter_end = mutable_iter_begin; - for (int i = 0; i < values.size() / 4; ++i) ++mutable_iter_end; - mutable_b.erase(mutable_iter_begin, mutable_iter_end); - EXPECT_EQ(mutable_b.size(), values.size() - values.size() / 4); - const_b.verify(); - - mutable_b.clear(); -} - -template -void ConstTest() { - using value_type = typename T::value_type; - typename KeyOfValue::type key_of_value; - - T mutable_b; - const T &const_b = mutable_b; - - // Insert a single value into the container and test looking it up. - value_type value = Generator(2)(2); - mutable_b.insert(value); - EXPECT_TRUE(mutable_b.contains(key_of_value(value))); - EXPECT_NE(mutable_b.find(key_of_value(value)), const_b.end()); - EXPECT_TRUE(const_b.contains(key_of_value(value))); - EXPECT_NE(const_b.find(key_of_value(value)), mutable_b.end()); - EXPECT_EQ(*const_b.lower_bound(key_of_value(value)), value); - EXPECT_EQ(const_b.upper_bound(key_of_value(value)), const_b.end()); - EXPECT_EQ(*const_b.equal_range(key_of_value(value)).first, value); - - // We can only create a non-const iterator from a non-const container. - typename T::iterator mutable_iter(mutable_b.begin()); - EXPECT_EQ(mutable_iter, const_b.begin()); - EXPECT_NE(mutable_iter, const_b.end()); - EXPECT_EQ(const_b.begin(), mutable_iter); - EXPECT_NE(const_b.end(), mutable_iter); - typename T::reverse_iterator mutable_riter(mutable_b.rbegin()); - EXPECT_EQ(mutable_riter, const_b.rbegin()); - EXPECT_NE(mutable_riter, const_b.rend()); - EXPECT_EQ(const_b.rbegin(), mutable_riter); - EXPECT_NE(const_b.rend(), mutable_riter); - - // We can create a const iterator from a non-const iterator. - typename T::const_iterator const_iter(mutable_iter); - EXPECT_EQ(const_iter, mutable_b.begin()); - EXPECT_NE(const_iter, mutable_b.end()); - EXPECT_EQ(mutable_b.begin(), const_iter); - EXPECT_NE(mutable_b.end(), const_iter); - typename T::const_reverse_iterator const_riter(mutable_riter); - EXPECT_EQ(const_riter, mutable_b.rbegin()); - EXPECT_NE(const_riter, mutable_b.rend()); - EXPECT_EQ(mutable_b.rbegin(), const_riter); - EXPECT_NE(mutable_b.rend(), const_riter); - - // Make sure various methods can be invoked on a const container. - const_b.verify(); - ASSERT_TRUE(!const_b.empty()); - EXPECT_EQ(const_b.size(), 1); - EXPECT_GT(const_b.max_size(), 0); - EXPECT_TRUE(const_b.contains(key_of_value(value))); - EXPECT_EQ(const_b.count(key_of_value(value)), 1); -} - -template -void BtreeTest() { - ConstTest(); - - using V = typename remove_pair_const::type; - const std::vector random_values = GenerateValuesWithSeed( - absl::GetFlag(FLAGS_test_values), 4 * absl::GetFlag(FLAGS_test_values), - testing::GTEST_FLAG(random_seed)); - - unique_checker container; - - // Test key insertion/deletion in sorted order. - std::vector sorted_values(random_values); - std::sort(sorted_values.begin(), sorted_values.end()); - DoTest("sorted: ", &container, sorted_values); - - // Test key insertion/deletion in reverse sorted order. - std::reverse(sorted_values.begin(), sorted_values.end()); - DoTest("rsorted: ", &container, sorted_values); - - // Test key insertion/deletion in random order. - DoTest("random: ", &container, random_values); -} - -template -void BtreeMultiTest() { - ConstTest(); - - using V = typename remove_pair_const::type; - const std::vector random_values = GenerateValuesWithSeed( - absl::GetFlag(FLAGS_test_values), 4 * absl::GetFlag(FLAGS_test_values), - testing::GTEST_FLAG(random_seed)); - - multi_checker container; - - // Test keys in sorted order. - std::vector sorted_values(random_values); - std::sort(sorted_values.begin(), sorted_values.end()); - DoTest("sorted: ", &container, sorted_values); - - // Test keys in reverse sorted order. - std::reverse(sorted_values.begin(), sorted_values.end()); - DoTest("rsorted: ", &container, sorted_values); - - // Test keys in random order. - DoTest("random: ", &container, random_values); - - // Test keys in random order w/ duplicates. - std::vector duplicate_values(random_values); - duplicate_values.insert(duplicate_values.end(), random_values.begin(), - random_values.end()); - DoTest("duplicates:", &container, duplicate_values); - - // Test all identical keys. - std::vector identical_values(100); - std::fill(identical_values.begin(), identical_values.end(), - Generator(2)(2)); - DoTest("identical: ", &container, identical_values); -} - -template -struct PropagatingCountingAlloc : public CountingAllocator { - using propagate_on_container_copy_assignment = std::true_type; - using propagate_on_container_move_assignment = std::true_type; - using propagate_on_container_swap = std::true_type; - - using Base = CountingAllocator; - using Base::Base; - - template - explicit PropagatingCountingAlloc(const PropagatingCountingAlloc &other) - : Base(other.bytes_used_) {} - - template - struct rebind { - using other = PropagatingCountingAlloc; - }; -}; - -template -void BtreeAllocatorTest() { - using value_type = typename T::value_type; - - int64_t bytes1 = 0, bytes2 = 0; - PropagatingCountingAlloc allocator1(&bytes1); - PropagatingCountingAlloc allocator2(&bytes2); - Generator generator(1000); - - // Test that we allocate properly aligned memory. If we don't, then Layout - // will assert fail. - auto unused1 = allocator1.allocate(1); - auto unused2 = allocator2.allocate(1); - - // Test copy assignment - { - T b1(typename T::key_compare(), allocator1); - T b2(typename T::key_compare(), allocator2); - - int64_t original_bytes1 = bytes1; - b1.insert(generator(0)); - EXPECT_GT(bytes1, original_bytes1); - - // This should propagate the allocator. - b1 = b2; - EXPECT_EQ(b1.size(), 0); - EXPECT_EQ(b2.size(), 0); - EXPECT_EQ(bytes1, original_bytes1); - - for (int i = 1; i < 1000; i++) { - b1.insert(generator(i)); - } - - // We should have allocated out of allocator2. - EXPECT_GT(bytes2, bytes1); - } - - // Test move assignment - { - T b1(typename T::key_compare(), allocator1); - T b2(typename T::key_compare(), allocator2); - - int64_t original_bytes1 = bytes1; - b1.insert(generator(0)); - EXPECT_GT(bytes1, original_bytes1); - - // This should propagate the allocator. - b1 = std::move(b2); - EXPECT_EQ(b1.size(), 0); - EXPECT_EQ(bytes1, original_bytes1); - - for (int i = 1; i < 1000; i++) { - b1.insert(generator(i)); - } - - // We should have allocated out of allocator2. - EXPECT_GT(bytes2, bytes1); - } - - // Test swap - { - T b1(typename T::key_compare(), allocator1); - T b2(typename T::key_compare(), allocator2); - - int64_t original_bytes1 = bytes1; - b1.insert(generator(0)); - EXPECT_GT(bytes1, original_bytes1); - - // This should swap the allocators. - swap(b1, b2); - EXPECT_EQ(b1.size(), 0); - EXPECT_EQ(b2.size(), 1); - EXPECT_GT(bytes1, original_bytes1); - - for (int i = 1; i < 1000; i++) { - b1.insert(generator(i)); - } - - // We should have allocated out of allocator2. - EXPECT_GT(bytes2, bytes1); - } - - allocator1.deallocate(unused1, 1); - allocator2.deallocate(unused2, 1); -} - -template -void BtreeMapTest() { - using value_type = typename T::value_type; - using mapped_type = typename T::mapped_type; - - mapped_type m = Generator(0)(0); - (void)m; - - T b; - - // Verify we can insert using operator[]. - for (int i = 0; i < 1000; i++) { - value_type v = Generator(1000)(i); - b[v.first] = v.second; - } - EXPECT_EQ(b.size(), 1000); - - // Test whether we can use the "->" operator on iterators and - // reverse_iterators. This stresses the btree_map_params::pair_pointer - // mechanism. - EXPECT_EQ(b.begin()->first, Generator(1000)(0).first); - EXPECT_EQ(b.begin()->second, Generator(1000)(0).second); - EXPECT_EQ(b.rbegin()->first, Generator(1000)(999).first); - EXPECT_EQ(b.rbegin()->second, Generator(1000)(999).second); -} - -template -void BtreeMultiMapTest() { - using mapped_type = typename T::mapped_type; - mapped_type m = Generator(0)(0); - (void)m; -} - -template -void SetTest() { - EXPECT_EQ( - sizeof(absl::btree_set), - 2 * sizeof(void *) + sizeof(typename absl::btree_set::size_type)); - using BtreeSet = absl::btree_set; - using CountingBtreeSet = - absl::btree_set, PropagatingCountingAlloc>; - BtreeTest>(); - BtreeAllocatorTest(); -} - -template -void MapTest() { - EXPECT_EQ( - sizeof(absl::btree_map), - 2 * sizeof(void *) + sizeof(typename absl::btree_map::size_type)); - using BtreeMap = absl::btree_map; - using CountingBtreeMap = - absl::btree_map, - PropagatingCountingAlloc>>; - BtreeTest>(); - BtreeAllocatorTest(); - BtreeMapTest(); -} - -TEST(Btree, set_int32) { SetTest(); } -TEST(Btree, set_int64) { SetTest(); } -TEST(Btree, set_string) { SetTest(); } -TEST(Btree, set_cord) { SetTest(); } -TEST(Btree, set_pair) { SetTest>(); } -TEST(Btree, map_int32) { MapTest(); } -TEST(Btree, map_int64) { MapTest(); } -TEST(Btree, map_string) { MapTest(); } -TEST(Btree, map_cord) { MapTest(); } -TEST(Btree, map_pair) { MapTest>(); } - -template -void MultiSetTest() { - EXPECT_EQ( - sizeof(absl::btree_multiset), - 2 * sizeof(void *) + sizeof(typename absl::btree_multiset::size_type)); - using BtreeMSet = absl::btree_multiset; - using CountingBtreeMSet = - absl::btree_multiset, PropagatingCountingAlloc>; - BtreeMultiTest>(); - BtreeAllocatorTest(); -} - -template -void MultiMapTest() { - EXPECT_EQ(sizeof(absl::btree_multimap), - 2 * sizeof(void *) + - sizeof(typename absl::btree_multimap::size_type)); - using BtreeMMap = absl::btree_multimap; - using CountingBtreeMMap = - absl::btree_multimap, - PropagatingCountingAlloc>>; - BtreeMultiTest>(); - BtreeMultiMapTest(); - BtreeAllocatorTest(); -} - -TEST(Btree, multiset_int32) { MultiSetTest(); } -TEST(Btree, multiset_int64) { MultiSetTest(); } -TEST(Btree, multiset_string) { MultiSetTest(); } -TEST(Btree, multiset_cord) { MultiSetTest(); } -TEST(Btree, multiset_pair) { MultiSetTest>(); } -TEST(Btree, multimap_int32) { MultiMapTest(); } -TEST(Btree, multimap_int64) { MultiMapTest(); } -TEST(Btree, multimap_string) { MultiMapTest(); } -TEST(Btree, multimap_cord) { MultiMapTest(); } -TEST(Btree, multimap_pair) { MultiMapTest>(); } - -struct CompareIntToString { - bool operator()(const std::string &a, const std::string &b) const { - return a < b; - } - bool operator()(const std::string &a, int b) const { - return a < absl::StrCat(b); - } - bool operator()(int a, const std::string &b) const { - return absl::StrCat(a) < b; - } - using is_transparent = void; -}; - -struct NonTransparentCompare { - template - bool operator()(const T &t, const U &u) const { - // Treating all comparators as transparent can cause inefficiencies (see - // N3657 C++ proposal). Test that for comparators without 'is_transparent' - // alias (like this one), we do not attempt heterogeneous lookup. - EXPECT_TRUE((std::is_same())); - return t < u; - } -}; - -template -bool CanEraseWithEmptyBrace(T t, decltype(t.erase({})) *) { - return true; -} - -template -bool CanEraseWithEmptyBrace(T, ...) { - return false; -} - -template -void TestHeterogeneous(T table) { - auto lb = table.lower_bound("3"); - EXPECT_EQ(lb, table.lower_bound(3)); - EXPECT_NE(lb, table.lower_bound(4)); - EXPECT_EQ(lb, table.lower_bound({"3"})); - EXPECT_NE(lb, table.lower_bound({})); - - auto ub = table.upper_bound("3"); - EXPECT_EQ(ub, table.upper_bound(3)); - EXPECT_NE(ub, table.upper_bound(5)); - EXPECT_EQ(ub, table.upper_bound({"3"})); - EXPECT_NE(ub, table.upper_bound({})); - - auto er = table.equal_range("3"); - EXPECT_EQ(er, table.equal_range(3)); - EXPECT_NE(er, table.equal_range(4)); - EXPECT_EQ(er, table.equal_range({"3"})); - EXPECT_NE(er, table.equal_range({})); - - auto it = table.find("3"); - EXPECT_EQ(it, table.find(3)); - EXPECT_NE(it, table.find(4)); - EXPECT_EQ(it, table.find({"3"})); - EXPECT_NE(it, table.find({})); - - EXPECT_TRUE(table.contains(3)); - EXPECT_FALSE(table.contains(4)); - EXPECT_TRUE(table.count({"3"})); - EXPECT_FALSE(table.contains({})); - - EXPECT_EQ(1, table.count(3)); - EXPECT_EQ(0, table.count(4)); - EXPECT_EQ(1, table.count({"3"})); - EXPECT_EQ(0, table.count({})); - - auto copy = table; - copy.erase(3); - EXPECT_EQ(table.size() - 1, copy.size()); - copy.erase(4); - EXPECT_EQ(table.size() - 1, copy.size()); - copy.erase({"5"}); - EXPECT_EQ(table.size() - 2, copy.size()); - EXPECT_FALSE(CanEraseWithEmptyBrace(table, nullptr)); - - // Also run it with const T&. - if (std::is_class()) TestHeterogeneous(table); -} - -TEST(Btree, HeterogeneousLookup) { - TestHeterogeneous(btree_set{"1", "3", "5"}); - TestHeterogeneous(btree_map{ - {"1", 1}, {"3", 3}, {"5", 5}}); - TestHeterogeneous( - btree_multiset{"1", "3", "5"}); - TestHeterogeneous(btree_multimap{ - {"1", 1}, {"3", 3}, {"5", 5}}); - - // Only maps have .at() - btree_map map{ - {"", -1}, {"1", 1}, {"3", 3}, {"5", 5}}; - EXPECT_EQ(1, map.at(1)); - EXPECT_EQ(3, map.at({"3"})); - EXPECT_EQ(-1, map.at({})); - const auto &cmap = map; - EXPECT_EQ(1, cmap.at(1)); - EXPECT_EQ(3, cmap.at({"3"})); - EXPECT_EQ(-1, cmap.at({})); -} - -TEST(Btree, NoHeterogeneousLookupWithoutAlias) { - using StringSet = absl::btree_set; - StringSet s; - ASSERT_TRUE(s.insert("hello").second); - ASSERT_TRUE(s.insert("world").second); - EXPECT_TRUE(s.end() == s.find("blah")); - EXPECT_TRUE(s.begin() == s.lower_bound("hello")); - EXPECT_EQ(1, s.count("world")); - EXPECT_TRUE(s.contains("hello")); - EXPECT_TRUE(s.contains("world")); - EXPECT_FALSE(s.contains("blah")); - - using StringMultiSet = - absl::btree_multiset; - StringMultiSet ms; - ms.insert("hello"); - ms.insert("world"); - ms.insert("world"); - EXPECT_TRUE(ms.end() == ms.find("blah")); - EXPECT_TRUE(ms.begin() == ms.lower_bound("hello")); - EXPECT_EQ(2, ms.count("world")); - EXPECT_TRUE(ms.contains("hello")); - EXPECT_TRUE(ms.contains("world")); - EXPECT_FALSE(ms.contains("blah")); -} - -TEST(Btree, DefaultTransparent) { - { - // `int` does not have a default transparent comparator. - // The input value is converted to key_type. - btree_set s = {1}; - double d = 1.1; - EXPECT_EQ(s.begin(), s.find(d)); - EXPECT_TRUE(s.contains(d)); - } - - { - // `std::string` has heterogeneous support. - btree_set s = {"A"}; - EXPECT_EQ(s.begin(), s.find(absl::string_view("A"))); - EXPECT_TRUE(s.contains(absl::string_view("A"))); - } -} - -class StringLike { - public: - StringLike() = default; - - StringLike(const char *s) : s_(s) { // NOLINT - ++constructor_calls_; - } - - bool operator<(const StringLike &a) const { return s_ < a.s_; } - - static void clear_constructor_call_count() { constructor_calls_ = 0; } - - static int constructor_calls() { return constructor_calls_; } - - private: - static int constructor_calls_; - std::string s_; -}; - -int StringLike::constructor_calls_ = 0; - -TEST(Btree, HeterogeneousLookupDoesntDegradePerformance) { - using StringSet = absl::btree_set; - StringSet s; - for (int i = 0; i < 100; ++i) { - ASSERT_TRUE(s.insert(absl::StrCat(i).c_str()).second); - } - StringLike::clear_constructor_call_count(); - s.find("50"); - ASSERT_EQ(1, StringLike::constructor_calls()); - - StringLike::clear_constructor_call_count(); - s.contains("50"); - ASSERT_EQ(1, StringLike::constructor_calls()); - - StringLike::clear_constructor_call_count(); - s.count("50"); - ASSERT_EQ(1, StringLike::constructor_calls()); - - StringLike::clear_constructor_call_count(); - s.lower_bound("50"); - ASSERT_EQ(1, StringLike::constructor_calls()); - - StringLike::clear_constructor_call_count(); - s.upper_bound("50"); - ASSERT_EQ(1, StringLike::constructor_calls()); - - StringLike::clear_constructor_call_count(); - s.equal_range("50"); - ASSERT_EQ(1, StringLike::constructor_calls()); - - StringLike::clear_constructor_call_count(); - s.erase("50"); - ASSERT_EQ(1, StringLike::constructor_calls()); -} - -// Verify that swapping btrees swaps the key comparison functors and that we can -// use non-default constructible comparators. -struct SubstringLess { - SubstringLess() = delete; - explicit SubstringLess(int length) : n(length) {} - bool operator()(const std::string &a, const std::string &b) const { - return absl::string_view(a).substr(0, n) < - absl::string_view(b).substr(0, n); - } - int n; -}; - -TEST(Btree, SwapKeyCompare) { - using SubstringSet = absl::btree_set; - SubstringSet s1(SubstringLess(1), SubstringSet::allocator_type()); - SubstringSet s2(SubstringLess(2), SubstringSet::allocator_type()); - - ASSERT_TRUE(s1.insert("a").second); - ASSERT_FALSE(s1.insert("aa").second); - - ASSERT_TRUE(s2.insert("a").second); - ASSERT_TRUE(s2.insert("aa").second); - ASSERT_FALSE(s2.insert("aaa").second); - - swap(s1, s2); - - ASSERT_TRUE(s1.insert("b").second); - ASSERT_TRUE(s1.insert("bb").second); - ASSERT_FALSE(s1.insert("bbb").second); - - ASSERT_TRUE(s2.insert("b").second); - ASSERT_FALSE(s2.insert("bb").second); -} - -TEST(Btree, UpperBoundRegression) { - // Regress a bug where upper_bound would default-construct a new key_compare - // instead of copying the existing one. - using SubstringSet = absl::btree_set; - SubstringSet my_set(SubstringLess(3)); - my_set.insert("aab"); - my_set.insert("abb"); - // We call upper_bound("aaa"). If this correctly uses the length 3 - // comparator, aaa < aab < abb, so we should get aab as the result. - // If it instead uses the default-constructed length 2 comparator, - // aa == aa < ab, so we'll get abb as our result. - SubstringSet::iterator it = my_set.upper_bound("aaa"); - ASSERT_TRUE(it != my_set.end()); - EXPECT_EQ("aab", *it); -} - -TEST(Btree, Comparison) { - const int kSetSize = 1201; - absl::btree_set my_set; - for (int i = 0; i < kSetSize; ++i) { - my_set.insert(i); - } - absl::btree_set my_set_copy(my_set); - EXPECT_TRUE(my_set_copy == my_set); - EXPECT_TRUE(my_set == my_set_copy); - EXPECT_FALSE(my_set_copy != my_set); - EXPECT_FALSE(my_set != my_set_copy); - - my_set.insert(kSetSize); - EXPECT_FALSE(my_set_copy == my_set); - EXPECT_FALSE(my_set == my_set_copy); - EXPECT_TRUE(my_set_copy != my_set); - EXPECT_TRUE(my_set != my_set_copy); - - my_set.erase(kSetSize - 1); - EXPECT_FALSE(my_set_copy == my_set); - EXPECT_FALSE(my_set == my_set_copy); - EXPECT_TRUE(my_set_copy != my_set); - EXPECT_TRUE(my_set != my_set_copy); - - absl::btree_map my_map; - for (int i = 0; i < kSetSize; ++i) { - my_map[std::string(i, 'a')] = i; - } - absl::btree_map my_map_copy(my_map); - EXPECT_TRUE(my_map_copy == my_map); - EXPECT_TRUE(my_map == my_map_copy); - EXPECT_FALSE(my_map_copy != my_map); - EXPECT_FALSE(my_map != my_map_copy); - - ++my_map_copy[std::string(7, 'a')]; - EXPECT_FALSE(my_map_copy == my_map); - EXPECT_FALSE(my_map == my_map_copy); - EXPECT_TRUE(my_map_copy != my_map); - EXPECT_TRUE(my_map != my_map_copy); - - my_map_copy = my_map; - my_map["hello"] = kSetSize; - EXPECT_FALSE(my_map_copy == my_map); - EXPECT_FALSE(my_map == my_map_copy); - EXPECT_TRUE(my_map_copy != my_map); - EXPECT_TRUE(my_map != my_map_copy); - - my_map.erase(std::string(kSetSize - 1, 'a')); - EXPECT_FALSE(my_map_copy == my_map); - EXPECT_FALSE(my_map == my_map_copy); - EXPECT_TRUE(my_map_copy != my_map); - EXPECT_TRUE(my_map != my_map_copy); -} - -TEST(Btree, RangeCtorSanity) { - std::vector ivec; - ivec.push_back(1); - std::map imap; - imap.insert(std::make_pair(1, 2)); - absl::btree_multiset tmset(ivec.begin(), ivec.end()); - absl::btree_multimap tmmap(imap.begin(), imap.end()); - absl::btree_set tset(ivec.begin(), ivec.end()); - absl::btree_map tmap(imap.begin(), imap.end()); - EXPECT_EQ(1, tmset.size()); - EXPECT_EQ(1, tmmap.size()); - EXPECT_EQ(1, tset.size()); - EXPECT_EQ(1, tmap.size()); -} - -} // namespace - -class BtreeNodePeer { - public: - // Yields the size of a leaf node with a specific number of values. - template - constexpr static size_t GetTargetNodeSize(size_t target_values_per_node) { - return btree_node< - set_params, std::allocator, - /*TargetNodeSize=*/256, // This parameter isn't used here. - /*Multi=*/false>>::SizeWithNValues(target_values_per_node); - } - - // Yields the number of values in a (non-root) leaf node for this btree. - template - constexpr static size_t GetNumValuesPerNode() { - return btree_node::kNodeValues; - } - - template - constexpr static size_t GetMaxFieldType() { - return std::numeric_limits< - typename btree_node::field_type>::max(); - } - - template - constexpr static bool UsesLinearNodeSearch() { - return btree_node::use_linear_search::value; - } -}; - -namespace { - -class BtreeMapTest : public ::testing::Test { - public: - struct Key {}; - struct Cmp { - template - bool operator()(T, T) const { - return false; - } - }; - - struct KeyLin { - using absl_btree_prefer_linear_node_search = std::true_type; - }; - struct CmpLin : Cmp { - using absl_btree_prefer_linear_node_search = std::true_type; - }; - - struct KeyBin { - using absl_btree_prefer_linear_node_search = std::false_type; - }; - struct CmpBin : Cmp { - using absl_btree_prefer_linear_node_search = std::false_type; - }; - - template - static bool IsLinear() { - return BtreeNodePeer::UsesLinearNodeSearch>(); - } -}; - -TEST_F(BtreeMapTest, TestLinearSearchPreferredForKeyLinearViaAlias) { - // Test requesting linear search by directly exporting an alias. - EXPECT_FALSE((IsLinear())); - EXPECT_TRUE((IsLinear())); - EXPECT_TRUE((IsLinear())); - EXPECT_TRUE((IsLinear())); -} - -TEST_F(BtreeMapTest, LinearChoiceTree) { - // Cmp has precedence, and is forcing binary - EXPECT_FALSE((IsLinear())); - EXPECT_FALSE((IsLinear())); - EXPECT_FALSE((IsLinear())); - EXPECT_FALSE((IsLinear())); - EXPECT_FALSE((IsLinear())); - // Cmp has precedence, and is forcing linear - EXPECT_TRUE((IsLinear())); - EXPECT_TRUE((IsLinear())); - EXPECT_TRUE((IsLinear())); - EXPECT_TRUE((IsLinear())); - EXPECT_TRUE((IsLinear())); - // Cmp has no preference, Key determines linear vs binary. - EXPECT_FALSE((IsLinear())); - EXPECT_TRUE((IsLinear())); - EXPECT_FALSE((IsLinear())); - // arithmetic key w/ std::less or std::greater: linear - EXPECT_TRUE((IsLinear>())); - EXPECT_TRUE((IsLinear>())); - // arithmetic key w/ custom compare: binary - EXPECT_FALSE((IsLinear())); - // non-arithmetic key: binary - EXPECT_FALSE((IsLinear>())); -} - -TEST(Btree, BtreeMapCanHoldMoveOnlyTypes) { - absl::btree_map> m; - - std::unique_ptr &v = m["A"]; - EXPECT_TRUE(v == nullptr); - v.reset(new std::string("X")); - - auto iter = m.find("A"); - EXPECT_EQ("X", *iter->second); -} - -TEST(Btree, InitializerListConstructor) { - absl::btree_set set({"a", "b"}); - EXPECT_EQ(set.count("a"), 1); - EXPECT_EQ(set.count("b"), 1); - - absl::btree_multiset mset({1, 1, 4}); - EXPECT_EQ(mset.count(1), 2); - EXPECT_EQ(mset.count(4), 1); - - absl::btree_map map({{1, 5}, {2, 10}}); - EXPECT_EQ(map[1], 5); - EXPECT_EQ(map[2], 10); - - absl::btree_multimap mmap({{1, 5}, {1, 10}}); - auto range = mmap.equal_range(1); - auto it = range.first; - ASSERT_NE(it, range.second); - EXPECT_EQ(it->second, 5); - ASSERT_NE(++it, range.second); - EXPECT_EQ(it->second, 10); - EXPECT_EQ(++it, range.second); -} - -TEST(Btree, InitializerListInsert) { - absl::btree_set set; - set.insert({"a", "b"}); - EXPECT_EQ(set.count("a"), 1); - EXPECT_EQ(set.count("b"), 1); - - absl::btree_multiset mset; - mset.insert({1, 1, 4}); - EXPECT_EQ(mset.count(1), 2); - EXPECT_EQ(mset.count(4), 1); - - absl::btree_map map; - map.insert({{1, 5}, {2, 10}}); - // Test that inserting one element using an initializer list also works. - map.insert({3, 15}); - EXPECT_EQ(map[1], 5); - EXPECT_EQ(map[2], 10); - EXPECT_EQ(map[3], 15); - - absl::btree_multimap mmap; - mmap.insert({{1, 5}, {1, 10}}); - auto range = mmap.equal_range(1); - auto it = range.first; - ASSERT_NE(it, range.second); - EXPECT_EQ(it->second, 5); - ASSERT_NE(++it, range.second); - EXPECT_EQ(it->second, 10); - EXPECT_EQ(++it, range.second); -} - -template -void AssertKeyCompareToAdapted() { - using Adapted = typename key_compare_to_adapter::type; - static_assert(!std::is_same::value, - "key_compare_to_adapter should have adapted this comparator."); - static_assert( - std::is_same>::value, - "Adapted comparator should be a key-compare-to comparator."); -} -template -void AssertKeyCompareToNotAdapted() { - using Unadapted = typename key_compare_to_adapter::type; - static_assert( - std::is_same::value, - "key_compare_to_adapter shouldn't have adapted this comparator."); - static_assert( - std::is_same>::value, - "Un-adapted comparator should return bool."); -} - -TEST(Btree, KeyCompareToAdapter) { - AssertKeyCompareToAdapted, std::string>(); - AssertKeyCompareToAdapted, std::string>(); - AssertKeyCompareToAdapted, absl::string_view>(); - AssertKeyCompareToAdapted, - absl::string_view>(); - AssertKeyCompareToAdapted, absl::Cord>(); - AssertKeyCompareToAdapted, absl::Cord>(); - AssertKeyCompareToNotAdapted, int>(); - AssertKeyCompareToNotAdapted, int>(); -} - -TEST(Btree, RValueInsert) { - InstanceTracker tracker; - - absl::btree_set set; - set.insert(MovableOnlyInstance(1)); - set.insert(MovableOnlyInstance(3)); - MovableOnlyInstance two(2); - set.insert(set.find(MovableOnlyInstance(3)), std::move(two)); - auto it = set.find(MovableOnlyInstance(2)); - ASSERT_NE(it, set.end()); - ASSERT_NE(++it, set.end()); - EXPECT_EQ(it->value(), 3); - - absl::btree_multiset mset; - MovableOnlyInstance zero(0); - MovableOnlyInstance zero2(0); - mset.insert(std::move(zero)); - mset.insert(mset.find(MovableOnlyInstance(0)), std::move(zero2)); - EXPECT_EQ(mset.count(MovableOnlyInstance(0)), 2); - - absl::btree_map map; - std::pair p1 = {1, MovableOnlyInstance(5)}; - std::pair p2 = {2, MovableOnlyInstance(10)}; - std::pair p3 = {3, MovableOnlyInstance(15)}; - map.insert(std::move(p1)); - map.insert(std::move(p3)); - map.insert(map.find(3), std::move(p2)); - ASSERT_NE(map.find(2), map.end()); - EXPECT_EQ(map.find(2)->second.value(), 10); - - absl::btree_multimap mmap; - std::pair p4 = {1, MovableOnlyInstance(5)}; - std::pair p5 = {1, MovableOnlyInstance(10)}; - mmap.insert(std::move(p4)); - mmap.insert(mmap.find(1), std::move(p5)); - auto range = mmap.equal_range(1); - auto it1 = range.first; - ASSERT_NE(it1, range.second); - EXPECT_EQ(it1->second.value(), 10); - ASSERT_NE(++it1, range.second); - EXPECT_EQ(it1->second.value(), 5); - EXPECT_EQ(++it1, range.second); - - EXPECT_EQ(tracker.copies(), 0); - EXPECT_EQ(tracker.swaps(), 0); -} - -// A btree set with a specific number of values per node. -template > -class SizedBtreeSet - : public btree_set_container, - BtreeNodePeer::GetTargetNodeSize(TargetValuesPerNode), - /*Multi=*/false>>> { - using Base = typename SizedBtreeSet::btree_set_container; - - public: - SizedBtreeSet() {} - using Base::Base; -}; - -template -void ExpectOperationCounts(const int expected_moves, - const int expected_comparisons, - const std::vector &values, - InstanceTracker *tracker, Set *set) { - for (const int v : values) set->insert(MovableOnlyInstance(v)); - set->clear(); - EXPECT_EQ(tracker->moves(), expected_moves); - EXPECT_EQ(tracker->comparisons(), expected_comparisons); - EXPECT_EQ(tracker->copies(), 0); - EXPECT_EQ(tracker->swaps(), 0); - tracker->ResetCopiesMovesSwaps(); -} - -// Note: when the values in this test change, it is expected to have an impact -// on performance. -TEST(Btree, MovesComparisonsCopiesSwapsTracking) { - InstanceTracker tracker; - // Note: this is minimum number of values per node. - SizedBtreeSet set3; - // Note: this is the default number of values per node for a set of int32s - // (with 64-bit pointers). - SizedBtreeSet set61; - SizedBtreeSet set100; - - // Don't depend on flags for random values because then the expectations will - // fail if the flags change. - std::vector values = - GenerateValuesWithSeed(10000, 1 << 22, /*seed=*/23); - - EXPECT_EQ(BtreeNodePeer::GetNumValuesPerNode(), 3); - EXPECT_EQ(BtreeNodePeer::GetNumValuesPerNode(), 61); - EXPECT_EQ(BtreeNodePeer::GetNumValuesPerNode(), 100); - if (sizeof(void *) == 8) { - EXPECT_EQ(BtreeNodePeer::GetNumValuesPerNode>(), - BtreeNodePeer::GetNumValuesPerNode()); - } - - // Test key insertion/deletion in random order. - ExpectOperationCounts(45281, 132551, values, &tracker, &set3); - ExpectOperationCounts(386718, 129807, values, &tracker, &set61); - ExpectOperationCounts(586761, 130310, values, &tracker, &set100); - - // Test key insertion/deletion in sorted order. - std::sort(values.begin(), values.end()); - ExpectOperationCounts(26638, 92134, values, &tracker, &set3); - ExpectOperationCounts(20208, 87757, values, &tracker, &set61); - ExpectOperationCounts(20124, 96583, values, &tracker, &set100); - - // Test key insertion/deletion in reverse sorted order. - std::reverse(values.begin(), values.end()); - ExpectOperationCounts(49951, 119325, values, &tracker, &set3); - ExpectOperationCounts(338813, 118266, values, &tracker, &set61); - ExpectOperationCounts(534529, 125279, values, &tracker, &set100); -} - -struct MovableOnlyInstanceThreeWayCompare { - absl::weak_ordering operator()(const MovableOnlyInstance &a, - const MovableOnlyInstance &b) const { - return a.compare(b); - } -}; - -// Note: when the values in this test change, it is expected to have an impact -// on performance. -TEST(Btree, MovesComparisonsCopiesSwapsTrackingThreeWayCompare) { - InstanceTracker tracker; - // Note: this is minimum number of values per node. - SizedBtreeSet - set3; - // Note: this is the default number of values per node for a set of int32s - // (with 64-bit pointers). - SizedBtreeSet - set61; - SizedBtreeSet - set100; - - // Don't depend on flags for random values because then the expectations will - // fail if the flags change. - std::vector values = - GenerateValuesWithSeed(10000, 1 << 22, /*seed=*/23); - - EXPECT_EQ(BtreeNodePeer::GetNumValuesPerNode(), 3); - EXPECT_EQ(BtreeNodePeer::GetNumValuesPerNode(), 61); - EXPECT_EQ(BtreeNodePeer::GetNumValuesPerNode(), 100); - if (sizeof(void *) == 8) { - EXPECT_EQ(BtreeNodePeer::GetNumValuesPerNode>(), - BtreeNodePeer::GetNumValuesPerNode()); - } - - // Test key insertion/deletion in random order. - ExpectOperationCounts(45281, 122560, values, &tracker, &set3); - ExpectOperationCounts(386718, 119816, values, &tracker, &set61); - ExpectOperationCounts(586761, 120319, values, &tracker, &set100); - - // Test key insertion/deletion in sorted order. - std::sort(values.begin(), values.end()); - ExpectOperationCounts(26638, 92134, values, &tracker, &set3); - ExpectOperationCounts(20208, 87757, values, &tracker, &set61); - ExpectOperationCounts(20124, 96583, values, &tracker, &set100); - - // Test key insertion/deletion in reverse sorted order. - std::reverse(values.begin(), values.end()); - ExpectOperationCounts(49951, 109326, values, &tracker, &set3); - ExpectOperationCounts(338813, 108267, values, &tracker, &set61); - ExpectOperationCounts(534529, 115280, values, &tracker, &set100); -} - -struct NoDefaultCtor { - int num; - explicit NoDefaultCtor(int i) : num(i) {} - - friend bool operator<(const NoDefaultCtor &a, const NoDefaultCtor &b) { - return a.num < b.num; - } -}; - -TEST(Btree, BtreeMapCanHoldNoDefaultCtorTypes) { - absl::btree_map m; - - for (int i = 1; i <= 99; ++i) { - SCOPED_TRACE(i); - EXPECT_TRUE(m.emplace(NoDefaultCtor(i), NoDefaultCtor(100 - i)).second); - } - EXPECT_FALSE(m.emplace(NoDefaultCtor(78), NoDefaultCtor(0)).second); - - auto iter99 = m.find(NoDefaultCtor(99)); - ASSERT_NE(iter99, m.end()); - EXPECT_EQ(iter99->second.num, 1); - - auto iter1 = m.find(NoDefaultCtor(1)); - ASSERT_NE(iter1, m.end()); - EXPECT_EQ(iter1->second.num, 99); - - auto iter50 = m.find(NoDefaultCtor(50)); - ASSERT_NE(iter50, m.end()); - EXPECT_EQ(iter50->second.num, 50); - - auto iter25 = m.find(NoDefaultCtor(25)); - ASSERT_NE(iter25, m.end()); - EXPECT_EQ(iter25->second.num, 75); -} - -TEST(Btree, BtreeMultimapCanHoldNoDefaultCtorTypes) { - absl::btree_multimap m; - - for (int i = 1; i <= 99; ++i) { - SCOPED_TRACE(i); - m.emplace(NoDefaultCtor(i), NoDefaultCtor(100 - i)); - } - - auto iter99 = m.find(NoDefaultCtor(99)); - ASSERT_NE(iter99, m.end()); - EXPECT_EQ(iter99->second.num, 1); - - auto iter1 = m.find(NoDefaultCtor(1)); - ASSERT_NE(iter1, m.end()); - EXPECT_EQ(iter1->second.num, 99); - - auto iter50 = m.find(NoDefaultCtor(50)); - ASSERT_NE(iter50, m.end()); - EXPECT_EQ(iter50->second.num, 50); - - auto iter25 = m.find(NoDefaultCtor(25)); - ASSERT_NE(iter25, m.end()); - EXPECT_EQ(iter25->second.num, 75); -} - -TEST(Btree, MapAt) { - absl::btree_map map = {{1, 2}, {2, 4}}; - EXPECT_EQ(map.at(1), 2); - EXPECT_EQ(map.at(2), 4); - map.at(2) = 8; - const absl::btree_map &const_map = map; - EXPECT_EQ(const_map.at(1), 2); - EXPECT_EQ(const_map.at(2), 8); -#ifdef ABSL_HAVE_EXCEPTIONS - EXPECT_THROW(map.at(3), std::out_of_range); -#else - EXPECT_DEATH_IF_SUPPORTED(map.at(3), "absl::btree_map::at"); -#endif -} - -TEST(Btree, BtreeMultisetEmplace) { - const int value_to_insert = 123456; - absl::btree_multiset s; - auto iter = s.emplace(value_to_insert); - ASSERT_NE(iter, s.end()); - EXPECT_EQ(*iter, value_to_insert); - auto iter2 = s.emplace(value_to_insert); - EXPECT_NE(iter2, iter); - ASSERT_NE(iter2, s.end()); - EXPECT_EQ(*iter2, value_to_insert); - auto result = s.equal_range(value_to_insert); - EXPECT_EQ(std::distance(result.first, result.second), 2); -} - -TEST(Btree, BtreeMultisetEmplaceHint) { - const int value_to_insert = 123456; - absl::btree_multiset s; - auto iter = s.emplace(value_to_insert); - ASSERT_NE(iter, s.end()); - EXPECT_EQ(*iter, value_to_insert); - auto emplace_iter = s.emplace_hint(iter, value_to_insert); - EXPECT_NE(emplace_iter, iter); - ASSERT_NE(emplace_iter, s.end()); - EXPECT_EQ(*emplace_iter, value_to_insert); -} - -TEST(Btree, BtreeMultimapEmplace) { - const int key_to_insert = 123456; - const char value0[] = "a"; - absl::btree_multimap s; - auto iter = s.emplace(key_to_insert, value0); - ASSERT_NE(iter, s.end()); - EXPECT_EQ(iter->first, key_to_insert); - EXPECT_EQ(iter->second, value0); - const char value1[] = "b"; - auto iter2 = s.emplace(key_to_insert, value1); - EXPECT_NE(iter2, iter); - ASSERT_NE(iter2, s.end()); - EXPECT_EQ(iter2->first, key_to_insert); - EXPECT_EQ(iter2->second, value1); - auto result = s.equal_range(key_to_insert); - EXPECT_EQ(std::distance(result.first, result.second), 2); -} - -TEST(Btree, BtreeMultimapEmplaceHint) { - const int key_to_insert = 123456; - const char value0[] = "a"; - absl::btree_multimap s; - auto iter = s.emplace(key_to_insert, value0); - ASSERT_NE(iter, s.end()); - EXPECT_EQ(iter->first, key_to_insert); - EXPECT_EQ(iter->second, value0); - const char value1[] = "b"; - auto emplace_iter = s.emplace_hint(iter, key_to_insert, value1); - EXPECT_NE(emplace_iter, iter); - ASSERT_NE(emplace_iter, s.end()); - EXPECT_EQ(emplace_iter->first, key_to_insert); - EXPECT_EQ(emplace_iter->second, value1); -} - -TEST(Btree, ConstIteratorAccessors) { - absl::btree_set set; - for (int i = 0; i < 100; ++i) { - set.insert(i); - } - - auto it = set.cbegin(); - auto r_it = set.crbegin(); - for (int i = 0; i < 100; ++i, ++it, ++r_it) { - ASSERT_EQ(*it, i); - ASSERT_EQ(*r_it, 99 - i); - } - EXPECT_EQ(it, set.cend()); - EXPECT_EQ(r_it, set.crend()); -} - -TEST(Btree, StrSplitCompatible) { - const absl::btree_set split_set = absl::StrSplit("a,b,c", ','); - const absl::btree_set expected_set = {"a", "b", "c"}; - - EXPECT_EQ(split_set, expected_set); -} - -// We can't use EXPECT_EQ/etc. to compare absl::weak_ordering because they -// convert literal 0 to int and absl::weak_ordering can only be compared with -// literal 0. Defining this function allows for avoiding ClangTidy warnings. -bool Identity(const bool b) { return b; } - -TEST(Btree, ValueComp) { - absl::btree_set s; - EXPECT_TRUE(s.value_comp()(1, 2)); - EXPECT_FALSE(s.value_comp()(2, 2)); - EXPECT_FALSE(s.value_comp()(2, 1)); - - absl::btree_map m1; - EXPECT_TRUE(m1.value_comp()(std::make_pair(1, 0), std::make_pair(2, 0))); - EXPECT_FALSE(m1.value_comp()(std::make_pair(2, 0), std::make_pair(2, 0))); - EXPECT_FALSE(m1.value_comp()(std::make_pair(2, 0), std::make_pair(1, 0))); - - absl::btree_map m2; - EXPECT_TRUE(Identity( - m2.value_comp()(std::make_pair("a", 0), std::make_pair("b", 0)) < 0)); - EXPECT_TRUE(Identity( - m2.value_comp()(std::make_pair("b", 0), std::make_pair("b", 0)) == 0)); - EXPECT_TRUE(Identity( - m2.value_comp()(std::make_pair("b", 0), std::make_pair("a", 0)) > 0)); -} - -TEST(Btree, DefaultConstruction) { - absl::btree_set s; - absl::btree_map m; - absl::btree_multiset ms; - absl::btree_multimap mm; - - EXPECT_TRUE(s.empty()); - EXPECT_TRUE(m.empty()); - EXPECT_TRUE(ms.empty()); - EXPECT_TRUE(mm.empty()); -} - -TEST(Btree, SwissTableHashable) { - static constexpr int kValues = 10000; - std::vector values(kValues); - std::iota(values.begin(), values.end(), 0); - std::vector> map_values; - for (int v : values) map_values.emplace_back(v, -v); - - using set = absl::btree_set; - EXPECT_TRUE(absl::VerifyTypeImplementsAbslHashCorrectly({ - set{}, - set{1}, - set{2}, - set{1, 2}, - set{2, 1}, - set(values.begin(), values.end()), - set(values.rbegin(), values.rend()), - })); - - using mset = absl::btree_multiset; - EXPECT_TRUE(absl::VerifyTypeImplementsAbslHashCorrectly({ - mset{}, - mset{1}, - mset{1, 1}, - mset{2}, - mset{2, 2}, - mset{1, 2}, - mset{1, 1, 2}, - mset{1, 2, 2}, - mset{1, 1, 2, 2}, - mset(values.begin(), values.end()), - mset(values.rbegin(), values.rend()), - })); - - using map = absl::btree_map; - EXPECT_TRUE(absl::VerifyTypeImplementsAbslHashCorrectly({ - map{}, - map{{1, 0}}, - map{{1, 1}}, - map{{2, 0}}, - map{{2, 2}}, - map{{1, 0}, {2, 1}}, - map(map_values.begin(), map_values.end()), - map(map_values.rbegin(), map_values.rend()), - })); - - using mmap = absl::btree_multimap; - EXPECT_TRUE(absl::VerifyTypeImplementsAbslHashCorrectly({ - mmap{}, - mmap{{1, 0}}, - mmap{{1, 1}}, - mmap{{1, 0}, {1, 1}}, - mmap{{1, 1}, {1, 0}}, - mmap{{2, 0}}, - mmap{{2, 2}}, - mmap{{1, 0}, {2, 1}}, - mmap(map_values.begin(), map_values.end()), - mmap(map_values.rbegin(), map_values.rend()), - })); -} - -TEST(Btree, ComparableSet) { - absl::btree_set s1 = {1, 2}; - absl::btree_set s2 = {2, 3}; - EXPECT_LT(s1, s2); - EXPECT_LE(s1, s2); - EXPECT_LE(s1, s1); - EXPECT_GT(s2, s1); - EXPECT_GE(s2, s1); - EXPECT_GE(s1, s1); -} - -TEST(Btree, ComparableSetsDifferentLength) { - absl::btree_set s1 = {1, 2}; - absl::btree_set s2 = {1, 2, 3}; - EXPECT_LT(s1, s2); - EXPECT_LE(s1, s2); - EXPECT_GT(s2, s1); - EXPECT_GE(s2, s1); -} - -TEST(Btree, ComparableMultiset) { - absl::btree_multiset s1 = {1, 2}; - absl::btree_multiset s2 = {2, 3}; - EXPECT_LT(s1, s2); - EXPECT_LE(s1, s2); - EXPECT_LE(s1, s1); - EXPECT_GT(s2, s1); - EXPECT_GE(s2, s1); - EXPECT_GE(s1, s1); -} - -TEST(Btree, ComparableMap) { - absl::btree_map s1 = {{1, 2}}; - absl::btree_map s2 = {{2, 3}}; - EXPECT_LT(s1, s2); - EXPECT_LE(s1, s2); - EXPECT_LE(s1, s1); - EXPECT_GT(s2, s1); - EXPECT_GE(s2, s1); - EXPECT_GE(s1, s1); -} - -TEST(Btree, ComparableMultimap) { - absl::btree_multimap s1 = {{1, 2}}; - absl::btree_multimap s2 = {{2, 3}}; - EXPECT_LT(s1, s2); - EXPECT_LE(s1, s2); - EXPECT_LE(s1, s1); - EXPECT_GT(s2, s1); - EXPECT_GE(s2, s1); - EXPECT_GE(s1, s1); -} - -TEST(Btree, ComparableSetWithCustomComparator) { - // As specified by - // http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2012/n3337.pdf section - // [container.requirements.general].12, ordering associative containers always - // uses default '<' operator - // - even if otherwise the container uses custom functor. - absl::btree_set> s1 = {1, 2}; - absl::btree_set> s2 = {2, 3}; - EXPECT_LT(s1, s2); - EXPECT_LE(s1, s2); - EXPECT_LE(s1, s1); - EXPECT_GT(s2, s1); - EXPECT_GE(s2, s1); - EXPECT_GE(s1, s1); -} - -TEST(Btree, EraseReturnsIterator) { - absl::btree_set set = {1, 2, 3, 4, 5}; - auto result_it = set.erase(set.begin(), set.find(3)); - EXPECT_EQ(result_it, set.find(3)); - result_it = set.erase(set.find(5)); - EXPECT_EQ(result_it, set.end()); -} - -TEST(Btree, ExtractAndInsertNodeHandleSet) { - absl::btree_set src1 = {1, 2, 3, 4, 5}; - auto nh = src1.extract(src1.find(3)); - EXPECT_THAT(src1, ElementsAre(1, 2, 4, 5)); - absl::btree_set other; - absl::btree_set::insert_return_type res = other.insert(std::move(nh)); - EXPECT_THAT(other, ElementsAre(3)); - EXPECT_EQ(res.position, other.find(3)); - EXPECT_TRUE(res.inserted); - EXPECT_TRUE(res.node.empty()); - - absl::btree_set src2 = {3, 4}; - nh = src2.extract(src2.find(3)); - EXPECT_THAT(src2, ElementsAre(4)); - res = other.insert(std::move(nh)); - EXPECT_THAT(other, ElementsAre(3)); - EXPECT_EQ(res.position, other.find(3)); - EXPECT_FALSE(res.inserted); - ASSERT_FALSE(res.node.empty()); - EXPECT_EQ(res.node.value(), 3); -} - -template -void TestExtractWithTrackingForSet() { - InstanceTracker tracker; - { - Set s; - // Add enough elements to make sure we test internal nodes too. - const size_t kSize = 1000; - while (s.size() < kSize) { - s.insert(MovableOnlyInstance(s.size())); - } - for (int i = 0; i < kSize; ++i) { - // Extract with key - auto nh = s.extract(MovableOnlyInstance(i)); - EXPECT_EQ(s.size(), kSize - 1); - EXPECT_EQ(nh.value().value(), i); - // Insert with node - s.insert(std::move(nh)); - EXPECT_EQ(s.size(), kSize); - - // Extract with iterator - auto it = s.find(MovableOnlyInstance(i)); - nh = s.extract(it); - EXPECT_EQ(s.size(), kSize - 1); - EXPECT_EQ(nh.value().value(), i); - // Insert with node and hint - s.insert(s.begin(), std::move(nh)); - EXPECT_EQ(s.size(), kSize); - } - } - EXPECT_EQ(0, tracker.instances()); -} - -template -void TestExtractWithTrackingForMap() { - InstanceTracker tracker; - { - Map m; - // Add enough elements to make sure we test internal nodes too. - const size_t kSize = 1000; - while (m.size() < kSize) { - m.insert( - {CopyableMovableInstance(m.size()), MovableOnlyInstance(m.size())}); - } - for (int i = 0; i < kSize; ++i) { - // Extract with key - auto nh = m.extract(CopyableMovableInstance(i)); - EXPECT_EQ(m.size(), kSize - 1); - EXPECT_EQ(nh.key().value(), i); - EXPECT_EQ(nh.mapped().value(), i); - // Insert with node - m.insert(std::move(nh)); - EXPECT_EQ(m.size(), kSize); - - // Extract with iterator - auto it = m.find(CopyableMovableInstance(i)); - nh = m.extract(it); - EXPECT_EQ(m.size(), kSize - 1); - EXPECT_EQ(nh.key().value(), i); - EXPECT_EQ(nh.mapped().value(), i); - // Insert with node and hint - m.insert(m.begin(), std::move(nh)); - EXPECT_EQ(m.size(), kSize); - } - } - EXPECT_EQ(0, tracker.instances()); -} - -TEST(Btree, ExtractTracking) { - TestExtractWithTrackingForSet>(); - TestExtractWithTrackingForSet>(); - TestExtractWithTrackingForMap< - absl::btree_map>(); - TestExtractWithTrackingForMap< - absl::btree_multimap>(); -} - -TEST(Btree, ExtractAndInsertNodeHandleMultiSet) { - absl::btree_multiset src1 = {1, 2, 3, 3, 4, 5}; - auto nh = src1.extract(src1.find(3)); - EXPECT_THAT(src1, ElementsAre(1, 2, 3, 4, 5)); - absl::btree_multiset other; - auto res = other.insert(std::move(nh)); - EXPECT_THAT(other, ElementsAre(3)); - EXPECT_EQ(res, other.find(3)); - - absl::btree_multiset src2 = {3, 4}; - nh = src2.extract(src2.find(3)); - EXPECT_THAT(src2, ElementsAre(4)); - res = other.insert(std::move(nh)); - EXPECT_THAT(other, ElementsAre(3, 3)); - EXPECT_EQ(res, ++other.find(3)); -} - -TEST(Btree, ExtractAndInsertNodeHandleMap) { - absl::btree_map src1 = {{1, 2}, {3, 4}, {5, 6}}; - auto nh = src1.extract(src1.find(3)); - EXPECT_THAT(src1, ElementsAre(Pair(1, 2), Pair(5, 6))); - absl::btree_map other; - absl::btree_map::insert_return_type res = - other.insert(std::move(nh)); - EXPECT_THAT(other, ElementsAre(Pair(3, 4))); - EXPECT_EQ(res.position, other.find(3)); - EXPECT_TRUE(res.inserted); - EXPECT_TRUE(res.node.empty()); - - absl::btree_map src2 = {{3, 6}}; - nh = src2.extract(src2.find(3)); - EXPECT_TRUE(src2.empty()); - res = other.insert(std::move(nh)); - EXPECT_THAT(other, ElementsAre(Pair(3, 4))); - EXPECT_EQ(res.position, other.find(3)); - EXPECT_FALSE(res.inserted); - ASSERT_FALSE(res.node.empty()); - EXPECT_EQ(res.node.key(), 3); - EXPECT_EQ(res.node.mapped(), 6); -} - -TEST(Btree, ExtractAndInsertNodeHandleMultiMap) { - absl::btree_multimap src1 = {{1, 2}, {3, 4}, {5, 6}}; - auto nh = src1.extract(src1.find(3)); - EXPECT_THAT(src1, ElementsAre(Pair(1, 2), Pair(5, 6))); - absl::btree_multimap other; - auto res = other.insert(std::move(nh)); - EXPECT_THAT(other, ElementsAre(Pair(3, 4))); - EXPECT_EQ(res, other.find(3)); - - absl::btree_multimap src2 = {{3, 6}}; - nh = src2.extract(src2.find(3)); - EXPECT_TRUE(src2.empty()); - res = other.insert(std::move(nh)); - EXPECT_THAT(other, ElementsAre(Pair(3, 4), Pair(3, 6))); - EXPECT_EQ(res, ++other.begin()); -} - -// For multisets, insert with hint also affects correctness because we need to -// insert immediately before the hint if possible. -struct InsertMultiHintData { - int key; - int not_key; - bool operator==(const InsertMultiHintData other) const { - return key == other.key && not_key == other.not_key; - } -}; - -struct InsertMultiHintDataKeyCompare { - using is_transparent = void; - bool operator()(const InsertMultiHintData a, - const InsertMultiHintData b) const { - return a.key < b.key; - } - bool operator()(const int a, const InsertMultiHintData b) const { - return a < b.key; - } - bool operator()(const InsertMultiHintData a, const int b) const { - return a.key < b; - } -}; - -TEST(Btree, InsertHintNodeHandle) { - // For unique sets, insert with hint is just a performance optimization. - // Test that insert works correctly when the hint is right or wrong. - { - absl::btree_set src = {1, 2, 3, 4, 5}; - auto nh = src.extract(src.find(3)); - EXPECT_THAT(src, ElementsAre(1, 2, 4, 5)); - absl::btree_set other = {0, 100}; - // Test a correct hint. - auto it = other.insert(other.lower_bound(3), std::move(nh)); - EXPECT_THAT(other, ElementsAre(0, 3, 100)); - EXPECT_EQ(it, other.find(3)); - - nh = src.extract(src.find(5)); - // Test an incorrect hint. - it = other.insert(other.end(), std::move(nh)); - EXPECT_THAT(other, ElementsAre(0, 3, 5, 100)); - EXPECT_EQ(it, other.find(5)); - } - - absl::btree_multiset src = - {{1, 2}, {3, 4}, {3, 5}}; - auto nh = src.extract(src.lower_bound(3)); - EXPECT_EQ(nh.value(), (InsertMultiHintData{3, 4})); - absl::btree_multiset - other = {{3, 1}, {3, 2}, {3, 3}}; - auto it = other.insert(--other.end(), std::move(nh)); - EXPECT_THAT( - other, ElementsAre(InsertMultiHintData{3, 1}, InsertMultiHintData{3, 2}, - InsertMultiHintData{3, 4}, InsertMultiHintData{3, 3})); - EXPECT_EQ(it, --(--other.end())); - - nh = src.extract(src.find(3)); - EXPECT_EQ(nh.value(), (InsertMultiHintData{3, 5})); - it = other.insert(other.begin(), std::move(nh)); - EXPECT_THAT(other, - ElementsAre(InsertMultiHintData{3, 5}, InsertMultiHintData{3, 1}, - InsertMultiHintData{3, 2}, InsertMultiHintData{3, 4}, - InsertMultiHintData{3, 3})); - EXPECT_EQ(it, other.begin()); -} - -struct IntCompareToCmp { - absl::weak_ordering operator()(int a, int b) const { - if (a < b) return absl::weak_ordering::less; - if (a > b) return absl::weak_ordering::greater; - return absl::weak_ordering::equivalent; - } -}; - -TEST(Btree, MergeIntoUniqueContainers) { - absl::btree_set src1 = {1, 2, 3}; - absl::btree_multiset src2 = {3, 4, 4, 5}; - absl::btree_set dst; - - dst.merge(src1); - EXPECT_TRUE(src1.empty()); - EXPECT_THAT(dst, ElementsAre(1, 2, 3)); - dst.merge(src2); - EXPECT_THAT(src2, ElementsAre(3, 4)); - EXPECT_THAT(dst, ElementsAre(1, 2, 3, 4, 5)); -} - -TEST(Btree, MergeIntoUniqueContainersWithCompareTo) { - absl::btree_set src1 = {1, 2, 3}; - absl::btree_multiset src2 = {3, 4, 4, 5}; - absl::btree_set dst; - - dst.merge(src1); - EXPECT_TRUE(src1.empty()); - EXPECT_THAT(dst, ElementsAre(1, 2, 3)); - dst.merge(src2); - EXPECT_THAT(src2, ElementsAre(3, 4)); - EXPECT_THAT(dst, ElementsAre(1, 2, 3, 4, 5)); -} - -TEST(Btree, MergeIntoMultiContainers) { - absl::btree_set src1 = {1, 2, 3}; - absl::btree_multiset src2 = {3, 4, 4, 5}; - absl::btree_multiset dst; - - dst.merge(src1); - EXPECT_TRUE(src1.empty()); - EXPECT_THAT(dst, ElementsAre(1, 2, 3)); - dst.merge(src2); - EXPECT_TRUE(src2.empty()); - EXPECT_THAT(dst, ElementsAre(1, 2, 3, 3, 4, 4, 5)); -} - -TEST(Btree, MergeIntoMultiContainersWithCompareTo) { - absl::btree_set src1 = {1, 2, 3}; - absl::btree_multiset src2 = {3, 4, 4, 5}; - absl::btree_multiset dst; - - dst.merge(src1); - EXPECT_TRUE(src1.empty()); - EXPECT_THAT(dst, ElementsAre(1, 2, 3)); - dst.merge(src2); - EXPECT_TRUE(src2.empty()); - EXPECT_THAT(dst, ElementsAre(1, 2, 3, 3, 4, 4, 5)); -} - -TEST(Btree, MergeIntoMultiMapsWithDifferentComparators) { - absl::btree_map src1 = {{1, 1}, {2, 2}, {3, 3}}; - absl::btree_multimap> src2 = { - {5, 5}, {4, 1}, {4, 4}, {3, 2}}; - absl::btree_multimap dst; - - dst.merge(src1); - EXPECT_TRUE(src1.empty()); - EXPECT_THAT(dst, ElementsAre(Pair(1, 1), Pair(2, 2), Pair(3, 3))); - dst.merge(src2); - EXPECT_TRUE(src2.empty()); - EXPECT_THAT(dst, ElementsAre(Pair(1, 1), Pair(2, 2), Pair(3, 3), Pair(3, 2), - Pair(4, 1), Pair(4, 4), Pair(5, 5))); -} - -TEST(Btree, MergeIntoSetMovableOnly) { - absl::btree_set src; - src.insert(MovableOnlyInstance(1)); - absl::btree_multiset dst1; - dst1.insert(MovableOnlyInstance(2)); - absl::btree_set dst2; - - // Test merge into multiset. - dst1.merge(src); - - EXPECT_TRUE(src.empty()); - // ElementsAre/ElementsAreArray don't work with move-only types. - ASSERT_THAT(dst1, SizeIs(2)); - EXPECT_EQ(*dst1.begin(), MovableOnlyInstance(1)); - EXPECT_EQ(*std::next(dst1.begin()), MovableOnlyInstance(2)); - - // Test merge into set. - dst2.merge(dst1); - - EXPECT_TRUE(dst1.empty()); - ASSERT_THAT(dst2, SizeIs(2)); - EXPECT_EQ(*dst2.begin(), MovableOnlyInstance(1)); - EXPECT_EQ(*std::next(dst2.begin()), MovableOnlyInstance(2)); -} - -struct KeyCompareToWeakOrdering { - template - absl::weak_ordering operator()(const T &a, const T &b) const { - return a < b ? absl::weak_ordering::less - : a == b ? absl::weak_ordering::equivalent - : absl::weak_ordering::greater; - } -}; - -struct KeyCompareToStrongOrdering { - template - absl::strong_ordering operator()(const T &a, const T &b) const { - return a < b ? absl::strong_ordering::less - : a == b ? absl::strong_ordering::equal - : absl::strong_ordering::greater; - } -}; - -TEST(Btree, UserProvidedKeyCompareToComparators) { - absl::btree_set weak_set = {1, 2, 3}; - EXPECT_TRUE(weak_set.contains(2)); - EXPECT_FALSE(weak_set.contains(4)); - - absl::btree_set strong_set = {1, 2, 3}; - EXPECT_TRUE(strong_set.contains(2)); - EXPECT_FALSE(strong_set.contains(4)); -} - -TEST(Btree, TryEmplaceBasicTest) { - absl::btree_map m; - - // Should construct a string from the literal. - m.try_emplace(1, "one"); - EXPECT_EQ(1, m.size()); - - // Try other string constructors and const lvalue key. - const int key(42); - m.try_emplace(key, 3, 'a'); - m.try_emplace(2, std::string("two")); - - EXPECT_TRUE(std::is_sorted(m.begin(), m.end())); - EXPECT_THAT(m, ElementsAreArray(std::vector>{ - {1, "one"}, {2, "two"}, {42, "aaa"}})); -} - -TEST(Btree, TryEmplaceWithHintWorks) { - // Use a counting comparator here to verify that hint is used. - int calls = 0; - auto cmp = [&calls](int x, int y) { - ++calls; - return x < y; - }; - using Cmp = decltype(cmp); - - absl::btree_map m(cmp); - for (int i = 0; i < 128; ++i) { - m.emplace(i, i); - } - - // Sanity check for the comparator - calls = 0; - m.emplace(127, 127); - EXPECT_GE(calls, 4); - - // Try with begin hint: - calls = 0; - auto it = m.try_emplace(m.begin(), -1, -1); - EXPECT_EQ(129, m.size()); - EXPECT_EQ(it, m.begin()); - EXPECT_LE(calls, 2); - - // Try with end hint: - calls = 0; - std::pair pair1024 = {1024, 1024}; - it = m.try_emplace(m.end(), pair1024.first, pair1024.second); - EXPECT_EQ(130, m.size()); - EXPECT_EQ(it, --m.end()); - EXPECT_LE(calls, 2); - - // Try value already present, bad hint; ensure no duplicate added: - calls = 0; - it = m.try_emplace(m.end(), 16, 17); - EXPECT_EQ(130, m.size()); - EXPECT_GE(calls, 4); - EXPECT_EQ(it, m.find(16)); - - // Try value already present, hint points directly to it: - calls = 0; - it = m.try_emplace(it, 16, 17); - EXPECT_EQ(130, m.size()); - EXPECT_LE(calls, 2); - EXPECT_EQ(it, m.find(16)); - - m.erase(2); - EXPECT_EQ(129, m.size()); - auto hint = m.find(3); - // Try emplace in the middle of two other elements. - calls = 0; - m.try_emplace(hint, 2, 2); - EXPECT_EQ(130, m.size()); - EXPECT_LE(calls, 2); - - EXPECT_TRUE(std::is_sorted(m.begin(), m.end())); -} - -TEST(Btree, TryEmplaceWithBadHint) { - absl::btree_map m = {{1, 1}, {9, 9}}; - - // Bad hint (too small), should still emplace: - auto it = m.try_emplace(m.begin(), 2, 2); - EXPECT_EQ(it, ++m.begin()); - EXPECT_THAT(m, ElementsAreArray( - std::vector>{{1, 1}, {2, 2}, {9, 9}})); - - // Bad hint, too large this time: - it = m.try_emplace(++(++m.begin()), 0, 0); - EXPECT_EQ(it, m.begin()); - EXPECT_THAT(m, ElementsAreArray(std::vector>{ - {0, 0}, {1, 1}, {2, 2}, {9, 9}})); -} - -TEST(Btree, TryEmplaceMaintainsSortedOrder) { - absl::btree_map m; - std::pair pair5 = {5, "five"}; - - // Test both lvalue & rvalue emplace. - m.try_emplace(10, "ten"); - m.try_emplace(pair5.first, pair5.second); - EXPECT_EQ(2, m.size()); - EXPECT_TRUE(std::is_sorted(m.begin(), m.end())); - - int int100{100}; - m.try_emplace(int100, "hundred"); - m.try_emplace(1, "one"); - EXPECT_EQ(4, m.size()); - EXPECT_TRUE(std::is_sorted(m.begin(), m.end())); -} - -TEST(Btree, TryEmplaceWithHintAndNoValueArgsWorks) { - absl::btree_map m; - m.try_emplace(m.end(), 1); - EXPECT_EQ(0, m[1]); -} - -TEST(Btree, TryEmplaceWithHintAndMultipleValueArgsWorks) { - absl::btree_map m; - m.try_emplace(m.end(), 1, 10, 'a'); - EXPECT_EQ(std::string(10, 'a'), m[1]); -} - -TEST(Btree, MoveAssignmentAllocatorPropagation) { - InstanceTracker tracker; - - int64_t bytes1 = 0, bytes2 = 0; - PropagatingCountingAlloc allocator1(&bytes1); - PropagatingCountingAlloc allocator2(&bytes2); - std::less cmp; - - // Test propagating allocator_type. - { - absl::btree_set, - PropagatingCountingAlloc> - set1(cmp, allocator1), set2(cmp, allocator2); - - for (int i = 0; i < 100; ++i) set1.insert(MovableOnlyInstance(i)); - - tracker.ResetCopiesMovesSwaps(); - set2 = std::move(set1); - EXPECT_EQ(tracker.moves(), 0); - } - // Test non-propagating allocator_type with equal allocators. - { - absl::btree_set, - CountingAllocator> - set1(cmp, allocator1), set2(cmp, allocator1); - - for (int i = 0; i < 100; ++i) set1.insert(MovableOnlyInstance(i)); - - tracker.ResetCopiesMovesSwaps(); - set2 = std::move(set1); - EXPECT_EQ(tracker.moves(), 0); - } - // Test non-propagating allocator_type with different allocators. - { - absl::btree_set, - CountingAllocator> - set1(cmp, allocator1), set2(cmp, allocator2); - - for (int i = 0; i < 100; ++i) set1.insert(MovableOnlyInstance(i)); - - tracker.ResetCopiesMovesSwaps(); - set2 = std::move(set1); - EXPECT_GE(tracker.moves(), 100); - } -} - -TEST(Btree, EmptyTree) { - absl::btree_set s; - EXPECT_TRUE(s.empty()); - EXPECT_EQ(s.size(), 0); - EXPECT_GT(s.max_size(), 0); -} - -bool IsEven(int k) { return k % 2 == 0; } - -TEST(Btree, EraseIf) { - // Test that erase_if works with all the container types and supports lambdas. - { - absl::btree_set s = {1, 3, 5, 6, 100}; - erase_if(s, [](int k) { return k > 3; }); - EXPECT_THAT(s, ElementsAre(1, 3)); - } - { - absl::btree_multiset s = {1, 3, 3, 5, 6, 6, 100}; - erase_if(s, [](int k) { return k <= 3; }); - EXPECT_THAT(s, ElementsAre(5, 6, 6, 100)); - } - { - absl::btree_map m = {{1, 1}, {3, 3}, {6, 6}, {100, 100}}; - erase_if(m, [](std::pair kv) { return kv.first > 3; }); - EXPECT_THAT(m, ElementsAre(Pair(1, 1), Pair(3, 3))); - } - { - absl::btree_multimap m = {{1, 1}, {3, 3}, {3, 6}, - {6, 6}, {6, 7}, {100, 6}}; - erase_if(m, [](std::pair kv) { return kv.second == 6; }); - EXPECT_THAT(m, ElementsAre(Pair(1, 1), Pair(3, 3), Pair(6, 7))); - } - // Test that erasing all elements from a large set works and test support for - // function pointers. - { - absl::btree_set s; - for (int i = 0; i < 1000; ++i) s.insert(2 * i); - erase_if(s, IsEven); - EXPECT_THAT(s, IsEmpty()); - } - // Test that erase_if supports other format of function pointers. - { - absl::btree_set s = {1, 3, 5, 6, 100}; - erase_if(s, &IsEven); - EXPECT_THAT(s, ElementsAre(1, 3, 5)); - } -} - -TEST(Btree, InsertOrAssign) { - absl::btree_map m = {{1, 1}, {3, 3}}; - using value_type = typename decltype(m)::value_type; - - auto ret = m.insert_or_assign(4, 4); - EXPECT_EQ(*ret.first, value_type(4, 4)); - EXPECT_TRUE(ret.second); - ret = m.insert_or_assign(3, 100); - EXPECT_EQ(*ret.first, value_type(3, 100)); - EXPECT_FALSE(ret.second); - - auto hint_ret = m.insert_or_assign(ret.first, 3, 200); - EXPECT_EQ(*hint_ret, value_type(3, 200)); - hint_ret = m.insert_or_assign(m.find(1), 0, 1); - EXPECT_EQ(*hint_ret, value_type(0, 1)); - // Test with bad hint. - hint_ret = m.insert_or_assign(m.end(), -1, 1); - EXPECT_EQ(*hint_ret, value_type(-1, 1)); - - EXPECT_THAT(m, ElementsAre(Pair(-1, 1), Pair(0, 1), Pair(1, 1), Pair(3, 200), - Pair(4, 4))); -} - -TEST(Btree, InsertOrAssignMovableOnly) { - absl::btree_map m; - using value_type = typename decltype(m)::value_type; - - auto ret = m.insert_or_assign(4, MovableOnlyInstance(4)); - EXPECT_EQ(*ret.first, value_type(4, MovableOnlyInstance(4))); - EXPECT_TRUE(ret.second); - ret = m.insert_or_assign(4, MovableOnlyInstance(100)); - EXPECT_EQ(*ret.first, value_type(4, MovableOnlyInstance(100))); - EXPECT_FALSE(ret.second); - - auto hint_ret = m.insert_or_assign(ret.first, 3, MovableOnlyInstance(200)); - EXPECT_EQ(*hint_ret, value_type(3, MovableOnlyInstance(200))); - - EXPECT_EQ(m.size(), 2); -} - -TEST(Btree, BitfieldArgument) { - union { - int n : 1; - }; - n = 0; - absl::btree_map m; - m.erase(n); - m.count(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(Btree, SetRangeConstructorAndInsertSupportExplicitConversionComparable) { - const absl::string_view names[] = {"n1", "n2"}; - - absl::btree_set name_set1{std::begin(names), std::end(names)}; - EXPECT_THAT(name_set1, ElementsAreArray(names)); - - absl::btree_set name_set2; - name_set2.insert(std::begin(names), std::end(names)); - EXPECT_THAT(name_set2, ElementsAreArray(names)); -} - -// A type that is explicitly convertible from int and counts constructor calls. -struct ConstructorCounted { - explicit ConstructorCounted(int i) : i(i) { ++constructor_calls; } - bool operator==(int other) const { return i == other; } - - int i; - static int constructor_calls; -}; -int ConstructorCounted::constructor_calls = 0; - -struct ConstructorCountedCompare { - bool operator()(int a, const ConstructorCounted &b) const { return a < b.i; } - bool operator()(const ConstructorCounted &a, int b) const { return a.i < b; } - bool operator()(const ConstructorCounted &a, - const ConstructorCounted &b) const { - return a.i < b.i; - } - using is_transparent = void; -}; - -TEST(Btree, - SetRangeConstructorAndInsertExplicitConvComparableLimitConstruction) { - const int i[] = {0, 1, 1}; - ConstructorCounted::constructor_calls = 0; - - absl::btree_set set{ - std::begin(i), std::end(i)}; - EXPECT_THAT(set, ElementsAre(0, 1)); - EXPECT_EQ(ConstructorCounted::constructor_calls, 2); - - set.insert(std::begin(i), std::end(i)); - EXPECT_THAT(set, ElementsAre(0, 1)); - EXPECT_EQ(ConstructorCounted::constructor_calls, 2); -} - -TEST(Btree, - SetRangeConstructorAndInsertSupportExplicitConversionNonComparable) { - const int i[] = {0, 1}; - - absl::btree_set> s1{std::begin(i), std::end(i)}; - EXPECT_THAT(s1, ElementsAre(IsEmpty(), ElementsAre(IsNull()))); - - absl::btree_set> s2; - s2.insert(std::begin(i), std::end(i)); - EXPECT_THAT(s2, ElementsAre(IsEmpty(), ElementsAre(IsNull()))); -} - -// libstdc++ included with GCC 4.9 has a bug in the std::pair constructors that -// prevents explicit conversions between pair types. -// We only run this test for the libstdc++ from GCC 7 or newer because we can't -// reliably check the libstdc++ version prior to that release. -#if !defined(__GLIBCXX__) || \ - (defined(_GLIBCXX_RELEASE) && _GLIBCXX_RELEASE >= 7) -TEST(Btree, MapRangeConstructorAndInsertSupportExplicitConversionComparable) { - const std::pair names[] = {{"n1", 1}, {"n2", 2}}; - - absl::btree_map name_map1{std::begin(names), - std::end(names)}; - EXPECT_THAT(name_map1, ElementsAre(Pair("n1", 1), Pair("n2", 2))); - - absl::btree_map name_map2; - name_map2.insert(std::begin(names), std::end(names)); - EXPECT_THAT(name_map2, ElementsAre(Pair("n1", 1), Pair("n2", 2))); -} - -TEST(Btree, - MapRangeConstructorAndInsertExplicitConvComparableLimitConstruction) { - const std::pair i[] = {{0, 1}, {1, 2}, {1, 3}}; - ConstructorCounted::constructor_calls = 0; - - absl::btree_map map{ - std::begin(i), std::end(i)}; - EXPECT_THAT(map, ElementsAre(Pair(0, 1), Pair(1, 2))); - EXPECT_EQ(ConstructorCounted::constructor_calls, 2); - - map.insert(std::begin(i), std::end(i)); - EXPECT_THAT(map, ElementsAre(Pair(0, 1), Pair(1, 2))); - EXPECT_EQ(ConstructorCounted::constructor_calls, 2); -} - -TEST(Btree, - MapRangeConstructorAndInsertSupportExplicitConversionNonComparable) { - const std::pair i[] = {{0, 1}, {1, 2}}; - - absl::btree_map, int> m1{std::begin(i), std::end(i)}; - EXPECT_THAT(m1, - ElementsAre(Pair(IsEmpty(), 1), Pair(ElementsAre(IsNull()), 2))); - - absl::btree_map, int> m2; - m2.insert(std::begin(i), std::end(i)); - EXPECT_THAT(m2, - ElementsAre(Pair(IsEmpty(), 1), Pair(ElementsAre(IsNull()), 2))); -} - -TEST(Btree, HeterogeneousTryEmplace) { - absl::btree_map m; - std::string s = "key"; - absl::string_view sv = s; - m.try_emplace(sv, 1); - EXPECT_EQ(m[s], 1); - - m.try_emplace(m.end(), sv, 2); - EXPECT_EQ(m[s], 1); -} - -TEST(Btree, HeterogeneousOperatorMapped) { - absl::btree_map m; - std::string s = "key"; - absl::string_view sv = s; - m[sv] = 1; - EXPECT_EQ(m[s], 1); - - m[sv] = 2; - EXPECT_EQ(m[s], 2); -} - -TEST(Btree, HeterogeneousInsertOrAssign) { - absl::btree_map m; - std::string s = "key"; - absl::string_view sv = s; - m.insert_or_assign(sv, 1); - EXPECT_EQ(m[s], 1); - - m.insert_or_assign(m.end(), sv, 2); - EXPECT_EQ(m[s], 2); -} -#endif - -// This test requires std::launder for mutable key access in node handles. -#if defined(__cpp_lib_launder) && __cpp_lib_launder >= 201606 -TEST(Btree, NodeHandleMutableKeyAccess) { - { - absl::btree_map map; - - map["key1"] = "mapped"; - - auto nh = map.extract(map.begin()); - nh.key().resize(3); - map.insert(std::move(nh)); - - EXPECT_THAT(map, ElementsAre(Pair("key", "mapped"))); - } - // Also for multimap. - { - absl::btree_multimap map; - - map.emplace("key1", "mapped"); - - auto nh = map.extract(map.begin()); - nh.key().resize(3); - map.insert(std::move(nh)); - - EXPECT_THAT(map, ElementsAre(Pair("key", "mapped"))); - } -} -#endif - -struct MultiKey { - int i1; - int i2; -}; - -bool operator==(const MultiKey a, const MultiKey b) { - return a.i1 == b.i1 && a.i2 == b.i2; -} - -// A heterogeneous comparator that has different equivalence classes for -// different lookup types. -struct MultiKeyComp { - using is_transparent = void; - bool operator()(const MultiKey a, const MultiKey b) const { - if (a.i1 != b.i1) return a.i1 < b.i1; - return a.i2 < b.i2; - } - bool operator()(const int a, const MultiKey b) const { return a < b.i1; } - bool operator()(const MultiKey a, const int b) const { return a.i1 < b; } -}; - -TEST(Btree, MultiKeyEqualRange) { - absl::btree_set set; - - for (int i = 0; i < 100; ++i) { - for (int j = 0; j < 100; ++j) { - set.insert({i, j}); - } - } - - for (int i = 0; i < 100; ++i) { - auto equal_range = set.equal_range(i); - EXPECT_EQ(equal_range.first->i1, i); - EXPECT_EQ(equal_range.first->i2, 0); - EXPECT_EQ(std::distance(equal_range.first, equal_range.second), 100) << i; - } -} - -TEST(Btree, MultiKeyErase) { - absl::btree_set set = { - {1, 1}, {2, 1}, {2, 2}, {3, 1}}; - EXPECT_EQ(set.erase(2), 2); - EXPECT_THAT(set, ElementsAre(MultiKey{1, 1}, MultiKey{3, 1})); -} - -TEST(Btree, MultiKeyCount) { - const absl::btree_set set = { - {1, 1}, {2, 1}, {2, 2}, {3, 1}}; - EXPECT_EQ(set.count(2), 2); -} - -TEST(Btree, AllocConstructor) { - using Alloc = CountingAllocator; - using Set = absl::btree_set, Alloc>; - int64_t bytes_used = 0; - Alloc alloc(&bytes_used); - Set set(alloc); - - set.insert({1, 2, 3}); - - EXPECT_THAT(set, ElementsAre(1, 2, 3)); - EXPECT_GT(bytes_used, set.size() * sizeof(int)); -} - -TEST(Btree, AllocInitializerListConstructor) { - using Alloc = CountingAllocator; - using Set = absl::btree_set, Alloc>; - int64_t bytes_used = 0; - Alloc alloc(&bytes_used); - Set set({1, 2, 3}, alloc); - - EXPECT_THAT(set, ElementsAre(1, 2, 3)); - EXPECT_GT(bytes_used, set.size() * sizeof(int)); -} - -TEST(Btree, AllocRangeConstructor) { - using Alloc = CountingAllocator; - using Set = absl::btree_set, Alloc>; - int64_t bytes_used = 0; - Alloc alloc(&bytes_used); - std::vector v = {1, 2, 3}; - Set set(v.begin(), v.end(), alloc); - - EXPECT_THAT(set, ElementsAre(1, 2, 3)); - EXPECT_GT(bytes_used, set.size() * sizeof(int)); -} - -TEST(Btree, AllocCopyConstructor) { - using Alloc = CountingAllocator; - using Set = absl::btree_set, Alloc>; - int64_t bytes_used1 = 0; - Alloc alloc1(&bytes_used1); - Set set1(alloc1); - - set1.insert({1, 2, 3}); - - int64_t bytes_used2 = 0; - Alloc alloc2(&bytes_used2); - Set set2(set1, alloc2); - - EXPECT_THAT(set1, ElementsAre(1, 2, 3)); - EXPECT_THAT(set2, ElementsAre(1, 2, 3)); - EXPECT_GT(bytes_used1, set1.size() * sizeof(int)); - EXPECT_EQ(bytes_used1, bytes_used2); -} - -TEST(Btree, AllocMoveConstructor_SameAlloc) { - using Alloc = CountingAllocator; - using Set = absl::btree_set, Alloc>; - int64_t bytes_used = 0; - Alloc alloc(&bytes_used); - Set set1(alloc); - - set1.insert({1, 2, 3}); - - const int64_t original_bytes_used = bytes_used; - EXPECT_GT(original_bytes_used, set1.size() * sizeof(int)); - - Set set2(std::move(set1), alloc); - - EXPECT_THAT(set2, ElementsAre(1, 2, 3)); - EXPECT_EQ(bytes_used, original_bytes_used); -} - -TEST(Btree, AllocMoveConstructor_DifferentAlloc) { - using Alloc = CountingAllocator; - using Set = absl::btree_set, Alloc>; - int64_t bytes_used1 = 0; - Alloc alloc1(&bytes_used1); - Set set1(alloc1); - - set1.insert({1, 2, 3}); - - const int64_t original_bytes_used = bytes_used1; - EXPECT_GT(original_bytes_used, set1.size() * sizeof(int)); - - int64_t bytes_used2 = 0; - Alloc alloc2(&bytes_used2); - Set set2(std::move(set1), alloc2); - - EXPECT_THAT(set2, ElementsAre(1, 2, 3)); - // We didn't free these bytes allocated by `set1` yet. - EXPECT_EQ(bytes_used1, original_bytes_used); - EXPECT_EQ(bytes_used2, original_bytes_used); -} - -} // namespace -} // namespace container_internal -ABSL_NAMESPACE_END -} // namespace absl diff --git a/third_party/abseil_cpp/absl/container/btree_test.h b/third_party/abseil_cpp/absl/container/btree_test.h deleted file mode 100644 index 624908072d59..000000000000 --- a/third_party/abseil_cpp/absl/container/btree_test.h +++ /dev/null @@ -1,166 +0,0 @@ -// 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 -#include -#include -#include -#include -#include - -#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 -struct remove_pair_const { - using type = typename std::remove_const::type; -}; -template -struct remove_pair_const > { - using type = std::pair::type, - typename remove_pair_const::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 -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 -struct KeyOfValue { - 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 -struct Generator { - int maxval; - explicit Generator(int m) : maxval(m) {} - K operator()(int i) const { - assert(i <= maxval); - return K(i); - } -}; - -template <> -struct Generator { - int maxval; - explicit Generator(int m) : maxval(m) {} - absl::Time operator()(int i) const { return absl::FromUnixMillis(i); } -}; - -template <> -struct Generator { - 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 { - int maxval; - explicit Generator(int m) : maxval(m) {} - Cord operator()(int i) const { - char buf[16]; - return Cord(GenerateDigits(buf, i, maxval)); - } -}; - -template -struct Generator > { - Generator::type> tgen; - Generator::type> ugen; - - explicit Generator(int m) : tgen(m), ugen(m) {} - std::pair 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 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 values; - absl::flat_hash_set unique_values; - if (values.size() < n) { - for (int i = values.size(); i < n; i++) { - int value; - do { - value = static_cast(rng()) % (maxval + 1); - } while (!unique_values.insert(value).second); - - values.push_back(value); - } - } - return values; -} - -// Generates n values in the range [0, maxval]. -template -std::vector GenerateValuesWithSeed(int n, int maxval, int seed) { - const std::vector nums = GenerateNumbersWithSeed(n, maxval, seed); - Generator gen(maxval); - std::vector 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_ diff --git a/third_party/abseil_cpp/absl/container/fixed_array.h b/third_party/abseil_cpp/absl/container/fixed_array.h deleted file mode 100644 index fcb3e545b243..000000000000 --- a/third_party/abseil_cpp/absl/container/fixed_array.h +++ /dev/null @@ -1,532 +0,0 @@ -// 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` 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 -#include -#include -#include -#include -#include -#include -#include -#include - -#include "absl/algorithm/algorithm.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(-1); - -// ----------------------------------------------------------------------------- -// FixedArray -// ----------------------------------------------------------------------------- -// -// A `FixedArray` provides a run-time fixed-size array, allocating a small array -// inline for efficiency. -// -// Most users should not specify an `inline_elements` argument and let -// `FixedArray` automatically determine the number of elements -// to store inline based on `sizeof(T)`. If `inline_elements` is specified, the -// `FixedArray` implementation will use inline storage for arrays with a -// length <= `inline_elements`. -// -// 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`. -// -// Note that `FixedArray` does not provide a public allocator; if it requires a -// heap allocation, it will do so with global `::operator new[]()` and -// `::operator delete[]()`, even if T provides class-scope overrides for these -// operators. -template > -class FixedArray { - static_assert(!std::is_array::value || std::extent::value > 0, - "Arrays with unknown bounds cannot be used with FixedArray."); - - static constexpr size_t kInlineBytesDefault = 256; - - using AllocatorTraits = std::allocator_traits; - // std::iterator_traits isn't guaranteed to be SFINAE-friendly until C++17, - // but this seems to be mostly pedantic. - template - using EnableIfForwardIterator = absl::enable_if_t::iterator_category, - std::forward_iterator_tag>::value>; - static constexpr bool NoexceptCopyable() { - return std::is_nothrow_copy_constructible::value && - absl::allocator_is_nothrow::value; - } - static constexpr bool NoexceptMovable() { - return std::is_nothrow_move_constructible::value && - absl::allocator_is_nothrow::value; - } - static constexpr bool DefaultConstructorIsNonTrivial() { - return !absl::is_trivially_default_constructible::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; - using const_reverse_iterator = std::reverse_iterator; - - static constexpr size_type inline_elements = - (N == kFixedArrayUseDefault ? kInlineBytesDefault / sizeof(value_type) - : static_cast(N)); - - FixedArray( - const FixedArray& other, - const allocator_type& a = allocator_type()) noexcept(NoexceptCopyable()) - : FixedArray(other.begin(), other.end(), a) {} - - FixedArray( - FixedArray&& other, - const allocator_type& a = allocator_type()) 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 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 * = 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`. 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::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 { 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() { 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_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_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) { - 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 { - 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_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_HARDENING_ASSERT(!empty()); - return data()[0]; - } - - // FixedArray::back() - // - // Returns a reference to the last element of the fixed array. - reference back() { - 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_HARDENING_ASSERT(!empty()); - return data()[size() - 1]; - } - - // FixedArray::begin() - // - // Returns an iterator to the beginning of the fixed array. - iterator begin() { return data(); } - - // Overload of FixedArray::begin() to return a const iterator to the - // beginning of the fixed array. - const_iterator begin() const { return data(); } - - // FixedArray::cbegin() - // - // Returns a const iterator to the beginning of the fixed array. - const_iterator cbegin() const { return begin(); } - - // FixedArray::end() - // - // Returns an iterator to the end of the fixed array. - iterator end() { return data() + size(); } - - // Overload of FixedArray::end() to return a const iterator to the end of the - // fixed array. - const_iterator end() const { return data() + size(); } - - // FixedArray::cend() - // - // Returns a const iterator to the end of the fixed array. - const_iterator cend() const { return end(); } - - // FixedArray::rbegin() - // - // Returns a reverse iterator from the end of the fixed array. - reverse_iterator rbegin() { 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 { - return const_reverse_iterator(end()); - } - - // FixedArray::crbegin() - // - // Returns a const reverse iterator from the end of the fixed array. - const_reverse_iterator crbegin() const { return rbegin(); } - - // FixedArray::rend() - // - // Returns a reverse iterator from the beginning of the fixed array. - reverse_iterator rend() { 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 { - return const_reverse_iterator(begin()); - } - - // FixedArray::crend() - // - // Returns a reverse iterator from the beginning of the fixed array. - const_reverse_iterator crend() const { 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 absl::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 - 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 buf(1); - // sprintf(buf.data(), "foo"); - // - // error: call to int __builtin___sprintf_chk(etc...) - // will always overflow destination buffer [-Werror] - // - template , - size_t InnerN = std::extent::value> - struct StorageElementWrapper { - InnerT array[InnerN]; - }; - - using StorageElement = - absl::conditional_t::value, - StorageElementWrapper, value_type>; - - static pointer AsValueType(pointer ptr) { return ptr; } - static pointer AsValueType(StorageElementWrapper* 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(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; - - // 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>(); } - - private: - static bool UsingInlinedStorage(size_type n) { - return n <= inline_elements; - } - - StorageElement* InitializeData() { - if (UsingInlinedStorage(size())) { - InlinedStorage::AnnotateConstruct(size()); - return InlinedStorage::data(); - } else { - return reinterpret_cast( - AllocatorTraits::allocate(alloc(), size())); - } - } - - // `CompressedTuple` takes advantage of EBCO for stateless `allocator_type`s - container_internal::CompressedTuple size_alloc_; - StorageElement* data_; - }; - - Storage storage_; -}; - -template -constexpr size_t FixedArray::kInlineBytesDefault; - -template -constexpr typename FixedArray::size_type - FixedArray::inline_elements; - -template -void FixedArray::NonEmptyInlinedStorage::AnnotateConstruct( - typename FixedArray::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(n); // Mark used when not in asan mode -} - -template -void FixedArray::NonEmptyInlinedStorage::AnnotateDestruct( - typename FixedArray::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(n); // Mark used when not in asan mode -} -ABSL_NAMESPACE_END -} // namespace absl - -#endif // ABSL_CONTAINER_FIXED_ARRAY_H_ diff --git a/third_party/abseil_cpp/absl/container/fixed_array_benchmark.cc b/third_party/abseil_cpp/absl/container/fixed_array_benchmark.cc deleted file mode 100644 index 3c7a5a723450..000000000000 --- a/third_party/abseil_cpp/absl/container/fixed_array_benchmark.cc +++ /dev/null @@ -1,67 +0,0 @@ -// 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 - -#include - -#include "benchmark/benchmark.h" -#include "absl/container/fixed_array.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 -void BM_FixedArray(benchmark::State& state) { - const int size = state.range(0); - for (auto _ : state) { - absl::FixedArray 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 diff --git a/third_party/abseil_cpp/absl/container/fixed_array_exception_safety_test.cc b/third_party/abseil_cpp/absl/container/fixed_array_exception_safety_test.cc deleted file mode 100644 index e5f59299b358..000000000000 --- a/third_party/abseil_cpp/absl/container/fixed_array_exception_safety_test.cc +++ /dev/null @@ -1,201 +0,0 @@ -// 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 - -#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; -using ThrowAlloc = - testing::ThrowingAllocator; -using MoveThrower = testing::ThrowingValue; -using MoveThrowAlloc = - testing::ThrowingAllocator; - -using FixedArr = absl::FixedArray; -using FixedArrWithAlloc = absl::FixedArray; - -using MoveFixedArr = absl::FixedArray; -using MoveFixedArrWithAlloc = - absl::FixedArray; - -TEST(FixedArrayExceptionSafety, CopyConstructor) { - auto small = FixedArr(kSmallSize); - TestThrowingCtor(small); - - auto large = FixedArr(kLargeSize); - TestThrowingCtor(large); -} - -TEST(FixedArrayExceptionSafety, CopyConstructorWithAlloc) { - auto small = FixedArrWithAlloc(kSmallSize); - TestThrowingCtor(small); - - auto large = FixedArrWithAlloc(kLargeSize); - TestThrowingCtor(large); -} - -TEST(FixedArrayExceptionSafety, MoveConstructor) { - TestThrowingCtor(FixedArr(kSmallSize)); - TestThrowingCtor(FixedArr(kLargeSize)); - - // TypeSpec::kNoThrowMove - TestThrowingCtor(MoveFixedArr(kSmallSize)); - TestThrowingCtor(MoveFixedArr(kLargeSize)); -} - -TEST(FixedArrayExceptionSafety, MoveConstructorWithAlloc) { - TestThrowingCtor(FixedArrWithAlloc(kSmallSize)); - TestThrowingCtor(FixedArrWithAlloc(kLargeSize)); - - // TypeSpec::kNoThrowMove - TestThrowingCtor(MoveFixedArrWithAlloc(kSmallSize)); - TestThrowingCtor(MoveFixedArrWithAlloc(kLargeSize)); -} - -TEST(FixedArrayExceptionSafety, SizeConstructor) { - TestThrowingCtor(kSmallSize); - TestThrowingCtor(kLargeSize); -} - -TEST(FixedArrayExceptionSafety, SizeConstructorWithAlloc) { - TestThrowingCtor(kSmallSize); - TestThrowingCtor(kLargeSize); -} - -TEST(FixedArrayExceptionSafety, SizeValueConstructor) { - TestThrowingCtor(kSmallSize, Thrower()); - TestThrowingCtor(kLargeSize, Thrower()); -} - -TEST(FixedArrayExceptionSafety, SizeValueConstructorWithAlloc) { - TestThrowingCtor(kSmallSize, Thrower()); - TestThrowingCtor(kLargeSize, Thrower()); -} - -TEST(FixedArrayExceptionSafety, IteratorConstructor) { - auto small = FixedArr(kSmallSize); - TestThrowingCtor(small.begin(), small.end()); - - auto large = FixedArr(kLargeSize); - TestThrowingCtor(large.begin(), large.end()); -} - -TEST(FixedArrayExceptionSafety, IteratorConstructorWithAlloc) { - auto small = FixedArrWithAlloc(kSmallSize); - TestThrowingCtor(small.begin(), small.end()); - - auto large = FixedArrWithAlloc(kLargeSize); - TestThrowingCtor(large.begin(), large.end()); -} - -TEST(FixedArrayExceptionSafety, InitListConstructor) { - constexpr int small_inlined = 3; - using SmallFixedArr = absl::FixedArray; - - TestThrowingCtor(std::initializer_list{}); - // Test inlined allocation - TestThrowingCtor( - std::initializer_list{Thrower{}, Thrower{}}); - // Test out of line allocation - TestThrowingCtor(std::initializer_list{ - Thrower{}, Thrower{}, Thrower{}, Thrower{}, Thrower{}}); -} - -TEST(FixedArrayExceptionSafety, InitListConstructorWithAlloc) { - constexpr int small_inlined = 3; - using SmallFixedArrWithAlloc = - absl::FixedArray; - - TestThrowingCtor(std::initializer_list{}); - // Test inlined allocation - TestThrowingCtor( - std::initializer_list{Thrower{}, Thrower{}}); - // Test out of line allocation - TestThrowingCtor(std::initializer_list{ - Thrower{}, Thrower{}, Thrower{}, Thrower{}, Thrower{}}); -} - -template -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) - .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) - .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 diff --git a/third_party/abseil_cpp/absl/container/fixed_array_test.cc b/third_party/abseil_cpp/absl/container/fixed_array_test.cc deleted file mode 100644 index 49598e7a053c..000000000000 --- a/third_party/abseil_cpp/absl/container/fixed_array_test.cc +++ /dev/null @@ -1,837 +0,0 @@ -// 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 - -#include -#include -#include -#include -#include -#include -#include -#include - -#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/counting_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 -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 on_stack(5); - std::iota(on_stack.begin(), on_stack.end(), 0); - absl::FixedArray stack_copy = on_stack; - EXPECT_THAT(stack_copy, ElementsAreArray(on_stack)); - EXPECT_TRUE(IsOnStack(stack_copy)); - - absl::FixedArray allocated(15); - std::iota(allocated.begin(), allocated.end(), 0); - absl::FixedArray alloced_copy = allocated; - EXPECT_THAT(alloced_copy, ElementsAreArray(allocated)); - EXPECT_FALSE(IsOnStack(alloced_copy)); -} - -TEST(FixedArrayTest, MoveCtor) { - absl::FixedArray, 10> on_stack(5); - for (int i = 0; i < 5; ++i) { - on_stack[i] = absl::make_unique(i); - } - - absl::FixedArray, 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, 10> allocated(15); - for (int i = 0; i < 15; ++i) { - allocated[i] = absl::make_unique(i); - } - - absl::FixedArray, 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 array(4); - EXPECT_TRUE(IsOnStack(array)); - } - - { - // Large arrays should be on the heap - absl::FixedArray array(1048576); - EXPECT_FALSE(IsOnStack(array)); - } - - { - // Arrays of <= default size should be on the stack - absl::FixedArray array(100); - EXPECT_TRUE(IsOnStack(array)); - } - - { - // Arrays of > default size should be on the heap - absl::FixedArray array(101); - EXPECT_FALSE(IsOnStack(array)); - } - - { - // Arrays with different size elements should use approximately - // same amount of stack space - absl::FixedArray array1(0); - absl::FixedArray 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> 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 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 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 a = {1, 2, 3}; - EXPECT_EQ(a[2], 3); - EXPECT_DEATH_IF_SUPPORTED(a[3], ""); - EXPECT_DEATH_IF_SUPPORTED(a[-1], ""); - - absl::FixedArray 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 a1(i); - std::iota(a1.begin(), a1.end(), 0); - absl::FixedArray 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 a1(i); - std::iota(a1.begin(), a1.end(), 0); - absl::FixedArray 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 -static void TestArray(int n) { - SCOPED_TRACE(n); - SCOPED_TRACE(stack_elements); - ConstructionTester::constructions = 0; - ConstructionTester::destructions = 0; - { - absl::FixedArray 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 -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>(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 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 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 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 const empty; - absl::FixedArray 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 const items(kInput, kInput + ABSL_ARRAYSIZE(kInput)); - absl::FixedArray 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 const items(kInput, kInput + ABSL_ARRAYSIZE(kInput)); - absl::FixedArray const fixed(items.begin(), items.end()); - EXPECT_THAT(fixed, testing::ElementsAreArray(kInput)); -} - -TEST(InitListConstructorTest, InitListConstruction) { - absl::FixedArray fixed = {1, 2, 3}; - EXPECT_THAT(fixed, testing::ElementsAreArray({1, 2, 3})); -} - -TEST(FillConstructorTest, NonEmptyArrays) { - absl::FixedArray stack_array(4, 1); - EXPECT_THAT(stack_array, testing::ElementsAreArray({1, 1, 1, 1})); - - absl::FixedArray heap_array(4, 1); - EXPECT_THAT(stack_array, testing::ElementsAreArray({1, 1, 1, 1})); -} - -TEST(FillConstructorTest, EmptyArray) { - absl::FixedArray empty_fill(0, 1); - absl::FixedArray empty_size(0); - EXPECT_EQ(empty_fill, empty_size); -} - -TEST(FillConstructorTest, NotTriviallyCopyable) { - std::string str = "abcd"; - absl::FixedArray strings = {str, str, str, str}; - - absl::FixedArray array(4, str); - EXPECT_EQ(array, strings); -} - -TEST(FillConstructorTest, Disambiguation) { - absl::FixedArray a(1, 2); - EXPECT_THAT(a, testing::ElementsAre(2)); -} - -TEST(FixedArrayTest, ManySizedArrays) { - std::vector 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 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) == sizeof(Data), - "0-sized absl::FixedArray should have same size as Data."); - static_assert(alignof(absl::FixedArray) == alignof(Data), - "0-sized absl::FixedArray should have same alignment as Data."); - static_assert(sizeof(absl::FixedArray) == sizeof(Data), - "default-sized absl::FixedArray should have same size as Data"); - static_assert( - alignof(absl::FixedArray) == 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 a(5); } - -TEST(FixedArrayTest, Data) { - static const int kInput[] = {2, 3, 5, 7, 11, 13, 17}; - absl::FixedArray fa(std::begin(kInput), std::end(kInput)); - EXPECT_EQ(fa.data(), &*fa.begin()); - EXPECT_EQ(fa.data(), &fa[0]); - - const absl::FixedArray& cfa = fa; - EXPECT_EQ(cfa.data(), &*cfa.begin()); - EXPECT_EQ(cfa.data(), &cfa[0]); -} - -TEST(FixedArrayTest, Empty) { - absl::FixedArray empty(0); - absl::FixedArray inline_filled(1); - absl::FixedArray heap_filled(1); - EXPECT_TRUE(empty.empty()); - EXPECT_FALSE(inline_filled.empty()); - EXPECT_FALSE(heap_filled.empty()); -} - -TEST(FixedArrayTest, FrontAndBack) { - absl::FixedArray inlined = {1, 2, 3}; - EXPECT_EQ(inlined.front(), 1); - EXPECT_EQ(inlined.back(), 3); - - absl::FixedArray allocated = {1, 2, 3}; - EXPECT_EQ(allocated.front(), 1); - EXPECT_EQ(allocated.back(), 3); - - absl::FixedArray one_element = {1}; - EXPECT_EQ(one_element.front(), one_element.back()); -} - -TEST(FixedArrayTest, ReverseIteratorInlined) { - absl::FixedArray a = {0, 1, 2, 3, 4}; - - int counter = 5; - for (absl::FixedArray::reverse_iterator iter = a.rbegin(); - iter != a.rend(); ++iter) { - counter--; - EXPECT_EQ(counter, *iter); - } - EXPECT_EQ(counter, 0); - - counter = 5; - for (absl::FixedArray::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 a = {0, 1, 2, 3, 4}; - - int counter = 5; - for (absl::FixedArray::reverse_iterator iter = a.rbegin(); - iter != a.rend(); ++iter) { - counter--; - EXPECT_EQ(counter, *iter); - } - EXPECT_EQ(counter, 0); - - counter = 5; - for (absl::FixedArray::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 inlined(5); - int fill_val = 42; - inlined.fill(fill_val); - for (int i : inlined) EXPECT_EQ(i, fill_val); - - absl::FixedArray 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 empty(0); - empty.fill(fill_val); -} - -#ifndef __GNUC__ -TEST(FixedArrayTest, DefaultCtorDoesNotValueInit) { - using T = char; - constexpr auto capacity = 10; - using FixedArrType = absl::FixedArray; - 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(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; - using AllocFxdArr = absl::FixedArray; - - 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(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; - using AllocFxdArr = absl::FixedArray; - - 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(arr); - } - - EXPECT_EQ(active_instances, 0); -} - -TEST(AllocatorSupportTest, CountCopyInlineAllocations) { - constexpr size_t inlined_size = 4; - using Alloc = absl::container_internal::CountingAllocator; - using AllocFxdArr = absl::FixedArray; - - 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(arr1); - static_cast(arr2); - } - - EXPECT_EQ(active_instances, 0); -} - -TEST(AllocatorSupportTest, CountCopyOutoflineAllocations) { - constexpr size_t inlined_size = 4; - using Alloc = absl::container_internal::CountingAllocator; - using AllocFxdArr = absl::FixedArray; - - 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(arr1); - static_cast(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; - using AllocFxdArr = absl::FixedArray; - - { - 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))); - } -} - -#ifdef ABSL_HAVE_ADDRESS_SANITIZER -TEST(FixedArrayTest, AddressSanitizerAnnotations1) { - absl::FixedArray 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 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 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 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; - std::vector 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 diff --git a/third_party/abseil_cpp/absl/container/flat_hash_map.h b/third_party/abseil_cpp/absl/container/flat_hash_map.h deleted file mode 100644 index 74def0df0e33..000000000000 --- a/third_party/abseil_cpp/absl/container/flat_hash_map.h +++ /dev/null @@ -1,606 +0,0 @@ -// 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` 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`. - -#ifndef ABSL_CONTAINER_FLAT_HASH_MAP_H_ -#define ABSL_CONTAINER_FLAT_HASH_MAP_H_ - -#include -#include -#include -#include - -#include "absl/algorithm/container.h" -#include "absl/container/internal/container_memory.h" -#include "absl/container/internal/hash_function_defaults.h" // IWYU pragma: export -#include "absl/container/internal/raw_hash_map.h" // IWYU pragma: export -#include "absl/memory/memory.h" - -namespace absl { -ABSL_NAMESPACE_BEGIN -namespace container_internal { -template -struct FlatHashMapPolicy; -} // namespace container_internal - -// ----------------------------------------------------------------------------- -// absl::flat_hash_map -// ----------------------------------------------------------------------------- -// -// An `absl::flat_hash_map` 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` 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. -// * Invalidates any references and pointers to elements within the table after -// `rehash()`. -// * 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. -// -// 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>` 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 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 Eq = absl::container_internal::hash_default_eq, - class Allocator = std::allocator>> -class flat_hash_map : public absl::container_internal::raw_hash_map< - absl::container_internal::FlatHashMapPolicy, - 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 map1; - // - // * Initializer List constructor - // - // absl::flat_hash_map map2 = - // {{1, "huey"}, {2, "dewey"}, {3, "louie"},}; - // - // * Copy constructor - // - // absl::flat_hash_map map3(map2); - // - // * Copy assignment operator - // - // // Hash functor and Comparator are copied as well - // absl::flat_hash_map map4; - // map4 = map3; - // - // * Move constructor - // - // // Move is guaranteed efficient - // absl::flat_hash_map map5(std::move(map4)); - // - // * Move assignment operator - // - // // May be efficient if allocators are compatible - // absl::flat_hash_map map6; - // map6 = std::move(map5); - // - // * Range constructor - // - // std::vector> v = {{1, "a"}, {2, "b"}}; - // absl::flat_hash_map 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`. - 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`. - // - // 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 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 insert(T&& value): - // std::pair 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 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 insert_or_assign(const init_type& k, T&& obj): - // pair 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 try_emplace(const key_type& k, Args&&... args): - // pair 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 init_type& k, Args&&... args): - // iterator try_emplace(const_iterator hint, init_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, avoiding invocation of any move, copy, or swap operations on - // individual elements. - // - // 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::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`. -template -void erase_if(flat_hash_map& c, Predicate pred) { - container_internal::EraseIf(pred, &c); -} - -namespace container_internal { - -template -struct FlatHashMapPolicy { - using slot_policy = container_internal::map_slot_policy; - using slot_type = typename slot_policy::slot_type; - using key_type = K; - using mapped_type = V; - using init_type = std::pair; - - template - static void construct(Allocator* alloc, slot_type* slot, Args&&... args) { - slot_policy::construct(alloc, slot, std::forward(args)...); - } - - template - static void destroy(Allocator* alloc, slot_type* slot) { - slot_policy::destroy(alloc, slot); - } - - template - static void transfer(Allocator* alloc, slot_type* new_slot, - slot_type* old_slot) { - slot_policy::transfer(alloc, new_slot, old_slot); - } - - template - static decltype(absl::container_internal::DecomposePair( - std::declval(), std::declval()...)) - apply(F&& f, Args&&... args) { - return absl::container_internal::DecomposePair(std::forward(f), - std::forward(args)...); - } - - static size_t space_used(const slot_type*) { return 0; } - - static std::pair& element(slot_type* slot) { return slot->value; } - - static V& value(std::pair* kv) { return kv->second; } - static const V& value(const std::pair* kv) { return kv->second; } -}; - -} // namespace container_internal - -namespace container_algorithm_internal { - -// Specialization of trait in absl/algorithm/container.h -template -struct IsUnorderedContainer< - absl::flat_hash_map> : std::true_type {}; - -} // namespace container_algorithm_internal - -ABSL_NAMESPACE_END -} // namespace absl - -#endif // ABSL_CONTAINER_FLAT_HASH_MAP_H_ diff --git a/third_party/abseil_cpp/absl/container/flat_hash_map_test.cc b/third_party/abseil_cpp/absl/container/flat_hash_map_test.cc deleted file mode 100644 index 89ec60c916ed..000000000000 --- a/third_party/abseil_cpp/absl/container/flat_hash_map_test.cc +++ /dev/null @@ -1,288 +0,0 @@ -// 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 - -#include "absl/base/internal/raw_logging.h" -#include "absl/container/internal/hash_generator_testing.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/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; - -// Check that absl::flat_hash_map works in a global constructor. -struct BeforeMain { - BeforeMain() { - absl::flat_hash_map x; - x.insert({1, 1}); - ABSL_RAW_CHECK(x.find(0) == x.end(), "x should not contain 0"); - auto it = x.find(1); - ABSL_RAW_CHECK(it != x.end(), "x should contain 1"); - ABSL_RAW_CHECK(it->second, "1 should map to 1"); - } -}; -const BeforeMain before_main; - -template -using Map = flat_hash_map>>; - -static_assert(!std::is_standard_layout(), ""); - -using MapTypes = - ::testing::Types, Map, - Map, Map, - Map, Map>; - -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>>; - -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(), ""); - - 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 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 m; - m.try_emplace(Int(1), Int(2)); - m.try_emplace(Int(3), Int(4)); - m.erase(Int(1)); - m.clear(); - } -} - -// 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> garbage; - for (int i = 0; i < 100; ++i) { - absl::flat_hash_map 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 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 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 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 p) { return p.first % 2 == 0; } - -TEST(FlatHashMap, EraseIf) { - // Erase all elements. - { - flat_hash_map s = {{1, 1}, {2, 2}, {3, 3}, {4, 4}, {5, 5}}; - erase_if(s, [](std::pair) { return true; }); - EXPECT_THAT(s, IsEmpty()); - } - // Erase no elements. - { - flat_hash_map s = {{1, 1}, {2, 2}, {3, 3}, {4, 4}, {5, 5}}; - erase_if(s, [](std::pair) { return false; }); - 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 s = {{1, 1}, {2, 2}, {3, 3}, {4, 4}, {5, 5}}; - erase_if(s, - [](std::pair kvp) { return kvp.first % 2 == 1; }); - EXPECT_THAT(s, UnorderedElementsAre(Pair(2, 2), Pair(4, 4))); - } - // Predicate is function reference. - { - flat_hash_map s = {{1, 1}, {2, 2}, {3, 3}, {4, 4}, {5, 5}}; - erase_if(s, FirstIsEven); - EXPECT_THAT(s, UnorderedElementsAre(Pair(1, 1), Pair(3, 3), Pair(5, 5))); - } - // Predicate is function pointer. - { - flat_hash_map s = {{1, 1}, {2, 2}, {3, 3}, {4, 4}, {5, 5}}; - erase_if(s, &FirstIsEven); - EXPECT_THAT(s, UnorderedElementsAre(Pair(1, 1), Pair(3, 3), Pair(5, 5))); - } -} - -// 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 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 - -} // namespace -} // namespace container_internal -ABSL_NAMESPACE_END -} // namespace absl diff --git a/third_party/abseil_cpp/absl/container/flat_hash_set.h b/third_party/abseil_cpp/absl/container/flat_hash_set.h deleted file mode 100644 index 6b89da65714c..000000000000 --- a/third_party/abseil_cpp/absl/container/flat_hash_set.h +++ /dev/null @@ -1,504 +0,0 @@ -// 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` 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`. -#ifndef ABSL_CONTAINER_FLAT_HASH_SET_H_ -#define ABSL_CONTAINER_FLAT_HASH_SET_H_ - -#include -#include - -#include "absl/algorithm/container.h" -#include "absl/base/macros.h" -#include "absl/container/internal/container_memory.h" -#include "absl/container/internal/hash_function_defaults.h" // IWYU pragma: export -#include "absl/container/internal/raw_hash_set.h" // IWYU pragma: export -#include "absl/memory/memory.h" - -namespace absl { -ABSL_NAMESPACE_BEGIN -namespace container_internal { -template -struct FlatHashSetPolicy; -} // namespace container_internal - -// ----------------------------------------------------------------------------- -// absl::flat_hash_set -// ----------------------------------------------------------------------------- -// -// An `absl::flat_hash_set` 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` 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. -// * Invalidates any references and pointers to elements within the table after -// `rehash()`. -// * 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_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. -// -// 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>`. 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 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 Eq = absl::container_internal::hash_default_eq, - class Allocator = std::allocator> -class flat_hash_set - : public absl::container_internal::raw_hash_set< - absl::container_internal::FlatHashSetPolicy, 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_map` - // for construction and assignment: - // - // * Default constructor - // - // // No allocation for the table's elements is made. - // absl::flat_hash_set set1; - // - // * Initializer List constructor - // - // absl::flat_hash_set set2 = - // {{"huey"}, {"dewey"}, {"louie"},}; - // - // * Copy constructor - // - // absl::flat_hash_set set3(set2); - // - // * Copy assignment operator - // - // // Hash functor and Comparator are copied as well - // absl::flat_hash_set set4; - // set4 = set3; - // - // * Move constructor - // - // // Move is guaranteed efficient - // absl::flat_hash_set set5(std::move(set4)); - // - // * Move assignment operator - // - // // May be efficient if allocators are compatible - // absl::flat_hash_set set6; - // set6 = std::move(set5); - // - // * Range constructor - // - // std::vector v = {"a", "b"}; - // absl::flat_hash_set 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_map` 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`. - 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`. - // - // 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 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 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 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 map, avoiding invocation of any move, copy, or swap operations on - // individual elements. - // - // 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 exchaged using unqualified calls to - // non-member `swap()`. If the map's allocator has - // `std::allocator_traits::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 map 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 map). - 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`. -template -void erase_if(flat_hash_set& c, Predicate pred) { - container_internal::EraseIf(pred, &c); -} - -namespace container_internal { - -template -struct FlatHashSetPolicy { - using slot_type = T; - using key_type = T; - using init_type = T; - using constant_iterators = std::true_type; - - template - static void construct(Allocator* alloc, slot_type* slot, Args&&... args) { - absl::allocator_traits::construct(*alloc, slot, - std::forward(args)...); - } - - template - static void destroy(Allocator* alloc, slot_type* slot) { - absl::allocator_traits::destroy(*alloc, slot); - } - - template - static void transfer(Allocator* alloc, slot_type* new_slot, - slot_type* old_slot) { - construct(alloc, new_slot, std::move(*old_slot)); - destroy(alloc, old_slot); - } - - static T& element(slot_type* slot) { return *slot; } - - template - static decltype(absl::container_internal::DecomposeValue( - std::declval(), std::declval()...)) - apply(F&& f, Args&&... args) { - return absl::container_internal::DecomposeValue( - std::forward(f), std::forward(args)...); - } - - static size_t space_used(const T*) { return 0; } -}; -} // namespace container_internal - -namespace container_algorithm_internal { - -// Specialization of trait in absl/algorithm/container.h -template -struct IsUnorderedContainer> - : std::true_type {}; - -} // namespace container_algorithm_internal - -ABSL_NAMESPACE_END -} // namespace absl - -#endif // ABSL_CONTAINER_FLAT_HASH_SET_H_ diff --git a/third_party/abseil_cpp/absl/container/flat_hash_set_test.cc b/third_party/abseil_cpp/absl/container/flat_hash_set_test.cc deleted file mode 100644 index 8f6f9944ca4e..000000000000 --- a/third_party/abseil_cpp/absl/container/flat_hash_set_test.cc +++ /dev/null @@ -1,178 +0,0 @@ -// 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 - -#include "absl/base/internal/raw_logging.h" -#include "absl/container/internal/hash_generator_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" -#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 x; - x.insert(1); - ABSL_RAW_CHECK(!x.contains(0), "x should not contain 0"); - ABSL_RAW_CHECK(x.contains(1), "x should contain 1"); - } -}; -const BeforeMain before_main; - -template -using Set = - absl::flat_hash_set>; - -using SetTypes = - ::testing::Types, Set, Set, Set>; - -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 v = {"a", "b"}; - absl::flat_hash_set hs(v.begin(), v.end()); - EXPECT_THAT(hs, UnorderedElementsAreArray(v)); -} - -TEST(FlatHashSet, BitfieldArgument) { - union { - int n : 1; - }; - n = 0; - absl::flat_hash_set 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& p) const { return *p; } - }; - struct Eq { - bool operator()(const std::unique_ptr& a, - const std::unique_ptr& b) const { - return *a == *b; - } - }; - absl::flat_hash_set, Hash, Eq> set1, set2; - set1.insert(absl::make_unique(7)); - set1.insert(absl::make_unique(17)); - - set2.insert(absl::make_unique(7)); - set2.insert(absl::make_unique(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(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(17)); - EXPECT_TRUE(node); - EXPECT_THAT(node.value(), Pointee(17)); - EXPECT_THAT(set1, UnorderedElementsAre(Pointee(19))); - - node.value() = absl::make_unique(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 s = {1, 2, 3, 4, 5}; - erase_if(s, [](int) { return true; }); - EXPECT_THAT(s, IsEmpty()); - } - // Erase no elements. - { - flat_hash_set s = {1, 2, 3, 4, 5}; - erase_if(s, [](int) { return false; }); - EXPECT_THAT(s, UnorderedElementsAre(1, 2, 3, 4, 5)); - } - // Erase specific elements. - { - flat_hash_set s = {1, 2, 3, 4, 5}; - erase_if(s, [](int k) { return k % 2 == 1; }); - EXPECT_THAT(s, UnorderedElementsAre(2, 4)); - } - // Predicate is function reference. - { - flat_hash_set s = {1, 2, 3, 4, 5}; - erase_if(s, IsEven); - EXPECT_THAT(s, UnorderedElementsAre(1, 3, 5)); - } - // Predicate is function pointer. - { - flat_hash_set s = {1, 2, 3, 4, 5}; - erase_if(s, &IsEven); - EXPECT_THAT(s, UnorderedElementsAre(1, 3, 5)); - } -} - -} // namespace -} // namespace container_internal -ABSL_NAMESPACE_END -} // namespace absl diff --git a/third_party/abseil_cpp/absl/container/inlined_vector.h b/third_party/abseil_cpp/absl/container/inlined_vector.h deleted file mode 100644 index 90bb96e8325c..000000000000 --- a/third_party/abseil_cpp/absl/container/inlined_vector.h +++ /dev/null @@ -1,845 +0,0 @@ -// 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. -// -// ----------------------------------------------------------------------------- -// File: inlined_vector.h -// ----------------------------------------------------------------------------- -// -// This header file contains the declaration and definition of an "inlined -// vector" which behaves in an equivalent fashion to a `std::vector`, except -// that storage for small sequences of the vector are provided inline without -// requiring any heap allocation. -// -// An `absl::InlinedVector` specifies the default capacity `N` as one of -// its template parameters. Instances where `size() <= N` hold contained -// elements in inline space. Typically `N` is very small so that sequences that -// are expected to be short do not require allocations. -// -// An `absl::InlinedVector` does not usually require a specific allocator. If -// the inlined vector grows beyond its initial constraints, it will need to -// allocate (as any normal `std::vector` would). This is usually performed with -// the default allocator (defined as `std::allocator`). Optionally, a custom -// allocator type may be specified as `A` in `absl::InlinedVector`. - -#ifndef ABSL_CONTAINER_INLINED_VECTOR_H_ -#define ABSL_CONTAINER_INLINED_VECTOR_H_ - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include "absl/algorithm/algorithm.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/inlined_vector.h" -#include "absl/memory/memory.h" - -namespace absl { -ABSL_NAMESPACE_BEGIN -// ----------------------------------------------------------------------------- -// InlinedVector -// ----------------------------------------------------------------------------- -// -// An `absl::InlinedVector` is designed to be a drop-in replacement for -// `std::vector` for use cases where the vector's size is sufficiently small -// that it can be inlined. If the inlined vector does grow beyond its estimated -// capacity, it will trigger an initial allocation on the heap, and will behave -// as a `std::vector`. The API of the `absl::InlinedVector` within this file is -// designed to cover the same API footprint as covered by `std::vector`. -template > -class InlinedVector { - static_assert(N > 0, "`absl::InlinedVector` requires an inlined capacity."); - - using Storage = inlined_vector_internal::Storage; - - using AllocatorTraits = typename Storage::AllocatorTraits; - using RValueReference = typename Storage::RValueReference; - using MoveIterator = typename Storage::MoveIterator; - using IsMemcpyOk = typename Storage::IsMemcpyOk; - - template - using IteratorValueAdapter = - typename Storage::template IteratorValueAdapter; - using CopyValueAdapter = typename Storage::CopyValueAdapter; - using DefaultValueAdapter = typename Storage::DefaultValueAdapter; - - template - using EnableIfAtLeastForwardIterator = absl::enable_if_t< - inlined_vector_internal::IsAtLeastForwardIterator::value>; - template - using DisableIfAtLeastForwardIterator = absl::enable_if_t< - !inlined_vector_internal::IsAtLeastForwardIterator::value>; - - public: - using allocator_type = typename Storage::allocator_type; - using value_type = typename Storage::value_type; - using pointer = typename Storage::pointer; - using const_pointer = typename Storage::const_pointer; - using size_type = typename Storage::size_type; - using difference_type = typename Storage::difference_type; - using reference = typename Storage::reference; - using const_reference = typename Storage::const_reference; - using iterator = typename Storage::iterator; - using const_iterator = typename Storage::const_iterator; - using reverse_iterator = typename Storage::reverse_iterator; - using const_reverse_iterator = typename Storage::const_reverse_iterator; - - // --------------------------------------------------------------------------- - // InlinedVector Constructors and Destructor - // --------------------------------------------------------------------------- - - // Creates an empty inlined vector with a value-initialized allocator. - InlinedVector() noexcept(noexcept(allocator_type())) : storage_() {} - - // Creates an empty inlined vector with a copy of `alloc`. - explicit InlinedVector(const allocator_type& alloc) noexcept - : storage_(alloc) {} - - // Creates an inlined vector with `n` copies of `value_type()`. - explicit InlinedVector(size_type n, - const allocator_type& alloc = allocator_type()) - : storage_(alloc) { - storage_.Initialize(DefaultValueAdapter(), n); - } - - // Creates an inlined vector with `n` copies of `v`. - InlinedVector(size_type n, const_reference v, - const allocator_type& alloc = allocator_type()) - : storage_(alloc) { - storage_.Initialize(CopyValueAdapter(v), n); - } - - // Creates an inlined vector with copies of the elements of `list`. - InlinedVector(std::initializer_list list, - const allocator_type& alloc = allocator_type()) - : InlinedVector(list.begin(), list.end(), alloc) {} - - // Creates an inlined vector with elements constructed from the provided - // forward iterator range [`first`, `last`). - // - // NOTE: the `enable_if` prevents ambiguous interpretation between a call to - // this constructor with two integral arguments and a call to the above - // `InlinedVector(size_type, const_reference)` constructor. - template * = nullptr> - InlinedVector(ForwardIterator first, ForwardIterator last, - const allocator_type& alloc = allocator_type()) - : storage_(alloc) { - storage_.Initialize(IteratorValueAdapter(first), - std::distance(first, last)); - } - - // Creates an inlined vector with elements constructed from the provided input - // iterator range [`first`, `last`). - template * = nullptr> - InlinedVector(InputIterator first, InputIterator last, - const allocator_type& alloc = allocator_type()) - : storage_(alloc) { - std::copy(first, last, std::back_inserter(*this)); - } - - // Creates an inlined vector by copying the contents of `other` using - // `other`'s allocator. - InlinedVector(const InlinedVector& other) - : InlinedVector(other, *other.storage_.GetAllocPtr()) {} - - // Creates an inlined vector by copying the contents of `other` using `alloc`. - InlinedVector(const InlinedVector& other, const allocator_type& alloc) - : storage_(alloc) { - if (IsMemcpyOk::value && !other.storage_.GetIsAllocated()) { - storage_.MemcpyFrom(other.storage_); - } else { - storage_.Initialize(IteratorValueAdapter(other.data()), - other.size()); - } - } - - // Creates an inlined vector by moving in the contents of `other` without - // allocating. If `other` contains allocated memory, the newly-created inlined - // vector will take ownership of that memory. However, if `other` does not - // contain allocated memory, the newly-created inlined vector will perform - // element-wise move construction of the contents of `other`. - // - // NOTE: since no allocation is performed for the inlined vector in either - // case, the `noexcept(...)` specification depends on whether moving the - // underlying objects can throw. It is assumed assumed that... - // a) move constructors should only throw due to allocation failure. - // b) if `value_type`'s move constructor allocates, it uses the same - // allocation function as the inlined vector's allocator. - // Thus, the move constructor is non-throwing if the allocator is non-throwing - // or `value_type`'s move constructor is specified as `noexcept`. - InlinedVector(InlinedVector&& other) noexcept( - absl::allocator_is_nothrow::value || - std::is_nothrow_move_constructible::value) - : storage_(*other.storage_.GetAllocPtr()) { - if (IsMemcpyOk::value) { - storage_.MemcpyFrom(other.storage_); - - other.storage_.SetInlinedSize(0); - } else if (other.storage_.GetIsAllocated()) { - storage_.SetAllocatedData(other.storage_.GetAllocatedData(), - other.storage_.GetAllocatedCapacity()); - storage_.SetAllocatedSize(other.storage_.GetSize()); - - other.storage_.SetInlinedSize(0); - } else { - IteratorValueAdapter other_values( - MoveIterator(other.storage_.GetInlinedData())); - - inlined_vector_internal::ConstructElements( - storage_.GetAllocPtr(), storage_.GetInlinedData(), &other_values, - other.storage_.GetSize()); - - storage_.SetInlinedSize(other.storage_.GetSize()); - } - } - - // Creates an inlined vector by moving in the contents of `other` with a copy - // of `alloc`. - // - // NOTE: if `other`'s allocator is not equal to `alloc`, even if `other` - // contains allocated memory, this move constructor will still allocate. Since - // allocation is performed, this constructor can only be `noexcept` if the - // specified allocator is also `noexcept`. - InlinedVector(InlinedVector&& other, const allocator_type& alloc) noexcept( - absl::allocator_is_nothrow::value) - : storage_(alloc) { - if (IsMemcpyOk::value) { - storage_.MemcpyFrom(other.storage_); - - other.storage_.SetInlinedSize(0); - } else if ((*storage_.GetAllocPtr() == *other.storage_.GetAllocPtr()) && - other.storage_.GetIsAllocated()) { - storage_.SetAllocatedData(other.storage_.GetAllocatedData(), - other.storage_.GetAllocatedCapacity()); - storage_.SetAllocatedSize(other.storage_.GetSize()); - - other.storage_.SetInlinedSize(0); - } else { - storage_.Initialize( - IteratorValueAdapter(MoveIterator(other.data())), - other.size()); - } - } - - ~InlinedVector() {} - - // --------------------------------------------------------------------------- - // InlinedVector Member Accessors - // --------------------------------------------------------------------------- - - // `InlinedVector::empty()` - // - // Returns whether the inlined vector contains no elements. - bool empty() const noexcept { return !size(); } - - // `InlinedVector::size()` - // - // Returns the number of elements in the inlined vector. - size_type size() const noexcept { return storage_.GetSize(); } - - // `InlinedVector::max_size()` - // - // Returns the maximum number of elements the inlined vector can hold. - size_type max_size() const noexcept { - // One bit of the size storage is used to indicate whether the inlined - // vector contains allocated memory. As a result, the maximum size that the - // inlined vector can express is half of the max for `size_type`. - return (std::numeric_limits::max)() / 2; - } - - // `InlinedVector::capacity()` - // - // Returns the number of elements that could be stored in the inlined vector - // without requiring a reallocation. - // - // NOTE: for most inlined vectors, `capacity()` should be equal to the - // template parameter `N`. For inlined vectors which exceed this capacity, - // they will no longer be inlined and `capacity()` will equal the capactity of - // the allocated memory. - size_type capacity() const noexcept { - return storage_.GetIsAllocated() ? storage_.GetAllocatedCapacity() - : storage_.GetInlinedCapacity(); - } - - // `InlinedVector::data()` - // - // Returns a `pointer` to the elements of the inlined vector. This pointer - // can be used to access and modify the contained elements. - // - // NOTE: only elements within [`data()`, `data() + size()`) are valid. - pointer data() noexcept { - return storage_.GetIsAllocated() ? storage_.GetAllocatedData() - : storage_.GetInlinedData(); - } - - // Overload of `InlinedVector::data()` that returns a `const_pointer` to the - // elements of the inlined vector. This pointer can be used to access but not - // modify the contained elements. - // - // NOTE: only elements within [`data()`, `data() + size()`) are valid. - const_pointer data() const noexcept { - return storage_.GetIsAllocated() ? storage_.GetAllocatedData() - : storage_.GetInlinedData(); - } - - // `InlinedVector::operator[](...)` - // - // Returns a `reference` to the `i`th element of the inlined vector. - reference operator[](size_type i) { - ABSL_HARDENING_ASSERT(i < size()); - return data()[i]; - } - - // Overload of `InlinedVector::operator[](...)` that returns a - // `const_reference` to the `i`th element of the inlined vector. - const_reference operator[](size_type i) const { - ABSL_HARDENING_ASSERT(i < size()); - return data()[i]; - } - - // `InlinedVector::at(...)` - // - // Returns a `reference` to the `i`th element of the inlined vector. - // - // NOTE: if `i` is not within the required range of `InlinedVector::at(...)`, - // in both debug and non-debug builds, `std::out_of_range` will be thrown. - reference at(size_type i) { - if (ABSL_PREDICT_FALSE(i >= size())) { - base_internal::ThrowStdOutOfRange( - "`InlinedVector::at(size_type)` failed bounds check"); - } - return data()[i]; - } - - // Overload of `InlinedVector::at(...)` that returns a `const_reference` to - // the `i`th element of the inlined vector. - // - // NOTE: if `i` is not within the required range of `InlinedVector::at(...)`, - // in both debug and non-debug builds, `std::out_of_range` will be thrown. - const_reference at(size_type i) const { - if (ABSL_PREDICT_FALSE(i >= size())) { - base_internal::ThrowStdOutOfRange( - "`InlinedVector::at(size_type) const` failed bounds check"); - } - return data()[i]; - } - - // `InlinedVector::front()` - // - // Returns a `reference` to the first element of the inlined vector. - reference front() { - ABSL_HARDENING_ASSERT(!empty()); - return data()[0]; - } - - // Overload of `InlinedVector::front()` that returns a `const_reference` to - // the first element of the inlined vector. - const_reference front() const { - ABSL_HARDENING_ASSERT(!empty()); - return data()[0]; - } - - // `InlinedVector::back()` - // - // Returns a `reference` to the last element of the inlined vector. - reference back() { - ABSL_HARDENING_ASSERT(!empty()); - return data()[size() - 1]; - } - - // Overload of `InlinedVector::back()` that returns a `const_reference` to the - // last element of the inlined vector. - const_reference back() const { - ABSL_HARDENING_ASSERT(!empty()); - return data()[size() - 1]; - } - - // `InlinedVector::begin()` - // - // Returns an `iterator` to the beginning of the inlined vector. - iterator begin() noexcept { return data(); } - - // Overload of `InlinedVector::begin()` that returns a `const_iterator` to - // the beginning of the inlined vector. - const_iterator begin() const noexcept { return data(); } - - // `InlinedVector::end()` - // - // Returns an `iterator` to the end of the inlined vector. - iterator end() noexcept { return data() + size(); } - - // Overload of `InlinedVector::end()` that returns a `const_iterator` to the - // end of the inlined vector. - const_iterator end() const noexcept { return data() + size(); } - - // `InlinedVector::cbegin()` - // - // Returns a `const_iterator` to the beginning of the inlined vector. - const_iterator cbegin() const noexcept { return begin(); } - - // `InlinedVector::cend()` - // - // Returns a `const_iterator` to the end of the inlined vector. - const_iterator cend() const noexcept { return end(); } - - // `InlinedVector::rbegin()` - // - // Returns a `reverse_iterator` from the end of the inlined vector. - reverse_iterator rbegin() noexcept { return reverse_iterator(end()); } - - // Overload of `InlinedVector::rbegin()` that returns a - // `const_reverse_iterator` from the end of the inlined vector. - const_reverse_iterator rbegin() const noexcept { - return const_reverse_iterator(end()); - } - - // `InlinedVector::rend()` - // - // Returns a `reverse_iterator` from the beginning of the inlined vector. - reverse_iterator rend() noexcept { return reverse_iterator(begin()); } - - // Overload of `InlinedVector::rend()` that returns a `const_reverse_iterator` - // from the beginning of the inlined vector. - const_reverse_iterator rend() const noexcept { - return const_reverse_iterator(begin()); - } - - // `InlinedVector::crbegin()` - // - // Returns a `const_reverse_iterator` from the end of the inlined vector. - const_reverse_iterator crbegin() const noexcept { return rbegin(); } - - // `InlinedVector::crend()` - // - // Returns a `const_reverse_iterator` from the beginning of the inlined - // vector. - const_reverse_iterator crend() const noexcept { return rend(); } - - // `InlinedVector::get_allocator()` - // - // Returns a copy of the inlined vector's allocator. - allocator_type get_allocator() const { return *storage_.GetAllocPtr(); } - - // --------------------------------------------------------------------------- - // InlinedVector Member Mutators - // --------------------------------------------------------------------------- - - // `InlinedVector::operator=(...)` - // - // Replaces the elements of the inlined vector with copies of the elements of - // `list`. - InlinedVector& operator=(std::initializer_list list) { - assign(list.begin(), list.end()); - - return *this; - } - - // Overload of `InlinedVector::operator=(...)` that replaces the elements of - // the inlined vector with copies of the elements of `other`. - InlinedVector& operator=(const InlinedVector& other) { - if (ABSL_PREDICT_TRUE(this != std::addressof(other))) { - const_pointer other_data = other.data(); - assign(other_data, other_data + other.size()); - } - - return *this; - } - - // Overload of `InlinedVector::operator=(...)` that moves the elements of - // `other` into the inlined vector. - // - // NOTE: as a result of calling this overload, `other` is left in a valid but - // unspecified state. - InlinedVector& operator=(InlinedVector&& other) { - if (ABSL_PREDICT_TRUE(this != std::addressof(other))) { - if (IsMemcpyOk::value || other.storage_.GetIsAllocated()) { - inlined_vector_internal::DestroyElements(storage_.GetAllocPtr(), data(), - size()); - storage_.DeallocateIfAllocated(); - storage_.MemcpyFrom(other.storage_); - - other.storage_.SetInlinedSize(0); - } else { - storage_.Assign(IteratorValueAdapter( - MoveIterator(other.storage_.GetInlinedData())), - other.size()); - } - } - - return *this; - } - - // `InlinedVector::assign(...)` - // - // Replaces the contents of the inlined vector with `n` copies of `v`. - void assign(size_type n, const_reference v) { - storage_.Assign(CopyValueAdapter(v), n); - } - - // Overload of `InlinedVector::assign(...)` that replaces the contents of the - // inlined vector with copies of the elements of `list`. - void assign(std::initializer_list list) { - assign(list.begin(), list.end()); - } - - // Overload of `InlinedVector::assign(...)` to replace the contents of the - // inlined vector with the range [`first`, `last`). - // - // NOTE: this overload is for iterators that are "forward" category or better. - template * = nullptr> - void assign(ForwardIterator first, ForwardIterator last) { - storage_.Assign(IteratorValueAdapter(first), - std::distance(first, last)); - } - - // Overload of `InlinedVector::assign(...)` to replace the contents of the - // inlined vector with the range [`first`, `last`). - // - // NOTE: this overload is for iterators that are "input" category. - template * = nullptr> - void assign(InputIterator first, InputIterator last) { - size_type i = 0; - for (; i < size() && first != last; ++i, static_cast(++first)) { - data()[i] = *first; - } - - erase(data() + i, data() + size()); - std::copy(first, last, std::back_inserter(*this)); - } - - // `InlinedVector::resize(...)` - // - // Resizes the inlined vector to contain `n` elements. - // - // NOTE: If `n` is smaller than `size()`, extra elements are destroyed. If `n` - // is larger than `size()`, new elements are value-initialized. - void resize(size_type n) { - ABSL_HARDENING_ASSERT(n <= max_size()); - storage_.Resize(DefaultValueAdapter(), n); - } - - // Overload of `InlinedVector::resize(...)` that resizes the inlined vector to - // contain `n` elements. - // - // NOTE: if `n` is smaller than `size()`, extra elements are destroyed. If `n` - // is larger than `size()`, new elements are copied-constructed from `v`. - void resize(size_type n, const_reference v) { - ABSL_HARDENING_ASSERT(n <= max_size()); - storage_.Resize(CopyValueAdapter(v), n); - } - - // `InlinedVector::insert(...)` - // - // Inserts a copy of `v` at `pos`, returning an `iterator` to the newly - // inserted element. - iterator insert(const_iterator pos, const_reference v) { - return emplace(pos, v); - } - - // Overload of `InlinedVector::insert(...)` that inserts `v` at `pos` using - // move semantics, returning an `iterator` to the newly inserted element. - iterator insert(const_iterator pos, RValueReference v) { - return emplace(pos, std::move(v)); - } - - // Overload of `InlinedVector::insert(...)` that inserts `n` contiguous copies - // of `v` starting at `pos`, returning an `iterator` pointing to the first of - // the newly inserted elements. - iterator insert(const_iterator pos, size_type n, const_reference v) { - ABSL_HARDENING_ASSERT(pos >= begin()); - ABSL_HARDENING_ASSERT(pos <= end()); - - if (ABSL_PREDICT_TRUE(n != 0)) { - value_type dealias = v; - return storage_.Insert(pos, CopyValueAdapter(dealias), n); - } else { - return const_cast(pos); - } - } - - // Overload of `InlinedVector::insert(...)` that inserts copies of the - // elements of `list` starting at `pos`, returning an `iterator` pointing to - // the first of the newly inserted elements. - iterator insert(const_iterator pos, std::initializer_list list) { - return insert(pos, list.begin(), list.end()); - } - - // Overload of `InlinedVector::insert(...)` that inserts the range [`first`, - // `last`) starting at `pos`, returning an `iterator` pointing to the first - // of the newly inserted elements. - // - // NOTE: this overload is for iterators that are "forward" category or better. - template * = nullptr> - iterator insert(const_iterator pos, ForwardIterator first, - ForwardIterator last) { - ABSL_HARDENING_ASSERT(pos >= begin()); - ABSL_HARDENING_ASSERT(pos <= end()); - - if (ABSL_PREDICT_TRUE(first != last)) { - return storage_.Insert(pos, IteratorValueAdapter(first), - std::distance(first, last)); - } else { - return const_cast(pos); - } - } - - // Overload of `InlinedVector::insert(...)` that inserts the range [`first`, - // `last`) starting at `pos`, returning an `iterator` pointing to the first - // of the newly inserted elements. - // - // NOTE: this overload is for iterators that are "input" category. - template * = nullptr> - iterator insert(const_iterator pos, InputIterator first, InputIterator last) { - ABSL_HARDENING_ASSERT(pos >= begin()); - ABSL_HARDENING_ASSERT(pos <= end()); - - size_type index = std::distance(cbegin(), pos); - for (size_type i = index; first != last; ++i, static_cast(++first)) { - insert(data() + i, *first); - } - - return iterator(data() + index); - } - - // `InlinedVector::emplace(...)` - // - // Constructs and inserts an element using `args...` in the inlined vector at - // `pos`, returning an `iterator` pointing to the newly emplaced element. - template - iterator emplace(const_iterator pos, Args&&... args) { - ABSL_HARDENING_ASSERT(pos >= begin()); - ABSL_HARDENING_ASSERT(pos <= end()); - - value_type dealias(std::forward(args)...); - return storage_.Insert(pos, - IteratorValueAdapter( - MoveIterator(std::addressof(dealias))), - 1); - } - - // `InlinedVector::emplace_back(...)` - // - // Constructs and inserts an element using `args...` in the inlined vector at - // `end()`, returning a `reference` to the newly emplaced element. - template - reference emplace_back(Args&&... args) { - return storage_.EmplaceBack(std::forward(args)...); - } - - // `InlinedVector::push_back(...)` - // - // Inserts a copy of `v` in the inlined vector at `end()`. - void push_back(const_reference v) { static_cast(emplace_back(v)); } - - // Overload of `InlinedVector::push_back(...)` for inserting `v` at `end()` - // using move semantics. - void push_back(RValueReference v) { - static_cast(emplace_back(std::move(v))); - } - - // `InlinedVector::pop_back()` - // - // Destroys the element at `back()`, reducing the size by `1`. - void pop_back() noexcept { - ABSL_HARDENING_ASSERT(!empty()); - - AllocatorTraits::destroy(*storage_.GetAllocPtr(), data() + (size() - 1)); - storage_.SubtractSize(1); - } - - // `InlinedVector::erase(...)` - // - // Erases the element at `pos`, returning an `iterator` pointing to where the - // erased element was located. - // - // NOTE: may return `end()`, which is not dereferencable. - iterator erase(const_iterator pos) { - ABSL_HARDENING_ASSERT(pos >= begin()); - ABSL_HARDENING_ASSERT(pos < end()); - - return storage_.Erase(pos, pos + 1); - } - - // Overload of `InlinedVector::erase(...)` that erases every element in the - // range [`from`, `to`), returning an `iterator` pointing to where the first - // erased element was located. - // - // NOTE: may return `end()`, which is not dereferencable. - iterator erase(const_iterator from, const_iterator to) { - ABSL_HARDENING_ASSERT(from >= begin()); - ABSL_HARDENING_ASSERT(from <= to); - ABSL_HARDENING_ASSERT(to <= end()); - - if (ABSL_PREDICT_TRUE(from != to)) { - return storage_.Erase(from, to); - } else { - return const_cast(from); - } - } - - // `InlinedVector::clear()` - // - // Destroys all elements in the inlined vector, setting the size to `0` and - // deallocating any held memory. - void clear() noexcept { - inlined_vector_internal::DestroyElements(storage_.GetAllocPtr(), data(), - size()); - storage_.DeallocateIfAllocated(); - - storage_.SetInlinedSize(0); - } - - // `InlinedVector::reserve(...)` - // - // Ensures that there is enough room for at least `n` elements. - void reserve(size_type n) { storage_.Reserve(n); } - - // `InlinedVector::shrink_to_fit()` - // - // Reduces memory usage by freeing unused memory. After being called, calls to - // `capacity()` will be equal to `max(N, size())`. - // - // If `size() <= N` and the inlined vector contains allocated memory, the - // elements will all be moved to the inlined space and the allocated memory - // will be deallocated. - // - // If `size() > N` and `size() < capacity()`, the elements will be moved to a - // smaller allocation. - void shrink_to_fit() { - if (storage_.GetIsAllocated()) { - storage_.ShrinkToFit(); - } - } - - // `InlinedVector::swap(...)` - // - // Swaps the contents of the inlined vector with `other`. - void swap(InlinedVector& other) { - if (ABSL_PREDICT_TRUE(this != std::addressof(other))) { - storage_.Swap(std::addressof(other.storage_)); - } - } - - private: - template - friend H AbslHashValue(H h, const absl::InlinedVector& a); - - Storage storage_; -}; - -// ----------------------------------------------------------------------------- -// InlinedVector Non-Member Functions -// ----------------------------------------------------------------------------- - -// `swap(...)` -// -// Swaps the contents of two inlined vectors. -template -void swap(absl::InlinedVector& a, - absl::InlinedVector& b) noexcept(noexcept(a.swap(b))) { - a.swap(b); -} - -// `operator==(...)` -// -// Tests for value-equality of two inlined vectors. -template -bool operator==(const absl::InlinedVector& a, - const absl::InlinedVector& b) { - auto a_data = a.data(); - auto b_data = b.data(); - return absl::equal(a_data, a_data + a.size(), b_data, b_data + b.size()); -} - -// `operator!=(...)` -// -// Tests for value-inequality of two inlined vectors. -template -bool operator!=(const absl::InlinedVector& a, - const absl::InlinedVector& b) { - return !(a == b); -} - -// `operator<(...)` -// -// Tests whether the value of an inlined vector is less than the value of -// another inlined vector using a lexicographical comparison algorithm. -template -bool operator<(const absl::InlinedVector& a, - const absl::InlinedVector& b) { - auto a_data = a.data(); - auto b_data = b.data(); - return std::lexicographical_compare(a_data, a_data + a.size(), b_data, - b_data + b.size()); -} - -// `operator>(...)` -// -// Tests whether the value of an inlined vector is greater than the value of -// another inlined vector using a lexicographical comparison algorithm. -template -bool operator>(const absl::InlinedVector& a, - const absl::InlinedVector& b) { - return b < a; -} - -// `operator<=(...)` -// -// Tests whether the value of an inlined vector is less than or equal to the -// value of another inlined vector using a lexicographical comparison algorithm. -template -bool operator<=(const absl::InlinedVector& a, - const absl::InlinedVector& b) { - return !(b < a); -} - -// `operator>=(...)` -// -// Tests whether the value of an inlined vector is greater than or equal to the -// value of another inlined vector using a lexicographical comparison algorithm. -template -bool operator>=(const absl::InlinedVector& a, - const absl::InlinedVector& b) { - return !(a < b); -} - -// `AbslHashValue(...)` -// -// Provides `absl::Hash` support for `absl::InlinedVector`. It is uncommon to -// call this directly. -template -H AbslHashValue(H h, const absl::InlinedVector& a) { - auto size = a.size(); - return H::combine(H::combine_contiguous(std::move(h), a.data(), size), size); -} - -ABSL_NAMESPACE_END -} // namespace absl - -#endif // ABSL_CONTAINER_INLINED_VECTOR_H_ diff --git a/third_party/abseil_cpp/absl/container/inlined_vector_benchmark.cc b/third_party/abseil_cpp/absl/container/inlined_vector_benchmark.cc deleted file mode 100644 index b8dafe932320..000000000000 --- a/third_party/abseil_cpp/absl/container/inlined_vector_benchmark.cc +++ /dev/null @@ -1,807 +0,0 @@ -// 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 -#include -#include - -#include "benchmark/benchmark.h" -#include "absl/base/internal/raw_logging.h" -#include "absl/base/macros.h" -#include "absl/container/inlined_vector.h" -#include "absl/strings/str_cat.h" - -namespace { - -void BM_InlinedVectorFill(benchmark::State& state) { - const int len = state.range(0); - absl::InlinedVector 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 src(len, len); - absl::InlinedVector 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 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 efficent 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 v; - for (int i = 0; i < len; i++) { - v.push_back(strings[i & 3]); - } - } - state.SetItemsProcessed(static_cast(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 v; - for (int i = 0; i < len; i++) { - v.push_back(strings[i & 3]); - } - } - state.SetItemsProcessed(static_cast(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; - - 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 src{1, 2, 3}; - benchmark::DoNotOptimize(src); - absl::InlinedVector 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 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 d; -}; - -struct LargeCopyableMovable { - LargeCopyableMovable() : d(1024, 17) {} - // Use implicitly defined copy and move. - - std::vector 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 d; -}; - -template -void BM_SwapElements(benchmark::State& state) { - const int len = state.range(0); - using Vec = absl::InlinedVector; - 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 -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); -BENCHMARK_TEMPLATE(BM_Sizeof, absl::InlinedVector); -BENCHMARK_TEMPLATE(BM_Sizeof, absl::InlinedVector); -BENCHMARK_TEMPLATE(BM_Sizeof, absl::InlinedVector); - -BENCHMARK_TEMPLATE(BM_Sizeof, absl::InlinedVector); -BENCHMARK_TEMPLATE(BM_Sizeof, absl::InlinedVector); -BENCHMARK_TEMPLATE(BM_Sizeof, absl::InlinedVector); -BENCHMARK_TEMPLATE(BM_Sizeof, absl::InlinedVector); - -BENCHMARK_TEMPLATE(BM_Sizeof, absl::InlinedVector); -BENCHMARK_TEMPLATE(BM_Sizeof, absl::InlinedVector); -BENCHMARK_TEMPLATE(BM_Sizeof, absl::InlinedVector); -BENCHMARK_TEMPLATE(BM_Sizeof, absl::InlinedVector); - -BENCHMARK_TEMPLATE(BM_Sizeof, absl::InlinedVector); -BENCHMARK_TEMPLATE(BM_Sizeof, absl::InlinedVector); -BENCHMARK_TEMPLATE(BM_Sizeof, absl::InlinedVector); -BENCHMARK_TEMPLATE(BM_Sizeof, absl::InlinedVector); - -void BM_InlinedVectorIndexInlined(benchmark::State& state) { - absl::InlinedVector 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 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 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 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 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(state.iterations())); -} -BENCHMARK(BM_InlinedVectorDataExternal); - -void BM_StdVectorData(benchmark::State& state) { - std::vector 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(state.iterations())); -} -BENCHMARK(BM_StdVectorData); - -void BM_InlinedVectorSizeInlined(benchmark::State& state) { - absl::InlinedVector 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 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 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 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 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 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 -using InlVec = absl::InlinedVector; - -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 -void BatchedBenchmark(benchmark::State& state, PrepareVecFn prepare_vec, - TestVecFn test_vec) { - std::array, 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 -void BM_ConstructFromSize(benchmark::State& state) { - using VecT = InlVec; - auto size = ToSize; - BatchedBenchmark( - state, - /* prepare_vec = */ [](InlVec* 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 -void BM_ConstructFromSizeRef(benchmark::State& state) { - using VecT = InlVec; - auto size = ToSize; - auto ref = T(); - BatchedBenchmark( - state, - /* prepare_vec = */ [](InlVec* 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 -void BM_ConstructFromRange(benchmark::State& state) { - using VecT = InlVec; - std::array arr{}; - BatchedBenchmark( - state, - /* prepare_vec = */ [](InlVec* 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 -void BM_ConstructFromCopy(benchmark::State& state) { - using VecT = InlVec; - VecT other_vec(ToSize); - BatchedBenchmark( - state, - /* prepare_vec = */ - [](InlVec* 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 -void BM_ConstructFromMove(benchmark::State& state) { - using VecT = InlVec; - std::array vector_batch{}; - BatchedBenchmark( - state, - /* prepare_vec = */ - [&](InlVec* 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); - -template -void BM_AssignSizeRef(benchmark::State& state) { - auto size = ToSize; - auto ref = T(); - BatchedBenchmark( - state, - /* prepare_vec = */ [](InlVec* vec, size_t) { vec->resize(FromSize); }, - /* test_vec = */ - [&](InlVec* 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 -void BM_AssignRange(benchmark::State& state) { - std::array arr{}; - BatchedBenchmark( - state, - /* prepare_vec = */ [](InlVec* vec, size_t) { vec->resize(FromSize); }, - /* test_vec = */ - [&](InlVec* 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 -void BM_AssignFromCopy(benchmark::State& state) { - InlVec other_vec(ToSize); - BatchedBenchmark( - state, - /* prepare_vec = */ [](InlVec* vec, size_t) { vec->resize(FromSize); }, - /* test_vec = */ - [&](InlVec* 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 -void BM_AssignFromMove(benchmark::State& state) { - using VecT = InlVec; - std::array vector_batch{}; - BatchedBenchmark( - state, - /* prepare_vec = */ - [&](InlVec* vec, size_t i) { - vector_batch[i].clear(); - vector_batch[i].resize(ToSize); - vec->resize(FromSize); - }, - /* test_vec = */ - [&](InlVec* 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 -void BM_ResizeSize(benchmark::State& state) { - BatchedBenchmark( - state, - /* prepare_vec = */ - [](InlVec* vec, size_t) { - vec->clear(); - vec->resize(FromSize); - }, - /* test_vec = */ - [](InlVec* vec, size_t) { vec->resize(ToSize); }); -} -ABSL_INTERNAL_BENCHMARK_TWO_SIZE(BM_ResizeSize, TrivialType); -ABSL_INTERNAL_BENCHMARK_TWO_SIZE(BM_ResizeSize, NontrivialType); - -template -void BM_ResizeSizeRef(benchmark::State& state) { - auto t = T(); - BatchedBenchmark( - state, - /* prepare_vec = */ - [](InlVec* vec, size_t) { - vec->clear(); - vec->resize(FromSize); - }, - /* test_vec = */ - [&](InlVec* 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 -void BM_InsertSizeRef(benchmark::State& state) { - auto t = T(); - BatchedBenchmark( - state, - /* prepare_vec = */ - [](InlVec* vec, size_t) { - vec->clear(); - vec->resize(FromSize); - }, - /* test_vec = */ - [&](InlVec* 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 -void BM_InsertRange(benchmark::State& state) { - InlVec other_vec(ToSize); - BatchedBenchmark( - state, - /* prepare_vec = */ - [](InlVec* vec, size_t) { - vec->clear(); - vec->resize(FromSize); - }, - /* test_vec = */ - [&](InlVec* 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 -void BM_EmplaceBack(benchmark::State& state) { - BatchedBenchmark( - state, - /* prepare_vec = */ - [](InlVec* vec, size_t) { - vec->clear(); - vec->resize(FromSize); - }, - /* test_vec = */ - [](InlVec* vec, size_t) { vec->emplace_back(); }); -} -ABSL_INTERNAL_BENCHMARK_ONE_SIZE(BM_EmplaceBack, TrivialType); -ABSL_INTERNAL_BENCHMARK_ONE_SIZE(BM_EmplaceBack, NontrivialType); - -template -void BM_PopBack(benchmark::State& state) { - BatchedBenchmark( - state, - /* prepare_vec = */ - [](InlVec* vec, size_t) { - vec->clear(); - vec->resize(FromSize); - }, - /* test_vec = */ - [](InlVec* vec, size_t) { vec->pop_back(); }); -} -ABSL_INTERNAL_BENCHMARK_ONE_SIZE(BM_PopBack, TrivialType); -ABSL_INTERNAL_BENCHMARK_ONE_SIZE(BM_PopBack, NontrivialType); - -template -void BM_EraseOne(benchmark::State& state) { - BatchedBenchmark( - state, - /* prepare_vec = */ - [](InlVec* vec, size_t) { - vec->clear(); - vec->resize(FromSize); - }, - /* test_vec = */ - [](InlVec* 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 -void BM_EraseRange(benchmark::State& state) { - BatchedBenchmark( - state, - /* prepare_vec = */ - [](InlVec* vec, size_t) { - vec->clear(); - vec->resize(FromSize); - }, - /* test_vec = */ - [](InlVec* 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 -void BM_Clear(benchmark::State& state) { - BatchedBenchmark( - state, - /* prepare_vec = */ [](InlVec* vec, size_t) { vec->resize(FromSize); }, - /* test_vec = */ [](InlVec* vec, size_t) { vec->clear(); }); -} -ABSL_INTERNAL_BENCHMARK_ONE_SIZE(BM_Clear, TrivialType); -ABSL_INTERNAL_BENCHMARK_ONE_SIZE(BM_Clear, NontrivialType); - -template -void BM_Reserve(benchmark::State& state) { - BatchedBenchmark( - state, - /* prepare_vec = */ - [](InlVec* vec, size_t) { - vec->clear(); - vec->resize(FromSize); - }, - /* test_vec = */ - [](InlVec* vec, size_t) { vec->reserve(ToCapacity); }); -} -ABSL_INTERNAL_BENCHMARK_TWO_SIZE(BM_Reserve, TrivialType); -ABSL_INTERNAL_BENCHMARK_TWO_SIZE(BM_Reserve, NontrivialType); - -template -void BM_ShrinkToFit(benchmark::State& state) { - BatchedBenchmark( - state, - /* prepare_vec = */ - [](InlVec* vec, size_t) { - vec->clear(); - vec->resize(ToCapacity); - vec->reserve(FromCapacity); - }, - /* test_vec = */ [](InlVec* vec, size_t) { vec->shrink_to_fit(); }); -} -ABSL_INTERNAL_BENCHMARK_TWO_SIZE(BM_ShrinkToFit, TrivialType); -ABSL_INTERNAL_BENCHMARK_TWO_SIZE(BM_ShrinkToFit, NontrivialType); - -template -void BM_Swap(benchmark::State& state) { - using VecT = InlVec; - std::array vector_batch{}; - BatchedBenchmark( - state, - /* prepare_vec = */ - [&](InlVec* vec, size_t i) { - vector_batch[i].clear(); - vector_batch[i].resize(ToSize); - vec->resize(FromSize); - }, - /* test_vec = */ - [&](InlVec* 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 diff --git a/third_party/abseil_cpp/absl/container/inlined_vector_exception_safety_test.cc b/third_party/abseil_cpp/absl/container/inlined_vector_exception_safety_test.cc deleted file mode 100644 index 0e6a05b5f6a7..000000000000 --- a/third_party/abseil_cpp/absl/container/inlined_vector_exception_safety_test.cc +++ /dev/null @@ -1,508 +0,0 @@ -// 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 -#include -#include -#include -#include - -#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; -using ThrowAlloc = testing::ThrowingAllocator; - -using ThrowerVec = absl::InlinedVector; -using MovableThrowerVec = absl::InlinedVector; - -using ThrowAllocThrowerVec = - absl::InlinedVector; -using ThrowAllocMovableThrowerVec = - absl::InlinedVector; - -// 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(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(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 -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, - TestParams, - TestParams>; - -using OneSizeTestParams = - ::testing::Types, - TestParams, - TestParams, - TestParams, - TestParams, - TestParams, - TestParams, - TestParams>; - -using TwoSizeTestParams = ::testing::Types< - TestParams, - TestParams, - TestParams, - TestParams, - TestParams, - TestParams, - TestParams, - TestParams, - TestParams, - TestParams, - TestParams, - TestParams, - TestParams, - TestParams, - TestParams, - TestParams>; - -template -struct NoSizeTest : ::testing::Test {}; -TYPED_TEST_SUITE(NoSizeTest, NoSizeTestParams); - -template -struct OneSizeTest : ::testing::Test {}; -TYPED_TEST_SUITE(OneSizeTest, OneSizeTestParams); - -template -struct TwoSizeTest : ::testing::Test {}; -TYPED_TEST_SUITE(TwoSizeTest, TwoSizeTestParams); - -template -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 -bool NoThrowGuarantee(VecT* /* vec */) { - return false; -} - -TYPED_TEST(NoSizeTest, DefaultConstructor) { - using VecT = typename TypeParam::VecT; - using allocator_type = typename VecT::allocator_type; - - testing::TestThrowingCtor(); - - testing::TestThrowingCtor(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(size); - - testing::TestThrowingCtor(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(size, value_type{}); - - testing::TestThrowingCtor(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( - ABSL_INTERNAL_MAKE_INIT_LIST(value_type, size)); - - testing::TestThrowingCtor( - 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 arr{}; - - testing::TestThrowingCtor(arr.begin(), arr.end()); - - testing::TestThrowingCtor(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(other_vec); - - testing::TestThrowingCtor(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::value) { - testing::TestThrowingCtor(VecT{size}); - - testing::TestThrowingCtor(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); - - 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 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, - 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); - - 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); - - 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 arr{}; - vec->insert(it, arr.begin(), arr.end()); - })); - EXPECT_TRUE(tester.Test([](VecT* vec) { - auto it = vec->begin() + (vec->size() / 2); - std::array arr{}; - vec->insert(it, arr.begin(), arr.end()); - })); - EXPECT_TRUE(tester.Test([](VecT* vec) { - auto it = vec->end(); - std::array 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); - - 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); - - 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); - - 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); - - 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); - - 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); - - 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); - - 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) diff --git a/third_party/abseil_cpp/absl/container/inlined_vector_test.cc b/third_party/abseil_cpp/absl/container/inlined_vector_test.cc deleted file mode 100644 index 98aff33498b2..000000000000 --- a/third_party/abseil_cpp/absl/container/inlined_vector_test.cc +++ /dev/null @@ -1,1815 +0,0 @@ -// 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 -#include -#include -#include -#include -#include -#include -#include -#include - -#include "gmock/gmock.h" -#include "gtest/gtest.h" -#include "absl/base/attributes.h" -#include "absl/base/internal/exception_testing.h" -#include "absl/base/internal/raw_logging.h" -#include "absl/base/macros.h" -#include "absl/base/options.h" -#include "absl/container/internal/counting_allocator.h" -#include "absl/container/internal/test_instance_tracker.h" -#include "absl/hash/hash_testing.h" -#include "absl/memory/memory.h" -#include "absl/strings/str_cat.h" - -namespace { - -using absl::container_internal::CountingAllocator; -using absl::test_internal::CopyableMovableInstance; -using absl::test_internal::CopyableOnlyInstance; -using absl::test_internal::InstanceTracker; -using testing::AllOf; -using testing::Each; -using testing::ElementsAre; -using testing::ElementsAreArray; -using testing::Eq; -using testing::Gt; -using testing::PrintToString; - -using IntVec = absl::InlinedVector; - -MATCHER_P(SizeIs, n, "") { - return testing::ExplainMatchResult(n, arg.size(), result_listener); -} - -MATCHER_P(CapacityIs, n, "") { - return testing::ExplainMatchResult(n, arg.capacity(), result_listener); -} - -MATCHER_P(ValueIs, e, "") { - return testing::ExplainMatchResult(e, arg.value(), result_listener); -} - -// TODO(bsamwel): Add support for movable-only types. - -// Test fixture for typed tests on BaseCountedInstance derived classes, see -// test_instance_tracker.h. -template -class InstanceTest : public ::testing::Test {}; -TYPED_TEST_SUITE_P(InstanceTest); - -// A simple reference counted class to make sure that the proper elements are -// destroyed in the erase(begin, end) test. -class RefCounted { - public: - RefCounted(int value, int* count) : value_(value), count_(count) { Ref(); } - - RefCounted(const RefCounted& v) : value_(v.value_), count_(v.count_) { - Ref(); - } - - ~RefCounted() { - Unref(); - count_ = nullptr; - } - - friend void swap(RefCounted& a, RefCounted& b) { - using std::swap; - swap(a.value_, b.value_); - swap(a.count_, b.count_); - } - - RefCounted& operator=(RefCounted v) { - using std::swap; - swap(*this, v); - return *this; - } - - void Ref() const { - ABSL_RAW_CHECK(count_ != nullptr, ""); - ++(*count_); - } - - void Unref() const { - --(*count_); - ABSL_RAW_CHECK(*count_ >= 0, ""); - } - - int value_; - int* count_; -}; - -using RefCountedVec = absl::InlinedVector; - -// A class with a vtable pointer -class Dynamic { - public: - virtual ~Dynamic() {} -}; - -using DynamicVec = absl::InlinedVector; - -// Append 0..len-1 to *v -template -static void Fill(Container* v, int len, int offset = 0) { - for (int i = 0; i < len; i++) { - v->push_back(i + offset); - } -} - -static IntVec Fill(int len, int offset = 0) { - IntVec v; - Fill(&v, len, offset); - return v; -} - -TEST(IntVec, SimpleOps) { - for (int len = 0; len < 20; len++) { - IntVec v; - const IntVec& cv = v; // const alias - - Fill(&v, len); - EXPECT_EQ(len, v.size()); - EXPECT_LE(len, v.capacity()); - - for (int i = 0; i < len; i++) { - EXPECT_EQ(i, v[i]); - EXPECT_EQ(i, v.at(i)); - } - EXPECT_EQ(v.begin(), v.data()); - EXPECT_EQ(cv.begin(), cv.data()); - - int counter = 0; - for (IntVec::iterator iter = v.begin(); iter != v.end(); ++iter) { - EXPECT_EQ(counter, *iter); - counter++; - } - EXPECT_EQ(counter, len); - - counter = 0; - for (IntVec::const_iterator iter = v.begin(); iter != v.end(); ++iter) { - EXPECT_EQ(counter, *iter); - counter++; - } - EXPECT_EQ(counter, len); - - counter = 0; - for (IntVec::const_iterator iter = v.cbegin(); iter != v.cend(); ++iter) { - EXPECT_EQ(counter, *iter); - counter++; - } - EXPECT_EQ(counter, len); - - if (len > 0) { - EXPECT_EQ(0, v.front()); - EXPECT_EQ(len - 1, v.back()); - v.pop_back(); - EXPECT_EQ(len - 1, v.size()); - for (int i = 0; i < v.size(); ++i) { - EXPECT_EQ(i, v[i]); - EXPECT_EQ(i, v.at(i)); - } - } - } -} - -TEST(IntVec, PopBackNoOverflow) { - IntVec v = {1}; - v.pop_back(); - EXPECT_EQ(v.size(), 0); -} - -TEST(IntVec, AtThrows) { - IntVec v = {1, 2, 3}; - EXPECT_EQ(v.at(2), 3); - ABSL_BASE_INTERNAL_EXPECT_FAIL(v.at(3), std::out_of_range, - "failed bounds check"); -} - -TEST(IntVec, ReverseIterator) { - for (int len = 0; len < 20; len++) { - IntVec v; - Fill(&v, len); - - int counter = len; - for (IntVec::reverse_iterator iter = v.rbegin(); iter != v.rend(); ++iter) { - counter--; - EXPECT_EQ(counter, *iter); - } - EXPECT_EQ(counter, 0); - - counter = len; - for (IntVec::const_reverse_iterator iter = v.rbegin(); iter != v.rend(); - ++iter) { - counter--; - EXPECT_EQ(counter, *iter); - } - EXPECT_EQ(counter, 0); - - counter = len; - for (IntVec::const_reverse_iterator iter = v.crbegin(); iter != v.crend(); - ++iter) { - counter--; - EXPECT_EQ(counter, *iter); - } - EXPECT_EQ(counter, 0); - } -} - -TEST(IntVec, Erase) { - for (int len = 1; len < 20; len++) { - for (int i = 0; i < len; ++i) { - IntVec v; - Fill(&v, len); - v.erase(v.begin() + i); - EXPECT_EQ(len - 1, v.size()); - for (int j = 0; j < i; ++j) { - EXPECT_EQ(j, v[j]); - } - for (int j = i; j < len - 1; ++j) { - EXPECT_EQ(j + 1, v[j]); - } - } - } -} - -TEST(IntVec, Hardened) { - IntVec v; - Fill(&v, 10); - EXPECT_EQ(v[9], 9); -#if !defined(NDEBUG) || ABSL_OPTION_HARDENED - EXPECT_DEATH_IF_SUPPORTED(v[10], ""); - EXPECT_DEATH_IF_SUPPORTED(v[-1], ""); -#endif -} - -// At the end of this test loop, the elements between [erase_begin, erase_end) -// should have reference counts == 0, and all others elements should have -// reference counts == 1. -TEST(RefCountedVec, EraseBeginEnd) { - for (int len = 1; len < 20; ++len) { - for (int erase_begin = 0; erase_begin < len; ++erase_begin) { - for (int erase_end = erase_begin; erase_end <= len; ++erase_end) { - std::vector counts(len, 0); - RefCountedVec v; - for (int i = 0; i < len; ++i) { - v.push_back(RefCounted(i, &counts[i])); - } - - int erase_len = erase_end - erase_begin; - - v.erase(v.begin() + erase_begin, v.begin() + erase_end); - - EXPECT_EQ(len - erase_len, v.size()); - - // Check the elements before the first element erased. - for (int i = 0; i < erase_begin; ++i) { - EXPECT_EQ(i, v[i].value_); - } - - // Check the elements after the first element erased. - for (int i = erase_begin; i < v.size(); ++i) { - EXPECT_EQ(i + erase_len, v[i].value_); - } - - // Check that the elements at the beginning are preserved. - for (int i = 0; i < erase_begin; ++i) { - EXPECT_EQ(1, counts[i]); - } - - // Check that the erased elements are destroyed - for (int i = erase_begin; i < erase_end; ++i) { - EXPECT_EQ(0, counts[i]); - } - - // Check that the elements at the end are preserved. - for (int i = erase_end; i < len; ++i) { - EXPECT_EQ(1, counts[i]); - } - } - } - } -} - -struct NoDefaultCtor { - explicit NoDefaultCtor(int) {} -}; -struct NoCopy { - NoCopy() {} - NoCopy(const NoCopy&) = delete; -}; -struct NoAssign { - NoAssign() {} - NoAssign& operator=(const NoAssign&) = delete; -}; -struct MoveOnly { - MoveOnly() {} - MoveOnly(MoveOnly&&) = default; - MoveOnly& operator=(MoveOnly&&) = default; -}; -TEST(InlinedVectorTest, NoDefaultCtor) { - absl::InlinedVector v(10, NoDefaultCtor(2)); - (void)v; -} -TEST(InlinedVectorTest, NoCopy) { - absl::InlinedVector v(10); - (void)v; -} -TEST(InlinedVectorTest, NoAssign) { - absl::InlinedVector v(10); - (void)v; -} -TEST(InlinedVectorTest, MoveOnly) { - absl::InlinedVector v; - v.push_back(MoveOnly{}); - v.push_back(MoveOnly{}); - v.push_back(MoveOnly{}); - v.erase(v.begin()); - v.push_back(MoveOnly{}); - v.erase(v.begin(), v.begin() + 1); - v.insert(v.begin(), MoveOnly{}); - v.emplace(v.begin()); - v.emplace(v.begin(), MoveOnly{}); -} -TEST(InlinedVectorTest, Noexcept) { - EXPECT_TRUE(std::is_nothrow_move_constructible::value); - EXPECT_TRUE((std::is_nothrow_move_constructible< - absl::InlinedVector>::value)); - - struct MoveCanThrow { - MoveCanThrow(MoveCanThrow&&) {} - }; - EXPECT_EQ(absl::default_allocator_is_nothrow::value, - (std::is_nothrow_move_constructible< - absl::InlinedVector>::value)); -} - -TEST(InlinedVectorTest, EmplaceBack) { - absl::InlinedVector, 1> v; - - auto& inlined_element = v.emplace_back("answer", 42); - EXPECT_EQ(&inlined_element, &v[0]); - EXPECT_EQ(inlined_element.first, "answer"); - EXPECT_EQ(inlined_element.second, 42); - - auto& allocated_element = v.emplace_back("taxicab", 1729); - EXPECT_EQ(&allocated_element, &v[1]); - EXPECT_EQ(allocated_element.first, "taxicab"); - EXPECT_EQ(allocated_element.second, 1729); -} - -TEST(InlinedVectorTest, ShrinkToFitGrowingVector) { - absl::InlinedVector, 1> v; - - v.shrink_to_fit(); - EXPECT_EQ(v.capacity(), 1); - - v.emplace_back("answer", 42); - v.shrink_to_fit(); - EXPECT_EQ(v.capacity(), 1); - - v.emplace_back("taxicab", 1729); - EXPECT_GE(v.capacity(), 2); - v.shrink_to_fit(); - EXPECT_EQ(v.capacity(), 2); - - v.reserve(100); - EXPECT_GE(v.capacity(), 100); - v.shrink_to_fit(); - EXPECT_EQ(v.capacity(), 2); -} - -TEST(InlinedVectorTest, ShrinkToFitEdgeCases) { - { - absl::InlinedVector, 1> v; - v.emplace_back("answer", 42); - v.emplace_back("taxicab", 1729); - EXPECT_GE(v.capacity(), 2); - v.pop_back(); - v.shrink_to_fit(); - EXPECT_EQ(v.capacity(), 1); - EXPECT_EQ(v[0].first, "answer"); - EXPECT_EQ(v[0].second, 42); - } - - { - absl::InlinedVector v(100); - v.resize(0); - v.shrink_to_fit(); - EXPECT_EQ(v.capacity(), 2); // inlined capacity - } - - { - absl::InlinedVector v(100); - v.resize(1); - v.shrink_to_fit(); - EXPECT_EQ(v.capacity(), 2); // inlined capacity - } - - { - absl::InlinedVector v(100); - v.resize(2); - v.shrink_to_fit(); - EXPECT_EQ(v.capacity(), 2); - } - - { - absl::InlinedVector v(100); - v.resize(3); - v.shrink_to_fit(); - EXPECT_EQ(v.capacity(), 3); - } -} - -TEST(IntVec, Insert) { - for (int len = 0; len < 20; len++) { - for (int pos = 0; pos <= len; pos++) { - { - // Single element - std::vector std_v; - Fill(&std_v, len); - IntVec v; - Fill(&v, len); - - std_v.insert(std_v.begin() + pos, 9999); - IntVec::iterator it = v.insert(v.cbegin() + pos, 9999); - EXPECT_THAT(v, ElementsAreArray(std_v)); - EXPECT_EQ(it, v.cbegin() + pos); - } - { - // n elements - std::vector std_v; - Fill(&std_v, len); - IntVec v; - Fill(&v, len); - - IntVec::size_type n = 5; - std_v.insert(std_v.begin() + pos, n, 9999); - IntVec::iterator it = v.insert(v.cbegin() + pos, n, 9999); - EXPECT_THAT(v, ElementsAreArray(std_v)); - EXPECT_EQ(it, v.cbegin() + pos); - } - { - // Iterator range (random access iterator) - std::vector std_v; - Fill(&std_v, len); - IntVec v; - Fill(&v, len); - - const std::vector input = {9999, 8888, 7777}; - std_v.insert(std_v.begin() + pos, input.cbegin(), input.cend()); - IntVec::iterator it = - v.insert(v.cbegin() + pos, input.cbegin(), input.cend()); - EXPECT_THAT(v, ElementsAreArray(std_v)); - EXPECT_EQ(it, v.cbegin() + pos); - } - { - // Iterator range (forward iterator) - std::vector std_v; - Fill(&std_v, len); - IntVec v; - Fill(&v, len); - - const std::forward_list input = {9999, 8888, 7777}; - std_v.insert(std_v.begin() + pos, input.cbegin(), input.cend()); - IntVec::iterator it = - v.insert(v.cbegin() + pos, input.cbegin(), input.cend()); - EXPECT_THAT(v, ElementsAreArray(std_v)); - EXPECT_EQ(it, v.cbegin() + pos); - } - { - // Iterator range (input iterator) - std::vector std_v; - Fill(&std_v, len); - IntVec v; - Fill(&v, len); - - std_v.insert(std_v.begin() + pos, {9999, 8888, 7777}); - std::istringstream input("9999 8888 7777"); - IntVec::iterator it = - v.insert(v.cbegin() + pos, std::istream_iterator(input), - std::istream_iterator()); - EXPECT_THAT(v, ElementsAreArray(std_v)); - EXPECT_EQ(it, v.cbegin() + pos); - } - { - // Initializer list - std::vector std_v; - Fill(&std_v, len); - IntVec v; - Fill(&v, len); - - std_v.insert(std_v.begin() + pos, {9999, 8888}); - IntVec::iterator it = v.insert(v.cbegin() + pos, {9999, 8888}); - EXPECT_THAT(v, ElementsAreArray(std_v)); - EXPECT_EQ(it, v.cbegin() + pos); - } - } - } -} - -TEST(RefCountedVec, InsertConstructorDestructor) { - // Make sure the proper construction/destruction happen during insert - // operations. - for (int len = 0; len < 20; len++) { - SCOPED_TRACE(len); - for (int pos = 0; pos <= len; pos++) { - SCOPED_TRACE(pos); - std::vector counts(len, 0); - int inserted_count = 0; - RefCountedVec v; - for (int i = 0; i < len; ++i) { - SCOPED_TRACE(i); - v.push_back(RefCounted(i, &counts[i])); - } - - EXPECT_THAT(counts, Each(Eq(1))); - - RefCounted insert_element(9999, &inserted_count); - EXPECT_EQ(1, inserted_count); - v.insert(v.begin() + pos, insert_element); - EXPECT_EQ(2, inserted_count); - // Check that the elements at the end are preserved. - EXPECT_THAT(counts, Each(Eq(1))); - EXPECT_EQ(2, inserted_count); - } - } -} - -TEST(IntVec, Resize) { - for (int len = 0; len < 20; len++) { - IntVec v; - Fill(&v, len); - - // Try resizing up and down by k elements - static const int kResizeElem = 1000000; - for (int k = 0; k < 10; k++) { - // Enlarging resize - v.resize(len + k, kResizeElem); - EXPECT_EQ(len + k, v.size()); - EXPECT_LE(len + k, v.capacity()); - for (int i = 0; i < len + k; i++) { - if (i < len) { - EXPECT_EQ(i, v[i]); - } else { - EXPECT_EQ(kResizeElem, v[i]); - } - } - - // Shrinking resize - v.resize(len, kResizeElem); - EXPECT_EQ(len, v.size()); - EXPECT_LE(len, v.capacity()); - for (int i = 0; i < len; i++) { - EXPECT_EQ(i, v[i]); - } - } - } -} - -TEST(IntVec, InitWithLength) { - for (int len = 0; len < 20; len++) { - IntVec v(len, 7); - EXPECT_EQ(len, v.size()); - EXPECT_LE(len, v.capacity()); - for (int i = 0; i < len; i++) { - EXPECT_EQ(7, v[i]); - } - } -} - -TEST(IntVec, CopyConstructorAndAssignment) { - for (int len = 0; len < 20; len++) { - IntVec v; - Fill(&v, len); - EXPECT_EQ(len, v.size()); - EXPECT_LE(len, v.capacity()); - - IntVec v2(v); - EXPECT_TRUE(v == v2) << PrintToString(v) << PrintToString(v2); - - for (int start_len = 0; start_len < 20; start_len++) { - IntVec v3; - Fill(&v3, start_len, 99); // Add dummy elements that should go away - v3 = v; - EXPECT_TRUE(v == v3) << PrintToString(v) << PrintToString(v3); - } - } -} - -TEST(IntVec, AliasingCopyAssignment) { - for (int len = 0; len < 20; ++len) { - IntVec original; - Fill(&original, len); - IntVec dup = original; - dup = *&dup; - EXPECT_EQ(dup, original); - } -} - -TEST(IntVec, MoveConstructorAndAssignment) { - for (int len = 0; len < 20; len++) { - IntVec v_in; - const int inlined_capacity = v_in.capacity(); - Fill(&v_in, len); - EXPECT_EQ(len, v_in.size()); - EXPECT_LE(len, v_in.capacity()); - - { - IntVec v_temp(v_in); - auto* old_data = v_temp.data(); - IntVec v_out(std::move(v_temp)); - EXPECT_TRUE(v_in == v_out) << PrintToString(v_in) << PrintToString(v_out); - if (v_in.size() > inlined_capacity) { - // Allocation is moved as a whole, data stays in place. - EXPECT_TRUE(v_out.data() == old_data); - } else { - EXPECT_FALSE(v_out.data() == old_data); - } - } - for (int start_len = 0; start_len < 20; start_len++) { - IntVec v_out; - Fill(&v_out, start_len, 99); // Add dummy elements that should go away - IntVec v_temp(v_in); - auto* old_data = v_temp.data(); - v_out = std::move(v_temp); - EXPECT_TRUE(v_in == v_out) << PrintToString(v_in) << PrintToString(v_out); - if (v_in.size() > inlined_capacity) { - // Allocation is moved as a whole, data stays in place. - EXPECT_TRUE(v_out.data() == old_data); - } else { - EXPECT_FALSE(v_out.data() == old_data); - } - } - } -} - -class NotTriviallyDestructible { - public: - NotTriviallyDestructible() : p_(new int(1)) {} - explicit NotTriviallyDestructible(int i) : p_(new int(i)) {} - - NotTriviallyDestructible(const NotTriviallyDestructible& other) - : p_(new int(*other.p_)) {} - - NotTriviallyDestructible& operator=(const NotTriviallyDestructible& other) { - p_ = absl::make_unique(*other.p_); - return *this; - } - - bool operator==(const NotTriviallyDestructible& other) const { - return *p_ == *other.p_; - } - - private: - std::unique_ptr p_; -}; - -TEST(AliasingTest, Emplace) { - for (int i = 2; i < 20; ++i) { - absl::InlinedVector vec; - for (int j = 0; j < i; ++j) { - vec.push_back(NotTriviallyDestructible(j)); - } - vec.emplace(vec.begin(), vec[0]); - EXPECT_EQ(vec[0], vec[1]); - vec.emplace(vec.begin() + i / 2, vec[i / 2]); - EXPECT_EQ(vec[i / 2], vec[i / 2 + 1]); - vec.emplace(vec.end() - 1, vec.back()); - EXPECT_EQ(vec[vec.size() - 2], vec.back()); - } -} - -TEST(AliasingTest, InsertWithCount) { - for (int i = 1; i < 20; ++i) { - absl::InlinedVector vec; - for (int j = 0; j < i; ++j) { - vec.push_back(NotTriviallyDestructible(j)); - } - for (int n = 0; n < 5; ++n) { - // We use back where we can because it's guaranteed to become invalidated - vec.insert(vec.begin(), n, vec.back()); - auto b = vec.begin(); - EXPECT_TRUE( - std::all_of(b, b + n, [&vec](const NotTriviallyDestructible& x) { - return x == vec.back(); - })); - - auto m_idx = vec.size() / 2; - vec.insert(vec.begin() + m_idx, n, vec.back()); - auto m = vec.begin() + m_idx; - EXPECT_TRUE( - std::all_of(m, m + n, [&vec](const NotTriviallyDestructible& x) { - return x == vec.back(); - })); - - // We want distinct values so the equality test is meaningful, - // vec[vec.size() - 1] is also almost always invalidated. - auto old_e = vec.size() - 1; - auto val = vec[old_e]; - vec.insert(vec.end(), n, vec[old_e]); - auto e = vec.begin() + old_e; - EXPECT_TRUE(std::all_of( - e, e + n, - [&val](const NotTriviallyDestructible& x) { return x == val; })); - } - } -} - -TEST(OverheadTest, Storage) { - // Check for size overhead. - // In particular, ensure that std::allocator doesn't cost anything to store. - // The union should be absorbing some of the allocation bookkeeping overhead - // in the larger vectors, leaving only the size_ field as overhead. - - struct T { void* val; }; - size_t expected_overhead = sizeof(T); - - EXPECT_EQ((2 * expected_overhead), - sizeof(absl::InlinedVector) - sizeof(T[1])); - EXPECT_EQ(expected_overhead, - sizeof(absl::InlinedVector) - sizeof(T[2])); - EXPECT_EQ(expected_overhead, - sizeof(absl::InlinedVector) - sizeof(T[3])); - EXPECT_EQ(expected_overhead, - sizeof(absl::InlinedVector) - sizeof(T[4])); - EXPECT_EQ(expected_overhead, - sizeof(absl::InlinedVector) - sizeof(T[5])); - EXPECT_EQ(expected_overhead, - sizeof(absl::InlinedVector) - sizeof(T[6])); - EXPECT_EQ(expected_overhead, - sizeof(absl::InlinedVector) - sizeof(T[7])); - EXPECT_EQ(expected_overhead, - sizeof(absl::InlinedVector) - sizeof(T[8])); -} - -TEST(IntVec, Clear) { - for (int len = 0; len < 20; len++) { - SCOPED_TRACE(len); - IntVec v; - Fill(&v, len); - v.clear(); - EXPECT_EQ(0, v.size()); - EXPECT_EQ(v.begin(), v.end()); - } -} - -TEST(IntVec, Reserve) { - for (int len = 0; len < 20; len++) { - IntVec v; - Fill(&v, len); - - for (int newlen = 0; newlen < 100; newlen++) { - const int* start_rep = v.data(); - v.reserve(newlen); - const int* final_rep = v.data(); - if (newlen <= len) { - EXPECT_EQ(start_rep, final_rep); - } - EXPECT_LE(newlen, v.capacity()); - - // Filling up to newlen should not change rep - while (v.size() < newlen) { - v.push_back(0); - } - EXPECT_EQ(final_rep, v.data()); - } - } -} - -TEST(StringVec, SelfRefPushBack) { - std::vector std_v; - absl::InlinedVector v; - const std::string s = "A quite long string to ensure heap."; - std_v.push_back(s); - v.push_back(s); - for (int i = 0; i < 20; ++i) { - EXPECT_THAT(v, ElementsAreArray(std_v)); - - v.push_back(v.back()); - std_v.push_back(std_v.back()); - } - EXPECT_THAT(v, ElementsAreArray(std_v)); -} - -TEST(StringVec, SelfRefPushBackWithMove) { - std::vector std_v; - absl::InlinedVector v; - const std::string s = "A quite long string to ensure heap."; - std_v.push_back(s); - v.push_back(s); - for (int i = 0; i < 20; ++i) { - EXPECT_EQ(v.back(), std_v.back()); - - v.push_back(std::move(v.back())); - std_v.push_back(std::move(std_v.back())); - } - EXPECT_EQ(v.back(), std_v.back()); -} - -TEST(StringVec, SelfMove) { - const std::string s = "A quite long string to ensure heap."; - for (int len = 0; len < 20; len++) { - SCOPED_TRACE(len); - absl::InlinedVector v; - for (int i = 0; i < len; ++i) { - SCOPED_TRACE(i); - v.push_back(s); - } - // Indirection necessary to avoid compiler warning. - v = std::move(*(&v)); - // Ensure that the inlined vector is still in a valid state by copying it. - // We don't expect specific contents since a self-move results in an - // unspecified valid state. - std::vector copy(v.begin(), v.end()); - } -} - -TEST(IntVec, Swap) { - for (int l1 = 0; l1 < 20; l1++) { - SCOPED_TRACE(l1); - for (int l2 = 0; l2 < 20; l2++) { - SCOPED_TRACE(l2); - IntVec a = Fill(l1, 0); - IntVec b = Fill(l2, 100); - { - using std::swap; - swap(a, b); - } - EXPECT_EQ(l1, b.size()); - EXPECT_EQ(l2, a.size()); - for (int i = 0; i < l1; i++) { - SCOPED_TRACE(i); - EXPECT_EQ(i, b[i]); - } - for (int i = 0; i < l2; i++) { - SCOPED_TRACE(i); - EXPECT_EQ(100 + i, a[i]); - } - } - } -} - -TYPED_TEST_P(InstanceTest, Swap) { - using Instance = TypeParam; - using InstanceVec = absl::InlinedVector; - for (int l1 = 0; l1 < 20; l1++) { - SCOPED_TRACE(l1); - for (int l2 = 0; l2 < 20; l2++) { - SCOPED_TRACE(l2); - InstanceTracker tracker; - InstanceVec a, b; - const size_t inlined_capacity = a.capacity(); - auto min_len = std::min(l1, l2); - auto max_len = std::max(l1, l2); - for (int i = 0; i < l1; i++) a.push_back(Instance(i)); - for (int i = 0; i < l2; i++) b.push_back(Instance(100 + i)); - EXPECT_EQ(tracker.instances(), l1 + l2); - tracker.ResetCopiesMovesSwaps(); - { - using std::swap; - swap(a, b); - } - EXPECT_EQ(tracker.instances(), l1 + l2); - if (a.size() > inlined_capacity && b.size() > inlined_capacity) { - EXPECT_EQ(tracker.swaps(), 0); // Allocations are swapped. - EXPECT_EQ(tracker.moves(), 0); - } else if (a.size() <= inlined_capacity && b.size() <= inlined_capacity) { - EXPECT_EQ(tracker.swaps(), min_len); - EXPECT_EQ((tracker.moves() ? tracker.moves() : tracker.copies()), - max_len - min_len); - } else { - // One is allocated and the other isn't. The allocation is transferred - // without copying elements, and the inlined instances are copied/moved. - EXPECT_EQ(tracker.swaps(), 0); - EXPECT_EQ((tracker.moves() ? tracker.moves() : tracker.copies()), - min_len); - } - - EXPECT_EQ(l1, b.size()); - EXPECT_EQ(l2, a.size()); - for (int i = 0; i < l1; i++) { - EXPECT_EQ(i, b[i].value()); - } - for (int i = 0; i < l2; i++) { - EXPECT_EQ(100 + i, a[i].value()); - } - } - } -} - -TEST(IntVec, EqualAndNotEqual) { - IntVec a, b; - EXPECT_TRUE(a == b); - EXPECT_FALSE(a != b); - - a.push_back(3); - EXPECT_FALSE(a == b); - EXPECT_TRUE(a != b); - - b.push_back(3); - EXPECT_TRUE(a == b); - EXPECT_FALSE(a != b); - - b.push_back(7); - EXPECT_FALSE(a == b); - EXPECT_TRUE(a != b); - - a.push_back(6); - EXPECT_FALSE(a == b); - EXPECT_TRUE(a != b); - - a.clear(); - b.clear(); - for (int i = 0; i < 100; i++) { - a.push_back(i); - b.push_back(i); - EXPECT_TRUE(a == b); - EXPECT_FALSE(a != b); - - b[i] = b[i] + 1; - EXPECT_FALSE(a == b); - EXPECT_TRUE(a != b); - - b[i] = b[i] - 1; // Back to before - EXPECT_TRUE(a == b); - EXPECT_FALSE(a != b); - } -} - -TEST(IntVec, RelationalOps) { - IntVec a, b; - EXPECT_FALSE(a < b); - EXPECT_FALSE(b < a); - EXPECT_FALSE(a > b); - EXPECT_FALSE(b > a); - EXPECT_TRUE(a <= b); - EXPECT_TRUE(b <= a); - EXPECT_TRUE(a >= b); - EXPECT_TRUE(b >= a); - b.push_back(3); - EXPECT_TRUE(a < b); - EXPECT_FALSE(b < a); - EXPECT_FALSE(a > b); - EXPECT_TRUE(b > a); - EXPECT_TRUE(a <= b); - EXPECT_FALSE(b <= a); - EXPECT_FALSE(a >= b); - EXPECT_TRUE(b >= a); -} - -TYPED_TEST_P(InstanceTest, CountConstructorsDestructors) { - using Instance = TypeParam; - using InstanceVec = absl::InlinedVector; - InstanceTracker tracker; - for (int len = 0; len < 20; len++) { - SCOPED_TRACE(len); - tracker.ResetCopiesMovesSwaps(); - - InstanceVec v; - const size_t inlined_capacity = v.capacity(); - for (int i = 0; i < len; i++) { - v.push_back(Instance(i)); - } - EXPECT_EQ(tracker.instances(), len); - EXPECT_GE(tracker.copies() + tracker.moves(), - len); // More due to reallocation. - tracker.ResetCopiesMovesSwaps(); - - // Enlarging resize() must construct some objects - tracker.ResetCopiesMovesSwaps(); - v.resize(len + 10, Instance(100)); - EXPECT_EQ(tracker.instances(), len + 10); - if (len <= inlined_capacity && len + 10 > inlined_capacity) { - EXPECT_EQ(tracker.copies() + tracker.moves(), 10 + len); - } else { - // Only specify a minimum number of copies + moves. We don't want to - // depend on the reallocation policy here. - EXPECT_GE(tracker.copies() + tracker.moves(), - 10); // More due to reallocation. - } - - // Shrinking resize() must destroy some objects - tracker.ResetCopiesMovesSwaps(); - v.resize(len, Instance(100)); - EXPECT_EQ(tracker.instances(), len); - EXPECT_EQ(tracker.copies(), 0); - EXPECT_EQ(tracker.moves(), 0); - - // reserve() must not increase the number of initialized objects - SCOPED_TRACE("reserve"); - v.reserve(len + 1000); - EXPECT_EQ(tracker.instances(), len); - EXPECT_EQ(tracker.copies() + tracker.moves(), len); - - // pop_back() and erase() must destroy one object - if (len > 0) { - tracker.ResetCopiesMovesSwaps(); - v.pop_back(); - EXPECT_EQ(tracker.instances(), len - 1); - EXPECT_EQ(tracker.copies(), 0); - EXPECT_EQ(tracker.moves(), 0); - - if (!v.empty()) { - tracker.ResetCopiesMovesSwaps(); - v.erase(v.begin()); - EXPECT_EQ(tracker.instances(), len - 2); - EXPECT_EQ(tracker.copies() + tracker.moves(), len - 2); - } - } - - tracker.ResetCopiesMovesSwaps(); - int instances_before_empty_erase = tracker.instances(); - v.erase(v.begin(), v.begin()); - EXPECT_EQ(tracker.instances(), instances_before_empty_erase); - EXPECT_EQ(tracker.copies() + tracker.moves(), 0); - } -} - -TYPED_TEST_P(InstanceTest, CountConstructorsDestructorsOnCopyConstruction) { - using Instance = TypeParam; - using InstanceVec = absl::InlinedVector; - InstanceTracker tracker; - for (int len = 0; len < 20; len++) { - SCOPED_TRACE(len); - tracker.ResetCopiesMovesSwaps(); - - InstanceVec v; - for (int i = 0; i < len; i++) { - v.push_back(Instance(i)); - } - EXPECT_EQ(tracker.instances(), len); - EXPECT_GE(tracker.copies() + tracker.moves(), - len); // More due to reallocation. - tracker.ResetCopiesMovesSwaps(); - { // Copy constructor should create 'len' more instances. - InstanceVec v_copy(v); - EXPECT_EQ(tracker.instances(), len + len); - EXPECT_EQ(tracker.copies(), len); - EXPECT_EQ(tracker.moves(), 0); - } - EXPECT_EQ(tracker.instances(), len); - } -} - -TYPED_TEST_P(InstanceTest, CountConstructorsDestructorsOnMoveConstruction) { - using Instance = TypeParam; - using InstanceVec = absl::InlinedVector; - InstanceTracker tracker; - for (int len = 0; len < 20; len++) { - SCOPED_TRACE(len); - tracker.ResetCopiesMovesSwaps(); - - InstanceVec v; - const size_t inlined_capacity = v.capacity(); - for (int i = 0; i < len; i++) { - v.push_back(Instance(i)); - } - EXPECT_EQ(tracker.instances(), len); - EXPECT_GE(tracker.copies() + tracker.moves(), - len); // More due to reallocation. - tracker.ResetCopiesMovesSwaps(); - { - InstanceVec v_copy(std::move(v)); - if (len > inlined_capacity) { - // Allocation is moved as a whole. - EXPECT_EQ(tracker.instances(), len); - EXPECT_EQ(tracker.live_instances(), len); - // Tests an implementation detail, don't rely on this in your code. - EXPECT_EQ(v.size(), 0); // NOLINT misc-use-after-move - EXPECT_EQ(tracker.copies(), 0); - EXPECT_EQ(tracker.moves(), 0); - } else { - EXPECT_EQ(tracker.instances(), len + len); - if (Instance::supports_move()) { - EXPECT_EQ(tracker.live_instances(), len); - EXPECT_EQ(tracker.copies(), 0); - EXPECT_EQ(tracker.moves(), len); - } else { - EXPECT_EQ(tracker.live_instances(), len + len); - EXPECT_EQ(tracker.copies(), len); - EXPECT_EQ(tracker.moves(), 0); - } - } - EXPECT_EQ(tracker.swaps(), 0); - } - } -} - -TYPED_TEST_P(InstanceTest, CountConstructorsDestructorsOnAssignment) { - using Instance = TypeParam; - using InstanceVec = absl::InlinedVector; - InstanceTracker tracker; - for (int len = 0; len < 20; len++) { - SCOPED_TRACE(len); - for (int longorshort = 0; longorshort <= 1; ++longorshort) { - SCOPED_TRACE(longorshort); - tracker.ResetCopiesMovesSwaps(); - - InstanceVec longer, shorter; - for (int i = 0; i < len; i++) { - longer.push_back(Instance(i)); - shorter.push_back(Instance(i)); - } - longer.push_back(Instance(len)); - EXPECT_EQ(tracker.instances(), len + len + 1); - EXPECT_GE(tracker.copies() + tracker.moves(), - len + len + 1); // More due to reallocation. - - tracker.ResetCopiesMovesSwaps(); - if (longorshort) { - shorter = longer; - EXPECT_EQ(tracker.instances(), (len + 1) + (len + 1)); - EXPECT_GE(tracker.copies() + tracker.moves(), - len + 1); // More due to reallocation. - } else { - longer = shorter; - EXPECT_EQ(tracker.instances(), len + len); - EXPECT_EQ(tracker.copies() + tracker.moves(), len); - } - } - } -} - -TYPED_TEST_P(InstanceTest, CountConstructorsDestructorsOnMoveAssignment) { - using Instance = TypeParam; - using InstanceVec = absl::InlinedVector; - InstanceTracker tracker; - for (int len = 0; len < 20; len++) { - SCOPED_TRACE(len); - for (int longorshort = 0; longorshort <= 1; ++longorshort) { - SCOPED_TRACE(longorshort); - tracker.ResetCopiesMovesSwaps(); - - InstanceVec longer, shorter; - const int inlined_capacity = longer.capacity(); - for (int i = 0; i < len; i++) { - longer.push_back(Instance(i)); - shorter.push_back(Instance(i)); - } - longer.push_back(Instance(len)); - EXPECT_EQ(tracker.instances(), len + len + 1); - EXPECT_GE(tracker.copies() + tracker.moves(), - len + len + 1); // More due to reallocation. - - tracker.ResetCopiesMovesSwaps(); - int src_len; - if (longorshort) { - src_len = len + 1; - shorter = std::move(longer); - } else { - src_len = len; - longer = std::move(shorter); - } - if (src_len > inlined_capacity) { - // Allocation moved as a whole. - EXPECT_EQ(tracker.instances(), src_len); - EXPECT_EQ(tracker.live_instances(), src_len); - EXPECT_EQ(tracker.copies(), 0); - EXPECT_EQ(tracker.moves(), 0); - } else { - // Elements are all copied. - EXPECT_EQ(tracker.instances(), src_len + src_len); - if (Instance::supports_move()) { - EXPECT_EQ(tracker.copies(), 0); - EXPECT_EQ(tracker.moves(), src_len); - EXPECT_EQ(tracker.live_instances(), src_len); - } else { - EXPECT_EQ(tracker.copies(), src_len); - EXPECT_EQ(tracker.moves(), 0); - EXPECT_EQ(tracker.live_instances(), src_len + src_len); - } - } - EXPECT_EQ(tracker.swaps(), 0); - } - } -} - -TEST(CountElemAssign, SimpleTypeWithInlineBacking) { - for (size_t original_size = 0; original_size <= 5; ++original_size) { - SCOPED_TRACE(original_size); - // Original contents are [12345, 12345, ...] - std::vector original_contents(original_size, 12345); - - absl::InlinedVector v(original_contents.begin(), - original_contents.end()); - v.assign(2, 123); - EXPECT_THAT(v, AllOf(SizeIs(2), ElementsAre(123, 123))); - if (original_size <= 2) { - // If the original had inline backing, it should stay inline. - EXPECT_EQ(2, v.capacity()); - } - } -} - -TEST(CountElemAssign, SimpleTypeWithAllocation) { - for (size_t original_size = 0; original_size <= 5; ++original_size) { - SCOPED_TRACE(original_size); - // Original contents are [12345, 12345, ...] - std::vector original_contents(original_size, 12345); - - absl::InlinedVector v(original_contents.begin(), - original_contents.end()); - v.assign(3, 123); - EXPECT_THAT(v, AllOf(SizeIs(3), ElementsAre(123, 123, 123))); - EXPECT_LE(v.size(), v.capacity()); - } -} - -TYPED_TEST_P(InstanceTest, CountElemAssignInlineBacking) { - using Instance = TypeParam; - for (size_t original_size = 0; original_size <= 5; ++original_size) { - SCOPED_TRACE(original_size); - // Original contents are [12345, 12345, ...] - std::vector original_contents(original_size, Instance(12345)); - - absl::InlinedVector v(original_contents.begin(), - original_contents.end()); - v.assign(2, Instance(123)); - EXPECT_THAT(v, AllOf(SizeIs(2), ElementsAre(ValueIs(123), ValueIs(123)))); - if (original_size <= 2) { - // If the original had inline backing, it should stay inline. - EXPECT_EQ(2, v.capacity()); - } - } -} - -template -void InstanceCountElemAssignWithAllocationTest() { - for (size_t original_size = 0; original_size <= 5; ++original_size) { - SCOPED_TRACE(original_size); - // Original contents are [12345, 12345, ...] - std::vector original_contents(original_size, Instance(12345)); - - absl::InlinedVector v(original_contents.begin(), - original_contents.end()); - v.assign(3, Instance(123)); - EXPECT_THAT(v, AllOf(SizeIs(3), ElementsAre(ValueIs(123), ValueIs(123), - ValueIs(123)))); - EXPECT_LE(v.size(), v.capacity()); - } -} -TEST(CountElemAssign, WithAllocationCopyableInstance) { - InstanceCountElemAssignWithAllocationTest(); -} -TEST(CountElemAssign, WithAllocationCopyableMovableInstance) { - InstanceCountElemAssignWithAllocationTest(); -} - -TEST(RangedConstructor, SimpleType) { - std::vector source_v = {4, 5, 6}; - // First try to fit in inline backing - absl::InlinedVector v(source_v.begin(), source_v.end()); - EXPECT_EQ(3, v.size()); - EXPECT_EQ(4, v.capacity()); // Indication that we're still on inlined storage - EXPECT_EQ(4, v[0]); - EXPECT_EQ(5, v[1]); - EXPECT_EQ(6, v[2]); - - // Now, force a re-allocate - absl::InlinedVector realloc_v(source_v.begin(), source_v.end()); - EXPECT_EQ(3, realloc_v.size()); - EXPECT_LT(2, realloc_v.capacity()); - EXPECT_EQ(4, realloc_v[0]); - EXPECT_EQ(5, realloc_v[1]); - EXPECT_EQ(6, realloc_v[2]); -} - -// Test for ranged constructors using Instance as the element type and -// SourceContainer as the source container type. -template -void InstanceRangedConstructorTestForContainer() { - InstanceTracker tracker; - SourceContainer source_v = {Instance(0), Instance(1)}; - tracker.ResetCopiesMovesSwaps(); - absl::InlinedVector v(source_v.begin(), - source_v.end()); - EXPECT_EQ(2, v.size()); - EXPECT_LT(1, v.capacity()); - EXPECT_EQ(0, v[0].value()); - EXPECT_EQ(1, v[1].value()); - EXPECT_EQ(tracker.copies(), 2); - EXPECT_EQ(tracker.moves(), 0); -} - -template -void InstanceRangedConstructorTestWithCapacity() { - // Test with const and non-const, random access and non-random-access sources. - // TODO(bsamwel): Test with an input iterator source. - { - SCOPED_TRACE("std::list"); - InstanceRangedConstructorTestForContainer, - inlined_capacity>(); - { - SCOPED_TRACE("const std::list"); - InstanceRangedConstructorTestForContainer< - Instance, const std::list, inlined_capacity>(); - } - { - SCOPED_TRACE("std::vector"); - InstanceRangedConstructorTestForContainer, - inlined_capacity>(); - } - { - SCOPED_TRACE("const std::vector"); - InstanceRangedConstructorTestForContainer< - Instance, const std::vector, inlined_capacity>(); - } - } -} - -TYPED_TEST_P(InstanceTest, RangedConstructor) { - using Instance = TypeParam; - SCOPED_TRACE("capacity=1"); - InstanceRangedConstructorTestWithCapacity(); - SCOPED_TRACE("capacity=2"); - InstanceRangedConstructorTestWithCapacity(); -} - -TEST(RangedConstructor, ElementsAreConstructed) { - std::vector source_v = {"cat", "dog"}; - - // Force expansion and re-allocation of v. Ensures that when the vector is - // expanded that new elements are constructed. - absl::InlinedVector v(source_v.begin(), source_v.end()); - EXPECT_EQ("cat", v[0]); - EXPECT_EQ("dog", v[1]); -} - -TEST(RangedAssign, SimpleType) { - // Test for all combinations of original sizes (empty and non-empty inline, - // and out of line) and target sizes. - for (size_t original_size = 0; original_size <= 5; ++original_size) { - SCOPED_TRACE(original_size); - // Original contents are [12345, 12345, ...] - std::vector original_contents(original_size, 12345); - - for (size_t target_size = 0; target_size <= 5; ++target_size) { - SCOPED_TRACE(target_size); - - // New contents are [3, 4, ...] - std::vector new_contents; - for (size_t i = 0; i < target_size; ++i) { - new_contents.push_back(i + 3); - } - - absl::InlinedVector v(original_contents.begin(), - original_contents.end()); - v.assign(new_contents.begin(), new_contents.end()); - - EXPECT_EQ(new_contents.size(), v.size()); - EXPECT_LE(new_contents.size(), v.capacity()); - if (target_size <= 3 && original_size <= 3) { - // Storage should stay inline when target size is small. - EXPECT_EQ(3, v.capacity()); - } - EXPECT_THAT(v, ElementsAreArray(new_contents)); - } - } -} - -// Returns true if lhs and rhs have the same value. -template -static bool InstanceValuesEqual(const Instance& lhs, const Instance& rhs) { - return lhs.value() == rhs.value(); -} - -// Test for ranged assign() using Instance as the element type and -// SourceContainer as the source container type. -template -void InstanceRangedAssignTestForContainer() { - // Test for all combinations of original sizes (empty and non-empty inline, - // and out of line) and target sizes. - for (size_t original_size = 0; original_size <= 5; ++original_size) { - SCOPED_TRACE(original_size); - // Original contents are [12345, 12345, ...] - std::vector original_contents(original_size, Instance(12345)); - - for (size_t target_size = 0; target_size <= 5; ++target_size) { - SCOPED_TRACE(target_size); - - // New contents are [3, 4, ...] - // Generate data using a non-const container, because SourceContainer - // itself may be const. - // TODO(bsamwel): Test with an input iterator. - std::vector new_contents_in; - for (size_t i = 0; i < target_size; ++i) { - new_contents_in.push_back(Instance(i + 3)); - } - SourceContainer new_contents(new_contents_in.begin(), - new_contents_in.end()); - - absl::InlinedVector v(original_contents.begin(), - original_contents.end()); - v.assign(new_contents.begin(), new_contents.end()); - - EXPECT_EQ(new_contents.size(), v.size()); - EXPECT_LE(new_contents.size(), v.capacity()); - if (target_size <= 3 && original_size <= 3) { - // Storage should stay inline when target size is small. - EXPECT_EQ(3, v.capacity()); - } - EXPECT_TRUE(std::equal(v.begin(), v.end(), new_contents.begin(), - InstanceValuesEqual)); - } - } -} - -TYPED_TEST_P(InstanceTest, RangedAssign) { - using Instance = TypeParam; - // Test with const and non-const, random access and non-random-access sources. - // TODO(bsamwel): Test with an input iterator source. - SCOPED_TRACE("std::list"); - InstanceRangedAssignTestForContainer>(); - SCOPED_TRACE("const std::list"); - InstanceRangedAssignTestForContainer>(); - SCOPED_TRACE("std::vector"); - InstanceRangedAssignTestForContainer>(); - SCOPED_TRACE("const std::vector"); - InstanceRangedAssignTestForContainer>(); -} - -TEST(InitializerListConstructor, SimpleTypeWithInlineBacking) { - EXPECT_THAT((absl::InlinedVector{4, 5, 6}), - AllOf(SizeIs(3), CapacityIs(4), ElementsAre(4, 5, 6))); -} - -TEST(InitializerListConstructor, SimpleTypeWithReallocationRequired) { - EXPECT_THAT((absl::InlinedVector{4, 5, 6}), - AllOf(SizeIs(3), CapacityIs(Gt(2)), ElementsAre(4, 5, 6))); -} - -TEST(InitializerListConstructor, DisparateTypesInList) { - EXPECT_THAT((absl::InlinedVector{-7, 8ULL}), ElementsAre(-7, 8)); - - EXPECT_THAT((absl::InlinedVector{"foo", std::string("bar")}), - ElementsAre("foo", "bar")); -} - -TEST(InitializerListConstructor, ComplexTypeWithInlineBacking) { - EXPECT_THAT((absl::InlinedVector{ - CopyableMovableInstance(0)}), - AllOf(SizeIs(1), CapacityIs(1), ElementsAre(ValueIs(0)))); -} - -TEST(InitializerListConstructor, ComplexTypeWithReallocationRequired) { - EXPECT_THAT( - (absl::InlinedVector{ - CopyableMovableInstance(0), CopyableMovableInstance(1)}), - AllOf(SizeIs(2), CapacityIs(Gt(1)), ElementsAre(ValueIs(0), ValueIs(1)))); -} - -TEST(InitializerListAssign, SimpleTypeFitsInlineBacking) { - for (size_t original_size = 0; original_size <= 4; ++original_size) { - SCOPED_TRACE(original_size); - - absl::InlinedVector v1(original_size, 12345); - const size_t original_capacity_v1 = v1.capacity(); - v1.assign({3}); - EXPECT_THAT( - v1, AllOf(SizeIs(1), CapacityIs(original_capacity_v1), ElementsAre(3))); - - absl::InlinedVector v2(original_size, 12345); - const size_t original_capacity_v2 = v2.capacity(); - v2 = {3}; - EXPECT_THAT( - v2, AllOf(SizeIs(1), CapacityIs(original_capacity_v2), ElementsAre(3))); - } -} - -TEST(InitializerListAssign, SimpleTypeDoesNotFitInlineBacking) { - for (size_t original_size = 0; original_size <= 4; ++original_size) { - SCOPED_TRACE(original_size); - absl::InlinedVector v1(original_size, 12345); - v1.assign({3, 4, 5}); - EXPECT_THAT(v1, AllOf(SizeIs(3), ElementsAre(3, 4, 5))); - EXPECT_LE(3, v1.capacity()); - - absl::InlinedVector v2(original_size, 12345); - v2 = {3, 4, 5}; - EXPECT_THAT(v2, AllOf(SizeIs(3), ElementsAre(3, 4, 5))); - EXPECT_LE(3, v2.capacity()); - } -} - -TEST(InitializerListAssign, DisparateTypesInList) { - absl::InlinedVector v_int1; - v_int1.assign({-7, 8ULL}); - EXPECT_THAT(v_int1, ElementsAre(-7, 8)); - - absl::InlinedVector v_int2; - v_int2 = {-7, 8ULL}; - EXPECT_THAT(v_int2, ElementsAre(-7, 8)); - - absl::InlinedVector v_string1; - v_string1.assign({"foo", std::string("bar")}); - EXPECT_THAT(v_string1, ElementsAre("foo", "bar")); - - absl::InlinedVector v_string2; - v_string2 = {"foo", std::string("bar")}; - EXPECT_THAT(v_string2, ElementsAre("foo", "bar")); -} - -TYPED_TEST_P(InstanceTest, InitializerListAssign) { - using Instance = TypeParam; - for (size_t original_size = 0; original_size <= 4; ++original_size) { - SCOPED_TRACE(original_size); - absl::InlinedVector v(original_size, Instance(12345)); - const size_t original_capacity = v.capacity(); - v.assign({Instance(3)}); - EXPECT_THAT(v, AllOf(SizeIs(1), CapacityIs(original_capacity), - ElementsAre(ValueIs(3)))); - } - for (size_t original_size = 0; original_size <= 4; ++original_size) { - SCOPED_TRACE(original_size); - absl::InlinedVector v(original_size, Instance(12345)); - v.assign({Instance(3), Instance(4), Instance(5)}); - EXPECT_THAT( - v, AllOf(SizeIs(3), ElementsAre(ValueIs(3), ValueIs(4), ValueIs(5)))); - EXPECT_LE(3, v.capacity()); - } -} - -REGISTER_TYPED_TEST_CASE_P(InstanceTest, Swap, CountConstructorsDestructors, - CountConstructorsDestructorsOnCopyConstruction, - CountConstructorsDestructorsOnMoveConstruction, - CountConstructorsDestructorsOnAssignment, - CountConstructorsDestructorsOnMoveAssignment, - CountElemAssignInlineBacking, RangedConstructor, - RangedAssign, InitializerListAssign); - -using InstanceTypes = - ::testing::Types; -INSTANTIATE_TYPED_TEST_CASE_P(InstanceTestOnTypes, InstanceTest, InstanceTypes); - -TEST(DynamicVec, DynamicVecCompiles) { - DynamicVec v; - (void)v; -} - -TEST(AllocatorSupportTest, Constructors) { - using MyAlloc = CountingAllocator; - using AllocVec = absl::InlinedVector; - const int ia[] = {0, 1, 2, 3, 4, 5, 6, 7}; - int64_t allocated = 0; - MyAlloc alloc(&allocated); - { AllocVec ABSL_ATTRIBUTE_UNUSED v; } - { AllocVec ABSL_ATTRIBUTE_UNUSED v(alloc); } - { AllocVec ABSL_ATTRIBUTE_UNUSED v(ia, ia + ABSL_ARRAYSIZE(ia), alloc); } - { AllocVec ABSL_ATTRIBUTE_UNUSED v({1, 2, 3}, alloc); } - - AllocVec v2; - { AllocVec ABSL_ATTRIBUTE_UNUSED v(v2, alloc); } - { AllocVec ABSL_ATTRIBUTE_UNUSED v(std::move(v2), alloc); } -} - -TEST(AllocatorSupportTest, CountAllocations) { - using MyAlloc = CountingAllocator; - using AllocVec = absl::InlinedVector; - const int ia[] = {0, 1, 2, 3, 4, 5, 6, 7}; - int64_t allocated = 0; - MyAlloc alloc(&allocated); - { - AllocVec ABSL_ATTRIBUTE_UNUSED v(ia, ia + 4, alloc); - EXPECT_THAT(allocated, 0); - } - EXPECT_THAT(allocated, 0); - { - AllocVec ABSL_ATTRIBUTE_UNUSED v(ia, ia + ABSL_ARRAYSIZE(ia), alloc); - EXPECT_THAT(allocated, v.size() * sizeof(int)); - } - EXPECT_THAT(allocated, 0); - { - AllocVec v(4, 1, alloc); - EXPECT_THAT(allocated, 0); - - int64_t allocated2 = 0; - MyAlloc alloc2(&allocated2); - AllocVec v2(v, alloc2); - EXPECT_THAT(allocated2, 0); - - int64_t allocated3 = 0; - MyAlloc alloc3(&allocated3); - AllocVec v3(std::move(v), alloc3); - EXPECT_THAT(allocated3, 0); - } - EXPECT_THAT(allocated, 0); - { - AllocVec v(8, 2, alloc); - EXPECT_THAT(allocated, v.size() * sizeof(int)); - - int64_t allocated2 = 0; - MyAlloc alloc2(&allocated2); - AllocVec v2(v, alloc2); - EXPECT_THAT(allocated2, v2.size() * sizeof(int)); - - int64_t allocated3 = 0; - MyAlloc alloc3(&allocated3); - AllocVec v3(std::move(v), alloc3); - EXPECT_THAT(allocated3, v3.size() * sizeof(int)); - } - EXPECT_EQ(allocated, 0); - { - // Test shrink_to_fit deallocations. - AllocVec v(8, 2, alloc); - EXPECT_EQ(allocated, 8 * sizeof(int)); - v.resize(5); - EXPECT_EQ(allocated, 8 * sizeof(int)); - v.shrink_to_fit(); - EXPECT_EQ(allocated, 5 * sizeof(int)); - v.resize(4); - EXPECT_EQ(allocated, 5 * sizeof(int)); - v.shrink_to_fit(); - EXPECT_EQ(allocated, 0); - } -} - -TEST(AllocatorSupportTest, SwapBothAllocated) { - using MyAlloc = CountingAllocator; - using AllocVec = absl::InlinedVector; - int64_t allocated1 = 0; - int64_t allocated2 = 0; - { - const int ia1[] = {0, 1, 2, 3, 4, 5, 6, 7}; - const int ia2[] = {0, 1, 2, 3, 4, 5, 6, 7, 8}; - MyAlloc a1(&allocated1); - MyAlloc a2(&allocated2); - AllocVec v1(ia1, ia1 + ABSL_ARRAYSIZE(ia1), a1); - AllocVec v2(ia2, ia2 + ABSL_ARRAYSIZE(ia2), a2); - EXPECT_LT(v1.capacity(), v2.capacity()); - EXPECT_THAT(allocated1, v1.capacity() * sizeof(int)); - EXPECT_THAT(allocated2, v2.capacity() * sizeof(int)); - v1.swap(v2); - EXPECT_THAT(v1, ElementsAreArray(ia2)); - EXPECT_THAT(v2, ElementsAreArray(ia1)); - EXPECT_THAT(allocated1, v2.capacity() * sizeof(int)); - EXPECT_THAT(allocated2, v1.capacity() * sizeof(int)); - } - EXPECT_THAT(allocated1, 0); - EXPECT_THAT(allocated2, 0); -} - -TEST(AllocatorSupportTest, SwapOneAllocated) { - using MyAlloc = CountingAllocator; - using AllocVec = absl::InlinedVector; - int64_t allocated1 = 0; - int64_t allocated2 = 0; - { - const int ia1[] = {0, 1, 2, 3, 4, 5, 6, 7}; - const int ia2[] = {0, 1, 2, 3}; - MyAlloc a1(&allocated1); - MyAlloc a2(&allocated2); - AllocVec v1(ia1, ia1 + ABSL_ARRAYSIZE(ia1), a1); - AllocVec v2(ia2, ia2 + ABSL_ARRAYSIZE(ia2), a2); - EXPECT_THAT(allocated1, v1.capacity() * sizeof(int)); - EXPECT_THAT(allocated2, 0); - v1.swap(v2); - EXPECT_THAT(v1, ElementsAreArray(ia2)); - EXPECT_THAT(v2, ElementsAreArray(ia1)); - EXPECT_THAT(allocated1, v2.capacity() * sizeof(int)); - EXPECT_THAT(allocated2, 0); - EXPECT_TRUE(v2.get_allocator() == a1); - EXPECT_TRUE(v1.get_allocator() == a2); - } - EXPECT_THAT(allocated1, 0); - EXPECT_THAT(allocated2, 0); -} - -TEST(AllocatorSupportTest, ScopedAllocatorWorksInlined) { - using StdVector = std::vector>; - using Alloc = CountingAllocator; - using ScopedAlloc = std::scoped_allocator_adaptor; - using AllocVec = absl::InlinedVector; - - int64_t total_allocated_byte_count = 0; - - AllocVec inlined_case(ScopedAlloc(Alloc(+&total_allocated_byte_count))); - - // Called only once to remain inlined - inlined_case.emplace_back(); - - int64_t absl_responsible_for_count = total_allocated_byte_count; - - // MSVC's allocator preemptively allocates in debug mode -#if !defined(_MSC_VER) - EXPECT_EQ(absl_responsible_for_count, 0); -#endif // !defined(_MSC_VER) - - inlined_case[0].emplace_back(); - EXPECT_GT(total_allocated_byte_count, absl_responsible_for_count); - - inlined_case.clear(); - inlined_case.shrink_to_fit(); - EXPECT_EQ(total_allocated_byte_count, 0); -} - -TEST(AllocatorSupportTest, ScopedAllocatorWorksAllocated) { - using StdVector = std::vector>; - using Alloc = CountingAllocator; - using ScopedAlloc = std::scoped_allocator_adaptor; - using AllocVec = absl::InlinedVector; - - int64_t total_allocated_byte_count = 0; - - AllocVec allocated_case(ScopedAlloc(Alloc(+&total_allocated_byte_count))); - - // Called twice to force into being allocated - allocated_case.emplace_back(); - allocated_case.emplace_back(); - - int64_t absl_responsible_for_count = total_allocated_byte_count; - EXPECT_GT(absl_responsible_for_count, 0); - - allocated_case[1].emplace_back(); - EXPECT_GT(total_allocated_byte_count, absl_responsible_for_count); - - allocated_case.clear(); - allocated_case.shrink_to_fit(); - EXPECT_EQ(total_allocated_byte_count, 0); -} - -TEST(AllocatorSupportTest, SizeAllocConstructor) { - constexpr int inlined_size = 4; - using Alloc = CountingAllocator; - using AllocVec = absl::InlinedVector; - - { - auto len = inlined_size / 2; - int64_t allocated = 0; - auto v = AllocVec(len, Alloc(&allocated)); - - // Inline storage used; allocator should not be invoked - EXPECT_THAT(allocated, 0); - EXPECT_THAT(v, AllOf(SizeIs(len), Each(0))); - } - - { - auto len = inlined_size * 2; - int64_t allocated = 0; - auto v = AllocVec(len, Alloc(&allocated)); - - // Out of line storage used; allocation of 8 elements expected - EXPECT_THAT(allocated, len * sizeof(int)); - EXPECT_THAT(v, AllOf(SizeIs(len), Each(0))); - } -} - -TEST(InlinedVectorTest, MinimumAllocatorCompilesUsingTraits) { - using T = int; - using A = std::allocator; - using ATraits = absl::allocator_traits; - - struct MinimumAllocator { - using value_type = T; - - value_type* allocate(size_t n) { - A a; - return ATraits::allocate(a, n); - } - - void deallocate(value_type* p, size_t n) { - A a; - ATraits::deallocate(a, p, n); - } - }; - - absl::InlinedVector vec; - vec.emplace_back(); - vec.resize(0); -} - -TEST(InlinedVectorTest, AbslHashValueWorks) { - using V = absl::InlinedVector; - std::vector 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; - for (int j = 0; j < i; ++j) { - v.push_back(j); - } - cases.push_back(v); - v.resize(i % 4); - cases.push_back(v); - } - - EXPECT_TRUE(absl::VerifyTypeImplementsAbslHashCorrectly(cases)); -} - -} // anonymous namespace diff --git a/third_party/abseil_cpp/absl/container/internal/btree.h b/third_party/abseil_cpp/absl/container/internal/btree.h deleted file mode 100644 index f2fc31df8d41..000000000000 --- a/third_party/abseil_cpp/absl/container/internal/btree.h +++ /dev/null @@ -1,2587 +0,0 @@ -// 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. - -// A btree implementation of the STL set and map interfaces. A btree is smaller -// and generally also faster than STL set/map (refer to the benchmarks below). -// The red-black tree implementation of STL set/map has an overhead of 3 -// pointers (left, right and parent) plus the node color information for each -// stored value. So a set consumes 40 bytes for each value stored in -// 64-bit mode. This btree implementation stores multiple values on fixed -// size nodes (usually 256 bytes) and doesn't store child pointers for leaf -// nodes. The result is that a btree_set may use much less memory per -// stored value. For the random insertion benchmark in btree_bench.cc, a -// btree_set with node-size of 256 uses 5.1 bytes per stored value. -// -// The packing of multiple values on to each node of a btree has another effect -// besides better space utilization: better cache locality due to fewer cache -// lines being accessed. Better cache locality translates into faster -// operations. -// -// CAVEATS -// -// Insertions and deletions on a btree can cause splitting, merging or -// rebalancing of btree nodes. And even without these operations, insertions -// and deletions on a btree will move values around within a node. In both -// cases, the result is that insertions and deletions can invalidate iterators -// pointing to values other than the one being inserted/deleted. Therefore, this -// container does not provide pointer stability. This is notably different from -// STL set/map which takes care to not invalidate iterators on insert/erase -// except, of course, for iterators pointing to the value being erased. A -// partial workaround when erasing is available: erase() returns an iterator -// pointing to the item just after the one that was erased (or end() if none -// exists). - -#ifndef ABSL_CONTAINER_INTERNAL_BTREE_H_ -#define ABSL_CONTAINER_INTERNAL_BTREE_H_ - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include "absl/base/macros.h" -#include "absl/container/internal/common.h" -#include "absl/container/internal/compressed_tuple.h" -#include "absl/container/internal/container_memory.h" -#include "absl/container/internal/layout.h" -#include "absl/memory/memory.h" -#include "absl/meta/type_traits.h" -#include "absl/strings/cord.h" -#include "absl/strings/string_view.h" -#include "absl/types/compare.h" -#include "absl/utility/utility.h" - -namespace absl { -ABSL_NAMESPACE_BEGIN -namespace container_internal { - -// A helper class that indicates if the Compare parameter is a key-compare-to -// comparator. -template -using btree_is_key_compare_to = - std::is_convertible, - absl::weak_ordering>; - -struct StringBtreeDefaultLess { - using is_transparent = void; - - StringBtreeDefaultLess() = default; - - // Compatibility constructor. - StringBtreeDefaultLess(std::less) {} // NOLINT - StringBtreeDefaultLess(std::less) {} // NOLINT - - absl::weak_ordering operator()(absl::string_view lhs, - absl::string_view rhs) const { - return compare_internal::compare_result_as_ordering(lhs.compare(rhs)); - } - StringBtreeDefaultLess(std::less) {} // NOLINT - absl::weak_ordering operator()(const absl::Cord &lhs, - const absl::Cord &rhs) const { - return compare_internal::compare_result_as_ordering(lhs.Compare(rhs)); - } - absl::weak_ordering operator()(const absl::Cord &lhs, - absl::string_view rhs) const { - return compare_internal::compare_result_as_ordering(lhs.Compare(rhs)); - } - absl::weak_ordering operator()(absl::string_view lhs, - const absl::Cord &rhs) const { - return compare_internal::compare_result_as_ordering(-rhs.Compare(lhs)); - } -}; - -struct StringBtreeDefaultGreater { - using is_transparent = void; - - StringBtreeDefaultGreater() = default; - - StringBtreeDefaultGreater(std::greater) {} // NOLINT - StringBtreeDefaultGreater(std::greater) {} // NOLINT - - absl::weak_ordering operator()(absl::string_view lhs, - absl::string_view rhs) const { - return compare_internal::compare_result_as_ordering(rhs.compare(lhs)); - } - StringBtreeDefaultGreater(std::greater) {} // NOLINT - absl::weak_ordering operator()(const absl::Cord &lhs, - const absl::Cord &rhs) const { - return compare_internal::compare_result_as_ordering(rhs.Compare(lhs)); - } - absl::weak_ordering operator()(const absl::Cord &lhs, - absl::string_view rhs) const { - return compare_internal::compare_result_as_ordering(-lhs.Compare(rhs)); - } - absl::weak_ordering operator()(absl::string_view lhs, - const absl::Cord &rhs) const { - return compare_internal::compare_result_as_ordering(rhs.Compare(lhs)); - } -}; - -// A helper class to convert a boolean comparison into a three-way "compare-to" -// comparison that returns an `absl::weak_ordering`. This helper -// class is specialized for less, greater, -// less, greater, less, and -// greater. -// -// key_compare_to_adapter is provided so that btree users -// automatically get the more efficient compare-to code when using common -// Abseil string types with common comparison functors. -// These string-like specializations also turn on heterogeneous lookup by -// default. -template -struct key_compare_to_adapter { - using type = Compare; -}; - -template <> -struct key_compare_to_adapter> { - using type = StringBtreeDefaultLess; -}; - -template <> -struct key_compare_to_adapter> { - using type = StringBtreeDefaultGreater; -}; - -template <> -struct key_compare_to_adapter> { - using type = StringBtreeDefaultLess; -}; - -template <> -struct key_compare_to_adapter> { - using type = StringBtreeDefaultGreater; -}; - -template <> -struct key_compare_to_adapter> { - using type = StringBtreeDefaultLess; -}; - -template <> -struct key_compare_to_adapter> { - using type = StringBtreeDefaultGreater; -}; - -// Detects an 'absl_btree_prefer_linear_node_search' member. This is -// a protocol used as an opt-in or opt-out of linear search. -// -// For example, this would be useful for key types that wrap an integer -// and define their own cheap operator<(). For example: -// -// class K { -// public: -// using absl_btree_prefer_linear_node_search = std::true_type; -// ... -// private: -// friend bool operator<(K a, K b) { return a.k_ < b.k_; } -// int k_; -// }; -// -// btree_map m; // Uses linear search -// -// If T has the preference tag, then it has a preference. -// Btree will use the tag's truth value. -template -struct has_linear_node_search_preference : std::false_type {}; -template -struct prefers_linear_node_search : std::false_type {}; -template -struct has_linear_node_search_preference< - T, absl::void_t> - : std::true_type {}; -template -struct prefers_linear_node_search< - T, absl::void_t> - : T::absl_btree_prefer_linear_node_search {}; - -template -struct common_params { - // If Compare is a common comparator for a string-like type, then we adapt it - // to use heterogeneous lookup and to be a key-compare-to comparator. - using key_compare = typename key_compare_to_adapter::type; - // True when key_compare has been adapted to StringBtreeDefault{Less,Greater}. - using is_key_compare_adapted = - absl::negation>; - // A type which indicates if we have a key-compare-to functor or a plain old - // key-compare functor. - using is_key_compare_to = btree_is_key_compare_to; - - using allocator_type = Alloc; - using key_type = Key; - using size_type = std::make_signed::type; - using difference_type = ptrdiff_t; - - // True if this is a multiset or multimap. - using is_multi_container = std::integral_constant; - - using slot_policy = SlotPolicy; - using slot_type = typename slot_policy::slot_type; - using value_type = typename slot_policy::value_type; - using init_type = typename slot_policy::mutable_value_type; - using pointer = value_type *; - using const_pointer = const value_type *; - using reference = value_type &; - using const_reference = const value_type &; - - enum { - kTargetNodeSize = TargetNodeSize, - - // Upper bound for the available space for values. This is largest for leaf - // nodes, which have overhead of at least a pointer + 4 bytes (for storing - // 3 field_types and an enum). - kNodeValueSpace = - TargetNodeSize - /*minimum overhead=*/(sizeof(void *) + 4), - }; - - // This is an integral type large enough to hold as many - // ValueSize-values as will fit a node of TargetNodeSize bytes. - using node_count_type = - absl::conditional_t<(kNodeValueSpace / sizeof(value_type) > - (std::numeric_limits::max)()), - uint16_t, uint8_t>; // NOLINT - - // The following methods are necessary for passing this struct as PolicyTraits - // for node_handle and/or are used within btree. - static value_type &element(slot_type *slot) { - return slot_policy::element(slot); - } - static const value_type &element(const slot_type *slot) { - return slot_policy::element(slot); - } - template - static void construct(Alloc *alloc, slot_type *slot, Args &&... args) { - slot_policy::construct(alloc, slot, std::forward(args)...); - } - static void construct(Alloc *alloc, slot_type *slot, slot_type *other) { - slot_policy::construct(alloc, slot, other); - } - static void destroy(Alloc *alloc, slot_type *slot) { - slot_policy::destroy(alloc, slot); - } - static void transfer(Alloc *alloc, slot_type *new_slot, slot_type *old_slot) { - construct(alloc, new_slot, old_slot); - destroy(alloc, old_slot); - } - static void swap(Alloc *alloc, slot_type *a, slot_type *b) { - slot_policy::swap(alloc, a, b); - } - static void move(Alloc *alloc, slot_type *src, slot_type *dest) { - slot_policy::move(alloc, src, dest); - } -}; - -// A parameters structure for holding the type parameters for a btree_map. -// Compare and Alloc should be nothrow copy-constructible. -template -struct map_params : common_params> { - 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; - - using key_compare = typename super_type::key_compare; - // Inherit from key_compare for empty base class optimization. - struct value_compare : private key_compare { - value_compare() = default; - explicit value_compare(const key_compare &cmp) : key_compare(cmp) {} - - template - auto operator()(const T &left, const U &right) const - -> decltype(std::declval()(left.first, right.first)) { - return key_compare::operator()(left.first, right.first); - } - }; - using is_map_container = std::true_type; - - template - static auto key(const V &value) -> 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; } -}; - -// This type implements the necessary functions from the -// absl::container_internal::slot_type interface. -template -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 - static void construct(Alloc *alloc, slot_type *slot, Args &&... args) { - absl::allocator_traits::construct(*alloc, slot, - std::forward(args)...); - } - - template - static void construct(Alloc *alloc, slot_type *slot, slot_type *other) { - absl::allocator_traits::construct(*alloc, slot, std::move(*other)); - } - - template - static void destroy(Alloc *alloc, slot_type *slot) { - absl::allocator_traits::destroy(*alloc, slot); - } - - template - static void swap(Alloc * /*alloc*/, slot_type *a, slot_type *b) { - using std::swap; - swap(*a, *b); - } - - template - static void move(Alloc * /*alloc*/, slot_type *src, slot_type *dest) { - *dest = std::move(*src); - } -}; - -// A parameters structure for holding the type parameters for a btree_set. -// Compare and Alloc should be nothrow copy-constructible. -template -struct set_params : common_params> { - using value_type = Key; - using slot_type = typename set_params::common_params::slot_type; - using value_compare = typename set_params::common_params::key_compare; - using is_map_container = std::false_type; - - template - 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; } -}; - -// An adapter class that converts a lower-bound compare into an upper-bound -// compare. Note: there is no need to make a version of this adapter specialized -// for key-compare-to functors because the upper-bound (the first value greater -// than the input) is never an exact match. -template -struct upper_bound_adapter { - explicit upper_bound_adapter(const Compare &c) : comp(c) {} - template - bool operator()(const K1 &a, const K2 &b) const { - // Returns true when a is not greater than b. - return !compare_internal::compare_result_as_less_than(comp(b, a)); - } - - private: - Compare comp; -}; - -enum class MatchKind : uint8_t { kEq, kNe }; - -template -struct SearchResult { - V value; - MatchKind match; - - static constexpr bool HasMatch() { return true; } - bool IsEq() const { return match == MatchKind::kEq; } -}; - -// When we don't use CompareTo, `match` is not present. -// This ensures that callers can't use it accidentally when it provides no -// useful information. -template -struct SearchResult { - SearchResult() {} - explicit SearchResult(V value) : value(value) {} - SearchResult(V value, MatchKind /*match*/) : value(value) {} - - V value; - - static constexpr bool HasMatch() { return false; } - static constexpr bool IsEq() { return false; } -}; - -// A node in the btree holding. The same node type is used for both internal -// and leaf nodes in the btree, though the nodes are allocated in such a way -// that the children array is only valid in internal nodes. -template -class btree_node { - using is_key_compare_to = typename Params::is_key_compare_to; - using is_multi_container = typename Params::is_multi_container; - using field_type = typename Params::node_count_type; - using allocator_type = typename Params::allocator_type; - using slot_type = typename Params::slot_type; - - public: - using params_type = Params; - using key_type = typename Params::key_type; - using value_type = typename Params::value_type; - using pointer = typename Params::pointer; - using const_pointer = typename Params::const_pointer; - using reference = typename Params::reference; - using const_reference = typename Params::const_reference; - using key_compare = typename Params::key_compare; - using size_type = typename Params::size_type; - using difference_type = typename Params::difference_type; - - // Btree decides whether to use linear node search as follows: - // - If the comparator expresses a preference, use that. - // - If the key expresses a preference, use that. - // - If the key is arithmetic and the comparator is std::less or - // std::greater, choose linear. - // - Otherwise, choose binary. - // TODO(ezb): Might make sense to add condition(s) based on node-size. - using use_linear_search = std::integral_constant< - bool, - has_linear_node_search_preference::value - ? prefers_linear_node_search::value - : has_linear_node_search_preference::value - ? prefers_linear_node_search::value - : std::is_arithmetic::value && - (std::is_same, key_compare>::value || - std::is_same, - key_compare>::value)>; - - // This class is organized by gtl::Layout as if it had the following - // structure: - // // A pointer to the node's parent. - // btree_node *parent; - // - // // The position of the node in the node's parent. - // field_type position; - // // The index of the first populated value in `values`. - // // TODO(ezb): right now, `start` is always 0. Update insertion/merge - // // logic to allow for floating storage within nodes. - // field_type start; - // // The index after the last populated value in `values`. Currently, this - // // is the same as the count of values. - // field_type finish; - // // The maximum number of values the node can hold. This is an integer in - // // [1, kNodeValues] for root leaf nodes, kNodeValues for non-root leaf - // // nodes, and kInternalNodeMaxCount (as a sentinel value) for internal - // // nodes (even though there are still kNodeValues values in the node). - // // TODO(ezb): make max_count use only 4 bits and record log2(capacity) - // // to free extra bits for is_root, etc. - // field_type max_count; - // - // // The array of values. The capacity is `max_count` for leaf nodes and - // // kNodeValues for internal nodes. Only the values in - // // [start, finish) have been initialized and are valid. - // slot_type values[max_count]; - // - // // The array of child pointers. The keys in children[i] are all less - // // than key(i). The keys in children[i + 1] are all greater than key(i). - // // There are 0 children for leaf nodes and kNodeValues + 1 children for - // // internal nodes. - // btree_node *children[kNodeValues + 1]; - // - // This class is only constructed by EmptyNodeType. Normally, pointers to the - // layout above are allocated, cast to btree_node*, and de-allocated within - // the btree implementation. - ~btree_node() = default; - btree_node(btree_node const &) = delete; - btree_node &operator=(btree_node const &) = delete; - - // Public for EmptyNodeType. - constexpr static size_type Alignment() { - static_assert(LeafLayout(1).Alignment() == InternalLayout().Alignment(), - "Alignment of all nodes must be equal."); - return InternalLayout().Alignment(); - } - - protected: - btree_node() = default; - - private: - using layout_type = absl::container_internal::Layout; - constexpr static size_type SizeWithNValues(size_type n) { - return layout_type(/*parent*/ 1, - /*position, start, finish, max_count*/ 4, - /*values*/ n, - /*children*/ 0) - .AllocSize(); - } - // A lower bound for the overhead of fields other than values in a leaf node. - constexpr static size_type MinimumOverhead() { - return SizeWithNValues(1) - sizeof(value_type); - } - - // Compute how many values we can fit onto a leaf node taking into account - // padding. - constexpr static size_type NodeTargetValues(const int begin, const int end) { - return begin == end ? begin - : SizeWithNValues((begin + end) / 2 + 1) > - params_type::kTargetNodeSize - ? NodeTargetValues(begin, (begin + end) / 2) - : NodeTargetValues((begin + end) / 2 + 1, end); - } - - enum { - kTargetNodeSize = params_type::kTargetNodeSize, - kNodeTargetValues = NodeTargetValues(0, params_type::kTargetNodeSize), - - // We need a minimum of 3 values per internal node in order to perform - // splitting (1 value for the two nodes involved in the split and 1 value - // propagated to the parent as the delimiter for the split). - kNodeValues = kNodeTargetValues >= 3 ? kNodeTargetValues : 3, - - // The node is internal (i.e. is not a leaf node) if and only if `max_count` - // has this value. - kInternalNodeMaxCount = 0, - }; - - // Leaves can have less than kNodeValues values. - constexpr static layout_type LeafLayout(const int max_values = kNodeValues) { - return layout_type(/*parent*/ 1, - /*position, start, finish, max_count*/ 4, - /*values*/ max_values, - /*children*/ 0); - } - constexpr static layout_type InternalLayout() { - return layout_type(/*parent*/ 1, - /*position, start, finish, max_count*/ 4, - /*values*/ kNodeValues, - /*children*/ kNodeValues + 1); - } - constexpr static size_type LeafSize(const int max_values = kNodeValues) { - return LeafLayout(max_values).AllocSize(); - } - constexpr static size_type InternalSize() { - return InternalLayout().AllocSize(); - } - - // N is the index of the type in the Layout definition. - // ElementType is the Nth type in the Layout definition. - template - inline typename layout_type::template ElementType *GetField() { - // We assert that we don't read from values that aren't there. - assert(N < 3 || !leaf()); - return InternalLayout().template Pointer(reinterpret_cast(this)); - } - template - inline const typename layout_type::template ElementType *GetField() const { - assert(N < 3 || !leaf()); - return InternalLayout().template Pointer( - reinterpret_cast(this)); - } - void set_parent(btree_node *p) { *GetField<0>() = p; } - field_type &mutable_finish() { return GetField<1>()[2]; } - slot_type *slot(int i) { return &GetField<2>()[i]; } - slot_type *start_slot() { return slot(start()); } - slot_type *finish_slot() { return slot(finish()); } - const slot_type *slot(int i) const { return &GetField<2>()[i]; } - void set_position(field_type v) { GetField<1>()[0] = v; } - void set_start(field_type v) { GetField<1>()[1] = v; } - void set_finish(field_type v) { GetField<1>()[2] = v; } - // This method is only called by the node init methods. - void set_max_count(field_type v) { GetField<1>()[3] = v; } - - public: - // Whether this is a leaf node or not. This value doesn't change after the - // node is created. - bool leaf() const { return GetField<1>()[3] != kInternalNodeMaxCount; } - - // Getter for the position of this node in its parent. - field_type position() const { return GetField<1>()[0]; } - - // Getter for the offset of the first value in the `values` array. - field_type start() const { - // TODO(ezb): when floating storage is implemented, return GetField<1>()[1]; - assert(GetField<1>()[1] == 0); - return 0; - } - - // Getter for the offset after the last value in the `values` array. - field_type finish() const { return GetField<1>()[2]; } - - // Getters for the number of values stored in this node. - field_type count() const { - assert(finish() >= start()); - return finish() - start(); - } - field_type max_count() const { - // Internal nodes have max_count==kInternalNodeMaxCount. - // Leaf nodes have max_count in [1, kNodeValues]. - const field_type max_count = GetField<1>()[3]; - return max_count == field_type{kInternalNodeMaxCount} - ? field_type{kNodeValues} - : max_count; - } - - // Getter for the parent of this node. - btree_node *parent() const { return *GetField<0>(); } - // Getter for whether the node is the root of the tree. The parent of the - // root of the tree is the leftmost node in the tree which is guaranteed to - // be a leaf. - bool is_root() const { return parent()->leaf(); } - void make_root() { - assert(parent()->is_root()); - set_parent(parent()->parent()); - } - - // Getters for the key/value at position i in the node. - const key_type &key(int i) const { return params_type::key(slot(i)); } - reference value(int i) { return params_type::element(slot(i)); } - const_reference value(int i) const { return params_type::element(slot(i)); } - - // Getters/setter for the child at position i in the node. - btree_node *child(int i) const { return GetField<3>()[i]; } - btree_node *start_child() const { return child(start()); } - btree_node *&mutable_child(int i) { return GetField<3>()[i]; } - void clear_child(int i) { - absl::container_internal::SanitizerPoisonObject(&mutable_child(i)); - } - void set_child(int i, btree_node *c) { - absl::container_internal::SanitizerUnpoisonObject(&mutable_child(i)); - mutable_child(i) = c; - c->set_position(i); - } - void init_child(int i, btree_node *c) { - set_child(i, c); - c->set_parent(this); - } - - // Returns the position of the first value whose key is not less than k. - template - SearchResult lower_bound( - const K &k, const key_compare &comp) const { - return use_linear_search::value ? linear_search(k, comp) - : binary_search(k, comp); - } - // Returns the position of the first value whose key is greater than k. - template - int upper_bound(const K &k, const key_compare &comp) const { - auto upper_compare = upper_bound_adapter(comp); - return use_linear_search::value ? linear_search(k, upper_compare).value - : binary_search(k, upper_compare).value; - } - - template - SearchResult::value> - linear_search(const K &k, const Compare &comp) const { - return linear_search_impl(k, start(), finish(), comp, - btree_is_key_compare_to()); - } - - template - SearchResult::value> - binary_search(const K &k, const Compare &comp) const { - return binary_search_impl(k, start(), finish(), comp, - btree_is_key_compare_to()); - } - - // Returns the position of the first value whose key is not less than k using - // linear search performed using plain compare. - template - SearchResult linear_search_impl( - const K &k, int s, const int e, const Compare &comp, - std::false_type /* IsCompareTo */) const { - while (s < e) { - if (!comp(key(s), k)) { - break; - } - ++s; - } - return SearchResult{s}; - } - - // Returns the position of the first value whose key is not less than k using - // linear search performed using compare-to. - template - SearchResult linear_search_impl( - const K &k, int s, const int e, const Compare &comp, - std::true_type /* IsCompareTo */) const { - while (s < e) { - const absl::weak_ordering c = comp(key(s), k); - if (c == 0) { - return {s, MatchKind::kEq}; - } else if (c > 0) { - break; - } - ++s; - } - return {s, MatchKind::kNe}; - } - - // Returns the position of the first value whose key is not less than k using - // binary search performed using plain compare. - template - SearchResult binary_search_impl( - const K &k, int s, int e, const Compare &comp, - std::false_type /* IsCompareTo */) const { - while (s != e) { - const int mid = (s + e) >> 1; - if (comp(key(mid), k)) { - s = mid + 1; - } else { - e = mid; - } - } - return SearchResult{s}; - } - - // Returns the position of the first value whose key is not less than k using - // binary search performed using compare-to. - template - SearchResult binary_search_impl( - const K &k, int s, int e, const CompareTo &comp, - std::true_type /* IsCompareTo */) const { - if (is_multi_container::value) { - MatchKind exact_match = MatchKind::kNe; - while (s != e) { - const int mid = (s + e) >> 1; - const absl::weak_ordering c = comp(key(mid), k); - if (c < 0) { - s = mid + 1; - } else { - e = mid; - if (c == 0) { - // Need to return the first value whose key is not less than k, - // which requires continuing the binary search if this is a - // multi-container. - exact_match = MatchKind::kEq; - } - } - } - return {s, exact_match}; - } else { // Not a multi-container. - while (s != e) { - const int mid = (s + e) >> 1; - const absl::weak_ordering c = comp(key(mid), k); - if (c < 0) { - s = mid + 1; - } else if (c > 0) { - e = mid; - } else { - return {mid, MatchKind::kEq}; - } - } - return {s, MatchKind::kNe}; - } - } - - // Emplaces a value at position i, shifting all existing values and - // children at positions >= i to the right by 1. - template - void emplace_value(size_type i, allocator_type *alloc, Args &&... args); - - // Removes the values at positions [i, i + to_erase), shifting all existing - // values and children after that range to the left by to_erase. Clears all - // children between [i, i + to_erase). - void remove_values(field_type i, field_type to_erase, allocator_type *alloc); - - // Rebalances a node with its right sibling. - void rebalance_right_to_left(int to_move, btree_node *right, - allocator_type *alloc); - void rebalance_left_to_right(int to_move, btree_node *right, - allocator_type *alloc); - - // Splits a node, moving a portion of the node's values to its right sibling. - void split(int insert_position, btree_node *dest, allocator_type *alloc); - - // Merges a node with its right sibling, moving all of the values and the - // delimiting key in the parent node onto itself, and deleting the src node. - void merge(btree_node *src, allocator_type *alloc); - - // Node allocation/deletion routines. - void init_leaf(btree_node *parent, int max_count) { - set_parent(parent); - set_position(0); - set_start(0); - set_finish(0); - set_max_count(max_count); - absl::container_internal::SanitizerPoisonMemoryRegion( - start_slot(), max_count * sizeof(slot_type)); - } - void init_internal(btree_node *parent) { - init_leaf(parent, kNodeValues); - // Set `max_count` to a sentinel value to indicate that this node is - // internal. - set_max_count(kInternalNodeMaxCount); - absl::container_internal::SanitizerPoisonMemoryRegion( - &mutable_child(start()), (kNodeValues + 1) * sizeof(btree_node *)); - } - - static void deallocate(const size_type size, btree_node *node, - allocator_type *alloc) { - absl::container_internal::Deallocate(alloc, node, size); - } - - // Deletes a node and all of its children. - static void clear_and_delete(btree_node *node, allocator_type *alloc); - - private: - template - void value_init(const field_type i, allocator_type *alloc, Args &&... args) { - absl::container_internal::SanitizerUnpoisonObject(slot(i)); - params_type::construct(alloc, slot(i), std::forward(args)...); - } - void value_destroy(const field_type i, allocator_type *alloc) { - params_type::destroy(alloc, slot(i)); - absl::container_internal::SanitizerPoisonObject(slot(i)); - } - void value_destroy_n(const field_type i, const field_type n, - allocator_type *alloc) { - for (slot_type *s = slot(i), *end = slot(i + n); s != end; ++s) { - params_type::destroy(alloc, s); - absl::container_internal::SanitizerPoisonObject(s); - } - } - - static void transfer(slot_type *dest, slot_type *src, allocator_type *alloc) { - absl::container_internal::SanitizerUnpoisonObject(dest); - params_type::transfer(alloc, dest, src); - absl::container_internal::SanitizerPoisonObject(src); - } - - // Transfers value from slot `src_i` in `src_node` to slot `dest_i` in `this`. - void transfer(const size_type dest_i, const size_type src_i, - btree_node *src_node, allocator_type *alloc) { - transfer(slot(dest_i), src_node->slot(src_i), alloc); - } - - // Transfers `n` values starting at value `src_i` in `src_node` into the - // values starting at value `dest_i` in `this`. - void transfer_n(const size_type n, const size_type dest_i, - const size_type src_i, btree_node *src_node, - allocator_type *alloc) { - for (slot_type *src = src_node->slot(src_i), *end = src + n, - *dest = slot(dest_i); - src != end; ++src, ++dest) { - transfer(dest, src, alloc); - } - } - - // Same as above, except that we start at the end and work our way to the - // beginning. - void transfer_n_backward(const size_type n, const size_type dest_i, - const size_type src_i, btree_node *src_node, - allocator_type *alloc) { - for (slot_type *src = src_node->slot(src_i + n - 1), *end = src - n, - *dest = slot(dest_i + n - 1); - src != end; --src, --dest) { - transfer(dest, src, alloc); - } - } - - template - friend class btree; - template - friend struct btree_iterator; - friend class BtreeNodePeer; -}; - -template -struct btree_iterator { - private: - using key_type = typename Node::key_type; - using size_type = typename Node::size_type; - using params_type = typename Node::params_type; - - using node_type = Node; - using normal_node = typename std::remove_const::type; - using const_node = const Node; - using normal_pointer = typename params_type::pointer; - using normal_reference = typename params_type::reference; - using const_pointer = typename params_type::const_pointer; - using const_reference = typename params_type::const_reference; - using slot_type = typename params_type::slot_type; - - using iterator = - btree_iterator; - using const_iterator = - btree_iterator; - - public: - // These aliases are public for std::iterator_traits. - using difference_type = typename Node::difference_type; - using value_type = typename params_type::value_type; - using pointer = Pointer; - using reference = Reference; - using iterator_category = std::bidirectional_iterator_tag; - - btree_iterator() : node(nullptr), position(-1) {} - explicit btree_iterator(Node *n) : node(n), position(n->start()) {} - btree_iterator(Node *n, int p) : node(n), position(p) {} - - // NOTE: this SFINAE allows for implicit conversions from iterator to - // const_iterator, but it specifically avoids defining copy constructors so - // that btree_iterator can be trivially copyable. This is for performance and - // binary size reasons. - template , iterator>::value && - std::is_same::value, - int> = 0> - btree_iterator(const btree_iterator &other) // NOLINT - : node(other.node), position(other.position) {} - - private: - // This SFINAE allows explicit conversions from const_iterator to - // iterator, but also avoids defining a copy constructor. - // NOTE: the const_cast is safe because this constructor is only called by - // non-const methods and the container owns the nodes. - template , const_iterator>::value && - std::is_same::value, - int> = 0> - explicit btree_iterator(const btree_iterator &other) - : node(const_cast(other.node)), position(other.position) {} - - // Increment/decrement the iterator. - void increment() { - if (node->leaf() && ++position < node->finish()) { - return; - } - increment_slow(); - } - void increment_slow(); - - void decrement() { - if (node->leaf() && --position >= node->start()) { - return; - } - decrement_slow(); - } - void decrement_slow(); - - public: - bool operator==(const iterator &other) const { - return node == other.node && position == other.position; - } - bool operator==(const const_iterator &other) const { - return node == other.node && position == other.position; - } - bool operator!=(const iterator &other) const { - return node != other.node || position != other.position; - } - bool operator!=(const const_iterator &other) const { - return node != other.node || position != other.position; - } - - // Accessors for the key/value the iterator is pointing at. - reference operator*() const { - ABSL_HARDENING_ASSERT(node != nullptr); - ABSL_HARDENING_ASSERT(node->start() <= position); - ABSL_HARDENING_ASSERT(node->finish() > position); - return node->value(position); - } - pointer operator->() const { return &operator*(); } - - btree_iterator &operator++() { - increment(); - return *this; - } - btree_iterator &operator--() { - decrement(); - return *this; - } - btree_iterator operator++(int) { - btree_iterator tmp = *this; - ++*this; - return tmp; - } - btree_iterator operator--(int) { - btree_iterator tmp = *this; - --*this; - return tmp; - } - - private: - template - friend class btree; - template - friend class btree_container; - template - friend class btree_set_container; - template - friend class btree_map_container; - template - friend class btree_multiset_container; - template - friend struct btree_iterator; - template - friend class base_checker; - - const key_type &key() const { return node->key(position); } - slot_type *slot() { return node->slot(position); } - - // The node in the tree the iterator is pointing at. - Node *node; - // The position within the node of the tree the iterator is pointing at. - // NOTE: this is an int rather than a field_type because iterators can point - // to invalid positions (such as -1) in certain circumstances. - int position; -}; - -template -class btree { - using node_type = btree_node; - using is_key_compare_to = typename Params::is_key_compare_to; - using init_type = typename Params::init_type; - using field_type = typename node_type::field_type; - using is_multi_container = typename Params::is_multi_container; - using is_key_compare_adapted = typename Params::is_key_compare_adapted; - - // We use a static empty node for the root/leftmost/rightmost of empty btrees - // in order to avoid branching in begin()/end(). - struct alignas(node_type::Alignment()) EmptyNodeType : node_type { - using field_type = typename node_type::field_type; - node_type *parent; - field_type position = 0; - field_type start = 0; - field_type finish = 0; - // max_count must be != kInternalNodeMaxCount (so that this node is regarded - // as a leaf node). max_count() is never called when the tree is empty. - field_type max_count = node_type::kInternalNodeMaxCount + 1; - -#ifdef _MSC_VER - // MSVC has constexpr code generations bugs here. - EmptyNodeType() : parent(this) {} -#else - constexpr EmptyNodeType(node_type *p) : parent(p) {} -#endif - }; - - static node_type *EmptyNode() { -#ifdef _MSC_VER - static EmptyNodeType *empty_node = new EmptyNodeType; - // This assert fails on some other construction methods. - assert(empty_node->parent == empty_node); - return empty_node; -#else - static constexpr EmptyNodeType empty_node( - const_cast(&empty_node)); - return const_cast(&empty_node); -#endif - } - - enum : uint32_t { - kNodeValues = node_type::kNodeValues, - kMinNodeValues = kNodeValues / 2, - }; - - struct node_stats { - using size_type = typename Params::size_type; - - node_stats(size_type l, size_type i) : leaf_nodes(l), internal_nodes(i) {} - - node_stats &operator+=(const node_stats &other) { - leaf_nodes += other.leaf_nodes; - internal_nodes += other.internal_nodes; - return *this; - } - - size_type leaf_nodes; - size_type internal_nodes; - }; - - public: - using key_type = typename Params::key_type; - using value_type = typename Params::value_type; - using size_type = typename Params::size_type; - using difference_type = typename Params::difference_type; - using key_compare = typename Params::key_compare; - using value_compare = typename Params::value_compare; - using allocator_type = typename Params::allocator_type; - using reference = typename Params::reference; - using const_reference = typename Params::const_reference; - using pointer = typename Params::pointer; - using const_pointer = typename Params::const_pointer; - using iterator = btree_iterator; - using const_iterator = typename iterator::const_iterator; - using reverse_iterator = std::reverse_iterator; - using const_reverse_iterator = std::reverse_iterator; - using node_handle_type = node_handle; - - // Internal types made public for use by btree_container types. - using params_type = Params; - using slot_type = typename Params::slot_type; - - private: - // For use in copy_or_move_values_in_order. - const value_type &maybe_move_from_iterator(const_iterator it) { return *it; } - value_type &&maybe_move_from_iterator(iterator it) { return std::move(*it); } - - // Copies or moves (depending on the template parameter) the values in - // other into this btree in their order in other. This btree must be empty - // before this method is called. This method is used in copy construction, - // copy assignment, and move assignment. - template - void copy_or_move_values_in_order(Btree &other); - - // Validates that various assumptions/requirements are true at compile time. - constexpr static bool static_assert_validation(); - - public: - btree(const key_compare &comp, const allocator_type &alloc) - : root_(comp, alloc, EmptyNode()), rightmost_(EmptyNode()), size_(0) {} - - btree(const btree &other) : btree(other, other.allocator()) {} - btree(const btree &other, const allocator_type &alloc) - : btree(other.key_comp(), alloc) { - copy_or_move_values_in_order(other); - } - btree(btree &&other) noexcept - : root_(std::move(other.root_)), - rightmost_(absl::exchange(other.rightmost_, EmptyNode())), - size_(absl::exchange(other.size_, 0)) { - other.mutable_root() = EmptyNode(); - } - btree(btree &&other, const allocator_type &alloc) - : btree(other.key_comp(), alloc) { - if (alloc == other.allocator()) { - swap(other); - } else { - // Move values from `other` one at a time when allocators are different. - copy_or_move_values_in_order(other); - } - } - - ~btree() { - // Put static_asserts in destructor to avoid triggering them before the type - // is complete. - static_assert(static_assert_validation(), "This call must be elided."); - clear(); - } - - // Assign the contents of other to *this. - btree &operator=(const btree &other); - btree &operator=(btree &&other) noexcept; - - iterator begin() { return iterator(leftmost()); } - const_iterator begin() const { return const_iterator(leftmost()); } - iterator end() { return iterator(rightmost_, rightmost_->finish()); } - const_iterator end() const { - return const_iterator(rightmost_, rightmost_->finish()); - } - reverse_iterator rbegin() { return reverse_iterator(end()); } - const_reverse_iterator rbegin() const { - return const_reverse_iterator(end()); - } - reverse_iterator rend() { return reverse_iterator(begin()); } - const_reverse_iterator rend() const { - return const_reverse_iterator(begin()); - } - - // Finds the first element whose key is not less than key. - template - iterator lower_bound(const K &key) { - return internal_end(internal_lower_bound(key).value); - } - template - const_iterator lower_bound(const K &key) const { - return internal_end(internal_lower_bound(key).value); - } - - // Finds the first element whose key is greater than key. - template - iterator upper_bound(const K &key) { - return internal_end(internal_upper_bound(key)); - } - template - const_iterator upper_bound(const K &key) const { - return internal_end(internal_upper_bound(key)); - } - - // Finds the range of values which compare equal to key. The first member of - // the returned pair is equal to lower_bound(key). The second member of the - // pair is equal to upper_bound(key). - template - std::pair equal_range(const K &key); - template - std::pair equal_range(const K &key) const { - return const_cast(this)->equal_range(key); - } - - // Inserts a value into the btree only if it does not already exist. The - // boolean return value indicates whether insertion succeeded or failed. - // Requirement: if `key` already exists in the btree, does not consume `args`. - // Requirement: `key` is never referenced after consuming `args`. - template - std::pair insert_unique(const K &key, Args &&... args); - - // Inserts with hint. Checks to see if the value should be placed immediately - // before `position` in the tree. If so, then the insertion will take - // amortized constant time. If not, the insertion will take amortized - // logarithmic time as if a call to insert_unique() were made. - // Requirement: if `key` already exists in the btree, does not consume `args`. - // Requirement: `key` is never referenced after consuming `args`. - template - std::pair insert_hint_unique(iterator position, - const K &key, - Args &&... args); - - // Insert a range of values into the btree. - // Note: the first overload avoids constructing a value_type if the key - // already exists in the btree. - template ()( - params_type::key(*std::declval()), - std::declval()))> - void insert_iterator_unique(InputIterator b, InputIterator e, int); - // We need the second overload for cases in which we need to construct a - // value_type in order to compare it with the keys already in the btree. - template - void insert_iterator_unique(InputIterator b, InputIterator e, char); - - // Inserts a value into the btree. - template - iterator insert_multi(const key_type &key, ValueType &&v); - - // Inserts a value into the btree. - template - iterator insert_multi(ValueType &&v) { - return insert_multi(params_type::key(v), std::forward(v)); - } - - // Insert with hint. Check to see if the value should be placed immediately - // before position in the tree. If it does, then the insertion will take - // amortized constant time. If not, the insertion will take amortized - // logarithmic time as if a call to insert_multi(v) were made. - template - iterator insert_hint_multi(iterator position, ValueType &&v); - - // Insert a range of values into the btree. - template - void insert_iterator_multi(InputIterator b, InputIterator e); - - // 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). - // Requirement: does not read the value at `*iter`. - iterator erase(iterator iter); - - // Erases range. Returns the number of keys erased and an iterator pointing - // to the element after the last erased element. - std::pair erase_range(iterator begin, iterator end); - - // Finds the iterator corresponding to a key or returns end() if the key is - // not present. - template - iterator find(const K &key) { - return internal_end(internal_find(key)); - } - template - const_iterator find(const K &key) const { - return internal_end(internal_find(key)); - } - - // Clear the btree, deleting all of the values it contains. - void clear(); - - // Swaps the contents of `this` and `other`. - void swap(btree &other); - - const key_compare &key_comp() const noexcept { - return root_.template get<0>(); - } - template - bool compare_keys(const K1 &a, const K2 &b) const { - return compare_internal::compare_result_as_less_than(key_comp()(a, b)); - } - - value_compare value_comp() const { return value_compare(key_comp()); } - - // Verifies the structure of the btree. - void verify() const; - - // Size routines. - size_type size() const { return size_; } - size_type max_size() const { return (std::numeric_limits::max)(); } - bool empty() const { return size_ == 0; } - - // The height of the btree. An empty tree will have height 0. - size_type height() const { - size_type h = 0; - if (!empty()) { - // Count the length of the chain from the leftmost node up to the - // root. We actually count from the root back around to the level below - // the root, but the calculation is the same because of the circularity - // of that traversal. - const node_type *n = root(); - do { - ++h; - n = n->parent(); - } while (n != root()); - } - return h; - } - - // The number of internal, leaf and total nodes used by the btree. - size_type leaf_nodes() const { return internal_stats(root()).leaf_nodes; } - size_type internal_nodes() const { - return internal_stats(root()).internal_nodes; - } - size_type nodes() const { - node_stats stats = internal_stats(root()); - return stats.leaf_nodes + stats.internal_nodes; - } - - // The total number of bytes used by the btree. - size_type bytes_used() const { - node_stats stats = internal_stats(root()); - if (stats.leaf_nodes == 1 && stats.internal_nodes == 0) { - return sizeof(*this) + node_type::LeafSize(root()->max_count()); - } else { - return sizeof(*this) + stats.leaf_nodes * node_type::LeafSize() + - stats.internal_nodes * node_type::InternalSize(); - } - } - - // The average number of bytes used per value stored in the btree. - static double average_bytes_per_value() { - // Returns the number of bytes per value on a leaf node that is 75% - // full. Experimentally, this matches up nicely with the computed number of - // bytes per value in trees that had their values inserted in random order. - return node_type::LeafSize() / (kNodeValues * 0.75); - } - - // The fullness of the btree. Computed as the number of elements in the btree - // divided by the maximum number of elements a tree with the current number - // of nodes could hold. A value of 1 indicates perfect space - // utilization. Smaller values indicate space wastage. - // Returns 0 for empty trees. - double fullness() const { - if (empty()) return 0.0; - return static_cast(size()) / (nodes() * kNodeValues); - } - // The overhead of the btree structure in bytes per node. Computed as the - // total number of bytes used by the btree minus the number of bytes used for - // storing elements divided by the number of elements. - // Returns 0 for empty trees. - double overhead() const { - if (empty()) return 0.0; - return (bytes_used() - size() * sizeof(value_type)) / - static_cast(size()); - } - - // The allocator used by the btree. - allocator_type get_allocator() const { return allocator(); } - - private: - // Internal accessor routines. - node_type *root() { return root_.template get<2>(); } - const node_type *root() const { return root_.template get<2>(); } - node_type *&mutable_root() noexcept { return root_.template get<2>(); } - key_compare *mutable_key_comp() noexcept { return &root_.template get<0>(); } - - // The leftmost node is stored as the parent of the root node. - node_type *leftmost() { return root()->parent(); } - const node_type *leftmost() const { return root()->parent(); } - - // Allocator routines. - allocator_type *mutable_allocator() noexcept { - return &root_.template get<1>(); - } - const allocator_type &allocator() const noexcept { - return root_.template get<1>(); - } - - // Allocates a correctly aligned node of at least size bytes using the - // allocator. - node_type *allocate(const size_type size) { - return reinterpret_cast( - absl::container_internal::Allocate( - mutable_allocator(), size)); - } - - // Node creation/deletion routines. - node_type *new_internal_node(node_type *parent) { - node_type *n = allocate(node_type::InternalSize()); - n->init_internal(parent); - return n; - } - node_type *new_leaf_node(node_type *parent) { - node_type *n = allocate(node_type::LeafSize()); - n->init_leaf(parent, kNodeValues); - return n; - } - node_type *new_leaf_root_node(const int max_count) { - node_type *n = allocate(node_type::LeafSize(max_count)); - n->init_leaf(/*parent=*/n, max_count); - return n; - } - - // Deletion helper routines. - iterator rebalance_after_delete(iterator iter); - - // Rebalances or splits the node iter points to. - void rebalance_or_split(iterator *iter); - - // Merges the values of left, right and the delimiting key on their parent - // onto left, removing the delimiting key and deleting right. - void merge_nodes(node_type *left, node_type *right); - - // Tries to merge node with its left or right sibling, and failing that, - // rebalance with its left or right sibling. Returns true if a merge - // occurred, at which point it is no longer valid to access node. Returns - // false if no merging took place. - bool try_merge_or_rebalance(iterator *iter); - - // Tries to shrink the height of the tree by 1. - void try_shrink(); - - iterator internal_end(iterator iter) { - return iter.node != nullptr ? iter : end(); - } - const_iterator internal_end(const_iterator iter) const { - return iter.node != nullptr ? iter : end(); - } - - // Emplaces a value into the btree immediately before iter. Requires that - // key(v) <= iter.key() and (--iter).key() <= key(v). - template - iterator internal_emplace(iterator iter, Args &&... args); - - // Returns an iterator pointing to the first value >= the value "iter" is - // pointing at. Note that "iter" might be pointing to an invalid location such - // as iter.position == iter.node->finish(). This routine simply moves iter up - // in the tree to a valid location. - // Requires: iter.node is non-null. - template - static IterType internal_last(IterType iter); - - // Returns an iterator pointing to the leaf position at which key would - // reside in the tree, unless there is an exact match - in which case, the - // result may not be on a leaf. When there's a three-way comparator, we can - // return whether there was an exact match. This allows the caller to avoid a - // subsequent comparison to determine if an exact match was made, which is - // important for keys with expensive comparison, such as strings. - template - SearchResult internal_locate( - const K &key) const; - - // Internal routine which implements lower_bound(). - template - SearchResult internal_lower_bound( - const K &key) const; - - // Internal routine which implements upper_bound(). - template - iterator internal_upper_bound(const K &key) const; - - // Internal routine which implements find(). - template - iterator internal_find(const K &key) const; - - // Verifies the tree structure of node. - int internal_verify(const node_type *node, const key_type *lo, - const key_type *hi) const; - - node_stats internal_stats(const node_type *node) const { - // The root can be a static empty node. - if (node == nullptr || (node == root() && empty())) { - return node_stats(0, 0); - } - if (node->leaf()) { - return node_stats(1, 0); - } - node_stats res(0, 1); - for (int i = node->start(); i <= node->finish(); ++i) { - res += internal_stats(node->child(i)); - } - return res; - } - - // We use compressed tuple in order to save space because key_compare and - // allocator_type are usually empty. - absl::container_internal::CompressedTuple - root_; - - // A pointer to the rightmost node. Note that the leftmost node is stored as - // the root's parent. - node_type *rightmost_; - - // Number of values. - size_type size_; -}; - -//// -// btree_node methods -template -template -inline void btree_node

::emplace_value(const size_type i, - allocator_type *alloc, - Args &&... args) { - assert(i >= start()); - assert(i <= finish()); - // Shift old values to create space for new value and then construct it in - // place. - if (i < finish()) { - transfer_n_backward(finish() - i, /*dest_i=*/i + 1, /*src_i=*/i, this, - alloc); - } - value_init(i, alloc, std::forward(args)...); - set_finish(finish() + 1); - - if (!leaf() && finish() > i + 1) { - for (int j = finish(); j > i + 1; --j) { - set_child(j, child(j - 1)); - } - clear_child(i + 1); - } -} - -template -inline void btree_node

::remove_values(const field_type i, - const field_type to_erase, - allocator_type *alloc) { - // Transfer values after the removed range into their new places. - value_destroy_n(i, to_erase, alloc); - const field_type orig_finish = finish(); - const field_type src_i = i + to_erase; - transfer_n(orig_finish - src_i, i, src_i, this, alloc); - - if (!leaf()) { - // Delete all children between begin and end. - for (int j = 0; j < to_erase; ++j) { - clear_and_delete(child(i + j + 1), alloc); - } - // Rotate children after end into new positions. - for (int j = i + to_erase + 1; j <= orig_finish; ++j) { - set_child(j - to_erase, child(j)); - clear_child(j); - } - } - set_finish(orig_finish - to_erase); -} - -template -void btree_node

::rebalance_right_to_left(const int to_move, - btree_node *right, - allocator_type *alloc) { - assert(parent() == right->parent()); - assert(position() + 1 == right->position()); - assert(right->count() >= count()); - assert(to_move >= 1); - assert(to_move <= right->count()); - - // 1) Move the delimiting value in the parent to the left node. - transfer(finish(), position(), parent(), alloc); - - // 2) Move the (to_move - 1) values from the right node to the left node. - transfer_n(to_move - 1, finish() + 1, right->start(), right, alloc); - - // 3) Move the new delimiting value to the parent from the right node. - parent()->transfer(position(), right->start() + to_move - 1, right, alloc); - - // 4) Shift the values in the right node to their correct positions. - right->transfer_n(right->count() - to_move, right->start(), - right->start() + to_move, right, alloc); - - if (!leaf()) { - // Move the child pointers from the right to the left node. - for (int i = 0; i < to_move; ++i) { - init_child(finish() + i + 1, right->child(i)); - } - for (int i = right->start(); i <= right->finish() - to_move; ++i) { - assert(i + to_move <= right->max_count()); - right->init_child(i, right->child(i + to_move)); - right->clear_child(i + to_move); - } - } - - // Fixup `finish` on the left and right nodes. - set_finish(finish() + to_move); - right->set_finish(right->finish() - to_move); -} - -template -void btree_node

::rebalance_left_to_right(const int to_move, - btree_node *right, - allocator_type *alloc) { - assert(parent() == right->parent()); - assert(position() + 1 == right->position()); - assert(count() >= right->count()); - assert(to_move >= 1); - assert(to_move <= count()); - - // Values in the right node are shifted to the right to make room for the - // new to_move values. Then, the delimiting value in the parent and the - // other (to_move - 1) values in the left node are moved into the right node. - // Lastly, a new delimiting value is moved from the left node into the - // parent, and the remaining empty left node entries are destroyed. - - // 1) Shift existing values in the right node to their correct positions. - right->transfer_n_backward(right->count(), right->start() + to_move, - right->start(), right, alloc); - - // 2) Move the delimiting value in the parent to the right node. - right->transfer(right->start() + to_move - 1, position(), parent(), alloc); - - // 3) Move the (to_move - 1) values from the left node to the right node. - right->transfer_n(to_move - 1, right->start(), finish() - (to_move - 1), this, - alloc); - - // 4) Move the new delimiting value to the parent from the left node. - parent()->transfer(position(), finish() - to_move, this, alloc); - - if (!leaf()) { - // Move the child pointers from the left to the right node. - for (int i = right->finish(); i >= right->start(); --i) { - right->init_child(i + to_move, right->child(i)); - right->clear_child(i); - } - for (int i = 1; i <= to_move; ++i) { - right->init_child(i - 1, child(finish() - to_move + i)); - clear_child(finish() - to_move + i); - } - } - - // Fixup the counts on the left and right nodes. - set_finish(finish() - to_move); - right->set_finish(right->finish() + to_move); -} - -template -void btree_node

::split(const int insert_position, btree_node *dest, - allocator_type *alloc) { - assert(dest->count() == 0); - assert(max_count() == kNodeValues); - - // We bias the split based on the position being inserted. If we're - // inserting at the beginning of the left node then bias the split to put - // more values on the right node. If we're inserting at the end of the - // right node then bias the split to put more values on the left node. - if (insert_position == start()) { - dest->set_finish(dest->start() + finish() - 1); - } else if (insert_position == kNodeValues) { - dest->set_finish(dest->start()); - } else { - dest->set_finish(dest->start() + count() / 2); - } - set_finish(finish() - dest->count()); - assert(count() >= 1); - - // Move values from the left sibling to the right sibling. - dest->transfer_n(dest->count(), dest->start(), finish(), this, alloc); - - // The split key is the largest value in the left sibling. - --mutable_finish(); - parent()->emplace_value(position(), alloc, finish_slot()); - value_destroy(finish(), alloc); - parent()->init_child(position() + 1, dest); - - if (!leaf()) { - for (int i = dest->start(), j = finish() + 1; i <= dest->finish(); - ++i, ++j) { - assert(child(j) != nullptr); - dest->init_child(i, child(j)); - clear_child(j); - } - } -} - -template -void btree_node

::merge(btree_node *src, allocator_type *alloc) { - assert(parent() == src->parent()); - assert(position() + 1 == src->position()); - - // Move the delimiting value to the left node. - value_init(finish(), alloc, parent()->slot(position())); - - // Move the values from the right to the left node. - transfer_n(src->count(), finish() + 1, src->start(), src, alloc); - - if (!leaf()) { - // Move the child pointers from the right to the left node. - for (int i = src->start(), j = finish() + 1; i <= src->finish(); ++i, ++j) { - init_child(j, src->child(i)); - src->clear_child(i); - } - } - - // Fixup `finish` on the src and dest nodes. - set_finish(start() + 1 + count() + src->count()); - src->set_finish(src->start()); - - // Remove the value on the parent node and delete the src node. - parent()->remove_values(position(), /*to_erase=*/1, alloc); -} - -template -void btree_node

::clear_and_delete(btree_node *node, allocator_type *alloc) { - if (node->leaf()) { - node->value_destroy_n(node->start(), node->count(), alloc); - deallocate(LeafSize(node->max_count()), node, alloc); - return; - } - if (node->count() == 0) { - deallocate(InternalSize(), node, alloc); - return; - } - - // The parent of the root of the subtree we are deleting. - btree_node *delete_root_parent = node->parent(); - - // Navigate to the leftmost leaf under node, and then delete upwards. - while (!node->leaf()) node = node->start_child(); - // Use `int` because `pos` needs to be able to hold `kNodeValues+1`, which - // isn't guaranteed to be a valid `field_type`. - int pos = node->position(); - btree_node *parent = node->parent(); - for (;;) { - // In each iteration of the next loop, we delete one leaf node and go right. - assert(pos <= parent->finish()); - do { - node = parent->child(pos); - if (!node->leaf()) { - // Navigate to the leftmost leaf under node. - while (!node->leaf()) node = node->start_child(); - pos = node->position(); - parent = node->parent(); - } - node->value_destroy_n(node->start(), node->count(), alloc); - deallocate(LeafSize(node->max_count()), node, alloc); - ++pos; - } while (pos <= parent->finish()); - - // Once we've deleted all children of parent, delete parent and go up/right. - assert(pos > parent->finish()); - do { - node = parent; - pos = node->position(); - parent = node->parent(); - node->value_destroy_n(node->start(), node->count(), alloc); - deallocate(InternalSize(), node, alloc); - if (parent == delete_root_parent) return; - ++pos; - } while (pos > parent->finish()); - } -} - -//// -// btree_iterator methods -template -void btree_iterator::increment_slow() { - if (node->leaf()) { - assert(position >= node->finish()); - btree_iterator save(*this); - while (position == node->finish() && !node->is_root()) { - assert(node->parent()->child(node->position()) == node); - position = node->position(); - node = node->parent(); - } - // TODO(ezb): assert we aren't incrementing end() instead of handling. - if (position == node->finish()) { - *this = save; - } - } else { - assert(position < node->finish()); - node = node->child(position + 1); - while (!node->leaf()) { - node = node->start_child(); - } - position = node->start(); - } -} - -template -void btree_iterator::decrement_slow() { - if (node->leaf()) { - assert(position <= -1); - btree_iterator save(*this); - while (position < node->start() && !node->is_root()) { - assert(node->parent()->child(node->position()) == node); - position = node->position() - 1; - node = node->parent(); - } - // TODO(ezb): assert we aren't decrementing begin() instead of handling. - if (position < node->start()) { - *this = save; - } - } else { - assert(position >= node->start()); - node = node->child(position); - while (!node->leaf()) { - node = node->child(node->finish()); - } - position = node->finish() - 1; - } -} - -//// -// btree methods -template -template -void btree

::copy_or_move_values_in_order(Btree &other) { - static_assert(std::is_same::value || - std::is_same::value, - "Btree type must be same or const."); - assert(empty()); - - // We can avoid key comparisons because we know the order of the - // values is the same order we'll store them in. - auto iter = other.begin(); - if (iter == other.end()) return; - insert_multi(maybe_move_from_iterator(iter)); - ++iter; - for (; iter != other.end(); ++iter) { - // If the btree is not empty, we can just insert the new value at the end - // of the tree. - internal_emplace(end(), maybe_move_from_iterator(iter)); - } -} - -template -constexpr bool btree

::static_assert_validation() { - static_assert(std::is_nothrow_copy_constructible::value, - "Key comparison must be nothrow copy constructible"); - static_assert(std::is_nothrow_copy_constructible::value, - "Allocator must be nothrow copy constructible"); - static_assert(type_traits_internal::is_trivially_copyable::value, - "iterator not trivially copyable."); - - // Note: We assert that kTargetValues, which is computed from - // Params::kTargetNodeSize, must fit the node_type::field_type. - static_assert( - kNodeValues < (1 << (8 * sizeof(typename node_type::field_type))), - "target node size too large"); - - // Verify that key_compare returns an absl::{weak,strong}_ordering or bool. - using compare_result_type = - absl::result_of_t; - static_assert( - std::is_same::value || - std::is_convertible::value, - "key comparison function must return absl::{weak,strong}_ordering or " - "bool."); - - // Test the assumption made in setting kNodeValueSpace. - static_assert(node_type::MinimumOverhead() >= sizeof(void *) + 4, - "node space assumption incorrect"); - - return true; -} - -template -template -auto btree

::equal_range(const K &key) -> std::pair { - const SearchResult res = - internal_lower_bound(key); - const iterator lower = internal_end(res.value); - if (res.HasMatch() ? !res.IsEq() - : lower == end() || compare_keys(key, lower.key())) { - return {lower, lower}; - } - - const iterator next = std::next(lower); - // When the comparator is heterogeneous, we can't assume that comparison with - // non-`key_type` will be equivalent to `key_type` comparisons so there - // could be multiple equivalent keys even in a unique-container. But for - // heterogeneous comparisons from the default string adapted comparators, we - // don't need to worry about this. - if (!is_multi_container::value && - (std::is_same::value || is_key_compare_adapted::value)) { - // The next iterator after lower must point to a key greater than `key`. - // Note: if this assert fails, then it may indicate that the comparator does - // not meet the equivalence requirements for Compare - // (see https://en.cppreference.com/w/cpp/named_req/Compare). - assert(next == end() || compare_keys(key, next.key())); - return {lower, next}; - } - // Try once more to avoid the call to upper_bound() if there's only one - // equivalent key. This should prevent all calls to upper_bound() in cases of - // unique-containers with heterogeneous comparators in which all comparison - // operators have the same equivalence classes. - if (next == end() || compare_keys(key, next.key())) return {lower, next}; - - // In this case, we need to call upper_bound() to avoid worst case O(N) - // behavior if we were to iterate over equal keys. - return {lower, upper_bound(key)}; -} - -template -template -auto btree

::insert_unique(const K &key, Args &&... args) - -> std::pair { - if (empty()) { - mutable_root() = rightmost_ = new_leaf_root_node(1); - } - - SearchResult res = internal_locate(key); - iterator iter = res.value; - - if (res.HasMatch()) { - if (res.IsEq()) { - // The key already exists in the tree, do nothing. - return {iter, false}; - } - } else { - iterator last = internal_last(iter); - if (last.node && !compare_keys(key, last.key())) { - // The key already exists in the tree, do nothing. - return {last, false}; - } - } - return {internal_emplace(iter, std::forward(args)...), true}; -} - -template -template -inline auto btree

::insert_hint_unique(iterator position, const K &key, - Args &&... args) - -> std::pair { - if (!empty()) { - if (position == end() || compare_keys(key, position.key())) { - if (position == begin() || compare_keys(std::prev(position).key(), key)) { - // prev.key() < key < position.key() - return {internal_emplace(position, std::forward(args)...), true}; - } - } else if (compare_keys(position.key(), key)) { - ++position; - if (position == end() || compare_keys(key, position.key())) { - // {original `position`}.key() < key < {current `position`}.key() - return {internal_emplace(position, std::forward(args)...), true}; - } - } else { - // position.key() == key - return {position, false}; - } - } - return insert_unique(key, std::forward(args)...); -} - -template -template -void btree

::insert_iterator_unique(InputIterator b, InputIterator e, int) { - for (; b != e; ++b) { - insert_hint_unique(end(), params_type::key(*b), *b); - } -} - -template -template -void btree

::insert_iterator_unique(InputIterator b, InputIterator e, char) { - for (; b != e; ++b) { - init_type value(*b); - insert_hint_unique(end(), params_type::key(value), std::move(value)); - } -} - -template -template -auto btree

::insert_multi(const key_type &key, ValueType &&v) -> iterator { - if (empty()) { - mutable_root() = rightmost_ = new_leaf_root_node(1); - } - - iterator iter = internal_upper_bound(key); - if (iter.node == nullptr) { - iter = end(); - } - return internal_emplace(iter, std::forward(v)); -} - -template -template -auto btree

::insert_hint_multi(iterator position, ValueType &&v) -> iterator { - if (!empty()) { - const key_type &key = params_type::key(v); - if (position == end() || !compare_keys(position.key(), key)) { - if (position == begin() || - !compare_keys(key, std::prev(position).key())) { - // prev.key() <= key <= position.key() - return internal_emplace(position, std::forward(v)); - } - } else { - ++position; - if (position == end() || !compare_keys(position.key(), key)) { - // {original `position`}.key() < key < {current `position`}.key() - return internal_emplace(position, std::forward(v)); - } - } - } - return insert_multi(std::forward(v)); -} - -template -template -void btree

::insert_iterator_multi(InputIterator b, InputIterator e) { - for (; b != e; ++b) { - insert_hint_multi(end(), *b); - } -} - -template -auto btree

::operator=(const btree &other) -> btree & { - if (this != &other) { - clear(); - - *mutable_key_comp() = other.key_comp(); - if (absl::allocator_traits< - allocator_type>::propagate_on_container_copy_assignment::value) { - *mutable_allocator() = other.allocator(); - } - - copy_or_move_values_in_order(other); - } - return *this; -} - -template -auto btree

::operator=(btree &&other) noexcept -> btree & { - if (this != &other) { - clear(); - - using std::swap; - if (absl::allocator_traits< - allocator_type>::propagate_on_container_copy_assignment::value) { - // Note: `root_` also contains the allocator and the key comparator. - swap(root_, other.root_); - swap(rightmost_, other.rightmost_); - swap(size_, other.size_); - } else { - if (allocator() == other.allocator()) { - swap(mutable_root(), other.mutable_root()); - swap(*mutable_key_comp(), *other.mutable_key_comp()); - swap(rightmost_, other.rightmost_); - swap(size_, other.size_); - } else { - // We aren't allowed to propagate the allocator and the allocator is - // different so we can't take over its memory. We must move each element - // individually. We need both `other` and `this` to have `other`s key - // comparator while moving the values so we can't swap the key - // comparators. - *mutable_key_comp() = other.key_comp(); - copy_or_move_values_in_order(other); - } - } - } - return *this; -} - -template -auto btree

::erase(iterator iter) -> iterator { - bool internal_delete = false; - if (!iter.node->leaf()) { - // Deletion of a value on an internal node. First, move the largest value - // from our left child here, then delete that position (in remove_values() - // below). We can get to the largest value from our left child by - // decrementing iter. - iterator internal_iter(iter); - --iter; - assert(iter.node->leaf()); - params_type::move(mutable_allocator(), iter.node->slot(iter.position), - internal_iter.node->slot(internal_iter.position)); - internal_delete = true; - } - - // Delete the key from the leaf. - iter.node->remove_values(iter.position, /*to_erase=*/1, mutable_allocator()); - --size_; - - // We want to return the next value after the one we just erased. If we - // erased from an internal node (internal_delete == true), then the next - // value is ++(++iter). If we erased from a leaf node (internal_delete == - // false) then the next value is ++iter. Note that ++iter may point to an - // internal node and the value in the internal node may move to a leaf node - // (iter.node) when rebalancing is performed at the leaf level. - - iterator res = rebalance_after_delete(iter); - - // If we erased from an internal node, advance the iterator. - if (internal_delete) { - ++res; - } - return res; -} - -template -auto btree

::rebalance_after_delete(iterator iter) -> iterator { - // Merge/rebalance as we walk back up the tree. - iterator res(iter); - bool first_iteration = true; - for (;;) { - if (iter.node == root()) { - try_shrink(); - if (empty()) { - return end(); - } - break; - } - if (iter.node->count() >= kMinNodeValues) { - break; - } - bool merged = try_merge_or_rebalance(&iter); - // On the first iteration, we should update `res` with `iter` because `res` - // may have been invalidated. - if (first_iteration) { - res = iter; - first_iteration = false; - } - if (!merged) { - break; - } - iter.position = iter.node->position(); - iter.node = iter.node->parent(); - } - - // Adjust our return value. If we're pointing at the end of a node, advance - // the iterator. - if (res.position == res.node->finish()) { - res.position = res.node->finish() - 1; - ++res; - } - - return res; -} - -template -auto btree

::erase_range(iterator begin, iterator end) - -> std::pair { - difference_type count = std::distance(begin, end); - assert(count >= 0); - - if (count == 0) { - return {0, begin}; - } - - if (count == size_) { - clear(); - return {count, this->end()}; - } - - if (begin.node == end.node) { - assert(end.position > begin.position); - begin.node->remove_values(begin.position, end.position - begin.position, - mutable_allocator()); - size_ -= count; - return {count, rebalance_after_delete(begin)}; - } - - const size_type target_size = size_ - count; - while (size_ > target_size) { - if (begin.node->leaf()) { - const size_type remaining_to_erase = size_ - target_size; - const size_type remaining_in_node = begin.node->finish() - begin.position; - const size_type to_erase = - (std::min)(remaining_to_erase, remaining_in_node); - begin.node->remove_values(begin.position, to_erase, mutable_allocator()); - size_ -= to_erase; - begin = rebalance_after_delete(begin); - } else { - begin = erase(begin); - } - } - return {count, begin}; -} - -template -void btree

::clear() { - if (!empty()) { - node_type::clear_and_delete(root(), mutable_allocator()); - } - mutable_root() = EmptyNode(); - rightmost_ = EmptyNode(); - size_ = 0; -} - -template -void btree

::swap(btree &other) { - using std::swap; - if (absl::allocator_traits< - allocator_type>::propagate_on_container_swap::value) { - // Note: `root_` also contains the allocator and the key comparator. - swap(root_, other.root_); - } else { - // It's undefined behavior if the allocators are unequal here. - assert(allocator() == other.allocator()); - swap(mutable_root(), other.mutable_root()); - swap(*mutable_key_comp(), *other.mutable_key_comp()); - } - swap(rightmost_, other.rightmost_); - swap(size_, other.size_); -} - -template -void btree

::verify() const { - assert(root() != nullptr); - assert(leftmost() != nullptr); - assert(rightmost_ != nullptr); - assert(empty() || size() == internal_verify(root(), nullptr, nullptr)); - assert(leftmost() == (++const_iterator(root(), -1)).node); - assert(rightmost_ == (--const_iterator(root(), root()->finish())).node); - assert(leftmost()->leaf()); - assert(rightmost_->leaf()); -} - -template -void btree

::rebalance_or_split(iterator *iter) { - node_type *&node = iter->node; - int &insert_position = iter->position; - assert(node->count() == node->max_count()); - assert(kNodeValues == node->max_count()); - - // First try to make room on the node by rebalancing. - node_type *parent = node->parent(); - if (node != root()) { - if (node->position() > parent->start()) { - // Try rebalancing with our left sibling. - node_type *left = parent->child(node->position() - 1); - assert(left->max_count() == kNodeValues); - if (left->count() < kNodeValues) { - // We bias rebalancing based on the position being inserted. If we're - // inserting at the end of the right node then we bias rebalancing to - // fill up the left node. - int to_move = (kNodeValues - left->count()) / - (1 + (insert_position < static_cast(kNodeValues))); - to_move = (std::max)(1, to_move); - - if (insert_position - to_move >= node->start() || - left->count() + to_move < static_cast(kNodeValues)) { - left->rebalance_right_to_left(to_move, node, mutable_allocator()); - - assert(node->max_count() - node->count() == to_move); - insert_position = insert_position - to_move; - if (insert_position < node->start()) { - insert_position = insert_position + left->count() + 1; - node = left; - } - - assert(node->count() < node->max_count()); - return; - } - } - } - - if (node->position() < parent->finish()) { - // Try rebalancing with our right sibling. - node_type *right = parent->child(node->position() + 1); - assert(right->max_count() == kNodeValues); - if (right->count() < kNodeValues) { - // We bias rebalancing based on the position being inserted. If we're - // inserting at the beginning of the left node then we bias rebalancing - // to fill up the right node. - int to_move = (static_cast(kNodeValues) - right->count()) / - (1 + (insert_position > node->start())); - to_move = (std::max)(1, to_move); - - if (insert_position <= node->finish() - to_move || - right->count() + to_move < static_cast(kNodeValues)) { - node->rebalance_left_to_right(to_move, right, mutable_allocator()); - - if (insert_position > node->finish()) { - insert_position = insert_position - node->count() - 1; - node = right; - } - - assert(node->count() < node->max_count()); - return; - } - } - } - - // Rebalancing failed, make sure there is room on the parent node for a new - // value. - assert(parent->max_count() == kNodeValues); - if (parent->count() == kNodeValues) { - iterator parent_iter(node->parent(), node->position()); - rebalance_or_split(&parent_iter); - } - } else { - // Rebalancing not possible because this is the root node. - // Create a new root node and set the current root node as the child of the - // new root. - parent = new_internal_node(parent); - parent->init_child(parent->start(), root()); - mutable_root() = parent; - // If the former root was a leaf node, then it's now the rightmost node. - assert(!parent->start_child()->leaf() || - parent->start_child() == rightmost_); - } - - // Split the node. - node_type *split_node; - if (node->leaf()) { - split_node = new_leaf_node(parent); - node->split(insert_position, split_node, mutable_allocator()); - if (rightmost_ == node) rightmost_ = split_node; - } else { - split_node = new_internal_node(parent); - node->split(insert_position, split_node, mutable_allocator()); - } - - if (insert_position > node->finish()) { - insert_position = insert_position - node->count() - 1; - node = split_node; - } -} - -template -void btree

::merge_nodes(node_type *left, node_type *right) { - left->merge(right, mutable_allocator()); - if (rightmost_ == right) rightmost_ = left; -} - -template -bool btree

::try_merge_or_rebalance(iterator *iter) { - node_type *parent = iter->node->parent(); - if (iter->node->position() > parent->start()) { - // Try merging with our left sibling. - node_type *left = parent->child(iter->node->position() - 1); - assert(left->max_count() == kNodeValues); - if (1U + left->count() + iter->node->count() <= kNodeValues) { - iter->position += 1 + left->count(); - merge_nodes(left, iter->node); - iter->node = left; - return true; - } - } - if (iter->node->position() < parent->finish()) { - // Try merging with our right sibling. - node_type *right = parent->child(iter->node->position() + 1); - assert(right->max_count() == kNodeValues); - if (1U + iter->node->count() + right->count() <= kNodeValues) { - merge_nodes(iter->node, right); - return true; - } - // Try rebalancing with our right sibling. We don't perform rebalancing if - // we deleted the first element from iter->node and the node is not - // empty. This is a small optimization for the common pattern of deleting - // from the front of the tree. - if (right->count() > kMinNodeValues && - (iter->node->count() == 0 || iter->position > iter->node->start())) { - int to_move = (right->count() - iter->node->count()) / 2; - to_move = (std::min)(to_move, right->count() - 1); - iter->node->rebalance_right_to_left(to_move, right, mutable_allocator()); - return false; - } - } - if (iter->node->position() > parent->start()) { - // Try rebalancing with our left sibling. We don't perform rebalancing if - // we deleted the last element from iter->node and the node is not - // empty. This is a small optimization for the common pattern of deleting - // from the back of the tree. - node_type *left = parent->child(iter->node->position() - 1); - if (left->count() > kMinNodeValues && - (iter->node->count() == 0 || iter->position < iter->node->finish())) { - int to_move = (left->count() - iter->node->count()) / 2; - to_move = (std::min)(to_move, left->count() - 1); - left->rebalance_left_to_right(to_move, iter->node, mutable_allocator()); - iter->position += to_move; - return false; - } - } - return false; -} - -template -void btree

::try_shrink() { - node_type *orig_root = root(); - if (orig_root->count() > 0) { - return; - } - // Deleted the last item on the root node, shrink the height of the tree. - if (orig_root->leaf()) { - assert(size() == 0); - mutable_root() = rightmost_ = EmptyNode(); - } else { - node_type *child = orig_root->start_child(); - child->make_root(); - mutable_root() = child; - } - node_type::clear_and_delete(orig_root, mutable_allocator()); -} - -template -template -inline IterType btree

::internal_last(IterType iter) { - assert(iter.node != nullptr); - while (iter.position == iter.node->finish()) { - iter.position = iter.node->position(); - iter.node = iter.node->parent(); - if (iter.node->leaf()) { - iter.node = nullptr; - break; - } - } - return iter; -} - -template -template -inline auto btree

::internal_emplace(iterator iter, Args &&... args) - -> iterator { - if (!iter.node->leaf()) { - // We can't insert on an internal node. Instead, we'll insert after the - // previous value which is guaranteed to be on a leaf node. - --iter; - ++iter.position; - } - const field_type max_count = iter.node->max_count(); - allocator_type *alloc = mutable_allocator(); - if (iter.node->count() == max_count) { - // Make room in the leaf for the new item. - if (max_count < kNodeValues) { - // Insertion into the root where the root is smaller than the full node - // size. Simply grow the size of the root node. - assert(iter.node == root()); - iter.node = - new_leaf_root_node((std::min)(kNodeValues, 2 * max_count)); - // Transfer the values from the old root to the new root. - node_type *old_root = root(); - node_type *new_root = iter.node; - new_root->transfer_n(old_root->count(), new_root->start(), - old_root->start(), old_root, alloc); - new_root->set_finish(old_root->finish()); - old_root->set_finish(old_root->start()); - node_type::clear_and_delete(old_root, alloc); - mutable_root() = rightmost_ = new_root; - } else { - rebalance_or_split(&iter); - } - } - iter.node->emplace_value(iter.position, alloc, std::forward(args)...); - ++size_; - return iter; -} - -template -template -inline auto btree

::internal_locate(const K &key) const - -> SearchResult { - iterator iter(const_cast(root())); - for (;;) { - SearchResult res = - iter.node->lower_bound(key, key_comp()); - iter.position = res.value; - if (res.IsEq()) { - return {iter, MatchKind::kEq}; - } - // Note: in the non-key-compare-to case, we don't need to walk all the way - // down the tree if the keys are equal, but determining equality would - // require doing an extra comparison on each node on the way down, and we - // will need to go all the way to the leaf node in the expected case. - if (iter.node->leaf()) { - break; - } - iter.node = iter.node->child(iter.position); - } - // Note: in the non-key-compare-to case, the key may actually be equivalent - // here (and the MatchKind::kNe is ignored). - return {iter, MatchKind::kNe}; -} - -template -template -auto btree

::internal_lower_bound(const K &key) const - -> SearchResult { - iterator iter(const_cast(root())); - SearchResult res; - bool seen_eq = false; - for (;;) { - res = iter.node->lower_bound(key, key_comp()); - iter.position = res.value; - // TODO(ezb): we should be able to terminate early on IsEq() if there can't - // be multiple equivalent keys in container for this lookup type. - if (iter.node->leaf()) { - break; - } - seen_eq = seen_eq || res.IsEq(); - iter.node = iter.node->child(iter.position); - } - if (res.IsEq()) return {iter, MatchKind::kEq}; - return {internal_last(iter), seen_eq ? MatchKind::kEq : MatchKind::kNe}; -} - -template -template -auto btree

::internal_upper_bound(const K &key) const -> iterator { - iterator iter(const_cast(root())); - for (;;) { - iter.position = iter.node->upper_bound(key, key_comp()); - if (iter.node->leaf()) { - break; - } - iter.node = iter.node->child(iter.position); - } - return internal_last(iter); -} - -template -template -auto btree

::internal_find(const K &key) const -> iterator { - SearchResult res = internal_locate(key); - if (res.HasMatch()) { - if (res.IsEq()) { - return res.value; - } - } else { - const iterator iter = internal_last(res.value); - if (iter.node != nullptr && !compare_keys(key, iter.key())) { - return iter; - } - } - return {nullptr, 0}; -} - -template -int btree

::internal_verify(const node_type *node, const key_type *lo, - const key_type *hi) const { - assert(node->count() > 0); - assert(node->count() <= node->max_count()); - if (lo) { - assert(!compare_keys(node->key(node->start()), *lo)); - } - if (hi) { - assert(!compare_keys(*hi, node->key(node->finish() - 1))); - } - for (int i = node->start() + 1; i < node->finish(); ++i) { - assert(!compare_keys(node->key(i), node->key(i - 1))); - } - int count = node->count(); - if (!node->leaf()) { - for (int i = node->start(); i <= node->finish(); ++i) { - assert(node->child(i) != nullptr); - assert(node->child(i)->parent() == node); - assert(node->child(i)->position() == i); - count += internal_verify(node->child(i), - i == node->start() ? lo : &node->key(i - 1), - i == node->finish() ? hi : &node->key(i)); - } - } - return count; -} - -} // namespace container_internal -ABSL_NAMESPACE_END -} // namespace absl - -#endif // ABSL_CONTAINER_INTERNAL_BTREE_H_ diff --git a/third_party/abseil_cpp/absl/container/internal/btree_container.h b/third_party/abseil_cpp/absl/container/internal/btree_container.h deleted file mode 100644 index 887eda4122f8..000000000000 --- a/third_party/abseil_cpp/absl/container/internal/btree_container.h +++ /dev/null @@ -1,683 +0,0 @@ -// 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 -#include -#include -#include - -#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 -class btree_container { - using params_type = typename Tree::params_type; - - protected: - // Alias used for heterogeneous lookup functions. - // `key_arg` 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 - using key_arg = - typename KeyArg::value>:: - template 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::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; - - // 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:: - 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::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::value) = default; - - // Iterator routines. - iterator begin() { return tree_.begin(); } - const_iterator begin() const { return tree_.begin(); } - const_iterator cbegin() const { return tree_.begin(); } - iterator end() { return tree_.end(); } - const_iterator end() const { return tree_.end(); } - const_iterator cend() const { return tree_.end(); } - reverse_iterator rbegin() { return tree_.rbegin(); } - const_reverse_iterator rbegin() const { return tree_.rbegin(); } - const_reverse_iterator crbegin() const { return tree_.rbegin(); } - reverse_iterator rend() { return tree_.rend(); } - const_reverse_iterator rend() const { return tree_.rend(); } - const_reverse_iterator crend() const { return tree_.rend(); } - - // Lookup routines. - template - size_type count(const key_arg &key) const { - auto equal_range = this->equal_range(key); - return std::distance(equal_range.first, equal_range.second); - } - template - iterator find(const key_arg &key) { - return tree_.find(key); - } - template - const_iterator find(const key_arg &key) const { - return tree_.find(key); - } - template - bool contains(const key_arg &key) const { - return find(key) != end(); - } - template - iterator lower_bound(const key_arg &key) { - return tree_.lower_bound(key); - } - template - const_iterator lower_bound(const key_arg &key) const { - return tree_.lower_bound(key); - } - template - iterator upper_bound(const key_arg &key) { - return tree_.upper_bound(key); - } - template - const_iterator upper_bound(const key_arg &key) const { - return tree_.upper_bound(key); - } - template - std::pair equal_range(const key_arg &key) { - return tree_.equal_range(key); - } - template - std::pair equal_range( - const key_arg &key) const { - 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) { return tree_.erase(iterator(iter)); } - iterator erase(iterator iter) { return tree_.erase(iter); } - iterator erase(const_iterator first, const_iterator last) { - return tree_.erase_range(iterator(first), iterator(last)).second; - } - template - size_type erase(const key_arg &key) { - auto equal_range = this->equal_range(key); - return tree_.erase_range(equal_range.first, equal_range.second).first; - } - - // Extract routines. - node_type extract(iterator position) { - // Use Move instead of Transfer, because the rebalancing code expects to - // have a valid object to scribble metadata bits on top of. - auto node = CommonAccess::Move(get_allocator(), position.slot()); - erase(position); - return node; - } - node_type extract(const_iterator position) { - return extract(iterator(position)); - } - - // Utility routines. - 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 tree_.key_comp(); } - value_compare value_comp() const { return tree_.value_comp(); } - - // Support absl::Hash. - template - 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: - Tree tree_; -}; - -// A common base class for btree_set and btree_map. -template -class btree_set_container : public btree_container { - using super_type = btree_container; - 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 - using key_arg = typename super_type::template key_arg; - - 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::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; - - // Inherit constructors. - using super_type::super_type; - btree_set_container() {} - - // Range constructors. - template - 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 - 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, - 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, - const allocator_type &alloc) - : btree_set_container(init.begin(), init.end(), alloc) {} - - // Insertion routines. - std::pair insert(const value_type &v) { - return this->tree_.insert_unique(params_type::key(v), v); - } - std::pair insert(value_type &&v) { - return this->tree_.insert_unique(params_type::key(v), std::move(v)); - } - template - std::pair emplace(Args &&... args) { - init_type v(std::forward(args)...); - return this->tree_.insert_unique(params_type::key(v), std::move(v)); - } - iterator insert(const_iterator hint, const value_type &v) { - return this->tree_ - .insert_hint_unique(iterator(hint), params_type::key(v), v) - .first; - } - iterator insert(const_iterator hint, value_type &&v) { - return this->tree_ - .insert_hint_unique(iterator(hint), params_type::key(v), std::move(v)) - .first; - } - template - iterator emplace_hint(const_iterator hint, Args &&... args) { - init_type v(std::forward(args)...); - return this->tree_ - .insert_hint_unique(iterator(hint), params_type::key(v), std::move(v)) - .first; - } - template - void insert(InputIterator b, InputIterator e) { - this->tree_.insert_iterator_unique(b, e, 0); - } - void insert(std::initializer_list init) { - this->tree_.insert_iterator_unique(init.begin(), init.end(), 0); - } - insert_return_type insert(node_type &&node) { - if (!node) return {this->end(), false, node_type()}; - std::pair 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) { - if (!node) return this->end(); - std::pair 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. - // TODO(ezb): when the comparator is heterogeneous and has different - // equivalence classes for different lookup types, we should extract the first - // equivalent value if there are multiple. - template - node_type extract(const key_arg &key) { - auto it = this->find(key); - return it == this->end() ? node_type() : extract(it); - } - 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, - std::is_same, - std::is_same>::value, - int> = 0> - void merge(btree_container &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, - std::is_same, - std::is_same>::value, - int> = 0> - void merge(btree_container &&src) { - merge(src); - } -}; - -// Base class for btree_map. -template -class btree_map_container : public btree_set_container { - using super_type = btree_set_container; - using params_type = typename Tree::params_type; - friend class BtreeNodePeer; - - private: - template - using key_arg = typename super_type::template key_arg; - - 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::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 - std::pair insert_or_assign(const key_arg &k, - const M &obj) { - return insert_or_assign_impl(k, obj); - } - template - std::pair insert_or_assign(key_arg &&k, const M &obj) { - return insert_or_assign_impl(std::forward(k), obj); - } - template - std::pair insert_or_assign(const key_arg &k, M &&obj) { - return insert_or_assign_impl(k, std::forward(obj)); - } - template - std::pair insert_or_assign(key_arg &&k, M &&obj) { - return insert_or_assign_impl(std::forward(k), std::forward(obj)); - } - template - iterator insert_or_assign(const_iterator hint, const key_arg &k, - const M &obj) { - return insert_or_assign_hint_impl(hint, k, obj); - } - template - iterator insert_or_assign(const_iterator hint, key_arg &&k, const M &obj) { - return insert_or_assign_hint_impl(hint, std::forward(k), obj); - } - template - iterator insert_or_assign(const_iterator hint, const key_arg &k, M &&obj) { - return insert_or_assign_hint_impl(hint, k, std::forward(obj)); - } - template - iterator insert_or_assign(const_iterator hint, key_arg &&k, M &&obj) { - return insert_or_assign_hint_impl(hint, std::forward(k), - std::forward(obj)); - } - - template ::value, int> = 0> - std::pair try_emplace(const key_arg &k, Args &&... args) { - return try_emplace_impl(k, std::forward(args)...); - } - template ::value, int> = 0> - std::pair try_emplace(key_arg &&k, Args &&... args) { - return try_emplace_impl(std::forward(k), std::forward(args)...); - } - template - iterator try_emplace(const_iterator hint, const key_arg &k, - Args &&... args) { - return try_emplace_hint_impl(hint, k, std::forward(args)...); - } - template - iterator try_emplace(const_iterator hint, key_arg &&k, Args &&... args) { - return try_emplace_hint_impl(hint, std::forward(k), - std::forward(args)...); - } - - template - mapped_type &operator[](const key_arg &k) { - return try_emplace(k).first->second; - } - template - mapped_type &operator[](key_arg &&k) { - return try_emplace(std::forward(k)).first->second; - } - - template - mapped_type &at(const key_arg &key) { - auto it = this->find(key); - if (it == this->end()) - base_internal::ThrowStdOutOfRange("absl::btree_map::at"); - return it->second; - } - template - const mapped_type &at(const key_arg &key) const { - 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(obj)` twice, it's safe because - // insert_unique/insert_hint_unique are guaranteed to not consume `obj` when - // `ret.second` is false. - template - std::pair insert_or_assign_impl(K &&k, M &&obj) { - const std::pair ret = - this->tree_.insert_unique(k, std::forward(k), std::forward(obj)); - if (!ret.second) ret.first->second = std::forward(obj); - return ret; - } - template - iterator insert_or_assign_hint_impl(const_iterator hint, K &&k, M &&obj) { - const std::pair ret = this->tree_.insert_hint_unique( - iterator(hint), k, std::forward(k), std::forward(obj)); - if (!ret.second) ret.first->second = std::forward(obj); - return ret.first; - } - - template - std::pair try_emplace_impl(K &&k, Args &&... args) { - return this->tree_.insert_unique( - k, std::piecewise_construct, std::forward_as_tuple(std::forward(k)), - std::forward_as_tuple(std::forward(args)...)); - } - template - 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)), - std::forward_as_tuple(std::forward(args)...)) - .first; - } -}; - -// A common base class for btree_multiset and btree_multimap. -template -class btree_multiset_container : public btree_container { - using super_type = btree_container; - 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; - - template - using key_arg = typename super_type::template key_arg; - - 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::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 - 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 - 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, - 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, - const allocator_type &alloc) - : btree_multiset_container(init.begin(), init.end(), alloc) {} - - // Insertion routines. - iterator insert(const value_type &v) { return this->tree_.insert_multi(v); } - iterator insert(value_type &&v) { - return this->tree_.insert_multi(std::move(v)); - } - iterator insert(const_iterator hint, const value_type &v) { - return this->tree_.insert_hint_multi(iterator(hint), v); - } - iterator insert(const_iterator hint, value_type &&v) { - return this->tree_.insert_hint_multi(iterator(hint), std::move(v)); - } - template - void insert(InputIterator b, InputIterator e) { - this->tree_.insert_iterator_multi(b, e); - } - void insert(std::initializer_list init) { - this->tree_.insert_iterator_multi(init.begin(), init.end()); - } - template - iterator emplace(Args &&... args) { - return this->tree_.insert_multi(init_type(std::forward(args)...)); - } - template - iterator emplace_hint(const_iterator hint, Args &&... args) { - return this->tree_.insert_hint_multi( - iterator(hint), init_type(std::forward(args)...)); - } - iterator insert(node_type &&node) { - 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) { - 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. - // TODO(ezb): we are supposed to extract the first equivalent key if there are - // multiple, but this isn't guaranteed to extract the first one. - template - node_type extract(const key_arg &key) { - auto it = this->find(key); - return it == this->end() ? node_type() : extract(it); - } - 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, - std::is_same, - std::is_same>::value, - int> = 0> - void merge(btree_container &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, - std::is_same, - std::is_same>::value, - int> = 0> - void merge(btree_container &&src) { - merge(src); - } -}; - -// A base class for btree_multimap. -template -class btree_multimap_container : public btree_multiset_container { - using super_type = btree_multiset_container; - using params_type = typename Tree::params_type; - - 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_ diff --git a/third_party/abseil_cpp/absl/container/internal/common.h b/third_party/abseil_cpp/absl/container/internal/common.h deleted file mode 100644 index 030e9d4ab07d..000000000000 --- a/third_party/abseil_cpp/absl/container/internal/common.h +++ /dev/null @@ -1,206 +0,0 @@ -// 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_H_ -#define ABSL_CONTAINER_INTERNAL_CONTAINER_H_ - -#include -#include - -#include "absl/meta/type_traits.h" -#include "absl/types/optional.h" - -namespace absl { -ABSL_NAMESPACE_BEGIN -namespace container_internal { - -template -struct IsTransparent : std::false_type {}; -template -struct IsTransparent> - : std::true_type {}; - -template -struct KeyArg { - // Transparent. Forward `K`. - template - using type = K; -}; - -template <> -struct KeyArg { - // Not transparent. Always use `key_type`. - template - 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 -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 move_tag_t {}; - node_handle_base(move_tag_t, const allocator_type& a, slot_type* s) - : alloc_(a) { - PolicyTraits::construct(alloc(), slot(), s); - } - - 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(std::addressof(slot_space_)); - } - allocator_type* alloc() { return std::addressof(*alloc_); } - - private: - absl::optional alloc_ = {}; - alignas(slot_type) mutable unsigned char slot_space_[sizeof(slot_type)] = {}; -}; - -// For sets. -template -class node_handle : public node_handle_base { - using Base = node_handle_base; - - 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 -class node_handle> - : public node_handle_base { - using Base = node_handle_base; - 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())) { - 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 - static auto GetSlot(const Node& node) -> decltype(node.slot()) { - return node.slot(); - } - - template - static void Destroy(Node* node) { - node->destroy(); - } - - template - static void Reset(Node* node) { - node->reset(); - } - - template - static T Transfer(Args&&... args) { - return T(typename T::transfer_tag_t{}, std::forward(args)...); - } - - template - static T Move(Args&&... args) { - return T(typename T::move_tag_t{}, std::forward(args)...); - } -}; - -// Implement the insert_return_type<> concept of C++17. -template -struct InsertReturnType { - Iterator position; - bool inserted; - NodeType node; -}; - -} // namespace container_internal -ABSL_NAMESPACE_END -} // namespace absl - -#endif // ABSL_CONTAINER_INTERNAL_CONTAINER_H_ diff --git a/third_party/abseil_cpp/absl/container/internal/compressed_tuple.h b/third_party/abseil_cpp/absl/container/internal/compressed_tuple.h deleted file mode 100644 index 5ebe16494249..000000000000 --- a/third_party/abseil_cpp/absl/container/internal/compressed_tuple.h +++ /dev/null @@ -1,290 +0,0 @@ -// 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 is itself an empty class. -// -// To access the members, use member get() function. -// -// Eg: -// absl::container_internal::CompressedTuple 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 -#include -#include -#include - -#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 -class CompressedTuple; - -namespace internal_compressed_tuple { - -template -struct Elem; -template -struct Elem, I> - : std::tuple_element> {}; -template -using ElemT = typename Elem::type; - -// Use the __is_final intrinsic if available. Where it's not available, classes -// declared with the 'final' specifier cannot be used as CompressedTuple -// elements. -// TODO(sbenza): Replace this with std::is_final in C++14. -template -constexpr bool IsFinal() { -#if defined(__clang__) || defined(__GNUC__) - return __is_final(T); -#else - return false; -#endif -} - -// 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 -constexpr bool ShouldUseBase() { - return std::is_class::value && std::is_empty::value && !IsFinal() && - !std::is_base_of::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 ::type>()> -#else - bool UseBase = ShouldUseBase()> -#endif -struct Storage { - T value; - constexpr Storage() = default; - template - explicit constexpr Storage(absl::in_place_t, V&& v) - : value(absl::forward(v)) {} - constexpr const T& get() const& { return value; } - T& get() & { return value; } - constexpr const T&& get() const&& { return absl::move(*this).value; } - T&& get() && { return std::move(*this).value; } -}; - -template -struct ABSL_INTERNAL_COMPRESSED_TUPLE_DECLSPEC Storage : T { - constexpr Storage() = default; - - template - explicit constexpr Storage(absl::in_place_t, V&& v) - : T(absl::forward(v)) {} - - constexpr const T& get() const& { return *this; } - T& get() & { return *this; } - constexpr const T&& get() const&& { return absl::move(*this); } - T&& get() && { return std::move(*this); } -}; - -template -struct ABSL_INTERNAL_COMPRESSED_TUPLE_DECLSPEC CompressedTupleImpl; - -template -struct ABSL_INTERNAL_COMPRESSED_TUPLE_DECLSPEC CompressedTupleImpl< - CompressedTuple, absl::index_sequence, 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::value>... { - constexpr CompressedTupleImpl() = default; - template - explicit constexpr CompressedTupleImpl(absl::in_place_t, Vs&&... args) - : Storage(absl::in_place, absl::forward(args))... {} - friend CompressedTuple; -}; - -template -struct ABSL_INTERNAL_COMPRESSED_TUPLE_DECLSPEC CompressedTupleImpl< - CompressedTuple, absl::index_sequence, false> - // We use the dummy identity function as above... - : Storage::value, false>... { - constexpr CompressedTupleImpl() = default; - template - explicit constexpr CompressedTupleImpl(absl::in_place_t, Vs&&... args) - : Storage(absl::in_place, absl::forward(args))... {} - friend CompressedTuple; -}; - -std::false_type Or(std::initializer_list); -std::true_type Or(std::initializer_list); - -// MSVC requires this to be done separately rather than within the declaration -// of CompressedTuple below. -template -constexpr bool ShouldAnyUseBase() { - return decltype( - Or({std::integral_constant()>()...})){}; -} - -template -using TupleElementMoveConstructible = - typename std::conditional::value, - std::is_convertible, - std::is_constructible>::type; - -template -struct TupleMoveConstructible : std::false_type {}; - -template -struct TupleMoveConstructible, Vs...> - : std::integral_constant< - bool, absl::conjunction< - TupleElementMoveConstructible...>::value> {}; - -template -struct compressed_tuple_size; - -template -struct compressed_tuple_size> - : public std::integral_constant {}; - -template -struct TupleItemsMoveConstructible - : std::integral_constant< - bool, TupleMoveConstructible::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 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() function. -// -// Eg: -// absl::container_internal::CompressedTuple 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 -class ABSL_INTERNAL_COMPRESSED_TUPLE_DECLSPEC CompressedTuple - : private internal_compressed_tuple::CompressedTupleImpl< - CompressedTuple, absl::index_sequence_for, - internal_compressed_tuple::ShouldAnyUseBase()> { - private: - template - using ElemT = internal_compressed_tuple::ElemT; - - template - using StorageT = internal_compressed_tuple::Storage, 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 )>>, - internal_compressed_tuple::TupleItemsMoveConstructible< - CompressedTuple, First, Vs...>>::value, - bool> = true> - explicit constexpr CompressedTuple(First&& first, Vs&&... base) - : CompressedTuple::CompressedTupleImpl(absl::in_place, - absl::forward(first), - absl::forward(base)...) {} - - template - ElemT& get() & { - return StorageT::get(); - } - - template - constexpr const ElemT& get() const& { - return StorageT::get(); - } - - template - ElemT&& get() && { - return std::move(*this).StorageT::get(); - } - - template - constexpr const ElemT&& get() const&& { - return absl::move(*this).StorageT::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_ diff --git a/third_party/abseil_cpp/absl/container/internal/compressed_tuple_test.cc b/third_party/abseil_cpp/absl/container/internal/compressed_tuple_test.cc deleted file mode 100644 index 62a7483ee311..000000000000 --- a/third_party/abseil_cpp/absl/container/internal/compressed_tuple_test.cc +++ /dev/null @@ -1,409 +0,0 @@ -// 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 -#include - -#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 { kConstRef, kConstMove }; - -template -struct Empty { - constexpr CallType value() const& { return CallType::kConstRef; } - constexpr CallType value() const&& { return CallType::kConstMove; } -}; - -template -struct NotEmpty { - T value; -}; - -template -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; - -TEST(CompressedTupleTest, Sizeof) { - EXPECT_EQ(sizeof(int), sizeof(CompressedTuple)); - EXPECT_EQ(sizeof(int), sizeof(CompressedTuple>)); - EXPECT_EQ(sizeof(int), sizeof(CompressedTuple, Empty<1>>)); - EXPECT_EQ(sizeof(int), - sizeof(CompressedTuple, Empty<1>, Empty<2>>)); - - EXPECT_EQ(sizeof(TwoValues), - sizeof(CompressedTuple>)); - EXPECT_EQ(sizeof(TwoValues), - sizeof(CompressedTuple, NotEmpty>)); - EXPECT_EQ(sizeof(TwoValues), - sizeof(CompressedTuple, NotEmpty, Empty<1>>)); -} - -TEST(CompressedTupleTest, OneMoveOnRValueConstructionTemp) { - InstanceTracker tracker; - CompressedTuple 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 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> - 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> -MakeWithIncomplete(CopyableMovableInstance i1, - IncompleteType& t, // NOLINT - Empty<0> empty) { - return CompressedTuple>{ - 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> x1 = - MakeWithIncomplete(std::move(i1), fd, empty); - - EXPECT_EQ(x1.get<0>().value(), 1); - EXPECT_EQ(static_cast(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> - 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 x1(i1); - EXPECT_EQ(tracker.copies(), 1); - EXPECT_EQ(tracker.moves(), 0); - - tracker.ResetCopiesMovesSwaps(); - - CopyableMovableInstance i2(2); - const CopyableMovableInstance& i2_ref = i2; - CompressedTuple x2(i2_ref); - EXPECT_EQ(tracker.copies(), 1); - EXPECT_EQ(tracker.moves(), 0); -} - -TEST(CompressedTupleTest, OneMoveOnRValueAccess) { - InstanceTracker tracker; - CopyableMovableInstance i1(1); - CompressedTuple 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 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 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, S> x(7, {}, S{"ABC"}); - EXPECT_EQ(sizeof(x), sizeof(TwoValues)); - EXPECT_EQ(7, x.get<0>()); - EXPECT_EQ("ABC", x.get<2>().x); -} - -TEST(CompressedTupleTest, NonClasses) { - CompressedTuple x(7, "ABC"); - EXPECT_EQ(7, x.get<0>()); - EXPECT_STREQ("ABC", x.get<1>()); -} - -TEST(CompressedTupleTest, MixClassAndNonClass) { - CompressedTuple, NotEmpty> 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, - CompressedTuple>> - x(1, CompressedTuple(2), - CompressedTuple>(3, CompressedTuple(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>, - CompressedTuple, CompressedTuple>>> - y; - std::set*> 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>)); - EXPECT_TRUE((std::is_empty, Empty<1>>>::value)); - - // Make sure everything still works when things are nested. - struct CT_Empty : CompressedTuple> {}; - CompressedTuple, CT_Empty> nested_empty; - auto contained = nested_empty.get<0>(); - auto nested = nested_empty.get<1>().get<0>(); - EXPECT_TRUE((std::is_same::value)); -} - -TEST(CompressedTupleTest, Reference) { - int i = 7; - std::string s = "Very long string that goes in the heap"; - CompressedTuple 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(x); // Silence -Wunused-variable. - EXPECT_TRUE(std::is_empty>::value); -} - -TEST(CompressedTupleTest, MoveOnlyElements) { - CompressedTuple> str_tup( - absl::make_unique("str")); - - CompressedTuple>, - std::unique_ptr> - x(std::move(str_tup), absl::make_unique(5)); - - EXPECT_EQ(*x.get<0>().get<0>(), "str"); - EXPECT_EQ(*x.get<1>(), 5); - - std::unique_ptr x0 = std::move(x.get<0>()).get<0>(); - std::unique_ptr x1 = std::move(x).get<1>(); - - EXPECT_EQ(*x0, "str"); - EXPECT_EQ(*x1, 5); -} - -TEST(CompressedTupleTest, MoveConstructionMoveOnlyElements) { - CompressedTuple> base( - absl::make_unique("str")); - EXPECT_EQ(*base.get<0>(), "str"); - - CompressedTuple> copy(std::move(base)); - EXPECT_EQ(*copy.get<0>(), "str"); -} - -TEST(CompressedTupleTest, AnyElements) { - any a(std::string("str")); - CompressedTuple x(any(5), a); - EXPECT_EQ(absl::any_cast(x.get<0>()), 5); - EXPECT_EQ(absl::any_cast(x.get<1>()), "str"); - - a = 0.5f; - EXPECT_EQ(absl::any_cast(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; - }; - constexpr CompressedTuple, Empty<0>> x( - 7, 1.25, CompressedTuple(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); - -#if !defined(__GNUC__) || defined(__clang__) || __GNUC__ > 4 - constexpr CompressedTuple, 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); -#endif - - constexpr CompressedTuple, NonTrivialStruct, absl::optional> - non_trivial = {}; - constexpr CallType non_trivial0 = non_trivial.get<0>().value(); - constexpr int non_trivial1 = non_trivial.get<1>().value(); - constexpr absl::optional 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 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 = absl::move(x.get<2>()).get<0>(); - constexpr CallType x3m = absl::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 x; - EXPECT_EQ(x.get<0>().f(), 5); -} -#endif - -} // namespace -} // namespace container_internal -ABSL_NAMESPACE_END -} // namespace absl diff --git a/third_party/abseil_cpp/absl/container/internal/container_memory.h b/third_party/abseil_cpp/absl/container/internal/container_memory.h deleted file mode 100644 index e67529ecb6e6..000000000000 --- a/third_party/abseil_cpp/absl/container/internal/container_memory.h +++ /dev/null @@ -1,460 +0,0 @@ -// 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 -#include -#include -#include -#include -#include -#include - -#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 -#endif - -#ifdef ABSL_HAVE_MEMORY_SANITIZER -#include -#endif - -namespace absl { -ABSL_NAMESPACE_BEGIN -namespace container_internal { - -template -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 -void* Allocate(Alloc* alloc, size_t n) { - static_assert(Alignment > 0, ""); - assert(n && "n must be positive"); - using M = AlignedType; - using A = typename absl::allocator_traits::template rebind_alloc; - using AT = typename absl::allocator_traits::template rebind_traits; - // 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(p) % Alignment == 0 && - "allocator does not respect alignment"); - return p; -} - -// The pointer must have been previously obtained by calling -// Allocate(alloc, n). -template -void Deallocate(Alloc* alloc, void* p, size_t n) { - static_assert(Alignment > 0, ""); - assert(n && "n must be positive"); - using M = AlignedType; - using A = typename absl::allocator_traits::template rebind_alloc; - using AT = typename absl::allocator_traits::template rebind_traits; - // 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(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 -void ConstructFromTupleImpl(Alloc* alloc, T* ptr, Tuple&& t, - absl::index_sequence) { - absl::allocator_traits::construct( - *alloc, ptr, std::get(std::forward(t))...); -} - -template -struct WithConstructedImplF { - template - decltype(std::declval()(std::declval())) operator()( - Args&&... args) const { - return std::forward(f)(T(std::forward(args)...)); - } - F&& f; -}; - -template -decltype(std::declval()(std::declval())) WithConstructedImpl( - Tuple&& t, absl::index_sequence, F&& f) { - return WithConstructedImplF{std::forward(f)}( - std::get(std::forward(t))...); -} - -template -auto TupleRefImpl(T&& t, absl::index_sequence) - -> decltype(std::forward_as_tuple(std::get(std::forward(t))...)) { - return std::forward_as_tuple(std::get(std::forward(t))...); -} - -// Returns a tuple of references to the elements of the input tuple. T must be a -// tuple. -template -auto TupleRef(T&& t) -> decltype( - TupleRefImpl(std::forward(t), - absl::make_index_sequence< - std::tuple_size::type>::value>())) { - return TupleRefImpl( - std::forward(t), - absl::make_index_sequence< - std::tuple_size::type>::value>()); -} - -template -decltype(std::declval()(std::declval(), std::piecewise_construct, - std::declval>(), std::declval())) -DecomposePairImpl(F&& f, std::pair, V> p) { - const auto& key = std::get<0>(p.first); - return std::forward(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 -void ConstructFromTuple(Alloc* alloc, T* ptr, Tuple&& t) { - memory_internal::ConstructFromTupleImpl( - alloc, ptr, std::forward(t), - absl::make_index_sequence< - std::tuple_size::type>::value>()); -} - -// Constructs T using the args specified in the tuple and calls F with the -// constructed value. -template -decltype(std::declval()(std::declval())) WithConstructed( - Tuple&& t, F&& f) { - return memory_internal::WithConstructedImpl( - std::forward(t), - absl::make_index_sequence< - std::tuple_size::type>::value>(), - std::forward(f)); -} - -// Given arguments of an std::pair's consructor, 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 p(args...); -// -// 2. auto a = PairArgs(args...); -// std::pair p(std::piecewise_construct, -// std::move(p.first), std::move(p.second)); -inline std::pair, std::tuple<>> PairArgs() { return {}; } -template -std::pair, std::tuple> PairArgs(F&& f, S&& s) { - return {std::piecewise_construct, std::forward_as_tuple(std::forward(f)), - std::forward_as_tuple(std::forward(s))}; -} -template -std::pair, std::tuple> PairArgs( - const std::pair& p) { - return PairArgs(p.first, p.second); -} -template -std::pair, std::tuple> PairArgs(std::pair&& p) { - return PairArgs(std::forward(p.first), std::forward(p.second)); -} -template -auto PairArgs(std::piecewise_construct_t, F&& f, S&& s) - -> decltype(std::make_pair(memory_internal::TupleRef(std::forward(f)), - memory_internal::TupleRef(std::forward(s)))) { - return std::make_pair(memory_internal::TupleRef(std::forward(f)), - memory_internal::TupleRef(std::forward(s))); -} - -// A helper function for implementing apply() in map policies. -template -auto DecomposePair(F&& f, Args&&... args) - -> decltype(memory_internal::DecomposePairImpl( - std::forward(f), PairArgs(std::forward(args)...))) { - return memory_internal::DecomposePairImpl( - std::forward(f), PairArgs(std::forward(args)...)); -} - -// A helper function for implementing apply() in set policies. -template -decltype(std::declval()(std::declval(), std::declval())) -DecomposeValue(F&& f, Arg&& arg) { - const auto& key = arg; - return std::forward(f)(key, std::forward(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 -inline void SanitizerPoisonObject(const T* object) { - SanitizerPoisonMemoryRegion(object, sizeof(T)); -} - -template -inline void SanitizerUnpoisonObject(const T* object) { - SanitizerUnpoisonMemoryRegion(object, sizeof(T)); -} - -namespace memory_internal { - -// If Pair is a standard-layout type, OffsetOf::kFirst and -// OffsetOf::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 -struct OffsetOf { - static constexpr size_t kFirst = static_cast(-1); - static constexpr size_t kSecond = static_cast(-1); -}; - -template -struct OffsetOf::type> { - static constexpr size_t kFirst = offsetof(Pair, first); - static constexpr size_t kSecond = offsetof(Pair, second); -}; - -template -struct IsLayoutCompatible { - private: - struct Pair { - K first; - V second; - }; - - // Is P layout-compatible with Pair? - template - static constexpr bool LayoutCompatible() { - return std::is_standard_layout

() && sizeof(P) == sizeof(Pair) && - alignof(P) == alignof(Pair) && - memory_internal::OffsetOf

::kFirst == - memory_internal::OffsetOf::kFirst && - memory_internal::OffsetOf

::kSecond == - memory_internal::OffsetOf::kSecond; - } - - public: - // Whether pair and pair 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() && - std::is_standard_layout() && - memory_internal::OffsetOf::kFirst == 0 && - LayoutCompatible>() && - LayoutCompatible>(); -}; - -} // 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 to be -// pair; 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 in insert(). -// -// The solution is this union, which aliases the const and non-const versions -// of the pair. This also allows flat_hash_map 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 -union map_slot_type { - map_slot_type() {} - ~map_slot_type() = delete; - using value_type = std::pair; - using mutable_value_type = - std::pair, absl::remove_const_t>; - - value_type value; - mutable_value_type mutable_value; - absl::remove_const_t key; -}; - -template -struct map_slot_policy { - using slot_type = map_slot_type; - using value_type = std::pair; - using mutable_value_type = std::pair; - - 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 and pair 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; - - 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( - 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 - static void construct(Allocator* alloc, slot_type* slot, Args&&... args) { - emplace(slot); - if (kMutableKeys::value) { - absl::allocator_traits::construct(*alloc, &slot->mutable_value, - std::forward(args)...); - } else { - absl::allocator_traits::construct(*alloc, &slot->value, - std::forward(args)...); - } - } - - // Construct this slot by moving from another slot. - template - static void construct(Allocator* alloc, slot_type* slot, slot_type* other) { - emplace(slot); - if (kMutableKeys::value) { - absl::allocator_traits::construct( - *alloc, &slot->mutable_value, std::move(other->mutable_value)); - } else { - absl::allocator_traits::construct(*alloc, &slot->value, - std::move(other->value)); - } - } - - template - static void destroy(Allocator* alloc, slot_type* slot) { - if (kMutableKeys::value) { - absl::allocator_traits::destroy(*alloc, &slot->mutable_value); - } else { - absl::allocator_traits::destroy(*alloc, &slot->value); - } - } - - template - static void transfer(Allocator* alloc, slot_type* new_slot, - slot_type* old_slot) { - emplace(new_slot); - if (kMutableKeys::value) { - absl::allocator_traits::construct( - *alloc, &new_slot->mutable_value, std::move(old_slot->mutable_value)); - } else { - absl::allocator_traits::construct(*alloc, &new_slot->value, - std::move(old_slot->value)); - } - destroy(alloc, old_slot); - } - - template - static void swap(Allocator* alloc, slot_type* a, slot_type* b) { - if (kMutableKeys::value) { - using std::swap; - swap(a->mutable_value, b->mutable_value); - } else { - value_type tmp = std::move(a->value); - absl::allocator_traits::destroy(*alloc, &a->value); - absl::allocator_traits::construct(*alloc, &a->value, - std::move(b->value)); - absl::allocator_traits::destroy(*alloc, &b->value); - absl::allocator_traits::construct(*alloc, &b->value, - std::move(tmp)); - } - } - - template - static void move(Allocator* alloc, slot_type* src, slot_type* dest) { - if (kMutableKeys::value) { - dest->mutable_value = std::move(src->mutable_value); - } else { - absl::allocator_traits::destroy(*alloc, &dest->value); - absl::allocator_traits::construct(*alloc, &dest->value, - std::move(src->value)); - } - } -}; - -} // namespace container_internal -ABSL_NAMESPACE_END -} // namespace absl - -#endif // ABSL_CONTAINER_INTERNAL_CONTAINER_MEMORY_H_ diff --git a/third_party/abseil_cpp/absl/container/internal/container_memory_test.cc b/third_party/abseil_cpp/absl/container/internal/container_memory_test.cc deleted file mode 100644 index 6a7fcd29ba90..000000000000 --- a/third_party/abseil_cpp/absl/container/internal/container_memory_test.cc +++ /dev/null @@ -1,256 +0,0 @@ -// 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 -#include -#include -#include -#include - -#include "gmock/gmock.h" -#include "gtest/gtest.h" -#include "absl/container/internal/test_instance_tracker.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 alloc; - void* mem = Allocate<2>(&alloc, 3); - EXPECT_EQ(0, reinterpret_cast(mem) % 2); - memcpy(mem, "abc", 3); - Deallocate<2>(&alloc, mem, 3); -} - -TEST(Memory, AlignmentSmallerThanBase) { - std::allocator alloc; - void* mem = Allocate<2>(&alloc, 3); - EXPECT_EQ(0, reinterpret_cast(mem) % 2); - memcpy(mem, "abc", 3); - Deallocate<2>(&alloc, mem, 3); -} - -std::map& AllocationMap() { - static auto* map = new std::map; - return *map; -} - -template -struct TypeCountingAllocator { - TypeCountingAllocator() = default; - template - TypeCountingAllocator(const TypeCountingAllocator&) {} // NOLINT - - using value_type = T; - - T* allocate(size_t n, const void* = nullptr) { - AllocationMap()[typeid(T)] += n; - return std::allocator().allocate(n); - } - void deallocate(T* p, std::size_t n) { - AllocationMap()[typeid(T)] -= n; - return std::allocator().deallocate(p, n); - } -}; - -TEST(Memory, AllocateDeallocateMatchType) { - TypeCountingAllocator 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; - - public: - Fixture() { ptr_ = std::allocator_traits::allocate(*alloc(), 1); } - ~Fixture() override { - std::allocator_traits::destroy(*alloc(), ptr_); - std::allocator_traits::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( - std::make_tuple(std::string("a")), - [](absl::string_view str) { return str.size(); })); -} - -template -decltype(DecomposeValue(std::declval(), std::declval())) -DecomposeValueImpl(int, F&& f, Arg&& arg) { - return DecomposeValue(std::forward(f), std::forward(arg)); -} - -template -const char* DecomposeValueImpl(char, F&& f, Arg&& arg) { - return "not decomposable"; -} - -template -decltype(DecomposeValueImpl(0, std::declval(), std::declval())) -TryDecomposeValue(F&& f, Arg&& arg) { - return DecomposeValueImpl(0, std::forward(f), std::forward(arg)); -} - -TEST(DecomposeValue, Decomposable) { - auto f = [](const int& x, int&& y) { - 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 -decltype(DecomposePair(std::declval(), std::declval()...)) -DecomposePairImpl(int, F&& f, Args&&... args) { - return DecomposePair(std::forward(f), std::forward(args)...); -} - -template -const char* DecomposePairImpl(char, F&& f, Args&&... args) { - return "not decomposable"; -} - -template -decltype(DecomposePairImpl(0, std::declval(), std::declval()...)) -TryDecomposePair(F&& f, Args&&... args) { - return DecomposePairImpl(0, std::forward(f), std::forward(args)...); -} - -TEST(DecomposePair, Decomposable) { - auto f = [](const int& x, std::piecewise_construct_t, std::tuple k, - std::tuple&& 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; - using slot_type = typename slot_policy::slot_type; - - union Slots { - Slots() {} - ~Slots() {} - slot_type slots[100]; - } slots; - - std::allocator< - std::pair> - 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); -} - -} // namespace -} // namespace container_internal -ABSL_NAMESPACE_END -} // namespace absl diff --git a/third_party/abseil_cpp/absl/container/internal/counting_allocator.h b/third_party/abseil_cpp/absl/container/internal/counting_allocator.h deleted file mode 100644 index 927cf0825579..000000000000 --- a/third_party/abseil_cpp/absl/container/internal/counting_allocator.h +++ /dev/null @@ -1,114 +0,0 @@ -// 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_COUNTING_ALLOCATOR_H_ -#define ABSL_CONTAINER_INTERNAL_COUNTING_ALLOCATOR_H_ - -#include -#include - -#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 -class CountingAllocator { - public: - using Allocator = std::allocator; - using AllocatorTraits = std::allocator_traits; - 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 - CountingAllocator(const CountingAllocator& 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 - void construct(U* p, Args&&... args) { - Allocator allocator; - AllocatorTraits::construct(allocator, p, std::forward(args)...); - if (instance_count_ != nullptr) { - *instance_count_ += 1; - } - } - - template - void destroy(U* p) { - Allocator allocator; - AllocatorTraits::destroy(allocator, p); - if (instance_count_ != nullptr) { - *instance_count_ -= 1; - } - } - - template - class rebind { - public: - using other = CountingAllocator; - }; - - 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; -}; - -} // namespace container_internal -ABSL_NAMESPACE_END -} // namespace absl - -#endif // ABSL_CONTAINER_INTERNAL_COUNTING_ALLOCATOR_H_ diff --git a/third_party/abseil_cpp/absl/container/internal/hash_function_defaults.h b/third_party/abseil_cpp/absl/container/internal/hash_function_defaults.h deleted file mode 100644 index 0683422ad89b..000000000000 --- a/third_party/abseil_cpp/absl/container/internal/hash_function_defaults.h +++ /dev/null @@ -1,161 +0,0 @@ -// 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 and std::equal_to 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(a, b) returns true for any a and b of type T, then -// hash_default_hash(a) must equal hash_default_hash(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 but as a pointer when the hash -// function is hash. -// -#ifndef ABSL_CONTAINER_INTERNAL_HASH_FUNCTION_DEFAULTS_H_ -#define ABSL_CONTAINER_INTERNAL_HASH_FUNCTION_DEFAULTS_H_ - -#include -#include -#include -#include -#include - -#include "absl/base/config.h" -#include "absl/hash/hash.h" -#include "absl/strings/cord.h" -#include "absl/strings/string_view.h" - -namespace absl { -ABSL_NAMESPACE_BEGIN -namespace container_internal { - -// The hash of an object of type T is computed by using absl::Hash. -template -struct HashEq { - using Hash = absl::Hash; - using Eq = std::equal_to; -}; - -struct StringHash { - using is_transparent = void; - - size_t operator()(absl::string_view v) const { - return absl::Hash{}(v); - } - size_t operator()(const absl::Cord& v) const { - return absl::Hash{}(v); - } -}; - -// Supports heterogeneous lookup for string-like elements. -struct StringHashEq { - using Hash = StringHash; - struct Eq { - 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; - } - }; -}; - -template <> -struct HashEq : StringHashEq {}; -template <> -struct HashEq : StringHashEq {}; -template <> -struct HashEq : StringHashEq {}; - -// Supports heterogeneous lookup for pointers and smart pointers. -template -struct HashEq { - struct Hash { - using is_transparent = void; - template - size_t operator()(const U& ptr) const { - return absl::Hash{}(HashEq::ToPtr(ptr)); - } - }; - struct Eq { - using is_transparent = void; - template - 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 - static const T* ToPtr(const std::unique_ptr& ptr) { - return ptr.get(); - } - template - static const T* ToPtr(const std::shared_ptr& ptr) { - return ptr.get(); - } -}; - -template -struct HashEq> : HashEq {}; -template -struct HashEq> : HashEq {}; - -// 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::hasher -template -using hash_default_hash = typename container_internal::HashEq::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::key_equal -template -using hash_default_eq = typename container_internal::HashEq::Eq; - -} // namespace container_internal -ABSL_NAMESPACE_END -} // namespace absl - -#endif // ABSL_CONTAINER_INTERNAL_HASH_FUNCTION_DEFAULTS_H_ diff --git a/third_party/abseil_cpp/absl/container/internal/hash_function_defaults_test.cc b/third_party/abseil_cpp/absl/container/internal/hash_function_defaults_test.cc deleted file mode 100644 index 59576b8edebc..000000000000 --- a/third_party/abseil_cpp/absl/container/internal/hash_function_defaults_test.cc +++ /dev/null @@ -1,383 +0,0 @@ -// 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 -#include -#include - -#include "gtest/gtest.h" -#include "absl/random/random.h" -#include "absl/strings/cord.h" -#include "absl/strings/cord_test_helpers.h" -#include "absl/strings/string_view.h" - -namespace absl { -ABSL_NAMESPACE_BEGIN -namespace container_internal { -namespace { - -using ::testing::Types; - -TEST(Eq, Int32) { - hash_default_eq 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 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 eq; - EXPECT_TRUE(eq(MyEnum::A, MyEnum::A)); - EXPECT_FALSE(eq(MyEnum::A, MyEnum::B)); -} - -TEST(Hash, Enum) { - hash_default_hash hash; - - for (MyEnum e : {MyEnum::A, MyEnum::B, MyEnum::C}) { - auto h = hash(e); - EXPECT_EQ(h, hash_default_hash{}(static_cast(e))); - EXPECT_NE(h, hash(MyEnum::D)); - } -} - -using StringTypes = ::testing::Types; - -template -struct EqString : ::testing::Test { - hash_default_eq key_eq; -}; - -TYPED_TEST_SUITE(EqString, StringTypes); - -template -struct HashString : ::testing::Test { - hash_default_hash 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"))); -} - -struct NoDeleter { - template - void operator()(const T* ptr) const {} -}; - -using PointerTypes = - ::testing::Types, - std::unique_ptr, - std::unique_ptr, std::unique_ptr, - std::shared_ptr, std::shared_ptr>; - -template -struct EqPointer : ::testing::Test { - hash_default_eq key_eq; -}; - -TYPED_TEST_SUITE(EqPointer, PointerTypes); - -template -struct HashPointer : ::testing::Test { - hash_default_hash hasher; -}; - -TYPED_TEST_SUITE(HashPointer, PointerTypes); - -TYPED_TEST(EqPointer, Works) { - int dummy; - auto eq = this->key_eq; - auto sptr = std::make_shared(); - std::shared_ptr csptr = sptr; - int* ptr = sptr.get(); - const int* cptr = ptr; - std::unique_ptr uptr(ptr); - std::unique_ptr 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 hasher; - - Base base; - Derived derived; - EXPECT_NE(hasher(&base), hasher(&derived)); - EXPECT_EQ(hasher(static_cast(&derived)), hasher(&derived)); - - auto dp = std::make_shared(); - EXPECT_EQ(hasher(static_cast(dp.get())), hasher(dp)); -} - -TEST(Hash, FunctionPointer) { - using Func = int (*)(); - hash_default_hash hasher; - hash_default_eq 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(); - std::shared_ptr csptr = sptr; - int* ptr = sptr.get(); - const int* cptr = ptr; - std::unique_ptr uptr(ptr); - std::unique_ptr 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 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 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 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 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 hash; - auto bitgen = absl::BitGen(); - for (int i = 0; i < 1000; ++i) { - const int number_of_segments = absl::Uniform(bitgen, 0, 10); - std::vector 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(absl::Uniform(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, - std::pair, - std::pair, - std::pair, - - std::pair, - std::pair, - - std::pair, - std::pair, - std::pair>; -// clang-format on - -constexpr char kFirstString[] = "abc123"; -constexpr char kSecondString[] = "ijk456"; - -template -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 eq; - hash_default_hash hash; -}; - -TYPED_TEST_CASE_P(StringLikeTest); - -TYPED_TEST_P(StringLikeTest, Eq) { - EXPECT_TRUE(this->eq(this->a1, this->b1)); - EXPECT_TRUE(this->eq(this->b1, this->a1)); -} - -TYPED_TEST_P(StringLikeTest, NotEq) { - EXPECT_FALSE(this->eq(this->a1, this->b2)); - EXPECT_FALSE(this->eq(this->b2, this->a1)); -} - -TYPED_TEST_P(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)); -} - -TYPED_TEST_SUITE(StringLikeTest, StringTypesCartesianProduct); - -} // 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 is hashable via all means specified in H. -template -struct Hashable { - static constexpr bool HashableBy(Hash h) { return h & H; } -}; - -namespace std { -template -struct hash> { - template , - class = typename std::enable_if::type> - size_t operator()(E) const { - return kStd; - } -}; -} // namespace std - -namespace absl { -ABSL_NAMESPACE_BEGIN -namespace container_internal { -namespace { - -template -size_t Hash(const T& v) { - return hash_default_hash()(v); -} - -TEST(Delegate, HashDispatch) { - EXPECT_EQ(Hash(kStd), Hash(Hashable())); -} - -} // namespace -} // namespace container_internal -ABSL_NAMESPACE_END -} // namespace absl diff --git a/third_party/abseil_cpp/absl/container/internal/hash_generator_testing.cc b/third_party/abseil_cpp/absl/container/internal/hash_generator_testing.cc deleted file mode 100644 index 59cc5aac7ab8..000000000000 --- a/third_party/abseil_cpp/absl/container/internal/hash_generator_testing.cc +++ /dev/null @@ -1,76 +0,0 @@ -// 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 - -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 - void generate(Iterator start, Iterator end) { - while (start != end) { - *start = gen_(); - ++start; - } - } - - private: - std::random_device gen_; -}; - -} // namespace - -std::mt19937_64* GetSharedRng() { - static auto* rng = [] { - RandomDeviceSeedSeq seed_seq; - return new std::mt19937_64(seed_seq); - }(); - return rng; -} - -std::string Generator::operator()() const { - // NOLINTNEXTLINE(runtime/int) - std::uniform_int_distribution chars(0x20, 0x7E); - std::string res; - res.resize(32); - std::generate(res.begin(), res.end(), - [&]() { return chars(*GetSharedRng()); }); - return res; -} - -absl::string_view Generator::operator()() const { - static auto* arena = new std::deque(); - // NOLINTNEXTLINE(runtime/int) - std::uniform_int_distribution 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 diff --git a/third_party/abseil_cpp/absl/container/internal/hash_generator_testing.h b/third_party/abseil_cpp/absl/container/internal/hash_generator_testing.h deleted file mode 100644 index 6869fe45e8c8..000000000000 --- a/third_party/abseil_cpp/absl/container/internal/hash_generator_testing.h +++ /dev/null @@ -1,161 +0,0 @@ -// 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 - -#include -#include -#include -#include -#include -#include - -#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 -struct IsMap : std::false_type {}; - -template -struct IsMap> : 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(ec); -} - -template -struct Generator; - -template -struct Generator::value>::type> { - T operator()() const { - std::uniform_int_distribution dist; - return dist(*GetSharedRng()); - } -}; - -template <> -struct Generator { - Enum operator()() const { - std::uniform_int_distribution::type> - dist; - while (true) { - auto variate = dist(*GetSharedRng()); - if (variate != kEnumEmpty && variate != kEnumDeleted) - return static_cast(variate); - } - } -}; - -template <> -struct Generator { - EnumClass operator()() const { - std::uniform_int_distribution< - typename std::underlying_type::type> - dist; - while (true) { - EnumClass variate = static_cast(dist(*GetSharedRng())); - if (variate != EnumClass::kEmpty && variate != EnumClass::kDeleted) - return static_cast(variate); - } - } -}; - -template <> -struct Generator { - std::string operator()() const; -}; - -template <> -struct Generator { - absl::string_view operator()() const; -}; - -template <> -struct Generator { - NonStandardLayout operator()() const { - return NonStandardLayout(Generator()()); - } -}; - -template -struct Generator> { - std::pair operator()() const { - return std::pair(Generator::type>()(), - Generator::type>()()); - } -}; - -template -struct Generator> { - std::tuple operator()() const { - return std::tuple(Generator::type>()()...); - } -}; - -template -struct Generator> { - std::unique_ptr operator()() const { - return absl::make_unique(Generator()()); - } -}; - -template -struct Generator().key()), - decltype(std::declval().value())>> - : Generator().key())>::type, - typename std::decay().value())>::type>> {}; - -template -using GeneratedType = decltype( - std::declval::value, - typename Container::value_type, - typename Container::key_type>::type>&>()()); - -} // namespace hash_internal -} // namespace container_internal -ABSL_NAMESPACE_END -} // namespace absl - -#endif // ABSL_CONTAINER_INTERNAL_HASH_GENERATOR_TESTING_H_ diff --git a/third_party/abseil_cpp/absl/container/internal/hash_policy_testing.h b/third_party/abseil_cpp/absl/container/internal/hash_policy_testing.h deleted file mode 100644 index 01c40d2e5cff..000000000000 --- a/third_party/abseil_cpp/absl/container/internal/hash_policy_testing.h +++ /dev/null @@ -1,184 +0,0 @@ -// 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 -#include -#include -#include -#include -#include -#include - -#include "absl/hash/hash.h" -#include "absl/strings/string_view.h" - -namespace absl { -ABSL_NAMESPACE_BEGIN -namespace container_internal { -namespace hash_testing_internal { - -template -struct WithId { - WithId() : id_(next_id()) {} - 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 - 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 - 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 - size_t operator()(const T& t) const { - return absl::Hash{}(t); - } -}; - -struct StatefulTestingEqual - : absl::container_internal::hash_testing_internal::WithId< - StatefulTestingEqual> { - template - 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 -struct Alloc : std::allocator { - 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 - Alloc(const Alloc& that) : std::allocator(that), id_(that.id()) {} - - template - struct rebind { - using other = Alloc; - }; - - 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::max)(); -}; - -template -auto items(const Map& m) -> std::vector< - std::pair> { - using std::get; - std::vector> res; - res.reserve(m.size()); - for (const auto& v : m) res.emplace_back(get<0>(v), get<1>(v)); - return res; -} - -template -auto keys(const Set& s) - -> std::vector::type> { - std::vector::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 and -// meet the allocator-aware container requirements;" -#if (defined(__GLIBCXX__) && __GLIBCXX__ <= 20140425 ) || \ -( __GNUC__ < 4 || (__GNUC__ == 4 && __GNUC_MINOR__ < 9 )) -#define ABSL_UNORDERED_SUPPORTS_ALLOC_CTORS 0 -#else -#define ABSL_UNORDERED_SUPPORTS_ALLOC_CTORS 1 -#endif - -#endif // ABSL_CONTAINER_INTERNAL_HASH_POLICY_TESTING_H_ diff --git a/third_party/abseil_cpp/absl/container/internal/hash_policy_testing_test.cc b/third_party/abseil_cpp/absl/container/internal/hash_policy_testing_test.cc deleted file mode 100644 index f0b20fe345e2..000000000000 --- a/third_party/abseil_cpp/absl/container/internal/hash_policy_testing_test.cc +++ /dev/null @@ -1,45 +0,0 @@ -// 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 diff --git a/third_party/abseil_cpp/absl/container/internal/hash_policy_traits.h b/third_party/abseil_cpp/absl/container/internal/hash_policy_traits.h deleted file mode 100644 index 46c97b18a227..000000000000 --- a/third_party/abseil_cpp/absl/container/internal/hash_policy_traits.h +++ /dev/null @@ -1,208 +0,0 @@ -// 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 -#include -#include -#include -#include - -#include "absl/meta/type_traits.h" - -namespace absl { -ABSL_NAMESPACE_BEGIN -namespace container_internal { - -// Defines how slots are initialized/destroyed/moved. -template -struct hash_policy_traits { - // 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 ::value, int> = 0> - static key_type& Impl(Key&& k, int) { - return *std::launder( - const_cast(std::addressof(std::forward(k)))); - } -#endif - - template - static Key Impl(Key&& k, char) { - return std::forward(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 - auto operator()(Key&& k, const Args&...) const - -> decltype(Impl(std::forward(k), 0)) { - return Impl(std::forward(k), 0); - } - }; - - template - struct ConstantIteratorsImpl : std::false_type {}; - - template - struct ConstantIteratorsImpl> - : 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())); - using pointer = typename std::remove_reference::type*; - using value_type = typename std::remove_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<>; - - // PRECONDITION: `slot` is UNINITIALIZED - // POSTCONDITION: `slot` is INITIALIZED - template - static void construct(Alloc* alloc, slot_type* slot, Args&&... args) { - Policy::construct(alloc, slot, std::forward(args)...); - } - - // PRECONDITION: `slot` is INITIALIZED - // POSTCONDITION: `slot` is UNINITIALIZED - template - static void destroy(Alloc* alloc, slot_type* slot) { - 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 - static void transfer(Alloc* alloc, slot_type* new_slot, slot_type* old_slot) { - transfer_impl(alloc, new_slot, old_slot, 0); - } - - // PRECONDITION: `slot` is INITIALIZED - // POSTCONDITION: `slot` is INITIALIZED - template - static auto element(slot_type* slot) -> decltype(P::element(slot)) { - return P::element(slot); - } - - // 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 - 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)(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 - static auto apply(F&& f, Ts&&... ts) - -> decltype(P::apply(std::forward(f), std::forward(ts)...)) { - return P::apply(std::forward(f), std::forward(ts)...); - } - - // Returns the "key" portion of the slot. - // Used for node handle manipulation. - template - static auto mutable_key(slot_type* slot) - -> decltype(P::apply(ReturnKey(), element(slot))) { - return P::apply(ReturnKey(), 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 - static auto value(T* elem) -> decltype(P::value(elem)) { - return P::value(elem); - } - - private: - // Use auto -> decltype as an enabler. - template - static auto transfer_impl(Alloc* alloc, slot_type* new_slot, - slot_type* old_slot, int) - -> decltype((void)P::transfer(alloc, new_slot, old_slot)) { - P::transfer(alloc, new_slot, old_slot); - } - template - static void transfer_impl(Alloc* alloc, slot_type* new_slot, - slot_type* old_slot, char) { - 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_HASH_POLICY_TRAITS_H_ diff --git a/third_party/abseil_cpp/absl/container/internal/hash_policy_traits_test.cc b/third_party/abseil_cpp/absl/container/internal/hash_policy_traits_test.cc deleted file mode 100644 index 6ef8b9e05fb1..000000000000 --- a/third_party/abseil_cpp/absl/container/internal/hash_policy_traits_test.cc +++ /dev/null @@ -1,144 +0,0 @@ -// 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 -#include -#include - -#include "gmock/gmock.h" -#include "gtest/gtest.h" - -namespace absl { -ABSL_NAMESPACE_BEGIN -namespace container_internal { -namespace { - -using ::testing::MockFunction; -using ::testing::Return; -using ::testing::ReturnRef; - -using Alloc = std::allocator; -using Slot = int; - -struct PolicyWithoutOptionalOps { - using slot_type = Slot; - using key_type = Slot; - using init_type = Slot; - - static std::function construct; - static std::function destroy; - - static std::function element; - static int apply(int v) { return apply_impl(v); } - static std::function apply_impl; - static std::function value; -}; - -std::function PolicyWithoutOptionalOps::construct; -std::function PolicyWithoutOptionalOps::destroy; - -std::function PolicyWithoutOptionalOps::element; -std::function PolicyWithoutOptionalOps::apply_impl; -std::function PolicyWithoutOptionalOps::value; - -struct PolicyWithOptionalOps : PolicyWithoutOptionalOps { - static std::function transfer; -}; - -std::function PolicyWithOptionalOps::transfer; - -struct Test : ::testing::Test { - Test() { - PolicyWithoutOptionalOps::construct = [&](void* a1, Slot* a2, Slot a3) { - construct.Call(a1, a2, std::move(a3)); - }; - PolicyWithoutOptionalOps::destroy = [&](void* a1, Slot* a2) { - destroy.Call(a1, a2); - }; - - PolicyWithoutOptionalOps::element = [&](Slot* a1) -> Slot& { - return element.Call(a1); - }; - PolicyWithoutOptionalOps::apply_impl = [&](int a1) -> int { - return apply.Call(a1); - }; - PolicyWithoutOptionalOps::value = [&](Slot* a1) -> Slot& { - return value.Call(a1); - }; - - PolicyWithOptionalOps::transfer = [&](void* a1, Slot* a2, Slot* a3) { - return transfer.Call(a1, a2, a3); - }; - } - - std::allocator alloc; - int a = 53; - - MockFunction construct; - MockFunction destroy; - - MockFunction element; - MockFunction apply; - MockFunction value; - - MockFunction transfer; -}; - -TEST_F(Test, construct) { - EXPECT_CALL(construct, Call(&alloc, &a, 53)); - hash_policy_traits::construct(&alloc, &a, 53); -} - -TEST_F(Test, destroy) { - EXPECT_CALL(destroy, Call(&alloc, &a)); - hash_policy_traits::destroy(&alloc, &a); -} - -TEST_F(Test, element) { - int b = 0; - EXPECT_CALL(element, Call(&a)).WillOnce(ReturnRef(b)); - EXPECT_EQ(&b, &hash_policy_traits::element(&a)); -} - -TEST_F(Test, apply) { - EXPECT_CALL(apply, Call(42)).WillOnce(Return(1337)); - EXPECT_EQ(1337, (hash_policy_traits::apply(42))); -} - -TEST_F(Test, value) { - int b = 0; - EXPECT_CALL(value, Call(&a)).WillOnce(ReturnRef(b)); - EXPECT_EQ(&b, &hash_policy_traits::value(&a)); -} - -TEST_F(Test, without_transfer) { - int b = 42; - EXPECT_CALL(element, Call(&b)).WillOnce(::testing::ReturnRef(b)); - EXPECT_CALL(construct, Call(&alloc, &a, b)); - EXPECT_CALL(destroy, Call(&alloc, &b)); - hash_policy_traits::transfer(&alloc, &a, &b); -} - -TEST_F(Test, with_transfer) { - int b = 42; - EXPECT_CALL(transfer, Call(&alloc, &a, &b)); - hash_policy_traits::transfer(&alloc, &a, &b); -} - -} // namespace -} // namespace container_internal -ABSL_NAMESPACE_END -} // namespace absl diff --git a/third_party/abseil_cpp/absl/container/internal/hashtable_debug.h b/third_party/abseil_cpp/absl/container/internal/hashtable_debug.h deleted file mode 100644 index 19d52121d688..000000000000 --- a/third_party/abseil_cpp/absl/container/internal/hashtable_debug.h +++ /dev/null @@ -1,110 +0,0 @@ -// 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 -#include -#include -#include - -#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 -size_t GetHashtableDebugNumProbes( - const C& c, const typename C::key_type& key) { - return absl::container_internal::hashtable_debug_internal:: - HashtableDebugAccess::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 -std::vector GetHashtableDebugNumProbesHistogram(const C& container) { - std::vector v; - for (auto it = container.begin(); it != container.end(); ++it) { - size_t num_probes = GetHashtableDebugNumProbes( - container, - absl::container_internal::hashtable_debug_internal::GetKey(*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 -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 -size_t AllocatedByteSize(const C& c) { - return absl::container_internal::hashtable_debug_internal:: - HashtableDebugAccess::AllocatedByteSize(c); -} - -// Returns a tight lower bound for AllocatedByteSize(c) where `c` is of type `C` -// and `c.size()` is equal to `num_elements`. -template -size_t LowerBoundAllocatedByteSize(size_t num_elements) { - return absl::container_internal::hashtable_debug_internal:: - HashtableDebugAccess::LowerBoundAllocatedByteSize(num_elements); -} - -} // namespace container_internal -ABSL_NAMESPACE_END -} // namespace absl - -#endif // ABSL_CONTAINER_INTERNAL_HASHTABLE_DEBUG_H_ diff --git a/third_party/abseil_cpp/absl/container/internal/hashtable_debug_hooks.h b/third_party/abseil_cpp/absl/container/internal/hashtable_debug_hooks.h deleted file mode 100644 index 3e9ea5954e0a..000000000000 --- a/third_party/abseil_cpp/absl/container/internal/hashtable_debug_hooks.h +++ /dev/null @@ -1,85 +0,0 @@ -// 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 - -#include -#include -#include - -#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 -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 -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 -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(*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_ diff --git a/third_party/abseil_cpp/absl/container/internal/hashtablez_sampler.cc b/third_party/abseil_cpp/absl/container/internal/hashtablez_sampler.cc deleted file mode 100644 index e4484fbb1b43..000000000000 --- a/third_party/abseil_cpp/absl/container/internal/hashtablez_sampler.cc +++ /dev/null @@ -1,270 +0,0 @@ -// 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 -#include -#include -#include -#include - -#include "absl/base/attributes.h" -#include "absl/base/internal/exponential_biased.h" -#include "absl/container/internal/have_sse.h" -#include "absl/debugging/stacktrace.h" -#include "absl/memory/memory.h" -#include "absl/synchronization/mutex.h" - -namespace absl { -ABSL_NAMESPACE_BEGIN -namespace container_internal { -constexpr int HashtablezInfo::kMaxStackDepth; - -namespace { -ABSL_CONST_INIT std::atomic g_hashtablez_enabled{ - false -}; -ABSL_CONST_INIT std::atomic g_hashtablez_sample_parameter{1 << 10}; -ABSL_CONST_INIT std::atomic g_hashtablez_max_samples{1 << 20}; - -#if defined(ABSL_INTERNAL_HASHTABLEZ_SAMPLE) -ABSL_PER_THREAD_TLS_KEYWORD absl::base_internal::ExponentialBiased - g_exponential_biased_generator; -#endif - -} // namespace - -#if defined(ABSL_INTERNAL_HASHTABLEZ_SAMPLE) -ABSL_PER_THREAD_TLS_KEYWORD int64_t global_next_sample = 0; -#endif // defined(ABSL_INTERNAL_HASHTABLEZ_SAMPLE) - -HashtablezSampler& HashtablezSampler::Global() { - static auto* sampler = new HashtablezSampler(); - return *sampler; -} - -HashtablezSampler::DisposeCallback HashtablezSampler::SetDisposeCallback( - DisposeCallback f) { - return dispose_.exchange(f, std::memory_order_relaxed); -} - -HashtablezInfo::HashtablezInfo() { PrepareForSampling(); } -HashtablezInfo::~HashtablezInfo() = default; - -void HashtablezInfo::PrepareForSampling() { - 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); - - create_time = absl::Now(); - // 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); - dead = nullptr; -} - -HashtablezSampler::HashtablezSampler() - : dropped_samples_(0), size_estimate_(0), all_(nullptr), dispose_(nullptr) { - absl::MutexLock l(&graveyard_.init_mu); - graveyard_.dead = &graveyard_; -} - -HashtablezSampler::~HashtablezSampler() { - HashtablezInfo* s = all_.load(std::memory_order_acquire); - while (s != nullptr) { - HashtablezInfo* next = s->next; - delete s; - s = next; - } -} - -void HashtablezSampler::PushNew(HashtablezInfo* sample) { - sample->next = all_.load(std::memory_order_relaxed); - while (!all_.compare_exchange_weak(sample->next, sample, - std::memory_order_release, - std::memory_order_relaxed)) { - } -} - -void HashtablezSampler::PushDead(HashtablezInfo* sample) { - if (auto* dispose = dispose_.load(std::memory_order_relaxed)) { - dispose(*sample); - } - - absl::MutexLock graveyard_lock(&graveyard_.init_mu); - absl::MutexLock sample_lock(&sample->init_mu); - sample->dead = graveyard_.dead; - graveyard_.dead = sample; -} - -HashtablezInfo* HashtablezSampler::PopDead() { - absl::MutexLock graveyard_lock(&graveyard_.init_mu); - - // The list is circular, so eventually it collapses down to - // graveyard_.dead == &graveyard_ - // when it is empty. - HashtablezInfo* sample = graveyard_.dead; - if (sample == &graveyard_) return nullptr; - - absl::MutexLock sample_lock(&sample->init_mu); - graveyard_.dead = sample->dead; - sample->PrepareForSampling(); - return sample; -} - -HashtablezInfo* HashtablezSampler::Register() { - int64_t size = size_estimate_.fetch_add(1, std::memory_order_relaxed); - if (size > g_hashtablez_max_samples.load(std::memory_order_relaxed)) { - size_estimate_.fetch_sub(1, std::memory_order_relaxed); - dropped_samples_.fetch_add(1, std::memory_order_relaxed); - return nullptr; - } - - HashtablezInfo* sample = PopDead(); - if (sample == nullptr) { - // Resurrection failed. Hire a new warlock. - sample = new HashtablezInfo(); - PushNew(sample); - } - - return sample; -} - -void HashtablezSampler::Unregister(HashtablezInfo* sample) { - PushDead(sample); - size_estimate_.fetch_sub(1, std::memory_order_relaxed); -} - -int64_t HashtablezSampler::Iterate( - const std::function& f) { - HashtablezInfo* s = all_.load(std::memory_order_acquire); - while (s != nullptr) { - absl::MutexLock l(&s->init_mu); - if (s->dead == nullptr) { - f(*s); - } - s = s->next; - } - - return dropped_samples_.load(std::memory_order_relaxed); -} - -static bool ShouldForceSampling() { - enum ForceState { - kDontForce, - kForce, - kUninitialized - }; - ABSL_CONST_INIT static std::atomic global_state{ - kUninitialized}; - ForceState state = global_state.load(std::memory_order_relaxed); - if (ABSL_PREDICT_TRUE(state == kDontForce)) return false; - - if (state == kUninitialized) { - state = AbslContainerInternalSampleEverything() ? kForce : kDontForce; - global_state.store(state, std::memory_order_relaxed); - } - return state == kForce; -} - -HashtablezInfo* SampleSlow(int64_t* next_sample) { - if (ABSL_PREDICT_FALSE(ShouldForceSampling())) { - *next_sample = 1; - return HashtablezSampler::Global().Register(); - } - -#if !defined(ABSL_INTERNAL_HASHTABLEZ_SAMPLE) - *next_sample = std::numeric_limits::max(); - return nullptr; -#else - bool first = *next_sample < 0; - *next_sample = g_exponential_biased_generator.GetStride( - g_hashtablez_sample_parameter.load(std::memory_order_relaxed)); - // Small values of interval are equivalent to just sampling next time. - ABSL_ASSERT(*next_sample >= 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 > 0)) return nullptr; - return SampleSlow(next_sample); - } - - return HashtablezSampler::Global().Register(); -#endif -} - -void UnsampleSlow(HashtablezInfo* info) { - HashtablezSampler::Global().Unregister(info); -} - -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; -#if ABSL_INTERNAL_RAW_HASH_SET_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->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 SetHashtablezEnabled(bool enabled) { - g_hashtablez_enabled.store(enabled, std::memory_order_release); -} - -void SetHashtablezSampleParameter(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(rate)); // NOLINT(runtime/int) - } -} - -void SetHashtablezMaxSamples(int32_t max) { - if (max > 0) { - g_hashtablez_max_samples.store(max, std::memory_order_release); - } else { - ABSL_RAW_LOG(ERROR, "Invalid hashtablez max samples: %lld", - static_cast(max)); // NOLINT(runtime/int) - } -} - -} // namespace container_internal -ABSL_NAMESPACE_END -} // namespace absl diff --git a/third_party/abseil_cpp/absl/container/internal/hashtablez_sampler.h b/third_party/abseil_cpp/absl/container/internal/hashtablez_sampler.h deleted file mode 100644 index 394348da58f5..000000000000 --- a/third_party/abseil_cpp/absl/container/internal/hashtablez_sampler.h +++ /dev/null @@ -1,321 +0,0 @@ -// 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 -#include -#include -#include - -#include "absl/base/internal/per_thread_tls.h" -#include "absl/base/optimization.h" -#include "absl/container/internal/have_sse.h" -#include "absl/synchronization/mutex.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 { - // 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() ABSL_EXCLUSIVE_LOCKS_REQUIRED(init_mu); - - // These fields are mutated by the various Record* APIs and need to be - // thread-safe. - std::atomic capacity; - std::atomic size; - std::atomic num_erases; - std::atomic num_rehashes; - std::atomic max_probe_length; - std::atomic total_probe_length; - std::atomic hashes_bitwise_or; - std::atomic hashes_bitwise_and; - - // `HashtablezSampler` maintains intrusive linked lists for all samples. See - // comments on `HashtablezSampler::all_` for details on these. `init_mu` - // guards the ability to restore the sample to a pristine state. This - // prevents races with sampling and resurrecting an object. - absl::Mutex init_mu; - HashtablezInfo* next; - HashtablezInfo* dead ABSL_GUARDED_BY(init_mu); - - // 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, who - // can only read them during `HashtablezSampler::Iterate` which will hold the - // lock. - static constexpr int kMaxStackDepth = 64; - absl::Time create_time; - int32_t depth; - void* stack[kMaxStackDepth]; -}; - -inline void RecordRehashSlow(HashtablezInfo* info, size_t total_probe_length) { -#if ABSL_INTERNAL_RAW_HASH_SET_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); -} - -inline 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); - -inline 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); -} - -HashtablezInfo* SampleSlow(int64_t* next_sample); -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) {} - ~HashtablezInfoHandle() { - if (ABSL_PREDICT_TRUE(info_ == nullptr)) return; - UnsampleSlow(info_); - } - - HashtablezInfoHandle(const HashtablezInfoHandle&) = delete; - HashtablezInfoHandle& operator=(const HashtablezInfoHandle&) = delete; - - HashtablezInfoHandle(HashtablezInfoHandle&& o) noexcept - : info_(absl::exchange(o.info_, nullptr)) {} - HashtablezInfoHandle& operator=(HashtablezInfoHandle&& o) noexcept { - if (ABSL_PREDICT_FALSE(info_ != nullptr)) { - UnsampleSlow(info_); - } - info_ = absl::exchange(o.info_, nullptr); - return *this; - } - - 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 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 RecordStorageChanged(size_t /*size*/, size_t /*capacity*/) {} - inline void RecordRehash(size_t /*total_probe_length*/) {} - 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 int64_t global_next_sample; -#endif // defined(ABSL_INTERNAL_HASHTABLEZ_SAMPLE) - -// Returns an RAII sampling handle that manages registration and unregistation -// with the global sampler. -inline HashtablezInfoHandle Sample() { -#if defined(ABSL_INTERNAL_HASHTABLEZ_SAMPLE) - if (ABSL_PREDICT_TRUE(--global_next_sample > 0)) { - return HashtablezInfoHandle(nullptr); - } - return HashtablezInfoHandle(SampleSlow(&global_next_sample)); -#else - return HashtablezInfoHandle(nullptr); -#endif // !ABSL_PER_THREAD_TLS -} - -// Holds samples and their associated stack traces with a soft limit of -// `SetHashtablezMaxSamples()`. -// -// Thread safe. -class HashtablezSampler { - public: - // Returns a global Sampler. - static HashtablezSampler& Global(); - - HashtablezSampler(); - ~HashtablezSampler(); - - // Registers for sampling. Returns an opaque registration info. - HashtablezInfo* Register(); - - // Unregisters the sample. - void Unregister(HashtablezInfo* sample); - - // The dispose callback will be called on all samples the moment they are - // being unregistered. Only affects samples that are unregistered after the - // callback has been set. - // Returns the previous callback. - using DisposeCallback = void (*)(const HashtablezInfo&); - DisposeCallback SetDisposeCallback(DisposeCallback f); - - // Iterates over all the registered `StackInfo`s. Returning the number of - // samples that have been dropped. - int64_t Iterate(const std::function& f); - - private: - void PushNew(HashtablezInfo* sample); - void PushDead(HashtablezInfo* sample); - HashtablezInfo* PopDead(); - - std::atomic dropped_samples_; - std::atomic size_estimate_; - - // Intrusive lock free linked lists for tracking samples. - // - // `all_` records all samples (they are never removed from this list) and is - // terminated with a `nullptr`. - // - // `graveyard_.dead` is a circular linked list. When it is empty, - // `graveyard_.dead == &graveyard`. The list is circular so that - // every item on it (even the last) has a non-null dead pointer. This allows - // `Iterate` to determine if a given sample is live or dead using only - // information on the sample itself. - // - // For example, nodes [A, B, C, D, E] with [A, C, E] alive and [B, D] dead - // looks like this (G is the Graveyard): - // - // +---+ +---+ +---+ +---+ +---+ - // all -->| A |--->| B |--->| C |--->| D |--->| E | - // | | | | | | | | | | - // +---+ | | +->| |-+ | | +->| |-+ | | - // | G | +---+ | +---+ | +---+ | +---+ | +---+ - // | | | | | | - // | | --------+ +--------+ | - // +---+ | - // ^ | - // +--------------------------------------+ - // - std::atomic all_; - HashtablezInfo graveyard_; - - std::atomic dispose_; -}; - -// Enables or disables sampling for Swiss tables. -void SetHashtablezEnabled(bool enabled); - -// Sets the rate at which Swiss tables will be sampled. -void SetHashtablezSampleParameter(int32_t rate); - -// Sets a soft max for the number of samples that will be kept. -void SetHashtablezMaxSamples(int32_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 AbslContainerInternalSampleEverything(); - -} // namespace container_internal -ABSL_NAMESPACE_END -} // namespace absl - -#endif // ABSL_CONTAINER_INTERNAL_HASHTABLEZ_SAMPLER_H_ diff --git a/third_party/abseil_cpp/absl/container/internal/hashtablez_sampler_force_weak_definition.cc b/third_party/abseil_cpp/absl/container/internal/hashtablez_sampler_force_weak_definition.cc deleted file mode 100644 index 78b9d362acf3..000000000000 --- a/third_party/abseil_cpp/absl/container/internal/hashtablez_sampler_force_weak_definition.cc +++ /dev/null @@ -1,30 +0,0 @@ -// 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 AbslContainerInternalSampleEverything() { - return false; -} - -} // namespace container_internal -ABSL_NAMESPACE_END -} // namespace absl diff --git a/third_party/abseil_cpp/absl/container/internal/hashtablez_sampler_test.cc b/third_party/abseil_cpp/absl/container/internal/hashtablez_sampler_test.cc deleted file mode 100644 index 8d10a1e94030..000000000000 --- a/third_party/abseil_cpp/absl/container/internal/hashtablez_sampler_test.cc +++ /dev/null @@ -1,371 +0,0 @@ -// 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 -#include -#include - -#include "gmock/gmock.h" -#include "gtest/gtest.h" -#include "absl/base/attributes.h" -#include "absl/container/internal/have_sse.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" - -#if ABSL_INTERNAL_RAW_HASH_SET_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 bool IsSampled(const HashtablezInfoHandle& h) { - return h.info_ != nullptr; - } - - static HashtablezInfo* GetInfo(HashtablezInfoHandle* h) { return h->info_; } -}; -#else -class HashtablezInfoHandlePeer { - public: - static bool IsSampled(const HashtablezInfoHandle&) { return false; } - 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 GetSizes(HashtablezSampler* s) { - std::vector 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) { - auto* info = s->Register(); - assert(info != nullptr); - info->size.store(size); - return info; -} - -TEST(HashtablezInfoTest, PrepareForSampling) { - absl::Time test_start = absl::Now(); - HashtablezInfo info; - absl::MutexLock l(&info.init_mu); - info.PrepareForSampling(); - - 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_GE(info.create_time, test_start); - - 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.create_time = test_start - absl::Hours(20); - - info.PrepareForSampling(); - 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_GE(info.create_time, test_start); -} - -TEST(HashtablezInfoTest, RecordStorageChanged) { - HashtablezInfo info; - absl::MutexLock l(&info.init_mu); - info.PrepareForSampling(); - 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); - info.PrepareForSampling(); - 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); - 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); - 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); -} - -TEST(HashtablezInfoTest, RecordErase) { - HashtablezInfo info; - absl::MutexLock l(&info.init_mu); - info.PrepareForSampling(); - 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); -} - -TEST(HashtablezInfoTest, RecordRehash) { - HashtablezInfo info; - absl::MutexLock l(&info.init_mu); - info.PrepareForSampling(); - 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); -} - -#if defined(ABSL_INTERNAL_HASHTABLEZ_SAMPLE) -TEST(HashtablezSamplerTest, SmallSampleParameter) { - SetHashtablezEnabled(true); - SetHashtablezSampleParameter(100); - - for (int i = 0; i < 1000; ++i) { - int64_t next_sample = 0; - HashtablezInfo* sample = SampleSlow(&next_sample); - EXPECT_GT(next_sample, 0); - EXPECT_NE(sample, nullptr); - UnsampleSlow(sample); - } -} - -TEST(HashtablezSamplerTest, LargeSampleParameter) { - SetHashtablezEnabled(true); - SetHashtablezSampleParameter(std::numeric_limits::max()); - - for (int i = 0; i < 1000; ++i) { - int64_t next_sample = 0; - HashtablezInfo* sample = SampleSlow(&next_sample); - EXPECT_GT(next_sample, 0); - EXPECT_NE(sample, nullptr); - UnsampleSlow(sample); - } -} - -TEST(HashtablezSamplerTest, Sample) { - 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(); - ++total; - if (HashtablezInfoHandlePeer::IsSampled(h)) { - ++num_sampled; - } - sample_rate = static_cast(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 = HashtablezSampler::Global(); - HashtablezInfoHandle h(sampler.Register()); - 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.hashes_bitwise_and.load(), 0x12345678); - found = true; - } - }); - EXPECT_TRUE(found); - - 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 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) { - pool.Schedule([&sampler, &stop]() { - std::random_device rd; - std::mt19937 gen(rd()); - - std::vector infoz; - while (!stop.HasBeenNotified()) { - if (infoz.empty()) { - infoz.push_back(sampler.Register()); - } - switch (std::uniform_int_distribution<>(0, 2)(gen)) { - case 0: { - infoz.push_back(sampler.Register()); - 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(); - 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 diff --git a/third_party/abseil_cpp/absl/container/internal/have_sse.h b/third_party/abseil_cpp/absl/container/internal/have_sse.h deleted file mode 100644 index e75e1a16d327..000000000000 --- a/third_party/abseil_cpp/absl/container/internal/have_sse.h +++ /dev/null @@ -1,50 +0,0 @@ -// 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. -// -// Shared config probing for SSE instructions used in Swiss tables. -#ifndef ABSL_CONTAINER_INTERNAL_HAVE_SSE_H_ -#define ABSL_CONTAINER_INTERNAL_HAVE_SSE_H_ - -#ifndef ABSL_INTERNAL_RAW_HASH_SET_HAVE_SSE2 -#if defined(__SSE2__) || \ - (defined(_MSC_VER) && \ - (defined(_M_X64) || (defined(_M_IX86) && _M_IX86_FP >= 2))) -#define ABSL_INTERNAL_RAW_HASH_SET_HAVE_SSE2 1 -#else -#define ABSL_INTERNAL_RAW_HASH_SET_HAVE_SSE2 0 -#endif -#endif - -#ifndef ABSL_INTERNAL_RAW_HASH_SET_HAVE_SSSE3 -#ifdef __SSSE3__ -#define ABSL_INTERNAL_RAW_HASH_SET_HAVE_SSSE3 1 -#else -#define ABSL_INTERNAL_RAW_HASH_SET_HAVE_SSSE3 0 -#endif -#endif - -#if ABSL_INTERNAL_RAW_HASH_SET_HAVE_SSSE3 && \ - !ABSL_INTERNAL_RAW_HASH_SET_HAVE_SSE2 -#error "Bad configuration!" -#endif - -#if ABSL_INTERNAL_RAW_HASH_SET_HAVE_SSE2 -#include -#endif - -#if ABSL_INTERNAL_RAW_HASH_SET_HAVE_SSSE3 -#include -#endif - -#endif // ABSL_CONTAINER_INTERNAL_HAVE_SSE_H_ diff --git a/third_party/abseil_cpp/absl/container/internal/inlined_vector.h b/third_party/abseil_cpp/absl/container/internal/inlined_vector.h deleted file mode 100644 index c98c25c44221..000000000000 --- a/third_party/abseil_cpp/absl/container/internal/inlined_vector.h +++ /dev/null @@ -1,895 +0,0 @@ -// 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_INLINED_VECTOR_INTERNAL_H_ -#define ABSL_CONTAINER_INTERNAL_INLINED_VECTOR_INTERNAL_H_ - -#include -#include -#include -#include -#include -#include -#include - -#include "absl/base/macros.h" -#include "absl/container/internal/compressed_tuple.h" -#include "absl/memory/memory.h" -#include "absl/meta/type_traits.h" -#include "absl/types/span.h" - -namespace absl { -ABSL_NAMESPACE_BEGIN -namespace inlined_vector_internal { - -template -using IsAtLeastForwardIterator = std::is_convertible< - typename std::iterator_traits::iterator_category, - std::forward_iterator_tag>; - -template ::value_type> -using IsMemcpyOk = - absl::conjunction>, - absl::is_trivially_copy_constructible, - absl::is_trivially_copy_assignable, - absl::is_trivially_destructible>; - -template -void DestroyElements(AllocatorType* alloc_ptr, Pointer destroy_first, - SizeType destroy_size) { - using AllocatorTraits = absl::allocator_traits; - - if (destroy_first != nullptr) { - for (auto i = destroy_size; i != 0;) { - --i; - AllocatorTraits::destroy(*alloc_ptr, destroy_first + i); - } - -#if !defined(NDEBUG) - { - using ValueType = typename AllocatorTraits::value_type; - - // Overwrite unused memory with `0xab` so we can catch uninitialized - // usage. - // - // Cast to `void*` to tell the compiler that we don't care that we might - // be scribbling on a vtable pointer. - void* memory_ptr = destroy_first; - auto memory_size = destroy_size * sizeof(ValueType); - std::memset(memory_ptr, 0xab, memory_size); - } -#endif // !defined(NDEBUG) - } -} - -template -void ConstructElements(AllocatorType* alloc_ptr, Pointer construct_first, - ValueAdapter* values_ptr, SizeType construct_size) { - for (SizeType i = 0; i < construct_size; ++i) { - ABSL_INTERNAL_TRY { - values_ptr->ConstructNext(alloc_ptr, construct_first + i); - } - ABSL_INTERNAL_CATCH_ANY { - inlined_vector_internal::DestroyElements(alloc_ptr, construct_first, i); - ABSL_INTERNAL_RETHROW; - } - } -} - -template -void AssignElements(Pointer assign_first, ValueAdapter* values_ptr, - SizeType assign_size) { - for (SizeType i = 0; i < assign_size; ++i) { - values_ptr->AssignNext(assign_first + i); - } -} - -template -struct StorageView { - using AllocatorTraits = absl::allocator_traits; - using Pointer = typename AllocatorTraits::pointer; - using SizeType = typename AllocatorTraits::size_type; - - Pointer data; - SizeType size; - SizeType capacity; -}; - -template -class IteratorValueAdapter { - using AllocatorTraits = absl::allocator_traits; - using Pointer = typename AllocatorTraits::pointer; - - public: - explicit IteratorValueAdapter(const Iterator& it) : it_(it) {} - - void ConstructNext(AllocatorType* alloc_ptr, Pointer construct_at) { - AllocatorTraits::construct(*alloc_ptr, construct_at, *it_); - ++it_; - } - - void AssignNext(Pointer assign_at) { - *assign_at = *it_; - ++it_; - } - - private: - Iterator it_; -}; - -template -class CopyValueAdapter { - using AllocatorTraits = absl::allocator_traits; - using ValueType = typename AllocatorTraits::value_type; - using Pointer = typename AllocatorTraits::pointer; - using ConstPointer = typename AllocatorTraits::const_pointer; - - public: - explicit CopyValueAdapter(const ValueType& v) : ptr_(std::addressof(v)) {} - - void ConstructNext(AllocatorType* alloc_ptr, Pointer construct_at) { - AllocatorTraits::construct(*alloc_ptr, construct_at, *ptr_); - } - - void AssignNext(Pointer assign_at) { *assign_at = *ptr_; } - - private: - ConstPointer ptr_; -}; - -template -class DefaultValueAdapter { - using AllocatorTraits = absl::allocator_traits; - using ValueType = typename AllocatorTraits::value_type; - using Pointer = typename AllocatorTraits::pointer; - - public: - explicit DefaultValueAdapter() {} - - void ConstructNext(AllocatorType* alloc_ptr, Pointer construct_at) { - AllocatorTraits::construct(*alloc_ptr, construct_at); - } - - void AssignNext(Pointer assign_at) { *assign_at = ValueType(); } -}; - -template -class AllocationTransaction { - using AllocatorTraits = absl::allocator_traits; - using Pointer = typename AllocatorTraits::pointer; - using SizeType = typename AllocatorTraits::size_type; - - public: - explicit AllocationTransaction(AllocatorType* alloc_ptr) - : alloc_data_(*alloc_ptr, nullptr) {} - - ~AllocationTransaction() { - if (DidAllocate()) { - AllocatorTraits::deallocate(GetAllocator(), GetData(), GetCapacity()); - } - } - - AllocationTransaction(const AllocationTransaction&) = delete; - void operator=(const AllocationTransaction&) = delete; - - AllocatorType& GetAllocator() { return alloc_data_.template get<0>(); } - Pointer& GetData() { return alloc_data_.template get<1>(); } - SizeType& GetCapacity() { return capacity_; } - - bool DidAllocate() { return GetData() != nullptr; } - Pointer Allocate(SizeType capacity) { - GetData() = AllocatorTraits::allocate(GetAllocator(), capacity); - GetCapacity() = capacity; - return GetData(); - } - - void Reset() { - GetData() = nullptr; - GetCapacity() = 0; - } - - private: - container_internal::CompressedTuple alloc_data_; - SizeType capacity_ = 0; -}; - -template -class ConstructionTransaction { - using AllocatorTraits = absl::allocator_traits; - using Pointer = typename AllocatorTraits::pointer; - using SizeType = typename AllocatorTraits::size_type; - - public: - explicit ConstructionTransaction(AllocatorType* alloc_ptr) - : alloc_data_(*alloc_ptr, nullptr) {} - - ~ConstructionTransaction() { - if (DidConstruct()) { - inlined_vector_internal::DestroyElements(std::addressof(GetAllocator()), - GetData(), GetSize()); - } - } - - ConstructionTransaction(const ConstructionTransaction&) = delete; - void operator=(const ConstructionTransaction&) = delete; - - AllocatorType& GetAllocator() { return alloc_data_.template get<0>(); } - Pointer& GetData() { return alloc_data_.template get<1>(); } - SizeType& GetSize() { return size_; } - - bool DidConstruct() { return GetData() != nullptr; } - template - void Construct(Pointer data, ValueAdapter* values_ptr, SizeType size) { - inlined_vector_internal::ConstructElements(std::addressof(GetAllocator()), - data, values_ptr, size); - GetData() = data; - GetSize() = size; - } - void Commit() { - GetData() = nullptr; - GetSize() = 0; - } - - private: - container_internal::CompressedTuple alloc_data_; - SizeType size_ = 0; -}; - -template -class Storage { - public: - using AllocatorTraits = absl::allocator_traits; - 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 size_type = typename AllocatorTraits::size_type; - using difference_type = typename AllocatorTraits::difference_type; - - using reference = value_type&; - using const_reference = const value_type&; - using RValueReference = value_type&&; - using iterator = pointer; - using const_iterator = const_pointer; - using reverse_iterator = std::reverse_iterator; - using const_reverse_iterator = std::reverse_iterator; - using MoveIterator = std::move_iterator; - using IsMemcpyOk = inlined_vector_internal::IsMemcpyOk; - - using StorageView = inlined_vector_internal::StorageView; - - template - using IteratorValueAdapter = - inlined_vector_internal::IteratorValueAdapter; - using CopyValueAdapter = - inlined_vector_internal::CopyValueAdapter; - using DefaultValueAdapter = - inlined_vector_internal::DefaultValueAdapter; - - using AllocationTransaction = - inlined_vector_internal::AllocationTransaction; - using ConstructionTransaction = - inlined_vector_internal::ConstructionTransaction; - - static size_type NextCapacity(size_type current_capacity) { - return current_capacity * 2; - } - - static size_type ComputeCapacity(size_type current_capacity, - size_type requested_capacity) { - return (std::max)(NextCapacity(current_capacity), requested_capacity); - } - - // --------------------------------------------------------------------------- - // Storage Constructors and Destructor - // --------------------------------------------------------------------------- - - Storage() : metadata_() {} - - explicit Storage(const allocator_type& alloc) : metadata_(alloc, {}) {} - - ~Storage() { - pointer data = GetIsAllocated() ? GetAllocatedData() : GetInlinedData(); - inlined_vector_internal::DestroyElements(GetAllocPtr(), data, GetSize()); - DeallocateIfAllocated(); - } - - // --------------------------------------------------------------------------- - // Storage Member Accessors - // --------------------------------------------------------------------------- - - size_type& GetSizeAndIsAllocated() { return metadata_.template get<1>(); } - - const size_type& GetSizeAndIsAllocated() const { - return metadata_.template get<1>(); - } - - size_type GetSize() const { return GetSizeAndIsAllocated() >> 1; } - - bool GetIsAllocated() const { return GetSizeAndIsAllocated() & 1; } - - pointer GetAllocatedData() { return data_.allocated.allocated_data; } - - const_pointer GetAllocatedData() const { - return data_.allocated.allocated_data; - } - - pointer GetInlinedData() { - return reinterpret_cast( - std::addressof(data_.inlined.inlined_data[0])); - } - - const_pointer GetInlinedData() const { - return reinterpret_cast( - std::addressof(data_.inlined.inlined_data[0])); - } - - size_type GetAllocatedCapacity() const { - return data_.allocated.allocated_capacity; - } - - size_type GetInlinedCapacity() const { return static_cast(N); } - - StorageView MakeStorageView() { - return GetIsAllocated() - ? StorageView{GetAllocatedData(), GetSize(), - GetAllocatedCapacity()} - : StorageView{GetInlinedData(), GetSize(), GetInlinedCapacity()}; - } - - allocator_type* GetAllocPtr() { - return std::addressof(metadata_.template get<0>()); - } - - const allocator_type* GetAllocPtr() const { - return std::addressof(metadata_.template get<0>()); - } - - // --------------------------------------------------------------------------- - // Storage Member Mutators - // --------------------------------------------------------------------------- - - template - void Initialize(ValueAdapter values, size_type new_size); - - template - void Assign(ValueAdapter values, size_type new_size); - - template - void Resize(ValueAdapter values, size_type new_size); - - template - iterator Insert(const_iterator pos, ValueAdapter values, - size_type insert_count); - - template - reference EmplaceBack(Args&&... args); - - iterator Erase(const_iterator from, const_iterator to); - - void Reserve(size_type requested_capacity); - - void ShrinkToFit(); - - void Swap(Storage* other_storage_ptr); - - void SetIsAllocated() { - GetSizeAndIsAllocated() |= static_cast(1); - } - - void UnsetIsAllocated() { - GetSizeAndIsAllocated() &= ((std::numeric_limits::max)() - 1); - } - - void SetSize(size_type size) { - GetSizeAndIsAllocated() = - (size << 1) | static_cast(GetIsAllocated()); - } - - void SetAllocatedSize(size_type size) { - GetSizeAndIsAllocated() = (size << 1) | static_cast(1); - } - - void SetInlinedSize(size_type size) { - GetSizeAndIsAllocated() = size << static_cast(1); - } - - void AddSize(size_type count) { - GetSizeAndIsAllocated() += count << static_cast(1); - } - - void SubtractSize(size_type count) { - assert(count <= GetSize()); - - GetSizeAndIsAllocated() -= count << static_cast(1); - } - - void SetAllocatedData(pointer data, size_type capacity) { - data_.allocated.allocated_data = data; - data_.allocated.allocated_capacity = capacity; - } - - void AcquireAllocatedData(AllocationTransaction* allocation_tx_ptr) { - SetAllocatedData(allocation_tx_ptr->GetData(), - allocation_tx_ptr->GetCapacity()); - - allocation_tx_ptr->Reset(); - } - - void MemcpyFrom(const Storage& other_storage) { - assert(IsMemcpyOk::value || other_storage.GetIsAllocated()); - - GetSizeAndIsAllocated() = other_storage.GetSizeAndIsAllocated(); - data_ = other_storage.data_; - } - - void DeallocateIfAllocated() { - if (GetIsAllocated()) { - AllocatorTraits::deallocate(*GetAllocPtr(), GetAllocatedData(), - GetAllocatedCapacity()); - } - } - - private: - using Metadata = - container_internal::CompressedTuple; - - struct Allocated { - pointer allocated_data; - size_type allocated_capacity; - }; - - struct Inlined { - alignas(value_type) char inlined_data[sizeof(value_type[N])]; - }; - - union Data { - Allocated allocated; - Inlined inlined; - }; - - template - ABSL_ATTRIBUTE_NOINLINE reference EmplaceBackSlow(Args&&... args); - - Metadata metadata_; - Data data_; -}; - -template -template -auto Storage::Initialize(ValueAdapter values, size_type new_size) - -> void { - // Only callable from constructors! - assert(!GetIsAllocated()); - assert(GetSize() == 0); - - pointer construct_data; - if (new_size > GetInlinedCapacity()) { - // Because this is only called from the `InlinedVector` constructors, it's - // safe to take on the allocation with size `0`. If `ConstructElements(...)` - // throws, deallocation will be automatically handled by `~Storage()`. - size_type new_capacity = ComputeCapacity(GetInlinedCapacity(), new_size); - construct_data = AllocatorTraits::allocate(*GetAllocPtr(), new_capacity); - SetAllocatedData(construct_data, new_capacity); - SetIsAllocated(); - } else { - construct_data = GetInlinedData(); - } - - inlined_vector_internal::ConstructElements(GetAllocPtr(), construct_data, - &values, new_size); - - // Since the initial size was guaranteed to be `0` and the allocated bit is - // already correct for either case, *adding* `new_size` gives us the correct - // result faster than setting it directly. - AddSize(new_size); -} - -template -template -auto Storage::Assign(ValueAdapter values, size_type new_size) -> void { - StorageView storage_view = MakeStorageView(); - - AllocationTransaction allocation_tx(GetAllocPtr()); - - absl::Span assign_loop; - absl::Span construct_loop; - absl::Span destroy_loop; - - if (new_size > storage_view.capacity) { - size_type new_capacity = ComputeCapacity(storage_view.capacity, new_size); - construct_loop = {allocation_tx.Allocate(new_capacity), new_size}; - destroy_loop = {storage_view.data, storage_view.size}; - } else if (new_size > storage_view.size) { - assign_loop = {storage_view.data, storage_view.size}; - construct_loop = {storage_view.data + storage_view.size, - new_size - storage_view.size}; - } else { - assign_loop = {storage_view.data, new_size}; - destroy_loop = {storage_view.data + new_size, storage_view.size - new_size}; - } - - inlined_vector_internal::AssignElements(assign_loop.data(), &values, - assign_loop.size()); - - inlined_vector_internal::ConstructElements( - GetAllocPtr(), construct_loop.data(), &values, construct_loop.size()); - - inlined_vector_internal::DestroyElements(GetAllocPtr(), destroy_loop.data(), - destroy_loop.size()); - - if (allocation_tx.DidAllocate()) { - DeallocateIfAllocated(); - AcquireAllocatedData(&allocation_tx); - SetIsAllocated(); - } - - SetSize(new_size); -} - -template -template -auto Storage::Resize(ValueAdapter values, size_type new_size) -> void { - StorageView storage_view = MakeStorageView(); - auto* const base = storage_view.data; - const size_type size = storage_view.size; - auto* alloc = GetAllocPtr(); - if (new_size <= size) { - // Destroy extra old elements. - inlined_vector_internal::DestroyElements(alloc, base + new_size, - size - new_size); - } else if (new_size <= storage_view.capacity) { - // Construct new elements in place. - inlined_vector_internal::ConstructElements(alloc, base + size, &values, - new_size - size); - } else { - // Steps: - // a. Allocate new backing store. - // b. Construct new elements in new backing store. - // c. Move existing elements from old backing store to now. - // d. Destroy all elements in old backing store. - // Use transactional wrappers for the first two steps so we can roll - // back if necessary due to exceptions. - AllocationTransaction allocation_tx(alloc); - size_type new_capacity = ComputeCapacity(storage_view.capacity, new_size); - pointer new_data = allocation_tx.Allocate(new_capacity); - - ConstructionTransaction construction_tx(alloc); - construction_tx.Construct(new_data + size, &values, new_size - size); - - IteratorValueAdapter move_values((MoveIterator(base))); - inlined_vector_internal::ConstructElements(alloc, new_data, &move_values, - size); - - inlined_vector_internal::DestroyElements(alloc, base, size); - construction_tx.Commit(); - DeallocateIfAllocated(); - AcquireAllocatedData(&allocation_tx); - SetIsAllocated(); - } - SetSize(new_size); -} - -template -template -auto Storage::Insert(const_iterator pos, ValueAdapter values, - size_type insert_count) -> iterator { - StorageView storage_view = MakeStorageView(); - - size_type insert_index = - std::distance(const_iterator(storage_view.data), pos); - size_type insert_end_index = insert_index + insert_count; - size_type new_size = storage_view.size + insert_count; - - if (new_size > storage_view.capacity) { - AllocationTransaction allocation_tx(GetAllocPtr()); - ConstructionTransaction construction_tx(GetAllocPtr()); - ConstructionTransaction move_construciton_tx(GetAllocPtr()); - - IteratorValueAdapter move_values( - MoveIterator(storage_view.data)); - - size_type new_capacity = ComputeCapacity(storage_view.capacity, new_size); - pointer new_data = allocation_tx.Allocate(new_capacity); - - construction_tx.Construct(new_data + insert_index, &values, insert_count); - - move_construciton_tx.Construct(new_data, &move_values, insert_index); - - inlined_vector_internal::ConstructElements( - GetAllocPtr(), new_data + insert_end_index, &move_values, - storage_view.size - insert_index); - - inlined_vector_internal::DestroyElements(GetAllocPtr(), storage_view.data, - storage_view.size); - - construction_tx.Commit(); - move_construciton_tx.Commit(); - DeallocateIfAllocated(); - AcquireAllocatedData(&allocation_tx); - - SetAllocatedSize(new_size); - return iterator(new_data + insert_index); - } else { - size_type move_construction_destination_index = - (std::max)(insert_end_index, storage_view.size); - - ConstructionTransaction move_construction_tx(GetAllocPtr()); - - IteratorValueAdapter move_construction_values( - MoveIterator(storage_view.data + - (move_construction_destination_index - insert_count))); - absl::Span move_construction = { - storage_view.data + move_construction_destination_index, - new_size - move_construction_destination_index}; - - pointer move_assignment_values = storage_view.data + insert_index; - absl::Span move_assignment = { - storage_view.data + insert_end_index, - move_construction_destination_index - insert_end_index}; - - absl::Span insert_assignment = {move_assignment_values, - move_construction.size()}; - - absl::Span insert_construction = { - insert_assignment.data() + insert_assignment.size(), - insert_count - insert_assignment.size()}; - - move_construction_tx.Construct(move_construction.data(), - &move_construction_values, - move_construction.size()); - - for (pointer destination = move_assignment.data() + move_assignment.size(), - last_destination = move_assignment.data(), - source = move_assignment_values + move_assignment.size(); - ;) { - --destination; - --source; - if (destination < last_destination) break; - *destination = std::move(*source); - } - - inlined_vector_internal::AssignElements(insert_assignment.data(), &values, - insert_assignment.size()); - - inlined_vector_internal::ConstructElements( - GetAllocPtr(), insert_construction.data(), &values, - insert_construction.size()); - - move_construction_tx.Commit(); - - AddSize(insert_count); - return iterator(storage_view.data + insert_index); - } -} - -template -template -auto Storage::EmplaceBack(Args&&... args) -> reference { - StorageView storage_view = MakeStorageView(); - const auto n = storage_view.size; - if (ABSL_PREDICT_TRUE(n != storage_view.capacity)) { - // Fast path; new element fits. - pointer last_ptr = storage_view.data + n; - AllocatorTraits::construct(*GetAllocPtr(), last_ptr, - std::forward(args)...); - AddSize(1); - return *last_ptr; - } - // TODO(b/173712035): Annotate with musttail attribute to prevent regression. - return EmplaceBackSlow(std::forward(args)...); -} - -template -template -auto Storage::EmplaceBackSlow(Args&&... args) -> reference { - StorageView storage_view = MakeStorageView(); - AllocationTransaction allocation_tx(GetAllocPtr()); - IteratorValueAdapter move_values( - MoveIterator(storage_view.data)); - size_type new_capacity = NextCapacity(storage_view.capacity); - pointer construct_data = allocation_tx.Allocate(new_capacity); - pointer last_ptr = construct_data + storage_view.size; - - // Construct new element. - AllocatorTraits::construct(*GetAllocPtr(), last_ptr, - std::forward(args)...); - // Move elements from old backing store to new backing store. - ABSL_INTERNAL_TRY { - inlined_vector_internal::ConstructElements( - GetAllocPtr(), allocation_tx.GetData(), &move_values, - storage_view.size); - } - ABSL_INTERNAL_CATCH_ANY { - AllocatorTraits::destroy(*GetAllocPtr(), last_ptr); - ABSL_INTERNAL_RETHROW; - } - // Destroy elements in old backing store. - inlined_vector_internal::DestroyElements(GetAllocPtr(), storage_view.data, - storage_view.size); - - DeallocateIfAllocated(); - AcquireAllocatedData(&allocation_tx); - SetIsAllocated(); - AddSize(1); - return *last_ptr; -} - -template -auto Storage::Erase(const_iterator from, const_iterator to) - -> iterator { - StorageView storage_view = MakeStorageView(); - - size_type erase_size = std::distance(from, to); - size_type erase_index = - std::distance(const_iterator(storage_view.data), from); - size_type erase_end_index = erase_index + erase_size; - - IteratorValueAdapter move_values( - MoveIterator(storage_view.data + erase_end_index)); - - inlined_vector_internal::AssignElements(storage_view.data + erase_index, - &move_values, - storage_view.size - erase_end_index); - - inlined_vector_internal::DestroyElements( - GetAllocPtr(), storage_view.data + (storage_view.size - erase_size), - erase_size); - - SubtractSize(erase_size); - return iterator(storage_view.data + erase_index); -} - -template -auto Storage::Reserve(size_type requested_capacity) -> void { - StorageView storage_view = MakeStorageView(); - - if (ABSL_PREDICT_FALSE(requested_capacity <= storage_view.capacity)) return; - - AllocationTransaction allocation_tx(GetAllocPtr()); - - IteratorValueAdapter move_values( - MoveIterator(storage_view.data)); - - size_type new_capacity = - ComputeCapacity(storage_view.capacity, requested_capacity); - pointer new_data = allocation_tx.Allocate(new_capacity); - - inlined_vector_internal::ConstructElements(GetAllocPtr(), new_data, - &move_values, storage_view.size); - - inlined_vector_internal::DestroyElements(GetAllocPtr(), storage_view.data, - storage_view.size); - - DeallocateIfAllocated(); - AcquireAllocatedData(&allocation_tx); - SetIsAllocated(); -} - -template -auto Storage::ShrinkToFit() -> void { - // May only be called on allocated instances! - assert(GetIsAllocated()); - - StorageView storage_view{GetAllocatedData(), GetSize(), - GetAllocatedCapacity()}; - - if (ABSL_PREDICT_FALSE(storage_view.size == storage_view.capacity)) return; - - AllocationTransaction allocation_tx(GetAllocPtr()); - - IteratorValueAdapter move_values( - MoveIterator(storage_view.data)); - - pointer construct_data; - if (storage_view.size > GetInlinedCapacity()) { - size_type new_capacity = storage_view.size; - construct_data = allocation_tx.Allocate(new_capacity); - } else { - construct_data = GetInlinedData(); - } - - ABSL_INTERNAL_TRY { - inlined_vector_internal::ConstructElements(GetAllocPtr(), construct_data, - &move_values, storage_view.size); - } - ABSL_INTERNAL_CATCH_ANY { - SetAllocatedData(storage_view.data, storage_view.capacity); - ABSL_INTERNAL_RETHROW; - } - - inlined_vector_internal::DestroyElements(GetAllocPtr(), storage_view.data, - storage_view.size); - - AllocatorTraits::deallocate(*GetAllocPtr(), storage_view.data, - storage_view.capacity); - - if (allocation_tx.DidAllocate()) { - AcquireAllocatedData(&allocation_tx); - } else { - UnsetIsAllocated(); - } -} - -template -auto Storage::Swap(Storage* other_storage_ptr) -> void { - using std::swap; - assert(this != other_storage_ptr); - - if (GetIsAllocated() && other_storage_ptr->GetIsAllocated()) { - swap(data_.allocated, other_storage_ptr->data_.allocated); - } else if (!GetIsAllocated() && !other_storage_ptr->GetIsAllocated()) { - Storage* small_ptr = this; - Storage* large_ptr = other_storage_ptr; - if (small_ptr->GetSize() > large_ptr->GetSize()) swap(small_ptr, large_ptr); - - for (size_type i = 0; i < small_ptr->GetSize(); ++i) { - swap(small_ptr->GetInlinedData()[i], large_ptr->GetInlinedData()[i]); - } - - IteratorValueAdapter move_values( - MoveIterator(large_ptr->GetInlinedData() + small_ptr->GetSize())); - - inlined_vector_internal::ConstructElements( - large_ptr->GetAllocPtr(), - small_ptr->GetInlinedData() + small_ptr->GetSize(), &move_values, - large_ptr->GetSize() - small_ptr->GetSize()); - - inlined_vector_internal::DestroyElements( - large_ptr->GetAllocPtr(), - large_ptr->GetInlinedData() + small_ptr->GetSize(), - large_ptr->GetSize() - small_ptr->GetSize()); - } else { - Storage* allocated_ptr = this; - Storage* inlined_ptr = other_storage_ptr; - if (!allocated_ptr->GetIsAllocated()) swap(allocated_ptr, inlined_ptr); - - StorageView allocated_storage_view{allocated_ptr->GetAllocatedData(), - allocated_ptr->GetSize(), - allocated_ptr->GetAllocatedCapacity()}; - - IteratorValueAdapter move_values( - MoveIterator(inlined_ptr->GetInlinedData())); - - ABSL_INTERNAL_TRY { - inlined_vector_internal::ConstructElements( - inlined_ptr->GetAllocPtr(), allocated_ptr->GetInlinedData(), - &move_values, inlined_ptr->GetSize()); - } - ABSL_INTERNAL_CATCH_ANY { - allocated_ptr->SetAllocatedData(allocated_storage_view.data, - allocated_storage_view.capacity); - ABSL_INTERNAL_RETHROW; - } - - inlined_vector_internal::DestroyElements(inlined_ptr->GetAllocPtr(), - inlined_ptr->GetInlinedData(), - inlined_ptr->GetSize()); - - inlined_ptr->SetAllocatedData(allocated_storage_view.data, - allocated_storage_view.capacity); - } - - swap(GetSizeAndIsAllocated(), other_storage_ptr->GetSizeAndIsAllocated()); - swap(*GetAllocPtr(), *other_storage_ptr->GetAllocPtr()); -} - -} // namespace inlined_vector_internal -ABSL_NAMESPACE_END -} // namespace absl - -#endif // ABSL_CONTAINER_INTERNAL_INLINED_VECTOR_INTERNAL_H_ diff --git a/third_party/abseil_cpp/absl/container/internal/layout.h b/third_party/abseil_cpp/absl/container/internal/layout.h deleted file mode 100644 index 233678331543..000000000000 --- a/third_party/abseil_cpp/absl/container/internal/layout.h +++ /dev/null @@ -1,743 +0,0 @@ -// 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 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 p(new unsigned char[layout.AllocSize()]); -// -// // Obtain the pointer to the array of doubles. -// // Equivalent to `reinterpret_cast(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(p.get()); -// -// // Obtain the pointer to the array of ints. -// // Equivalent to `reinterpret_cast(p.get() + N * 8)`. -// int* b = layout.Pointer(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[1] containing N, size_t[1] containing M, double[N], int[M]. -// using L = Layout; -// -// 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(p); -// int* b = layout.Pointer(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 generates -// optimal code. All computations that can be performed at compile time are -// indeed performed at compile time. -// -// Efficiency tip: The order of fields matters. In `Layout` 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`. `Layout<..., Aligned, ...>` 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(1, 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(p_.get()) = size; -// // Store the characters in the allocation. -// memcpy(layout.Pointer(p_.get()), s, size + 1); -// } -// -// size_t size() const { -// // Equivalent to reinterpret_cast(*p). -// return *L::Partial().Pointer(p_.get()); -// } -// -// const char* c_str() const { -// // Equivalent to reinterpret_cast(p.get() + sizeof(size_t)). -// // The argument in Partial(1) specifies that we have size_t[1] in front -// // of the characters. -// return L::Partial(1).Pointer(p_.get()); -// } -// -// private: -// // Our heap allocation contains a size_t followed by an array of chars. -// using L = Layout; -// std::unique_ptr 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 class `internal_layout::LayoutImpl<>`. That class -// isn't intended to be used directly, and its name and template parameter -// list are internal implementation details, but the class itself provides -// most of the functionality in this file. See comments on its members for -// detailed documentation. -// -// `Layout::Partial(count1,..., countm)` (where `m` <= `n`) returns a -// `LayoutImpl<>` object. `Layout 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 -#include -#include - -#include -#include -#include -#include -#include -#include - -#include "absl/base/config.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 -#endif - -#if defined(__GXX_RTTI) -#define ABSL_INTERNAL_HAS_CXA_DEMANGLE -#endif - -#ifdef ABSL_INTERNAL_HAS_CXA_DEMANGLE -#include -#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, ...>` 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 -struct Aligned; - -namespace internal_layout { - -template -struct NotAligned {}; - -template -struct NotAligned> { - static_assert(sizeof(T) == 0, "Aligned cannot be const-qualified"); -}; - -template -using IntToSize = size_t; - -template -using TypeToSize = size_t; - -template -struct Type : NotAligned { - using type = T; -}; - -template -struct Type> { - using type = T; -}; - -template -struct SizeOf : NotAligned, std::integral_constant {}; - -template -struct SizeOf> : std::integral_constant {}; - -// Note: workaround for https://gcc.gnu.org/PR88115 -template -struct AlignOf : NotAligned { - static constexpr size_t value = alignof(T); -}; - -template -struct AlignOf> { - 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 -using Contains = absl::disjunction...>; - -template -using CopyConst = - typename std::conditional::value, const To, To>::type; - -// Note: We're not qualifying this with absl:: because it doesn't compile under -// MSVC. -template -using SliceType = Span; - -// This namespace contains no types. It prevents functions defined in it from -// being found by ADL. -namespace adl_barrier { - -template -constexpr size_t Find(Needle, Needle, Ts...) { - static_assert(!Contains(), "Duplicate element type"); - return 0; -} - -template -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 -constexpr size_t Max(size_t a, size_t b, Ts... rest) { - return adl_barrier::Max(b < a ? a : b, rest...); -} - -template -std::string TypeName() { - std::string out; - int status = 0; - char* demangled = nullptr; -#ifdef ABSL_INTERNAL_HAS_CXA_DEMANGLE - demangled = abi::__cxa_demangle(typeid(T).name(), nullptr, nullptr, &status); -#endif - if (status == 0 && demangled != nullptr) { // Demangling succeeded. - absl::StrAppend(&out, "<", demangled, ">"); - free(demangled); - } else { -#if defined(__GXX_RTTI) || defined(_CPPRTTI) - absl::StrAppend(&out, "<", typeid(T).name(), ">"); -#endif - } - return out; -} - -} // namespace adl_barrier - -template -using EnableIf = typename std::enable_if::type; - -// Can `T` be a template argument of `Layout`? -template -using IsLegalElementType = std::integral_constant< - bool, !std::is_reference::value && !std::is_volatile::value && - !std::is_reference::type>::value && - !std::is_volatile::type>::value && - adl_barrier::IsPow2(AlignOf::value)>; - -template -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. -// -// `SizeSeq...` is `[0, NumSizes)` where `NumSizes` is the number of arguments -// passed to `Layout::Partial()` or `Layout::Layout()`. -// -// `OffsetSeq...` is `[0, NumOffsets)` where `NumOffsets` is -// `Min(sizeof...(Elements), NumSizes + 1)` (the number of arrays for which we -// can compute offsets). -template -class LayoutImpl, absl::index_sequence, - absl::index_sequence> { - private: - static_assert(sizeof...(Elements) > 0, "At least one field is required"); - static_assert(absl::conjunction...>::value, - "Invalid element type (see IsLegalElementType)"); - - enum { - NumTypes = sizeof...(Elements), - NumSizes = sizeof...(SizeSeq), - NumOffsets = sizeof...(OffsetSeq), - }; - - // These are guaranteed by `Layout`. - static_assert(NumOffsets == adl_barrier::Min(NumTypes, NumSizes + 1), - "Internal error"); - static_assert(NumTypes > 0, "Internal error"); - - // Returns the index of `T` in `Elements...`. Results in a compilation error - // if `Elements...` doesn't contain exactly one instance of `T`. - template - static constexpr size_t ElementIndex() { - static_assert(Contains, Type::type>...>(), - "Type not found"); - return adl_barrier::Find(Type(), - Type::type>()...); - } - - template - using ElementAlignment = - AlignOf>::type>; - - public: - // Element types of all arrays packed in a tuple. - using ElementTypes = std::tuple::type...>; - - // Element type of the Nth array. - template - using ElementType = typename std::tuple_element::type; - - constexpr explicit LayoutImpl(IntToSize... 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::value...); - } - - // Offset in bytes of the Nth array. - // - // // int[3], 4 bytes of padding, double[4]. - // Layout 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 = 0> - constexpr size_t Offset() const { - return 0; - } - - template = 0> - constexpr size_t Offset() const { - static_assert(N < NumOffsets, "Index out of bounds"); - return adl_barrier::Align( - Offset() + SizeOf>() * size_[N - 1], - ElementAlignment::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 x(3, 4); - // assert(x.Offset() == 0); // The ints starts from 0. - // assert(x.Offset() == 16); // The doubles starts from 16. - template - constexpr size_t Offset() const { - return Offset()>(); - } - - // Offsets in bytes of all arrays for which the offsets are known. - constexpr std::array Offsets() const { - return {{Offset()...}}; - } - - // The number of elements in the Nth array. This is the Nth argument of - // `Layout::Partial()` or `Layout::Layout()` (zero-based). - // - // // int[3], 4 bytes of padding, double[4]. - // Layout x(3, 4); - // assert(x.Size<0>() == 3); - // assert(x.Size<1>() == 4); - // - // Requires: `N < NumSizes`. - template - constexpr size_t Size() const { - static_assert(N < NumSizes, "Index out of bounds"); - return size_[N]; - } - - // 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 x(3, 4); - // assert(x.Size() == 3); - // assert(x.Size() == 4); - template - constexpr size_t Size() const { - return Size()>(); - } - - // The number of elements of all arrays for which they are known. - constexpr std::array Sizes() const { - return {{Size()...}}; - } - - // Pointer to the beginning of the Nth array. - // - // `Char` must be `[const] [signed|unsigned] char`. - // - // // int[3], 4 bytes of padding, double[4]. - // Layout 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 - CopyConst>* Pointer(Char* p) const { - using C = typename std::remove_const::type; - static_assert( - std::is_same() || std::is_same() || - std::is_same(), - "The argument must be a pointer to [const] [signed|unsigned] char"); - constexpr size_t alignment = Alignment(); - (void)alignment; - assert(reinterpret_cast(p) % alignment == 0); - return reinterpret_cast>*>(p + Offset()); - } - - // 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 x(3, 4); - // unsigned char* p = new unsigned char[x.AllocSize()]; - // int* ints = x.Pointer(p); - // double* doubles = x.Pointer(p); - // - // Requires: `p` is aligned to `Alignment()`. - template - CopyConst* Pointer(Char* p) const { - return Pointer()>(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 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()`. - // - // Note: We're not using ElementType alias here because it does not compile - // under MSVC. - template - std::tuple::type>*...> - Pointers(Char* p) const { - return std::tuple>*...>( - Pointer(p)...); - } - - // The Nth array. - // - // `Char` must be `[const] [signed|unsigned] char`. - // - // // int[3], 4 bytes of padding, double[4]. - // Layout x(3, 4); - // unsigned char* p = new unsigned char[x.AllocSize()]; - // Span ints = x.Slice<0>(p); - // Span doubles = x.Slice<1>(p); - // - // Requires: `N < NumSizes`. - // Requires: `p` is aligned to `Alignment()`. - template - SliceType>> Slice(Char* p) const { - return SliceType>>(Pointer(p), Size()); - } - - // 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 x(3, 4); - // unsigned char* p = new unsigned char[x.AllocSize()]; - // Span ints = x.Slice(p); - // Span doubles = x.Slice(p); - // - // Requires: `p` is aligned to `Alignment()`. - template - SliceType> Slice(Char* p) const { - return Slice()>(p); - } - - // All arrays with known sizes. - // - // `Char` must be `[const] [signed|unsigned] char`. - // - // // int[3], 4 bytes of padding, double[4]. - // Layout x(3, 4); - // unsigned char* p = new unsigned char[x.AllocSize()]; - // - // Span ints; - // Span doubles; - // std::tie(ints, doubles) = x.Slices(p); - // - // Requires: `p` is aligned to `Alignment()`. - // - // Note: We're not using ElementType alias here because it does not compile - // under MSVC. - template - std::tuple::type>>...> - Slices(Char* p) const { - // Workaround for https://gcc.gnu.org/bugzilla/show_bug.cgi?id=63875 (fixed - // in 6.1). - (void)p; - return std::tuple>>...>( - Slice(p)...); - } - - // The size of the allocation that fits all arrays. - // - // // int[3], 4 bytes of padding, double[4]. - // Layout 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() + - SizeOf>() * 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 = 0> - void PoisonPadding(const Char* p) const { - Pointer<0>(p); // verify the requirements on `Char` and `p` - } - - template = 0> - void PoisonPadding(const Char* p) const { - static_assert(N < NumOffsets, "Index out of bounds"); - (void)p; -#ifdef ABSL_HAVE_ADDRESS_SANITIZER - PoisonPadding(p); - // The `if` is an optimization. It doesn't affect the observable behaviour. - if (ElementAlignment::value % ElementAlignment::value) { - size_t start = - Offset() + SizeOf>() * size_[N - 1]; - ASAN_POISON_MEMORY_REGION(p + start, Offset() - 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::Partial(5, 3); - // assert(x.DebugString() == - // "@0(1)[5]; @8(4)[3]; @24(8)"); - // - // Each field is in the following format: @offset(sizeof)[size] ( - // may be missing depending on the target platform). For example, - // @8(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>()...}; - const std::string types[] = { - adl_barrier::TypeName>()...}; - std::string res = absl::StrCat("@0", types[0], "(", sizes[0], ")"); - for (size_t i = 0; i != NumOffsets - 1; ++i) { - absl::StrAppend(&res, "[", size_[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(NumSizes) - 1; - if (NumTypes == NumSizes && last >= 0) { - absl::StrAppend(&res, "[", size_[last], "]"); - } - return res; - } - - private: - // Arguments of `Layout::Partial()` or `Layout::Layout()`. - size_t size_[NumSizes > 0 ? NumSizes : 1]; -}; - -template -using LayoutType = LayoutImpl< - std::tuple, absl::make_index_sequence, - absl::make_index_sequence>; - -} // 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::LayoutImpl above. The type is -// internal to the library but its methods are public, and they are inherited -// by `Layout`. -template -class Layout : public internal_layout::LayoutType { - public: - static_assert(sizeof...(Ts) > 0, "At least one field is required"); - static_assert( - absl::conjunction...>::value, - "Invalid element type (see IsLegalElementType)"); - - // The result type of `Partial()` with `NumSizes` arguments. - template - using PartialType = internal_layout::LayoutType; - - // `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::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 x(3, 5); - // - // Note: The sizes of the arrays must be specified in number of elements, - // not in bytes. - // - // Requires: `sizeof...(Sizes) <= sizeof...(Ts)`. - // Requires: all arguments are convertible to `size_t`. - template - static constexpr PartialType Partial(Sizes&&... sizes) { - static_assert(sizeof...(Sizes) <= sizeof...(Ts), ""); - return PartialType(absl::forward(sizes)...); - } - - // 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. - constexpr explicit Layout(internal_layout::TypeToSize... sizes) - : internal_layout::LayoutType(sizes...) {} -}; - -} // namespace container_internal -ABSL_NAMESPACE_END -} // namespace absl - -#endif // ABSL_CONTAINER_INTERNAL_LAYOUT_H_ diff --git a/third_party/abseil_cpp/absl/container/internal/layout_test.cc b/third_party/abseil_cpp/absl/container/internal/layout_test.cc deleted file mode 100644 index 1d7158ffc0b9..000000000000 --- a/third_party/abseil_cpp/absl/container/internal/layout_test.cc +++ /dev/null @@ -1,1635 +0,0 @@ -// 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/layout.h" - -// We need ::max_align_t because some libstdc++ versions don't provide -// std::max_align_t -#include - -#include -#include -#include -#include - -#include "gmock/gmock.h" -#include "gtest/gtest.h" -#include "absl/base/config.h" -#include "absl/base/internal/raw_logging.h" -#include "absl/types/span.h" - -namespace absl { -ABSL_NAMESPACE_BEGIN -namespace container_internal { -namespace { - -using ::absl::Span; -using ::testing::ElementsAre; - -size_t Distance(const void* from, const void* to) { - ABSL_RAW_CHECK(from <= to, "Distance must be non-negative"); - return static_cast(to) - static_cast(from); -} - -template -Expected Type(Actual val) { - static_assert(std::is_same(), ""); - return val; -} - -// Helper classes to test different size and alignments. -struct alignas(8) Int128 { - uint64_t a, b; - friend bool operator==(Int128 lhs, Int128 rhs) { - return std::tie(lhs.a, lhs.b) == std::tie(rhs.a, rhs.b); - } - - static std::string Name() { - return internal_layout::adl_barrier::TypeName(); - } -}; - -// int64_t is *not* 8-byte aligned on all platforms! -struct alignas(8) Int64 { - int64_t a; - friend bool operator==(Int64 lhs, Int64 rhs) { - return lhs.a == rhs.a; - } -}; - -// Properties of types that this test relies on. -static_assert(sizeof(int8_t) == 1, ""); -static_assert(alignof(int8_t) == 1, ""); -static_assert(sizeof(int16_t) == 2, ""); -static_assert(alignof(int16_t) == 2, ""); -static_assert(sizeof(int32_t) == 4, ""); -static_assert(alignof(int32_t) == 4, ""); -static_assert(sizeof(Int64) == 8, ""); -static_assert(alignof(Int64) == 8, ""); -static_assert(sizeof(Int128) == 16, ""); -static_assert(alignof(Int128) == 8, ""); - -template -void SameType() { - static_assert(std::is_same(), ""); -} - -TEST(Layout, ElementType) { - { - using L = Layout; - SameType>(); - SameType>(); - SameType>(); - } - { - using L = Layout; - SameType>(); - SameType>(); - SameType>(); - SameType>(); - SameType>(); - SameType>(); - } - { - using L = Layout; - SameType>(); - SameType>(); - SameType>(); - SameType>(); - SameType>(); - SameType>(); - SameType>(); - SameType>(); - SameType>(); - SameType>(); - SameType>(); - SameType>(); - } -} - -TEST(Layout, ElementTypes) { - { - using L = Layout; - SameType, L::ElementTypes>(); - SameType, decltype(L::Partial())::ElementTypes>(); - SameType, decltype(L::Partial(0))::ElementTypes>(); - } - { - using L = Layout; - SameType, L::ElementTypes>(); - SameType, - decltype(L::Partial())::ElementTypes>(); - SameType, - decltype(L::Partial(0))::ElementTypes>(); - } - { - using L = Layout; - SameType, L::ElementTypes>(); - SameType, - decltype(L::Partial())::ElementTypes>(); - SameType, - decltype(L::Partial(0))::ElementTypes>(); - SameType, - decltype(L::Partial(0, 0))::ElementTypes>(); - SameType, - decltype(L::Partial(0, 0, 0))::ElementTypes>(); - } -} - -TEST(Layout, OffsetByIndex) { - { - using L = Layout; - EXPECT_EQ(0, L::Partial().Offset<0>()); - EXPECT_EQ(0, L::Partial(3).Offset<0>()); - EXPECT_EQ(0, L(3).Offset<0>()); - } - { - using L = Layout; - EXPECT_EQ(0, L::Partial().Offset<0>()); - EXPECT_EQ(0, L::Partial(3).Offset<0>()); - EXPECT_EQ(12, L::Partial(3).Offset<1>()); - EXPECT_EQ(0, L::Partial(3, 5).Offset<0>()); - EXPECT_EQ(12, L::Partial(3, 5).Offset<1>()); - EXPECT_EQ(0, L(3, 5).Offset<0>()); - EXPECT_EQ(12, L(3, 5).Offset<1>()); - } - { - using L = Layout; - EXPECT_EQ(0, L::Partial().Offset<0>()); - EXPECT_EQ(0, L::Partial(0).Offset<0>()); - EXPECT_EQ(0, L::Partial(0).Offset<1>()); - EXPECT_EQ(0, L::Partial(1).Offset<0>()); - EXPECT_EQ(4, L::Partial(1).Offset<1>()); - EXPECT_EQ(0, L::Partial(5).Offset<0>()); - EXPECT_EQ(8, L::Partial(5).Offset<1>()); - EXPECT_EQ(0, L::Partial(0, 0).Offset<0>()); - EXPECT_EQ(0, L::Partial(0, 0).Offset<1>()); - EXPECT_EQ(0, L::Partial(0, 0).Offset<2>()); - EXPECT_EQ(0, L::Partial(1, 0).Offset<0>()); - EXPECT_EQ(4, L::Partial(1, 0).Offset<1>()); - EXPECT_EQ(8, L::Partial(1, 0).Offset<2>()); - EXPECT_EQ(0, L::Partial(5, 3).Offset<0>()); - EXPECT_EQ(8, L::Partial(5, 3).Offset<1>()); - EXPECT_EQ(24, L::Partial(5, 3).Offset<2>()); - EXPECT_EQ(0, L::Partial(0, 0, 0).Offset<0>()); - EXPECT_EQ(0, L::Partial(0, 0, 0).Offset<1>()); - EXPECT_EQ(0, L::Partial(0, 0, 0).Offset<2>()); - EXPECT_EQ(0, L::Partial(1, 0, 0).Offset<0>()); - EXPECT_EQ(4, L::Partial(1, 0, 0).Offset<1>()); - EXPECT_EQ(8, L::Partial(1, 0, 0).Offset<2>()); - EXPECT_EQ(0, L::Partial(5, 3, 1).Offset<0>()); - EXPECT_EQ(24, L::Partial(5, 3, 1).Offset<2>()); - EXPECT_EQ(8, L::Partial(5, 3, 1).Offset<1>()); - EXPECT_EQ(0, L(5, 3, 1).Offset<0>()); - EXPECT_EQ(24, L(5, 3, 1).Offset<2>()); - EXPECT_EQ(8, L(5, 3, 1).Offset<1>()); - } -} - -TEST(Layout, OffsetByType) { - { - using L = Layout; - EXPECT_EQ(0, L::Partial().Offset()); - EXPECT_EQ(0, L::Partial(3).Offset()); - EXPECT_EQ(0, L(3).Offset()); - } - { - using L = Layout; - EXPECT_EQ(0, L::Partial().Offset()); - EXPECT_EQ(0, L::Partial(0).Offset()); - EXPECT_EQ(0, L::Partial(0).Offset()); - EXPECT_EQ(0, L::Partial(1).Offset()); - EXPECT_EQ(4, L::Partial(1).Offset()); - EXPECT_EQ(0, L::Partial(5).Offset()); - EXPECT_EQ(8, L::Partial(5).Offset()); - EXPECT_EQ(0, L::Partial(0, 0).Offset()); - EXPECT_EQ(0, L::Partial(0, 0).Offset()); - EXPECT_EQ(0, L::Partial(0, 0).Offset()); - EXPECT_EQ(0, L::Partial(1, 0).Offset()); - EXPECT_EQ(4, L::Partial(1, 0).Offset()); - EXPECT_EQ(8, L::Partial(1, 0).Offset()); - EXPECT_EQ(0, L::Partial(5, 3).Offset()); - EXPECT_EQ(8, L::Partial(5, 3).Offset()); - EXPECT_EQ(24, L::Partial(5, 3).Offset()); - EXPECT_EQ(0, L::Partial(0, 0, 0).Offset()); - EXPECT_EQ(0, L::Partial(0, 0, 0).Offset()); - EXPECT_EQ(0, L::Partial(0, 0, 0).Offset()); - EXPECT_EQ(0, L::Partial(1, 0, 0).Offset()); - EXPECT_EQ(4, L::Partial(1, 0, 0).Offset()); - EXPECT_EQ(8, L::Partial(1, 0, 0).Offset()); - EXPECT_EQ(0, L::Partial(5, 3, 1).Offset()); - EXPECT_EQ(24, L::Partial(5, 3, 1).Offset()); - EXPECT_EQ(8, L::Partial(5, 3, 1).Offset()); - EXPECT_EQ(0, L(5, 3, 1).Offset()); - EXPECT_EQ(24, L(5, 3, 1).Offset()); - EXPECT_EQ(8, L(5, 3, 1).Offset()); - } -} - -TEST(Layout, Offsets) { - { - using L = Layout; - EXPECT_THAT(L::Partial().Offsets(), ElementsAre(0)); - EXPECT_THAT(L::Partial(3).Offsets(), ElementsAre(0)); - EXPECT_THAT(L(3).Offsets(), ElementsAre(0)); - } - { - using L = Layout; - EXPECT_THAT(L::Partial().Offsets(), ElementsAre(0)); - EXPECT_THAT(L::Partial(3).Offsets(), ElementsAre(0, 12)); - EXPECT_THAT(L::Partial(3, 5).Offsets(), ElementsAre(0, 12)); - EXPECT_THAT(L(3, 5).Offsets(), ElementsAre(0, 12)); - } - { - using L = Layout; - EXPECT_THAT(L::Partial().Offsets(), ElementsAre(0)); - EXPECT_THAT(L::Partial(1).Offsets(), ElementsAre(0, 4)); - EXPECT_THAT(L::Partial(5).Offsets(), ElementsAre(0, 8)); - EXPECT_THAT(L::Partial(0, 0).Offsets(), ElementsAre(0, 0, 0)); - EXPECT_THAT(L::Partial(1, 0).Offsets(), ElementsAre(0, 4, 8)); - EXPECT_THAT(L::Partial(5, 3).Offsets(), ElementsAre(0, 8, 24)); - EXPECT_THAT(L::Partial(0, 0, 0).Offsets(), ElementsAre(0, 0, 0)); - EXPECT_THAT(L::Partial(1, 0, 0).Offsets(), ElementsAre(0, 4, 8)); - EXPECT_THAT(L::Partial(5, 3, 1).Offsets(), ElementsAre(0, 8, 24)); - EXPECT_THAT(L(5, 3, 1).Offsets(), ElementsAre(0, 8, 24)); - } -} - -TEST(Layout, AllocSize) { - { - using L = Layout; - EXPECT_EQ(0, L::Partial(0).AllocSize()); - EXPECT_EQ(12, L::Partial(3).AllocSize()); - EXPECT_EQ(12, L(3).AllocSize()); - } - { - using L = Layout; - EXPECT_EQ(32, L::Partial(3, 5).AllocSize()); - EXPECT_EQ(32, L(3, 5).AllocSize()); - } - { - using L = Layout; - EXPECT_EQ(0, L::Partial(0, 0, 0).AllocSize()); - EXPECT_EQ(8, L::Partial(1, 0, 0).AllocSize()); - EXPECT_EQ(8, L::Partial(0, 1, 0).AllocSize()); - EXPECT_EQ(16, L::Partial(0, 0, 1).AllocSize()); - EXPECT_EQ(24, L::Partial(1, 1, 1).AllocSize()); - EXPECT_EQ(136, L::Partial(3, 5, 7).AllocSize()); - EXPECT_EQ(136, L(3, 5, 7).AllocSize()); - } -} - -TEST(Layout, SizeByIndex) { - { - using L = Layout; - EXPECT_EQ(0, L::Partial(0).Size<0>()); - EXPECT_EQ(3, L::Partial(3).Size<0>()); - EXPECT_EQ(3, L(3).Size<0>()); - } - { - using L = Layout; - EXPECT_EQ(0, L::Partial(0).Size<0>()); - EXPECT_EQ(3, L::Partial(3).Size<0>()); - EXPECT_EQ(3, L::Partial(3, 5).Size<0>()); - EXPECT_EQ(5, L::Partial(3, 5).Size<1>()); - EXPECT_EQ(3, L(3, 5).Size<0>()); - EXPECT_EQ(5, L(3, 5).Size<1>()); - } - { - using L = Layout; - EXPECT_EQ(3, L::Partial(3).Size<0>()); - EXPECT_EQ(3, L::Partial(3, 5).Size<0>()); - EXPECT_EQ(5, L::Partial(3, 5).Size<1>()); - EXPECT_EQ(3, L::Partial(3, 5, 7).Size<0>()); - EXPECT_EQ(5, L::Partial(3, 5, 7).Size<1>()); - EXPECT_EQ(7, L::Partial(3, 5, 7).Size<2>()); - EXPECT_EQ(3, L(3, 5, 7).Size<0>()); - EXPECT_EQ(5, L(3, 5, 7).Size<1>()); - EXPECT_EQ(7, L(3, 5, 7).Size<2>()); - } -} - -TEST(Layout, SizeByType) { - { - using L = Layout; - EXPECT_EQ(0, L::Partial(0).Size()); - EXPECT_EQ(3, L::Partial(3).Size()); - EXPECT_EQ(3, L(3).Size()); - } - { - using L = Layout; - EXPECT_EQ(3, L::Partial(3).Size()); - EXPECT_EQ(3, L::Partial(3, 5).Size()); - EXPECT_EQ(5, L::Partial(3, 5).Size()); - EXPECT_EQ(3, L::Partial(3, 5, 7).Size()); - EXPECT_EQ(5, L::Partial(3, 5, 7).Size()); - EXPECT_EQ(7, L::Partial(3, 5, 7).Size()); - EXPECT_EQ(3, L(3, 5, 7).Size()); - EXPECT_EQ(5, L(3, 5, 7).Size()); - EXPECT_EQ(7, L(3, 5, 7).Size()); - } -} - -TEST(Layout, Sizes) { - { - using L = Layout; - EXPECT_THAT(L::Partial().Sizes(), ElementsAre()); - EXPECT_THAT(L::Partial(3).Sizes(), ElementsAre(3)); - EXPECT_THAT(L(3).Sizes(), ElementsAre(3)); - } - { - using L = Layout; - EXPECT_THAT(L::Partial().Sizes(), ElementsAre()); - EXPECT_THAT(L::Partial(3).Sizes(), ElementsAre(3)); - EXPECT_THAT(L::Partial(3, 5).Sizes(), ElementsAre(3, 5)); - EXPECT_THAT(L(3, 5).Sizes(), ElementsAre(3, 5)); - } - { - using L = Layout; - EXPECT_THAT(L::Partial().Sizes(), ElementsAre()); - EXPECT_THAT(L::Partial(3).Sizes(), ElementsAre(3)); - EXPECT_THAT(L::Partial(3, 5).Sizes(), ElementsAre(3, 5)); - EXPECT_THAT(L::Partial(3, 5, 7).Sizes(), ElementsAre(3, 5, 7)); - EXPECT_THAT(L(3, 5, 7).Sizes(), ElementsAre(3, 5, 7)); - } -} - -TEST(Layout, PointerByIndex) { - alignas(max_align_t) const unsigned char p[100] = {}; - { - using L = Layout; - EXPECT_EQ(0, Distance(p, Type(L::Partial().Pointer<0>(p)))); - EXPECT_EQ(0, - Distance(p, Type(L::Partial(3).Pointer<0>(p)))); - EXPECT_EQ(0, Distance(p, Type(L(3).Pointer<0>(p)))); - } - { - using L = Layout; - EXPECT_EQ(0, Distance(p, Type(L::Partial().Pointer<0>(p)))); - EXPECT_EQ(0, - Distance(p, Type(L::Partial(3).Pointer<0>(p)))); - EXPECT_EQ(12, - Distance(p, Type(L::Partial(3).Pointer<1>(p)))); - EXPECT_EQ( - 0, Distance(p, Type(L::Partial(3, 5).Pointer<0>(p)))); - EXPECT_EQ( - 12, Distance(p, Type(L::Partial(3, 5).Pointer<1>(p)))); - EXPECT_EQ(0, Distance(p, Type(L(3, 5).Pointer<0>(p)))); - EXPECT_EQ(12, Distance(p, Type(L(3, 5).Pointer<1>(p)))); - } - { - using L = Layout; - EXPECT_EQ(0, Distance(p, Type(L::Partial().Pointer<0>(p)))); - EXPECT_EQ(0, Distance(p, Type(L::Partial(0).Pointer<0>(p)))); - EXPECT_EQ(0, - Distance(p, Type(L::Partial(0).Pointer<1>(p)))); - EXPECT_EQ(0, Distance(p, Type(L::Partial(1).Pointer<0>(p)))); - EXPECT_EQ(4, - Distance(p, Type(L::Partial(1).Pointer<1>(p)))); - EXPECT_EQ(0, Distance(p, Type(L::Partial(5).Pointer<0>(p)))); - EXPECT_EQ(8, - Distance(p, Type(L::Partial(5).Pointer<1>(p)))); - EXPECT_EQ(0, - Distance(p, Type(L::Partial(0, 0).Pointer<0>(p)))); - EXPECT_EQ( - 0, Distance(p, Type(L::Partial(0, 0).Pointer<1>(p)))); - EXPECT_EQ(0, - Distance(p, Type(L::Partial(0, 0).Pointer<2>(p)))); - EXPECT_EQ(0, - Distance(p, Type(L::Partial(1, 0).Pointer<0>(p)))); - EXPECT_EQ( - 4, Distance(p, Type(L::Partial(1, 0).Pointer<1>(p)))); - EXPECT_EQ(8, - Distance(p, Type(L::Partial(1, 0).Pointer<2>(p)))); - EXPECT_EQ(0, - Distance(p, Type(L::Partial(5, 3).Pointer<0>(p)))); - EXPECT_EQ( - 8, Distance(p, Type(L::Partial(5, 3).Pointer<1>(p)))); - EXPECT_EQ(24, - Distance(p, Type(L::Partial(5, 3).Pointer<2>(p)))); - EXPECT_EQ( - 0, Distance(p, Type(L::Partial(0, 0, 0).Pointer<0>(p)))); - EXPECT_EQ( - 0, - Distance(p, Type(L::Partial(0, 0, 0).Pointer<1>(p)))); - EXPECT_EQ( - 0, Distance(p, Type(L::Partial(0, 0, 0).Pointer<2>(p)))); - EXPECT_EQ( - 0, Distance(p, Type(L::Partial(1, 0, 0).Pointer<0>(p)))); - EXPECT_EQ( - 4, - Distance(p, Type(L::Partial(1, 0, 0).Pointer<1>(p)))); - EXPECT_EQ( - 8, Distance(p, Type(L::Partial(1, 0, 0).Pointer<2>(p)))); - EXPECT_EQ( - 0, Distance(p, Type(L::Partial(5, 3, 1).Pointer<0>(p)))); - EXPECT_EQ( - 24, - Distance(p, Type(L::Partial(5, 3, 1).Pointer<2>(p)))); - EXPECT_EQ( - 8, - Distance(p, Type(L::Partial(5, 3, 1).Pointer<1>(p)))); - EXPECT_EQ(0, Distance(p, Type(L(5, 3, 1).Pointer<0>(p)))); - EXPECT_EQ(24, Distance(p, Type(L(5, 3, 1).Pointer<2>(p)))); - EXPECT_EQ(8, Distance(p, Type(L(5, 3, 1).Pointer<1>(p)))); - } -} - -TEST(Layout, PointerByType) { - alignas(max_align_t) const unsigned char p[100] = {}; - { - using L = Layout; - EXPECT_EQ( - 0, Distance(p, Type(L::Partial().Pointer(p)))); - EXPECT_EQ( - 0, - Distance(p, Type(L::Partial(3).Pointer(p)))); - EXPECT_EQ(0, Distance(p, Type(L(3).Pointer(p)))); - } - { - using L = Layout; - EXPECT_EQ( - 0, Distance(p, Type(L::Partial().Pointer(p)))); - EXPECT_EQ( - 0, Distance(p, Type(L::Partial(0).Pointer(p)))); - EXPECT_EQ( - 0, - Distance(p, Type(L::Partial(0).Pointer(p)))); - EXPECT_EQ( - 0, Distance(p, Type(L::Partial(1).Pointer(p)))); - EXPECT_EQ( - 4, - Distance(p, Type(L::Partial(1).Pointer(p)))); - EXPECT_EQ( - 0, Distance(p, Type(L::Partial(5).Pointer(p)))); - EXPECT_EQ( - 8, - Distance(p, Type(L::Partial(5).Pointer(p)))); - EXPECT_EQ( - 0, - Distance(p, Type(L::Partial(0, 0).Pointer(p)))); - EXPECT_EQ(0, Distance(p, Type( - L::Partial(0, 0).Pointer(p)))); - EXPECT_EQ( - 0, - Distance(p, Type(L::Partial(0, 0).Pointer(p)))); - EXPECT_EQ( - 0, - Distance(p, Type(L::Partial(1, 0).Pointer(p)))); - EXPECT_EQ(4, Distance(p, Type( - L::Partial(1, 0).Pointer(p)))); - EXPECT_EQ( - 8, - Distance(p, Type(L::Partial(1, 0).Pointer(p)))); - EXPECT_EQ( - 0, - Distance(p, Type(L::Partial(5, 3).Pointer(p)))); - EXPECT_EQ(8, Distance(p, Type( - L::Partial(5, 3).Pointer(p)))); - EXPECT_EQ( - 24, - Distance(p, Type(L::Partial(5, 3).Pointer(p)))); - EXPECT_EQ(0, Distance(p, Type( - L::Partial(0, 0, 0).Pointer(p)))); - EXPECT_EQ(0, Distance(p, Type( - L::Partial(0, 0, 0).Pointer(p)))); - EXPECT_EQ(0, Distance(p, Type( - L::Partial(0, 0, 0).Pointer(p)))); - EXPECT_EQ(0, Distance(p, Type( - L::Partial(1, 0, 0).Pointer(p)))); - EXPECT_EQ(4, Distance(p, Type( - L::Partial(1, 0, 0).Pointer(p)))); - EXPECT_EQ(8, Distance(p, Type( - L::Partial(1, 0, 0).Pointer(p)))); - EXPECT_EQ(0, Distance(p, Type( - L::Partial(5, 3, 1).Pointer(p)))); - EXPECT_EQ(24, Distance(p, Type( - L::Partial(5, 3, 1).Pointer(p)))); - EXPECT_EQ(8, Distance(p, Type( - L::Partial(5, 3, 1).Pointer(p)))); - EXPECT_EQ(24, - Distance(p, Type(L(5, 3, 1).Pointer(p)))); - EXPECT_EQ( - 8, Distance(p, Type(L(5, 3, 1).Pointer(p)))); - } -} - -TEST(Layout, MutablePointerByIndex) { - alignas(max_align_t) unsigned char p[100]; - { - using L = Layout; - EXPECT_EQ(0, Distance(p, Type(L::Partial().Pointer<0>(p)))); - EXPECT_EQ(0, Distance(p, Type(L::Partial(3).Pointer<0>(p)))); - EXPECT_EQ(0, Distance(p, Type(L(3).Pointer<0>(p)))); - } - { - using L = Layout; - EXPECT_EQ(0, Distance(p, Type(L::Partial().Pointer<0>(p)))); - EXPECT_EQ(0, Distance(p, Type(L::Partial(3).Pointer<0>(p)))); - EXPECT_EQ(12, Distance(p, Type(L::Partial(3).Pointer<1>(p)))); - EXPECT_EQ(0, Distance(p, Type(L::Partial(3, 5).Pointer<0>(p)))); - EXPECT_EQ(12, Distance(p, Type(L::Partial(3, 5).Pointer<1>(p)))); - EXPECT_EQ(0, Distance(p, Type(L(3, 5).Pointer<0>(p)))); - EXPECT_EQ(12, Distance(p, Type(L(3, 5).Pointer<1>(p)))); - } - { - using L = Layout; - EXPECT_EQ(0, Distance(p, Type(L::Partial().Pointer<0>(p)))); - EXPECT_EQ(0, Distance(p, Type(L::Partial(0).Pointer<0>(p)))); - EXPECT_EQ(0, Distance(p, Type(L::Partial(0).Pointer<1>(p)))); - EXPECT_EQ(0, Distance(p, Type(L::Partial(1).Pointer<0>(p)))); - EXPECT_EQ(4, Distance(p, Type(L::Partial(1).Pointer<1>(p)))); - EXPECT_EQ(0, Distance(p, Type(L::Partial(5).Pointer<0>(p)))); - EXPECT_EQ(8, Distance(p, Type(L::Partial(5).Pointer<1>(p)))); - EXPECT_EQ(0, Distance(p, Type(L::Partial(0, 0).Pointer<0>(p)))); - EXPECT_EQ(0, Distance(p, Type(L::Partial(0, 0).Pointer<1>(p)))); - EXPECT_EQ(0, Distance(p, Type(L::Partial(0, 0).Pointer<2>(p)))); - EXPECT_EQ(0, Distance(p, Type(L::Partial(1, 0).Pointer<0>(p)))); - EXPECT_EQ(4, Distance(p, Type(L::Partial(1, 0).Pointer<1>(p)))); - EXPECT_EQ(8, Distance(p, Type(L::Partial(1, 0).Pointer<2>(p)))); - EXPECT_EQ(0, Distance(p, Type(L::Partial(5, 3).Pointer<0>(p)))); - EXPECT_EQ(8, Distance(p, Type(L::Partial(5, 3).Pointer<1>(p)))); - EXPECT_EQ(24, Distance(p, Type(L::Partial(5, 3).Pointer<2>(p)))); - EXPECT_EQ(0, Distance(p, Type(L::Partial(0, 0, 0).Pointer<0>(p)))); - EXPECT_EQ(0, - Distance(p, Type(L::Partial(0, 0, 0).Pointer<1>(p)))); - EXPECT_EQ(0, Distance(p, Type(L::Partial(0, 0, 0).Pointer<2>(p)))); - EXPECT_EQ(0, Distance(p, Type(L::Partial(1, 0, 0).Pointer<0>(p)))); - EXPECT_EQ(4, - Distance(p, Type(L::Partial(1, 0, 0).Pointer<1>(p)))); - EXPECT_EQ(8, Distance(p, Type(L::Partial(1, 0, 0).Pointer<2>(p)))); - EXPECT_EQ(0, Distance(p, Type(L::Partial(5, 3, 1).Pointer<0>(p)))); - EXPECT_EQ(24, - Distance(p, Type(L::Partial(5, 3, 1).Pointer<2>(p)))); - EXPECT_EQ(8, - Distance(p, Type(L::Partial(5, 3, 1).Pointer<1>(p)))); - EXPECT_EQ(0, Distance(p, Type(L(5, 3, 1).Pointer<0>(p)))); - EXPECT_EQ(24, Distance(p, Type(L(5, 3, 1).Pointer<2>(p)))); - EXPECT_EQ(8, Distance(p, Type(L(5, 3, 1).Pointer<1>(p)))); - } -} - -TEST(Layout, MutablePointerByType) { - alignas(max_align_t) unsigned char p[100]; - { - using L = Layout; - EXPECT_EQ(0, Distance(p, Type(L::Partial().Pointer(p)))); - EXPECT_EQ(0, - Distance(p, Type(L::Partial(3).Pointer(p)))); - EXPECT_EQ(0, Distance(p, Type(L(3).Pointer(p)))); - } - { - using L = Layout; - EXPECT_EQ(0, Distance(p, Type(L::Partial().Pointer(p)))); - EXPECT_EQ(0, Distance(p, Type(L::Partial(0).Pointer(p)))); - EXPECT_EQ(0, - Distance(p, Type(L::Partial(0).Pointer(p)))); - EXPECT_EQ(0, Distance(p, Type(L::Partial(1).Pointer(p)))); - EXPECT_EQ(4, - Distance(p, Type(L::Partial(1).Pointer(p)))); - EXPECT_EQ(0, Distance(p, Type(L::Partial(5).Pointer(p)))); - EXPECT_EQ(8, - Distance(p, Type(L::Partial(5).Pointer(p)))); - EXPECT_EQ(0, - Distance(p, Type(L::Partial(0, 0).Pointer(p)))); - EXPECT_EQ( - 0, Distance(p, Type(L::Partial(0, 0).Pointer(p)))); - EXPECT_EQ(0, - Distance(p, Type(L::Partial(0, 0).Pointer(p)))); - EXPECT_EQ(0, - Distance(p, Type(L::Partial(1, 0).Pointer(p)))); - EXPECT_EQ( - 4, Distance(p, Type(L::Partial(1, 0).Pointer(p)))); - EXPECT_EQ(8, - Distance(p, Type(L::Partial(1, 0).Pointer(p)))); - EXPECT_EQ(0, - Distance(p, Type(L::Partial(5, 3).Pointer(p)))); - EXPECT_EQ( - 8, Distance(p, Type(L::Partial(5, 3).Pointer(p)))); - EXPECT_EQ(24, - Distance(p, Type(L::Partial(5, 3).Pointer(p)))); - EXPECT_EQ( - 0, Distance(p, Type(L::Partial(0, 0, 0).Pointer(p)))); - EXPECT_EQ( - 0, - Distance(p, Type(L::Partial(0, 0, 0).Pointer(p)))); - EXPECT_EQ( - 0, Distance(p, Type(L::Partial(0, 0, 0).Pointer(p)))); - EXPECT_EQ( - 0, Distance(p, Type(L::Partial(1, 0, 0).Pointer(p)))); - EXPECT_EQ( - 4, - Distance(p, Type(L::Partial(1, 0, 0).Pointer(p)))); - EXPECT_EQ( - 8, Distance(p, Type(L::Partial(1, 0, 0).Pointer(p)))); - EXPECT_EQ( - 0, Distance(p, Type(L::Partial(5, 3, 1).Pointer(p)))); - EXPECT_EQ( - 24, Distance(p, Type(L::Partial(5, 3, 1).Pointer(p)))); - EXPECT_EQ( - 8, - Distance(p, Type(L::Partial(5, 3, 1).Pointer(p)))); - EXPECT_EQ(0, Distance(p, Type(L(5, 3, 1).Pointer(p)))); - EXPECT_EQ(24, Distance(p, Type(L(5, 3, 1).Pointer(p)))); - EXPECT_EQ(8, Distance(p, Type(L(5, 3, 1).Pointer(p)))); - } -} - -TEST(Layout, Pointers) { - alignas(max_align_t) const unsigned char p[100] = {}; - using L = Layout; - { - const auto x = L::Partial(); - EXPECT_EQ(std::make_tuple(x.Pointer<0>(p)), - Type>(x.Pointers(p))); - } - { - const auto x = L::Partial(1); - EXPECT_EQ(std::make_tuple(x.Pointer<0>(p), x.Pointer<1>(p)), - (Type>(x.Pointers(p)))); - } - { - const auto x = L::Partial(1, 2); - EXPECT_EQ( - std::make_tuple(x.Pointer<0>(p), x.Pointer<1>(p), x.Pointer<2>(p)), - (Type>( - x.Pointers(p)))); - } - { - const auto x = L::Partial(1, 2, 3); - EXPECT_EQ( - std::make_tuple(x.Pointer<0>(p), x.Pointer<1>(p), x.Pointer<2>(p)), - (Type>( - x.Pointers(p)))); - } - { - const L x(1, 2, 3); - EXPECT_EQ( - std::make_tuple(x.Pointer<0>(p), x.Pointer<1>(p), x.Pointer<2>(p)), - (Type>( - x.Pointers(p)))); - } -} - -TEST(Layout, MutablePointers) { - alignas(max_align_t) unsigned char p[100]; - using L = Layout; - { - const auto x = L::Partial(); - EXPECT_EQ(std::make_tuple(x.Pointer<0>(p)), - Type>(x.Pointers(p))); - } - { - const auto x = L::Partial(1); - EXPECT_EQ(std::make_tuple(x.Pointer<0>(p), x.Pointer<1>(p)), - (Type>(x.Pointers(p)))); - } - { - const auto x = L::Partial(1, 2); - EXPECT_EQ( - std::make_tuple(x.Pointer<0>(p), x.Pointer<1>(p), x.Pointer<2>(p)), - (Type>(x.Pointers(p)))); - } - { - const auto x = L::Partial(1, 2, 3); - EXPECT_EQ( - std::make_tuple(x.Pointer<0>(p), x.Pointer<1>(p), x.Pointer<2>(p)), - (Type>(x.Pointers(p)))); - } - { - const L x(1, 2, 3); - EXPECT_EQ( - std::make_tuple(x.Pointer<0>(p), x.Pointer<1>(p), x.Pointer<2>(p)), - (Type>(x.Pointers(p)))); - } -} - -TEST(Layout, SliceByIndexSize) { - alignas(max_align_t) const unsigned char p[100] = {}; - { - using L = Layout; - EXPECT_EQ(0, L::Partial(0).Slice<0>(p).size()); - EXPECT_EQ(3, L::Partial(3).Slice<0>(p).size()); - EXPECT_EQ(3, L(3).Slice<0>(p).size()); - } - { - using L = Layout; - EXPECT_EQ(3, L::Partial(3).Slice<0>(p).size()); - EXPECT_EQ(5, L::Partial(3, 5).Slice<1>(p).size()); - EXPECT_EQ(5, L(3, 5).Slice<1>(p).size()); - } - { - using L = Layout; - EXPECT_EQ(3, L::Partial(3).Slice<0>(p).size()); - EXPECT_EQ(3, L::Partial(3, 5).Slice<0>(p).size()); - EXPECT_EQ(5, L::Partial(3, 5).Slice<1>(p).size()); - EXPECT_EQ(3, L::Partial(3, 5, 7).Slice<0>(p).size()); - EXPECT_EQ(5, L::Partial(3, 5, 7).Slice<1>(p).size()); - EXPECT_EQ(7, L::Partial(3, 5, 7).Slice<2>(p).size()); - EXPECT_EQ(3, L(3, 5, 7).Slice<0>(p).size()); - EXPECT_EQ(5, L(3, 5, 7).Slice<1>(p).size()); - EXPECT_EQ(7, L(3, 5, 7).Slice<2>(p).size()); - } -} - -TEST(Layout, SliceByTypeSize) { - alignas(max_align_t) const unsigned char p[100] = {}; - { - using L = Layout; - EXPECT_EQ(0, L::Partial(0).Slice(p).size()); - EXPECT_EQ(3, L::Partial(3).Slice(p).size()); - EXPECT_EQ(3, L(3).Slice(p).size()); - } - { - using L = Layout; - EXPECT_EQ(3, L::Partial(3).Slice(p).size()); - EXPECT_EQ(3, L::Partial(3, 5).Slice(p).size()); - EXPECT_EQ(5, L::Partial(3, 5).Slice(p).size()); - EXPECT_EQ(3, L::Partial(3, 5, 7).Slice(p).size()); - EXPECT_EQ(5, L::Partial(3, 5, 7).Slice(p).size()); - EXPECT_EQ(7, L::Partial(3, 5, 7).Slice(p).size()); - EXPECT_EQ(3, L(3, 5, 7).Slice(p).size()); - EXPECT_EQ(5, L(3, 5, 7).Slice(p).size()); - EXPECT_EQ(7, L(3, 5, 7).Slice(p).size()); - } -} - -TEST(Layout, MutableSliceByIndexSize) { - alignas(max_align_t) unsigned char p[100]; - { - using L = Layout; - EXPECT_EQ(0, L::Partial(0).Slice<0>(p).size()); - EXPECT_EQ(3, L::Partial(3).Slice<0>(p).size()); - EXPECT_EQ(3, L(3).Slice<0>(p).size()); - } - { - using L = Layout; - EXPECT_EQ(3, L::Partial(3).Slice<0>(p).size()); - EXPECT_EQ(5, L::Partial(3, 5).Slice<1>(p).size()); - EXPECT_EQ(5, L(3, 5).Slice<1>(p).size()); - } - { - using L = Layout; - EXPECT_EQ(3, L::Partial(3).Slice<0>(p).size()); - EXPECT_EQ(3, L::Partial(3, 5).Slice<0>(p).size()); - EXPECT_EQ(5, L::Partial(3, 5).Slice<1>(p).size()); - EXPECT_EQ(3, L::Partial(3, 5, 7).Slice<0>(p).size()); - EXPECT_EQ(5, L::Partial(3, 5, 7).Slice<1>(p).size()); - EXPECT_EQ(7, L::Partial(3, 5, 7).Slice<2>(p).size()); - EXPECT_EQ(3, L(3, 5, 7).Slice<0>(p).size()); - EXPECT_EQ(5, L(3, 5, 7).Slice<1>(p).size()); - EXPECT_EQ(7, L(3, 5, 7).Slice<2>(p).size()); - } -} - -TEST(Layout, MutableSliceByTypeSize) { - alignas(max_align_t) unsigned char p[100]; - { - using L = Layout; - EXPECT_EQ(0, L::Partial(0).Slice(p).size()); - EXPECT_EQ(3, L::Partial(3).Slice(p).size()); - EXPECT_EQ(3, L(3).Slice(p).size()); - } - { - using L = Layout; - EXPECT_EQ(3, L::Partial(3).Slice(p).size()); - EXPECT_EQ(3, L::Partial(3, 5).Slice(p).size()); - EXPECT_EQ(5, L::Partial(3, 5).Slice(p).size()); - EXPECT_EQ(3, L::Partial(3, 5, 7).Slice(p).size()); - EXPECT_EQ(5, L::Partial(3, 5, 7).Slice(p).size()); - EXPECT_EQ(7, L::Partial(3, 5, 7).Slice(p).size()); - EXPECT_EQ(3, L(3, 5, 7).Slice(p).size()); - EXPECT_EQ(5, L(3, 5, 7).Slice(p).size()); - EXPECT_EQ(7, L(3, 5, 7).Slice(p).size()); - } -} - -TEST(Layout, SliceByIndexData) { - alignas(max_align_t) const unsigned char p[100] = {}; - { - using L = Layout; - EXPECT_EQ( - 0, Distance( - p, Type>(L::Partial(0).Slice<0>(p)).data())); - EXPECT_EQ( - 0, Distance( - p, Type>(L::Partial(3).Slice<0>(p)).data())); - EXPECT_EQ(0, - Distance(p, Type>(L(3).Slice<0>(p)).data())); - } - { - using L = Layout; - EXPECT_EQ( - 0, Distance( - p, Type>(L::Partial(3).Slice<0>(p)).data())); - EXPECT_EQ( - 0, - Distance( - p, Type>(L::Partial(3, 5).Slice<0>(p)).data())); - EXPECT_EQ( - 12, - Distance( - p, Type>(L::Partial(3, 5).Slice<1>(p)).data())); - EXPECT_EQ( - 0, Distance(p, Type>(L(3, 5).Slice<0>(p)).data())); - EXPECT_EQ( - 12, Distance(p, Type>(L(3, 5).Slice<1>(p)).data())); - } - { - using L = Layout; - EXPECT_EQ( - 0, Distance( - p, Type>(L::Partial(0).Slice<0>(p)).data())); - EXPECT_EQ( - 0, Distance( - p, Type>(L::Partial(1).Slice<0>(p)).data())); - EXPECT_EQ( - 0, Distance( - p, Type>(L::Partial(5).Slice<0>(p)).data())); - EXPECT_EQ( - 0, - Distance( - p, Type>(L::Partial(0, 0).Slice<0>(p)).data())); - EXPECT_EQ( - 0, - Distance( - p, Type>(L::Partial(0, 0).Slice<1>(p)).data())); - EXPECT_EQ( - 0, - Distance( - p, Type>(L::Partial(1, 0).Slice<0>(p)).data())); - EXPECT_EQ( - 4, - Distance( - p, Type>(L::Partial(1, 0).Slice<1>(p)).data())); - EXPECT_EQ( - 0, - Distance( - p, Type>(L::Partial(5, 3).Slice<0>(p)).data())); - EXPECT_EQ( - 8, - Distance( - p, Type>(L::Partial(5, 3).Slice<1>(p)).data())); - EXPECT_EQ( - 0, - Distance( - p, - Type>(L::Partial(0, 0, 0).Slice<0>(p)).data())); - EXPECT_EQ( - 0, - Distance( - p, - Type>(L::Partial(0, 0, 0).Slice<1>(p)).data())); - EXPECT_EQ( - 0, - Distance( - p, - Type>(L::Partial(0, 0, 0).Slice<2>(p)).data())); - EXPECT_EQ( - 0, - Distance( - p, - Type>(L::Partial(1, 0, 0).Slice<0>(p)).data())); - EXPECT_EQ( - 4, - Distance( - p, - Type>(L::Partial(1, 0, 0).Slice<1>(p)).data())); - EXPECT_EQ( - 8, - Distance( - p, - Type>(L::Partial(1, 0, 0).Slice<2>(p)).data())); - EXPECT_EQ( - 0, - Distance( - p, - Type>(L::Partial(5, 3, 1).Slice<0>(p)).data())); - EXPECT_EQ( - 24, - Distance( - p, - Type>(L::Partial(5, 3, 1).Slice<2>(p)).data())); - EXPECT_EQ( - 8, - Distance( - p, - Type>(L::Partial(5, 3, 1).Slice<1>(p)).data())); - EXPECT_EQ( - 0, - Distance(p, Type>(L(5, 3, 1).Slice<0>(p)).data())); - EXPECT_EQ( - 24, - Distance(p, Type>(L(5, 3, 1).Slice<2>(p)).data())); - EXPECT_EQ( - 8, - Distance(p, Type>(L(5, 3, 1).Slice<1>(p)).data())); - } -} - -TEST(Layout, SliceByTypeData) { - alignas(max_align_t) const unsigned char p[100] = {}; - { - using L = Layout; - EXPECT_EQ( - 0, - Distance( - p, - Type>(L::Partial(0).Slice(p)).data())); - EXPECT_EQ( - 0, - Distance( - p, - Type>(L::Partial(3).Slice(p)).data())); - EXPECT_EQ( - 0, - Distance(p, Type>(L(3).Slice(p)).data())); - } - { - using L = Layout; - EXPECT_EQ( - 0, - Distance( - p, - Type>(L::Partial(0).Slice(p)).data())); - EXPECT_EQ( - 0, - Distance( - p, - Type>(L::Partial(1).Slice(p)).data())); - EXPECT_EQ( - 0, - Distance( - p, - Type>(L::Partial(5).Slice(p)).data())); - EXPECT_EQ( - 0, - Distance(p, Type>(L::Partial(0, 0).Slice(p)) - .data())); - EXPECT_EQ(0, Distance(p, Type>( - L::Partial(0, 0).Slice(p)) - .data())); - EXPECT_EQ( - 0, - Distance(p, Type>(L::Partial(1, 0).Slice(p)) - .data())); - EXPECT_EQ(4, Distance(p, Type>( - L::Partial(1, 0).Slice(p)) - .data())); - EXPECT_EQ( - 0, - Distance(p, Type>(L::Partial(5, 3).Slice(p)) - .data())); - EXPECT_EQ(8, Distance(p, Type>( - L::Partial(5, 3).Slice(p)) - .data())); - EXPECT_EQ(0, Distance(p, Type>( - L::Partial(0, 0, 0).Slice(p)) - .data())); - EXPECT_EQ(0, Distance(p, Type>( - L::Partial(0, 0, 0).Slice(p)) - .data())); - EXPECT_EQ(0, Distance(p, Type>( - L::Partial(0, 0, 0).Slice(p)) - .data())); - EXPECT_EQ(0, Distance(p, Type>( - L::Partial(1, 0, 0).Slice(p)) - .data())); - EXPECT_EQ(4, Distance(p, Type>( - L::Partial(1, 0, 0).Slice(p)) - .data())); - EXPECT_EQ(8, Distance(p, Type>( - L::Partial(1, 0, 0).Slice(p)) - .data())); - EXPECT_EQ(0, Distance(p, Type>( - L::Partial(5, 3, 1).Slice(p)) - .data())); - EXPECT_EQ(24, Distance(p, Type>( - L::Partial(5, 3, 1).Slice(p)) - .data())); - EXPECT_EQ(8, Distance(p, Type>( - L::Partial(5, 3, 1).Slice(p)) - .data())); - EXPECT_EQ( - 0, - Distance(p, - Type>(L(5, 3, 1).Slice(p)).data())); - EXPECT_EQ( - 24, - Distance(p, - Type>(L(5, 3, 1).Slice(p)).data())); - EXPECT_EQ( - 8, - Distance( - p, Type>(L(5, 3, 1).Slice(p)).data())); - } -} - -TEST(Layout, MutableSliceByIndexData) { - alignas(max_align_t) unsigned char p[100]; - { - using L = Layout; - EXPECT_EQ( - 0, Distance(p, Type>(L::Partial(0).Slice<0>(p)).data())); - EXPECT_EQ( - 0, Distance(p, Type>(L::Partial(3).Slice<0>(p)).data())); - EXPECT_EQ(0, Distance(p, Type>(L(3).Slice<0>(p)).data())); - } - { - using L = Layout; - EXPECT_EQ( - 0, Distance(p, Type>(L::Partial(3).Slice<0>(p)).data())); - EXPECT_EQ( - 0, - Distance(p, Type>(L::Partial(3, 5).Slice<0>(p)).data())); - EXPECT_EQ( - 12, - Distance(p, Type>(L::Partial(3, 5).Slice<1>(p)).data())); - EXPECT_EQ(0, Distance(p, Type>(L(3, 5).Slice<0>(p)).data())); - EXPECT_EQ(12, Distance(p, Type>(L(3, 5).Slice<1>(p)).data())); - } - { - using L = Layout; - EXPECT_EQ( - 0, Distance(p, Type>(L::Partial(0).Slice<0>(p)).data())); - EXPECT_EQ( - 0, Distance(p, Type>(L::Partial(1).Slice<0>(p)).data())); - EXPECT_EQ( - 0, Distance(p, Type>(L::Partial(5).Slice<0>(p)).data())); - EXPECT_EQ( - 0, - Distance(p, Type>(L::Partial(0, 0).Slice<0>(p)).data())); - EXPECT_EQ( - 0, - Distance(p, Type>(L::Partial(0, 0).Slice<1>(p)).data())); - EXPECT_EQ( - 0, - Distance(p, Type>(L::Partial(1, 0).Slice<0>(p)).data())); - EXPECT_EQ( - 4, - Distance(p, Type>(L::Partial(1, 0).Slice<1>(p)).data())); - EXPECT_EQ( - 0, - Distance(p, Type>(L::Partial(5, 3).Slice<0>(p)).data())); - EXPECT_EQ( - 8, - Distance(p, Type>(L::Partial(5, 3).Slice<1>(p)).data())); - EXPECT_EQ( - 0, Distance( - p, Type>(L::Partial(0, 0, 0).Slice<0>(p)).data())); - EXPECT_EQ( - 0, Distance( - p, Type>(L::Partial(0, 0, 0).Slice<1>(p)).data())); - EXPECT_EQ( - 0, Distance( - p, Type>(L::Partial(0, 0, 0).Slice<2>(p)).data())); - EXPECT_EQ( - 0, Distance( - p, Type>(L::Partial(1, 0, 0).Slice<0>(p)).data())); - EXPECT_EQ( - 4, Distance( - p, Type>(L::Partial(1, 0, 0).Slice<1>(p)).data())); - EXPECT_EQ( - 8, Distance( - p, Type>(L::Partial(1, 0, 0).Slice<2>(p)).data())); - EXPECT_EQ( - 0, Distance( - p, Type>(L::Partial(5, 3, 1).Slice<0>(p)).data())); - EXPECT_EQ( - 24, Distance( - p, Type>(L::Partial(5, 3, 1).Slice<2>(p)).data())); - EXPECT_EQ( - 8, Distance( - p, Type>(L::Partial(5, 3, 1).Slice<1>(p)).data())); - EXPECT_EQ(0, - Distance(p, Type>(L(5, 3, 1).Slice<0>(p)).data())); - EXPECT_EQ(24, - Distance(p, Type>(L(5, 3, 1).Slice<2>(p)).data())); - EXPECT_EQ(8, - Distance(p, Type>(L(5, 3, 1).Slice<1>(p)).data())); - } -} - -TEST(Layout, MutableSliceByTypeData) { - alignas(max_align_t) unsigned char p[100]; - { - using L = Layout; - EXPECT_EQ( - 0, Distance( - p, Type>(L::Partial(0).Slice(p)).data())); - EXPECT_EQ( - 0, Distance( - p, Type>(L::Partial(3).Slice(p)).data())); - EXPECT_EQ(0, - Distance(p, Type>(L(3).Slice(p)).data())); - } - { - using L = Layout; - EXPECT_EQ( - 0, - Distance(p, Type>(L::Partial(0).Slice(p)).data())); - EXPECT_EQ( - 0, - Distance(p, Type>(L::Partial(1).Slice(p)).data())); - EXPECT_EQ( - 0, - Distance(p, Type>(L::Partial(5).Slice(p)).data())); - EXPECT_EQ( - 0, - Distance(p, - Type>(L::Partial(0, 0).Slice(p)).data())); - EXPECT_EQ( - 0, - Distance( - p, Type>(L::Partial(0, 0).Slice(p)).data())); - EXPECT_EQ( - 0, - Distance(p, - Type>(L::Partial(1, 0).Slice(p)).data())); - EXPECT_EQ( - 4, - Distance( - p, Type>(L::Partial(1, 0).Slice(p)).data())); - EXPECT_EQ( - 0, - Distance(p, - Type>(L::Partial(5, 3).Slice(p)).data())); - EXPECT_EQ( - 8, - Distance( - p, Type>(L::Partial(5, 3).Slice(p)).data())); - EXPECT_EQ( - 0, - Distance( - p, - Type>(L::Partial(0, 0, 0).Slice(p)).data())); - EXPECT_EQ( - 0, - Distance( - p, - Type>(L::Partial(0, 0, 0).Slice(p)).data())); - EXPECT_EQ( - 0, - Distance( - p, - Type>(L::Partial(0, 0, 0).Slice(p)).data())); - EXPECT_EQ( - 0, - Distance( - p, - Type>(L::Partial(1, 0, 0).Slice(p)).data())); - EXPECT_EQ( - 4, - Distance( - p, - Type>(L::Partial(1, 0, 0).Slice(p)).data())); - EXPECT_EQ( - 8, - Distance( - p, - Type>(L::Partial(1, 0, 0).Slice(p)).data())); - EXPECT_EQ( - 0, - Distance( - p, - Type>(L::Partial(5, 3, 1).Slice(p)).data())); - EXPECT_EQ( - 24, - Distance( - p, - Type>(L::Partial(5, 3, 1).Slice(p)).data())); - EXPECT_EQ( - 8, - Distance( - p, - Type>(L::Partial(5, 3, 1).Slice(p)).data())); - EXPECT_EQ( - 0, Distance(p, Type>(L(5, 3, 1).Slice(p)).data())); - EXPECT_EQ( - 24, - Distance(p, Type>(L(5, 3, 1).Slice(p)).data())); - EXPECT_EQ( - 8, - Distance(p, Type>(L(5, 3, 1).Slice(p)).data())); - } -} - -MATCHER_P(IsSameSlice, slice, "") { - return arg.size() == slice.size() && arg.data() == slice.data(); -} - -template -class TupleMatcher { - public: - explicit TupleMatcher(M... matchers) : matchers_(std::move(matchers)...) {} - - template - bool MatchAndExplain(const Tuple& p, - testing::MatchResultListener* /* listener */) const { - static_assert(std::tuple_size::value == sizeof...(M), ""); - return MatchAndExplainImpl( - p, absl::make_index_sequence::value>{}); - } - - // For the matcher concept. Left empty as we don't really need the diagnostics - // right now. - void DescribeTo(::std::ostream* os) const {} - void DescribeNegationTo(::std::ostream* os) const {} - - private: - template - bool MatchAndExplainImpl(const Tuple& p, absl::index_sequence) const { - // Using std::min as a simple variadic "and". - return std::min( - {true, testing::SafeMatcherCast< - const typename std::tuple_element::type&>( - std::get(matchers_)) - .Matches(std::get(p))...}); - } - - std::tuple matchers_; -}; - -template -testing::PolymorphicMatcher> Tuple(M... matchers) { - return testing::MakePolymorphicMatcher( - TupleMatcher(std::move(matchers)...)); -} - -TEST(Layout, Slices) { - alignas(max_align_t) const unsigned char p[100] = {}; - using L = Layout; - { - const auto x = L::Partial(); - EXPECT_THAT(Type>(x.Slices(p)), Tuple()); - } - { - const auto x = L::Partial(1); - EXPECT_THAT(Type>>(x.Slices(p)), - Tuple(IsSameSlice(x.Slice<0>(p)))); - } - { - const auto x = L::Partial(1, 2); - EXPECT_THAT( - (Type, Span>>(x.Slices(p))), - Tuple(IsSameSlice(x.Slice<0>(p)), IsSameSlice(x.Slice<1>(p)))); - } - { - const auto x = L::Partial(1, 2, 3); - EXPECT_THAT((Type, Span, - Span>>(x.Slices(p))), - Tuple(IsSameSlice(x.Slice<0>(p)), IsSameSlice(x.Slice<1>(p)), - IsSameSlice(x.Slice<2>(p)))); - } - { - const L x(1, 2, 3); - EXPECT_THAT((Type, Span, - Span>>(x.Slices(p))), - Tuple(IsSameSlice(x.Slice<0>(p)), IsSameSlice(x.Slice<1>(p)), - IsSameSlice(x.Slice<2>(p)))); - } -} - -TEST(Layout, MutableSlices) { - alignas(max_align_t) unsigned char p[100] = {}; - using L = Layout; - { - const auto x = L::Partial(); - EXPECT_THAT(Type>(x.Slices(p)), Tuple()); - } - { - const auto x = L::Partial(1); - EXPECT_THAT(Type>>(x.Slices(p)), - Tuple(IsSameSlice(x.Slice<0>(p)))); - } - { - const auto x = L::Partial(1, 2); - EXPECT_THAT((Type, Span>>(x.Slices(p))), - Tuple(IsSameSlice(x.Slice<0>(p)), IsSameSlice(x.Slice<1>(p)))); - } - { - const auto x = L::Partial(1, 2, 3); - EXPECT_THAT((Type, Span, Span>>( - x.Slices(p))), - Tuple(IsSameSlice(x.Slice<0>(p)), IsSameSlice(x.Slice<1>(p)), - IsSameSlice(x.Slice<2>(p)))); - } - { - const L x(1, 2, 3); - EXPECT_THAT((Type, Span, Span>>( - x.Slices(p))), - Tuple(IsSameSlice(x.Slice<0>(p)), IsSameSlice(x.Slice<1>(p)), - IsSameSlice(x.Slice<2>(p)))); - } -} - -TEST(Layout, UnalignedTypes) { - constexpr Layout x(1, 2, 3); - alignas(max_align_t) unsigned char p[x.AllocSize() + 1]; - EXPECT_THAT(x.Pointers(p + 1), Tuple(p + 1, p + 2, p + 4)); -} - -TEST(Layout, CustomAlignment) { - constexpr Layout> x(1, 2); - alignas(max_align_t) unsigned char p[x.AllocSize()]; - EXPECT_EQ(10, x.AllocSize()); - EXPECT_THAT(x.Pointers(p), Tuple(p + 0, p + 8)); -} - -TEST(Layout, OverAligned) { - constexpr size_t M = alignof(max_align_t); - constexpr Layout> x(1, 3); - alignas(2 * M) unsigned char p[x.AllocSize()]; - EXPECT_EQ(2 * M + 3, x.AllocSize()); - EXPECT_THAT(x.Pointers(p), Tuple(p + 0, p + 2 * M)); -} - -TEST(Layout, Alignment) { - static_assert(Layout::Alignment() == 1, ""); - static_assert(Layout::Alignment() == 4, ""); - static_assert(Layout::Alignment() == 8, ""); - static_assert(Layout>::Alignment() == 64, ""); - static_assert(Layout::Alignment() == 8, ""); - static_assert(Layout::Alignment() == 8, ""); - static_assert(Layout::Alignment() == 8, ""); - static_assert(Layout::Alignment() == 8, ""); - static_assert(Layout::Alignment() == 8, ""); - static_assert(Layout::Alignment() == 8, ""); -} - -TEST(Layout, ConstexprPartial) { - constexpr size_t M = alignof(max_align_t); - constexpr Layout> x(1, 3); - static_assert(x.Partial(1).template Offset<1>() == 2 * M, ""); -} -// [from, to) -struct Region { - size_t from; - size_t to; -}; - -void ExpectRegionPoisoned(const unsigned char* p, size_t n, bool poisoned) { -#ifdef ABSL_HAVE_ADDRESS_SANITIZER - for (size_t i = 0; i != n; ++i) { - EXPECT_EQ(poisoned, __asan_address_is_poisoned(p + i)); - } -#endif -} - -template -void ExpectPoisoned(const unsigned char (&buf)[N], - std::initializer_list reg) { - size_t prev = 0; - for (const Region& r : reg) { - ExpectRegionPoisoned(buf + prev, r.from - prev, false); - ExpectRegionPoisoned(buf + r.from, r.to - r.from, true); - prev = r.to; - } - ExpectRegionPoisoned(buf + prev, N - prev, false); -} - -TEST(Layout, PoisonPadding) { - using L = Layout; - - constexpr size_t n = L::Partial(1, 2, 3, 4).AllocSize(); - { - constexpr auto x = L::Partial(); - alignas(max_align_t) const unsigned char c[n] = {}; - x.PoisonPadding(c); - EXPECT_EQ(x.Slices(c), x.Slices(c)); - ExpectPoisoned(c, {}); - } - { - constexpr auto x = L::Partial(1); - alignas(max_align_t) const unsigned char c[n] = {}; - x.PoisonPadding(c); - EXPECT_EQ(x.Slices(c), x.Slices(c)); - ExpectPoisoned(c, {{1, 8}}); - } - { - constexpr auto x = L::Partial(1, 2); - alignas(max_align_t) const unsigned char c[n] = {}; - x.PoisonPadding(c); - EXPECT_EQ(x.Slices(c), x.Slices(c)); - ExpectPoisoned(c, {{1, 8}}); - } - { - constexpr auto x = L::Partial(1, 2, 3); - alignas(max_align_t) const unsigned char c[n] = {}; - x.PoisonPadding(c); - EXPECT_EQ(x.Slices(c), x.Slices(c)); - ExpectPoisoned(c, {{1, 8}, {36, 40}}); - } - { - constexpr auto x = L::Partial(1, 2, 3, 4); - alignas(max_align_t) const unsigned char c[n] = {}; - x.PoisonPadding(c); - EXPECT_EQ(x.Slices(c), x.Slices(c)); - ExpectPoisoned(c, {{1, 8}, {36, 40}}); - } - { - constexpr L x(1, 2, 3, 4); - alignas(max_align_t) const unsigned char c[n] = {}; - x.PoisonPadding(c); - EXPECT_EQ(x.Slices(c), x.Slices(c)); - ExpectPoisoned(c, {{1, 8}, {36, 40}}); - } -} - -TEST(Layout, DebugString) { - { - constexpr auto x = Layout::Partial(); - EXPECT_EQ("@0(1)", x.DebugString()); - } - { - constexpr auto x = Layout::Partial(1); - EXPECT_EQ("@0(1)[1]; @4(4)", x.DebugString()); - } - { - constexpr auto x = Layout::Partial(1, 2); - EXPECT_EQ("@0(1)[1]; @4(4)[2]; @12(1)", - x.DebugString()); - } - { - constexpr auto x = - Layout::Partial(1, 2, 3); - EXPECT_EQ( - "@0(1)[1]; @4(4)[2]; @12(1)[3]; " - "@16" + - Int128::Name() + "(16)", - x.DebugString()); - } - { - constexpr auto x = - Layout::Partial(1, 2, 3, 4); - EXPECT_EQ( - "@0(1)[1]; @4(4)[2]; @12(1)[3]; " - "@16" + - Int128::Name() + "(16)[4]", - x.DebugString()); - } - { - constexpr Layout x(1, 2, 3, 4); - EXPECT_EQ( - "@0(1)[1]; @4(4)[2]; @12(1)[3]; " - "@16" + - Int128::Name() + "(16)[4]", - x.DebugString()); - } -} - -TEST(Layout, CharTypes) { - constexpr Layout x(1); - alignas(max_align_t) char c[x.AllocSize()] = {}; - alignas(max_align_t) unsigned char uc[x.AllocSize()] = {}; - alignas(max_align_t) signed char sc[x.AllocSize()] = {}; - alignas(max_align_t) const char cc[x.AllocSize()] = {}; - alignas(max_align_t) const unsigned char cuc[x.AllocSize()] = {}; - alignas(max_align_t) const signed char csc[x.AllocSize()] = {}; - - Type(x.Pointer<0>(c)); - Type(x.Pointer<0>(uc)); - Type(x.Pointer<0>(sc)); - Type(x.Pointer<0>(cc)); - Type(x.Pointer<0>(cuc)); - Type(x.Pointer<0>(csc)); - - Type(x.Pointer(c)); - Type(x.Pointer(uc)); - Type(x.Pointer(sc)); - Type(x.Pointer(cc)); - Type(x.Pointer(cuc)); - Type(x.Pointer(csc)); - - Type>(x.Pointers(c)); - Type>(x.Pointers(uc)); - Type>(x.Pointers(sc)); - Type>(x.Pointers(cc)); - Type>(x.Pointers(cuc)); - Type>(x.Pointers(csc)); - - Type>(x.Slice<0>(c)); - Type>(x.Slice<0>(uc)); - Type>(x.Slice<0>(sc)); - Type>(x.Slice<0>(cc)); - Type>(x.Slice<0>(cuc)); - Type>(x.Slice<0>(csc)); - - Type>>(x.Slices(c)); - Type>>(x.Slices(uc)); - Type>>(x.Slices(sc)); - Type>>(x.Slices(cc)); - Type>>(x.Slices(cuc)); - Type>>(x.Slices(csc)); -} - -TEST(Layout, ConstElementType) { - constexpr Layout x(1); - alignas(int32_t) char c[x.AllocSize()] = {}; - const char* cc = c; - const int32_t* p = reinterpret_cast(cc); - - EXPECT_EQ(alignof(int32_t), x.Alignment()); - - EXPECT_EQ(0, x.Offset<0>()); - EXPECT_EQ(0, x.Offset()); - - EXPECT_THAT(x.Offsets(), ElementsAre(0)); - - EXPECT_EQ(1, x.Size<0>()); - EXPECT_EQ(1, x.Size()); - - EXPECT_THAT(x.Sizes(), ElementsAre(1)); - - EXPECT_EQ(sizeof(int32_t), x.AllocSize()); - - EXPECT_EQ(p, Type(x.Pointer<0>(c))); - EXPECT_EQ(p, Type(x.Pointer<0>(cc))); - - EXPECT_EQ(p, Type(x.Pointer(c))); - EXPECT_EQ(p, Type(x.Pointer(cc))); - - EXPECT_THAT(Type>(x.Pointers(c)), Tuple(p)); - EXPECT_THAT(Type>(x.Pointers(cc)), Tuple(p)); - - EXPECT_THAT(Type>(x.Slice<0>(c)), - IsSameSlice(Span(p, 1))); - EXPECT_THAT(Type>(x.Slice<0>(cc)), - IsSameSlice(Span(p, 1))); - - EXPECT_THAT(Type>(x.Slice(c)), - IsSameSlice(Span(p, 1))); - EXPECT_THAT(Type>(x.Slice(cc)), - IsSameSlice(Span(p, 1))); - - EXPECT_THAT(Type>>(x.Slices(c)), - Tuple(IsSameSlice(Span(p, 1)))); - EXPECT_THAT(Type>>(x.Slices(cc)), - Tuple(IsSameSlice(Span(p, 1)))); -} - -namespace 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 = "") { // NOLINT - const size_t size = strlen(s); - // size_t[1], followed by char[size + 1]. - // This statement doesn't allocate memory. - const L layout(1, size + 1); - // AllocSize() tells us how much memory we need to allocate for all our - // data. - 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. - // Pointer() is a synonym for Pointer<0>(). - *layout.Pointer(p_.get()) = size; - // Store the characters in the allocation. - memcpy(layout.Pointer(p_.get()), s, size + 1); - } - - size_t size() const { - // Equivalent to reinterpret_cast(*p). - return *L::Partial().Pointer(p_.get()); - } - - const char* c_str() const { - // Equivalent to reinterpret_cast(p.get() + sizeof(size_t)). - // The argument in Partial(1) specifies that we have size_t[1] in front of - // the characters. - return L::Partial(1).Pointer(p_.get()); - } - - private: - // Our heap allocation contains a size_t followed by an array of chars. - using L = Layout; - std::unique_ptr p_; -}; - -TEST(CompactString, Works) { - CompactString s = "hello"; - EXPECT_EQ(5, s.size()); - EXPECT_STREQ("hello", s.c_str()); -} - -} // namespace example - -} // namespace -} // namespace container_internal -ABSL_NAMESPACE_END -} // namespace absl diff --git a/third_party/abseil_cpp/absl/container/internal/node_hash_policy.h b/third_party/abseil_cpp/absl/container/internal/node_hash_policy.h deleted file mode 100644 index 4617162f0b32..000000000000 --- a/third_party/abseil_cpp/absl/container/internal/node_hash_policy.h +++ /dev/null @@ -1,92 +0,0 @@ -// 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 -// value_type* new_element(Alloc* alloc, Args&&... args) const; -// -// // Destroys and deallocates node using the allocator. -// template -// 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_HASH_POLICY_H_ -#define ABSL_CONTAINER_INTERNAL_NODE_HASH_POLICY_H_ - -#include -#include -#include -#include -#include - -#include "absl/base/config.h" - -namespace absl { -ABSL_NAMESPACE_BEGIN -namespace container_internal { - -template -struct node_hash_policy { - static_assert(std::is_lvalue_reference::value, ""); - - using slot_type = typename std::remove_cv< - typename std::remove_reference::type>::type*; - - template - static void construct(Alloc* alloc, slot_type* slot, Args&&... args) { - *slot = Policy::new_element(alloc, std::forward(args)...); - } - - template - static void destroy(Alloc* alloc, slot_type* slot) { - Policy::delete_element(alloc, *slot); - } - - template - static void transfer(Alloc*, slot_type* new_slot, slot_type* old_slot) { - *new_slot = *old_slot; - } - - 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 - static auto value(T* elem) -> decltype(P::value(elem)) { - return P::value(elem); - } - - template - static auto apply(Ts&&... ts) -> decltype(P::apply(std::forward(ts)...)) { - return P::apply(std::forward(ts)...); - } -}; - -} // namespace container_internal -ABSL_NAMESPACE_END -} // namespace absl - -#endif // ABSL_CONTAINER_INTERNAL_NODE_HASH_POLICY_H_ diff --git a/third_party/abseil_cpp/absl/container/internal/node_hash_policy_test.cc b/third_party/abseil_cpp/absl/container/internal/node_hash_policy_test.cc deleted file mode 100644 index 84aabba96830..000000000000 --- a/third_party/abseil_cpp/absl/container/internal/node_hash_policy_test.cc +++ /dev/null @@ -1,69 +0,0 @@ -// 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_hash_policy.h" - -#include - -#include "gmock/gmock.h" -#include "gtest/gtest.h" -#include "absl/container/internal/hash_policy_traits.h" - -namespace absl { -ABSL_NAMESPACE_BEGIN -namespace container_internal { -namespace { - -using ::testing::Pointee; - -struct Policy : node_hash_policy { - using key_type = int; - using init_type = int; - - template - static int* new_element(Alloc* alloc, int value) { - return new int(value); - } - - template - static void delete_element(Alloc* alloc, int* elem) { - delete elem; - } -}; - -using NodePolicy = hash_policy_traits; - -struct NodeTest : ::testing::Test { - std::allocator 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); -} - -} // namespace -} // namespace container_internal -ABSL_NAMESPACE_END -} // namespace absl diff --git a/third_party/abseil_cpp/absl/container/internal/raw_hash_map.h b/third_party/abseil_cpp/absl/container/internal/raw_hash_map.h deleted file mode 100644 index 0a02757ddfb4..000000000000 --- a/third_party/abseil_cpp/absl/container/internal/raw_hash_map.h +++ /dev/null @@ -1,197 +0,0 @@ -// 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 -#include -#include - -#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 raw_hash_map : public raw_hash_set { - // P is Policy. It's passed as a template argument to support maps that have - // incomplete types as values, as in unordered_map. - // MappedReference<> may be a non-reference type. - template - using MappedReference = decltype(P::value( - std::addressof(std::declval()))); - - // MappedConstReference<> may be a non-reference type. - template - using MappedConstReference = decltype(P::value( - std::addressof(std::declval()))); - - using KeyArgImpl = - KeyArg::value && IsTransparent::value>; - - public: - using key_type = typename Policy::key_type; - using mapped_type = typename Policy::mapped_type; - template - using key_arg = typename KeyArgImpl::template type; - - static_assert(!std::is_reference::value, ""); - // TODO(alkis): remove this assertion and verify that reference mapped_type is - // supported. - static_assert(!std::is_reference::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 m; - // m.insert_or_assign(n, n); - template - std::pair insert_or_assign(key_arg&& k, V&& v) { - return insert_or_assign_impl(std::forward(k), std::forward(v)); - } - - template - std::pair insert_or_assign(key_arg&& k, const V& v) { - return insert_or_assign_impl(std::forward(k), v); - } - - template - std::pair insert_or_assign(const key_arg& k, V&& v) { - return insert_or_assign_impl(k, std::forward(v)); - } - - template - std::pair insert_or_assign(const key_arg& k, const V& v) { - return insert_or_assign_impl(k, v); - } - - template - iterator insert_or_assign(const_iterator, key_arg&& k, V&& v) { - return insert_or_assign(std::forward(k), std::forward(v)).first; - } - - template - iterator insert_or_assign(const_iterator, key_arg&& k, const V& v) { - return insert_or_assign(std::forward(k), v).first; - } - - template - iterator insert_or_assign(const_iterator, const key_arg& k, V&& v) { - return insert_or_assign(k, std::forward(v)).first; - } - - template - iterator insert_or_assign(const_iterator, const key_arg& k, const V& v) { - 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 ::value, int>::type = 0, - K* = nullptr> - std::pair try_emplace(key_arg&& k, Args&&... args) { - return try_emplace_impl(std::forward(k), std::forward(args)...); - } - - template ::value, int>::type = 0> - std::pair try_emplace(const key_arg& k, Args&&... args) { - return try_emplace_impl(k, std::forward(args)...); - } - - template - iterator try_emplace(const_iterator, key_arg&& k, Args&&... args) { - return try_emplace(std::forward(k), std::forward(args)...).first; - } - - template - iterator try_emplace(const_iterator, const key_arg& k, Args&&... args) { - return try_emplace(k, std::forward(args)...).first; - } - - template - MappedReference

at(const key_arg& key) { - auto it = this->find(key); - if (it == this->end()) { - base_internal::ThrowStdOutOfRange( - "absl::container_internal::raw_hash_map<>::at"); - } - return Policy::value(&*it); - } - - template - MappedConstReference

at(const key_arg& key) const { - auto it = this->find(key); - if (it == this->end()) { - base_internal::ThrowStdOutOfRange( - "absl::container_internal::raw_hash_map<>::at"); - } - return Policy::value(&*it); - } - - template - MappedReference

operator[](key_arg&& key) { - return Policy::value(&*try_emplace(std::forward(key)).first); - } - - template - MappedReference

operator[](const key_arg& key) { - return Policy::value(&*try_emplace(key).first); - } - - private: - template - std::pair insert_or_assign_impl(K&& k, V&& v) { - auto res = this->find_or_prepare_insert(k); - if (res.second) - this->emplace_at(res.first, std::forward(k), std::forward(v)); - else - Policy::value(&*this->iterator_at(res.first)) = std::forward(v); - return {this->iterator_at(res.first), res.second}; - } - - template - std::pair try_emplace_impl(K&& k, Args&&... args) { - 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)), - std::forward_as_tuple(std::forward(args)...)); - return {this->iterator_at(res.first), res.second}; - } -}; - -} // namespace container_internal -ABSL_NAMESPACE_END -} // namespace absl - -#endif // ABSL_CONTAINER_INTERNAL_RAW_HASH_MAP_H_ diff --git a/third_party/abseil_cpp/absl/container/internal/raw_hash_set.cc b/third_party/abseil_cpp/absl/container/internal/raw_hash_set.cc deleted file mode 100644 index bfef071f29e6..000000000000 --- a/third_party/abseil_cpp/absl/container/internal/raw_hash_set.cc +++ /dev/null @@ -1,61 +0,0 @@ -// 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 -#include - -#include "absl/base/config.h" - -namespace absl { -ABSL_NAMESPACE_BEGIN -namespace container_internal { - -constexpr size_t Group::kWidth; - -// Returns "random" seed. -inline size_t RandomSeed() { -#ifdef ABSL_HAVE_THREAD_LOCAL - static thread_local size_t counter = 0; - size_t value = ++counter; -#else // ABSL_HAVE_THREAD_LOCAL - static std::atomic counter(0); - size_t value = counter.fetch_add(1, std::memory_order_relaxed); -#endif // ABSL_HAVE_THREAD_LOCAL - return value ^ static_cast(reinterpret_cast(&counter)); -} - -bool ShouldInsertBackwards(size_t hash, 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 (H1(hash, ctrl) ^ RandomSeed()) % 13 > 6; -} - -void ConvertDeletedToEmptyAndFullToDeleted( - ctrl_t* ctrl, size_t capacity) { - assert(ctrl[capacity] == kSentinel); - assert(IsValidCapacity(capacity)); - for (ctrl_t* pos = ctrl; pos != ctrl + capacity + 1; pos += Group::kWidth) { - Group{pos}.ConvertSpecialToEmptyAndFullToDeleted(pos); - } - // Copy the cloned ctrl bytes. - std::memcpy(ctrl + capacity + 1, ctrl, Group::kWidth); - ctrl[capacity] = kSentinel; -} - - -} // namespace container_internal -ABSL_NAMESPACE_END -} // namespace absl diff --git a/third_party/abseil_cpp/absl/container/internal/raw_hash_set.h b/third_party/abseil_cpp/absl/container/internal/raw_hash_set.h deleted file mode 100644 index 02158c4e0886..000000000000 --- a/third_party/abseil_cpp/absl/container/internal/raw_hash_set.h +++ /dev/null @@ -1,1903 +0,0 @@ -// 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. -// -// An open-addressing -// hashtable with quadratic probing. -// -// This is a low level hashtable on top of which different interfaces can be -// implemented, like flat_hash_set, node_hash_set, string_hash_set, etc. -// -// The table interface is similar to that of std::unordered_set. Notable -// differences are that most member functions support heterogeneous keys when -// BOTH the hash and eq functions are marked as transparent. They do so by -// providing a typedef called `is_transparent`. -// -// When heterogeneous lookup is enabled, functions that take key_type act as if -// they have an overload set like: -// -// iterator find(const key_type& key); -// template -// iterator find(const K& key); -// -// size_type erase(const key_type& key); -// template -// size_type erase(const K& key); -// -// std::pair equal_range(const key_type& key); -// template -// std::pair equal_range(const K& key); -// -// When heterogeneous lookup is disabled, only the explicit `key_type` overloads -// exist. -// -// find() also supports passing the hash explicitly: -// -// iterator find(const key_type& key, size_t hash); -// template -// iterator find(const U& key, size_t hash); -// -// In addition the pointer to element and iterator stability guarantees are -// weaker: all iterators and pointers are invalidated after a new element is -// inserted. -// -// IMPLEMENTATION DETAILS -// -// The table stores elements inline in a slot array. In addition to the slot -// array the table maintains some control state per slot. The extra state is one -// byte per slot and stores empty or deleted marks, or alternatively 7 bits from -// the hash of an occupied slot. The table is split into logical groups of -// slots, like so: -// -// Group 1 Group 2 Group 3 -// +---------------+---------------+---------------+ -// | | | | | | | | | | | | | | | | | | | | | | | | | -// +---------------+---------------+---------------+ -// -// On lookup the hash is split into two parts: -// - H2: 7 bits (those stored in the control bytes) -// - H1: the rest of the bits -// The groups are probed using H1. For each group the slots are matched to H2 in -// parallel. Because H2 is 7 bits (128 states) and the number of slots per group -// is low (8 or 16) in almost all cases a match in H2 is also a lookup hit. -// -// On insert, once the right group is found (as in lookup), its slots are -// filled in order. -// -// On erase a slot is cleared. In case the group did not have any empty slots -// before the erase, the erased slot is marked as deleted. -// -// Groups without empty slots (but maybe with deleted slots) extend the probe -// sequence. The probing algorithm is quadratic. Given N the number of groups, -// the probing function for the i'th probe is: -// -// P(0) = H1 % N -// -// P(i) = (P(i - 1) + i) % N -// -// This probing function guarantees that after N probes, all the groups of the -// table will be probed exactly once. - -#ifndef ABSL_CONTAINER_INTERNAL_RAW_HASH_SET_H_ -#define ABSL_CONTAINER_INTERNAL_RAW_HASH_SET_H_ - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include "absl/base/internal/bits.h" -#include "absl/base/internal/endian.h" -#include "absl/base/optimization.h" -#include "absl/base/port.h" -#include "absl/container/internal/common.h" -#include "absl/container/internal/compressed_tuple.h" -#include "absl/container/internal/container_memory.h" -#include "absl/container/internal/hash_policy_traits.h" -#include "absl/container/internal/hashtable_debug_hooks.h" -#include "absl/container/internal/hashtablez_sampler.h" -#include "absl/container/internal/have_sse.h" -#include "absl/container/internal/layout.h" -#include "absl/memory/memory.h" -#include "absl/meta/type_traits.h" -#include "absl/utility/utility.h" - -namespace absl { -ABSL_NAMESPACE_BEGIN -namespace container_internal { - -template -void SwapAlloc(AllocType& lhs, AllocType& rhs, - std::true_type /* propagate_on_container_swap */) { - using std::swap; - swap(lhs, rhs); -} -template -void SwapAlloc(AllocType& /*lhs*/, AllocType& /*rhs*/, - std::false_type /* propagate_on_container_swap */) {} - -template -class probe_seq { - public: - probe_seq(size_t hash, size_t mask) { - assert(((mask + 1) & mask) == 0 && "not a mask"); - mask_ = mask; - offset_ = hash & mask_; - } - size_t offset() const { return offset_; } - size_t offset(size_t i) const { return (offset_ + i) & mask_; } - - void next() { - index_ += Width; - offset_ += index_; - offset_ &= mask_; - } - // 0-based probe index. The i-th probe in the probe sequence. - size_t index() const { return index_; } - - private: - size_t mask_; - size_t offset_; - size_t index_ = 0; -}; - -template -struct RequireUsableKey { - template - std::pair< - decltype(std::declval()(std::declval())), - decltype(std::declval()(std::declval(), - std::declval()))>* - operator()(const PassedKey&, const Args&...) const; -}; - -template -struct IsDecomposable : std::false_type {}; - -template -struct IsDecomposable< - absl::void_t(), - std::declval()...))>, - Policy, Hash, Eq, Ts...> : std::true_type {}; - -// TODO(alkis): Switch to std::is_nothrow_swappable when gcc/clang supports it. -template -constexpr bool IsNoThrowSwappable(std::true_type = {} /* is_swappable */) { - using std::swap; - return noexcept(swap(std::declval(), std::declval())); -} -template -constexpr bool IsNoThrowSwappable(std::false_type /* is_swappable */) { - return false; -} - -template -int TrailingZeros(T x) { - return sizeof(T) == 8 ? base_internal::CountTrailingZerosNonZero64( - static_cast(x)) - : base_internal::CountTrailingZerosNonZero32( - static_cast(x)); -} - -template -int LeadingZeros(T x) { - return sizeof(T) == 8 - ? base_internal::CountLeadingZeros64(static_cast(x)) - : base_internal::CountLeadingZeros32(static_cast(x)); -} - -// An abstraction over a bitmask. It provides an easy way to iterate through the -// indexes of the set bits of a bitmask. When Shift=0 (platforms with SSE), -// this is a true bitmask. On non-SSE, platforms the arithematic used to -// emulate the SSE behavior works in bytes (Shift=3) and leaves each bytes as -// either 0x00 or 0x80. -// -// For example: -// for (int i : BitMask(0x5)) -> yields 0, 2 -// for (int i : BitMask(0x0000000080800000)) -> yields 2, 3 -template -class BitMask { - static_assert(std::is_unsigned::value, ""); - static_assert(Shift == 0 || Shift == 3, ""); - - public: - // These are useful for unit tests (gunit). - using value_type = int; - using iterator = BitMask; - using const_iterator = BitMask; - - explicit BitMask(T mask) : mask_(mask) {} - BitMask& operator++() { - mask_ &= (mask_ - 1); - return *this; - } - explicit operator bool() const { return mask_ != 0; } - int operator*() const { return LowestBitSet(); } - int LowestBitSet() const { - return container_internal::TrailingZeros(mask_) >> Shift; - } - int HighestBitSet() const { - return (sizeof(T) * CHAR_BIT - container_internal::LeadingZeros(mask_) - - 1) >> - Shift; - } - - BitMask begin() const { return *this; } - BitMask end() const { return BitMask(0); } - - int TrailingZeros() const { - return container_internal::TrailingZeros(mask_) >> Shift; - } - - int LeadingZeros() const { - constexpr int total_significant_bits = SignificantBits << Shift; - constexpr int extra_bits = sizeof(T) * 8 - total_significant_bits; - return container_internal::LeadingZeros(mask_ << extra_bits) >> Shift; - } - - private: - friend bool operator==(const BitMask& a, const BitMask& b) { - return a.mask_ == b.mask_; - } - friend bool operator!=(const BitMask& a, const BitMask& b) { - return a.mask_ != b.mask_; - } - - T mask_; -}; - -using ctrl_t = signed char; -using h2_t = uint8_t; - -// The values here are selected for maximum performance. See the static asserts -// below for details. -enum Ctrl : ctrl_t { - kEmpty = -128, // 0b10000000 - kDeleted = -2, // 0b11111110 - kSentinel = -1, // 0b11111111 -}; -static_assert( - kEmpty & kDeleted & kSentinel & 0x80, - "Special markers need to have the MSB to make checking for them efficient"); -static_assert(kEmpty < kSentinel && kDeleted < kSentinel, - "kEmpty and kDeleted must be smaller than kSentinel to make the " - "SIMD test of IsEmptyOrDeleted() efficient"); -static_assert(kSentinel == -1, - "kSentinel must be -1 to elide loading it from memory into SIMD " - "registers (pcmpeqd xmm, xmm)"); -static_assert(kEmpty == -128, - "kEmpty must be -128 to make the SIMD check for its " - "existence efficient (psignb xmm, xmm)"); -static_assert(~kEmpty & ~kDeleted & kSentinel & 0x7F, - "kEmpty and kDeleted must share an unset bit that is not shared " - "by kSentinel to make the scalar test for MatchEmptyOrDeleted() " - "efficient"); -static_assert(kDeleted == -2, - "kDeleted must be -2 to make the implementation of " - "ConvertSpecialToEmptyAndFullToDeleted efficient"); - -// A single block of empty control bytes for tables without any slots allocated. -// This enables removing a branch in the hot path of find(). -inline ctrl_t* EmptyGroup() { - alignas(16) static constexpr ctrl_t empty_group[] = { - kSentinel, kEmpty, kEmpty, kEmpty, kEmpty, kEmpty, kEmpty, kEmpty, - kEmpty, kEmpty, kEmpty, kEmpty, kEmpty, kEmpty, kEmpty, kEmpty}; - return const_cast(empty_group); -} - -// Mixes a randomly generated per-process seed with `hash` and `ctrl` to -// randomize insertion order within groups. -bool ShouldInsertBackwards(size_t hash, ctrl_t* ctrl); - -// Returns a hash seed. -// -// The seed consists of the ctrl_ pointer, which adds enough entropy to ensure -// non-determinism of iteration order in most cases. -inline size_t HashSeed(const ctrl_t* ctrl) { - // The low bits of the pointer have little or no entropy because of - // alignment. We shift the pointer to try to use higher entropy bits. A - // good number seems to be 12 bits, because that aligns with page size. - return reinterpret_cast(ctrl) >> 12; -} - -inline size_t H1(size_t hash, const ctrl_t* ctrl) { - return (hash >> 7) ^ HashSeed(ctrl); -} -inline ctrl_t H2(size_t hash) { return hash & 0x7F; } - -inline bool IsEmpty(ctrl_t c) { return c == kEmpty; } -inline bool IsFull(ctrl_t c) { return c >= 0; } -inline bool IsDeleted(ctrl_t c) { return c == kDeleted; } -inline bool IsEmptyOrDeleted(ctrl_t c) { return c < kSentinel; } - -#if ABSL_INTERNAL_RAW_HASH_SET_HAVE_SSE2 - -// https://github.com/abseil/abseil-cpp/issues/209 -// https://gcc.gnu.org/bugzilla/show_bug.cgi?id=87853 -// _mm_cmpgt_epi8 is broken under GCC with -funsigned-char -// Work around this by using the portable implementation of Group -// when using -funsigned-char under GCC. -inline __m128i _mm_cmpgt_epi8_fixed(__m128i a, __m128i b) { -#if defined(__GNUC__) && !defined(__clang__) - if (std::is_unsigned::value) { - const __m128i mask = _mm_set1_epi8(0x80); - const __m128i diff = _mm_subs_epi8(b, a); - return _mm_cmpeq_epi8(_mm_and_si128(diff, mask), mask); - } -#endif - return _mm_cmpgt_epi8(a, b); -} - -struct GroupSse2Impl { - static constexpr size_t kWidth = 16; // the number of slots per group - - explicit GroupSse2Impl(const ctrl_t* pos) { - ctrl = _mm_loadu_si128(reinterpret_cast(pos)); - } - - // Returns a bitmask representing the positions of slots that match hash. - BitMask Match(h2_t hash) const { - auto match = _mm_set1_epi8(hash); - return BitMask( - _mm_movemask_epi8(_mm_cmpeq_epi8(match, ctrl))); - } - - // Returns a bitmask representing the positions of empty slots. - BitMask MatchEmpty() const { -#if ABSL_INTERNAL_RAW_HASH_SET_HAVE_SSSE3 - // This only works because kEmpty is -128. - return BitMask( - _mm_movemask_epi8(_mm_sign_epi8(ctrl, ctrl))); -#else - return Match(static_cast(kEmpty)); -#endif - } - - // Returns a bitmask representing the positions of empty or deleted slots. - BitMask MatchEmptyOrDeleted() const { - auto special = _mm_set1_epi8(kSentinel); - return BitMask( - _mm_movemask_epi8(_mm_cmpgt_epi8_fixed(special, ctrl))); - } - - // Returns the number of trailing empty or deleted elements in the group. - uint32_t CountLeadingEmptyOrDeleted() const { - auto special = _mm_set1_epi8(kSentinel); - return TrailingZeros( - _mm_movemask_epi8(_mm_cmpgt_epi8_fixed(special, ctrl)) + 1); - } - - void ConvertSpecialToEmptyAndFullToDeleted(ctrl_t* dst) const { - auto msbs = _mm_set1_epi8(static_cast(-128)); - auto x126 = _mm_set1_epi8(126); -#if ABSL_INTERNAL_RAW_HASH_SET_HAVE_SSSE3 - auto res = _mm_or_si128(_mm_shuffle_epi8(x126, ctrl), msbs); -#else - auto zero = _mm_setzero_si128(); - auto special_mask = _mm_cmpgt_epi8_fixed(zero, ctrl); - auto res = _mm_or_si128(msbs, _mm_andnot_si128(special_mask, x126)); -#endif - _mm_storeu_si128(reinterpret_cast<__m128i*>(dst), res); - } - - __m128i ctrl; -}; -#endif // ABSL_INTERNAL_RAW_HASH_SET_HAVE_SSE2 - -struct GroupPortableImpl { - static constexpr size_t kWidth = 8; - - explicit GroupPortableImpl(const ctrl_t* pos) - : ctrl(little_endian::Load64(pos)) {} - - BitMask Match(h2_t hash) const { - // For the technique, see: - // http://graphics.stanford.edu/~seander/bithacks.html##ValueInWord - // (Determine if a word has a byte equal to n). - // - // Caveat: there are false positives but: - // - they only occur if there is a real match - // - they never occur on kEmpty, kDeleted, kSentinel - // - they will be handled gracefully by subsequent checks in code - // - // Example: - // v = 0x1716151413121110 - // hash = 0x12 - // retval = (v - lsbs) & ~v & msbs = 0x0000000080800000 - constexpr uint64_t msbs = 0x8080808080808080ULL; - constexpr uint64_t lsbs = 0x0101010101010101ULL; - auto x = ctrl ^ (lsbs * hash); - return BitMask((x - lsbs) & ~x & msbs); - } - - BitMask MatchEmpty() const { - constexpr uint64_t msbs = 0x8080808080808080ULL; - return BitMask((ctrl & (~ctrl << 6)) & msbs); - } - - BitMask MatchEmptyOrDeleted() const { - constexpr uint64_t msbs = 0x8080808080808080ULL; - return BitMask((ctrl & (~ctrl << 7)) & msbs); - } - - uint32_t CountLeadingEmptyOrDeleted() const { - constexpr uint64_t gaps = 0x00FEFEFEFEFEFEFEULL; - return (TrailingZeros(((~ctrl & (ctrl >> 7)) | gaps) + 1) + 7) >> 3; - } - - void ConvertSpecialToEmptyAndFullToDeleted(ctrl_t* dst) const { - constexpr uint64_t msbs = 0x8080808080808080ULL; - constexpr uint64_t lsbs = 0x0101010101010101ULL; - auto x = ctrl & msbs; - auto res = (~x + (x >> 7)) & ~lsbs; - little_endian::Store64(dst, res); - } - - uint64_t ctrl; -}; - -#if ABSL_INTERNAL_RAW_HASH_SET_HAVE_SSE2 -using Group = GroupSse2Impl; -#else -using Group = GroupPortableImpl; -#endif - -template -class raw_hash_set; - -inline bool IsValidCapacity(size_t n) { return ((n + 1) & n) == 0 && n > 0; } - -// PRECONDITION: -// IsValidCapacity(capacity) -// ctrl[capacity] == kSentinel -// ctrl[i] != kSentinel for all i < capacity -// Applies mapping for every byte in ctrl: -// DELETED -> EMPTY -// EMPTY -> EMPTY -// FULL -> DELETED -void ConvertDeletedToEmptyAndFullToDeleted(ctrl_t* ctrl, size_t capacity); - -// Rounds up the capacity to the next power of 2 minus 1, with a minimum of 1. -inline size_t NormalizeCapacity(size_t n) { - return n ? ~size_t{} >> LeadingZeros(n) : 1; -} - -// We use 7/8th as maximum load factor. -// For 16-wide groups, that gives an average of two empty slots per group. -inline size_t CapacityToGrowth(size_t capacity) { - assert(IsValidCapacity(capacity)); - // `capacity*7/8` - if (Group::kWidth == 8 && capacity == 7) { - // x-x/8 does not work when x==7. - return 6; - } - return capacity - capacity / 8; -} -// From desired "growth" to a lowerbound of the necessary capacity. -// Might not be a valid one and required NormalizeCapacity(). -inline size_t GrowthToLowerboundCapacity(size_t growth) { - // `growth*8/7` - if (Group::kWidth == 8 && growth == 7) { - // x+(x-1)/7 does not work when x==7. - return 8; - } - return growth + static_cast((static_cast(growth) - 1) / 7); -} - -inline void AssertIsFull(ctrl_t* ctrl) { - ABSL_HARDENING_ASSERT((ctrl != nullptr && IsFull(*ctrl)) && - "Invalid operation on iterator. The element might have " - "been erased, or the table might have rehashed."); -} - -inline void AssertIsValid(ctrl_t* ctrl) { - ABSL_HARDENING_ASSERT((ctrl == nullptr || IsFull(*ctrl)) && - "Invalid operation on iterator. The element might have " - "been erased, or the table might have rehashed."); -} - -struct FindInfo { - size_t offset; - size_t probe_length; -}; - -// The representation of the object has two modes: -// - small: For capacities < kWidth-1 -// - large: For the rest. -// -// Differences: -// - In small mode we are able to use the whole capacity. The extra control -// bytes give us at least one "empty" control byte to stop the iteration. -// This is important to make 1 a valid capacity. -// -// - In small mode only the first `capacity()` control bytes after the -// sentinel are valid. The rest contain dummy kEmpty values that do not -// represent a real slot. This is important to take into account on -// find_first_non_full(), where we never try ShouldInsertBackwards() for -// small tables. -inline bool is_small(size_t capacity) { return capacity < Group::kWidth - 1; } - -inline probe_seq probe(ctrl_t* ctrl, size_t hash, - size_t capacity) { - return probe_seq(H1(hash, ctrl), capacity); -} - -// Probes the raw_hash_set with the probe sequence for hash and returns the -// pointer to the first empty or deleted slot. -// NOTE: this function must work with tables having both kEmpty and kDelete -// in one group. Such tables appears during drop_deletes_without_resize. -// -// This function is very useful when insertions happen and: -// - the input is already a set -// - there are enough slots -// - the element with the hash is not in the table -inline FindInfo find_first_non_full(ctrl_t* ctrl, size_t hash, - size_t capacity) { - auto seq = probe(ctrl, hash, capacity); - while (true) { - Group g{ctrl + seq.offset()}; - auto mask = g.MatchEmptyOrDeleted(); - if (mask) { -#if !defined(NDEBUG) - // We want to add entropy even when ASLR is not enabled. - // In debug build we will randomly insert in either the front or back of - // the group. - // TODO(kfm,sbenza): revisit after we do unconditional mixing - if (!is_small(capacity) && ShouldInsertBackwards(hash, ctrl)) { - return {seq.offset(mask.HighestBitSet()), seq.index()}; - } -#endif - return {seq.offset(mask.LowestBitSet()), seq.index()}; - } - seq.next(); - assert(seq.index() < capacity && "full table!"); - } -} - -// Policy: a policy defines how to perform different operations on -// the slots of the hashtable (see hash_policy_traits.h for the full interface -// of policy). -// -// Hash: a (possibly polymorphic) functor that hashes keys of the hashtable. The -// functor should accept a key and return size_t as hash. For best performance -// it is important that the hash function provides high entropy across all bits -// of the hash. -// -// Eq: a (possibly polymorphic) functor that compares two keys for equality. It -// should accept two (of possibly different type) keys and return a bool: true -// if they are equal, false if they are not. If two keys compare equal, then -// their hash values as defined by Hash MUST be equal. -// -// Allocator: an Allocator -// [https://en.cppreference.com/w/cpp/named_req/Allocator] with which -// the storage of the hashtable will be allocated and the elements will be -// constructed and destroyed. -template -class raw_hash_set { - using PolicyTraits = hash_policy_traits; - using KeyArgImpl = - KeyArg::value && IsTransparent::value>; - - public: - using init_type = typename PolicyTraits::init_type; - using key_type = typename PolicyTraits::key_type; - // TODO(sbenza): Hide slot_type as it is an implementation detail. Needs user - // code fixes! - using slot_type = typename PolicyTraits::slot_type; - using allocator_type = Alloc; - using size_type = size_t; - using difference_type = ptrdiff_t; - using hasher = Hash; - using key_equal = Eq; - using policy_type = Policy; - using value_type = typename PolicyTraits::value_type; - using reference = value_type&; - using const_reference = const value_type&; - using pointer = typename absl::allocator_traits< - allocator_type>::template rebind_traits::pointer; - using const_pointer = typename absl::allocator_traits< - allocator_type>::template rebind_traits::const_pointer; - - // Alias used for heterogeneous lookup functions. - // `key_arg` 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 - using key_arg = typename KeyArgImpl::template type; - - private: - // Give an early error when key_type is not hashable/eq. - auto KeyTypeCanBeHashed(const Hash& h, const key_type& k) -> decltype(h(k)); - auto KeyTypeCanBeEq(const Eq& eq, const key_type& k) -> decltype(eq(k, k)); - - using Layout = absl::container_internal::Layout; - - static Layout MakeLayout(size_t capacity) { - assert(IsValidCapacity(capacity)); - return Layout(capacity + Group::kWidth + 1, capacity); - } - - using AllocTraits = absl::allocator_traits; - using SlotAlloc = typename absl::allocator_traits< - allocator_type>::template rebind_alloc; - using SlotAllocTraits = typename absl::allocator_traits< - allocator_type>::template rebind_traits; - - static_assert(std::is_lvalue_reference::value, - "Policy::element() must return a reference"); - - template - struct SameAsElementReference - : std::is_same::type>::type, - typename std::remove_cv< - typename std::remove_reference::type>::type> {}; - - // An enabler for insert(T&&): T must be convertible to init_type or be the - // same as [cv] value_type [ref]. - // Note: we separate SameAsElementReference into its own type to avoid using - // reference unless we need to. MSVC doesn't seem to like it in some - // cases. - template - using RequiresInsertable = typename std::enable_if< - absl::disjunction, - SameAsElementReference>::value, - int>::type; - - // RequiresNotInit is a workaround for gcc prior to 7.1. - // See https://godbolt.org/g/Y4xsUh. - template - using RequiresNotInit = - typename std::enable_if::value, int>::type; - - template - using IsDecomposable = IsDecomposable; - - public: - static_assert(std::is_same::value, - "Allocators with custom pointer types are not supported"); - static_assert(std::is_same::value, - "Allocators with custom pointer types are not supported"); - - class iterator { - friend class raw_hash_set; - - public: - using iterator_category = std::forward_iterator_tag; - using value_type = typename raw_hash_set::value_type; - using reference = - absl::conditional_t; - using pointer = absl::remove_reference_t*; - using difference_type = typename raw_hash_set::difference_type; - - iterator() {} - - // PRECONDITION: not an end() iterator. - reference operator*() const { - AssertIsFull(ctrl_); - return PolicyTraits::element(slot_); - } - - // PRECONDITION: not an end() iterator. - pointer operator->() const { return &operator*(); } - - // PRECONDITION: not an end() iterator. - iterator& operator++() { - AssertIsFull(ctrl_); - ++ctrl_; - ++slot_; - skip_empty_or_deleted(); - return *this; - } - // PRECONDITION: not an end() iterator. - iterator operator++(int) { - auto tmp = *this; - ++*this; - return tmp; - } - - friend bool operator==(const iterator& a, const iterator& b) { - AssertIsValid(a.ctrl_); - AssertIsValid(b.ctrl_); - return a.ctrl_ == b.ctrl_; - } - friend bool operator!=(const iterator& a, const iterator& b) { - return !(a == b); - } - - private: - iterator(ctrl_t* ctrl, slot_type* slot) : ctrl_(ctrl), slot_(slot) { - // This assumption helps the compiler know that any non-end iterator is - // not equal to any end iterator. - ABSL_INTERNAL_ASSUME(ctrl != nullptr); - } - - void skip_empty_or_deleted() { - while (IsEmptyOrDeleted(*ctrl_)) { - uint32_t shift = Group{ctrl_}.CountLeadingEmptyOrDeleted(); - ctrl_ += shift; - slot_ += shift; - } - if (ABSL_PREDICT_FALSE(*ctrl_ == kSentinel)) ctrl_ = nullptr; - } - - ctrl_t* ctrl_ = nullptr; - // To avoid uninitialized member warnings, put slot_ in an anonymous union. - // The member is not initialized on singleton and end iterators. - union { - slot_type* slot_; - }; - }; - - class const_iterator { - friend class raw_hash_set; - - public: - using iterator_category = typename iterator::iterator_category; - using value_type = typename raw_hash_set::value_type; - using reference = typename raw_hash_set::const_reference; - using pointer = typename raw_hash_set::const_pointer; - using difference_type = typename raw_hash_set::difference_type; - - const_iterator() {} - // Implicit construction from iterator. - const_iterator(iterator i) : inner_(std::move(i)) {} - - reference operator*() const { return *inner_; } - pointer operator->() const { return inner_.operator->(); } - - const_iterator& operator++() { - ++inner_; - return *this; - } - const_iterator operator++(int) { return inner_++; } - - friend bool operator==(const const_iterator& a, const const_iterator& b) { - return a.inner_ == b.inner_; - } - friend bool operator!=(const const_iterator& a, const const_iterator& b) { - return !(a == b); - } - - private: - const_iterator(const ctrl_t* ctrl, const slot_type* slot) - : inner_(const_cast(ctrl), const_cast(slot)) {} - - iterator inner_; - }; - - using node_type = node_handle, Alloc>; - using insert_return_type = InsertReturnType; - - raw_hash_set() noexcept( - std::is_nothrow_default_constructible::value&& - std::is_nothrow_default_constructible::value&& - std::is_nothrow_default_constructible::value) {} - - explicit raw_hash_set(size_t bucket_count, const hasher& hash = hasher(), - const key_equal& eq = key_equal(), - const allocator_type& alloc = allocator_type()) - : ctrl_(EmptyGroup()), settings_(0, hash, eq, alloc) { - if (bucket_count) { - capacity_ = NormalizeCapacity(bucket_count); - initialize_slots(); - } - } - - raw_hash_set(size_t bucket_count, const hasher& hash, - const allocator_type& alloc) - : raw_hash_set(bucket_count, hash, key_equal(), alloc) {} - - raw_hash_set(size_t bucket_count, const allocator_type& alloc) - : raw_hash_set(bucket_count, hasher(), key_equal(), alloc) {} - - explicit raw_hash_set(const allocator_type& alloc) - : raw_hash_set(0, hasher(), key_equal(), alloc) {} - - template - raw_hash_set(InputIter first, InputIter last, size_t bucket_count = 0, - const hasher& hash = hasher(), const key_equal& eq = key_equal(), - const allocator_type& alloc = allocator_type()) - : raw_hash_set(bucket_count, hash, eq, alloc) { - insert(first, last); - } - - template - raw_hash_set(InputIter first, InputIter last, size_t bucket_count, - const hasher& hash, const allocator_type& alloc) - : raw_hash_set(first, last, bucket_count, hash, key_equal(), alloc) {} - - template - raw_hash_set(InputIter first, InputIter last, size_t bucket_count, - const allocator_type& alloc) - : raw_hash_set(first, last, bucket_count, hasher(), key_equal(), alloc) {} - - template - raw_hash_set(InputIter first, InputIter last, const allocator_type& alloc) - : raw_hash_set(first, last, 0, hasher(), key_equal(), alloc) {} - - // Instead of accepting std::initializer_list as the first - // argument like std::unordered_set does, we have two overloads - // that accept std::initializer_list and std::initializer_list. - // This is advantageous for performance. - // - // // Turns {"abc", "def"} into std::initializer_list, then - // // copies the strings into the set. - // std::unordered_set s = {"abc", "def"}; - // - // // Turns {"abc", "def"} into std::initializer_list, then - // // copies the strings into the set. - // absl::flat_hash_set s = {"abc", "def"}; - // - // The same trick is used in insert(). - // - // The enabler is necessary to prevent this constructor from triggering where - // the copy constructor is meant to be called. - // - // absl::flat_hash_set a, b{a}; - // - // RequiresNotInit is a workaround for gcc prior to 7.1. - template = 0, RequiresInsertable = 0> - raw_hash_set(std::initializer_list init, size_t bucket_count = 0, - const hasher& hash = hasher(), const key_equal& eq = key_equal(), - const allocator_type& alloc = allocator_type()) - : raw_hash_set(init.begin(), init.end(), bucket_count, hash, eq, alloc) {} - - raw_hash_set(std::initializer_list init, size_t bucket_count = 0, - const hasher& hash = hasher(), const key_equal& eq = key_equal(), - const allocator_type& alloc = allocator_type()) - : raw_hash_set(init.begin(), init.end(), bucket_count, hash, eq, alloc) {} - - template = 0, RequiresInsertable = 0> - raw_hash_set(std::initializer_list init, size_t bucket_count, - const hasher& hash, const allocator_type& alloc) - : raw_hash_set(init, bucket_count, hash, key_equal(), alloc) {} - - raw_hash_set(std::initializer_list init, size_t bucket_count, - const hasher& hash, const allocator_type& alloc) - : raw_hash_set(init, bucket_count, hash, key_equal(), alloc) {} - - template = 0, RequiresInsertable = 0> - raw_hash_set(std::initializer_list init, size_t bucket_count, - const allocator_type& alloc) - : raw_hash_set(init, bucket_count, hasher(), key_equal(), alloc) {} - - raw_hash_set(std::initializer_list init, size_t bucket_count, - const allocator_type& alloc) - : raw_hash_set(init, bucket_count, hasher(), key_equal(), alloc) {} - - template = 0, RequiresInsertable = 0> - raw_hash_set(std::initializer_list init, const allocator_type& alloc) - : raw_hash_set(init, 0, hasher(), key_equal(), alloc) {} - - raw_hash_set(std::initializer_list init, - const allocator_type& alloc) - : raw_hash_set(init, 0, hasher(), key_equal(), alloc) {} - - raw_hash_set(const raw_hash_set& that) - : raw_hash_set(that, AllocTraits::select_on_container_copy_construction( - that.alloc_ref())) {} - - raw_hash_set(const raw_hash_set& that, const allocator_type& a) - : raw_hash_set(0, that.hash_ref(), that.eq_ref(), a) { - reserve(that.size()); - // Because the table is guaranteed to be empty, we can do something faster - // than a full `insert`. - for (const auto& v : that) { - const size_t hash = PolicyTraits::apply(HashElement{hash_ref()}, v); - auto target = find_first_non_full(ctrl_, hash, capacity_); - set_ctrl(target.offset, H2(hash)); - emplace_at(target.offset, v); - infoz_.RecordInsert(hash, target.probe_length); - } - size_ = that.size(); - growth_left() -= that.size(); - } - - raw_hash_set(raw_hash_set&& that) noexcept( - std::is_nothrow_copy_constructible::value&& - std::is_nothrow_copy_constructible::value&& - std::is_nothrow_copy_constructible::value) - : ctrl_(absl::exchange(that.ctrl_, EmptyGroup())), - slots_(absl::exchange(that.slots_, nullptr)), - size_(absl::exchange(that.size_, 0)), - capacity_(absl::exchange(that.capacity_, 0)), - infoz_(absl::exchange(that.infoz_, HashtablezInfoHandle())), - // Hash, equality and allocator are copied instead of moved because - // `that` must be left valid. If Hash is std::function, moving it - // would create a nullptr functor that cannot be called. - settings_(that.settings_) { - // growth_left was copied above, reset the one from `that`. - that.growth_left() = 0; - } - - raw_hash_set(raw_hash_set&& that, const allocator_type& a) - : ctrl_(EmptyGroup()), - slots_(nullptr), - size_(0), - capacity_(0), - settings_(0, that.hash_ref(), that.eq_ref(), a) { - if (a == that.alloc_ref()) { - std::swap(ctrl_, that.ctrl_); - std::swap(slots_, that.slots_); - std::swap(size_, that.size_); - std::swap(capacity_, that.capacity_); - std::swap(growth_left(), that.growth_left()); - std::swap(infoz_, that.infoz_); - } else { - reserve(that.size()); - // Note: this will copy elements of dense_set and unordered_set instead of - // moving them. This can be fixed if it ever becomes an issue. - for (auto& elem : that) insert(std::move(elem)); - } - } - - raw_hash_set& operator=(const raw_hash_set& that) { - raw_hash_set tmp(that, - AllocTraits::propagate_on_container_copy_assignment::value - ? that.alloc_ref() - : alloc_ref()); - swap(tmp); - return *this; - } - - raw_hash_set& operator=(raw_hash_set&& that) noexcept( - absl::allocator_traits::is_always_equal::value&& - std::is_nothrow_move_assignable::value&& - std::is_nothrow_move_assignable::value) { - // TODO(sbenza): We should only use the operations from the noexcept clause - // to make sure we actually adhere to that contract. - return move_assign( - std::move(that), - typename AllocTraits::propagate_on_container_move_assignment()); - } - - ~raw_hash_set() { destroy_slots(); } - - iterator begin() { - auto it = iterator_at(0); - it.skip_empty_or_deleted(); - return it; - } - iterator end() { return {}; } - - const_iterator begin() const { - return const_cast(this)->begin(); - } - const_iterator end() const { return {}; } - const_iterator cbegin() const { return begin(); } - const_iterator cend() const { return end(); } - - bool empty() const { return !size(); } - size_t size() const { return size_; } - size_t capacity() const { return capacity_; } - size_t max_size() const { return (std::numeric_limits::max)(); } - - ABSL_ATTRIBUTE_REINITIALIZES void clear() { - // Iterating over this container is O(bucket_count()). When bucket_count() - // is much greater than size(), iteration becomes prohibitively expensive. - // For clear() it is more important to reuse the allocated array when the - // container is small because allocation takes comparatively long time - // compared to destruction of the elements of the container. So we pick the - // largest bucket_count() threshold for which iteration is still fast and - // past that we simply deallocate the array. - if (capacity_ > 127) { - destroy_slots(); - } else if (capacity_) { - for (size_t i = 0; i != capacity_; ++i) { - if (IsFull(ctrl_[i])) { - PolicyTraits::destroy(&alloc_ref(), slots_ + i); - } - } - size_ = 0; - reset_ctrl(); - reset_growth_left(); - } - assert(empty()); - infoz_.RecordStorageChanged(0, capacity_); - } - - // This overload kicks in when the argument is an rvalue of insertable and - // decomposable type other than init_type. - // - // flat_hash_map m; - // m.insert(std::make_pair("abc", 42)); - // TODO(cheshire): A type alias T2 is introduced as a workaround for the nvcc - // bug. - template = 0, - class T2 = T, - typename std::enable_if::value, int>::type = 0, - T* = nullptr> - std::pair insert(T&& value) { - return emplace(std::forward(value)); - } - - // This overload kicks in when the argument is a bitfield or an lvalue of - // insertable and decomposable type. - // - // union { int n : 1; }; - // flat_hash_set s; - // s.insert(n); - // - // flat_hash_set s; - // const char* p = "hello"; - // s.insert(p); - // - // TODO(romanp): Once we stop supporting gcc 5.1 and below, replace - // RequiresInsertable with RequiresInsertable. - // We are hitting this bug: https://godbolt.org/g/1Vht4f. - template < - class T, RequiresInsertable = 0, - typename std::enable_if::value, int>::type = 0> - std::pair insert(const T& value) { - return emplace(value); - } - - // This overload kicks in when the argument is an rvalue of init_type. Its - // purpose is to handle brace-init-list arguments. - // - // flat_hash_map s; - // s.insert({"abc", 42}); - std::pair insert(init_type&& value) { - return emplace(std::move(value)); - } - - // TODO(cheshire): A type alias T2 is introduced as a workaround for the nvcc - // bug. - template = 0, class T2 = T, - typename std::enable_if::value, int>::type = 0, - T* = nullptr> - iterator insert(const_iterator, T&& value) { - return insert(std::forward(value)).first; - } - - // TODO(romanp): Once we stop supporting gcc 5.1 and below, replace - // RequiresInsertable with RequiresInsertable. - // We are hitting this bug: https://godbolt.org/g/1Vht4f. - template < - class T, RequiresInsertable = 0, - typename std::enable_if::value, int>::type = 0> - iterator insert(const_iterator, const T& value) { - return insert(value).first; - } - - iterator insert(const_iterator, init_type&& value) { - return insert(std::move(value)).first; - } - - template - void insert(InputIt first, InputIt last) { - for (; first != last; ++first) insert(*first); - } - - template = 0, RequiresInsertable = 0> - void insert(std::initializer_list ilist) { - insert(ilist.begin(), ilist.end()); - } - - void insert(std::initializer_list ilist) { - insert(ilist.begin(), ilist.end()); - } - - insert_return_type insert(node_type&& node) { - if (!node) return {end(), false, node_type()}; - const auto& elem = PolicyTraits::element(CommonAccess::GetSlot(node)); - auto res = PolicyTraits::apply( - InsertSlot{*this, std::move(*CommonAccess::GetSlot(node))}, - elem); - if (res.second) { - CommonAccess::Reset(&node); - return {res.first, true, node_type()}; - } else { - return {res.first, false, std::move(node)}; - } - } - - iterator insert(const_iterator, node_type&& node) { - auto res = insert(std::move(node)); - node = std::move(res.node); - return res.position; - } - - // This overload kicks in if we can deduce the key from args. This enables us - // to avoid constructing value_type if an entry with the same key already - // exists. - // - // For example: - // - // flat_hash_map m = {{"abc", "def"}}; - // // Creates no std::string copies and makes no heap allocations. - // m.emplace("abc", "xyz"); - template ::value, int>::type = 0> - std::pair emplace(Args&&... args) { - return PolicyTraits::apply(EmplaceDecomposable{*this}, - std::forward(args)...); - } - - // This overload kicks in if we cannot deduce the key from args. It constructs - // value_type unconditionally and then either moves it into the table or - // destroys. - template ::value, int>::type = 0> - std::pair emplace(Args&&... args) { - alignas(slot_type) unsigned char raw[sizeof(slot_type)]; - slot_type* slot = reinterpret_cast(&raw); - - PolicyTraits::construct(&alloc_ref(), slot, std::forward(args)...); - const auto& elem = PolicyTraits::element(slot); - return PolicyTraits::apply(InsertSlot{*this, std::move(*slot)}, elem); - } - - template - iterator emplace_hint(const_iterator, Args&&... args) { - return emplace(std::forward(args)...).first; - } - - // Extension API: support for lazy emplace. - // - // Looks up key in the table. If found, returns the iterator to the element. - // Otherwise calls `f` with one argument of type `raw_hash_set::constructor`. - // - // `f` must abide by several restrictions: - // - it MUST call `raw_hash_set::constructor` with arguments as if a - // `raw_hash_set::value_type` is constructed, - // - it MUST NOT access the container before the call to - // `raw_hash_set::constructor`, and - // - it MUST NOT erase the lazily emplaced element. - // Doing any of these is undefined behavior. - // - // For example: - // - // std::unordered_set s; - // // Makes ArenaStr even if "abc" is in the map. - // s.insert(ArenaString(&arena, "abc")); - // - // flat_hash_set s; - // // Makes ArenaStr only if "abc" is not in the map. - // s.lazy_emplace("abc", [&](const constructor& ctor) { - // ctor(&arena, "abc"); - // }); - // - // WARNING: This API is currently experimental. If there is a way to implement - // the same thing with the rest of the API, prefer that. - class constructor { - friend class raw_hash_set; - - public: - template - void operator()(Args&&... args) const { - assert(*slot_); - PolicyTraits::construct(alloc_, *slot_, std::forward(args)...); - *slot_ = nullptr; - } - - private: - constructor(allocator_type* a, slot_type** slot) : alloc_(a), slot_(slot) {} - - allocator_type* alloc_; - slot_type** slot_; - }; - - template - iterator lazy_emplace(const key_arg& key, F&& f) { - auto res = find_or_prepare_insert(key); - if (res.second) { - slot_type* slot = slots_ + res.first; - std::forward(f)(constructor(&alloc_ref(), &slot)); - assert(!slot); - } - return iterator_at(res.first); - } - - // Extension API: support for heterogeneous keys. - // - // std::unordered_set s; - // // Turns "abc" into std::string. - // s.erase("abc"); - // - // flat_hash_set s; - // // Uses "abc" directly without copying it into std::string. - // s.erase("abc"); - template - size_type erase(const key_arg& key) { - auto it = find(key); - if (it == end()) return 0; - erase(it); - return 1; - } - - // Erases the element pointed to by `it`. Unlike `std::unordered_set::erase`, - // this method returns void to reduce algorithmic complexity to O(1). The - // iterator is invalidated, so any increment should be done before calling - // erase. In order to erase while iterating across a map, use the following - // idiom (which also works for standard containers): - // - // for (auto it = m.begin(), end = m.end(); it != end;) { - // // `erase()` will invalidate `it`, so advance `it` first. - // auto copy_it = it++; - // if () { - // m.erase(copy_it); - // } - // } - void erase(const_iterator cit) { erase(cit.inner_); } - - // This overload is necessary because otherwise erase(const K&) would be - // a better match if non-const iterator is passed as an argument. - void erase(iterator it) { - AssertIsFull(it.ctrl_); - PolicyTraits::destroy(&alloc_ref(), it.slot_); - erase_meta_only(it); - } - - iterator erase(const_iterator first, const_iterator last) { - while (first != last) { - erase(first++); - } - return last.inner_; - } - - // Moves elements from `src` into `this`. - // If the element already exists in `this`, it is left unmodified in `src`. - template - void merge(raw_hash_set& src) { // NOLINT - assert(this != &src); - for (auto it = src.begin(), e = src.end(); it != e;) { - auto next = std::next(it); - if (PolicyTraits::apply(InsertSlot{*this, std::move(*it.slot_)}, - PolicyTraits::element(it.slot_)) - .second) { - src.erase_meta_only(it); - } - it = next; - } - } - - template - void merge(raw_hash_set&& src) { - merge(src); - } - - node_type extract(const_iterator position) { - AssertIsFull(position.inner_.ctrl_); - auto node = - CommonAccess::Transfer(alloc_ref(), position.inner_.slot_); - erase_meta_only(position); - return node; - } - - template < - class K = key_type, - typename std::enable_if::value, int>::type = 0> - node_type extract(const key_arg& key) { - auto it = find(key); - return it == end() ? node_type() : extract(const_iterator{it}); - } - - void swap(raw_hash_set& that) noexcept( - IsNoThrowSwappable() && IsNoThrowSwappable() && - IsNoThrowSwappable( - typename AllocTraits::propagate_on_container_swap{})) { - using std::swap; - swap(ctrl_, that.ctrl_); - swap(slots_, that.slots_); - swap(size_, that.size_); - swap(capacity_, that.capacity_); - swap(growth_left(), that.growth_left()); - swap(hash_ref(), that.hash_ref()); - swap(eq_ref(), that.eq_ref()); - swap(infoz_, that.infoz_); - SwapAlloc(alloc_ref(), that.alloc_ref(), - typename AllocTraits::propagate_on_container_swap{}); - } - - void rehash(size_t n) { - if (n == 0 && capacity_ == 0) return; - if (n == 0 && size_ == 0) { - destroy_slots(); - infoz_.RecordStorageChanged(0, 0); - return; - } - // bitor is a faster way of doing `max` here. We will round up to the next - // power-of-2-minus-1, so bitor is good enough. - auto m = NormalizeCapacity(n | GrowthToLowerboundCapacity(size())); - // n == 0 unconditionally rehashes as per the standard. - if (n == 0 || m > capacity_) { - resize(m); - } - } - - void reserve(size_t n) { - size_t m = GrowthToLowerboundCapacity(n); - if (m > capacity_) { - resize(NormalizeCapacity(m)); - } - } - - // Extension API: support for heterogeneous keys. - // - // std::unordered_set s; - // // Turns "abc" into std::string. - // s.count("abc"); - // - // ch_set s; - // // Uses "abc" directly without copying it into std::string. - // s.count("abc"); - template - size_t count(const key_arg& key) const { - return find(key) == end() ? 0 : 1; - } - - // Issues CPU prefetch instructions for the memory needed to find or insert - // a key. Like all lookup functions, this support heterogeneous keys. - // - // NOTE: This is a very low level operation and should not be used without - // specific benchmarks indicating its importance. - template - void prefetch(const key_arg& key) const { - (void)key; -#if defined(__GNUC__) - auto seq = probe(ctrl_, hash_ref()(key), capacity_); - __builtin_prefetch(static_cast(ctrl_ + seq.offset())); - __builtin_prefetch(static_cast(slots_ + seq.offset())); -#endif // __GNUC__ - } - - // The API of find() has two extensions. - // - // 1. The hash can be passed by the user. It must be equal to the hash of the - // key. - // - // 2. The type of the key argument doesn't have to be key_type. This is so - // called heterogeneous key support. - template - iterator find(const key_arg& key, size_t hash) { - auto seq = probe(ctrl_, hash, capacity_); - while (true) { - Group g{ctrl_ + seq.offset()}; - for (int i : g.Match(H2(hash))) { - if (ABSL_PREDICT_TRUE(PolicyTraits::apply( - EqualElement{key, eq_ref()}, - PolicyTraits::element(slots_ + seq.offset(i))))) - return iterator_at(seq.offset(i)); - } - if (ABSL_PREDICT_TRUE(g.MatchEmpty())) return end(); - seq.next(); - assert(seq.index() < capacity_ && "full table!"); - } - } - template - iterator find(const key_arg& key) { - return find(key, hash_ref()(key)); - } - - template - const_iterator find(const key_arg& key, size_t hash) const { - return const_cast(this)->find(key, hash); - } - template - const_iterator find(const key_arg& key) const { - return find(key, hash_ref()(key)); - } - - template - bool contains(const key_arg& key) const { - return find(key) != end(); - } - - template - std::pair equal_range(const key_arg& key) { - auto it = find(key); - if (it != end()) return {it, std::next(it)}; - return {it, it}; - } - template - std::pair equal_range( - const key_arg& key) const { - auto it = find(key); - if (it != end()) return {it, std::next(it)}; - return {it, it}; - } - - size_t bucket_count() const { return capacity_; } - float load_factor() const { - return capacity_ ? static_cast(size()) / capacity_ : 0.0; - } - float max_load_factor() const { return 1.0f; } - void max_load_factor(float) { - // Does nothing. - } - - hasher hash_function() const { return hash_ref(); } - key_equal key_eq() const { return eq_ref(); } - allocator_type get_allocator() const { return alloc_ref(); } - - friend bool operator==(const raw_hash_set& a, const raw_hash_set& b) { - if (a.size() != b.size()) return false; - const raw_hash_set* outer = &a; - const raw_hash_set* inner = &b; - if (outer->capacity() > inner->capacity()) std::swap(outer, inner); - for (const value_type& elem : *outer) - if (!inner->has_element(elem)) return false; - return true; - } - - friend bool operator!=(const raw_hash_set& a, const raw_hash_set& b) { - return !(a == b); - } - - friend void swap(raw_hash_set& a, - raw_hash_set& b) noexcept(noexcept(a.swap(b))) { - a.swap(b); - } - - private: - template - friend struct absl::container_internal::hashtable_debug_internal:: - HashtableDebugAccess; - - struct FindElement { - template - const_iterator operator()(const K& key, Args&&...) const { - return s.find(key); - } - const raw_hash_set& s; - }; - - struct HashElement { - template - size_t operator()(const K& key, Args&&...) const { - return h(key); - } - const hasher& h; - }; - - template - struct EqualElement { - template - bool operator()(const K2& lhs, Args&&...) const { - return eq(lhs, rhs); - } - const K1& rhs; - const key_equal& eq; - }; - - struct EmplaceDecomposable { - template - std::pair operator()(const K& key, Args&&... args) const { - auto res = s.find_or_prepare_insert(key); - if (res.second) { - s.emplace_at(res.first, std::forward(args)...); - } - return {s.iterator_at(res.first), res.second}; - } - raw_hash_set& s; - }; - - template - struct InsertSlot { - template - std::pair operator()(const K& key, Args&&...) && { - auto res = s.find_or_prepare_insert(key); - if (res.second) { - PolicyTraits::transfer(&s.alloc_ref(), s.slots_ + res.first, &slot); - } else if (do_destroy) { - PolicyTraits::destroy(&s.alloc_ref(), &slot); - } - return {s.iterator_at(res.first), res.second}; - } - raw_hash_set& s; - // Constructed slot. Either moved into place or destroyed. - slot_type&& slot; - }; - - // "erases" the object from the container, except that it doesn't actually - // destroy the object. It only updates all the metadata of the class. - // This can be used in conjunction with Policy::transfer to move the object to - // another place. - void erase_meta_only(const_iterator it) { - assert(IsFull(*it.inner_.ctrl_) && "erasing a dangling iterator"); - --size_; - const size_t index = it.inner_.ctrl_ - ctrl_; - const size_t index_before = (index - Group::kWidth) & capacity_; - const auto empty_after = Group(it.inner_.ctrl_).MatchEmpty(); - const auto empty_before = Group(ctrl_ + index_before).MatchEmpty(); - - // 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. - bool was_never_full = - empty_before && empty_after && - static_cast(empty_after.TrailingZeros() + - empty_before.LeadingZeros()) < Group::kWidth; - - set_ctrl(index, was_never_full ? kEmpty : kDeleted); - growth_left() += was_never_full; - infoz_.RecordErase(); - } - - void initialize_slots() { - assert(capacity_); - // Folks with custom allocators often make unwarranted assumptions about the - // behavior of their classes vis-a-vis trivial destructability and what - // calls they will or wont make. Avoid sampling for people with custom - // allocators to get us out of this mess. This is not a hard guarantee but - // a workaround while we plan the exact guarantee we want to provide. - // - // People are often sloppy with the exact type of their allocator (sometimes - // it has an extra const or is missing the pair, but rebinds made it work - // anyway). To avoid the ambiguity, we work off SlotAlloc which we have - // bound more carefully. - if (std::is_same>::value && - slots_ == nullptr) { - infoz_ = Sample(); - } - - auto layout = MakeLayout(capacity_); - char* mem = static_cast( - Allocate(&alloc_ref(), layout.AllocSize())); - ctrl_ = reinterpret_cast(layout.template Pointer<0>(mem)); - slots_ = layout.template Pointer<1>(mem); - reset_ctrl(); - reset_growth_left(); - infoz_.RecordStorageChanged(size_, capacity_); - } - - void destroy_slots() { - if (!capacity_) return; - for (size_t i = 0; i != capacity_; ++i) { - if (IsFull(ctrl_[i])) { - PolicyTraits::destroy(&alloc_ref(), slots_ + i); - } - } - auto layout = MakeLayout(capacity_); - // Unpoison before returning the memory to the allocator. - SanitizerUnpoisonMemoryRegion(slots_, sizeof(slot_type) * capacity_); - Deallocate(&alloc_ref(), ctrl_, layout.AllocSize()); - ctrl_ = EmptyGroup(); - slots_ = nullptr; - size_ = 0; - capacity_ = 0; - growth_left() = 0; - } - - void resize(size_t new_capacity) { - assert(IsValidCapacity(new_capacity)); - auto* old_ctrl = ctrl_; - auto* old_slots = slots_; - const size_t old_capacity = capacity_; - capacity_ = new_capacity; - initialize_slots(); - - size_t total_probe_length = 0; - for (size_t i = 0; i != old_capacity; ++i) { - if (IsFull(old_ctrl[i])) { - size_t hash = PolicyTraits::apply(HashElement{hash_ref()}, - PolicyTraits::element(old_slots + i)); - auto target = find_first_non_full(ctrl_, hash, capacity_); - size_t new_i = target.offset; - total_probe_length += target.probe_length; - set_ctrl(new_i, H2(hash)); - PolicyTraits::transfer(&alloc_ref(), slots_ + new_i, old_slots + i); - } - } - if (old_capacity) { - SanitizerUnpoisonMemoryRegion(old_slots, - sizeof(slot_type) * old_capacity); - auto layout = MakeLayout(old_capacity); - Deallocate(&alloc_ref(), old_ctrl, - layout.AllocSize()); - } - infoz_.RecordRehash(total_probe_length); - } - - void drop_deletes_without_resize() ABSL_ATTRIBUTE_NOINLINE { - 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) - ConvertDeletedToEmptyAndFullToDeleted(ctrl_, capacity_); - alignas(slot_type) unsigned char raw[sizeof(slot_type)]; - size_t total_probe_length = 0; - slot_type* slot = reinterpret_cast(&raw); - for (size_t i = 0; i != capacity_; ++i) { - if (!IsDeleted(ctrl_[i])) continue; - size_t hash = PolicyTraits::apply(HashElement{hash_ref()}, - PolicyTraits::element(slots_ + i)); - auto target = find_first_non_full(ctrl_, hash, capacity_); - 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 auto probe_index = [&](size_t pos) { - return ((pos - probe(ctrl_, hash, capacity_).offset()) & capacity_) / - Group::kWidth; - }; - - // Element doesn't move. - if (ABSL_PREDICT_TRUE(probe_index(new_i) == probe_index(i))) { - set_ctrl(i, H2(hash)); - continue; - } - if (IsEmpty(ctrl_[new_i])) { - // Transfer element to the empty spot. - // set_ctrl poisons/unpoisons the slots so we have to call it at the - // right time. - set_ctrl(new_i, H2(hash)); - PolicyTraits::transfer(&alloc_ref(), slots_ + new_i, slots_ + i); - set_ctrl(i, kEmpty); - } else { - assert(IsDeleted(ctrl_[new_i])); - set_ctrl(new_i, H2(hash)); - // Until we are done rehashing, DELETED marks previously FULL slots. - // Swap i and new_i elements. - PolicyTraits::transfer(&alloc_ref(), slot, slots_ + i); - PolicyTraits::transfer(&alloc_ref(), slots_ + i, slots_ + new_i); - PolicyTraits::transfer(&alloc_ref(), slots_ + new_i, slot); - --i; // repeat - } - } - reset_growth_left(); - infoz_.RecordRehash(total_probe_length); - } - - void rehash_and_grow_if_necessary() { - if (capacity_ == 0) { - resize(1); - } else if (size() <= CapacityToGrowth(capacity()) / 2) { - // Squash DELETED without growing if there is enough capacity. - drop_deletes_without_resize(); - } else { - // Otherwise grow the container. - resize(capacity_ * 2 + 1); - } - } - - bool has_element(const value_type& elem) const { - size_t hash = PolicyTraits::apply(HashElement{hash_ref()}, elem); - auto seq = probe(ctrl_, hash, capacity_); - while (true) { - Group g{ctrl_ + seq.offset()}; - for (int i : g.Match(H2(hash))) { - if (ABSL_PREDICT_TRUE(PolicyTraits::element(slots_ + seq.offset(i)) == - elem)) - return true; - } - if (ABSL_PREDICT_TRUE(g.MatchEmpty())) return false; - seq.next(); - assert(seq.index() < capacity_ && "full table!"); - } - return false; - } - - // TODO(alkis): Optimize this assuming *this and that don't overlap. - raw_hash_set& move_assign(raw_hash_set&& that, std::true_type) { - raw_hash_set tmp(std::move(that)); - swap(tmp); - return *this; - } - raw_hash_set& move_assign(raw_hash_set&& that, std::false_type) { - raw_hash_set tmp(std::move(that), alloc_ref()); - swap(tmp); - return *this; - } - - protected: - template - std::pair find_or_prepare_insert(const K& key) { - auto hash = hash_ref()(key); - auto seq = probe(ctrl_, hash, capacity_); - while (true) { - Group g{ctrl_ + seq.offset()}; - for (int i : g.Match(H2(hash))) { - if (ABSL_PREDICT_TRUE(PolicyTraits::apply( - EqualElement{key, eq_ref()}, - PolicyTraits::element(slots_ + seq.offset(i))))) - return {seq.offset(i), false}; - } - if (ABSL_PREDICT_TRUE(g.MatchEmpty())) break; - seq.next(); - assert(seq.index() < capacity_ && "full table!"); - } - return {prepare_insert(hash), true}; - } - - size_t prepare_insert(size_t hash) ABSL_ATTRIBUTE_NOINLINE { - auto target = find_first_non_full(ctrl_, hash, capacity_); - if (ABSL_PREDICT_FALSE(growth_left() == 0 && - !IsDeleted(ctrl_[target.offset]))) { - rehash_and_grow_if_necessary(); - target = find_first_non_full(ctrl_, hash, capacity_); - } - ++size_; - growth_left() -= IsEmpty(ctrl_[target.offset]); - set_ctrl(target.offset, H2(hash)); - infoz_.RecordInsert(hash, target.probe_length); - return target.offset; - } - - // Constructs the value in the space pointed by the iterator. This only works - // after an unsuccessful find_or_prepare_insert() and before any other - // modifications happen in the raw_hash_set. - // - // PRECONDITION: i is an index returned from find_or_prepare_insert(k), where - // k is the key decomposed from `forward(args)...`, and the bool - // returned by find_or_prepare_insert(k) was true. - // POSTCONDITION: *m.iterator_at(i) == value_type(forward(args)...). - template - void emplace_at(size_t i, Args&&... args) { - PolicyTraits::construct(&alloc_ref(), slots_ + i, - std::forward(args)...); - - assert(PolicyTraits::apply(FindElement{*this}, *iterator_at(i)) == - iterator_at(i) && - "constructed value does not match the lookup key"); - } - - iterator iterator_at(size_t i) { return {ctrl_ + i, slots_ + i}; } - const_iterator iterator_at(size_t i) const { return {ctrl_ + i, slots_ + i}; } - - private: - friend struct RawHashSetTestOnlyAccess; - - // Reset all ctrl bytes back to kEmpty, except the sentinel. - void reset_ctrl() { - std::memset(ctrl_, kEmpty, capacity_ + Group::kWidth); - ctrl_[capacity_] = kSentinel; - SanitizerPoisonMemoryRegion(slots_, sizeof(slot_type) * capacity_); - } - - void reset_growth_left() { - growth_left() = CapacityToGrowth(capacity()) - size_; - } - - // Sets the control byte, and if `i < Group::kWidth`, set the cloned byte at - // the end too. - void set_ctrl(size_t i, ctrl_t h) { - assert(i < capacity_); - - if (IsFull(h)) { - SanitizerUnpoisonObject(slots_ + i); - } else { - SanitizerPoisonObject(slots_ + i); - } - - ctrl_[i] = h; - ctrl_[((i - Group::kWidth) & capacity_) + 1 + - ((Group::kWidth - 1) & capacity_)] = h; - } - - size_t& growth_left() { return settings_.template get<0>(); } - - hasher& hash_ref() { return settings_.template get<1>(); } - const hasher& hash_ref() const { return settings_.template get<1>(); } - key_equal& eq_ref() { return settings_.template get<2>(); } - const key_equal& eq_ref() const { return settings_.template get<2>(); } - allocator_type& alloc_ref() { return settings_.template get<3>(); } - const allocator_type& alloc_ref() const { - return settings_.template get<3>(); - } - - // TODO(alkis): Investigate removing some of these fields: - // - ctrl/slots can be derived from each other - // - size can be moved into the slot array - ctrl_t* ctrl_ = EmptyGroup(); // [(capacity + 1) * ctrl_t] - slot_type* slots_ = nullptr; // [capacity * slot_type] - size_t size_ = 0; // number of full slots - size_t capacity_ = 0; // total number of slots - HashtablezInfoHandle infoz_; - absl::container_internal::CompressedTuple - settings_{0, hasher{}, key_equal{}, allocator_type{}}; -}; - -// Erases all elements that satisfy the predicate `pred` from the container `c`. -template -void EraseIf(Predicate pred, raw_hash_set* c) { - for (auto it = c->begin(), last = c->end(); it != last;) { - auto copy_it = it++; - if (pred(*copy_it)) { - c->erase(copy_it); - } - } -} - -namespace hashtable_debug_internal { -template -struct HashtableDebugAccess> { - using Traits = typename Set::PolicyTraits; - using Slot = typename Traits::slot_type; - - static size_t GetNumProbes(const Set& set, - const typename Set::key_type& key) { - size_t num_probes = 0; - size_t hash = set.hash_ref()(key); - auto seq = probe(set.ctrl_, hash, set.capacity_); - while (true) { - container_internal::Group g{set.ctrl_ + seq.offset()}; - for (int i : g.Match(container_internal::H2(hash))) { - if (Traits::apply( - typename Set::template EqualElement{ - key, set.eq_ref()}, - Traits::element(set.slots_ + seq.offset(i)))) - return num_probes; - ++num_probes; - } - if (g.MatchEmpty()) return num_probes; - seq.next(); - ++num_probes; - } - } - - static size_t AllocatedByteSize(const Set& c) { - size_t capacity = c.capacity_; - if (capacity == 0) return 0; - auto layout = Set::MakeLayout(capacity); - size_t m = layout.AllocSize(); - - size_t per_slot = Traits::space_used(static_cast(nullptr)); - if (per_slot != ~size_t{}) { - m += per_slot * c.size(); - } else { - for (size_t i = 0; i != capacity; ++i) { - if (container_internal::IsFull(c.ctrl_[i])) { - m += Traits::space_used(c.slots_ + i); - } - } - } - return m; - } - - static size_t LowerBoundAllocatedByteSize(size_t size) { - size_t capacity = GrowthToLowerboundCapacity(size); - if (capacity == 0) return 0; - auto layout = Set::MakeLayout(NormalizeCapacity(capacity)); - size_t m = layout.AllocSize(); - size_t per_slot = Traits::space_used(static_cast(nullptr)); - if (per_slot != ~size_t{}) { - m += per_slot * size; - } - return m; - } -}; - -} // namespace hashtable_debug_internal -} // namespace container_internal -ABSL_NAMESPACE_END -} // namespace absl - -#endif // ABSL_CONTAINER_INTERNAL_RAW_HASH_SET_H_ diff --git a/third_party/abseil_cpp/absl/container/internal/raw_hash_set_allocator_test.cc b/third_party/abseil_cpp/absl/container/internal/raw_hash_set_allocator_test.cc deleted file mode 100644 index e73f53fd637b..000000000000 --- a/third_party/abseil_cpp/absl/container/internal/raw_hash_set_allocator_test.cc +++ /dev/null @@ -1,505 +0,0 @@ -// 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 -#include - -#include "gtest/gtest.h" -#include "absl/container/internal/raw_hash_set.h" -#include "absl/container/internal/tracked.h" - -namespace absl { -ABSL_NAMESPACE_BEGIN -namespace container_internal { -namespace { - -enum AllocSpec { - kPropagateOnCopy = 1, - kPropagateOnMove = 2, - kPropagateOnSwap = 4, -}; - -struct AllocState { - size_t num_allocs = 0; - std::set owned; -}; - -template -class CheckedAlloc { - public: - template - 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 - CheckedAlloc(const CheckedAlloc& that) - : id_(that.id_), state_(that.state_) {} - - template - struct rebind { - using other = CheckedAlloc; - }; - - using propagate_on_container_copy_assignment = - std::integral_constant; - - using propagate_on_container_move_assignment = - std::integral_constant; - - using propagate_on_container_swap = - std::integral_constant; - - CheckedAlloc select_on_container_copy_construction() const { - if (Spec & kPropagateOnCopy) return *this; - return {}; - } - - T* allocate(size_t n) { - T* ptr = std::allocator().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().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::max(); - - std::shared_ptr state_ = std::make_shared(); -}; - -struct Identity { - int32_t operator()(int32_t v) const { return v; } -}; - -struct Policy { - using slot_type = Tracked; - using init_type = Tracked; - using key_type = int32_t; - - template - static void construct(allocator_type* alloc, slot_type* slot, - Args&&... args) { - std::allocator_traits::construct( - *alloc, slot, std::forward(args)...); - } - - template - static void destroy(allocator_type* alloc, slot_type* slot) { - std::allocator_traits::destroy(*alloc, slot); - } - - template - 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 - static auto apply(F&& f, int32_t v) -> decltype(std::forward(f)(v, v)) { - return std::forward(f)(v, v); - } - - template - static auto apply(F&& f, const slot_type& v) - -> decltype(std::forward(f)(v.val(), v)) { - return std::forward(f)(v.val(), v); - } - - template - static auto apply(F&& f, slot_type&& v) - -> decltype(std::forward(f)(v.val(), std::move(v))) { - return std::forward(f)(v.val(), std::move(v)); - } - - static slot_type& element(slot_type* slot) { return *slot; } -}; - -template -struct PropagateTest : public ::testing::Test { - using Alloc = CheckedAlloc, Spec>; - - using Table = raw_hash_set, 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; -using NoPropagateOnCopy = PropagateTest; -using NoPropagateOnMove = PropagateTest; - -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) { - auto it = t1.insert(0).first; - Table u(std::move(t1)); - EXPECT_EQ(1, a1.num_allocs()); - EXPECT_EQ(0, it->num_moves()); - EXPECT_EQ(0, it->num_copies()); -} - -TEST_F(NoPropagateOnMove, MoveConstructor) { - auto it = t1.insert(0).first; - Table u(std::move(t1)); - EXPECT_EQ(1, a1.num_allocs()); - EXPECT_EQ(0, it->num_moves()); - EXPECT_EQ(0, it->num_copies()); -} - -TEST_F(PropagateOnAll, MoveConstructorWithSameAlloc) { - auto it = t1.insert(0).first; - Table u(std::move(t1), a1); - EXPECT_EQ(1, a1.num_allocs()); - EXPECT_EQ(0, it->num_moves()); - EXPECT_EQ(0, it->num_copies()); -} - -TEST_F(NoPropagateOnMove, MoveConstructorWithSameAlloc) { - auto it = t1.insert(0).first; - Table u(std::move(t1), a1); - EXPECT_EQ(1, a1.num_allocs()); - EXPECT_EQ(0, it->num_moves()); - 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_EQ(1, a2.num_allocs()); - EXPECT_EQ(1, it->num_moves()); - 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_EQ(1, a2.num_allocs()); - EXPECT_EQ(1, it->num_moves()); - EXPECT_EQ(0, it->num_copies()); -} - -TEST_F(PropagateOnAll, CopyAssignmentWithSameAlloc) { - auto it = t1.insert(0).first; - Table u(0, a1); - u = t1; - EXPECT_EQ(2, a1.num_allocs()); - EXPECT_EQ(0, it->num_moves()); - EXPECT_EQ(1, it->num_copies()); -} - -TEST_F(NoPropagateOnCopy, CopyAssignmentWithSameAlloc) { - auto it = t1.insert(0).first; - Table u(0, a1); - u = t1; - EXPECT_EQ(2, a1.num_allocs()); - EXPECT_EQ(0, it->num_moves()); - 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_EQ(2, a1.num_allocs()); - EXPECT_EQ(0, a2.num_allocs()); - EXPECT_EQ(0, it->num_moves()); - 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_EQ(1, a2.num_allocs()); - EXPECT_EQ(0, it->num_moves()); - EXPECT_EQ(1, it->num_copies()); -} - -TEST_F(PropagateOnAll, MoveAssignmentWithSameAlloc) { - auto it = t1.insert(0).first; - Table u(0, a1); - u = std::move(t1); - EXPECT_EQ(a1, u.get_allocator()); - EXPECT_EQ(1, a1.num_allocs()); - EXPECT_EQ(0, it->num_moves()); - EXPECT_EQ(0, it->num_copies()); -} - -TEST_F(NoPropagateOnMove, MoveAssignmentWithSameAlloc) { - auto it = t1.insert(0).first; - Table u(0, a1); - u = std::move(t1); - EXPECT_EQ(a1, u.get_allocator()); - EXPECT_EQ(1, a1.num_allocs()); - EXPECT_EQ(0, it->num_moves()); - EXPECT_EQ(0, it->num_copies()); -} - -TEST_F(PropagateOnAll, MoveAssignmentWithDifferentAlloc) { - auto it = t1.insert(0).first; - Table u(0, a2); - u = std::move(t1); - EXPECT_EQ(a1, u.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()); -} - -TEST_F(NoPropagateOnMove, MoveAssignmentWithDifferentAlloc) { - auto it = t1.insert(0).first; - Table u(0, a2); - u = std::move(t1); - it = u.find(0); - EXPECT_EQ(a2, u.get_allocator()); - EXPECT_EQ(1, a1.num_allocs()); - EXPECT_EQ(1, a2.num_allocs()); - EXPECT_EQ(1, it->num_moves()); - 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 PAlloc { - template - friend class PAlloc; - - public: - // types - using value_type = T; - - // traits - using propagate_on_container_swap = std::false_type; - - PAlloc() noexcept = default; - explicit PAlloc(size_t id) noexcept : id_(id) {} - PAlloc(const PAlloc&) noexcept = default; - PAlloc& operator=(const PAlloc&) noexcept = delete; - - template - PAlloc(const PAlloc& that) noexcept : id_(that.id_) {} // NOLINT - - template - struct rebind { - using other = PAlloc; - }; - - 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::max(); -}; - -// This doesn't compile with GCC 5.4 and 5.5 due to a bug in noexcept handing. -#if !defined(__GNUC__) || __GNUC__ != 5 || (__GNUC_MINOR__ != 4 && \ - __GNUC_MINOR__ != 5) -TEST(NoPropagateOn, Swap) { - using PA = PAlloc; - using Table = raw_hash_set, PA>; - - Table t1(PA{1}), t2(PA{2}); - swap(t1, t2); - EXPECT_EQ(t1.get_allocator(), PA(1)); - EXPECT_EQ(t2.get_allocator(), PA(2)); -} -#endif - -TEST(NoPropagateOn, CopyConstruct) { - using PA = PAlloc; - using Table = raw_hash_set, PA>; - - Table t1(PA{1}), t2(t1); - EXPECT_EQ(t1.get_allocator(), PA(1)); - EXPECT_EQ(t2.get_allocator(), PA()); -} - -TEST(NoPropagateOn, Assignment) { - using PA = PAlloc; - using Table = raw_hash_set, PA>; - - Table t1(PA{1}), t2(PA{2}); - t1 = t2; - EXPECT_EQ(t1.get_allocator(), PA(1)); - EXPECT_EQ(t2.get_allocator(), PA(2)); -} - -} // namespace -} // namespace container_internal -ABSL_NAMESPACE_END -} // namespace absl diff --git a/third_party/abseil_cpp/absl/container/internal/raw_hash_set_test.cc b/third_party/abseil_cpp/absl/container/internal/raw_hash_set_test.cc deleted file mode 100644 index 33d2773de302..000000000000 --- a/third_party/abseil_cpp/absl/container/internal/raw_hash_set_test.cc +++ /dev/null @@ -1,1893 +0,0 @@ -// 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 -#include -#include -#include -#include -#include -#include -#include - -#include "gmock/gmock.h" -#include "gtest/gtest.h" -#include "absl/base/attributes.h" -#include "absl/base/config.h" -#include "absl/base/internal/cycleclock.h" -#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/hash_policy_testing.h" -#include "absl/container/internal/hashtable_debug.h" -#include "absl/strings/string_view.h" - -namespace absl { -ABSL_NAMESPACE_BEGIN -namespace container_internal { - -struct RawHashSetTestOnlyAccess { - template - static auto GetSlots(const C& c) -> decltype(c.slots_) { - return c.slots_; - } -}; - -namespace { - -using ::testing::DoubleNear; -using ::testing::ElementsAre; -using ::testing::Ge; -using ::testing::Lt; -using ::testing::Optional; -using ::testing::Pair; -using ::testing::UnorderedElementsAre; - -TEST(Util, NormalizeCapacity) { - EXPECT_EQ(1, NormalizeCapacity(0)); - EXPECT_EQ(1, NormalizeCapacity(1)); - EXPECT_EQ(3, NormalizeCapacity(2)); - EXPECT_EQ(3, NormalizeCapacity(3)); - EXPECT_EQ(7, NormalizeCapacity(4)); - EXPECT_EQ(7, NormalizeCapacity(7)); - EXPECT_EQ(15, NormalizeCapacity(8)); - EXPECT_EQ(15, NormalizeCapacity(15)); - EXPECT_EQ(15 * 2 + 1, NormalizeCapacity(15 + 1)); - EXPECT_EQ(15 * 2 + 1, NormalizeCapacity(15 + 2)); -} - -TEST(Util, GrowthAndCapacity) { - // Verify that GrowthToCapacity gives the minimum capacity that has enough - // growth. - for (size_t growth = 0; growth < 10000; ++growth) { - SCOPED_TRACE(growth); - size_t capacity = NormalizeCapacity(GrowthToLowerboundCapacity(growth)); - // The capacity is large enough for `growth` - EXPECT_THAT(CapacityToGrowth(capacity), Ge(growth)); - if (growth != 0 && capacity > 1) { - // There is no smaller capacity that works. - EXPECT_THAT(CapacityToGrowth(capacity / 2), Lt(growth)); - } - } - - for (size_t capacity = Group::kWidth - 1; capacity < 10000; - capacity = 2 * capacity + 1) { - SCOPED_TRACE(capacity); - size_t growth = CapacityToGrowth(capacity); - EXPECT_THAT(growth, Lt(capacity)); - EXPECT_LE(GrowthToLowerboundCapacity(growth), capacity); - EXPECT_EQ(NormalizeCapacity(GrowthToLowerboundCapacity(growth)), capacity); - } -} - -TEST(Util, probe_seq) { - probe_seq<16> seq(0, 127); - auto gen = [&]() { - size_t res = seq.offset(); - seq.next(); - return res; - }; - std::vector offsets(8); - std::generate_n(offsets.begin(), 8, gen); - EXPECT_THAT(offsets, ElementsAre(0, 16, 48, 96, 32, 112, 80, 64)); - seq = probe_seq<16>(128, 127); - std::generate_n(offsets.begin(), 8, gen); - EXPECT_THAT(offsets, ElementsAre(0, 16, 48, 96, 32, 112, 80, 64)); -} - -TEST(BitMask, Smoke) { - EXPECT_FALSE((BitMask(0))); - EXPECT_TRUE((BitMask(5))); - - EXPECT_THAT((BitMask(0)), ElementsAre()); - EXPECT_THAT((BitMask(0x1)), ElementsAre(0)); - EXPECT_THAT((BitMask(0x2)), ElementsAre(1)); - EXPECT_THAT((BitMask(0x3)), ElementsAre(0, 1)); - EXPECT_THAT((BitMask(0x4)), ElementsAre(2)); - EXPECT_THAT((BitMask(0x5)), ElementsAre(0, 2)); - EXPECT_THAT((BitMask(0x55)), ElementsAre(0, 2, 4, 6)); - EXPECT_THAT((BitMask(0xAA)), ElementsAre(1, 3, 5, 7)); -} - -TEST(BitMask, WithShift) { - // See the non-SSE version of Group for details on what this math is for. - uint64_t ctrl = 0x1716151413121110; - uint64_t hash = 0x12; - constexpr uint64_t msbs = 0x8080808080808080ULL; - constexpr uint64_t lsbs = 0x0101010101010101ULL; - auto x = ctrl ^ (lsbs * hash); - uint64_t mask = (x - lsbs) & ~x & msbs; - EXPECT_EQ(0x0000000080800000, mask); - - BitMask b(mask); - EXPECT_EQ(*b, 2); -} - -TEST(BitMask, LeadingTrailing) { - EXPECT_EQ((BitMask(0x00001a40).LeadingZeros()), 3); - EXPECT_EQ((BitMask(0x00001a40).TrailingZeros()), 6); - - EXPECT_EQ((BitMask(0x00000001).LeadingZeros()), 15); - EXPECT_EQ((BitMask(0x00000001).TrailingZeros()), 0); - - EXPECT_EQ((BitMask(0x00008000).LeadingZeros()), 0); - EXPECT_EQ((BitMask(0x00008000).TrailingZeros()), 15); - - EXPECT_EQ((BitMask(0x0000008080808000).LeadingZeros()), 3); - EXPECT_EQ((BitMask(0x0000008080808000).TrailingZeros()), 1); - - EXPECT_EQ((BitMask(0x0000000000000080).LeadingZeros()), 7); - EXPECT_EQ((BitMask(0x0000000000000080).TrailingZeros()), 0); - - EXPECT_EQ((BitMask(0x8000000000000000).LeadingZeros()), 0); - EXPECT_EQ((BitMask(0x8000000000000000).TrailingZeros()), 7); -} - -TEST(Group, EmptyGroup) { - for (h2_t h = 0; h != 128; ++h) EXPECT_FALSE(Group{EmptyGroup()}.Match(h)); -} - -TEST(Group, Match) { - if (Group::kWidth == 16) { - ctrl_t group[] = {kEmpty, 1, kDeleted, 3, kEmpty, 5, kSentinel, 7, - 7, 5, 3, 1, 1, 1, 1, 1}; - EXPECT_THAT(Group{group}.Match(0), ElementsAre()); - EXPECT_THAT(Group{group}.Match(1), ElementsAre(1, 11, 12, 13, 14, 15)); - EXPECT_THAT(Group{group}.Match(3), ElementsAre(3, 10)); - EXPECT_THAT(Group{group}.Match(5), ElementsAre(5, 9)); - EXPECT_THAT(Group{group}.Match(7), ElementsAre(7, 8)); - } else if (Group::kWidth == 8) { - ctrl_t group[] = {kEmpty, 1, 2, kDeleted, 2, 1, kSentinel, 1}; - EXPECT_THAT(Group{group}.Match(0), ElementsAre()); - EXPECT_THAT(Group{group}.Match(1), ElementsAre(1, 5, 7)); - EXPECT_THAT(Group{group}.Match(2), ElementsAre(2, 4)); - } else { - FAIL() << "No test coverage for Group::kWidth==" << Group::kWidth; - } -} - -TEST(Group, MatchEmpty) { - if (Group::kWidth == 16) { - ctrl_t group[] = {kEmpty, 1, kDeleted, 3, kEmpty, 5, kSentinel, 7, - 7, 5, 3, 1, 1, 1, 1, 1}; - EXPECT_THAT(Group{group}.MatchEmpty(), ElementsAre(0, 4)); - } else if (Group::kWidth == 8) { - ctrl_t group[] = {kEmpty, 1, 2, kDeleted, 2, 1, kSentinel, 1}; - EXPECT_THAT(Group{group}.MatchEmpty(), ElementsAre(0)); - } else { - FAIL() << "No test coverage for Group::kWidth==" << Group::kWidth; - } -} - -TEST(Group, MatchEmptyOrDeleted) { - if (Group::kWidth == 16) { - ctrl_t group[] = {kEmpty, 1, kDeleted, 3, kEmpty, 5, kSentinel, 7, - 7, 5, 3, 1, 1, 1, 1, 1}; - EXPECT_THAT(Group{group}.MatchEmptyOrDeleted(), ElementsAre(0, 2, 4)); - } else if (Group::kWidth == 8) { - ctrl_t group[] = {kEmpty, 1, 2, kDeleted, 2, 1, kSentinel, 1}; - EXPECT_THAT(Group{group}.MatchEmptyOrDeleted(), ElementsAre(0, 3)); - } else { - FAIL() << "No test coverage for Group::kWidth==" << Group::kWidth; - } -} - -TEST(Batch, DropDeletes) { - constexpr size_t kCapacity = 63; - constexpr size_t kGroupWidth = container_internal::Group::kWidth; - std::vector ctrl(kCapacity + 1 + kGroupWidth); - ctrl[kCapacity] = kSentinel; - std::vector pattern = {kEmpty, 2, kDeleted, 2, kEmpty, 1, kDeleted}; - for (size_t i = 0; i != kCapacity; ++i) { - ctrl[i] = pattern[i % pattern.size()]; - if (i < kGroupWidth - 1) - ctrl[i + kCapacity + 1] = pattern[i % pattern.size()]; - } - ConvertDeletedToEmptyAndFullToDeleted(ctrl.data(), kCapacity); - ASSERT_EQ(ctrl[kCapacity], kSentinel); - for (size_t i = 0; i < kCapacity + 1 + kGroupWidth; ++i) { - ctrl_t expected = pattern[i % (kCapacity + 1) % pattern.size()]; - if (i == kCapacity) expected = kSentinel; - if (expected == kDeleted) expected = kEmpty; - if (IsFull(expected)) expected = kDeleted; - EXPECT_EQ(ctrl[i], expected) - << i << " " << int{pattern[i % pattern.size()]}; - } -} - -TEST(Group, CountLeadingEmptyOrDeleted) { - const std::vector empty_examples = {kEmpty, kDeleted}; - const std::vector full_examples = {0, 1, 2, 3, 5, 9, 127, kSentinel}; - - for (ctrl_t empty : empty_examples) { - std::vector e(Group::kWidth, empty); - EXPECT_EQ(Group::kWidth, Group{e.data()}.CountLeadingEmptyOrDeleted()); - for (ctrl_t full : full_examples) { - for (size_t i = 0; i != Group::kWidth; ++i) { - std::vector f(Group::kWidth, empty); - f[i] = full; - EXPECT_EQ(i, Group{f.data()}.CountLeadingEmptyOrDeleted()); - } - std::vector f(Group::kWidth, empty); - f[Group::kWidth * 2 / 3] = full; - f[Group::kWidth / 2] = full; - EXPECT_EQ( - Group::kWidth / 2, Group{f.data()}.CountLeadingEmptyOrDeleted()); - } - } -} - -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 - static auto apply(F&& f, int64_t x) -> decltype(std::forward(f)(x, x)) { - return std::forward(f)(x, x); - } -}; - -class StringPolicy { - template ::value>::type> - decltype(std::declval()( - std::declval(), std::piecewise_construct, - std::declval>(), - std::declval())) static apply_impl(F&& f, - std::pair, V> p) { - const absl::string_view& key = std::get<0>(p.first); - return std::forward(f)(key, std::piecewise_construct, std::move(p.first), - std::move(p.second)); - } - - public: - struct slot_type { - struct ctor {}; - - template - slot_type(ctor, Ts&&... ts) : pair(std::forward(ts)...) {} - - std::pair pair; - }; - - using key_type = std::string; - using init_type = std::pair; - - template - static void construct(allocator_type* alloc, slot_type* slot, Args... args) { - std::allocator_traits::construct( - *alloc, slot, typename slot_type::ctor(), std::forward(args)...); - } - - template - static void destroy(allocator_type* alloc, slot_type* slot) { - std::allocator_traits::destroy(*alloc, slot); - } - - template - 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& element(slot_type* slot) { - return slot->pair; - } - - template - static auto apply(F&& f, Args&&... args) - -> decltype(apply_impl(std::forward(f), - PairArgs(std::forward(args)...))) { - return apply_impl(std::forward(f), - PairArgs(std::forward(args)...)); - } -}; - -struct StringHash : absl::Hash { - using is_transparent = void; -}; -struct StringEq : std::equal_to { - using is_transparent = void; -}; - -struct StringTable - : raw_hash_set> { - using Base = typename StringTable::raw_hash_set; - StringTable() {} - using Base::Base; -}; - -struct IntTable - : raw_hash_set, - std::equal_to, std::allocator> { - using Base = typename IntTable::raw_hash_set; - using Base::Base; -}; - -template -struct CustomAlloc : std::allocator { - CustomAlloc() {} - - template - CustomAlloc(const CustomAlloc& other) {} - - template struct rebind { - using other = CustomAlloc; - }; -}; - -struct CustomAllocIntTable - : raw_hash_set, - std::equal_to, CustomAlloc> { - using Base = typename CustomAllocIntTable::raw_hash_set; - using Base::Base; -}; - -struct BadFastHash { - template - size_t operator()(const T&) const { - return 0; - } -}; - -struct BadTable : raw_hash_set, - std::allocator> { - using Base = typename BadTable::raw_hash_set; - BadTable() {} - using Base::Base; -}; - -TEST(Table, EmptyFunctorOptimization) { - static_assert(std::is_empty>::value, ""); - static_assert(std::is_empty>::value, ""); - - struct MockTable { - void* ctrl; - void* slots; - size_t size; - size_t capacity; - size_t growth_left; - void* infoz; - }; - struct StatelessHash { - size_t operator()(absl::string_view) const { return 0; } - }; - struct StatefulHash : StatelessHash { - size_t dummy; - }; - - EXPECT_EQ( - sizeof(MockTable), - sizeof( - raw_hash_set, std::allocator>)); - - EXPECT_EQ( - sizeof(MockTable) + sizeof(StatefulHash), - sizeof( - raw_hash_set, std::allocator>)); -} - -TEST(Table, Empty) { - IntTable t; - EXPECT_EQ(0, t.size()); - EXPECT_TRUE(t.empty()); -} - -TEST(Table, LookupEmpty) { - IntTable t; - auto it = t.find(0); - EXPECT_TRUE(it == t.end()); -} - -TEST(Table, Insert1) { - IntTable t; - EXPECT_TRUE(t.find(0) == t.end()); - auto res = t.emplace(0); - EXPECT_TRUE(res.second); - EXPECT_THAT(*res.first, 0); - EXPECT_EQ(1, t.size()); - EXPECT_THAT(*t.find(0), 0); -} - -TEST(Table, Insert2) { - IntTable t; - EXPECT_TRUE(t.find(0) == t.end()); - auto res = t.emplace(0); - EXPECT_TRUE(res.second); - EXPECT_THAT(*res.first, 0); - EXPECT_EQ(1, t.size()); - EXPECT_TRUE(t.find(1) == t.end()); - res = t.emplace(1); - EXPECT_TRUE(res.second); - EXPECT_THAT(*res.first, 1); - EXPECT_EQ(2, t.size()); - EXPECT_THAT(*t.find(0), 0); - EXPECT_THAT(*t.find(1), 1); -} - -TEST(Table, InsertCollision) { - BadTable t; - EXPECT_TRUE(t.find(1) == t.end()); - auto res = t.emplace(1); - EXPECT_TRUE(res.second); - EXPECT_THAT(*res.first, 1); - EXPECT_EQ(1, t.size()); - - EXPECT_TRUE(t.find(2) == t.end()); - res = t.emplace(2); - EXPECT_THAT(*res.first, 2); - EXPECT_TRUE(res.second); - EXPECT_EQ(2, t.size()); - - EXPECT_THAT(*t.find(1), 1); - EXPECT_THAT(*t.find(2), 2); -} - -// Test that we do not add existent element in case we need to search through -// many groups with deleted elements -TEST(Table, InsertCollisionAndFindAfterDelete) { - BadTable t; // all elements go to the same group. - // Have at least 2 groups with Group::kWidth collisions - // plus some extra collisions in the last group. - constexpr size_t kNumInserts = Group::kWidth * 2 + 5; - for (size_t i = 0; i < kNumInserts; ++i) { - auto res = t.emplace(i); - EXPECT_TRUE(res.second); - EXPECT_THAT(*res.first, i); - EXPECT_EQ(i + 1, t.size()); - } - - // Remove elements one by one and check - // that we still can find all other elements. - for (size_t i = 0; i < kNumInserts; ++i) { - EXPECT_EQ(1, t.erase(i)) << i; - for (size_t j = i + 1; j < kNumInserts; ++j) { - EXPECT_THAT(*t.find(j), j); - auto res = t.emplace(j); - EXPECT_FALSE(res.second) << i << " " << j; - EXPECT_THAT(*res.first, j); - EXPECT_EQ(kNumInserts - i - 1, t.size()); - } - } - EXPECT_TRUE(t.empty()); -} - -TEST(Table, LazyEmplace) { - StringTable t; - bool called = false; - auto it = t.lazy_emplace("abc", [&](const StringTable::constructor& f) { - called = true; - f("abc", "ABC"); - }); - EXPECT_TRUE(called); - EXPECT_THAT(*it, Pair("abc", "ABC")); - called = false; - it = t.lazy_emplace("abc", [&](const StringTable::constructor& f) { - called = true; - f("abc", "DEF"); - }); - EXPECT_FALSE(called); - EXPECT_THAT(*it, Pair("abc", "ABC")); -} - -TEST(Table, ContainsEmpty) { - IntTable t; - - EXPECT_FALSE(t.contains(0)); -} - -TEST(Table, Contains1) { - IntTable t; - - EXPECT_TRUE(t.insert(0).second); - EXPECT_TRUE(t.contains(0)); - EXPECT_FALSE(t.contains(1)); - - EXPECT_EQ(1, t.erase(0)); - EXPECT_FALSE(t.contains(0)); -} - -TEST(Table, Contains2) { - IntTable t; - - EXPECT_TRUE(t.insert(0).second); - EXPECT_TRUE(t.contains(0)); - EXPECT_FALSE(t.contains(1)); - - t.clear(); - EXPECT_FALSE(t.contains(0)); -} - -int decompose_constructed; -struct DecomposeType { - DecomposeType(int i) : i(i) { // NOLINT - ++decompose_constructed; - } - - explicit DecomposeType(const char* d) : DecomposeType(*d) {} - - int i; -}; - -struct DecomposeHash { - using is_transparent = void; - size_t operator()(DecomposeType a) const { return a.i; } - size_t operator()(int a) const { return a; } - size_t operator()(const char* a) const { return *a; } -}; - -struct DecomposeEq { - using is_transparent = void; - bool operator()(DecomposeType a, DecomposeType b) const { return a.i == b.i; } - bool operator()(DecomposeType a, int b) const { return a.i == b; } - bool operator()(DecomposeType a, const char* b) const { return a.i == *b; } -}; - -struct DecomposePolicy { - using slot_type = DecomposeType; - using key_type = DecomposeType; - using init_type = DecomposeType; - - template - static void construct(void*, DecomposeType* slot, T&& v) { - *slot = DecomposeType(std::forward(v)); - } - static void destroy(void*, DecomposeType*) {} - static DecomposeType& element(slot_type* slot) { return *slot; } - - template - static auto apply(F&& f, const T& x) -> decltype(std::forward(f)(x, x)) { - return std::forward(f)(x, x); - } -}; - -template -void TestDecompose(bool construct_three) { - DecomposeType elem{0}; - const int one = 1; - const char* three_p = "3"; - const auto& three = three_p; - - raw_hash_set> set1; - - decompose_constructed = 0; - int expected_constructed = 0; - EXPECT_EQ(expected_constructed, decompose_constructed); - set1.insert(elem); - EXPECT_EQ(expected_constructed, decompose_constructed); - set1.insert(1); - EXPECT_EQ(++expected_constructed, decompose_constructed); - set1.emplace("3"); - EXPECT_EQ(++expected_constructed, decompose_constructed); - EXPECT_EQ(expected_constructed, decompose_constructed); - - { // insert(T&&) - set1.insert(1); - EXPECT_EQ(expected_constructed, decompose_constructed); - } - - { // insert(const T&) - set1.insert(one); - EXPECT_EQ(expected_constructed, decompose_constructed); - } - - { // insert(hint, T&&) - set1.insert(set1.begin(), 1); - EXPECT_EQ(expected_constructed, decompose_constructed); - } - - { // insert(hint, const T&) - set1.insert(set1.begin(), one); - EXPECT_EQ(expected_constructed, decompose_constructed); - } - - { // emplace(...) - set1.emplace(1); - EXPECT_EQ(expected_constructed, decompose_constructed); - set1.emplace("3"); - expected_constructed += construct_three; - EXPECT_EQ(expected_constructed, decompose_constructed); - set1.emplace(one); - EXPECT_EQ(expected_constructed, decompose_constructed); - set1.emplace(three); - expected_constructed += construct_three; - EXPECT_EQ(expected_constructed, decompose_constructed); - } - - { // emplace_hint(...) - set1.emplace_hint(set1.begin(), 1); - EXPECT_EQ(expected_constructed, decompose_constructed); - set1.emplace_hint(set1.begin(), "3"); - expected_constructed += construct_three; - EXPECT_EQ(expected_constructed, decompose_constructed); - set1.emplace_hint(set1.begin(), one); - EXPECT_EQ(expected_constructed, decompose_constructed); - set1.emplace_hint(set1.begin(), three); - expected_constructed += construct_three; - EXPECT_EQ(expected_constructed, decompose_constructed); - } -} - -TEST(Table, Decompose) { - TestDecompose(false); - - struct TransparentHashIntOverload { - size_t operator()(DecomposeType a) const { return a.i; } - size_t operator()(int a) const { return a; } - }; - struct TransparentEqIntOverload { - bool operator()(DecomposeType a, DecomposeType b) const { - return a.i == b.i; - } - bool operator()(DecomposeType a, int b) const { return a.i == b; } - }; - TestDecompose(true); - TestDecompose(true); - TestDecompose(true); -} - -// Returns the largest m such that a table with m elements has the same number -// of buckets as a table with n elements. -size_t MaxDensitySize(size_t n) { - IntTable t; - t.reserve(n); - for (size_t i = 0; i != n; ++i) t.emplace(i); - const size_t c = t.bucket_count(); - while (c == t.bucket_count()) t.emplace(n++); - return t.size() - 1; -} - -struct Modulo1000Hash { - size_t operator()(int x) const { return x % 1000; } -}; - -struct Modulo1000HashTable - : public raw_hash_set, - std::allocator> {}; - -// Test that rehash with no resize happen in case of many deleted slots. -TEST(Table, RehashWithNoResize) { - Modulo1000HashTable t; - // Adding the same length (and the same hash) strings - // to have at least kMinFullGroups groups - // with Group::kWidth collisions. Then fill up to MaxDensitySize; - const size_t kMinFullGroups = 7; - std::vector keys; - for (size_t i = 0; i < MaxDensitySize(Group::kWidth * kMinFullGroups); ++i) { - int k = i * 1000; - t.emplace(k); - keys.push_back(k); - } - const size_t capacity = t.capacity(); - - // Remove elements from all groups except the first and the last one. - // All elements removed from full groups will be marked as kDeleted. - const size_t erase_begin = Group::kWidth / 2; - const size_t erase_end = (t.size() / Group::kWidth - 1) * Group::kWidth; - for (size_t i = erase_begin; i < erase_end; ++i) { - EXPECT_EQ(1, t.erase(keys[i])) << i; - } - keys.erase(keys.begin() + erase_begin, keys.begin() + erase_end); - - auto last_key = keys.back(); - size_t last_key_num_probes = GetHashtableDebugNumProbes(t, last_key); - - // Make sure that we have to make a lot of probes for last key. - ASSERT_GT(last_key_num_probes, kMinFullGroups); - - int x = 1; - // Insert and erase one element, before inplace rehash happen. - while (last_key_num_probes == GetHashtableDebugNumProbes(t, last_key)) { - t.emplace(x); - ASSERT_EQ(capacity, t.capacity()); - // All elements should be there. - ASSERT_TRUE(t.find(x) != t.end()) << x; - for (const auto& k : keys) { - ASSERT_TRUE(t.find(k) != t.end()) << k; - } - t.erase(x); - ++x; - } -} - -TEST(Table, InsertEraseStressTest) { - IntTable t; - const size_t kMinElementCount = 250; - std::deque keys; - size_t i = 0; - for (; i < MaxDensitySize(kMinElementCount); ++i) { - t.emplace(i); - keys.push_back(i); - } - const size_t kNumIterations = 1000000; - for (; i < kNumIterations; ++i) { - ASSERT_EQ(1, t.erase(keys.front())); - keys.pop_front(); - t.emplace(i); - keys.push_back(i); - } -} - -TEST(Table, InsertOverloads) { - StringTable t; - // These should all trigger the insert(init_type) overload. - t.insert({{}, {}}); - t.insert({"ABC", {}}); - t.insert({"DEF", "!!!"}); - - EXPECT_THAT(t, UnorderedElementsAre(Pair("", ""), Pair("ABC", ""), - Pair("DEF", "!!!"))); -} - -TEST(Table, LargeTable) { - IntTable t; - for (int64_t i = 0; i != 100000; ++i) t.emplace(i << 40); - for (int64_t i = 0; i != 100000; ++i) ASSERT_EQ(i << 40, *t.find(i << 40)); -} - -// Timeout if copy is quadratic as it was in Rust. -TEST(Table, EnsureNonQuadraticAsInRust) { - static const size_t kLargeSize = 1 << 15; - - IntTable t; - for (size_t i = 0; i != kLargeSize; ++i) { - t.insert(i); - } - - // If this is quadratic, the test will timeout. - IntTable t2; - for (const auto& entry : t) t2.insert(entry); -} - -TEST(Table, ClearBug) { - IntTable t; - constexpr size_t capacity = container_internal::Group::kWidth - 1; - constexpr size_t max_size = capacity / 2 + 1; - for (size_t i = 0; i < max_size; ++i) { - t.insert(i); - } - ASSERT_EQ(capacity, t.capacity()); - intptr_t original = reinterpret_cast(&*t.find(2)); - t.clear(); - ASSERT_EQ(capacity, t.capacity()); - for (size_t i = 0; i < max_size; ++i) { - t.insert(i); - } - ASSERT_EQ(capacity, t.capacity()); - intptr_t second = reinterpret_cast(&*t.find(2)); - // We are checking that original and second are close enough to each other - // that they are probably still in the same group. This is not strictly - // guaranteed. - EXPECT_LT(std::abs(original - second), - capacity * sizeof(IntTable::value_type)); -} - -TEST(Table, Erase) { - IntTable t; - EXPECT_TRUE(t.find(0) == t.end()); - auto res = t.emplace(0); - EXPECT_TRUE(res.second); - EXPECT_EQ(1, t.size()); - t.erase(res.first); - EXPECT_EQ(0, t.size()); - EXPECT_TRUE(t.find(0) == t.end()); -} - -TEST(Table, EraseMaintainsValidIterator) { - IntTable t; - const int kNumElements = 100; - for (int i = 0; i < kNumElements; i ++) { - EXPECT_TRUE(t.emplace(i).second); - } - EXPECT_EQ(t.size(), kNumElements); - - int num_erase_calls = 0; - auto it = t.begin(); - while (it != t.end()) { - t.erase(it++); - num_erase_calls++; - } - - EXPECT_TRUE(t.empty()); - EXPECT_EQ(num_erase_calls, kNumElements); -} - -// Collect N bad keys by following algorithm: -// 1. Create an empty table and reserve it to 2 * N. -// 2. Insert N random elements. -// 3. Take first Group::kWidth - 1 to bad_keys array. -// 4. Clear the table without resize. -// 5. Go to point 2 while N keys not collected -std::vector CollectBadMergeKeys(size_t N) { - static constexpr int kGroupSize = Group::kWidth - 1; - - auto topk_range = [](size_t b, size_t e, - IntTable* t) -> std::vector { - for (size_t i = b; i != e; ++i) { - t->emplace(i); - } - std::vector res; - res.reserve(kGroupSize); - auto it = t->begin(); - for (size_t i = b; i != e && i != b + kGroupSize; ++i, ++it) { - res.push_back(*it); - } - return res; - }; - - std::vector bad_keys; - bad_keys.reserve(N); - IntTable t; - t.reserve(N * 2); - - for (size_t b = 0; bad_keys.size() < N; b += N) { - auto keys = topk_range(b, b + N, &t); - bad_keys.insert(bad_keys.end(), keys.begin(), keys.end()); - t.erase(t.begin(), t.end()); - EXPECT_TRUE(t.empty()); - } - return bad_keys; -} - -struct ProbeStats { - // Number of elements with specific probe length over all tested tables. - std::vector all_probes_histogram; - // Ratios total_probe_length/size for every tested table. - std::vector single_table_ratios; - - friend ProbeStats operator+(const ProbeStats& a, const ProbeStats& b) { - ProbeStats res = a; - res.all_probes_histogram.resize(std::max(res.all_probes_histogram.size(), - b.all_probes_histogram.size())); - std::transform(b.all_probes_histogram.begin(), b.all_probes_histogram.end(), - res.all_probes_histogram.begin(), - res.all_probes_histogram.begin(), std::plus()); - res.single_table_ratios.insert(res.single_table_ratios.end(), - b.single_table_ratios.begin(), - b.single_table_ratios.end()); - return res; - } - - // Average ratio total_probe_length/size over tables. - double AvgRatio() const { - return std::accumulate(single_table_ratios.begin(), - single_table_ratios.end(), 0.0) / - single_table_ratios.size(); - } - - // Maximum ratio total_probe_length/size over tables. - double MaxRatio() const { - return *std::max_element(single_table_ratios.begin(), - single_table_ratios.end()); - } - - // Percentile ratio total_probe_length/size over tables. - double PercentileRatio(double Percentile = 0.95) const { - auto r = single_table_ratios; - auto mid = r.begin() + static_cast(r.size() * Percentile); - if (mid != r.end()) { - std::nth_element(r.begin(), mid, r.end()); - return *mid; - } else { - return MaxRatio(); - } - } - - // Maximum probe length over all elements and all tables. - size_t MaxProbe() const { return all_probes_histogram.size(); } - - // Fraction of elements with specified probe length. - std::vector ProbeNormalizedHistogram() const { - double total_elements = std::accumulate(all_probes_histogram.begin(), - all_probes_histogram.end(), 0ull); - std::vector res; - for (size_t p : all_probes_histogram) { - res.push_back(p / total_elements); - } - return res; - } - - size_t PercentileProbe(double Percentile = 0.99) const { - size_t idx = 0; - for (double p : ProbeNormalizedHistogram()) { - if (Percentile > p) { - Percentile -= p; - ++idx; - } else { - return idx; - } - } - return idx; - } - - friend std::ostream& operator<<(std::ostream& out, const ProbeStats& s) { - out << "{AvgRatio:" << s.AvgRatio() << ", MaxRatio:" << s.MaxRatio() - << ", PercentileRatio:" << s.PercentileRatio() - << ", MaxProbe:" << s.MaxProbe() << ", Probes=["; - for (double p : s.ProbeNormalizedHistogram()) { - out << p << ","; - } - out << "]}"; - - return out; - } -}; - -struct ExpectedStats { - double avg_ratio; - double max_ratio; - std::vector> pecentile_ratios; - std::vector> pecentile_probes; - - friend std::ostream& operator<<(std::ostream& out, const ExpectedStats& s) { - out << "{AvgRatio:" << s.avg_ratio << ", MaxRatio:" << s.max_ratio - << ", PercentileRatios: ["; - for (auto el : s.pecentile_ratios) { - out << el.first << ":" << el.second << ", "; - } - out << "], PercentileProbes: ["; - for (auto el : s.pecentile_probes) { - out << el.first << ":" << el.second << ", "; - } - out << "]}"; - - return out; - } -}; - -void VerifyStats(size_t size, const ExpectedStats& exp, - const ProbeStats& stats) { - EXPECT_LT(stats.AvgRatio(), exp.avg_ratio) << size << " " << stats; - EXPECT_LT(stats.MaxRatio(), exp.max_ratio) << size << " " << stats; - for (auto pr : exp.pecentile_ratios) { - EXPECT_LE(stats.PercentileRatio(pr.first), pr.second) - << size << " " << pr.first << " " << stats; - } - - for (auto pr : exp.pecentile_probes) { - EXPECT_LE(stats.PercentileProbe(pr.first), pr.second) - << size << " " << pr.first << " " << stats; - } -} - -using ProbeStatsPerSize = std::map; - -// Collect total ProbeStats on num_iters iterations of the following algorithm: -// 1. Create new table and reserve it to keys.size() * 2 -// 2. Insert all keys xored with seed -// 3. Collect ProbeStats from final table. -ProbeStats CollectProbeStatsOnKeysXoredWithSeed( - const std::vector& keys, size_t num_iters) { - const size_t reserve_size = keys.size() * 2; - - ProbeStats stats; - - int64_t seed = 0x71b1a19b907d6e33; - while (num_iters--) { - seed = static_cast(static_cast(seed) * 17 + 13); - IntTable t1; - t1.reserve(reserve_size); - for (const auto& key : keys) { - t1.emplace(key ^ seed); - } - - auto probe_histogram = GetHashtableDebugNumProbesHistogram(t1); - stats.all_probes_histogram.resize( - std::max(stats.all_probes_histogram.size(), probe_histogram.size())); - std::transform(probe_histogram.begin(), probe_histogram.end(), - stats.all_probes_histogram.begin(), - stats.all_probes_histogram.begin(), std::plus()); - - size_t total_probe_seq_length = 0; - for (size_t i = 0; i < probe_histogram.size(); ++i) { - total_probe_seq_length += i * probe_histogram[i]; - } - stats.single_table_ratios.push_back(total_probe_seq_length * 1.0 / - keys.size()); - t1.erase(t1.begin(), t1.end()); - } - return stats; -} - -ExpectedStats XorSeedExpectedStats() { - constexpr bool kRandomizesInserts = -#ifdef NDEBUG - false; -#else // NDEBUG - true; -#endif // NDEBUG - - // The effective load factor is larger in non-opt mode because we insert - // elements out of order. - switch (container_internal::Group::kWidth) { - case 8: - if (kRandomizesInserts) { - return {0.05, - 1.0, - {{0.95, 0.5}}, - {{0.95, 0}, {0.99, 2}, {0.999, 4}, {0.9999, 10}}}; - } else { - return {0.05, - 2.0, - {{0.95, 0.1}}, - {{0.95, 0}, {0.99, 2}, {0.999, 4}, {0.9999, 10}}}; - } - case 16: - if (kRandomizesInserts) { - return {0.1, - 1.0, - {{0.95, 0.1}}, - {{0.95, 0}, {0.99, 1}, {0.999, 8}, {0.9999, 15}}}; - } else { - return {0.05, - 1.0, - {{0.95, 0.05}}, - {{0.95, 0}, {0.99, 1}, {0.999, 4}, {0.9999, 10}}}; - } - } - ABSL_RAW_LOG(FATAL, "%s", "Unknown Group width"); - return {}; -} - -TEST(Table, DISABLED_EnsureNonQuadraticTopNXorSeedByProbeSeqLength) { - ProbeStatsPerSize stats; - std::vector sizes = {Group::kWidth << 5, Group::kWidth << 10}; - for (size_t size : sizes) { - stats[size] = - CollectProbeStatsOnKeysXoredWithSeed(CollectBadMergeKeys(size), 200); - } - auto expected = XorSeedExpectedStats(); - for (size_t size : sizes) { - auto& stat = stats[size]; - VerifyStats(size, expected, stat); - } -} - -// Collect total ProbeStats on num_iters iterations of the following algorithm: -// 1. Create new table -// 2. Select 10% of keys and insert 10 elements key * 17 + j * 13 -// 3. Collect ProbeStats from final table -ProbeStats CollectProbeStatsOnLinearlyTransformedKeys( - const std::vector& keys, size_t num_iters) { - ProbeStats stats; - - std::random_device rd; - std::mt19937 rng(rd()); - auto linear_transform = [](size_t x, size_t y) { return x * 17 + y * 13; }; - std::uniform_int_distribution dist(0, keys.size()-1); - while (num_iters--) { - IntTable t1; - size_t num_keys = keys.size() / 10; - size_t start = dist(rng); - for (size_t i = 0; i != num_keys; ++i) { - for (size_t j = 0; j != 10; ++j) { - t1.emplace(linear_transform(keys[(i + start) % keys.size()], j)); - } - } - - auto probe_histogram = GetHashtableDebugNumProbesHistogram(t1); - stats.all_probes_histogram.resize( - std::max(stats.all_probes_histogram.size(), probe_histogram.size())); - std::transform(probe_histogram.begin(), probe_histogram.end(), - stats.all_probes_histogram.begin(), - stats.all_probes_histogram.begin(), std::plus()); - - size_t total_probe_seq_length = 0; - for (size_t i = 0; i < probe_histogram.size(); ++i) { - total_probe_seq_length += i * probe_histogram[i]; - } - stats.single_table_ratios.push_back(total_probe_seq_length * 1.0 / - t1.size()); - t1.erase(t1.begin(), t1.end()); - } - return stats; -} - -ExpectedStats LinearTransformExpectedStats() { - constexpr bool kRandomizesInserts = -#ifdef NDEBUG - false; -#else // NDEBUG - true; -#endif // NDEBUG - - // The effective load factor is larger in non-opt mode because we insert - // elements out of order. - switch (container_internal::Group::kWidth) { - case 8: - if (kRandomizesInserts) { - return {0.1, - 0.5, - {{0.95, 0.3}}, - {{0.95, 0}, {0.99, 1}, {0.999, 8}, {0.9999, 15}}}; - } else { - return {0.15, - 0.5, - {{0.95, 0.3}}, - {{0.95, 0}, {0.99, 3}, {0.999, 15}, {0.9999, 25}}}; - } - case 16: - if (kRandomizesInserts) { - return {0.1, - 0.4, - {{0.95, 0.3}}, - {{0.95, 0}, {0.99, 1}, {0.999, 8}, {0.9999, 15}}}; - } else { - return {0.05, - 0.2, - {{0.95, 0.1}}, - {{0.95, 0}, {0.99, 1}, {0.999, 6}, {0.9999, 10}}}; - } - } - ABSL_RAW_LOG(FATAL, "%s", "Unknown Group width"); - return {}; -} - -TEST(Table, DISABLED_EnsureNonQuadraticTopNLinearTransformByProbeSeqLength) { - ProbeStatsPerSize stats; - std::vector sizes = {Group::kWidth << 5, Group::kWidth << 10}; - for (size_t size : sizes) { - stats[size] = CollectProbeStatsOnLinearlyTransformedKeys( - CollectBadMergeKeys(size), 300); - } - auto expected = LinearTransformExpectedStats(); - for (size_t size : sizes) { - auto& stat = stats[size]; - VerifyStats(size, expected, stat); - } -} - -TEST(Table, EraseCollision) { - BadTable t; - - // 1 2 3 - t.emplace(1); - t.emplace(2); - t.emplace(3); - EXPECT_THAT(*t.find(1), 1); - EXPECT_THAT(*t.find(2), 2); - EXPECT_THAT(*t.find(3), 3); - EXPECT_EQ(3, t.size()); - - // 1 DELETED 3 - t.erase(t.find(2)); - EXPECT_THAT(*t.find(1), 1); - EXPECT_TRUE(t.find(2) == t.end()); - EXPECT_THAT(*t.find(3), 3); - EXPECT_EQ(2, t.size()); - - // DELETED DELETED 3 - t.erase(t.find(1)); - EXPECT_TRUE(t.find(1) == t.end()); - EXPECT_TRUE(t.find(2) == t.end()); - EXPECT_THAT(*t.find(3), 3); - EXPECT_EQ(1, t.size()); - - // DELETED DELETED DELETED - t.erase(t.find(3)); - EXPECT_TRUE(t.find(1) == t.end()); - EXPECT_TRUE(t.find(2) == t.end()); - EXPECT_TRUE(t.find(3) == t.end()); - EXPECT_EQ(0, t.size()); -} - -TEST(Table, EraseInsertProbing) { - BadTable t(100); - - // 1 2 3 4 - t.emplace(1); - t.emplace(2); - t.emplace(3); - t.emplace(4); - - // 1 DELETED 3 DELETED - t.erase(t.find(2)); - t.erase(t.find(4)); - - // 1 10 3 11 12 - t.emplace(10); - t.emplace(11); - t.emplace(12); - - EXPECT_EQ(5, t.size()); - EXPECT_THAT(t, UnorderedElementsAre(1, 10, 3, 11, 12)); -} - -TEST(Table, Clear) { - IntTable t; - EXPECT_TRUE(t.find(0) == t.end()); - t.clear(); - EXPECT_TRUE(t.find(0) == t.end()); - auto res = t.emplace(0); - EXPECT_TRUE(res.second); - EXPECT_EQ(1, t.size()); - t.clear(); - EXPECT_EQ(0, t.size()); - EXPECT_TRUE(t.find(0) == t.end()); -} - -TEST(Table, Swap) { - IntTable t; - EXPECT_TRUE(t.find(0) == t.end()); - auto res = t.emplace(0); - EXPECT_TRUE(res.second); - EXPECT_EQ(1, t.size()); - IntTable u; - t.swap(u); - EXPECT_EQ(0, t.size()); - EXPECT_EQ(1, u.size()); - EXPECT_TRUE(t.find(0) == t.end()); - EXPECT_THAT(*u.find(0), 0); -} - -TEST(Table, Rehash) { - IntTable t; - EXPECT_TRUE(t.find(0) == t.end()); - t.emplace(0); - t.emplace(1); - EXPECT_EQ(2, t.size()); - t.rehash(128); - EXPECT_EQ(2, t.size()); - EXPECT_THAT(*t.find(0), 0); - EXPECT_THAT(*t.find(1), 1); -} - -TEST(Table, RehashDoesNotRehashWhenNotNecessary) { - IntTable t; - t.emplace(0); - t.emplace(1); - auto* p = &*t.find(0); - t.rehash(1); - EXPECT_EQ(p, &*t.find(0)); -} - -TEST(Table, RehashZeroDoesNotAllocateOnEmptyTable) { - IntTable t; - t.rehash(0); - EXPECT_EQ(0, t.bucket_count()); -} - -TEST(Table, RehashZeroDeallocatesEmptyTable) { - IntTable t; - t.emplace(0); - t.clear(); - EXPECT_NE(0, t.bucket_count()); - t.rehash(0); - EXPECT_EQ(0, t.bucket_count()); -} - -TEST(Table, RehashZeroForcesRehash) { - IntTable t; - t.emplace(0); - t.emplace(1); - auto* p = &*t.find(0); - t.rehash(0); - EXPECT_NE(p, &*t.find(0)); -} - -TEST(Table, ConstructFromInitList) { - using P = std::pair; - struct Q { - operator P() const { return {}; } - }; - StringTable t = {P(), Q(), {}, {{}, {}}}; -} - -TEST(Table, CopyConstruct) { - IntTable t; - t.emplace(0); - EXPECT_EQ(1, t.size()); - { - IntTable u(t); - EXPECT_EQ(1, u.size()); - EXPECT_THAT(*u.find(0), 0); - } - { - IntTable u{t}; - EXPECT_EQ(1, u.size()); - EXPECT_THAT(*u.find(0), 0); - } - { - IntTable u = t; - EXPECT_EQ(1, u.size()); - EXPECT_THAT(*u.find(0), 0); - } -} - -TEST(Table, CopyConstructWithAlloc) { - StringTable t; - t.emplace("a", "b"); - EXPECT_EQ(1, t.size()); - StringTable u(t, Alloc>()); - EXPECT_EQ(1, u.size()); - EXPECT_THAT(*u.find("a"), Pair("a", "b")); -} - -struct ExplicitAllocIntTable - : raw_hash_set, - std::equal_to, Alloc> { - ExplicitAllocIntTable() {} -}; - -TEST(Table, AllocWithExplicitCtor) { - ExplicitAllocIntTable t; - EXPECT_EQ(0, t.size()); -} - -TEST(Table, MoveConstruct) { - { - StringTable t; - t.emplace("a", "b"); - EXPECT_EQ(1, t.size()); - - StringTable u(std::move(t)); - EXPECT_EQ(1, u.size()); - EXPECT_THAT(*u.find("a"), Pair("a", "b")); - } - { - StringTable t; - t.emplace("a", "b"); - EXPECT_EQ(1, t.size()); - - StringTable u{std::move(t)}; - EXPECT_EQ(1, u.size()); - EXPECT_THAT(*u.find("a"), Pair("a", "b")); - } - { - StringTable t; - t.emplace("a", "b"); - EXPECT_EQ(1, t.size()); - - StringTable u = std::move(t); - EXPECT_EQ(1, u.size()); - EXPECT_THAT(*u.find("a"), Pair("a", "b")); - } -} - -TEST(Table, MoveConstructWithAlloc) { - StringTable t; - t.emplace("a", "b"); - EXPECT_EQ(1, t.size()); - StringTable u(std::move(t), Alloc>()); - EXPECT_EQ(1, u.size()); - EXPECT_THAT(*u.find("a"), Pair("a", "b")); -} - -TEST(Table, CopyAssign) { - StringTable t; - t.emplace("a", "b"); - EXPECT_EQ(1, t.size()); - StringTable u; - u = t; - EXPECT_EQ(1, u.size()); - EXPECT_THAT(*u.find("a"), Pair("a", "b")); -} - -TEST(Table, CopySelfAssign) { - StringTable t; - t.emplace("a", "b"); - EXPECT_EQ(1, t.size()); - t = *&t; - EXPECT_EQ(1, t.size()); - EXPECT_THAT(*t.find("a"), Pair("a", "b")); -} - -TEST(Table, MoveAssign) { - StringTable t; - t.emplace("a", "b"); - EXPECT_EQ(1, t.size()); - StringTable u; - u = std::move(t); - EXPECT_EQ(1, u.size()); - EXPECT_THAT(*u.find("a"), Pair("a", "b")); -} - -TEST(Table, Equality) { - StringTable t; - std::vector> v = {{"a", "b"}, - {"aa", "bb"}}; - t.insert(std::begin(v), std::end(v)); - StringTable u = t; - EXPECT_EQ(u, t); -} - -TEST(Table, Equality2) { - StringTable t; - std::vector> v1 = {{"a", "b"}, - {"aa", "bb"}}; - t.insert(std::begin(v1), std::end(v1)); - StringTable u; - std::vector> v2 = {{"a", "a"}, - {"aa", "aa"}}; - u.insert(std::begin(v2), std::end(v2)); - EXPECT_NE(u, t); -} - -TEST(Table, Equality3) { - StringTable t; - std::vector> v1 = {{"b", "b"}, - {"bb", "bb"}}; - t.insert(std::begin(v1), std::end(v1)); - StringTable u; - std::vector> v2 = {{"a", "a"}, - {"aa", "aa"}}; - u.insert(std::begin(v2), std::end(v2)); - EXPECT_NE(u, t); -} - -TEST(Table, NumDeletedRegression) { - IntTable t; - t.emplace(0); - t.erase(t.find(0)); - // construct over a deleted slot. - t.emplace(0); - t.clear(); -} - -TEST(Table, FindFullDeletedRegression) { - IntTable t; - for (int i = 0; i < 1000; ++i) { - t.emplace(i); - t.erase(t.find(i)); - } - EXPECT_EQ(0, t.size()); -} - -TEST(Table, ReplacingDeletedSlotDoesNotRehash) { - size_t n; - { - // Compute n such that n is the maximum number of elements before rehash. - IntTable t; - t.emplace(0); - size_t c = t.bucket_count(); - for (n = 1; c == t.bucket_count(); ++n) t.emplace(n); - --n; - } - IntTable t; - t.rehash(n); - const size_t c = t.bucket_count(); - for (size_t i = 0; i != n; ++i) t.emplace(i); - EXPECT_EQ(c, t.bucket_count()) << "rehashing threshold = " << n; - t.erase(0); - t.emplace(0); - EXPECT_EQ(c, t.bucket_count()) << "rehashing threshold = " << n; -} - -TEST(Table, NoThrowMoveConstruct) { - ASSERT_TRUE( - std::is_nothrow_copy_constructible>::value); - ASSERT_TRUE(std::is_nothrow_copy_constructible< - std::equal_to>::value); - ASSERT_TRUE(std::is_nothrow_copy_constructible>::value); - EXPECT_TRUE(std::is_nothrow_move_constructible::value); -} - -TEST(Table, NoThrowMoveAssign) { - ASSERT_TRUE( - std::is_nothrow_move_assignable>::value); - ASSERT_TRUE( - std::is_nothrow_move_assignable>::value); - ASSERT_TRUE(std::is_nothrow_move_assignable>::value); - ASSERT_TRUE( - absl::allocator_traits>::is_always_equal::value); - EXPECT_TRUE(std::is_nothrow_move_assignable::value); -} - -TEST(Table, NoThrowSwappable) { - ASSERT_TRUE( - container_internal::IsNoThrowSwappable>()); - ASSERT_TRUE(container_internal::IsNoThrowSwappable< - std::equal_to>()); - ASSERT_TRUE(container_internal::IsNoThrowSwappable>()); - EXPECT_TRUE(container_internal::IsNoThrowSwappable()); -} - -TEST(Table, HeterogeneousLookup) { - struct Hash { - size_t operator()(int64_t i) const { return i; } - size_t operator()(double i) const { - ADD_FAILURE(); - return i; - } - }; - struct Eq { - bool operator()(int64_t a, int64_t b) const { return a == b; } - bool operator()(double a, int64_t b) const { - ADD_FAILURE(); - return a == b; - } - bool operator()(int64_t a, double b) const { - ADD_FAILURE(); - return a == b; - } - bool operator()(double a, double b) const { - ADD_FAILURE(); - return a == b; - } - }; - - struct THash { - using is_transparent = void; - size_t operator()(int64_t i) const { return i; } - size_t operator()(double i) const { return i; } - }; - struct TEq { - using is_transparent = void; - bool operator()(int64_t a, int64_t b) const { return a == b; } - bool operator()(double a, int64_t b) const { return a == b; } - bool operator()(int64_t a, double b) const { return a == b; } - bool operator()(double a, double b) const { return a == b; } - }; - - raw_hash_set> s{0, 1, 2}; - // It will convert to int64_t before the query. - EXPECT_EQ(1, *s.find(double{1.1})); - - raw_hash_set> ts{0, 1, 2}; - // It will try to use the double, and fail to find the object. - EXPECT_TRUE(ts.find(1.1) == ts.end()); -} - -template -using CallFind = decltype(std::declval().find(17)); - -template -using CallErase = decltype(std::declval().erase(17)); - -template -using CallExtract = decltype(std::declval().extract(17)); - -template -using CallPrefetch = decltype(std::declval().prefetch(17)); - -template -using CallCount = decltype(std::declval().count(17)); - -template