#ifndef PHMAP_BTREE_BTREE_CONTAINER_H_
#define PHMAP_BTREE_BTREE_CONTAINER_H_
#ifdef _MSC_VER
#pragma warning(push)
#pragma warning(disable : 4127)
#pragma warning(disable : 4324)
#pragma warning(disable : 4355)
#pragma warning(disable : 4365)
#pragma warning(disable : 4514)
#pragma warning(disable : 4623)
#pragma warning(disable : 4625)
#pragma warning(disable : 4626)
#pragma warning(disable : 4710)
#pragma warning(disable : 4711)
#pragma warning(disable : 4820)
#pragma warning(disable : 4868)
#pragma warning(disable : 5026)
#pragma warning(disable : 5027)
#pragma warning(disable : 5045)
#endif
#include <cstdint>
#include <cstdlib>
#include <cstring>
#include <limits>
#include <new>
#include "phmap_fwd_decl.h"
#include "phmap_base.h"
#if PHMAP_HAVE_STD_STRING_VIEW
#include <string_view>
#endif
#if defined(_MSC_VER) && !defined(__clang__) && !defined(__GNUC__)
#define PHMAP_META_INTERNAL_STD_CONSTRUCTION_TRAITS_DONT_CHECK_DESTRUCTION 1
#endif
namespace phmap {
template <typename T>
struct is_trivially_destructible;
template <typename T>
struct is_trivially_move_assignable;
namespace type_traits_internal {
#if defined(_MSC_VER) && !defined(__GNUC__)
#pragma warning(push)
#pragma warning(disable : 4624)
#endif
template <class T>
union SingleMemberUnion {
T t;
};
#if defined(_MSC_VER) && !defined(__GNUC__)
#pragma warning(pop)
#endif
template <class T>
struct IsTriviallyMoveConstructibleObject
: std::integral_constant<
bool, std::is_move_constructible<
type_traits_internal::SingleMemberUnion<T>>::value &&
phmap::is_trivially_destructible<T>::value> {};
template <class T>
struct IsTriviallyCopyConstructibleObject
: std::integral_constant<
bool, std::is_copy_constructible<
type_traits_internal::SingleMemberUnion<T>>::value &&
phmap::is_trivially_destructible<T>::value> {};
template <class T>
struct IsTriviallyMoveAssignableReference : std::false_type {};
template <class T>
struct IsTriviallyMoveAssignableReference<T&>
: phmap::is_trivially_move_assignable<T>::type {};
template <class T>
struct IsTriviallyMoveAssignableReference<T&&>
: phmap::is_trivially_move_assignable<T>::type {};
}
template <typename... Ts>
using void_t = typename type_traits_internal::VoidTImpl<Ts...>::type;
template <typename T>
struct is_function
: std::integral_constant<
bool, !(std::is_reference<T>::value ||
std::is_const<typename std::add_const<T>::type>::value)> {};
namespace type_traits_internal {
template <typename T>
class is_trivially_copyable_impl {
using ExtentsRemoved = typename std::remove_all_extents<T>::type;
static constexpr bool kIsCopyOrMoveConstructible =
std::is_copy_constructible<ExtentsRemoved>::value ||
std::is_move_constructible<ExtentsRemoved>::value;
static constexpr bool kIsCopyOrMoveAssignable =
phmap::is_copy_assignable<ExtentsRemoved>::value ||
phmap::is_move_assignable<ExtentsRemoved>::value;
public:
static constexpr bool kValue =
(__has_trivial_copy(ExtentsRemoved) || !kIsCopyOrMoveConstructible) &&
(__has_trivial_assign(ExtentsRemoved) || !kIsCopyOrMoveAssignable) &&
(kIsCopyOrMoveConstructible || kIsCopyOrMoveAssignable) &&
is_trivially_destructible<ExtentsRemoved>::value &&
!std::is_reference<ExtentsRemoved>::value;
};
template <typename T>
struct is_trivially_copyable
: std::integral_constant<
bool, type_traits_internal::is_trivially_copyable_impl<T>::kValue> {};
}
namespace swap_internal {
using std::swap;
void swap();
template <class T>
using IsSwappableImpl = decltype(swap(std::declval<T&>(), std::declval<T&>()));
template <class T,
class IsNoexcept = std::integral_constant<
bool, noexcept(swap(std::declval<T&>(), std::declval<T&>()))>>
using IsNothrowSwappableImpl = typename std::enable_if<IsNoexcept::value>::type;
template <class T>
struct IsSwappable
: phmap::type_traits_internal::is_detected<IsSwappableImpl, T> {};
template <class T>
struct IsNothrowSwappable
: phmap::type_traits_internal::is_detected<IsNothrowSwappableImpl, T> {};
template <class T, phmap::enable_if_t<IsSwappable<T>::value, int> = 0>
void Swap(T& lhs, T& rhs) noexcept(IsNothrowSwappable<T>::value) {
swap(lhs, rhs);
}
using StdSwapIsUnconstrained = IsSwappable<void()>;
}
namespace type_traits_internal {
using swap_internal::IsNothrowSwappable;
using swap_internal::IsSwappable;
using swap_internal::Swap;
using swap_internal::StdSwapIsUnconstrained;
}
namespace compare_internal {
using value_type = int8_t;
template <typename T>
struct Fail {
static_assert(sizeof(T) < 0, "Only literal `0` is allowed.");
};
template <typename NullPtrT = std::nullptr_t>
struct OnlyLiteralZero {
constexpr OnlyLiteralZero(NullPtrT) noexcept {}
template <
typename T,
typename = typename std::enable_if<
std::is_same<T, std::nullptr_t>::value ||
(std::is_integral<T>::value && !std::is_same<T, int>::value)>::type,
typename = typename Fail<T>::type>
OnlyLiteralZero(T); };
enum class eq : value_type {
equal = 0,
equivalent = equal,
nonequal = 1,
nonequivalent = nonequal,
};
enum class ord : value_type { less = -1, greater = 1 };
enum class ncmp : value_type { unordered = -127 };
#ifdef __cpp_inline_variables
#define PHMAP_COMPARE_INLINE_BASECLASS_DECL(name)
#define PHMAP_COMPARE_INLINE_SUBCLASS_DECL(type, name) \
static const type name;
#define PHMAP_COMPARE_INLINE_INIT(type, name, init) \
inline constexpr type type::name(init)
#else
#define PHMAP_COMPARE_INLINE_BASECLASS_DECL(name) \
static const T name;
#define PHMAP_COMPARE_INLINE_SUBCLASS_DECL(type, name)
#define PHMAP_COMPARE_INLINE_INIT(type, name, init) \
template <typename T> \
const T compare_internal::type##_base<T>::name(init)
#endif
template <typename T>
struct weak_equality_base {
PHMAP_COMPARE_INLINE_BASECLASS_DECL(equivalent)
PHMAP_COMPARE_INLINE_BASECLASS_DECL(nonequivalent)
};
template <typename T>
struct strong_equality_base {
PHMAP_COMPARE_INLINE_BASECLASS_DECL(equal)
PHMAP_COMPARE_INLINE_BASECLASS_DECL(nonequal)
PHMAP_COMPARE_INLINE_BASECLASS_DECL(equivalent)
PHMAP_COMPARE_INLINE_BASECLASS_DECL(nonequivalent)
};
template <typename T>
struct partial_ordering_base {
PHMAP_COMPARE_INLINE_BASECLASS_DECL(less)
PHMAP_COMPARE_INLINE_BASECLASS_DECL(equivalent)
PHMAP_COMPARE_INLINE_BASECLASS_DECL(greater)
PHMAP_COMPARE_INLINE_BASECLASS_DECL(unordered)
};
template <typename T>
struct weak_ordering_base {
PHMAP_COMPARE_INLINE_BASECLASS_DECL(less)
PHMAP_COMPARE_INLINE_BASECLASS_DECL(equivalent)
PHMAP_COMPARE_INLINE_BASECLASS_DECL(greater)
};
template <typename T>
struct strong_ordering_base {
PHMAP_COMPARE_INLINE_BASECLASS_DECL(less)
PHMAP_COMPARE_INLINE_BASECLASS_DECL(equal)
PHMAP_COMPARE_INLINE_BASECLASS_DECL(equivalent)
PHMAP_COMPARE_INLINE_BASECLASS_DECL(greater)
};
}
class weak_equality
: public compare_internal::weak_equality_base<weak_equality> {
explicit constexpr weak_equality(compare_internal::eq v) noexcept
: value_(static_cast<compare_internal::value_type>(v)) {}
friend struct compare_internal::weak_equality_base<weak_equality>;
public:
PHMAP_COMPARE_INLINE_SUBCLASS_DECL(weak_equality, equivalent)
PHMAP_COMPARE_INLINE_SUBCLASS_DECL(weak_equality, nonequivalent)
friend constexpr bool operator==(
weak_equality v, compare_internal::OnlyLiteralZero<>) noexcept {
return v.value_ == 0;
}
friend constexpr bool operator!=(
weak_equality v, compare_internal::OnlyLiteralZero<>) noexcept {
return v.value_ != 0;
}
friend constexpr bool operator==(compare_internal::OnlyLiteralZero<>,
weak_equality v) noexcept {
return 0 == v.value_;
}
friend constexpr bool operator!=(compare_internal::OnlyLiteralZero<>,
weak_equality v) noexcept {
return 0 != v.value_;
}
private:
compare_internal::value_type value_;
};
PHMAP_COMPARE_INLINE_INIT(weak_equality, equivalent,
compare_internal::eq::equivalent);
PHMAP_COMPARE_INLINE_INIT(weak_equality, nonequivalent,
compare_internal::eq::nonequivalent);
class strong_equality
: public compare_internal::strong_equality_base<strong_equality> {
explicit constexpr strong_equality(compare_internal::eq v) noexcept
: value_(static_cast<compare_internal::value_type>(v)) {}
friend struct compare_internal::strong_equality_base<strong_equality>;
public:
PHMAP_COMPARE_INLINE_SUBCLASS_DECL(strong_equality, equal)
PHMAP_COMPARE_INLINE_SUBCLASS_DECL(strong_equality, nonequal)
PHMAP_COMPARE_INLINE_SUBCLASS_DECL(strong_equality, equivalent)
PHMAP_COMPARE_INLINE_SUBCLASS_DECL(strong_equality, nonequivalent)
constexpr operator weak_equality() const noexcept { return value_ == 0 ? weak_equality::equivalent
: weak_equality::nonequivalent;
}
friend constexpr bool operator==(
strong_equality v, compare_internal::OnlyLiteralZero<>) noexcept {
return v.value_ == 0;
}
friend constexpr bool operator!=(
strong_equality v, compare_internal::OnlyLiteralZero<>) noexcept {
return v.value_ != 0;
}
friend constexpr bool operator==(compare_internal::OnlyLiteralZero<>,
strong_equality v) noexcept {
return 0 == v.value_;
}
friend constexpr bool operator!=(compare_internal::OnlyLiteralZero<>,
strong_equality v) noexcept {
return 0 != v.value_;
}
private:
compare_internal::value_type value_;
};
PHMAP_COMPARE_INLINE_INIT(strong_equality, equal, compare_internal::eq::equal);
PHMAP_COMPARE_INLINE_INIT(strong_equality, nonequal,
compare_internal::eq::nonequal);
PHMAP_COMPARE_INLINE_INIT(strong_equality, equivalent,
compare_internal::eq::equivalent);
PHMAP_COMPARE_INLINE_INIT(strong_equality, nonequivalent,
compare_internal::eq::nonequivalent);
class partial_ordering
: public compare_internal::partial_ordering_base<partial_ordering> {
explicit constexpr partial_ordering(compare_internal::eq v) noexcept
: value_(static_cast<compare_internal::value_type>(v)) {}
explicit constexpr partial_ordering(compare_internal::ord v) noexcept
: value_(static_cast<compare_internal::value_type>(v)) {}
explicit constexpr partial_ordering(compare_internal::ncmp v) noexcept
: value_(static_cast<compare_internal::value_type>(v)) {}
friend struct compare_internal::partial_ordering_base<partial_ordering>;
constexpr bool is_ordered() const noexcept {
return value_ !=
compare_internal::value_type(compare_internal::ncmp::unordered);
}
public:
PHMAP_COMPARE_INLINE_SUBCLASS_DECL(partial_ordering, less)
PHMAP_COMPARE_INLINE_SUBCLASS_DECL(partial_ordering, equivalent)
PHMAP_COMPARE_INLINE_SUBCLASS_DECL(partial_ordering, greater)
PHMAP_COMPARE_INLINE_SUBCLASS_DECL(partial_ordering, unordered)
constexpr operator weak_equality() const noexcept { return value_ == 0 ? weak_equality::equivalent
: weak_equality::nonequivalent;
}
friend constexpr bool operator==(
partial_ordering v, compare_internal::OnlyLiteralZero<>) noexcept {
return v.is_ordered() && v.value_ == 0;
}
friend constexpr bool operator!=(
partial_ordering v, compare_internal::OnlyLiteralZero<>) noexcept {
return !v.is_ordered() || v.value_ != 0;
}
friend constexpr bool operator<(
partial_ordering v, compare_internal::OnlyLiteralZero<>) noexcept {
return v.is_ordered() && v.value_ < 0;
}
friend constexpr bool operator<=(
partial_ordering v, compare_internal::OnlyLiteralZero<>) noexcept {
return v.is_ordered() && v.value_ <= 0;
}
friend constexpr bool operator>(
partial_ordering v, compare_internal::OnlyLiteralZero<>) noexcept {
return v.is_ordered() && v.value_ > 0;
}
friend constexpr bool operator>=(
partial_ordering v, compare_internal::OnlyLiteralZero<>) noexcept {
return v.is_ordered() && v.value_ >= 0;
}
friend constexpr bool operator==(compare_internal::OnlyLiteralZero<>,
partial_ordering v) noexcept {
return v.is_ordered() && 0 == v.value_;
}
friend constexpr bool operator!=(compare_internal::OnlyLiteralZero<>,
partial_ordering v) noexcept {
return !v.is_ordered() || 0 != v.value_;
}
friend constexpr bool operator<(compare_internal::OnlyLiteralZero<>,
partial_ordering v) noexcept {
return v.is_ordered() && 0 < v.value_;
}
friend constexpr bool operator<=(compare_internal::OnlyLiteralZero<>,
partial_ordering v) noexcept {
return v.is_ordered() && 0 <= v.value_;
}
friend constexpr bool operator>(compare_internal::OnlyLiteralZero<>,
partial_ordering v) noexcept {
return v.is_ordered() && 0 > v.value_;
}
friend constexpr bool operator>=(compare_internal::OnlyLiteralZero<>,
partial_ordering v) noexcept {
return v.is_ordered() && 0 >= v.value_;
}
private:
compare_internal::value_type value_;
};
PHMAP_COMPARE_INLINE_INIT(partial_ordering, less, compare_internal::ord::less);
PHMAP_COMPARE_INLINE_INIT(partial_ordering, equivalent,
compare_internal::eq::equivalent);
PHMAP_COMPARE_INLINE_INIT(partial_ordering, greater,
compare_internal::ord::greater);
PHMAP_COMPARE_INLINE_INIT(partial_ordering, unordered,
compare_internal::ncmp::unordered);
class weak_ordering
: public compare_internal::weak_ordering_base<weak_ordering> {
explicit constexpr weak_ordering(compare_internal::eq v) noexcept
: value_(static_cast<compare_internal::value_type>(v)) {}
explicit constexpr weak_ordering(compare_internal::ord v) noexcept
: value_(static_cast<compare_internal::value_type>(v)) {}
friend struct compare_internal::weak_ordering_base<weak_ordering>;
public:
PHMAP_COMPARE_INLINE_SUBCLASS_DECL(weak_ordering, less)
PHMAP_COMPARE_INLINE_SUBCLASS_DECL(weak_ordering, equivalent)
PHMAP_COMPARE_INLINE_SUBCLASS_DECL(weak_ordering, greater)
constexpr operator weak_equality() const noexcept { return value_ == 0 ? weak_equality::equivalent
: weak_equality::nonequivalent;
}
constexpr operator partial_ordering() const noexcept { return value_ == 0 ? partial_ordering::equivalent
: (value_ < 0 ? partial_ordering::less
: partial_ordering::greater);
}
friend constexpr bool operator==(
weak_ordering v, compare_internal::OnlyLiteralZero<>) noexcept {
return v.value_ == 0;
}
friend constexpr bool operator!=(
weak_ordering v, compare_internal::OnlyLiteralZero<>) noexcept {
return v.value_ != 0;
}
friend constexpr bool operator<(
weak_ordering v, compare_internal::OnlyLiteralZero<>) noexcept {
return v.value_ < 0;
}
friend constexpr bool operator<=(
weak_ordering v, compare_internal::OnlyLiteralZero<>) noexcept {
return v.value_ <= 0;
}
friend constexpr bool operator>(
weak_ordering v, compare_internal::OnlyLiteralZero<>) noexcept {
return v.value_ > 0;
}
friend constexpr bool operator>=(
weak_ordering v, compare_internal::OnlyLiteralZero<>) noexcept {
return v.value_ >= 0;
}
friend constexpr bool operator==(compare_internal::OnlyLiteralZero<>,
weak_ordering v) noexcept {
return 0 == v.value_;
}
friend constexpr bool operator!=(compare_internal::OnlyLiteralZero<>,
weak_ordering v) noexcept {
return 0 != v.value_;
}
friend constexpr bool operator<(compare_internal::OnlyLiteralZero<>,
weak_ordering v) noexcept {
return 0 < v.value_;
}
friend constexpr bool operator<=(compare_internal::OnlyLiteralZero<>,
weak_ordering v) noexcept {
return 0 <= v.value_;
}
friend constexpr bool operator>(compare_internal::OnlyLiteralZero<>,
weak_ordering v) noexcept {
return 0 > v.value_;
}
friend constexpr bool operator>=(compare_internal::OnlyLiteralZero<>,
weak_ordering v) noexcept {
return 0 >= v.value_;
}
private:
compare_internal::value_type value_;
};
PHMAP_COMPARE_INLINE_INIT(weak_ordering, less, compare_internal::ord::less);
PHMAP_COMPARE_INLINE_INIT(weak_ordering, equivalent,
compare_internal::eq::equivalent);
PHMAP_COMPARE_INLINE_INIT(weak_ordering, greater,
compare_internal::ord::greater);
class strong_ordering
: public compare_internal::strong_ordering_base<strong_ordering> {
explicit constexpr strong_ordering(compare_internal::eq v) noexcept
: value_(static_cast<compare_internal::value_type>(v)) {}
explicit constexpr strong_ordering(compare_internal::ord v) noexcept
: value_(static_cast<compare_internal::value_type>(v)) {}
friend struct compare_internal::strong_ordering_base<strong_ordering>;
public:
PHMAP_COMPARE_INLINE_SUBCLASS_DECL(strong_ordering, less)
PHMAP_COMPARE_INLINE_SUBCLASS_DECL(strong_ordering, equal)
PHMAP_COMPARE_INLINE_SUBCLASS_DECL(strong_ordering, equivalent)
PHMAP_COMPARE_INLINE_SUBCLASS_DECL(strong_ordering, greater)
constexpr operator weak_equality() const noexcept { return value_ == 0 ? weak_equality::equivalent
: weak_equality::nonequivalent;
}
constexpr operator strong_equality() const noexcept { return value_ == 0 ? strong_equality::equal : strong_equality::nonequal;
}
constexpr operator partial_ordering() const noexcept { return value_ == 0 ? partial_ordering::equivalent
: (value_ < 0 ? partial_ordering::less
: partial_ordering::greater);
}
constexpr operator weak_ordering() const noexcept { return value_ == 0
? weak_ordering::equivalent
: (value_ < 0 ? weak_ordering::less : weak_ordering::greater);
}
friend constexpr bool operator==(
strong_ordering v, compare_internal::OnlyLiteralZero<>) noexcept {
return v.value_ == 0;
}
friend constexpr bool operator!=(
strong_ordering v, compare_internal::OnlyLiteralZero<>) noexcept {
return v.value_ != 0;
}
friend constexpr bool operator<(
strong_ordering v, compare_internal::OnlyLiteralZero<>) noexcept {
return v.value_ < 0;
}
friend constexpr bool operator<=(
strong_ordering v, compare_internal::OnlyLiteralZero<>) noexcept {
return v.value_ <= 0;
}
friend constexpr bool operator>(
strong_ordering v, compare_internal::OnlyLiteralZero<>) noexcept {
return v.value_ > 0;
}
friend constexpr bool operator>=(
strong_ordering v, compare_internal::OnlyLiteralZero<>) noexcept {
return v.value_ >= 0;
}
friend constexpr bool operator==(compare_internal::OnlyLiteralZero<>,
strong_ordering v) noexcept {
return 0 == v.value_;
}
friend constexpr bool operator!=(compare_internal::OnlyLiteralZero<>,
strong_ordering v) noexcept {
return 0 != v.value_;
}
friend constexpr bool operator<(compare_internal::OnlyLiteralZero<>,
strong_ordering v) noexcept {
return 0 < v.value_;
}
friend constexpr bool operator<=(compare_internal::OnlyLiteralZero<>,
strong_ordering v) noexcept {
return 0 <= v.value_;
}
friend constexpr bool operator>(compare_internal::OnlyLiteralZero<>,
strong_ordering v) noexcept {
return 0 > v.value_;
}
friend constexpr bool operator>=(compare_internal::OnlyLiteralZero<>,
strong_ordering v) noexcept {
return 0 >= v.value_;
}
private:
compare_internal::value_type value_;
};
PHMAP_COMPARE_INLINE_INIT(strong_ordering, less, compare_internal::ord::less);
PHMAP_COMPARE_INLINE_INIT(strong_ordering, equal, compare_internal::eq::equal);
PHMAP_COMPARE_INLINE_INIT(strong_ordering, equivalent,
compare_internal::eq::equivalent);
PHMAP_COMPARE_INLINE_INIT(strong_ordering, greater,
compare_internal::ord::greater);
#undef PHMAP_COMPARE_INLINE_BASECLASS_DECL
#undef PHMAP_COMPARE_INLINE_SUBCLASS_DECL
#undef PHMAP_COMPARE_INLINE_INIT
namespace compare_internal {
template <typename BoolType,
phmap::enable_if_t<std::is_same<bool, BoolType>::value, int> = 0>
constexpr bool compare_result_as_less_than(const BoolType r) { return r; }
constexpr bool compare_result_as_less_than(const phmap::weak_ordering r) {
return r < 0;
}
template <typename Compare, typename K, typename LK>
constexpr bool do_less_than_comparison(const Compare &compare, const K &x,
const LK &y) {
return compare_result_as_less_than(compare(x, y));
}
template <typename Int,
phmap::enable_if_t<std::is_same<int, Int>::value, int> = 0>
constexpr phmap::weak_ordering compare_result_as_ordering(const Int c) {
return c < 0 ? phmap::weak_ordering::less
: c == 0 ? phmap::weak_ordering::equivalent
: phmap::weak_ordering::greater;
}
constexpr phmap::weak_ordering compare_result_as_ordering(
const phmap::weak_ordering c) {
return c;
}
template <
typename Compare, typename K, typename LK,
phmap::enable_if_t<!std::is_same<bool, phmap::invoke_result_t<
Compare, const K &, const LK &>>::value,
int> = 0>
constexpr phmap::weak_ordering do_three_way_comparison(const Compare &compare,
const K &x, const LK &y) {
return compare_result_as_ordering(compare(x, y));
}
template <
typename Compare, typename K, typename LK,
phmap::enable_if_t<std::is_same<bool, phmap::invoke_result_t<Compare,
const K &, const LK &>>::value,
int> = 0>
constexpr phmap::weak_ordering do_three_way_comparison(const Compare &compare,
const K &x, const LK &y) {
return compare(x, y) ? phmap::weak_ordering::less
: compare(y, x) ? phmap::weak_ordering::greater
: phmap::weak_ordering::equivalent;
}
} }
namespace phmap {
namespace priv {
template <typename Compare, typename T>
using btree_is_key_compare_to =
std::is_convertible<phmap::invoke_result_t<Compare, const T &, const T &>,
phmap::weak_ordering>;
struct StringBtreeDefaultLess {
using is_transparent = void;
StringBtreeDefaultLess() = default;
StringBtreeDefaultLess(std::less<std::string>) {} #if PHMAP_HAVE_STD_STRING_VIEW
StringBtreeDefaultLess(std::less<std::string_view>) {} StringBtreeDefaultLess(phmap::Less<std::string_view>) {}
phmap::weak_ordering operator()(std::string_view lhs,
std::string_view rhs) const {
return compare_internal::compare_result_as_ordering(lhs.compare(rhs));
}
#else
phmap::weak_ordering operator()(std::string lhs,
std::string rhs) const {
return compare_internal::compare_result_as_ordering(lhs.compare(rhs));
}
#endif
};
struct StringBtreeDefaultGreater {
using is_transparent = void;
StringBtreeDefaultGreater() = default;
StringBtreeDefaultGreater(std::greater<std::string>) {} #if PHMAP_HAVE_STD_STRING_VIEW
StringBtreeDefaultGreater(std::greater<std::string_view>) {}
phmap::weak_ordering operator()(std::string_view lhs,
std::string_view rhs) const {
return compare_internal::compare_result_as_ordering(rhs.compare(lhs));
}
#else
phmap::weak_ordering operator()(std::string lhs,
std::string rhs) const {
return compare_internal::compare_result_as_ordering(rhs.compare(lhs));
}
#endif
};
template <typename Compare>
struct key_compare_to_adapter {
using type = Compare;
};
template <>
struct key_compare_to_adapter<std::less<std::string>> {
using type = StringBtreeDefaultLess;
};
template <>
struct key_compare_to_adapter<phmap::Less<std::string>> {
using type = StringBtreeDefaultLess;
};
template <>
struct key_compare_to_adapter<std::greater<std::string>> {
using type = StringBtreeDefaultGreater;
};
#if PHMAP_HAVE_STD_STRING_VIEW
template <>
struct key_compare_to_adapter<std::less<std::string_view>> {
using type = StringBtreeDefaultLess;
};
template <>
struct key_compare_to_adapter<phmap::Less<std::string_view>> {
using type = StringBtreeDefaultLess;
};
template <>
struct key_compare_to_adapter<std::greater<std::string_view>> {
using type = StringBtreeDefaultGreater;
};
#endif
template <typename Key, typename Compare, typename Alloc, int TargetNodeSize,
bool Multi, typename SlotPolicy>
struct common_params {
using key_compare = typename key_compare_to_adapter<Compare>::type;
using is_key_compare_to = btree_is_key_compare_to<key_compare, Key>;
using allocator_type = Alloc;
using key_type = Key;
using size_type = std::make_signed<size_t>::type;
using difference_type = ptrdiff_t;
using is_multi_container = std::integral_constant<bool, Multi>;
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,
kNodeValueSpace =
TargetNodeSize - (sizeof(void *) + 4),
};
using node_count_type =
phmap::conditional_t<(kNodeValueSpace / sizeof(value_type) >
(std::numeric_limits<uint8_t>::max)()),
uint16_t, uint8_t>;
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 <class... Args>
static void construct(Alloc *alloc, slot_type *slot, Args &&... args) {
slot_policy::construct(alloc, slot, std::forward<Args>(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);
}
static void move(Alloc *alloc, slot_type *first, slot_type *last,
slot_type *result) {
slot_policy::move(alloc, first, last, result);
}
};
template <typename Key, typename Data, typename Compare, typename Alloc,
int TargetNodeSize, bool Multi>
struct map_params : common_params<Key, Compare, Alloc, TargetNodeSize, Multi,
phmap::priv::map_slot_policy<Key, Data>> {
using super_type = typename map_params::common_params;
using mapped_type = Data;
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;
struct value_compare : private key_compare {
value_compare() = default;
explicit value_compare(const key_compare &cmp) : key_compare(cmp) {}
template <typename T, typename U>
auto operator()(const T &left, const U &right) const
-> decltype(std::declval<key_compare>()(left.first, right.first)) {
return key_compare::operator()(left.first, right.first);
}
};
using is_map_container = std::true_type;
static const Key &key(const value_type &x) { return x.first; }
static const Key &key(const init_type &x) { return x.first; }
static const Key &key(const slot_type *x) { return slot_policy::key(x); }
static mapped_type &value(value_type *value) { return value->second; }
};
template <typename Key>
struct set_slot_policy {
using slot_type = Key;
using value_type = Key;
using mutable_value_type = Key;
static value_type &element(slot_type *slot) { return *slot; }
static const value_type &element(const slot_type *slot) { return *slot; }
template <typename Alloc, class... Args>
static void construct(Alloc *alloc, slot_type *slot, Args &&... args) {
phmap::allocator_traits<Alloc>::construct(*alloc, slot,
std::forward<Args>(args)...);
}
template <typename Alloc>
static void construct(Alloc *alloc, slot_type *slot, slot_type *other) {
phmap::allocator_traits<Alloc>::construct(*alloc, slot, std::move(*other));
}
template <typename Alloc>
static void destroy(Alloc *alloc, slot_type *slot) {
phmap::allocator_traits<Alloc>::destroy(*alloc, slot);
}
template <typename Alloc>
static void swap(Alloc * , slot_type *a, slot_type *b) {
using std::swap;
swap(*a, *b);
}
template <typename Alloc>
static void move(Alloc * , slot_type *src, slot_type *dest) {
*dest = std::move(*src);
}
template <typename Alloc>
static void move(Alloc *alloc, slot_type *first, slot_type *last,
slot_type *result) {
for (slot_type *src = first, *dest = result; src != last; ++src, ++dest)
move(alloc, src, dest);
}
};
template <typename Key, typename Compare, typename Alloc, int TargetNodeSize,
bool Multi>
struct set_params : common_params<Key, Compare, Alloc, TargetNodeSize, Multi,
set_slot_policy<Key>> {
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;
static const Key &key(const value_type &x) { return x; }
static const Key &key(const slot_type *x) { return *x; }
};
template <typename Compare>
struct upper_bound_adapter {
explicit upper_bound_adapter(const Compare &c) : comp(c) {}
template <typename K, typename LK>
bool operator()(const K &a, const LK &b) const {
return !phmap::compare_internal::compare_result_as_less_than(comp(b, a));
}
private:
Compare comp;
};
enum class MatchKind : uint8_t { kEq, kNe };
template <typename V, bool IsCompareTo>
struct SearchResult {
V value;
MatchKind match;
static constexpr bool HasMatch() { return true; }
bool IsEq() const { return match == MatchKind::kEq; }
};
template <typename V>
struct SearchResult<V, false> {
V value;
static constexpr bool HasMatch() { return false; }
static constexpr bool IsEq() { return false; }
};
template <typename Params>
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;
using use_linear_search = std::integral_constant<
bool,
std::is_arithmetic<key_type>::value &&
(std::is_same<phmap::Less<key_type>, key_compare>::value ||
std::is_same<std::less<key_type>, key_compare>::value ||
std::is_same<std::greater<key_type>, key_compare>::value)>;
~btree_node() = default;
btree_node(btree_node const &) = delete;
btree_node &operator=(btree_node const &) = delete;
constexpr static size_type Alignment() {
static_assert(LeafLayout(1).Alignment() == InternalLayout().Alignment(),
"Alignment of all nodes must be equal.");
return (size_type)InternalLayout().Alignment();
}
protected:
btree_node() = default;
private:
using layout_type = phmap::priv::Layout<btree_node *, field_type,
slot_type, btree_node *>;
constexpr static size_type SizeWithNValues(size_type n) {
return (size_type)layout_type( 1,
4,
(size_t)n,
0)
.AllocSize();
}
constexpr static size_type MinimumOverhead() {
return (size_type)(SizeWithNValues(1) - sizeof(value_type));
}
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),
kNodeValues = kNodeTargetValues >= 3 ? kNodeTargetValues : 3,
kInternalNodeMaxCount = 0,
};
constexpr static layout_type LeafLayout(const int max_values = kNodeValues) {
return layout_type( 1,
4,
(size_t)max_values,
0);
}
constexpr static layout_type InternalLayout() {
return layout_type( 1,
4,
kNodeValues,
kNodeValues + 1);
}
constexpr static size_type LeafSize(const int max_values = kNodeValues) {
return (size_type)LeafLayout(max_values).AllocSize();
}
constexpr static size_type InternalSize() {
return (size_type)InternalLayout().AllocSize();
}
template <size_type N>
inline typename layout_type::template ElementType<N> *GetField() {
assert(N < 3 || !leaf());
return InternalLayout().template Pointer<N>(reinterpret_cast<char *>(this));
}
template <size_type N>
inline const typename layout_type::template ElementType<N> *GetField() const {
assert(N < 3 || !leaf());
return InternalLayout().template Pointer<N>(
reinterpret_cast<const char *>(this));
}
void set_parent(btree_node *p) { *GetField<0>() = p; }
field_type &mutable_count() { return GetField<1>()[2]; }
slot_type *slot(size_type i) { return &GetField<2>()[i]; }
const slot_type *slot(size_type 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_count(field_type v) { GetField<1>()[2] = v; }
void set_max_count(field_type v) { GetField<1>()[3] = v; }
public:
bool leaf() const { return GetField<1>()[3] != kInternalNodeMaxCount; }
field_type position() const { return GetField<1>()[0]; }
field_type start() const { return GetField<1>()[1]; }
field_type count() const { return GetField<1>()[2]; }
field_type max_count() const {
const field_type max_cnt = GetField<1>()[3];
return max_cnt == field_type{kInternalNodeMaxCount}
? field_type{kNodeValues}
: max_cnt;
}
btree_node *parent() const { return *GetField<0>(); }
bool is_root() const { return parent()->leaf(); }
void make_root() {
assert(parent()->is_root());
set_parent(parent()->parent());
}
const key_type &key(size_type i) const { return params_type::key(slot(i)); }
reference value(size_type i) { return params_type::element(slot(i)); }
const_reference value(size_type i) const { return params_type::element(slot(i)); }
btree_node *child(size_type i) const { return GetField<3>()[i]; }
btree_node *&mutable_child(size_type i) { return GetField<3>()[i]; }
void clear_child(size_type i) {
phmap::priv::SanitizerPoisonObject(&mutable_child(i));
}
void set_child(size_type i, btree_node *c) {
phmap::priv::SanitizerUnpoisonObject(&mutable_child(i));
mutable_child(i) = c;
c->set_position((field_type)i);
}
void init_child(int i, btree_node *c) {
set_child(i, c);
c->set_parent(this);
}
template <typename K>
SearchResult<int, is_key_compare_to::value> lower_bound(
const K &k, const key_compare &comp) const {
return use_linear_search::value ? linear_search(k, comp)
: binary_search(k, comp);
}
template <typename K>
int upper_bound(const K &k, const key_compare &comp) const {
auto upper_compare = upper_bound_adapter<key_compare>(comp);
return use_linear_search::value ? linear_search(k, upper_compare).value
: binary_search(k, upper_compare).value;
}
template <typename K, typename Compare>
SearchResult<int, btree_is_key_compare_to<Compare, key_type>::value>
linear_search(const K &k, const Compare &comp) const {
return linear_search_impl(k, 0, count(), comp,
btree_is_key_compare_to<Compare, key_type>());
}
template <typename K, typename Compare>
SearchResult<int, btree_is_key_compare_to<Compare, key_type>::value>
binary_search(const K &k, const Compare &comp) const {
return binary_search_impl(k, 0, count(), comp,
btree_is_key_compare_to<Compare, key_type>());
}
template <typename K, typename Compare>
SearchResult<int, false> linear_search_impl(
const K &k, int s, const int e, const Compare &comp,
std::false_type ) const {
while (s < e) {
if (!comp(key(s), k)) {
break;
}
++s;
}
return {s};
}
template <typename K, typename Compare>
SearchResult<int, true> linear_search_impl(
const K &k, int s, const int e, const Compare &comp,
std::true_type ) const {
while (s < e) {
const phmap::weak_ordering c = comp(key(s), k);
if (c == 0) {
return {s, MatchKind::kEq};
} else if (c > 0) {
break;
}
++s;
}
return {s, MatchKind::kNe};
}
template <typename K, typename Compare>
SearchResult<int, false> binary_search_impl(
const K &k, int s, int e, const Compare &comp,
std::false_type ) const {
while (s != e) {
const int mid = (s + e) >> 1;
if (comp(key(mid), k)) {
s = mid + 1;
} else {
e = mid;
}
}
return {s};
}
template <typename K, typename CompareTo>
SearchResult<int, true> binary_search_impl(
const K &k, int s, int e, const CompareTo &comp,
std::true_type ) const {
if (is_multi_container::value) {
MatchKind exact_match = MatchKind::kNe;
while (s != e) {
const int mid = (s + e) >> 1;
const phmap::weak_ordering c = comp(key(mid), k);
if (c < 0) {
s = mid + 1;
} else {
e = mid;
if (c == 0) {
exact_match = MatchKind::kEq;
}
}
}
return {s, exact_match};
} else { while (s != e) {
const int mid = (s + e) >> 1;
const phmap::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};
}
}
template <typename... Args>
void emplace_value(size_type i, allocator_type *alloc, Args &&... args);
void remove_value(int i, allocator_type *alloc);
void remove_values_ignore_children(int i, size_type to_erase,
allocator_type *alloc);
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);
void split(int insert_position, btree_node *dest, allocator_type *alloc);
void merge(btree_node *sibling, allocator_type *alloc);
void swap(btree_node *src, allocator_type *alloc);
static btree_node *init_leaf(btree_node *n, btree_node *parent,
int max_cnt) {
n->set_parent(parent);
n->set_position(0);
n->set_start(0);
n->set_count(0);
n->set_max_count((field_type)max_cnt);
phmap::priv::SanitizerPoisonMemoryRegion(
n->slot(0), max_cnt * sizeof(slot_type));
return n;
}
static btree_node *init_internal(btree_node *n, btree_node *parent) {
init_leaf(n, parent, kNodeValues);
n->set_max_count(kInternalNodeMaxCount);
phmap::priv::SanitizerPoisonMemoryRegion(
&n->mutable_child(0), (kNodeValues + 1) * sizeof(btree_node *));
return n;
}
void destroy(allocator_type *alloc) {
for (int i = 0; i < count(); ++i) {
value_destroy(i, alloc);
}
}
public:
static bool testonly_uses_linear_node_search() {
return use_linear_search::value;
}
private:
template <typename... Args>
void value_init(const size_type i, allocator_type *alloc, Args &&... args) {
phmap::priv::SanitizerUnpoisonObject(slot(i));
params_type::construct(alloc, slot(i), std::forward<Args>(args)...);
}
void value_destroy(const size_type i, allocator_type *alloc) {
params_type::destroy(alloc, slot(i));
phmap::priv::SanitizerPoisonObject(slot(i));
}
void uninitialized_move_n(const size_type n, const size_type i,
const size_type j, btree_node *x,
allocator_type *alloc) {
phmap::priv::SanitizerUnpoisonMemoryRegion(
x->slot(j), n * sizeof(slot_type));
for (slot_type *src = slot(i), *end = src + n, *dest = x->slot(j);
src != end; ++src, ++dest) {
params_type::construct(alloc, dest, src);
}
}
void value_destroy_n(const size_type i, const size_type n,
allocator_type *alloc) {
for (int j = 0; j < n; ++j) {
value_destroy(i + j, alloc);
}
}
template <typename P>
friend class btree;
template <typename N, typename R, typename P>
friend struct btree_iterator;
friend class BtreeNodePeer;
};
template <typename Node, typename Reference, typename Pointer>
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<Node>::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<normal_node, normal_reference, normal_pointer>;
using const_iterator =
btree_iterator<const_node, const_reference, const_pointer>;
public:
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) {}
btree_iterator(Node *n, int p) : node(n), position(p) {}
template <typename N, typename R, typename P,
phmap::enable_if_t<
std::is_same<btree_iterator<N, R, P>, iterator>::value &&
std::is_same<btree_iterator, const_iterator>::value,
int> = 0>
btree_iterator(const btree_iterator<N, R, P> &x) : node(x.node), position(x.position) {}
private:
template <typename N, typename R, typename P,
phmap::enable_if_t<
std::is_same<btree_iterator<N, R, P>, const_iterator>::value &&
std::is_same<btree_iterator, iterator>::value,
int> = 0>
explicit btree_iterator(const btree_iterator<N, R, P> &x)
: node(const_cast<node_type *>(x.node)), position(x.position) {}
void increment() {
if (node->leaf() && ++position < node->count()) {
return;
}
increment_slow();
}
void increment_slow();
void decrement() {
if (node->leaf() && --position >= 0) {
return;
}
decrement_slow();
}
void decrement_slow();
public:
bool operator==(const const_iterator &x) const {
return node == x.node && position == x.position;
}
bool operator!=(const const_iterator &x) const {
return node != x.node || position != x.position;
}
reference operator*() const {
return node->value(position);
}
pointer operator->() const {
return &node->value(position);
}
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 <typename Params>
friend class btree;
template <typename Tree>
friend class btree_container;
template <typename Tree>
friend class btree_set_container;
template <typename Tree>
friend class btree_map_container;
template <typename Tree>
friend class btree_multiset_container;
template <typename N, typename R, typename P>
friend struct btree_iterator;
template <typename TreeType, typename CheckerType>
friend class base_checker;
const key_type &key() const { return node->key(position); }
slot_type *slot() { return node->slot(position); }
Node *node;
int position;
};
template <typename Params>
class btree {
using node_type = btree_node<Params>;
using is_key_compare_to = typename Params::is_key_compare_to;
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 count = 0;
field_type max_count = node_type::kInternalNodeMaxCount + 1;
#ifdef _MSC_VER
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;
assert(empty_node->parent == empty_node);
return empty_node;
#else
static constexpr EmptyNodeType empty_node(
const_cast<EmptyNodeType *>(&empty_node));
return const_cast<EmptyNodeType *>(&empty_node);
#endif
}
enum {
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 &x) {
leaf_nodes += x.leaf_nodes;
internal_nodes += x.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<node_type, reference, pointer>;
using const_iterator = typename iterator::const_iterator;
using reverse_iterator = std::reverse_iterator<iterator>;
using const_reverse_iterator = std::reverse_iterator<const_iterator>;
using node_handle_type = node_handle<Params, Params, allocator_type>;
using params_type = Params;
using slot_type = typename Params::slot_type;
private:
const value_type &maybe_move_from_iterator(const_iterator x) { return *x; }
value_type &&maybe_move_from_iterator(iterator x) { return std::move(*x); }
template <typename Btree>
void copy_or_move_values_in_order(Btree *x);
constexpr static bool static_assert_validation();
public:
btree(const key_compare &comp, const allocator_type &alloc);
btree(const btree &x);
btree(btree &&x) noexcept
: root_(std::move(x.root_)),
rightmost_(phmap::exchange(x.rightmost_, EmptyNode())),
size_(phmap::exchange(x.size_, 0)) {
x.mutable_root() = EmptyNode();
}
~btree() {
static_assert(static_assert_validation(), "This call must be elided.");
clear();
}
btree &operator=(const btree &x);
btree &operator=(btree &&x) noexcept;
iterator begin() {
return iterator(leftmost(), 0);
}
const_iterator begin() const {
return const_iterator(leftmost(), 0);
}
iterator end() { return iterator(rightmost_, rightmost_->count()); }
const_iterator end() const {
return const_iterator(rightmost_, rightmost_->count());
}
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());
}
template <typename K>
iterator lower_bound(const K &key) {
return internal_end(internal_lower_bound(key));
}
template <typename K>
const_iterator lower_bound(const K &key) const {
return internal_end(internal_lower_bound(key));
}
template <typename K>
iterator upper_bound(const K &key) {
return internal_end(internal_upper_bound(key));
}
template <typename K>
const_iterator upper_bound(const K &key) const {
return internal_end(internal_upper_bound(key));
}
template <typename K>
std::pair<iterator, iterator> equal_range(const K &key) {
return {lower_bound(key), upper_bound(key)};
}
template <typename K>
std::pair<const_iterator, const_iterator> equal_range(const K &key) const {
return {lower_bound(key), upper_bound(key)};
}
template <typename... Args>
std::pair<iterator, bool> insert_unique(const key_type &key, Args &&... args);
template <typename... Args>
std::pair<iterator, bool> insert_hint_unique(iterator position,
const key_type &key,
Args &&... args);
template <typename InputIterator>
void insert_iterator_unique(InputIterator b, InputIterator e);
template <typename ValueType>
iterator insert_multi(const key_type &key, ValueType &&v);
template <typename ValueType>
iterator insert_multi(ValueType &&v) {
return insert_multi(params_type::key(v), std::forward<ValueType>(v));
}
template <typename ValueType>
iterator insert_hint_multi(iterator position, ValueType &&v);
template <typename InputIterator>
void insert_iterator_multi(InputIterator b, InputIterator e);
iterator erase(iterator iter);
std::pair<size_type, iterator> erase(iterator begin, iterator end);
template <typename K>
size_type erase_unique(const K &key);
template <typename K>
size_type erase_multi(const K &key);
template <typename K>
iterator find(const K &key) {
return internal_end(internal_find(key));
}
template <typename K>
const_iterator find(const K &key) const {
return internal_end(internal_find(key));
}
template <typename K>
size_type count_unique(const K &key) const {
const iterator beg = internal_find(key);
if (beg.node == nullptr) {
return 0;
}
return 1;
}
template <typename K>
size_type count_multi(const K &key) const {
const auto range = equal_range(key);
return std::distance(range.first, range.second);
}
void clear();
void swap(btree &x);
const key_compare &key_comp() const noexcept {
return root_.template get<0>();
}
template <typename K, typename LK>
bool compare_keys(const K &x, const LK &y) const {
return compare_internal::compare_result_as_less_than(key_comp()(x, y));
}
value_compare value_comp() const { return value_compare(key_comp()); }
void verify() const;
size_type size() const { return size_; }
size_type max_size() const { return (std::numeric_limits<size_type>::max)(); }
bool empty() const { return size_ == 0; }
size_type height() const {
size_type h = 0;
if (!empty()) {
const node_type *n = root();
do {
++h;
n = n->parent();
} while (n != root());
}
return h;
}
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;
}
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();
}
}
static double average_bytes_per_value() {
return node_type::LeafSize() / (kNodeValues * 0.75);
}
double fullness() const {
if (empty()) return 0.0;
return static_cast<double>(size()) / (nodes() * kNodeValues);
}
double overhead() const {
if (empty()) return 0.0;
return (bytes_used() - size() * sizeof(value_type)) /
static_cast<double>(size());
}
allocator_type get_allocator() const {
return allocator();
}
private:
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>(); }
node_type *leftmost() { return root()->parent(); }
const node_type *leftmost() const { return root()->parent(); }
allocator_type *mutable_allocator() noexcept {
return &root_.template get<1>();
}
const allocator_type &allocator() const noexcept {
return root_.template get<1>();
}
node_type *allocate(const size_type sz) {
return reinterpret_cast<node_type *>(
phmap::priv::Allocate<node_type::Alignment()>(
mutable_allocator(), (size_t)sz));
}
node_type* new_internal_node(node_type *parent) {
node_type *p = allocate(node_type::InternalSize());
return node_type::init_internal(p, parent);
}
node_type* new_leaf_node(node_type *parent) {
node_type *p = allocate(node_type::LeafSize());
return node_type::init_leaf(p, parent, kNodeValues);
}
node_type *new_leaf_root_node(const int max_count) {
node_type *p = allocate(node_type::LeafSize(max_count));
return node_type::init_leaf(p, p, max_count);
}
void erase_same_node(iterator begin, iterator end);
iterator erase_from_leaf_node(iterator begin, size_type to_erase);
iterator rebalance_after_delete(iterator iter);
void deallocate(const size_type sz, node_type *node) {
phmap::priv::Deallocate<node_type::Alignment()>(
mutable_allocator(), node, (size_t)sz);
}
void delete_internal_node(node_type *node) {
node->destroy(mutable_allocator());
deallocate(node_type::InternalSize(), node);
}
void delete_leaf_node(node_type *node) {
node->destroy(mutable_allocator());
deallocate(node_type::LeafSize(node->max_count()), node);
}
void rebalance_or_split(iterator *iter);
void merge_nodes(node_type *left, node_type *right);
bool try_merge_or_rebalance(iterator *iter);
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();
}
template <typename... Args>
iterator internal_emplace(iterator iter, Args &&... args);
template <typename IterType>
static IterType internal_last(IterType iter);
template <typename K>
SearchResult<iterator, is_key_compare_to::value> internal_locate(
const K &key) const;
template <typename K>
SearchResult<iterator, false> internal_locate_impl(
const K &key, std::false_type ) const;
template <typename K>
SearchResult<iterator, true> internal_locate_impl(
const K &key, std::true_type ) const;
template <typename K>
iterator internal_lower_bound(const K &key) const;
template <typename K>
iterator internal_upper_bound(const K &key) const;
template <typename K>
iterator internal_find(const K &key) const;
void internal_clear(node_type *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 {
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 = 0; i <= node->count(); ++i) {
res += internal_stats(node->child(i));
}
return res;
}
public:
static bool testonly_uses_linear_node_search() {
return node_type::testonly_uses_linear_node_search();
}
private:
phmap::priv::CompressedTuple<key_compare, allocator_type,
node_type *>
root_;
node_type *rightmost_;
size_type size_;
};
template <typename P>
template <typename... Args>
inline void btree_node<P>::emplace_value(const size_type i,
allocator_type *alloc,
Args &&... args) {
assert(i <= count());
if (i < count()) {
value_init(count(), alloc, slot(count() - 1));
for (size_type j = count() - 1; j > i; --j)
params_type::move(alloc, slot(j - 1), slot(j));
value_destroy(i, alloc);
}
value_init(i, alloc, std::forward<Args>(args)...);
set_count((field_type)(count() + 1));
if (!leaf() && count() > i + 1) {
for (int j = count(); j > i + 1; --j) {
set_child(j, child(j - 1));
}
clear_child(i + 1);
}
}
template <typename P>
inline void btree_node<P>::remove_value(const int i, allocator_type *alloc) {
if (!leaf() && count() > i + 1) {
assert(child(i + 1)->count() == 0);
for (size_type j = i + 1; j < count(); ++j) {
set_child(j, child(j + 1));
}
clear_child(count());
}
remove_values_ignore_children(i, 1, alloc);
}
template <typename P>
inline void btree_node<P>::remove_values_ignore_children(
int i, size_type to_erase, allocator_type *alloc) {
params_type::move(alloc, slot(i + to_erase), slot(count()), slot(i));
value_destroy_n(count() - to_erase, to_erase, alloc);
set_count((field_type)(count() - to_erase));
}
template <typename P>
void btree_node<P>::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());
value_init(count(), alloc, parent()->slot(position()));
right->uninitialized_move_n(to_move - 1, 0, count() + 1, this, alloc);
params_type::move(alloc, right->slot(to_move - 1),
parent()->slot(position()));
params_type::move(alloc, right->slot(to_move), right->slot(right->count()),
right->slot(0));
right->value_destroy_n(right->count() - to_move, to_move, alloc);
if (!leaf()) {
for (int i = 0; i < to_move; ++i) {
init_child(count() + i + 1, right->child(i));
}
for (int i = 0; i <= right->count() - 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);
}
}
set_count((field_type)(count() + to_move));
right->set_count((field_type)(right->count() - to_move));
}
template <typename P>
void btree_node<P>::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());
if (right->count() >= to_move) {
right->uninitialized_move_n(to_move, right->count() - to_move,
right->count(), right, alloc);
for (slot_type *src = right->slot(right->count() - to_move - 1),
*dest = right->slot(right->count() - 1),
*end = right->slot(0);
src >= end; --src, --dest) {
params_type::move(alloc, src, dest);
}
params_type::move(alloc, parent()->slot(position()),
right->slot(to_move - 1));
params_type::move(alloc, slot(count() - (to_move - 1)), slot(count()),
right->slot(0));
} else {
right->uninitialized_move_n(right->count(), 0, to_move, right, alloc);
right->value_init(to_move - 1, alloc, parent()->slot(position()));
const size_type uninitialized_remaining = to_move - right->count() - 1;
uninitialized_move_n(uninitialized_remaining,
count() - uninitialized_remaining, right->count(),
right, alloc);
params_type::move(alloc, slot(count() - (to_move - 1)),
slot(count() - uninitialized_remaining), right->slot(0));
}
params_type::move(alloc, slot(count() - to_move), parent()->slot(position()));
value_destroy_n(count() - to_move, to_move, alloc);
if (!leaf()) {
for (int i = right->count(); i >= 0; --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(count() - to_move + i));
clear_child(count() - to_move + i);
}
}
set_count((field_type)(count() - to_move));
right->set_count((field_type)(right->count() + to_move));
}
template <typename P>
void btree_node<P>::split(const int insert_position, btree_node *dest,
allocator_type *alloc) {
assert(dest->count() == 0);
assert(max_count() == kNodeValues);
if (insert_position == 0) {
dest->set_count((field_type)(count() - 1));
} else if (insert_position == kNodeValues) {
dest->set_count(0);
} else {
dest->set_count((field_type)(count() / 2));
}
set_count((field_type)(count() - dest->count()));
assert(count() >= 1);
uninitialized_move_n(dest->count(), count(), 0, dest, alloc);
value_destroy_n(count(), dest->count(), alloc);
set_count((field_type)(count() - 1));
parent()->emplace_value(position(), alloc, slot(count()));
value_destroy(count(), alloc);
parent()->init_child(position() + 1, dest);
if (!leaf()) {
for (int i = 0; i <= dest->count(); ++i) {
assert(child(count() + i + 1) != nullptr);
dest->init_child(i, child(count() + i + 1));
clear_child(count() + i + 1);
}
}
}
template <typename P>
void btree_node<P>::merge(btree_node *src, allocator_type *alloc) {
assert(parent() == src->parent());
assert(position() + 1 == src->position());
value_init(count(), alloc, parent()->slot(position()));
src->uninitialized_move_n(src->count(), 0, count() + 1, this, alloc);
src->value_destroy_n(0, src->count(), alloc);
if (!leaf()) {
for (int i = 0; i <= src->count(); ++i) {
init_child(count() + i + 1, src->child(i));
src->clear_child(i);
}
}
set_count((field_type)(1 + count() + src->count()));
src->set_count(0);
parent()->remove_value(position(), alloc);
}
template <typename P>
void btree_node<P>::swap(btree_node *x, allocator_type *alloc) {
using std::swap;
assert(leaf() == x->leaf());
btree_node *smaller = this, *larger = x;
if (smaller->count() > larger->count()) {
swap(smaller, larger);
}
for (slot_type *a = smaller->slot(0), *b = larger->slot(0),
*end = a + smaller->count();
a != end; ++a, ++b) {
params_type::swap(alloc, a, b);
}
const size_type to_move = larger->count() - smaller->count();
larger->uninitialized_move_n(to_move, smaller->count(), smaller->count(),
smaller, alloc);
larger->value_destroy_n(smaller->count(), to_move, alloc);
if (!leaf()) {
std::swap_ranges(&smaller->mutable_child(0),
&smaller->mutable_child(smaller->count() + 1),
&larger->mutable_child(0));
int i = 0;
for (; i <= smaller->count(); ++i) {
smaller->child(i)->set_parent(smaller);
larger->child(i)->set_parent(larger);
}
for (; i <= larger->count(); ++i) {
smaller->init_child(i, larger->child(i));
larger->clear_child(i);
}
}
swap(mutable_count(), x->mutable_count());
}
template <typename N, typename R, typename P>
void btree_iterator<N, R, P>::increment_slow() {
if (node->leaf()) {
assert(position >= node->count());
btree_iterator save(*this);
while (position == node->count() && !node->is_root()) {
assert(node->parent()->child(node->position()) == node);
position = node->position();
node = node->parent();
}
if (position == node->count()) {
*this = save;
}
} else {
assert(position < node->count());
node = node->child(position + 1);
while (!node->leaf()) {
node = node->child(0);
}
position = 0;
}
}
template <typename N, typename R, typename P>
void btree_iterator<N, R, P>::decrement_slow() {
if (node->leaf()) {
assert(position <= -1);
btree_iterator save(*this);
while (position < 0 && !node->is_root()) {
assert(node->parent()->child(node->position()) == node);
position = node->position() - 1;
node = node->parent();
}
if (position < 0) {
*this = save;
}
} else {
assert(position >= 0);
node = node->child(position);
while (!node->leaf()) {
node = node->child(node->count());
}
position = node->count() - 1;
}
}
template <typename P>
template <typename Btree>
void btree<P>::copy_or_move_values_in_order(Btree *x) {
static_assert(std::is_same<btree, Btree>::value ||
std::is_same<const btree, Btree>::value,
"Btree type must be same or const.");
assert(empty());
auto iter = x->begin();
if (iter == x->end()) return;
insert_multi(maybe_move_from_iterator(iter));
++iter;
for (; iter != x->end(); ++iter) {
internal_emplace(end(), maybe_move_from_iterator(iter));
}
}
template <typename P>
constexpr bool btree<P>::static_assert_validation() {
static_assert(std::is_nothrow_copy_constructible<key_compare>::value,
"Key comparison must be nothrow copy constructible");
static_assert(std::is_nothrow_copy_constructible<allocator_type>::value,
"Allocator must be nothrow copy constructible");
static_assert(type_traits_internal::is_trivially_copyable<iterator>::value,
"iterator not trivially copyable.");
static_assert(
kNodeValues < (1 << (8 * sizeof(typename node_type::field_type))),
"target node size too large");
using compare_result_type =
phmap::invoke_result_t<key_compare, key_type, key_type>;
static_assert(
std::is_same<compare_result_type, bool>::value ||
std::is_convertible<compare_result_type, phmap::weak_ordering>::value,
"key comparison function must return phmap::{weak,strong}_ordering or "
"bool.");
static_assert(node_type::MinimumOverhead() >= sizeof(void *) + 4,
"node space assumption incorrect");
return true;
}
template <typename P>
btree<P>::btree(const key_compare &comp, const allocator_type &alloc)
: root_(comp, alloc, EmptyNode()), rightmost_(EmptyNode()), size_(0) {}
template <typename P>
btree<P>::btree(const btree &x) : btree(x.key_comp(), x.allocator()) {
copy_or_move_values_in_order(&x);
}
template <typename P>
template <typename... Args>
auto btree<P>::insert_unique(const key_type &key, Args &&... args)
-> std::pair<iterator, bool> {
if (empty()) {
mutable_root() = rightmost_ = new_leaf_root_node(1);
}
auto res = internal_locate(key);
iterator &iter = res.value;
if (res.HasMatch()) {
if (res.IsEq()) {
return {iter, false};
}
} else {
iterator last = internal_last(iter);
if (last.node && !compare_keys(key, last.key())) {
return {last, false};
}
}
return {internal_emplace(iter, std::forward<Args>(args)...), true};
}
template <typename P>
template <typename... Args>
inline auto btree<P>::insert_hint_unique(iterator position, const key_type &key,
Args &&... args)
-> std::pair<iterator, bool> {
if (!empty()) {
if (position == end() || compare_keys(key, position.key())) {
iterator prev = position;
if (position == begin() || compare_keys((--prev).key(), key)) {
return {internal_emplace(position, std::forward<Args>(args)...), true};
}
} else if (compare_keys(position.key(), key)) {
++position;
if (position == end() || compare_keys(key, position.key())) {
return {internal_emplace(position, std::forward<Args>(args)...), true};
}
} else {
return {position, false};
}
}
return insert_unique(key, std::forward<Args>(args)...);
}
template <typename P>
template <typename InputIterator>
void btree<P>::insert_iterator_unique(InputIterator b, InputIterator e) {
for (; b != e; ++b) {
insert_hint_unique(end(), params_type::key(*b), *b);
}
}
template <typename P>
template <typename ValueType>
auto btree<P>::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<ValueType>(v));
}
template <typename P>
template <typename ValueType>
auto btree<P>::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)) {
iterator prev = position;
if (position == begin() || !compare_keys(key, (--prev).key())) {
return internal_emplace(position, std::forward<ValueType>(v));
}
} else {
iterator next = position;
++next;
if (next == end() || !compare_keys(next.key(), key)) {
return internal_emplace(next, std::forward<ValueType>(v));
}
}
}
return insert_multi(std::forward<ValueType>(v));
}
template <typename P>
template <typename InputIterator>
void btree<P>::insert_iterator_multi(InputIterator b, InputIterator e) {
for (; b != e; ++b) {
insert_hint_multi(end(), *b);
}
}
template <typename P>
auto btree<P>::operator=(const btree &x) -> btree & {
if (this != &x) {
clear();
*mutable_key_comp() = x.key_comp();
if (phmap::allocator_traits<
allocator_type>::propagate_on_container_copy_assignment::value) {
*mutable_allocator() = x.allocator();
}
copy_or_move_values_in_order(&x);
}
return *this;
}
template <typename P>
auto btree<P>::operator=(btree &&x) noexcept -> btree & {
if (this != &x) {
clear();
using std::swap;
if (phmap::allocator_traits<
allocator_type>::propagate_on_container_copy_assignment::value) {
swap(root_, x.root_);
swap(rightmost_, x.rightmost_);
swap(size_, x.size_);
} else {
if (allocator() == x.allocator()) {
swap(mutable_root(), x.mutable_root());
swap(*mutable_key_comp(), *x.mutable_key_comp());
swap(rightmost_, x.rightmost_);
swap(size_, x.size_);
} else {
*mutable_key_comp() = x.key_comp();
copy_or_move_values_in_order(&x);
}
}
}
return *this;
}
template <typename P>
auto btree<P>::erase(iterator iter) -> iterator {
bool internal_delete = false;
if (!iter.node->leaf()) {
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;
}
iter.node->remove_value(iter.position, mutable_allocator());
--size_;
iterator res = rebalance_after_delete(iter);
if (internal_delete) {
++res;
}
return res;
}
template <typename P>
auto btree<P>::rebalance_after_delete(iterator iter) -> iterator {
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);
if (first_iteration) {
res = iter;
first_iteration = false;
}
if (!merged) {
break;
}
iter.position = iter.node->position();
iter.node = iter.node->parent();
}
if (res.position == res.node->count()) {
res.position = res.node->count() - 1;
++res;
}
return res;
}
template <typename P>
auto btree<P>::erase(iterator _begin, iterator _end)
-> std::pair<size_type, iterator> {
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) {
erase_same_node(_begin, _end);
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->count() - _begin.position;
_begin = erase_from_leaf_node(
_begin, (std::min)(remaining_to_erase, remaining_in_node));
} else {
_begin = erase(_begin);
}
}
return {count, _begin};
}
template <typename P>
void btree<P>::erase_same_node(iterator _begin, iterator _end) {
assert(_begin.node == _end.node);
assert(_end.position > _begin.position);
node_type *node = _begin.node;
size_type to_erase = _end.position - _begin.position;
if (!node->leaf()) {
for (size_type i = 0; i < to_erase; ++i) {
internal_clear(node->child(_begin.position + i + 1));
}
for (size_type i = _begin.position + to_erase + 1; i <= node->count(); ++i) {
node->set_child(i - to_erase, node->child(i));
node->clear_child(i);
}
}
node->remove_values_ignore_children(_begin.position, to_erase,
mutable_allocator());
}
template <typename P>
auto btree<P>::erase_from_leaf_node(iterator _begin, size_type to_erase)
-> iterator {
node_type *node = _begin.node;
assert(node->leaf());
assert(node->count() > _begin.position);
assert(_begin.position + to_erase <= node->count());
node->remove_values_ignore_children(_begin.position, to_erase,
mutable_allocator());
size_ -= to_erase;
return rebalance_after_delete(_begin);
}
template <typename P>
template <typename K>
auto btree<P>::erase_unique(const K &key) -> size_type {
const iterator iter = internal_find(key);
if (iter.node == nullptr) {
return 0;
}
erase(iter);
return 1;
}
template <typename P>
template <typename K>
auto btree<P>::erase_multi(const K &key) -> size_type {
const iterator _begin = internal_lower_bound(key);
if (_begin.node == nullptr) {
return 0;
}
const iterator _end = internal_end(internal_upper_bound(key));
return erase(_begin, _end).first;
}
template <typename P>
void btree<P>::clear() {
if (!empty()) {
internal_clear(root());
}
mutable_root() = EmptyNode();
rightmost_ = EmptyNode();
size_ = 0;
}
template <typename P>
void btree<P>::swap(btree &x) {
using std::swap;
if (phmap::allocator_traits<
allocator_type>::propagate_on_container_swap::value) {
swap(root_, x.root_);
} else {
assert(allocator() == x.allocator());
swap(mutable_root(), x.mutable_root());
swap(*mutable_key_comp(), *x.mutable_key_comp());
}
swap(rightmost_, x.rightmost_);
swap(size_, x.size_);
}
template <typename P>
void btree<P>::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()->count())).node);
assert(leftmost()->leaf());
assert(rightmost_->leaf());
}
template <typename P>
void btree<P>::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());
node_type *parent = node->parent();
if (node != root()) {
if (node->position() > 0) {
node_type *left = parent->child(node->position() - 1);
assert(left->max_count() == kNodeValues);
if (left->count() < kNodeValues) {
int to_move = (kNodeValues - left->count()) /
(1 + (insert_position < kNodeValues));
to_move = (std::max)(1, to_move);
if (((insert_position - to_move) >= 0) ||
((left->count() + to_move) < 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 < 0) {
insert_position = insert_position + left->count() + 1;
node = left;
}
assert(node->count() < node->max_count());
return;
}
}
}
if (node->position() < parent->count()) {
node_type *right = parent->child(node->position() + 1);
assert(right->max_count() == kNodeValues);
if (right->count() < kNodeValues) {
int to_move =
(kNodeValues - right->count()) / (1 + (insert_position > 0));
to_move = (std::max)(1, to_move);
if ((insert_position <= (node->count() - to_move)) ||
((right->count() + to_move) < kNodeValues)) {
node->rebalance_left_to_right(to_move, right, mutable_allocator());
if (insert_position > node->count()) {
insert_position = insert_position - node->count() - 1;
node = right;
}
assert(node->count() < node->max_count());
return;
}
}
}
assert(parent->max_count() == kNodeValues);
if (parent->count() == kNodeValues) {
iterator parent_iter(node->parent(), node->position());
rebalance_or_split(&parent_iter);
}
} else {
parent = new_internal_node(parent);
parent->init_child(0, root());
mutable_root() = parent;
assert(!parent->child(0)->leaf() || parent->child(0) == rightmost_);
}
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->count()) {
insert_position = insert_position - node->count() - 1;
node = split_node;
}
}
template <typename P>
void btree<P>::merge_nodes(node_type *left, node_type *right) {
left->merge(right, mutable_allocator());
if (right->leaf()) {
if (rightmost_ == right) rightmost_ = left;
delete_leaf_node(right);
} else {
delete_internal_node(right);
}
}
template <typename P>
bool btree<P>::try_merge_or_rebalance(iterator *iter) {
node_type *parent = iter->node->parent();
if (iter->node->position() > 0) {
node_type *left = parent->child(iter->node->position() - 1);
assert(left->max_count() == kNodeValues);
if ((1 + 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->count()) {
node_type *right = parent->child(iter->node->position() + 1);
assert(right->max_count() == kNodeValues);
if ((1 + iter->node->count() + right->count()) <= kNodeValues) {
merge_nodes(iter->node, right);
return true;
}
if ((right->count() > kMinNodeValues) &&
((iter->node->count() == 0) ||
(iter->position > 0))) {
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() > 0) {
node_type *left = parent->child(iter->node->position() - 1);
if ((left->count() > kMinNodeValues) &&
((iter->node->count() == 0) ||
(iter->position < iter->node->count()))) {
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 <typename P>
void btree<P>::try_shrink() {
if (root()->count() > 0) {
return;
}
if (root()->leaf()) {
assert(size() == 0);
delete_leaf_node(root());
mutable_root() = EmptyNode();
rightmost_ = EmptyNode();
} else {
node_type *child = root()->child(0);
child->make_root();
delete_internal_node(root());
mutable_root() = child;
}
}
template <typename P>
template <typename IterType>
inline IterType btree<P>::internal_last(IterType iter) {
assert(iter.node != nullptr);
while (iter.position == iter.node->count()) {
iter.position = iter.node->position();
iter.node = iter.node->parent();
if (iter.node->leaf()) {
iter.node = nullptr;
break;
}
}
return iter;
}
template <typename P>
template <typename... Args>
inline auto btree<P>::internal_emplace(iterator iter, Args &&... args)
-> iterator {
if (!iter.node->leaf()) {
--iter;
++iter.position;
}
const int max_count = iter.node->max_count();
if (iter.node->count() == max_count) {
if (max_count < kNodeValues) {
assert(iter.node == root());
iter.node =
new_leaf_root_node((std::min<int>)(kNodeValues, 2 * max_count));
iter.node->swap(root(), mutable_allocator());
delete_leaf_node(root());
mutable_root() = iter.node;
rightmost_ = iter.node;
} else {
rebalance_or_split(&iter);
}
}
iter.node->emplace_value(iter.position, mutable_allocator(),
std::forward<Args>(args)...);
++size_;
return iter;
}
template <typename P>
template <typename K>
inline auto btree<P>::internal_locate(const K &key) const
-> SearchResult<iterator, is_key_compare_to::value> {
return internal_locate_impl(key, is_key_compare_to());
}
template <typename P>
template <typename K>
inline auto btree<P>::internal_locate_impl(
const K &key, std::false_type ) const
-> SearchResult<iterator, false> {
iterator iter(const_cast<node_type *>(root()), 0);
for (;;) {
iter.position = iter.node->lower_bound(key, key_comp()).value;
if (iter.node->leaf()) {
break;
}
iter.node = iter.node->child(iter.position);
}
return {iter};
}
template <typename P>
template <typename K>
inline auto btree<P>::internal_locate_impl(
const K &key, std::true_type ) const
-> SearchResult<iterator, true> {
iterator iter(const_cast<node_type *>(root()), 0);
for (;;) {
SearchResult<int, true> res = iter.node->lower_bound(key, key_comp());
iter.position = res.value;
if (res.match == MatchKind::kEq) {
return {iter, MatchKind::kEq};
}
if (iter.node->leaf()) {
break;
}
iter.node = iter.node->child(iter.position);
}
return {iter, MatchKind::kNe};
}
template <typename P>
template <typename K>
auto btree<P>::internal_lower_bound(const K &key) const -> iterator {
iterator iter(const_cast<node_type *>(root()), 0);
for (;;) {
iter.position = iter.node->lower_bound(key, key_comp()).value;
if (iter.node->leaf()) {
break;
}
iter.node = iter.node->child(iter.position);
}
return internal_last(iter);
}
template <typename P>
template <typename K>
auto btree<P>::internal_upper_bound(const K &key) const -> iterator {
iterator iter(const_cast<node_type *>(root()), 0);
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 <typename P>
template <typename K>
auto btree<P>::internal_find(const K &key) const -> iterator {
auto 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 <typename P>
void btree<P>::internal_clear(node_type *node) {
if (!node->leaf()) {
for (int i = 0; i <= node->count(); ++i) {
internal_clear(node->child(i));
}
delete_internal_node(node);
} else {
delete_leaf_node(node);
}
}
template <typename P>
int btree<P>::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(0), *lo));
}
if (hi) {
assert(!compare_keys(*hi, node->key(node->count() - 1)));
}
for (int i = 1; i < node->count(); ++i) {
assert(!compare_keys(node->key(i), node->key(i - 1)));
}
int count = node->count();
if (!node->leaf()) {
for (int i = 0; i <= node->count(); ++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 == 0) ? lo : &node->key(i - 1),
(i == node->count()) ? hi : &node->key(i));
}
}
return count;
}
template <typename Tree>
class btree_container {
using params_type = typename Tree::params_type;
protected:
template <class K>
using key_arg =
typename KeyArg<IsTransparent<typename Tree::key_compare>::value>::
template type<K, typename Tree::key_type>;
public:
using key_type = typename Tree::key_type;
using value_type = typename Tree::value_type;
using size_type = typename Tree::size_type;
using difference_type = typename Tree::difference_type;
using key_compare = typename Tree::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;
btree_container() : tree_(key_compare(), allocator_type()) {}
explicit btree_container(const key_compare &comp,
const allocator_type &alloc = allocator_type())
: tree_(comp, alloc) {}
btree_container(const btree_container &x) = default;
btree_container(btree_container &&x) noexcept = default;
btree_container &operator=(const btree_container &x) = default;
btree_container &operator=(btree_container &&x) noexcept(
std::is_nothrow_move_assignable<Tree>::value) = default;
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(); }
template <typename K = key_type>
iterator find(const key_arg<K> &key) {
return tree_.find(key);
}
template <typename K = key_type>
const_iterator find(const key_arg<K> &key) const { return tree_.find(key); }
template <typename K = key_type>
bool contains(const key_arg<K> &key) const { return find(key) != end(); }
template <typename K = key_type>
iterator lower_bound(const key_arg<K> &key) { return tree_.lower_bound(key); }
template <typename K = key_type>
const_iterator lower_bound(const key_arg<K> &key) const { return tree_.lower_bound(key); }
template <typename K = key_type>
iterator upper_bound(const key_arg<K> &key) { return tree_.upper_bound(key); }
template <typename K = key_type>
const_iterator upper_bound(const key_arg<K> &key) const { return tree_.upper_bound(key); }
template <typename K = key_type>
std::pair<iterator, iterator> equal_range(const key_arg<K> &key) { return tree_.equal_range(key); }
template <typename K = key_type>
std::pair<const_iterator, const_iterator> equal_range(
const key_arg<K> &key) const {
return tree_.equal_range(key);
}
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(iterator(first), iterator(last)).second;
}
node_type extract(iterator position) {
auto node = CommonAccess::Move<node_type>(get_allocator(), position.slot());
erase(position);
return node;
}
node_type extract(const_iterator position) {
return extract(iterator(position));
}
public:
void clear() { tree_.clear(); }
void swap(btree_container &x) { tree_.swap(x.tree_); }
void verify() const { tree_.verify(); }
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); }
allocator_type get_allocator() const { return tree_.get_allocator(); }
key_compare key_comp() const { return tree_.key_comp(); }
value_compare value_comp() const { return tree_.value_comp(); }
template <typename State>
friend State AbslHashValue(State h, const btree_container &b) {
for (const auto &v : b) {
h = State::combine(std::move(h), v);
}
return State::combine(std::move(h), b.size());
}
protected:
Tree tree_;
};
template <typename Tree>
class btree_set_container : public btree_container<Tree> {
using super_type = btree_container<Tree>;
using params_type = typename Tree::params_type;
using init_type = typename params_type::init_type;
using is_key_compare_to = typename params_type::is_key_compare_to;
friend class BtreeNodePeer;
protected:
template <class K>
using key_arg = typename super_type::template key_arg<K>;
public:
using key_type = typename Tree::key_type;
using value_type = typename Tree::value_type;
using size_type = typename Tree::size_type;
using key_compare = typename Tree::key_compare;
using allocator_type = typename Tree::allocator_type;
using iterator = typename Tree::iterator;
using const_iterator = typename Tree::const_iterator;
using node_type = typename super_type::node_type;
using insert_return_type = InsertReturnType<iterator, node_type>;
using super_type::super_type;
btree_set_container() {}
template <class InputIterator>
btree_set_container(InputIterator b, InputIterator e,
const key_compare &comp = key_compare(),
const allocator_type &alloc = allocator_type())
: super_type(comp, alloc) {
insert(b, e);
}
btree_set_container(std::initializer_list<init_type> init,
const key_compare &comp = key_compare(),
const allocator_type &alloc = allocator_type())
: btree_set_container(init.begin(), init.end(), comp, alloc) {}
template <typename K = key_type>
size_type count(const key_arg<K> &key) const {
return this->tree_.count_unique(key);
}
std::pair<iterator, bool> insert(const value_type &x) {
return this->tree_.insert_unique(params_type::key(x), x);
}
std::pair<iterator, bool> insert(value_type &&x) {
return this->tree_.insert_unique(params_type::key(x), std::move(x));
}
template <typename... Args>
std::pair<iterator, bool> emplace(Args &&... args) {
init_type v(std::forward<Args>(args)...);
return this->tree_.insert_unique(params_type::key(v), std::move(v));
}
iterator insert(const_iterator position, const value_type &x) {
return this->tree_
.insert_hint_unique(iterator(position), params_type::key(x), x)
.first;
}
iterator insert(const_iterator position, value_type &&x) {
return this->tree_
.insert_hint_unique(iterator(position), params_type::key(x),
std::move(x))
.first;
}
template <typename... Args>
iterator emplace_hint(const_iterator position, Args &&... args) {
init_type v(std::forward<Args>(args)...);
return this->tree_
.insert_hint_unique(iterator(position), params_type::key(v),
std::move(v))
.first;
}
template <typename InputIterator>
void insert(InputIterator b, InputIterator e) {
this->tree_.insert_iterator_unique(b, e);
}
void insert(std::initializer_list<init_type> init) {
this->tree_.insert_iterator_unique(init.begin(), init.end());
}
insert_return_type insert(node_type &&node) {
if (!node) return {this->end(), false, node_type()};
std::pair<iterator, bool> res =
this->tree_.insert_unique(params_type::key(CommonAccess::GetSlot(node)),
CommonAccess::GetSlot(node));
if (res.second) {
CommonAccess::Destroy(&node);
return {res.first, true, node_type()};
} else {
return {res.first, false, std::move(node)};
}
}
iterator insert(const_iterator hint, node_type &&node) {
if (!node) return this->end();
std::pair<iterator, bool> res = this->tree_.insert_hint_unique(
iterator(hint), params_type::key(CommonAccess::GetSlot(node)),
CommonAccess::GetSlot(node));
if (res.second) CommonAccess::Destroy(&node);
return res.first;
}
template <typename K = key_type>
size_type erase(const key_arg<K> &key) { return this->tree_.erase_unique(key); }
using super_type::erase;
template <typename K = key_type>
node_type extract(const key_arg<K> &key) {
auto it = this->find(key);
return it == this->end() ? node_type() : extract(it);
}
using super_type::extract;
template <
typename T,
typename phmap::enable_if_t<
phmap::conjunction<
std::is_same<value_type, typename T::value_type>,
std::is_same<allocator_type, typename T::allocator_type>,
std::is_same<typename params_type::is_map_container,
typename T::params_type::is_map_container>>::value,
int> = 0>
void merge(btree_container<T> &src) { for (auto src_it = src.begin(); src_it != src.end();) {
if (insert(std::move(*src_it)).second) {
src_it = src.erase(src_it);
} else {
++src_it;
}
}
}
template <
typename T,
typename phmap::enable_if_t<
phmap::conjunction<
std::is_same<value_type, typename T::value_type>,
std::is_same<allocator_type, typename T::allocator_type>,
std::is_same<typename params_type::is_map_container,
typename T::params_type::is_map_container>>::value,
int> = 0>
void merge(btree_container<T> &&src) {
merge(src);
}
};
template <typename Tree>
class btree_map_container : public btree_set_container<Tree> {
using super_type = btree_set_container<Tree>;
using params_type = typename Tree::params_type;
protected:
template <class K>
using key_arg = typename super_type::template key_arg<K>;
public:
using key_type = typename Tree::key_type;
using mapped_type = typename params_type::mapped_type;
using value_type = typename Tree::value_type;
using key_compare = typename Tree::key_compare;
using allocator_type = typename Tree::allocator_type;
using iterator = typename Tree::iterator;
using const_iterator = typename Tree::const_iterator;
using super_type::super_type;
btree_map_container() {}
template <typename... Args>
std::pair<iterator, bool> try_emplace(const key_type &k, Args &&... args) {
return this->tree_.insert_unique(
k, std::piecewise_construct, std::forward_as_tuple(k),
std::forward_as_tuple(std::forward<Args>(args)...));
}
template <typename... Args>
std::pair<iterator, bool> try_emplace(key_type &&k, Args &&... args) {
const key_type& key_ref = k;
return this->tree_.insert_unique(
key_ref, std::piecewise_construct, std::forward_as_tuple(std::move(k)),
std::forward_as_tuple(std::forward<Args>(args)...));
}
template <typename... Args>
iterator try_emplace(const_iterator hint, const key_type &k,
Args &&... args) {
return this->tree_
.insert_hint_unique(iterator(hint), k, std::piecewise_construct,
std::forward_as_tuple(k),
std::forward_as_tuple(std::forward<Args>(args)...))
.first;
}
template <typename... Args>
iterator try_emplace(const_iterator hint, key_type &&k, Args &&... args) {
const key_type& key_ref = k;
return this->tree_
.insert_hint_unique(iterator(hint), key_ref, std::piecewise_construct,
std::forward_as_tuple(std::move(k)),
std::forward_as_tuple(std::forward<Args>(args)...))
.first;
}
mapped_type &operator[](const key_type &k) {
return try_emplace(k).first->second;
}
mapped_type &operator[](key_type &&k) {
return try_emplace(std::move(k)).first->second;
}
template <typename K = key_type>
mapped_type &at(const key_arg<K> &key) {
auto it = this->find(key);
if (it == this->end())
base_internal::ThrowStdOutOfRange("phmap::btree_map::at");
return it->second;
}
template <typename K = key_type>
const mapped_type &at(const key_arg<K> &key) const {
auto it = this->find(key);
if (it == this->end())
base_internal::ThrowStdOutOfRange("phmap::btree_map::at");
return it->second;
}
};
template <typename Tree>
class btree_multiset_container : public btree_container<Tree> {
using super_type = btree_container<Tree>;
using params_type = typename Tree::params_type;
using init_type = typename params_type::init_type;
using is_key_compare_to = typename params_type::is_key_compare_to;
template <class K>
using key_arg = typename super_type::template key_arg<K>;
public:
using key_type = typename Tree::key_type;
using value_type = typename Tree::value_type;
using size_type = typename Tree::size_type;
using key_compare = typename Tree::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 super_type::super_type;
btree_multiset_container() {}
template <class InputIterator>
btree_multiset_container(InputIterator b, InputIterator e,
const key_compare &comp = key_compare(),
const allocator_type &alloc = allocator_type())
: super_type(comp, alloc) {
insert(b, e);
}
btree_multiset_container(std::initializer_list<init_type> init,
const key_compare &comp = key_compare(),
const allocator_type &alloc = allocator_type())
: btree_multiset_container(init.begin(), init.end(), comp, alloc) {}
template <typename K = key_type>
size_type count(const key_arg<K> &key) const {
return this->tree_.count_multi(key);
}
iterator insert(const value_type &x) { return this->tree_.insert_multi(x); }
iterator insert(value_type &&x) {
return this->tree_.insert_multi(std::move(x));
}
iterator insert(const_iterator position, const value_type &x) {
return this->tree_.insert_hint_multi(iterator(position), x);
}
iterator insert(const_iterator position, value_type &&x) {
return this->tree_.insert_hint_multi(iterator(position), std::move(x));
}
template <typename InputIterator>
void insert(InputIterator b, InputIterator e) {
this->tree_.insert_iterator_multi(b, e);
}
void insert(std::initializer_list<init_type> init) {
this->tree_.insert_iterator_multi(init.begin(), init.end());
}
template <typename... Args>
iterator emplace(Args &&... args) {
return this->tree_.insert_multi(init_type(std::forward<Args>(args)...));
}
template <typename... Args>
iterator emplace_hint(const_iterator position, Args &&... args) {
return this->tree_.insert_hint_multi(
iterator(position), init_type(std::forward<Args>(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;
}
template <typename K = key_type>
size_type erase(const key_arg<K> &key) {
return this->tree_.erase_multi(key);
}
using super_type::erase;
template <typename K = key_type>
node_type extract(const key_arg<K> &key) {
auto it = this->find(key);
return it == this->end() ? node_type() : extract(it);
}
using super_type::extract;
template <
typename T,
typename phmap::enable_if_t<
phmap::conjunction<
std::is_same<value_type, typename T::value_type>,
std::is_same<allocator_type, typename T::allocator_type>,
std::is_same<typename params_type::is_map_container,
typename T::params_type::is_map_container>>::value,
int> = 0>
void merge(btree_container<T> &src) { insert(std::make_move_iterator(src.begin()),
std::make_move_iterator(src.end()));
src.clear();
}
template <
typename T,
typename phmap::enable_if_t<
phmap::conjunction<
std::is_same<value_type, typename T::value_type>,
std::is_same<allocator_type, typename T::allocator_type>,
std::is_same<typename params_type::is_map_container,
typename T::params_type::is_map_container>>::value,
int> = 0>
void merge(btree_container<T> &&src) {
merge(src);
}
};
template <typename Tree>
class btree_multimap_container : public btree_multiset_container<Tree> {
using super_type = btree_multiset_container<Tree>;
using params_type = typename Tree::params_type;
public:
using mapped_type = typename params_type::mapped_type;
using super_type::super_type;
btree_multimap_container() {}
};
}
template <typename Key, typename Compare, typename Alloc>
class btree_set : public priv::btree_set_container<
priv::btree<priv::set_params<
Key, Compare, Alloc, 256, false>>>
{
using Base = typename btree_set::btree_set_container;
public:
btree_set() {}
using Base::Base;
using Base::begin;
using Base::cbegin;
using Base::end;
using Base::cend;
using Base::empty;
using Base::max_size;
using Base::size;
using Base::clear;
using Base::erase;
using Base::insert;
using Base::emplace;
using Base::emplace_hint;
using Base::extract;
using Base::merge;
using Base::swap;
using Base::contains;
using Base::count;
using Base::equal_range;
using Base::find;
using Base::get_allocator;
using Base::key_comp;
using Base::value_comp;
};
template <typename K, typename C, typename A>
void swap(btree_set<K, C, A> &x, btree_set<K, C, A> &y) {
return x.swap(y);
}
template <typename K, typename C, typename A, typename Pred>
void erase_if(btree_set<K, C, A> &set, Pred pred) {
for (auto it = set.begin(); it != set.end();) {
if (pred(*it)) {
it = set.erase(it);
} else {
++it;
}
}
}
template <typename Key, typename Compare, typename Alloc>
class btree_multiset : public priv::btree_multiset_container<
priv::btree<priv::set_params<
Key, Compare, Alloc, 256, true>>>
{
using Base = typename btree_multiset::btree_multiset_container;
public:
btree_multiset() {}
using Base::Base;
using Base::begin;
using Base::cbegin;
using Base::end;
using Base::cend;
using Base::empty;
using Base::max_size;
using Base::size;
using Base::clear;
using Base::erase;
using Base::insert;
using Base::emplace;
using Base::emplace_hint;
using Base::extract;
using Base::merge;
using Base::swap;
using Base::contains;
using Base::count;
using Base::equal_range;
using Base::find;
using Base::get_allocator;
using Base::key_comp;
using Base::value_comp;
};
template <typename K, typename C, typename A>
void swap(btree_multiset<K, C, A> &x, btree_multiset<K, C, A> &y) {
return x.swap(y);
}
template <typename K, typename C, typename A, typename Pred>
void erase_if(btree_multiset<K, C, A> &set, Pred pred) {
for (auto it = set.begin(); it != set.end();) {
if (pred(*it)) {
it = set.erase(it);
} else {
++it;
}
}
}
template <typename Key, typename Value, typename Compare, typename Alloc>
class btree_map : public priv::btree_map_container<
priv::btree<priv::map_params<
Key, Value, Compare, Alloc, 256, false>>>
{
using Base = typename btree_map::btree_map_container;
public:
btree_map() {}
using Base::Base;
using Base::begin;
using Base::cbegin;
using Base::end;
using Base::cend;
using Base::empty;
using Base::max_size;
using Base::size;
using Base::clear;
using Base::erase;
using Base::insert;
using Base::emplace;
using Base::emplace_hint;
using Base::try_emplace;
using Base::extract;
using Base::merge;
using Base::swap;
using Base::at;
using Base::contains;
using Base::count;
using Base::equal_range;
using Base::find;
using Base::operator[];
using Base::get_allocator;
using Base::key_comp;
using Base::value_comp;
};
template <typename K, typename V, typename C, typename A>
void swap(btree_map<K, V, C, A> &x, btree_map<K, V, C, A> &y) {
return x.swap(y);
}
template <typename K, typename V, typename C, typename A, typename Pred>
void erase_if(btree_map<K, V, C, A> &map, Pred pred) {
for (auto it = map.begin(); it != map.end();) {
if (pred(*it)) {
it = map.erase(it);
} else {
++it;
}
}
}
template <typename Key, typename Value, typename Compare, typename Alloc>
class btree_multimap : public priv::btree_multimap_container<
priv::btree<priv::map_params<
Key, Value, Compare, Alloc, 256, true>>>
{
using Base = typename btree_multimap::btree_multimap_container;
public:
btree_multimap() {}
using Base::Base;
using Base::begin;
using Base::cbegin;
using Base::end;
using Base::cend;
using Base::empty;
using Base::max_size;
using Base::size;
using Base::clear;
using Base::erase;
using Base::insert;
using Base::emplace;
using Base::emplace_hint;
using Base::extract;
using Base::merge;
using Base::swap;
using Base::contains;
using Base::count;
using Base::equal_range;
using Base::find;
using Base::get_allocator;
using Base::key_comp;
using Base::value_comp;
};
template <typename K, typename V, typename C, typename A>
void swap(btree_multimap<K, V, C, A> &x, btree_multimap<K, V, C, A> &y) {
return x.swap(y);
}
template <typename K, typename V, typename C, typename A, typename Pred>
void erase_if(btree_multimap<K, V, C, A> &map, Pred pred) {
for (auto it = map.begin(); it != map.end();) {
if (pred(*it)) {
it = map.erase(it);
} else {
++it;
}
}
}
}
#ifdef _MSC_VER
#pragma warning(pop)
#endif
#endif