blob: b255984174e6c67e268b38d2ae60a4a4bf06bf99 [file] [log] [blame]
Austin Schuh36244a12019-09-21 17:52:38 -07001// Copyright 2018 The Abseil Authors.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// https://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15// A btree implementation of the STL set and map interfaces. A btree is smaller
16// and generally also faster than STL set/map (refer to the benchmarks below).
17// The red-black tree implementation of STL set/map has an overhead of 3
18// pointers (left, right and parent) plus the node color information for each
19// stored value. So a set<int32_t> consumes 40 bytes for each value stored in
20// 64-bit mode. This btree implementation stores multiple values on fixed
21// size nodes (usually 256 bytes) and doesn't store child pointers for leaf
22// nodes. The result is that a btree_set<int32_t> may use much less memory per
23// stored value. For the random insertion benchmark in btree_bench.cc, a
24// btree_set<int32_t> with node-size of 256 uses 5.1 bytes per stored value.
25//
26// The packing of multiple values on to each node of a btree has another effect
27// besides better space utilization: better cache locality due to fewer cache
28// lines being accessed. Better cache locality translates into faster
29// operations.
30//
31// CAVEATS
32//
33// Insertions and deletions on a btree can cause splitting, merging or
34// rebalancing of btree nodes. And even without these operations, insertions
35// and deletions on a btree will move values around within a node. In both
36// cases, the result is that insertions and deletions can invalidate iterators
37// pointing to values other than the one being inserted/deleted. Therefore, this
38// container does not provide pointer stability. This is notably different from
39// STL set/map which takes care to not invalidate iterators on insert/erase
40// except, of course, for iterators pointing to the value being erased. A
41// partial workaround when erasing is available: erase() returns an iterator
42// pointing to the item just after the one that was erased (or end() if none
43// exists).
44
45#ifndef ABSL_CONTAINER_INTERNAL_BTREE_H_
46#define ABSL_CONTAINER_INTERNAL_BTREE_H_
47
48#include <algorithm>
49#include <cassert>
50#include <cstddef>
51#include <cstdint>
52#include <cstring>
53#include <functional>
54#include <iterator>
55#include <limits>
56#include <new>
57#include <string>
58#include <type_traits>
59#include <utility>
60
61#include "absl/base/macros.h"
62#include "absl/container/internal/common.h"
63#include "absl/container/internal/compressed_tuple.h"
64#include "absl/container/internal/container_memory.h"
65#include "absl/container/internal/layout.h"
66#include "absl/memory/memory.h"
67#include "absl/meta/type_traits.h"
68#include "absl/strings/string_view.h"
69#include "absl/types/compare.h"
70#include "absl/utility/utility.h"
71
72namespace absl {
73namespace container_internal {
74
75// A helper class that indicates if the Compare parameter is a key-compare-to
76// comparator.
77template <typename Compare, typename T>
78using btree_is_key_compare_to =
79 std::is_convertible<absl::result_of_t<Compare(const T &, const T &)>,
80 absl::weak_ordering>;
81
82struct StringBtreeDefaultLess {
83 using is_transparent = void;
84
85 StringBtreeDefaultLess() = default;
86
87 // Compatibility constructor.
88 StringBtreeDefaultLess(std::less<std::string>) {} // NOLINT
89 StringBtreeDefaultLess(std::less<string_view>) {} // NOLINT
90
91 absl::weak_ordering operator()(absl::string_view lhs,
92 absl::string_view rhs) const {
93 return compare_internal::compare_result_as_ordering(lhs.compare(rhs));
94 }
95};
96
97struct StringBtreeDefaultGreater {
98 using is_transparent = void;
99
100 StringBtreeDefaultGreater() = default;
101
102 StringBtreeDefaultGreater(std::greater<std::string>) {} // NOLINT
103 StringBtreeDefaultGreater(std::greater<string_view>) {} // NOLINT
104
105 absl::weak_ordering operator()(absl::string_view lhs,
106 absl::string_view rhs) const {
107 return compare_internal::compare_result_as_ordering(rhs.compare(lhs));
108 }
109};
110
111// A helper class to convert a boolean comparison into a three-way "compare-to"
112// comparison that returns a negative value to indicate less-than, zero to
113// indicate equality and a positive value to indicate greater-than. This helper
114// class is specialized for less<std::string>, greater<std::string>,
115// less<string_view>, and greater<string_view>.
116//
117// key_compare_to_adapter is provided so that btree users
118// automatically get the more efficient compare-to code when using common
119// google string types with common comparison functors.
120// These string-like specializations also turn on heterogeneous lookup by
121// default.
122template <typename Compare>
123struct key_compare_to_adapter {
124 using type = Compare;
125};
126
127template <>
128struct key_compare_to_adapter<std::less<std::string>> {
129 using type = StringBtreeDefaultLess;
130};
131
132template <>
133struct key_compare_to_adapter<std::greater<std::string>> {
134 using type = StringBtreeDefaultGreater;
135};
136
137template <>
138struct key_compare_to_adapter<std::less<absl::string_view>> {
139 using type = StringBtreeDefaultLess;
140};
141
142template <>
143struct key_compare_to_adapter<std::greater<absl::string_view>> {
144 using type = StringBtreeDefaultGreater;
145};
146
147template <typename Key, typename Compare, typename Alloc, int TargetNodeSize,
148 bool Multi, typename SlotPolicy>
149struct common_params {
150 // If Compare is a common comparator for a std::string-like type, then we adapt it
151 // to use heterogeneous lookup and to be a key-compare-to comparator.
152 using key_compare = typename key_compare_to_adapter<Compare>::type;
153 // A type which indicates if we have a key-compare-to functor or a plain old
154 // key-compare functor.
155 using is_key_compare_to = btree_is_key_compare_to<key_compare, Key>;
156
157 using allocator_type = Alloc;
158 using key_type = Key;
159 using size_type = std::make_signed<size_t>::type;
160 using difference_type = ptrdiff_t;
161
162 // True if this is a multiset or multimap.
163 using is_multi_container = std::integral_constant<bool, Multi>;
164
165 using slot_policy = SlotPolicy;
166 using slot_type = typename slot_policy::slot_type;
167 using value_type = typename slot_policy::value_type;
168 using init_type = typename slot_policy::mutable_value_type;
169 using pointer = value_type *;
170 using const_pointer = const value_type *;
171 using reference = value_type &;
172 using const_reference = const value_type &;
173
174 enum {
175 kTargetNodeSize = TargetNodeSize,
176
177 // Upper bound for the available space for values. This is largest for leaf
178 // nodes, which have overhead of at least a pointer + 4 bytes (for storing
179 // 3 field_types and an enum).
180 kNodeValueSpace =
181 TargetNodeSize - /*minimum overhead=*/(sizeof(void *) + 4),
182 };
183
184 // This is an integral type large enough to hold as many
185 // ValueSize-values as will fit a node of TargetNodeSize bytes.
186 using node_count_type =
187 absl::conditional_t<(kNodeValueSpace / sizeof(value_type) >
188 (std::numeric_limits<uint8_t>::max)()),
189 uint16_t, uint8_t>; // NOLINT
190
191 // The following methods are necessary for passing this struct as PolicyTraits
192 // for node_handle and/or are used within btree.
193 static value_type &element(slot_type *slot) {
194 return slot_policy::element(slot);
195 }
196 static const value_type &element(const slot_type *slot) {
197 return slot_policy::element(slot);
198 }
199 template <class... Args>
200 static void construct(Alloc *alloc, slot_type *slot, Args &&... args) {
201 slot_policy::construct(alloc, slot, std::forward<Args>(args)...);
202 }
203 static void construct(Alloc *alloc, slot_type *slot, slot_type *other) {
204 slot_policy::construct(alloc, slot, other);
205 }
206 static void destroy(Alloc *alloc, slot_type *slot) {
207 slot_policy::destroy(alloc, slot);
208 }
209 static void transfer(Alloc *alloc, slot_type *new_slot, slot_type *old_slot) {
210 construct(alloc, new_slot, old_slot);
211 destroy(alloc, old_slot);
212 }
213 static void swap(Alloc *alloc, slot_type *a, slot_type *b) {
214 slot_policy::swap(alloc, a, b);
215 }
216 static void move(Alloc *alloc, slot_type *src, slot_type *dest) {
217 slot_policy::move(alloc, src, dest);
218 }
219 static void move(Alloc *alloc, slot_type *first, slot_type *last,
220 slot_type *result) {
221 slot_policy::move(alloc, first, last, result);
222 }
223};
224
225// A parameters structure for holding the type parameters for a btree_map.
226// Compare and Alloc should be nothrow copy-constructible.
227template <typename Key, typename Data, typename Compare, typename Alloc,
228 int TargetNodeSize, bool Multi>
229struct map_params : common_params<Key, Compare, Alloc, TargetNodeSize, Multi,
230 map_slot_policy<Key, Data>> {
231 using super_type = typename map_params::common_params;
232 using mapped_type = Data;
233 // This type allows us to move keys when it is safe to do so. It is safe
234 // for maps in which value_type and mutable_value_type are layout compatible.
235 using slot_policy = typename super_type::slot_policy;
236 using slot_type = typename super_type::slot_type;
237 using value_type = typename super_type::value_type;
238 using init_type = typename super_type::init_type;
239
240 using key_compare = typename super_type::key_compare;
241 // Inherit from key_compare for empty base class optimization.
242 struct value_compare : private key_compare {
243 value_compare() = default;
244 explicit value_compare(const key_compare &cmp) : key_compare(cmp) {}
245
246 template <typename T, typename U>
247 auto operator()(const T &left, const U &right) const
248 -> decltype(std::declval<key_compare>()(left.first, right.first)) {
249 return key_compare::operator()(left.first, right.first);
250 }
251 };
252 using is_map_container = std::true_type;
253
254 static const Key &key(const value_type &x) { return x.first; }
255 static const Key &key(const init_type &x) { return x.first; }
256 static const Key &key(const slot_type *x) { return slot_policy::key(x); }
257 static mapped_type &value(value_type *value) { return value->second; }
258};
259
260// This type implements the necessary functions from the
261// absl::container_internal::slot_type interface.
262template <typename Key>
263struct set_slot_policy {
264 using slot_type = Key;
265 using value_type = Key;
266 using mutable_value_type = Key;
267
268 static value_type &element(slot_type *slot) { return *slot; }
269 static const value_type &element(const slot_type *slot) { return *slot; }
270
271 template <typename Alloc, class... Args>
272 static void construct(Alloc *alloc, slot_type *slot, Args &&... args) {
273 absl::allocator_traits<Alloc>::construct(*alloc, slot,
274 std::forward<Args>(args)...);
275 }
276
277 template <typename Alloc>
278 static void construct(Alloc *alloc, slot_type *slot, slot_type *other) {
279 absl::allocator_traits<Alloc>::construct(*alloc, slot, std::move(*other));
280 }
281
282 template <typename Alloc>
283 static void destroy(Alloc *alloc, slot_type *slot) {
284 absl::allocator_traits<Alloc>::destroy(*alloc, slot);
285 }
286
287 template <typename Alloc>
288 static void swap(Alloc * /*alloc*/, slot_type *a, slot_type *b) {
289 using std::swap;
290 swap(*a, *b);
291 }
292
293 template <typename Alloc>
294 static void move(Alloc * /*alloc*/, slot_type *src, slot_type *dest) {
295 *dest = std::move(*src);
296 }
297
298 template <typename Alloc>
299 static void move(Alloc *alloc, slot_type *first, slot_type *last,
300 slot_type *result) {
301 for (slot_type *src = first, *dest = result; src != last; ++src, ++dest)
302 move(alloc, src, dest);
303 }
304};
305
306// A parameters structure for holding the type parameters for a btree_set.
307// Compare and Alloc should be nothrow copy-constructible.
308template <typename Key, typename Compare, typename Alloc, int TargetNodeSize,
309 bool Multi>
310struct set_params : common_params<Key, Compare, Alloc, TargetNodeSize, Multi,
311 set_slot_policy<Key>> {
312 using value_type = Key;
313 using slot_type = typename set_params::common_params::slot_type;
314 using value_compare = typename set_params::common_params::key_compare;
315 using is_map_container = std::false_type;
316
317 static const Key &key(const value_type &x) { return x; }
318 static const Key &key(const slot_type *x) { return *x; }
319};
320
321// An adapter class that converts a lower-bound compare into an upper-bound
322// compare. Note: there is no need to make a version of this adapter specialized
323// for key-compare-to functors because the upper-bound (the first value greater
324// than the input) is never an exact match.
325template <typename Compare>
326struct upper_bound_adapter {
327 explicit upper_bound_adapter(const Compare &c) : comp(c) {}
328 template <typename K, typename LK>
329 bool operator()(const K &a, const LK &b) const {
330 // Returns true when a is not greater than b.
331 return !compare_internal::compare_result_as_less_than(comp(b, a));
332 }
333
334 private:
335 Compare comp;
336};
337
338enum class MatchKind : uint8_t { kEq, kNe };
339
340template <typename V, bool IsCompareTo>
341struct SearchResult {
342 V value;
343 MatchKind match;
344
345 static constexpr bool HasMatch() { return true; }
346 bool IsEq() const { return match == MatchKind::kEq; }
347};
348
349// When we don't use CompareTo, `match` is not present.
350// This ensures that callers can't use it accidentally when it provides no
351// useful information.
352template <typename V>
353struct SearchResult<V, false> {
354 V value;
355
356 static constexpr bool HasMatch() { return false; }
357 static constexpr bool IsEq() { return false; }
358};
359
360// A node in the btree holding. The same node type is used for both internal
361// and leaf nodes in the btree, though the nodes are allocated in such a way
362// that the children array is only valid in internal nodes.
363template <typename Params>
364class btree_node {
365 using is_key_compare_to = typename Params::is_key_compare_to;
366 using is_multi_container = typename Params::is_multi_container;
367 using field_type = typename Params::node_count_type;
368 using allocator_type = typename Params::allocator_type;
369 using slot_type = typename Params::slot_type;
370
371 public:
372 using params_type = Params;
373 using key_type = typename Params::key_type;
374 using value_type = typename Params::value_type;
375 using pointer = typename Params::pointer;
376 using const_pointer = typename Params::const_pointer;
377 using reference = typename Params::reference;
378 using const_reference = typename Params::const_reference;
379 using key_compare = typename Params::key_compare;
380 using size_type = typename Params::size_type;
381 using difference_type = typename Params::difference_type;
382
383 // Btree decides whether to use linear node search as follows:
384 // - If the key is arithmetic and the comparator is std::less or
385 // std::greater, choose linear.
386 // - Otherwise, choose binary.
387 // TODO(ezb): Might make sense to add condition(s) based on node-size.
388 using use_linear_search = std::integral_constant<
389 bool,
390 std::is_arithmetic<key_type>::value &&
391 (std::is_same<std::less<key_type>, key_compare>::value ||
392 std::is_same<std::greater<key_type>, key_compare>::value)>;
393
394 // This class is organized by gtl::Layout as if it had the following
395 // structure:
396 // // A pointer to the node's parent.
397 // btree_node *parent;
398 //
399 // // The position of the node in the node's parent.
400 // field_type position;
401 // // The index of the first populated value in `values`.
402 // // TODO(ezb): right now, `start` is always 0. Update insertion/merge
403 // // logic to allow for floating storage within nodes.
404 // field_type start;
405 // // The count of the number of populated values in the node.
406 // field_type count;
407 // // The maximum number of values the node can hold. This is an integer in
408 // // [1, kNodeValues] for root leaf nodes, kNodeValues for non-root leaf
409 // // nodes, and kInternalNodeMaxCount (as a sentinel value) for internal
410 // // nodes (even though there are still kNodeValues values in the node).
411 // // TODO(ezb): make max_count use only 4 bits and record log2(capacity)
412 // // to free extra bits for is_root, etc.
413 // field_type max_count;
414 //
415 // // The array of values. The capacity is `max_count` for leaf nodes and
416 // // kNodeValues for internal nodes. Only the values in
417 // // [start, start + count) have been initialized and are valid.
418 // slot_type values[max_count];
419 //
420 // // The array of child pointers. The keys in children[i] are all less
421 // // than key(i). The keys in children[i + 1] are all greater than key(i).
422 // // There are 0 children for leaf nodes and kNodeValues + 1 children for
423 // // internal nodes.
424 // btree_node *children[kNodeValues + 1];
425 //
426 // This class is only constructed by EmptyNodeType. Normally, pointers to the
427 // layout above are allocated, cast to btree_node*, and de-allocated within
428 // the btree implementation.
429 ~btree_node() = default;
430 btree_node(btree_node const &) = delete;
431 btree_node &operator=(btree_node const &) = delete;
432
433 // Public for EmptyNodeType.
434 constexpr static size_type Alignment() {
435 static_assert(LeafLayout(1).Alignment() == InternalLayout().Alignment(),
436 "Alignment of all nodes must be equal.");
437 return InternalLayout().Alignment();
438 }
439
440 protected:
441 btree_node() = default;
442
443 private:
444 using layout_type = absl::container_internal::Layout<btree_node *, field_type,
445 slot_type, btree_node *>;
446 constexpr static size_type SizeWithNValues(size_type n) {
447 return layout_type(/*parent*/ 1,
448 /*position, start, count, max_count*/ 4,
449 /*values*/ n,
450 /*children*/ 0)
451 .AllocSize();
452 }
453 // A lower bound for the overhead of fields other than values in a leaf node.
454 constexpr static size_type MinimumOverhead() {
455 return SizeWithNValues(1) - sizeof(value_type);
456 }
457
458 // Compute how many values we can fit onto a leaf node taking into account
459 // padding.
460 constexpr static size_type NodeTargetValues(const int begin, const int end) {
461 return begin == end ? begin
462 : SizeWithNValues((begin + end) / 2 + 1) >
463 params_type::kTargetNodeSize
464 ? NodeTargetValues(begin, (begin + end) / 2)
465 : NodeTargetValues((begin + end) / 2 + 1, end);
466 }
467
468 enum {
469 kTargetNodeSize = params_type::kTargetNodeSize,
470 kNodeTargetValues = NodeTargetValues(0, params_type::kTargetNodeSize),
471
472 // We need a minimum of 3 values per internal node in order to perform
473 // splitting (1 value for the two nodes involved in the split and 1 value
474 // propagated to the parent as the delimiter for the split).
475 kNodeValues = kNodeTargetValues >= 3 ? kNodeTargetValues : 3,
476
477 // The node is internal (i.e. is not a leaf node) if and only if `max_count`
478 // has this value.
479 kInternalNodeMaxCount = 0,
480 };
481
482 // Leaves can have less than kNodeValues values.
483 constexpr static layout_type LeafLayout(const int max_values = kNodeValues) {
484 return layout_type(/*parent*/ 1,
485 /*position, start, count, max_count*/ 4,
486 /*values*/ max_values,
487 /*children*/ 0);
488 }
489 constexpr static layout_type InternalLayout() {
490 return layout_type(/*parent*/ 1,
491 /*position, start, count, max_count*/ 4,
492 /*values*/ kNodeValues,
493 /*children*/ kNodeValues + 1);
494 }
495 constexpr static size_type LeafSize(const int max_values = kNodeValues) {
496 return LeafLayout(max_values).AllocSize();
497 }
498 constexpr static size_type InternalSize() {
499 return InternalLayout().AllocSize();
500 }
501
502 // N is the index of the type in the Layout definition.
503 // ElementType<N> is the Nth type in the Layout definition.
504 template <size_type N>
505 inline typename layout_type::template ElementType<N> *GetField() {
506 // We assert that we don't read from values that aren't there.
507 assert(N < 3 || !leaf());
508 return InternalLayout().template Pointer<N>(reinterpret_cast<char *>(this));
509 }
510 template <size_type N>
511 inline const typename layout_type::template ElementType<N> *GetField() const {
512 assert(N < 3 || !leaf());
513 return InternalLayout().template Pointer<N>(
514 reinterpret_cast<const char *>(this));
515 }
516 void set_parent(btree_node *p) { *GetField<0>() = p; }
517 field_type &mutable_count() { return GetField<1>()[2]; }
518 slot_type *slot(int i) { return &GetField<2>()[i]; }
519 const slot_type *slot(int i) const { return &GetField<2>()[i]; }
520 void set_position(field_type v) { GetField<1>()[0] = v; }
521 void set_start(field_type v) { GetField<1>()[1] = v; }
522 void set_count(field_type v) { GetField<1>()[2] = v; }
523 // This method is only called by the node init methods.
524 void set_max_count(field_type v) { GetField<1>()[3] = v; }
525
526 public:
527 // Whether this is a leaf node or not. This value doesn't change after the
528 // node is created.
529 bool leaf() const { return GetField<1>()[3] != kInternalNodeMaxCount; }
530
531 // Getter for the position of this node in its parent.
532 field_type position() const { return GetField<1>()[0]; }
533
534 // Getter for the offset of the first value in the `values` array.
535 field_type start() const { return GetField<1>()[1]; }
536
537 // Getters for the number of values stored in this node.
538 field_type count() const { return GetField<1>()[2]; }
539 field_type max_count() const {
540 // Internal nodes have max_count==kInternalNodeMaxCount.
541 // Leaf nodes have max_count in [1, kNodeValues].
542 const field_type max_count = GetField<1>()[3];
543 return max_count == field_type{kInternalNodeMaxCount}
544 ? field_type{kNodeValues}
545 : max_count;
546 }
547
548 // Getter for the parent of this node.
549 btree_node *parent() const { return *GetField<0>(); }
550 // Getter for whether the node is the root of the tree. The parent of the
551 // root of the tree is the leftmost node in the tree which is guaranteed to
552 // be a leaf.
553 bool is_root() const { return parent()->leaf(); }
554 void make_root() {
555 assert(parent()->is_root());
556 set_parent(parent()->parent());
557 }
558
559 // Getters for the key/value at position i in the node.
560 const key_type &key(int i) const { return params_type::key(slot(i)); }
561 reference value(int i) { return params_type::element(slot(i)); }
562 const_reference value(int i) const { return params_type::element(slot(i)); }
563
564 // Getters/setter for the child at position i in the node.
565 btree_node *child(int i) const { return GetField<3>()[i]; }
566 btree_node *&mutable_child(int i) { return GetField<3>()[i]; }
567 void clear_child(int i) {
568 absl::container_internal::SanitizerPoisonObject(&mutable_child(i));
569 }
570 void set_child(int i, btree_node *c) {
571 absl::container_internal::SanitizerUnpoisonObject(&mutable_child(i));
572 mutable_child(i) = c;
573 c->set_position(i);
574 }
575 void init_child(int i, btree_node *c) {
576 set_child(i, c);
577 c->set_parent(this);
578 }
579
580 // Returns the position of the first value whose key is not less than k.
581 template <typename K>
582 SearchResult<int, is_key_compare_to::value> lower_bound(
583 const K &k, const key_compare &comp) const {
584 return use_linear_search::value ? linear_search(k, comp)
585 : binary_search(k, comp);
586 }
587 // Returns the position of the first value whose key is greater than k.
588 template <typename K>
589 int upper_bound(const K &k, const key_compare &comp) const {
590 auto upper_compare = upper_bound_adapter<key_compare>(comp);
591 return use_linear_search::value ? linear_search(k, upper_compare).value
592 : binary_search(k, upper_compare).value;
593 }
594
595 template <typename K, typename Compare>
596 SearchResult<int, btree_is_key_compare_to<Compare, key_type>::value>
597 linear_search(const K &k, const Compare &comp) const {
598 return linear_search_impl(k, 0, count(), comp,
599 btree_is_key_compare_to<Compare, key_type>());
600 }
601
602 template <typename K, typename Compare>
603 SearchResult<int, btree_is_key_compare_to<Compare, key_type>::value>
604 binary_search(const K &k, const Compare &comp) const {
605 return binary_search_impl(k, 0, count(), comp,
606 btree_is_key_compare_to<Compare, key_type>());
607 }
608
609 // Returns the position of the first value whose key is not less than k using
610 // linear search performed using plain compare.
611 template <typename K, typename Compare>
612 SearchResult<int, false> linear_search_impl(
613 const K &k, int s, const int e, const Compare &comp,
614 std::false_type /* IsCompareTo */) const {
615 while (s < e) {
616 if (!comp(key(s), k)) {
617 break;
618 }
619 ++s;
620 }
621 return {s};
622 }
623
624 // Returns the position of the first value whose key is not less than k using
625 // linear search performed using compare-to.
626 template <typename K, typename Compare>
627 SearchResult<int, true> linear_search_impl(
628 const K &k, int s, const int e, const Compare &comp,
629 std::true_type /* IsCompareTo */) const {
630 while (s < e) {
631 const absl::weak_ordering c = comp(key(s), k);
632 if (c == 0) {
633 return {s, MatchKind::kEq};
634 } else if (c > 0) {
635 break;
636 }
637 ++s;
638 }
639 return {s, MatchKind::kNe};
640 }
641
642 // Returns the position of the first value whose key is not less than k using
643 // binary search performed using plain compare.
644 template <typename K, typename Compare>
645 SearchResult<int, false> binary_search_impl(
646 const K &k, int s, int e, const Compare &comp,
647 std::false_type /* IsCompareTo */) const {
648 while (s != e) {
649 const int mid = (s + e) >> 1;
650 if (comp(key(mid), k)) {
651 s = mid + 1;
652 } else {
653 e = mid;
654 }
655 }
656 return {s};
657 }
658
659 // Returns the position of the first value whose key is not less than k using
660 // binary search performed using compare-to.
661 template <typename K, typename CompareTo>
662 SearchResult<int, true> binary_search_impl(
663 const K &k, int s, int e, const CompareTo &comp,
664 std::true_type /* IsCompareTo */) const {
665 if (is_multi_container::value) {
666 MatchKind exact_match = MatchKind::kNe;
667 while (s != e) {
668 const int mid = (s + e) >> 1;
669 const absl::weak_ordering c = comp(key(mid), k);
670 if (c < 0) {
671 s = mid + 1;
672 } else {
673 e = mid;
674 if (c == 0) {
675 // Need to return the first value whose key is not less than k,
676 // which requires continuing the binary search if this is a
677 // multi-container.
678 exact_match = MatchKind::kEq;
679 }
680 }
681 }
682 return {s, exact_match};
683 } else { // Not a multi-container.
684 while (s != e) {
685 const int mid = (s + e) >> 1;
686 const absl::weak_ordering c = comp(key(mid), k);
687 if (c < 0) {
688 s = mid + 1;
689 } else if (c > 0) {
690 e = mid;
691 } else {
692 return {mid, MatchKind::kEq};
693 }
694 }
695 return {s, MatchKind::kNe};
696 }
697 }
698
699 // Emplaces a value at position i, shifting all existing values and
700 // children at positions >= i to the right by 1.
701 template <typename... Args>
702 void emplace_value(size_type i, allocator_type *alloc, Args &&... args);
703
704 // Removes the value at position i, shifting all existing values and children
705 // at positions > i to the left by 1.
706 void remove_value(int i, allocator_type *alloc);
707
708 // Removes the values at positions [i, i + to_erase), shifting all values
709 // after that range to the left by to_erase. Does not change children at all.
710 void remove_values_ignore_children(int i, int to_erase,
711 allocator_type *alloc);
712
713 // Rebalances a node with its right sibling.
714 void rebalance_right_to_left(int to_move, btree_node *right,
715 allocator_type *alloc);
716 void rebalance_left_to_right(int to_move, btree_node *right,
717 allocator_type *alloc);
718
719 // Splits a node, moving a portion of the node's values to its right sibling.
720 void split(int insert_position, btree_node *dest, allocator_type *alloc);
721
722 // Merges a node with its right sibling, moving all of the values and the
723 // delimiting key in the parent node onto itself.
724 void merge(btree_node *sibling, allocator_type *alloc);
725
726 // Swap the contents of "this" and "src".
727 void swap(btree_node *src, allocator_type *alloc);
728
729 // Node allocation/deletion routines.
730 static btree_node *init_leaf(btree_node *n, btree_node *parent,
731 int max_count) {
732 n->set_parent(parent);
733 n->set_position(0);
734 n->set_start(0);
735 n->set_count(0);
736 n->set_max_count(max_count);
737 absl::container_internal::SanitizerPoisonMemoryRegion(
738 n->slot(0), max_count * sizeof(slot_type));
739 return n;
740 }
741 static btree_node *init_internal(btree_node *n, btree_node *parent) {
742 init_leaf(n, parent, kNodeValues);
743 // Set `max_count` to a sentinel value to indicate that this node is
744 // internal.
745 n->set_max_count(kInternalNodeMaxCount);
746 absl::container_internal::SanitizerPoisonMemoryRegion(
747 &n->mutable_child(0), (kNodeValues + 1) * sizeof(btree_node *));
748 return n;
749 }
750 void destroy(allocator_type *alloc) {
751 for (int i = 0; i < count(); ++i) {
752 value_destroy(i, alloc);
753 }
754 }
755
756 public:
757 // Exposed only for tests.
758 static bool testonly_uses_linear_node_search() {
759 return use_linear_search::value;
760 }
761
762 private:
763 template <typename... Args>
764 void value_init(const size_type i, allocator_type *alloc, Args &&... args) {
765 absl::container_internal::SanitizerUnpoisonObject(slot(i));
766 params_type::construct(alloc, slot(i), std::forward<Args>(args)...);
767 }
768 void value_destroy(const size_type i, allocator_type *alloc) {
769 params_type::destroy(alloc, slot(i));
770 absl::container_internal::SanitizerPoisonObject(slot(i));
771 }
772
773 // Move n values starting at value i in this node into the values starting at
774 // value j in node x.
775 void uninitialized_move_n(const size_type n, const size_type i,
776 const size_type j, btree_node *x,
777 allocator_type *alloc) {
778 absl::container_internal::SanitizerUnpoisonMemoryRegion(
779 x->slot(j), n * sizeof(slot_type));
780 for (slot_type *src = slot(i), *end = src + n, *dest = x->slot(j);
781 src != end; ++src, ++dest) {
782 params_type::construct(alloc, dest, src);
783 }
784 }
785
786 // Destroys a range of n values, starting at index i.
787 void value_destroy_n(const size_type i, const size_type n,
788 allocator_type *alloc) {
789 for (int j = 0; j < n; ++j) {
790 value_destroy(i + j, alloc);
791 }
792 }
793
794 template <typename P>
795 friend class btree;
796 template <typename N, typename R, typename P>
797 friend struct btree_iterator;
798 friend class BtreeNodePeer;
799};
800
801template <typename Node, typename Reference, typename Pointer>
802struct btree_iterator {
803 private:
804 using key_type = typename Node::key_type;
805 using size_type = typename Node::size_type;
806 using params_type = typename Node::params_type;
807
808 using node_type = Node;
809 using normal_node = typename std::remove_const<Node>::type;
810 using const_node = const Node;
811 using normal_pointer = typename params_type::pointer;
812 using normal_reference = typename params_type::reference;
813 using const_pointer = typename params_type::const_pointer;
814 using const_reference = typename params_type::const_reference;
815 using slot_type = typename params_type::slot_type;
816
817 using iterator =
818 btree_iterator<normal_node, normal_reference, normal_pointer>;
819 using const_iterator =
820 btree_iterator<const_node, const_reference, const_pointer>;
821
822 public:
823 // These aliases are public for std::iterator_traits.
824 using difference_type = typename Node::difference_type;
825 using value_type = typename params_type::value_type;
826 using pointer = Pointer;
827 using reference = Reference;
828 using iterator_category = std::bidirectional_iterator_tag;
829
830 btree_iterator() : node(nullptr), position(-1) {}
831 btree_iterator(Node *n, int p) : node(n), position(p) {}
832
833 // NOTE: this SFINAE allows for implicit conversions from iterator to
834 // const_iterator, but it specifically avoids defining copy constructors so
835 // that btree_iterator can be trivially copyable. This is for performance and
836 // binary size reasons.
837 template <typename N, typename R, typename P,
838 absl::enable_if_t<
839 std::is_same<btree_iterator<N, R, P>, iterator>::value &&
840 std::is_same<btree_iterator, const_iterator>::value,
841 int> = 0>
842 btree_iterator(const btree_iterator<N, R, P> &x) // NOLINT
843 : node(x.node), position(x.position) {}
844
845 private:
846 // This SFINAE allows explicit conversions from const_iterator to
847 // iterator, but also avoids defining a copy constructor.
848 // NOTE: the const_cast is safe because this constructor is only called by
849 // non-const methods and the container owns the nodes.
850 template <typename N, typename R, typename P,
851 absl::enable_if_t<
852 std::is_same<btree_iterator<N, R, P>, const_iterator>::value &&
853 std::is_same<btree_iterator, iterator>::value,
854 int> = 0>
855 explicit btree_iterator(const btree_iterator<N, R, P> &x)
856 : node(const_cast<node_type *>(x.node)), position(x.position) {}
857
858 // Increment/decrement the iterator.
859 void increment() {
860 if (node->leaf() && ++position < node->count()) {
861 return;
862 }
863 increment_slow();
864 }
865 void increment_slow();
866
867 void decrement() {
868 if (node->leaf() && --position >= 0) {
869 return;
870 }
871 decrement_slow();
872 }
873 void decrement_slow();
874
875 public:
876 bool operator==(const const_iterator &x) const {
877 return node == x.node && position == x.position;
878 }
879 bool operator!=(const const_iterator &x) const {
880 return node != x.node || position != x.position;
881 }
882
883 // Accessors for the key/value the iterator is pointing at.
884 reference operator*() const {
885 return node->value(position);
886 }
887 pointer operator->() const {
888 return &node->value(position);
889 }
890
891 btree_iterator& operator++() {
892 increment();
893 return *this;
894 }
895 btree_iterator& operator--() {
896 decrement();
897 return *this;
898 }
899 btree_iterator operator++(int) {
900 btree_iterator tmp = *this;
901 ++*this;
902 return tmp;
903 }
904 btree_iterator operator--(int) {
905 btree_iterator tmp = *this;
906 --*this;
907 return tmp;
908 }
909
910 private:
911 template <typename Params>
912 friend class btree;
913 template <typename Tree>
914 friend class btree_container;
915 template <typename Tree>
916 friend class btree_set_container;
917 template <typename Tree>
918 friend class btree_map_container;
919 template <typename Tree>
920 friend class btree_multiset_container;
921 template <typename N, typename R, typename P>
922 friend struct btree_iterator;
923 template <typename TreeType, typename CheckerType>
924 friend class base_checker;
925
926 const key_type &key() const { return node->key(position); }
927 slot_type *slot() { return node->slot(position); }
928
929 // The node in the tree the iterator is pointing at.
930 Node *node;
931 // The position within the node of the tree the iterator is pointing at.
932 // TODO(ezb): make this a field_type
933 int position;
934};
935
936template <typename Params>
937class btree {
938 using node_type = btree_node<Params>;
939 using is_key_compare_to = typename Params::is_key_compare_to;
940
941 // We use a static empty node for the root/leftmost/rightmost of empty btrees
942 // in order to avoid branching in begin()/end().
943 struct alignas(node_type::Alignment()) EmptyNodeType : node_type {
944 using field_type = typename node_type::field_type;
945 node_type *parent;
946 field_type position = 0;
947 field_type start = 0;
948 field_type count = 0;
949 // max_count must be != kInternalNodeMaxCount (so that this node is regarded
950 // as a leaf node). max_count() is never called when the tree is empty.
951 field_type max_count = node_type::kInternalNodeMaxCount + 1;
952
953#ifdef _MSC_VER
954 // MSVC has constexpr code generations bugs here.
955 EmptyNodeType() : parent(this) {}
956#else
957 constexpr EmptyNodeType(node_type *p) : parent(p) {}
958#endif
959 };
960
961 static node_type *EmptyNode() {
962#ifdef _MSC_VER
963 static EmptyNodeType* empty_node = new EmptyNodeType;
964 // This assert fails on some other construction methods.
965 assert(empty_node->parent == empty_node);
966 return empty_node;
967#else
968 static constexpr EmptyNodeType empty_node(
969 const_cast<EmptyNodeType *>(&empty_node));
970 return const_cast<EmptyNodeType *>(&empty_node);
971#endif
972 }
973
974 enum {
975 kNodeValues = node_type::kNodeValues,
976 kMinNodeValues = kNodeValues / 2,
977 };
978
979 struct node_stats {
980 using size_type = typename Params::size_type;
981
982 node_stats(size_type l, size_type i)
983 : leaf_nodes(l),
984 internal_nodes(i) {
985 }
986
987 node_stats& operator+=(const node_stats &x) {
988 leaf_nodes += x.leaf_nodes;
989 internal_nodes += x.internal_nodes;
990 return *this;
991 }
992
993 size_type leaf_nodes;
994 size_type internal_nodes;
995 };
996
997 public:
998 using key_type = typename Params::key_type;
999 using value_type = typename Params::value_type;
1000 using size_type = typename Params::size_type;
1001 using difference_type = typename Params::difference_type;
1002 using key_compare = typename Params::key_compare;
1003 using value_compare = typename Params::value_compare;
1004 using allocator_type = typename Params::allocator_type;
1005 using reference = typename Params::reference;
1006 using const_reference = typename Params::const_reference;
1007 using pointer = typename Params::pointer;
1008 using const_pointer = typename Params::const_pointer;
1009 using iterator = btree_iterator<node_type, reference, pointer>;
1010 using const_iterator = typename iterator::const_iterator;
1011 using reverse_iterator = std::reverse_iterator<iterator>;
1012 using const_reverse_iterator = std::reverse_iterator<const_iterator>;
1013 using node_handle_type = node_handle<Params, Params, allocator_type>;
1014
1015 // Internal types made public for use by btree_container types.
1016 using params_type = Params;
1017 using slot_type = typename Params::slot_type;
1018
1019 private:
1020 // For use in copy_or_move_values_in_order.
1021 const value_type &maybe_move_from_iterator(const_iterator x) { return *x; }
1022 value_type &&maybe_move_from_iterator(iterator x) { return std::move(*x); }
1023
1024 // Copies or moves (depending on the template parameter) the values in
1025 // x into this btree in their order in x. This btree must be empty before this
1026 // method is called. This method is used in copy construction, copy
1027 // assignment, and move assignment.
1028 template <typename Btree>
1029 void copy_or_move_values_in_order(Btree *x);
1030
1031 // Validates that various assumptions/requirements are true at compile time.
1032 constexpr static bool static_assert_validation();
1033
1034 public:
1035 btree(const key_compare &comp, const allocator_type &alloc);
1036
1037 btree(const btree &x);
1038 btree(btree &&x) noexcept
1039 : root_(std::move(x.root_)),
1040 rightmost_(absl::exchange(x.rightmost_, EmptyNode())),
1041 size_(absl::exchange(x.size_, 0)) {
1042 x.mutable_root() = EmptyNode();
1043 }
1044
1045 ~btree() {
1046 // Put static_asserts in destructor to avoid triggering them before the type
1047 // is complete.
1048 static_assert(static_assert_validation(), "This call must be elided.");
1049 clear();
1050 }
1051
1052 // Assign the contents of x to *this.
1053 btree &operator=(const btree &x);
1054 btree &operator=(btree &&x) noexcept;
1055
1056 iterator begin() {
1057 return iterator(leftmost(), 0);
1058 }
1059 const_iterator begin() const {
1060 return const_iterator(leftmost(), 0);
1061 }
1062 iterator end() { return iterator(rightmost_, rightmost_->count()); }
1063 const_iterator end() const {
1064 return const_iterator(rightmost_, rightmost_->count());
1065 }
1066 reverse_iterator rbegin() {
1067 return reverse_iterator(end());
1068 }
1069 const_reverse_iterator rbegin() const {
1070 return const_reverse_iterator(end());
1071 }
1072 reverse_iterator rend() {
1073 return reverse_iterator(begin());
1074 }
1075 const_reverse_iterator rend() const {
1076 return const_reverse_iterator(begin());
1077 }
1078
1079 // Finds the first element whose key is not less than key.
1080 template <typename K>
1081 iterator lower_bound(const K &key) {
1082 return internal_end(internal_lower_bound(key));
1083 }
1084 template <typename K>
1085 const_iterator lower_bound(const K &key) const {
1086 return internal_end(internal_lower_bound(key));
1087 }
1088
1089 // Finds the first element whose key is greater than key.
1090 template <typename K>
1091 iterator upper_bound(const K &key) {
1092 return internal_end(internal_upper_bound(key));
1093 }
1094 template <typename K>
1095 const_iterator upper_bound(const K &key) const {
1096 return internal_end(internal_upper_bound(key));
1097 }
1098
1099 // Finds the range of values which compare equal to key. The first member of
1100 // the returned pair is equal to lower_bound(key). The second member pair of
1101 // the pair is equal to upper_bound(key).
1102 template <typename K>
1103 std::pair<iterator, iterator> equal_range(const K &key) {
1104 return {lower_bound(key), upper_bound(key)};
1105 }
1106 template <typename K>
1107 std::pair<const_iterator, const_iterator> equal_range(const K &key) const {
1108 return {lower_bound(key), upper_bound(key)};
1109 }
1110
1111 // Inserts a value into the btree only if it does not already exist. The
1112 // boolean return value indicates whether insertion succeeded or failed.
1113 // Requirement: if `key` already exists in the btree, does not consume `args`.
1114 // Requirement: `key` is never referenced after consuming `args`.
1115 template <typename... Args>
1116 std::pair<iterator, bool> insert_unique(const key_type &key, Args &&... args);
1117
1118 // Inserts with hint. Checks to see if the value should be placed immediately
1119 // before `position` in the tree. If so, then the insertion will take
1120 // amortized constant time. If not, the insertion will take amortized
1121 // logarithmic time as if a call to insert_unique() were made.
1122 // Requirement: if `key` already exists in the btree, does not consume `args`.
1123 // Requirement: `key` is never referenced after consuming `args`.
1124 template <typename... Args>
1125 std::pair<iterator, bool> insert_hint_unique(iterator position,
1126 const key_type &key,
1127 Args &&... args);
1128
1129 // Insert a range of values into the btree.
1130 template <typename InputIterator>
1131 void insert_iterator_unique(InputIterator b, InputIterator e);
1132
1133 // Inserts a value into the btree.
1134 template <typename ValueType>
1135 iterator insert_multi(const key_type &key, ValueType &&v);
1136
1137 // Inserts a value into the btree.
1138 template <typename ValueType>
1139 iterator insert_multi(ValueType &&v) {
1140 return insert_multi(params_type::key(v), std::forward<ValueType>(v));
1141 }
1142
1143 // Insert with hint. Check to see if the value should be placed immediately
1144 // before position in the tree. If it does, then the insertion will take
1145 // amortized constant time. If not, the insertion will take amortized
1146 // logarithmic time as if a call to insert_multi(v) were made.
1147 template <typename ValueType>
1148 iterator insert_hint_multi(iterator position, ValueType &&v);
1149
1150 // Insert a range of values into the btree.
1151 template <typename InputIterator>
1152 void insert_iterator_multi(InputIterator b, InputIterator e);
1153
1154 // Erase the specified iterator from the btree. The iterator must be valid
1155 // (i.e. not equal to end()). Return an iterator pointing to the node after
1156 // the one that was erased (or end() if none exists).
1157 // Requirement: does not read the value at `*iter`.
1158 iterator erase(iterator iter);
1159
1160 // Erases range. Returns the number of keys erased and an iterator pointing
1161 // to the element after the last erased element.
1162 std::pair<size_type, iterator> erase(iterator begin, iterator end);
1163
1164 // Erases the specified key from the btree. Returns 1 if an element was
1165 // erased and 0 otherwise.
1166 template <typename K>
1167 size_type erase_unique(const K &key);
1168
1169 // Erases all of the entries matching the specified key from the
1170 // btree. Returns the number of elements erased.
1171 template <typename K>
1172 size_type erase_multi(const K &key);
1173
1174 // Finds the iterator corresponding to a key or returns end() if the key is
1175 // not present.
1176 template <typename K>
1177 iterator find(const K &key) {
1178 return internal_end(internal_find(key));
1179 }
1180 template <typename K>
1181 const_iterator find(const K &key) const {
1182 return internal_end(internal_find(key));
1183 }
1184
1185 // Returns a count of the number of times the key appears in the btree.
1186 template <typename K>
1187 size_type count_unique(const K &key) const {
1188 const iterator begin = internal_find(key);
1189 if (begin.node == nullptr) {
1190 // The key doesn't exist in the tree.
1191 return 0;
1192 }
1193 return 1;
1194 }
1195 // Returns a count of the number of times the key appears in the btree.
1196 template <typename K>
1197 size_type count_multi(const K &key) const {
1198 const auto range = equal_range(key);
1199 return std::distance(range.first, range.second);
1200 }
1201
1202 // Clear the btree, deleting all of the values it contains.
1203 void clear();
1204
1205 // Swap the contents of *this and x.
1206 void swap(btree &x);
1207
1208 const key_compare &key_comp() const noexcept {
1209 return root_.template get<0>();
1210 }
1211 template <typename K, typename LK>
1212 bool compare_keys(const K &x, const LK &y) const {
1213 return compare_internal::compare_result_as_less_than(key_comp()(x, y));
1214 }
1215
1216 value_compare value_comp() const { return value_compare(key_comp()); }
1217
1218 // Verifies the structure of the btree.
1219 void verify() const;
1220
1221 // Size routines.
1222 size_type size() const { return size_; }
1223 size_type max_size() const { return (std::numeric_limits<size_type>::max)(); }
1224 bool empty() const { return size_ == 0; }
1225
1226 // The height of the btree. An empty tree will have height 0.
1227 size_type height() const {
1228 size_type h = 0;
1229 if (root()) {
1230 // Count the length of the chain from the leftmost node up to the
1231 // root. We actually count from the root back around to the level below
1232 // the root, but the calculation is the same because of the circularity
1233 // of that traversal.
1234 const node_type *n = root();
1235 do {
1236 ++h;
1237 n = n->parent();
1238 } while (n != root());
1239 }
1240 return h;
1241 }
1242
1243 // The number of internal, leaf and total nodes used by the btree.
1244 size_type leaf_nodes() const {
1245 return internal_stats(root()).leaf_nodes;
1246 }
1247 size_type internal_nodes() const {
1248 return internal_stats(root()).internal_nodes;
1249 }
1250 size_type nodes() const {
1251 node_stats stats = internal_stats(root());
1252 return stats.leaf_nodes + stats.internal_nodes;
1253 }
1254
1255 // The total number of bytes used by the btree.
1256 size_type bytes_used() const {
1257 node_stats stats = internal_stats(root());
1258 if (stats.leaf_nodes == 1 && stats.internal_nodes == 0) {
1259 return sizeof(*this) +
1260 node_type::LeafSize(root()->max_count());
1261 } else {
1262 return sizeof(*this) +
1263 stats.leaf_nodes * node_type::LeafSize() +
1264 stats.internal_nodes * node_type::InternalSize();
1265 }
1266 }
1267
1268 // The average number of bytes used per value stored in the btree.
1269 static double average_bytes_per_value() {
1270 // Returns the number of bytes per value on a leaf node that is 75%
1271 // full. Experimentally, this matches up nicely with the computed number of
1272 // bytes per value in trees that had their values inserted in random order.
1273 return node_type::LeafSize() / (kNodeValues * 0.75);
1274 }
1275
1276 // The fullness of the btree. Computed as the number of elements in the btree
1277 // divided by the maximum number of elements a tree with the current number
1278 // of nodes could hold. A value of 1 indicates perfect space
1279 // utilization. Smaller values indicate space wastage.
1280 double fullness() const {
1281 return static_cast<double>(size()) / (nodes() * kNodeValues);
1282 }
1283 // The overhead of the btree structure in bytes per node. Computed as the
1284 // total number of bytes used by the btree minus the number of bytes used for
1285 // storing elements divided by the number of elements.
1286 double overhead() const {
1287 if (empty()) {
1288 return 0.0;
1289 }
1290 return (bytes_used() - size() * sizeof(value_type)) /
1291 static_cast<double>(size());
1292 }
1293
1294 // The allocator used by the btree.
1295 allocator_type get_allocator() const {
1296 return allocator();
1297 }
1298
1299 private:
1300 // Internal accessor routines.
1301 node_type *root() { return root_.template get<2>(); }
1302 const node_type *root() const { return root_.template get<2>(); }
1303 node_type *&mutable_root() noexcept { return root_.template get<2>(); }
1304 key_compare *mutable_key_comp() noexcept { return &root_.template get<0>(); }
1305
1306 // The leftmost node is stored as the parent of the root node.
1307 node_type *leftmost() { return root()->parent(); }
1308 const node_type *leftmost() const { return root()->parent(); }
1309
1310 // Allocator routines.
1311 allocator_type *mutable_allocator() noexcept {
1312 return &root_.template get<1>();
1313 }
1314 const allocator_type &allocator() const noexcept {
1315 return root_.template get<1>();
1316 }
1317
1318 // Allocates a correctly aligned node of at least size bytes using the
1319 // allocator.
1320 node_type *allocate(const size_type size) {
1321 return reinterpret_cast<node_type *>(
1322 absl::container_internal::Allocate<node_type::Alignment()>(
1323 mutable_allocator(), size));
1324 }
1325
1326 // Node creation/deletion routines.
1327 node_type* new_internal_node(node_type *parent) {
1328 node_type *p = allocate(node_type::InternalSize());
1329 return node_type::init_internal(p, parent);
1330 }
1331 node_type* new_leaf_node(node_type *parent) {
1332 node_type *p = allocate(node_type::LeafSize());
1333 return node_type::init_leaf(p, parent, kNodeValues);
1334 }
1335 node_type *new_leaf_root_node(const int max_count) {
1336 node_type *p = allocate(node_type::LeafSize(max_count));
1337 return node_type::init_leaf(p, p, max_count);
1338 }
1339
1340 // Deletion helper routines.
1341 void erase_same_node(iterator begin, iterator end);
1342 iterator erase_from_leaf_node(iterator begin, size_type to_erase);
1343 iterator rebalance_after_delete(iterator iter);
1344
1345 // Deallocates a node of a certain size in bytes using the allocator.
1346 void deallocate(const size_type size, node_type *node) {
1347 absl::container_internal::Deallocate<node_type::Alignment()>(
1348 mutable_allocator(), node, size);
1349 }
1350
1351 void delete_internal_node(node_type *node) {
1352 node->destroy(mutable_allocator());
1353 deallocate(node_type::InternalSize(), node);
1354 }
1355 void delete_leaf_node(node_type *node) {
1356 node->destroy(mutable_allocator());
1357 deallocate(node_type::LeafSize(node->max_count()), node);
1358 }
1359
1360 // Rebalances or splits the node iter points to.
1361 void rebalance_or_split(iterator *iter);
1362
1363 // Merges the values of left, right and the delimiting key on their parent
1364 // onto left, removing the delimiting key and deleting right.
1365 void merge_nodes(node_type *left, node_type *right);
1366
1367 // Tries to merge node with its left or right sibling, and failing that,
1368 // rebalance with its left or right sibling. Returns true if a merge
1369 // occurred, at which point it is no longer valid to access node. Returns
1370 // false if no merging took place.
1371 bool try_merge_or_rebalance(iterator *iter);
1372
1373 // Tries to shrink the height of the tree by 1.
1374 void try_shrink();
1375
1376 iterator internal_end(iterator iter) {
1377 return iter.node != nullptr ? iter : end();
1378 }
1379 const_iterator internal_end(const_iterator iter) const {
1380 return iter.node != nullptr ? iter : end();
1381 }
1382
1383 // Emplaces a value into the btree immediately before iter. Requires that
1384 // key(v) <= iter.key() and (--iter).key() <= key(v).
1385 template <typename... Args>
1386 iterator internal_emplace(iterator iter, Args &&... args);
1387
1388 // Returns an iterator pointing to the first value >= the value "iter" is
1389 // pointing at. Note that "iter" might be pointing to an invalid location as
1390 // iter.position == iter.node->count(). This routine simply moves iter up in
1391 // the tree to a valid location.
1392 // Requires: iter.node is non-null.
1393 template <typename IterType>
1394 static IterType internal_last(IterType iter);
1395
1396 // Returns an iterator pointing to the leaf position at which key would
1397 // reside in the tree. We provide 2 versions of internal_locate. The first
1398 // version uses a less-than comparator and is incapable of distinguishing when
1399 // there is an exact match. The second version is for the key-compare-to
1400 // specialization and distinguishes exact matches. The key-compare-to
1401 // specialization allows the caller to avoid a subsequent comparison to
1402 // determine if an exact match was made, which is important for keys with
1403 // expensive comparison, such as strings.
1404 template <typename K>
1405 SearchResult<iterator, is_key_compare_to::value> internal_locate(
1406 const K &key) const;
1407
1408 template <typename K>
1409 SearchResult<iterator, false> internal_locate_impl(
1410 const K &key, std::false_type /* IsCompareTo */) const;
1411
1412 template <typename K>
1413 SearchResult<iterator, true> internal_locate_impl(
1414 const K &key, std::true_type /* IsCompareTo */) const;
1415
1416 // Internal routine which implements lower_bound().
1417 template <typename K>
1418 iterator internal_lower_bound(const K &key) const;
1419
1420 // Internal routine which implements upper_bound().
1421 template <typename K>
1422 iterator internal_upper_bound(const K &key) const;
1423
1424 // Internal routine which implements find().
1425 template <typename K>
1426 iterator internal_find(const K &key) const;
1427
1428 // Deletes a node and all of its children.
1429 void internal_clear(node_type *node);
1430
1431 // Verifies the tree structure of node.
1432 int internal_verify(const node_type *node,
1433 const key_type *lo, const key_type *hi) const;
1434
1435 node_stats internal_stats(const node_type *node) const {
1436 // The root can be a static empty node.
1437 if (node == nullptr || (node == root() && empty())) {
1438 return node_stats(0, 0);
1439 }
1440 if (node->leaf()) {
1441 return node_stats(1, 0);
1442 }
1443 node_stats res(0, 1);
1444 for (int i = 0; i <= node->count(); ++i) {
1445 res += internal_stats(node->child(i));
1446 }
1447 return res;
1448 }
1449
1450 public:
1451 // Exposed only for tests.
1452 static bool testonly_uses_linear_node_search() {
1453 return node_type::testonly_uses_linear_node_search();
1454 }
1455
1456 private:
1457 // We use compressed tuple in order to save space because key_compare and
1458 // allocator_type are usually empty.
1459 absl::container_internal::CompressedTuple<key_compare, allocator_type,
1460 node_type *>
1461 root_;
1462
1463 // A pointer to the rightmost node. Note that the leftmost node is stored as
1464 // the root's parent.
1465 node_type *rightmost_;
1466
1467 // Number of values.
1468 size_type size_;
1469};
1470
1471////
1472// btree_node methods
1473template <typename P>
1474template <typename... Args>
1475inline void btree_node<P>::emplace_value(const size_type i,
1476 allocator_type *alloc,
1477 Args &&... args) {
1478 assert(i <= count());
1479 // Shift old values to create space for new value and then construct it in
1480 // place.
1481 if (i < count()) {
1482 value_init(count(), alloc, slot(count() - 1));
1483 for (size_type j = count() - 1; j > i; --j)
1484 params_type::move(alloc, slot(j - 1), slot(j));
1485 value_destroy(i, alloc);
1486 }
1487 value_init(i, alloc, std::forward<Args>(args)...);
1488 set_count(count() + 1);
1489
1490 if (!leaf() && count() > i + 1) {
1491 for (int j = count(); j > i + 1; --j) {
1492 set_child(j, child(j - 1));
1493 }
1494 clear_child(i + 1);
1495 }
1496}
1497
1498template <typename P>
1499inline void btree_node<P>::remove_value(const int i, allocator_type *alloc) {
1500 if (!leaf() && count() > i + 1) {
1501 assert(child(i + 1)->count() == 0);
1502 for (size_type j = i + 1; j < count(); ++j) {
1503 set_child(j, child(j + 1));
1504 }
1505 clear_child(count());
1506 }
1507
1508 remove_values_ignore_children(i, /*to_erase=*/1, alloc);
1509}
1510
1511template <typename P>
1512inline void btree_node<P>::remove_values_ignore_children(
1513 const int i, const int to_erase, allocator_type *alloc) {
1514 params_type::move(alloc, slot(i + to_erase), slot(count()), slot(i));
1515 value_destroy_n(count() - to_erase, to_erase, alloc);
1516 set_count(count() - to_erase);
1517}
1518
1519template <typename P>
1520void btree_node<P>::rebalance_right_to_left(const int to_move,
1521 btree_node *right,
1522 allocator_type *alloc) {
1523 assert(parent() == right->parent());
1524 assert(position() + 1 == right->position());
1525 assert(right->count() >= count());
1526 assert(to_move >= 1);
1527 assert(to_move <= right->count());
1528
1529 // 1) Move the delimiting value in the parent to the left node.
1530 value_init(count(), alloc, parent()->slot(position()));
1531
1532 // 2) Move the (to_move - 1) values from the right node to the left node.
1533 right->uninitialized_move_n(to_move - 1, 0, count() + 1, this, alloc);
1534
1535 // 3) Move the new delimiting value to the parent from the right node.
1536 params_type::move(alloc, right->slot(to_move - 1),
1537 parent()->slot(position()));
1538
1539 // 4) Shift the values in the right node to their correct position.
1540 params_type::move(alloc, right->slot(to_move), right->slot(right->count()),
1541 right->slot(0));
1542
1543 // 5) Destroy the now-empty to_move entries in the right node.
1544 right->value_destroy_n(right->count() - to_move, to_move, alloc);
1545
1546 if (!leaf()) {
1547 // Move the child pointers from the right to the left node.
1548 for (int i = 0; i < to_move; ++i) {
1549 init_child(count() + i + 1, right->child(i));
1550 }
1551 for (int i = 0; i <= right->count() - to_move; ++i) {
1552 assert(i + to_move <= right->max_count());
1553 right->init_child(i, right->child(i + to_move));
1554 right->clear_child(i + to_move);
1555 }
1556 }
1557
1558 // Fixup the counts on the left and right nodes.
1559 set_count(count() + to_move);
1560 right->set_count(right->count() - to_move);
1561}
1562
1563template <typename P>
1564void btree_node<P>::rebalance_left_to_right(const int to_move,
1565 btree_node *right,
1566 allocator_type *alloc) {
1567 assert(parent() == right->parent());
1568 assert(position() + 1 == right->position());
1569 assert(count() >= right->count());
1570 assert(to_move >= 1);
1571 assert(to_move <= count());
1572
1573 // Values in the right node are shifted to the right to make room for the
1574 // new to_move values. Then, the delimiting value in the parent and the
1575 // other (to_move - 1) values in the left node are moved into the right node.
1576 // Lastly, a new delimiting value is moved from the left node into the
1577 // parent, and the remaining empty left node entries are destroyed.
1578
1579 if (right->count() >= to_move) {
1580 // The original location of the right->count() values are sufficient to hold
1581 // the new to_move entries from the parent and left node.
1582
1583 // 1) Shift existing values in the right node to their correct positions.
1584 right->uninitialized_move_n(to_move, right->count() - to_move,
1585 right->count(), right, alloc);
1586 for (slot_type *src = right->slot(right->count() - to_move - 1),
1587 *dest = right->slot(right->count() - 1),
1588 *end = right->slot(0);
1589 src >= end; --src, --dest) {
1590 params_type::move(alloc, src, dest);
1591 }
1592
1593 // 2) Move the delimiting value in the parent to the right node.
1594 params_type::move(alloc, parent()->slot(position()),
1595 right->slot(to_move - 1));
1596
1597 // 3) Move the (to_move - 1) values from the left node to the right node.
1598 params_type::move(alloc, slot(count() - (to_move - 1)), slot(count()),
1599 right->slot(0));
1600 } else {
1601 // The right node does not have enough initialized space to hold the new
1602 // to_move entries, so part of them will move to uninitialized space.
1603
1604 // 1) Shift existing values in the right node to their correct positions.
1605 right->uninitialized_move_n(right->count(), 0, to_move, right, alloc);
1606
1607 // 2) Move the delimiting value in the parent to the right node.
1608 right->value_init(to_move - 1, alloc, parent()->slot(position()));
1609
1610 // 3) Move the (to_move - 1) values from the left node to the right node.
1611 const size_type uninitialized_remaining = to_move - right->count() - 1;
1612 uninitialized_move_n(uninitialized_remaining,
1613 count() - uninitialized_remaining, right->count(),
1614 right, alloc);
1615 params_type::move(alloc, slot(count() - (to_move - 1)),
1616 slot(count() - uninitialized_remaining), right->slot(0));
1617 }
1618
1619 // 4) Move the new delimiting value to the parent from the left node.
1620 params_type::move(alloc, slot(count() - to_move), parent()->slot(position()));
1621
1622 // 5) Destroy the now-empty to_move entries in the left node.
1623 value_destroy_n(count() - to_move, to_move, alloc);
1624
1625 if (!leaf()) {
1626 // Move the child pointers from the left to the right node.
1627 for (int i = right->count(); i >= 0; --i) {
1628 right->init_child(i + to_move, right->child(i));
1629 right->clear_child(i);
1630 }
1631 for (int i = 1; i <= to_move; ++i) {
1632 right->init_child(i - 1, child(count() - to_move + i));
1633 clear_child(count() - to_move + i);
1634 }
1635 }
1636
1637 // Fixup the counts on the left and right nodes.
1638 set_count(count() - to_move);
1639 right->set_count(right->count() + to_move);
1640}
1641
1642template <typename P>
1643void btree_node<P>::split(const int insert_position, btree_node *dest,
1644 allocator_type *alloc) {
1645 assert(dest->count() == 0);
1646 assert(max_count() == kNodeValues);
1647
1648 // We bias the split based on the position being inserted. If we're
1649 // inserting at the beginning of the left node then bias the split to put
1650 // more values on the right node. If we're inserting at the end of the
1651 // right node then bias the split to put more values on the left node.
1652 if (insert_position == 0) {
1653 dest->set_count(count() - 1);
1654 } else if (insert_position == kNodeValues) {
1655 dest->set_count(0);
1656 } else {
1657 dest->set_count(count() / 2);
1658 }
1659 set_count(count() - dest->count());
1660 assert(count() >= 1);
1661
1662 // Move values from the left sibling to the right sibling.
1663 uninitialized_move_n(dest->count(), count(), 0, dest, alloc);
1664
1665 // Destroy the now-empty entries in the left node.
1666 value_destroy_n(count(), dest->count(), alloc);
1667
1668 // The split key is the largest value in the left sibling.
1669 set_count(count() - 1);
1670 parent()->emplace_value(position(), alloc, slot(count()));
1671 value_destroy(count(), alloc);
1672 parent()->init_child(position() + 1, dest);
1673
1674 if (!leaf()) {
1675 for (int i = 0; i <= dest->count(); ++i) {
1676 assert(child(count() + i + 1) != nullptr);
1677 dest->init_child(i, child(count() + i + 1));
1678 clear_child(count() + i + 1);
1679 }
1680 }
1681}
1682
1683template <typename P>
1684void btree_node<P>::merge(btree_node *src, allocator_type *alloc) {
1685 assert(parent() == src->parent());
1686 assert(position() + 1 == src->position());
1687
1688 // Move the delimiting value to the left node.
1689 value_init(count(), alloc, parent()->slot(position()));
1690
1691 // Move the values from the right to the left node.
1692 src->uninitialized_move_n(src->count(), 0, count() + 1, this, alloc);
1693
1694 // Destroy the now-empty entries in the right node.
1695 src->value_destroy_n(0, src->count(), alloc);
1696
1697 if (!leaf()) {
1698 // Move the child pointers from the right to the left node.
1699 for (int i = 0; i <= src->count(); ++i) {
1700 init_child(count() + i + 1, src->child(i));
1701 src->clear_child(i);
1702 }
1703 }
1704
1705 // Fixup the counts on the src and dest nodes.
1706 set_count(1 + count() + src->count());
1707 src->set_count(0);
1708
1709 // Remove the value on the parent node.
1710 parent()->remove_value(position(), alloc);
1711}
1712
1713template <typename P>
1714void btree_node<P>::swap(btree_node *x, allocator_type *alloc) {
1715 using std::swap;
1716 assert(leaf() == x->leaf());
1717
1718 // Determine which is the smaller/larger node.
1719 btree_node *smaller = this, *larger = x;
1720 if (smaller->count() > larger->count()) {
1721 swap(smaller, larger);
1722 }
1723
1724 // Swap the values.
1725 for (slot_type *a = smaller->slot(0), *b = larger->slot(0),
1726 *end = a + smaller->count();
1727 a != end; ++a, ++b) {
1728 params_type::swap(alloc, a, b);
1729 }
1730
1731 // Move values that can't be swapped.
1732 const size_type to_move = larger->count() - smaller->count();
1733 larger->uninitialized_move_n(to_move, smaller->count(), smaller->count(),
1734 smaller, alloc);
1735 larger->value_destroy_n(smaller->count(), to_move, alloc);
1736
1737 if (!leaf()) {
1738 // Swap the child pointers.
1739 std::swap_ranges(&smaller->mutable_child(0),
1740 &smaller->mutable_child(smaller->count() + 1),
1741 &larger->mutable_child(0));
1742 // Update swapped children's parent pointers.
1743 int i = 0;
1744 for (; i <= smaller->count(); ++i) {
1745 smaller->child(i)->set_parent(smaller);
1746 larger->child(i)->set_parent(larger);
1747 }
1748 // Move the child pointers that couldn't be swapped.
1749 for (; i <= larger->count(); ++i) {
1750 smaller->init_child(i, larger->child(i));
1751 larger->clear_child(i);
1752 }
1753 }
1754
1755 // Swap the counts.
1756 swap(mutable_count(), x->mutable_count());
1757}
1758
1759////
1760// btree_iterator methods
1761template <typename N, typename R, typename P>
1762void btree_iterator<N, R, P>::increment_slow() {
1763 if (node->leaf()) {
1764 assert(position >= node->count());
1765 btree_iterator save(*this);
1766 while (position == node->count() && !node->is_root()) {
1767 assert(node->parent()->child(node->position()) == node);
1768 position = node->position();
1769 node = node->parent();
1770 }
1771 if (position == node->count()) {
1772 *this = save;
1773 }
1774 } else {
1775 assert(position < node->count());
1776 node = node->child(position + 1);
1777 while (!node->leaf()) {
1778 node = node->child(0);
1779 }
1780 position = 0;
1781 }
1782}
1783
1784template <typename N, typename R, typename P>
1785void btree_iterator<N, R, P>::decrement_slow() {
1786 if (node->leaf()) {
1787 assert(position <= -1);
1788 btree_iterator save(*this);
1789 while (position < 0 && !node->is_root()) {
1790 assert(node->parent()->child(node->position()) == node);
1791 position = node->position() - 1;
1792 node = node->parent();
1793 }
1794 if (position < 0) {
1795 *this = save;
1796 }
1797 } else {
1798 assert(position >= 0);
1799 node = node->child(position);
1800 while (!node->leaf()) {
1801 node = node->child(node->count());
1802 }
1803 position = node->count() - 1;
1804 }
1805}
1806
1807////
1808// btree methods
1809template <typename P>
1810template <typename Btree>
1811void btree<P>::copy_or_move_values_in_order(Btree *x) {
1812 static_assert(std::is_same<btree, Btree>::value ||
1813 std::is_same<const btree, Btree>::value,
1814 "Btree type must be same or const.");
1815 assert(empty());
1816
1817 // We can avoid key comparisons because we know the order of the
1818 // values is the same order we'll store them in.
1819 auto iter = x->begin();
1820 if (iter == x->end()) return;
1821 insert_multi(maybe_move_from_iterator(iter));
1822 ++iter;
1823 for (; iter != x->end(); ++iter) {
1824 // If the btree is not empty, we can just insert the new value at the end
1825 // of the tree.
1826 internal_emplace(end(), maybe_move_from_iterator(iter));
1827 }
1828}
1829
1830template <typename P>
1831constexpr bool btree<P>::static_assert_validation() {
1832 static_assert(std::is_nothrow_copy_constructible<key_compare>::value,
1833 "Key comparison must be nothrow copy constructible");
1834 static_assert(std::is_nothrow_copy_constructible<allocator_type>::value,
1835 "Allocator must be nothrow copy constructible");
1836 static_assert(type_traits_internal::is_trivially_copyable<iterator>::value,
1837 "iterator not trivially copyable.");
1838
1839 // Note: We assert that kTargetValues, which is computed from
1840 // Params::kTargetNodeSize, must fit the node_type::field_type.
1841 static_assert(
1842 kNodeValues < (1 << (8 * sizeof(typename node_type::field_type))),
1843 "target node size too large");
1844
1845 // Verify that key_compare returns an absl::{weak,strong}_ordering or bool.
1846 using compare_result_type =
1847 absl::result_of_t<key_compare(key_type, key_type)>;
1848 static_assert(
1849 std::is_same<compare_result_type, bool>::value ||
1850 std::is_convertible<compare_result_type, absl::weak_ordering>::value,
1851 "key comparison function must return absl::{weak,strong}_ordering or "
1852 "bool.");
1853
1854 // Test the assumption made in setting kNodeValueSpace.
1855 static_assert(node_type::MinimumOverhead() >= sizeof(void *) + 4,
1856 "node space assumption incorrect");
1857
1858 return true;
1859}
1860
1861template <typename P>
1862btree<P>::btree(const key_compare &comp, const allocator_type &alloc)
1863 : root_(comp, alloc, EmptyNode()), rightmost_(EmptyNode()), size_(0) {}
1864
1865template <typename P>
1866btree<P>::btree(const btree &x) : btree(x.key_comp(), x.allocator()) {
1867 copy_or_move_values_in_order(&x);
1868}
1869
1870template <typename P>
1871template <typename... Args>
1872auto btree<P>::insert_unique(const key_type &key, Args &&... args)
1873 -> std::pair<iterator, bool> {
1874 if (empty()) {
1875 mutable_root() = rightmost_ = new_leaf_root_node(1);
1876 }
1877
1878 auto res = internal_locate(key);
1879 iterator &iter = res.value;
1880
1881 if (res.HasMatch()) {
1882 if (res.IsEq()) {
1883 // The key already exists in the tree, do nothing.
1884 return {iter, false};
1885 }
1886 } else {
1887 iterator last = internal_last(iter);
1888 if (last.node && !compare_keys(key, last.key())) {
1889 // The key already exists in the tree, do nothing.
1890 return {last, false};
1891 }
1892 }
1893 return {internal_emplace(iter, std::forward<Args>(args)...), true};
1894}
1895
1896template <typename P>
1897template <typename... Args>
1898inline auto btree<P>::insert_hint_unique(iterator position, const key_type &key,
1899 Args &&... args)
1900 -> std::pair<iterator, bool> {
1901 if (!empty()) {
1902 if (position == end() || compare_keys(key, position.key())) {
1903 iterator prev = position;
1904 if (position == begin() || compare_keys((--prev).key(), key)) {
1905 // prev.key() < key < position.key()
1906 return {internal_emplace(position, std::forward<Args>(args)...), true};
1907 }
1908 } else if (compare_keys(position.key(), key)) {
1909 ++position;
1910 if (position == end() || compare_keys(key, position.key())) {
1911 // {original `position`}.key() < key < {current `position`}.key()
1912 return {internal_emplace(position, std::forward<Args>(args)...), true};
1913 }
1914 } else {
1915 // position.key() == key
1916 return {position, false};
1917 }
1918 }
1919 return insert_unique(key, std::forward<Args>(args)...);
1920}
1921
1922template <typename P>
1923template <typename InputIterator>
1924void btree<P>::insert_iterator_unique(InputIterator b, InputIterator e) {
1925 for (; b != e; ++b) {
1926 insert_hint_unique(end(), params_type::key(*b), *b);
1927 }
1928}
1929
1930template <typename P>
1931template <typename ValueType>
1932auto btree<P>::insert_multi(const key_type &key, ValueType &&v) -> iterator {
1933 if (empty()) {
1934 mutable_root() = rightmost_ = new_leaf_root_node(1);
1935 }
1936
1937 iterator iter = internal_upper_bound(key);
1938 if (iter.node == nullptr) {
1939 iter = end();
1940 }
1941 return internal_emplace(iter, std::forward<ValueType>(v));
1942}
1943
1944template <typename P>
1945template <typename ValueType>
1946auto btree<P>::insert_hint_multi(iterator position, ValueType &&v) -> iterator {
1947 if (!empty()) {
1948 const key_type &key = params_type::key(v);
1949 if (position == end() || !compare_keys(position.key(), key)) {
1950 iterator prev = position;
1951 if (position == begin() || !compare_keys(key, (--prev).key())) {
1952 // prev.key() <= key <= position.key()
1953 return internal_emplace(position, std::forward<ValueType>(v));
1954 }
1955 } else {
1956 iterator next = position;
1957 ++next;
1958 if (next == end() || !compare_keys(next.key(), key)) {
1959 // position.key() < key <= next.key()
1960 return internal_emplace(next, std::forward<ValueType>(v));
1961 }
1962 }
1963 }
1964 return insert_multi(std::forward<ValueType>(v));
1965}
1966
1967template <typename P>
1968template <typename InputIterator>
1969void btree<P>::insert_iterator_multi(InputIterator b, InputIterator e) {
1970 for (; b != e; ++b) {
1971 insert_hint_multi(end(), *b);
1972 }
1973}
1974
1975template <typename P>
1976auto btree<P>::operator=(const btree &x) -> btree & {
1977 if (this != &x) {
1978 clear();
1979
1980 *mutable_key_comp() = x.key_comp();
1981 if (absl::allocator_traits<
1982 allocator_type>::propagate_on_container_copy_assignment::value) {
1983 *mutable_allocator() = x.allocator();
1984 }
1985
1986 copy_or_move_values_in_order(&x);
1987 }
1988 return *this;
1989}
1990
1991template <typename P>
1992auto btree<P>::operator=(btree &&x) noexcept -> btree & {
1993 if (this != &x) {
1994 clear();
1995
1996 using std::swap;
1997 if (absl::allocator_traits<
1998 allocator_type>::propagate_on_container_copy_assignment::value) {
1999 // Note: `root_` also contains the allocator and the key comparator.
2000 swap(root_, x.root_);
2001 swap(rightmost_, x.rightmost_);
2002 swap(size_, x.size_);
2003 } else {
2004 if (allocator() == x.allocator()) {
2005 swap(mutable_root(), x.mutable_root());
2006 swap(*mutable_key_comp(), *x.mutable_key_comp());
2007 swap(rightmost_, x.rightmost_);
2008 swap(size_, x.size_);
2009 } else {
2010 // We aren't allowed to propagate the allocator and the allocator is
2011 // different so we can't take over its memory. We must move each element
2012 // individually. We need both `x` and `this` to have `x`s key comparator
2013 // while moving the values so we can't swap the key comparators.
2014 *mutable_key_comp() = x.key_comp();
2015 copy_or_move_values_in_order(&x);
2016 }
2017 }
2018 }
2019 return *this;
2020}
2021
2022template <typename P>
2023auto btree<P>::erase(iterator iter) -> iterator {
2024 bool internal_delete = false;
2025 if (!iter.node->leaf()) {
2026 // Deletion of a value on an internal node. First, move the largest value
2027 // from our left child here, then delete that position (in remove_value()
2028 // below). We can get to the largest value from our left child by
2029 // decrementing iter.
2030 iterator internal_iter(iter);
2031 --iter;
2032 assert(iter.node->leaf());
2033 assert(!compare_keys(internal_iter.key(), iter.key()));
2034 params_type::move(mutable_allocator(), iter.node->slot(iter.position),
2035 internal_iter.node->slot(internal_iter.position));
2036 internal_delete = true;
2037 }
2038
2039 // Delete the key from the leaf.
2040 iter.node->remove_value(iter.position, mutable_allocator());
2041 --size_;
2042
2043 // We want to return the next value after the one we just erased. If we
2044 // erased from an internal node (internal_delete == true), then the next
2045 // value is ++(++iter). If we erased from a leaf node (internal_delete ==
2046 // false) then the next value is ++iter. Note that ++iter may point to an
2047 // internal node and the value in the internal node may move to a leaf node
2048 // (iter.node) when rebalancing is performed at the leaf level.
2049
2050 iterator res = rebalance_after_delete(iter);
2051
2052 // If we erased from an internal node, advance the iterator.
2053 if (internal_delete) {
2054 ++res;
2055 }
2056 return res;
2057}
2058
2059template <typename P>
2060auto btree<P>::rebalance_after_delete(iterator iter) -> iterator {
2061 // Merge/rebalance as we walk back up the tree.
2062 iterator res(iter);
2063 bool first_iteration = true;
2064 for (;;) {
2065 if (iter.node == root()) {
2066 try_shrink();
2067 if (empty()) {
2068 return end();
2069 }
2070 break;
2071 }
2072 if (iter.node->count() >= kMinNodeValues) {
2073 break;
2074 }
2075 bool merged = try_merge_or_rebalance(&iter);
2076 // On the first iteration, we should update `res` with `iter` because `res`
2077 // may have been invalidated.
2078 if (first_iteration) {
2079 res = iter;
2080 first_iteration = false;
2081 }
2082 if (!merged) {
2083 break;
2084 }
2085 iter.position = iter.node->position();
2086 iter.node = iter.node->parent();
2087 }
2088
2089 // Adjust our return value. If we're pointing at the end of a node, advance
2090 // the iterator.
2091 if (res.position == res.node->count()) {
2092 res.position = res.node->count() - 1;
2093 ++res;
2094 }
2095
2096 return res;
2097}
2098
2099template <typename P>
2100auto btree<P>::erase(iterator begin, iterator end)
2101 -> std::pair<size_type, iterator> {
2102 difference_type count = std::distance(begin, end);
2103 assert(count >= 0);
2104
2105 if (count == 0) {
2106 return {0, begin};
2107 }
2108
2109 if (count == size_) {
2110 clear();
2111 return {count, this->end()};
2112 }
2113
2114 if (begin.node == end.node) {
2115 erase_same_node(begin, end);
2116 size_ -= count;
2117 return {count, rebalance_after_delete(begin)};
2118 }
2119
2120 const size_type target_size = size_ - count;
2121 while (size_ > target_size) {
2122 if (begin.node->leaf()) {
2123 const size_type remaining_to_erase = size_ - target_size;
2124 const size_type remaining_in_node = begin.node->count() - begin.position;
2125 begin = erase_from_leaf_node(
2126 begin, (std::min)(remaining_to_erase, remaining_in_node));
2127 } else {
2128 begin = erase(begin);
2129 }
2130 }
2131 return {count, begin};
2132}
2133
2134template <typename P>
2135void btree<P>::erase_same_node(iterator begin, iterator end) {
2136 assert(begin.node == end.node);
2137 assert(end.position > begin.position);
2138
2139 node_type *node = begin.node;
2140 size_type to_erase = end.position - begin.position;
2141 if (!node->leaf()) {
2142 // Delete all children between begin and end.
2143 for (size_type i = 0; i < to_erase; ++i) {
2144 internal_clear(node->child(begin.position + i + 1));
2145 }
2146 // Rotate children after end into new positions.
2147 for (size_type i = begin.position + to_erase + 1; i <= node->count(); ++i) {
2148 node->set_child(i - to_erase, node->child(i));
2149 node->clear_child(i);
2150 }
2151 }
2152 node->remove_values_ignore_children(begin.position, to_erase,
2153 mutable_allocator());
2154
2155 // Do not need to update rightmost_, because
2156 // * either end == this->end(), and therefore node == rightmost_, and still
2157 // exists
2158 // * or end != this->end(), and therefore rightmost_ hasn't been erased, since
2159 // it wasn't covered in [begin, end)
2160}
2161
2162template <typename P>
2163auto btree<P>::erase_from_leaf_node(iterator begin, size_type to_erase)
2164 -> iterator {
2165 node_type *node = begin.node;
2166 assert(node->leaf());
2167 assert(node->count() > begin.position);
2168 assert(begin.position + to_erase <= node->count());
2169
2170 node->remove_values_ignore_children(begin.position, to_erase,
2171 mutable_allocator());
2172
2173 size_ -= to_erase;
2174
2175 return rebalance_after_delete(begin);
2176}
2177
2178template <typename P>
2179template <typename K>
2180auto btree<P>::erase_unique(const K &key) -> size_type {
2181 const iterator iter = internal_find(key);
2182 if (iter.node == nullptr) {
2183 // The key doesn't exist in the tree, return nothing done.
2184 return 0;
2185 }
2186 erase(iter);
2187 return 1;
2188}
2189
2190template <typename P>
2191template <typename K>
2192auto btree<P>::erase_multi(const K &key) -> size_type {
2193 const iterator begin = internal_lower_bound(key);
2194 if (begin.node == nullptr) {
2195 // The key doesn't exist in the tree, return nothing done.
2196 return 0;
2197 }
2198 // Delete all of the keys between begin and upper_bound(key).
2199 const iterator end = internal_end(internal_upper_bound(key));
2200 return erase(begin, end).first;
2201}
2202
2203template <typename P>
2204void btree<P>::clear() {
2205 if (!empty()) {
2206 internal_clear(root());
2207 }
2208 mutable_root() = EmptyNode();
2209 rightmost_ = EmptyNode();
2210 size_ = 0;
2211}
2212
2213template <typename P>
2214void btree<P>::swap(btree &x) {
2215 using std::swap;
2216 if (absl::allocator_traits<
2217 allocator_type>::propagate_on_container_swap::value) {
2218 // Note: `root_` also contains the allocator and the key comparator.
2219 swap(root_, x.root_);
2220 } else {
2221 // It's undefined behavior if the allocators are unequal here.
2222 assert(allocator() == x.allocator());
2223 swap(mutable_root(), x.mutable_root());
2224 swap(*mutable_key_comp(), *x.mutable_key_comp());
2225 }
2226 swap(rightmost_, x.rightmost_);
2227 swap(size_, x.size_);
2228}
2229
2230template <typename P>
2231void btree<P>::verify() const {
2232 assert(root() != nullptr);
2233 assert(leftmost() != nullptr);
2234 assert(rightmost_ != nullptr);
2235 assert(empty() || size() == internal_verify(root(), nullptr, nullptr));
2236 assert(leftmost() == (++const_iterator(root(), -1)).node);
2237 assert(rightmost_ == (--const_iterator(root(), root()->count())).node);
2238 assert(leftmost()->leaf());
2239 assert(rightmost_->leaf());
2240}
2241
2242template <typename P>
2243void btree<P>::rebalance_or_split(iterator *iter) {
2244 node_type *&node = iter->node;
2245 int &insert_position = iter->position;
2246 assert(node->count() == node->max_count());
2247 assert(kNodeValues == node->max_count());
2248
2249 // First try to make room on the node by rebalancing.
2250 node_type *parent = node->parent();
2251 if (node != root()) {
2252 if (node->position() > 0) {
2253 // Try rebalancing with our left sibling.
2254 node_type *left = parent->child(node->position() - 1);
2255 assert(left->max_count() == kNodeValues);
2256 if (left->count() < kNodeValues) {
2257 // We bias rebalancing based on the position being inserted. If we're
2258 // inserting at the end of the right node then we bias rebalancing to
2259 // fill up the left node.
2260 int to_move = (kNodeValues - left->count()) /
2261 (1 + (insert_position < kNodeValues));
2262 to_move = (std::max)(1, to_move);
2263
2264 if (((insert_position - to_move) >= 0) ||
2265 ((left->count() + to_move) < kNodeValues)) {
2266 left->rebalance_right_to_left(to_move, node, mutable_allocator());
2267
2268 assert(node->max_count() - node->count() == to_move);
2269 insert_position = insert_position - to_move;
2270 if (insert_position < 0) {
2271 insert_position = insert_position + left->count() + 1;
2272 node = left;
2273 }
2274
2275 assert(node->count() < node->max_count());
2276 return;
2277 }
2278 }
2279 }
2280
2281 if (node->position() < parent->count()) {
2282 // Try rebalancing with our right sibling.
2283 node_type *right = parent->child(node->position() + 1);
2284 assert(right->max_count() == kNodeValues);
2285 if (right->count() < kNodeValues) {
2286 // We bias rebalancing based on the position being inserted. If we're
2287 // inserting at the beginning of the left node then we bias rebalancing
2288 // to fill up the right node.
2289 int to_move =
2290 (kNodeValues - right->count()) / (1 + (insert_position > 0));
2291 to_move = (std::max)(1, to_move);
2292
2293 if ((insert_position <= (node->count() - to_move)) ||
2294 ((right->count() + to_move) < kNodeValues)) {
2295 node->rebalance_left_to_right(to_move, right, mutable_allocator());
2296
2297 if (insert_position > node->count()) {
2298 insert_position = insert_position - node->count() - 1;
2299 node = right;
2300 }
2301
2302 assert(node->count() < node->max_count());
2303 return;
2304 }
2305 }
2306 }
2307
2308 // Rebalancing failed, make sure there is room on the parent node for a new
2309 // value.
2310 assert(parent->max_count() == kNodeValues);
2311 if (parent->count() == kNodeValues) {
2312 iterator parent_iter(node->parent(), node->position());
2313 rebalance_or_split(&parent_iter);
2314 }
2315 } else {
2316 // Rebalancing not possible because this is the root node.
2317 // Create a new root node and set the current root node as the child of the
2318 // new root.
2319 parent = new_internal_node(parent);
2320 parent->init_child(0, root());
2321 mutable_root() = parent;
2322 // If the former root was a leaf node, then it's now the rightmost node.
2323 assert(!parent->child(0)->leaf() || parent->child(0) == rightmost_);
2324 }
2325
2326 // Split the node.
2327 node_type *split_node;
2328 if (node->leaf()) {
2329 split_node = new_leaf_node(parent);
2330 node->split(insert_position, split_node, mutable_allocator());
2331 if (rightmost_ == node) rightmost_ = split_node;
2332 } else {
2333 split_node = new_internal_node(parent);
2334 node->split(insert_position, split_node, mutable_allocator());
2335 }
2336
2337 if (insert_position > node->count()) {
2338 insert_position = insert_position - node->count() - 1;
2339 node = split_node;
2340 }
2341}
2342
2343template <typename P>
2344void btree<P>::merge_nodes(node_type *left, node_type *right) {
2345 left->merge(right, mutable_allocator());
2346 if (right->leaf()) {
2347 if (rightmost_ == right) rightmost_ = left;
2348 delete_leaf_node(right);
2349 } else {
2350 delete_internal_node(right);
2351 }
2352}
2353
2354template <typename P>
2355bool btree<P>::try_merge_or_rebalance(iterator *iter) {
2356 node_type *parent = iter->node->parent();
2357 if (iter->node->position() > 0) {
2358 // Try merging with our left sibling.
2359 node_type *left = parent->child(iter->node->position() - 1);
2360 assert(left->max_count() == kNodeValues);
2361 if ((1 + left->count() + iter->node->count()) <= kNodeValues) {
2362 iter->position += 1 + left->count();
2363 merge_nodes(left, iter->node);
2364 iter->node = left;
2365 return true;
2366 }
2367 }
2368 if (iter->node->position() < parent->count()) {
2369 // Try merging with our right sibling.
2370 node_type *right = parent->child(iter->node->position() + 1);
2371 assert(right->max_count() == kNodeValues);
2372 if ((1 + iter->node->count() + right->count()) <= kNodeValues) {
2373 merge_nodes(iter->node, right);
2374 return true;
2375 }
2376 // Try rebalancing with our right sibling. We don't perform rebalancing if
2377 // we deleted the first element from iter->node and the node is not
2378 // empty. This is a small optimization for the common pattern of deleting
2379 // from the front of the tree.
2380 if ((right->count() > kMinNodeValues) &&
2381 ((iter->node->count() == 0) ||
2382 (iter->position > 0))) {
2383 int to_move = (right->count() - iter->node->count()) / 2;
2384 to_move = (std::min)(to_move, right->count() - 1);
2385 iter->node->rebalance_right_to_left(to_move, right, mutable_allocator());
2386 return false;
2387 }
2388 }
2389 if (iter->node->position() > 0) {
2390 // Try rebalancing with our left sibling. We don't perform rebalancing if
2391 // we deleted the last element from iter->node and the node is not
2392 // empty. This is a small optimization for the common pattern of deleting
2393 // from the back of the tree.
2394 node_type *left = parent->child(iter->node->position() - 1);
2395 if ((left->count() > kMinNodeValues) &&
2396 ((iter->node->count() == 0) ||
2397 (iter->position < iter->node->count()))) {
2398 int to_move = (left->count() - iter->node->count()) / 2;
2399 to_move = (std::min)(to_move, left->count() - 1);
2400 left->rebalance_left_to_right(to_move, iter->node, mutable_allocator());
2401 iter->position += to_move;
2402 return false;
2403 }
2404 }
2405 return false;
2406}
2407
2408template <typename P>
2409void btree<P>::try_shrink() {
2410 if (root()->count() > 0) {
2411 return;
2412 }
2413 // Deleted the last item on the root node, shrink the height of the tree.
2414 if (root()->leaf()) {
2415 assert(size() == 0);
2416 delete_leaf_node(root());
2417 mutable_root() = EmptyNode();
2418 rightmost_ = EmptyNode();
2419 } else {
2420 node_type *child = root()->child(0);
2421 child->make_root();
2422 delete_internal_node(root());
2423 mutable_root() = child;
2424 }
2425}
2426
2427template <typename P>
2428template <typename IterType>
2429inline IterType btree<P>::internal_last(IterType iter) {
2430 assert(iter.node != nullptr);
2431 while (iter.position == iter.node->count()) {
2432 iter.position = iter.node->position();
2433 iter.node = iter.node->parent();
2434 if (iter.node->leaf()) {
2435 iter.node = nullptr;
2436 break;
2437 }
2438 }
2439 return iter;
2440}
2441
2442template <typename P>
2443template <typename... Args>
2444inline auto btree<P>::internal_emplace(iterator iter, Args &&... args)
2445 -> iterator {
2446 if (!iter.node->leaf()) {
2447 // We can't insert on an internal node. Instead, we'll insert after the
2448 // previous value which is guaranteed to be on a leaf node.
2449 --iter;
2450 ++iter.position;
2451 }
2452 const int max_count = iter.node->max_count();
2453 if (iter.node->count() == max_count) {
2454 // Make room in the leaf for the new item.
2455 if (max_count < kNodeValues) {
2456 // Insertion into the root where the root is smaller than the full node
2457 // size. Simply grow the size of the root node.
2458 assert(iter.node == root());
2459 iter.node =
2460 new_leaf_root_node((std::min<int>)(kNodeValues, 2 * max_count));
2461 iter.node->swap(root(), mutable_allocator());
2462 delete_leaf_node(root());
2463 mutable_root() = iter.node;
2464 rightmost_ = iter.node;
2465 } else {
2466 rebalance_or_split(&iter);
2467 }
2468 }
2469 iter.node->emplace_value(iter.position, mutable_allocator(),
2470 std::forward<Args>(args)...);
2471 ++size_;
2472 return iter;
2473}
2474
2475template <typename P>
2476template <typename K>
2477inline auto btree<P>::internal_locate(const K &key) const
2478 -> SearchResult<iterator, is_key_compare_to::value> {
2479 return internal_locate_impl(key, is_key_compare_to());
2480}
2481
2482template <typename P>
2483template <typename K>
2484inline auto btree<P>::internal_locate_impl(
2485 const K &key, std::false_type /* IsCompareTo */) const
2486 -> SearchResult<iterator, false> {
2487 iterator iter(const_cast<node_type *>(root()), 0);
2488 for (;;) {
2489 iter.position = iter.node->lower_bound(key, key_comp()).value;
2490 // NOTE: we don't need to walk all the way down the tree if the keys are
2491 // equal, but determining equality would require doing an extra comparison
2492 // on each node on the way down, and we will need to go all the way to the
2493 // leaf node in the expected case.
2494 if (iter.node->leaf()) {
2495 break;
2496 }
2497 iter.node = iter.node->child(iter.position);
2498 }
2499 return {iter};
2500}
2501
2502template <typename P>
2503template <typename K>
2504inline auto btree<P>::internal_locate_impl(
2505 const K &key, std::true_type /* IsCompareTo */) const
2506 -> SearchResult<iterator, true> {
2507 iterator iter(const_cast<node_type *>(root()), 0);
2508 for (;;) {
2509 SearchResult<int, true> res = iter.node->lower_bound(key, key_comp());
2510 iter.position = res.value;
2511 if (res.match == MatchKind::kEq) {
2512 return {iter, MatchKind::kEq};
2513 }
2514 if (iter.node->leaf()) {
2515 break;
2516 }
2517 iter.node = iter.node->child(iter.position);
2518 }
2519 return {iter, MatchKind::kNe};
2520}
2521
2522template <typename P>
2523template <typename K>
2524auto btree<P>::internal_lower_bound(const K &key) const -> iterator {
2525 iterator iter(const_cast<node_type *>(root()), 0);
2526 for (;;) {
2527 iter.position = iter.node->lower_bound(key, key_comp()).value;
2528 if (iter.node->leaf()) {
2529 break;
2530 }
2531 iter.node = iter.node->child(iter.position);
2532 }
2533 return internal_last(iter);
2534}
2535
2536template <typename P>
2537template <typename K>
2538auto btree<P>::internal_upper_bound(const K &key) const -> iterator {
2539 iterator iter(const_cast<node_type *>(root()), 0);
2540 for (;;) {
2541 iter.position = iter.node->upper_bound(key, key_comp());
2542 if (iter.node->leaf()) {
2543 break;
2544 }
2545 iter.node = iter.node->child(iter.position);
2546 }
2547 return internal_last(iter);
2548}
2549
2550template <typename P>
2551template <typename K>
2552auto btree<P>::internal_find(const K &key) const -> iterator {
2553 auto res = internal_locate(key);
2554 if (res.HasMatch()) {
2555 if (res.IsEq()) {
2556 return res.value;
2557 }
2558 } else {
2559 const iterator iter = internal_last(res.value);
2560 if (iter.node != nullptr && !compare_keys(key, iter.key())) {
2561 return iter;
2562 }
2563 }
2564 return {nullptr, 0};
2565}
2566
2567template <typename P>
2568void btree<P>::internal_clear(node_type *node) {
2569 if (!node->leaf()) {
2570 for (int i = 0; i <= node->count(); ++i) {
2571 internal_clear(node->child(i));
2572 }
2573 delete_internal_node(node);
2574 } else {
2575 delete_leaf_node(node);
2576 }
2577}
2578
2579template <typename P>
2580int btree<P>::internal_verify(
2581 const node_type *node, const key_type *lo, const key_type *hi) const {
2582 assert(node->count() > 0);
2583 assert(node->count() <= node->max_count());
2584 if (lo) {
2585 assert(!compare_keys(node->key(0), *lo));
2586 }
2587 if (hi) {
2588 assert(!compare_keys(*hi, node->key(node->count() - 1)));
2589 }
2590 for (int i = 1; i < node->count(); ++i) {
2591 assert(!compare_keys(node->key(i), node->key(i - 1)));
2592 }
2593 int count = node->count();
2594 if (!node->leaf()) {
2595 for (int i = 0; i <= node->count(); ++i) {
2596 assert(node->child(i) != nullptr);
2597 assert(node->child(i)->parent() == node);
2598 assert(node->child(i)->position() == i);
2599 count += internal_verify(
2600 node->child(i),
2601 (i == 0) ? lo : &node->key(i - 1),
2602 (i == node->count()) ? hi : &node->key(i));
2603 }
2604 }
2605 return count;
2606}
2607
2608} // namespace container_internal
2609} // namespace absl
2610
2611#endif // ABSL_CONTAINER_INTERNAL_BTREE_H_