1 // 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/internal/raw_logging.h"
62 #include "absl/base/macros.h"
63 #include "absl/container/internal/common.h"
64 #include "absl/container/internal/common_policy_traits.h"
65 #include "absl/container/internal/compressed_tuple.h"
66 #include "absl/container/internal/container_memory.h"
67 #include "absl/container/internal/layout.h"
68 #include "absl/memory/memory.h"
69 #include "absl/meta/type_traits.h"
70 #include "absl/strings/cord.h"
71 #include "absl/strings/string_view.h"
72 #include "absl/types/compare.h"
73 #include "absl/utility/utility.h"
74
75 namespace absl {
76 ABSL_NAMESPACE_BEGIN
77 namespace container_internal {
78
79 #ifdef ABSL_BTREE_ENABLE_GENERATIONS
80 #error ABSL_BTREE_ENABLE_GENERATIONS cannot be directly set
81 #elif defined(ABSL_HAVE_ADDRESS_SANITIZER) || \
82 defined(ABSL_HAVE_MEMORY_SANITIZER)
83 // When compiled in sanitizer mode, we add generation integers to the nodes and
84 // iterators. When iterators are used, we validate that the container has not
85 // been mutated since the iterator was constructed.
86 #define ABSL_BTREE_ENABLE_GENERATIONS
87 #endif
88
89 #ifdef ABSL_BTREE_ENABLE_GENERATIONS
BtreeGenerationsEnabled()90 constexpr bool BtreeGenerationsEnabled() { return true; }
91 #else
92 constexpr bool BtreeGenerationsEnabled() { return false; }
93 #endif
94
95 template <typename Compare, typename T, typename U>
96 using compare_result_t = absl::result_of_t<const Compare(const T &, const U &)>;
97
98 // A helper class that indicates if the Compare parameter is a key-compare-to
99 // comparator.
100 template <typename Compare, typename T>
101 using btree_is_key_compare_to =
102 std::is_convertible<compare_result_t<Compare, T, T>, absl::weak_ordering>;
103
104 struct StringBtreeDefaultLess {
105 using is_transparent = void;
106
107 StringBtreeDefaultLess() = default;
108
109 // Compatibility constructor.
StringBtreeDefaultLessStringBtreeDefaultLess110 StringBtreeDefaultLess(std::less<std::string>) {} // NOLINT
StringBtreeDefaultLessStringBtreeDefaultLess111 StringBtreeDefaultLess(std::less<absl::string_view>) {} // NOLINT
112
113 // Allow converting to std::less for use in key_comp()/value_comp().
114 explicit operator std::less<std::string>() const { return {}; }
115 explicit operator std::less<absl::string_view>() const { return {}; }
116 explicit operator std::less<absl::Cord>() const { return {}; }
117
operatorStringBtreeDefaultLess118 absl::weak_ordering operator()(absl::string_view lhs,
119 absl::string_view rhs) const {
120 return compare_internal::compare_result_as_ordering(lhs.compare(rhs));
121 }
StringBtreeDefaultLessStringBtreeDefaultLess122 StringBtreeDefaultLess(std::less<absl::Cord>) {} // NOLINT
operatorStringBtreeDefaultLess123 absl::weak_ordering operator()(const absl::Cord &lhs,
124 const absl::Cord &rhs) const {
125 return compare_internal::compare_result_as_ordering(lhs.Compare(rhs));
126 }
operatorStringBtreeDefaultLess127 absl::weak_ordering operator()(const absl::Cord &lhs,
128 absl::string_view rhs) const {
129 return compare_internal::compare_result_as_ordering(lhs.Compare(rhs));
130 }
operatorStringBtreeDefaultLess131 absl::weak_ordering operator()(absl::string_view lhs,
132 const absl::Cord &rhs) const {
133 return compare_internal::compare_result_as_ordering(-rhs.Compare(lhs));
134 }
135 };
136
137 struct StringBtreeDefaultGreater {
138 using is_transparent = void;
139
140 StringBtreeDefaultGreater() = default;
141
StringBtreeDefaultGreaterStringBtreeDefaultGreater142 StringBtreeDefaultGreater(std::greater<std::string>) {} // NOLINT
StringBtreeDefaultGreaterStringBtreeDefaultGreater143 StringBtreeDefaultGreater(std::greater<absl::string_view>) {} // NOLINT
144
145 // Allow converting to std::greater for use in key_comp()/value_comp().
146 explicit operator std::greater<std::string>() const { return {}; }
147 explicit operator std::greater<absl::string_view>() const { return {}; }
148 explicit operator std::greater<absl::Cord>() const { return {}; }
149
operatorStringBtreeDefaultGreater150 absl::weak_ordering operator()(absl::string_view lhs,
151 absl::string_view rhs) const {
152 return compare_internal::compare_result_as_ordering(rhs.compare(lhs));
153 }
StringBtreeDefaultGreaterStringBtreeDefaultGreater154 StringBtreeDefaultGreater(std::greater<absl::Cord>) {} // NOLINT
operatorStringBtreeDefaultGreater155 absl::weak_ordering operator()(const absl::Cord &lhs,
156 const absl::Cord &rhs) const {
157 return compare_internal::compare_result_as_ordering(rhs.Compare(lhs));
158 }
operatorStringBtreeDefaultGreater159 absl::weak_ordering operator()(const absl::Cord &lhs,
160 absl::string_view rhs) const {
161 return compare_internal::compare_result_as_ordering(-lhs.Compare(rhs));
162 }
operatorStringBtreeDefaultGreater163 absl::weak_ordering operator()(absl::string_view lhs,
164 const absl::Cord &rhs) const {
165 return compare_internal::compare_result_as_ordering(rhs.Compare(lhs));
166 }
167 };
168
169 // See below comments for checked_compare.
170 template <typename Compare, bool is_class = std::is_class<Compare>::value>
171 struct checked_compare_base : Compare {
172 using Compare::Compare;
checked_compare_basechecked_compare_base173 explicit checked_compare_base(Compare c) : Compare(std::move(c)) {}
compchecked_compare_base174 const Compare &comp() const { return *this; }
175 };
176 template <typename Compare>
177 struct checked_compare_base<Compare, false> {
178 explicit checked_compare_base(Compare c) : compare(std::move(c)) {}
179 const Compare &comp() const { return compare; }
180 Compare compare;
181 };
182
183 // A mechanism for opting out of checked_compare for use only in btree_test.cc.
184 struct BtreeTestOnlyCheckedCompareOptOutBase {};
185
186 // A helper class to adapt the specified comparator for two use cases:
187 // (1) When using common Abseil string types with common comparison functors,
188 // convert a boolean comparison into a three-way comparison that returns an
189 // `absl::weak_ordering`. This helper class is specialized for
190 // less<std::string>, greater<std::string>, less<string_view>,
191 // greater<string_view>, less<absl::Cord>, and greater<absl::Cord>.
192 // (2) Adapt the comparator to diagnose cases of non-strict-weak-ordering (see
193 // https://en.cppreference.com/w/cpp/named_req/Compare) in debug mode. Whenever
194 // a comparison is made, we will make assertions to verify that the comparator
195 // is valid.
196 template <typename Compare, typename Key>
197 struct key_compare_adapter {
198 // Inherit from checked_compare_base to support function pointers and also
199 // keep empty-base-optimization (EBO) support for classes.
200 // Note: we can't use CompressedTuple here because that would interfere
201 // with the EBO for `btree::rightmost_`. `btree::rightmost_` is itself a
202 // CompressedTuple and nested `CompressedTuple`s don't support EBO.
203 // TODO(b/214288561): use CompressedTuple instead once it supports EBO for
204 // nested `CompressedTuple`s.
205 struct checked_compare : checked_compare_base<Compare> {
206 private:
207 using Base = typename checked_compare::checked_compare_base;
208 using Base::comp;
209
210 // If possible, returns whether `t` is equivalent to itself. We can only do
211 // this for `Key`s because we can't be sure that it's safe to call
212 // `comp()(k, k)` otherwise. Even if SFINAE allows it, there could be a
213 // compilation failure inside the implementation of the comparison operator.
214 bool is_self_equivalent(const Key &k) const {
215 // Note: this works for both boolean and three-way comparators.
216 return comp()(k, k) == 0;
217 }
218 // If we can't compare `t` with itself, returns true unconditionally.
219 template <typename T>
220 bool is_self_equivalent(const T &) const {
221 return true;
222 }
223
224 public:
225 using Base::Base;
226 checked_compare(Compare comp) : Base(std::move(comp)) {} // NOLINT
227
228 // Allow converting to Compare for use in key_comp()/value_comp().
229 explicit operator Compare() const { return comp(); }
230
231 template <typename T, typename U,
232 absl::enable_if_t<
233 std::is_same<bool, compare_result_t<Compare, T, U>>::value,
234 int> = 0>
235 bool operator()(const T &lhs, const U &rhs) const {
236 // NOTE: if any of these assertions fail, then the comparator does not
237 // establish a strict-weak-ordering (see
238 // https://en.cppreference.com/w/cpp/named_req/Compare).
239 assert(is_self_equivalent(lhs));
240 assert(is_self_equivalent(rhs));
241 const bool lhs_comp_rhs = comp()(lhs, rhs);
242 assert(!lhs_comp_rhs || !comp()(rhs, lhs));
243 return lhs_comp_rhs;
244 }
245
246 template <
247 typename T, typename U,
248 absl::enable_if_t<std::is_convertible<compare_result_t<Compare, T, U>,
249 absl::weak_ordering>::value,
250 int> = 0>
251 absl::weak_ordering operator()(const T &lhs, const U &rhs) const {
252 // NOTE: if any of these assertions fail, then the comparator does not
253 // establish a strict-weak-ordering (see
254 // https://en.cppreference.com/w/cpp/named_req/Compare).
255 assert(is_self_equivalent(lhs));
256 assert(is_self_equivalent(rhs));
257 const absl::weak_ordering lhs_comp_rhs = comp()(lhs, rhs);
258 #ifndef NDEBUG
259 const absl::weak_ordering rhs_comp_lhs = comp()(rhs, lhs);
260 if (lhs_comp_rhs > 0) {
261 assert(rhs_comp_lhs < 0 && "lhs_comp_rhs > 0 -> rhs_comp_lhs < 0");
262 } else if (lhs_comp_rhs == 0) {
263 assert(rhs_comp_lhs == 0 && "lhs_comp_rhs == 0 -> rhs_comp_lhs == 0");
264 } else {
265 assert(rhs_comp_lhs > 0 && "lhs_comp_rhs < 0 -> rhs_comp_lhs > 0");
266 }
267 #endif
268 return lhs_comp_rhs;
269 }
270 };
271 using type = absl::conditional_t<
272 std::is_base_of<BtreeTestOnlyCheckedCompareOptOutBase, Compare>::value,
273 Compare, checked_compare>;
274 };
275
276 template <>
277 struct key_compare_adapter<std::less<std::string>, std::string> {
278 using type = StringBtreeDefaultLess;
279 };
280
281 template <>
282 struct key_compare_adapter<std::greater<std::string>, std::string> {
283 using type = StringBtreeDefaultGreater;
284 };
285
286 template <>
287 struct key_compare_adapter<std::less<absl::string_view>, absl::string_view> {
288 using type = StringBtreeDefaultLess;
289 };
290
291 template <>
292 struct key_compare_adapter<std::greater<absl::string_view>, absl::string_view> {
293 using type = StringBtreeDefaultGreater;
294 };
295
296 template <>
297 struct key_compare_adapter<std::less<absl::Cord>, absl::Cord> {
298 using type = StringBtreeDefaultLess;
299 };
300
301 template <>
302 struct key_compare_adapter<std::greater<absl::Cord>, absl::Cord> {
303 using type = StringBtreeDefaultGreater;
304 };
305
306 // Detects an 'absl_btree_prefer_linear_node_search' member. This is
307 // a protocol used as an opt-in or opt-out of linear search.
308 //
309 // For example, this would be useful for key types that wrap an integer
310 // and define their own cheap operator<(). For example:
311 //
312 // class K {
313 // public:
314 // using absl_btree_prefer_linear_node_search = std::true_type;
315 // ...
316 // private:
317 // friend bool operator<(K a, K b) { return a.k_ < b.k_; }
318 // int k_;
319 // };
320 //
321 // btree_map<K, V> m; // Uses linear search
322 //
323 // If T has the preference tag, then it has a preference.
324 // Btree will use the tag's truth value.
325 template <typename T, typename = void>
326 struct has_linear_node_search_preference : std::false_type {};
327 template <typename T, typename = void>
328 struct prefers_linear_node_search : std::false_type {};
329 template <typename T>
330 struct has_linear_node_search_preference<
331 T, absl::void_t<typename T::absl_btree_prefer_linear_node_search>>
332 : std::true_type {};
333 template <typename T>
334 struct prefers_linear_node_search<
335 T, absl::void_t<typename T::absl_btree_prefer_linear_node_search>>
336 : T::absl_btree_prefer_linear_node_search {};
337
338 template <typename Compare, typename Key>
339 constexpr bool compare_has_valid_result_type() {
340 using compare_result_type = compare_result_t<Compare, Key, Key>;
341 return std::is_same<compare_result_type, bool>::value ||
342 std::is_convertible<compare_result_type, absl::weak_ordering>::value;
343 }
344
345 template <typename original_key_compare, typename value_type>
346 class map_value_compare {
347 template <typename Params>
348 friend class btree;
349
350 // Note: this `protected` is part of the API of std::map::value_compare. See
351 // https://en.cppreference.com/w/cpp/container/map/value_compare.
352 protected:
353 explicit map_value_compare(original_key_compare c) : comp(std::move(c)) {}
354
355 original_key_compare comp; // NOLINT
356
357 public:
358 auto operator()(const value_type &lhs, const value_type &rhs) const
359 -> decltype(comp(lhs.first, rhs.first)) {
360 return comp(lhs.first, rhs.first);
361 }
362 };
363
364 template <typename Key, typename Compare, typename Alloc, int TargetNodeSize,
365 bool IsMulti, bool IsMap, typename SlotPolicy>
366 struct common_params : common_policy_traits<SlotPolicy> {
367 using original_key_compare = Compare;
368
369 // If Compare is a common comparator for a string-like type, then we adapt it
370 // to use heterogeneous lookup and to be a key-compare-to comparator.
371 // We also adapt the comparator to diagnose invalid comparators in debug mode.
372 // We disable this when `Compare` is invalid in a way that will cause
373 // adaptation to fail (having invalid return type) so that we can give a
374 // better compilation failure in static_assert_validation. If we don't do
375 // this, then there will be cascading compilation failures that are confusing
376 // for users.
377 using key_compare =
378 absl::conditional_t<!compare_has_valid_result_type<Compare, Key>(),
379 Compare,
380 typename key_compare_adapter<Compare, Key>::type>;
381
382 static constexpr bool kIsKeyCompareStringAdapted =
383 std::is_same<key_compare, StringBtreeDefaultLess>::value ||
384 std::is_same<key_compare, StringBtreeDefaultGreater>::value;
385 static constexpr bool kIsKeyCompareTransparent =
386 IsTransparent<original_key_compare>::value || kIsKeyCompareStringAdapted;
387
388 // A type which indicates if we have a key-compare-to functor or a plain old
389 // key-compare functor.
390 using is_key_compare_to = btree_is_key_compare_to<key_compare, Key>;
391
392 using allocator_type = Alloc;
393 using key_type = Key;
394 using size_type = size_t;
395 using difference_type = ptrdiff_t;
396
397 using slot_policy = SlotPolicy;
398 using slot_type = typename slot_policy::slot_type;
399 using value_type = typename slot_policy::value_type;
400 using init_type = typename slot_policy::mutable_value_type;
401 using pointer = value_type *;
402 using const_pointer = const value_type *;
403 using reference = value_type &;
404 using const_reference = const value_type &;
405
406 using value_compare =
407 absl::conditional_t<IsMap,
408 map_value_compare<original_key_compare, value_type>,
409 original_key_compare>;
410 using is_map_container = std::integral_constant<bool, IsMap>;
411
412 // For the given lookup key type, returns whether we can have multiple
413 // equivalent keys in the btree. If this is a multi-container, then we can.
414 // Otherwise, we can have multiple equivalent keys only if all of the
415 // following conditions are met:
416 // - The comparator is transparent.
417 // - The lookup key type is not the same as key_type.
418 // - The comparator is not a StringBtreeDefault{Less,Greater} comparator
419 // that we know has the same equivalence classes for all lookup types.
420 template <typename LookupKey>
421 constexpr static bool can_have_multiple_equivalent_keys() {
422 return IsMulti || (IsTransparent<key_compare>::value &&
423 !std::is_same<LookupKey, Key>::value &&
424 !kIsKeyCompareStringAdapted);
425 }
426
427 enum {
428 kTargetNodeSize = TargetNodeSize,
429
430 // Upper bound for the available space for slots. This is largest for leaf
431 // nodes, which have overhead of at least a pointer + 4 bytes (for storing
432 // 3 field_types and an enum).
433 kNodeSlotSpace = TargetNodeSize - /*minimum overhead=*/(sizeof(void *) + 4),
434 };
435
436 // This is an integral type large enough to hold as many slots as will fit a
437 // node of TargetNodeSize bytes.
438 using node_count_type =
439 absl::conditional_t<(kNodeSlotSpace / sizeof(slot_type) >
440 (std::numeric_limits<uint8_t>::max)()),
441 uint16_t, uint8_t>; // NOLINT
442 };
443
444 // An adapter class that converts a lower-bound compare into an upper-bound
445 // compare. Note: there is no need to make a version of this adapter specialized
446 // for key-compare-to functors because the upper-bound (the first value greater
447 // than the input) is never an exact match.
448 template <typename Compare>
449 struct upper_bound_adapter {
450 explicit upper_bound_adapter(const Compare &c) : comp(c) {}
451 template <typename K1, typename K2>
452 bool operator()(const K1 &a, const K2 &b) const {
453 // Returns true when a is not greater than b.
454 return !compare_internal::compare_result_as_less_than(comp(b, a));
455 }
456
457 private:
458 Compare comp;
459 };
460
461 enum class MatchKind : uint8_t { kEq, kNe };
462
463 template <typename V, bool IsCompareTo>
464 struct SearchResult {
465 V value;
466 MatchKind match;
467
468 static constexpr bool HasMatch() { return true; }
469 bool IsEq() const { return match == MatchKind::kEq; }
470 };
471
472 // When we don't use CompareTo, `match` is not present.
473 // This ensures that callers can't use it accidentally when it provides no
474 // useful information.
475 template <typename V>
476 struct SearchResult<V, false> {
477 SearchResult() {}
478 explicit SearchResult(V v) : value(v) {}
479 SearchResult(V v, MatchKind /*match*/) : value(v) {}
480
481 V value;
482
483 static constexpr bool HasMatch() { return false; }
484 static constexpr bool IsEq() { return false; }
485 };
486
487 // A node in the btree holding. The same node type is used for both internal
488 // and leaf nodes in the btree, though the nodes are allocated in such a way
489 // that the children array is only valid in internal nodes.
490 template <typename Params>
491 class btree_node {
492 using is_key_compare_to = typename Params::is_key_compare_to;
493 using field_type = typename Params::node_count_type;
494 using allocator_type = typename Params::allocator_type;
495 using slot_type = typename Params::slot_type;
496 using original_key_compare = typename Params::original_key_compare;
497
498 public:
499 using params_type = Params;
500 using key_type = typename Params::key_type;
501 using value_type = typename Params::value_type;
502 using pointer = typename Params::pointer;
503 using const_pointer = typename Params::const_pointer;
504 using reference = typename Params::reference;
505 using const_reference = typename Params::const_reference;
506 using key_compare = typename Params::key_compare;
507 using size_type = typename Params::size_type;
508 using difference_type = typename Params::difference_type;
509
510 // Btree decides whether to use linear node search as follows:
511 // - If the comparator expresses a preference, use that.
512 // - If the key expresses a preference, use that.
513 // - If the key is arithmetic and the comparator is std::less or
514 // std::greater, choose linear.
515 // - Otherwise, choose binary.
516 // TODO(ezb): Might make sense to add condition(s) based on node-size.
517 using use_linear_search = std::integral_constant<
518 bool, has_linear_node_search_preference<original_key_compare>::value
519 ? prefers_linear_node_search<original_key_compare>::value
520 : has_linear_node_search_preference<key_type>::value
521 ? prefers_linear_node_search<key_type>::value
522 : std::is_arithmetic<key_type>::value &&
523 (std::is_same<std::less<key_type>,
524 original_key_compare>::value ||
525 std::is_same<std::greater<key_type>,
526 original_key_compare>::value)>;
527
528 // This class is organized by absl::container_internal::Layout as if it had
529 // the following structure:
530 // // A pointer to the node's parent.
531 // btree_node *parent;
532 //
533 // // When ABSL_BTREE_ENABLE_GENERATIONS is defined, we also have a
534 // // generation integer in order to check that when iterators are
535 // // used, they haven't been invalidated already. Only the generation on
536 // // the root is used, but we have one on each node because whether a node
537 // // is root or not can change.
538 // uint32_t generation;
539 //
540 // // The position of the node in the node's parent.
541 // field_type position;
542 // // The index of the first populated value in `values`.
543 // // TODO(ezb): right now, `start` is always 0. Update insertion/merge
544 // // logic to allow for floating storage within nodes.
545 // field_type start;
546 // // The index after the last populated value in `values`. Currently, this
547 // // is the same as the count of values.
548 // field_type finish;
549 // // The maximum number of values the node can hold. This is an integer in
550 // // [1, kNodeSlots] for root leaf nodes, kNodeSlots for non-root leaf
551 // // nodes, and kInternalNodeMaxCount (as a sentinel value) for internal
552 // // nodes (even though there are still kNodeSlots values in the node).
553 // // TODO(ezb): make max_count use only 4 bits and record log2(capacity)
554 // // to free extra bits for is_root, etc.
555 // field_type max_count;
556 //
557 // // The array of values. The capacity is `max_count` for leaf nodes and
558 // // kNodeSlots for internal nodes. Only the values in
559 // // [start, finish) have been initialized and are valid.
560 // slot_type values[max_count];
561 //
562 // // The array of child pointers. The keys in children[i] are all less
563 // // than key(i). The keys in children[i + 1] are all greater than key(i).
564 // // There are 0 children for leaf nodes and kNodeSlots + 1 children for
565 // // internal nodes.
566 // btree_node *children[kNodeSlots + 1];
567 //
568 // This class is only constructed by EmptyNodeType. Normally, pointers to the
569 // layout above are allocated, cast to btree_node*, and de-allocated within
570 // the btree implementation.
571 ~btree_node() = default;
572 btree_node(btree_node const &) = delete;
573 btree_node &operator=(btree_node const &) = delete;
574
575 // Public for EmptyNodeType.
576 constexpr static size_type Alignment() {
577 static_assert(LeafLayout(1).Alignment() == InternalLayout().Alignment(),
578 "Alignment of all nodes must be equal.");
579 return InternalLayout().Alignment();
580 }
581
582 protected:
583 btree_node() = default;
584
585 private:
586 using layout_type =
587 absl::container_internal::Layout<btree_node *, uint32_t, field_type,
588 slot_type, btree_node *>;
589 constexpr static size_type SizeWithNSlots(size_type n) {
590 return layout_type(
591 /*parent*/ 1,
592 /*generation*/ BtreeGenerationsEnabled() ? 1 : 0,
593 /*position, start, finish, max_count*/ 4,
594 /*slots*/ n,
595 /*children*/ 0)
596 .AllocSize();
597 }
598 // A lower bound for the overhead of fields other than slots in a leaf node.
599 constexpr static size_type MinimumOverhead() {
600 return SizeWithNSlots(1) - sizeof(slot_type);
601 }
602
603 // Compute how many values we can fit onto a leaf node taking into account
604 // padding.
605 constexpr static size_type NodeTargetSlots(const size_type begin,
606 const size_type end) {
607 return begin == end ? begin
608 : SizeWithNSlots((begin + end) / 2 + 1) >
609 params_type::kTargetNodeSize
610 ? NodeTargetSlots(begin, (begin + end) / 2)
611 : NodeTargetSlots((begin + end) / 2 + 1, end);
612 }
613
614 constexpr static size_type kTargetNodeSize = params_type::kTargetNodeSize;
615 constexpr static size_type kNodeTargetSlots =
616 NodeTargetSlots(0, kTargetNodeSize);
617
618 // We need a minimum of 3 slots per internal node in order to perform
619 // splitting (1 value for the two nodes involved in the split and 1 value
620 // propagated to the parent as the delimiter for the split). For performance
621 // reasons, we don't allow 3 slots-per-node due to bad worst case occupancy of
622 // 1/3 (for a node, not a b-tree).
623 constexpr static size_type kMinNodeSlots = 4;
624
625 constexpr static size_type kNodeSlots =
626 kNodeTargetSlots >= kMinNodeSlots ? kNodeTargetSlots : kMinNodeSlots;
627
628 // The node is internal (i.e. is not a leaf node) if and only if `max_count`
629 // has this value.
630 constexpr static field_type kInternalNodeMaxCount = 0;
631
632 constexpr static layout_type Layout(const size_type slot_count,
633 const size_type child_count) {
634 return layout_type(
635 /*parent*/ 1,
636 /*generation*/ BtreeGenerationsEnabled() ? 1 : 0,
637 /*position, start, finish, max_count*/ 4,
638 /*slots*/ slot_count,
639 /*children*/ child_count);
640 }
641 // Leaves can have less than kNodeSlots values.
642 constexpr static layout_type LeafLayout(
643 const size_type slot_count = kNodeSlots) {
644 return Layout(slot_count, 0);
645 }
646 constexpr static layout_type InternalLayout() {
647 return Layout(kNodeSlots, kNodeSlots + 1);
648 }
649 constexpr static size_type LeafSize(const size_type slot_count = kNodeSlots) {
650 return LeafLayout(slot_count).AllocSize();
651 }
652 constexpr static size_type InternalSize() {
653 return InternalLayout().AllocSize();
654 }
655
656 // N is the index of the type in the Layout definition.
657 // ElementType<N> is the Nth type in the Layout definition.
658 template <size_type N>
659 inline typename layout_type::template ElementType<N> *GetField() {
660 // We assert that we don't read from values that aren't there.
661 assert(N < 4 || is_internal());
662 return InternalLayout().template Pointer<N>(reinterpret_cast<char *>(this));
663 }
664 template <size_type N>
665 inline const typename layout_type::template ElementType<N> *GetField() const {
666 assert(N < 4 || is_internal());
667 return InternalLayout().template Pointer<N>(
668 reinterpret_cast<const char *>(this));
669 }
670 void set_parent(btree_node *p) { *GetField<0>() = p; }
671 field_type &mutable_finish() { return GetField<2>()[2]; }
672 slot_type *slot(size_type i) { return &GetField<3>()[i]; }
673 slot_type *start_slot() { return slot(start()); }
674 slot_type *finish_slot() { return slot(finish()); }
675 const slot_type *slot(size_type i) const { return &GetField<3>()[i]; }
676 void set_position(field_type v) { GetField<2>()[0] = v; }
677 void set_start(field_type v) { GetField<2>()[1] = v; }
678 void set_finish(field_type v) { GetField<2>()[2] = v; }
679 // This method is only called by the node init methods.
680 void set_max_count(field_type v) { GetField<2>()[3] = v; }
681
682 public:
683 // Whether this is a leaf node or not. This value doesn't change after the
684 // node is created.
685 bool is_leaf() const { return GetField<2>()[3] != kInternalNodeMaxCount; }
686 // Whether this is an internal node or not. This value doesn't change after
687 // the node is created.
688 bool is_internal() const { return !is_leaf(); }
689
690 // Getter for the position of this node in its parent.
691 field_type position() const { return GetField<2>()[0]; }
692
693 // Getter for the offset of the first value in the `values` array.
694 field_type start() const {
695 // TODO(ezb): when floating storage is implemented, return GetField<2>()[1];
696 assert(GetField<2>()[1] == 0);
697 return 0;
698 }
699
700 // Getter for the offset after the last value in the `values` array.
701 field_type finish() const { return GetField<2>()[2]; }
702
703 // Getters for the number of values stored in this node.
704 field_type count() const {
705 assert(finish() >= start());
706 return finish() - start();
707 }
708 field_type max_count() const {
709 // Internal nodes have max_count==kInternalNodeMaxCount.
710 // Leaf nodes have max_count in [1, kNodeSlots].
711 const field_type max_count = GetField<2>()[3];
712 return max_count == field_type{kInternalNodeMaxCount}
713 ? field_type{kNodeSlots}
714 : max_count;
715 }
716
717 // Getter for the parent of this node.
718 btree_node *parent() const { return *GetField<0>(); }
719 // Getter for whether the node is the root of the tree. The parent of the
720 // root of the tree is the leftmost node in the tree which is guaranteed to
721 // be a leaf.
722 bool is_root() const { return parent()->is_leaf(); }
723 void make_root() {
724 assert(parent()->is_root());
725 set_generation(parent()->generation());
726 set_parent(parent()->parent());
727 }
728
729 // Gets the root node's generation integer, which is the one used by the tree.
730 uint32_t *get_root_generation() const {
731 assert(BtreeGenerationsEnabled());
732 const btree_node *curr = this;
733 for (; !curr->is_root(); curr = curr->parent()) continue;
734 return const_cast<uint32_t *>(&curr->GetField<1>()[0]);
735 }
736
737 // Returns the generation for iterator validation.
738 uint32_t generation() const {
739 return BtreeGenerationsEnabled() ? *get_root_generation() : 0;
740 }
741 // Updates generation. Should only be called on a root node or during node
742 // initialization.
743 void set_generation(uint32_t generation) {
744 if (BtreeGenerationsEnabled()) GetField<1>()[0] = generation;
745 }
746 // Updates the generation. We do this whenever the node is mutated.
747 void next_generation() {
748 if (BtreeGenerationsEnabled()) ++*get_root_generation();
749 }
750
751 // Getters for the key/value at position i in the node.
752 const key_type &key(size_type i) const { return params_type::key(slot(i)); }
753 reference value(size_type i) { return params_type::element(slot(i)); }
754 const_reference value(size_type i) const {
755 return params_type::element(slot(i));
756 }
757
758 // Getters/setter for the child at position i in the node.
759 btree_node *child(field_type i) const { return GetField<4>()[i]; }
760 btree_node *start_child() const { return child(start()); }
761 btree_node *&mutable_child(field_type i) { return GetField<4>()[i]; }
762 void clear_child(field_type i) {
763 absl::container_internal::SanitizerPoisonObject(&mutable_child(i));
764 }
765 void set_child_noupdate_position(field_type i, btree_node *c) {
766 absl::container_internal::SanitizerUnpoisonObject(&mutable_child(i));
767 mutable_child(i) = c;
768 }
769 void set_child(field_type i, btree_node *c) {
770 set_child_noupdate_position(i, c);
771 c->set_position(i);
772 }
773 void init_child(field_type i, btree_node *c) {
774 set_child(i, c);
775 c->set_parent(this);
776 }
777
778 // Returns the position of the first value whose key is not less than k.
779 template <typename K>
780 SearchResult<size_type, is_key_compare_to::value> lower_bound(
781 const K &k, const key_compare &comp) const {
782 return use_linear_search::value ? linear_search(k, comp)
783 : binary_search(k, comp);
784 }
785 // Returns the position of the first value whose key is greater than k.
786 template <typename K>
787 size_type upper_bound(const K &k, const key_compare &comp) const {
788 auto upper_compare = upper_bound_adapter<key_compare>(comp);
789 return use_linear_search::value ? linear_search(k, upper_compare).value
790 : binary_search(k, upper_compare).value;
791 }
792
793 template <typename K, typename Compare>
794 SearchResult<size_type, btree_is_key_compare_to<Compare, key_type>::value>
795 linear_search(const K &k, const Compare &comp) const {
796 return linear_search_impl(k, start(), finish(), comp,
797 btree_is_key_compare_to<Compare, key_type>());
798 }
799
800 template <typename K, typename Compare>
801 SearchResult<size_type, btree_is_key_compare_to<Compare, key_type>::value>
802 binary_search(const K &k, const Compare &comp) const {
803 return binary_search_impl(k, start(), finish(), comp,
804 btree_is_key_compare_to<Compare, key_type>());
805 }
806
807 // Returns the position of the first value whose key is not less than k using
808 // linear search performed using plain compare.
809 template <typename K, typename Compare>
810 SearchResult<size_type, false> linear_search_impl(
811 const K &k, size_type s, const size_type e, const Compare &comp,
812 std::false_type /* IsCompareTo */) const {
813 while (s < e) {
814 if (!comp(key(s), k)) {
815 break;
816 }
817 ++s;
818 }
819 return SearchResult<size_type, false>{s};
820 }
821
822 // Returns the position of the first value whose key is not less than k using
823 // linear search performed using compare-to.
824 template <typename K, typename Compare>
825 SearchResult<size_type, true> linear_search_impl(
826 const K &k, size_type s, const size_type e, const Compare &comp,
827 std::true_type /* IsCompareTo */) const {
828 while (s < e) {
829 const absl::weak_ordering c = comp(key(s), k);
830 if (c == 0) {
831 return {s, MatchKind::kEq};
832 } else if (c > 0) {
833 break;
834 }
835 ++s;
836 }
837 return {s, MatchKind::kNe};
838 }
839
840 // Returns the position of the first value whose key is not less than k using
841 // binary search performed using plain compare.
842 template <typename K, typename Compare>
843 SearchResult<size_type, false> binary_search_impl(
844 const K &k, size_type s, size_type e, const Compare &comp,
845 std::false_type /* IsCompareTo */) const {
846 while (s != e) {
847 const size_type mid = (s + e) >> 1;
848 if (comp(key(mid), k)) {
849 s = mid + 1;
850 } else {
851 e = mid;
852 }
853 }
854 return SearchResult<size_type, false>{s};
855 }
856
857 // Returns the position of the first value whose key is not less than k using
858 // binary search performed using compare-to.
859 template <typename K, typename CompareTo>
860 SearchResult<size_type, true> binary_search_impl(
861 const K &k, size_type s, size_type e, const CompareTo &comp,
862 std::true_type /* IsCompareTo */) const {
863 if (params_type::template can_have_multiple_equivalent_keys<K>()) {
864 MatchKind exact_match = MatchKind::kNe;
865 while (s != e) {
866 const size_type mid = (s + e) >> 1;
867 const absl::weak_ordering c = comp(key(mid), k);
868 if (c < 0) {
869 s = mid + 1;
870 } else {
871 e = mid;
872 if (c == 0) {
873 // Need to return the first value whose key is not less than k,
874 // which requires continuing the binary search if there could be
875 // multiple equivalent keys.
876 exact_match = MatchKind::kEq;
877 }
878 }
879 }
880 return {s, exact_match};
881 } else { // Can't have multiple equivalent keys.
882 while (s != e) {
883 const size_type mid = (s + e) >> 1;
884 const absl::weak_ordering c = comp(key(mid), k);
885 if (c < 0) {
886 s = mid + 1;
887 } else if (c > 0) {
888 e = mid;
889 } else {
890 return {mid, MatchKind::kEq};
891 }
892 }
893 return {s, MatchKind::kNe};
894 }
895 }
896
897 // Returns whether key i is ordered correctly with respect to the other keys
898 // in the node. The motivation here is to detect comparators that violate
899 // transitivity. Note: we only do comparisons of keys on this node rather than
900 // the whole tree so that this is constant time.
901 template <typename Compare>
902 bool is_ordered_correctly(field_type i, const Compare &comp) const {
903 if (std::is_base_of<BtreeTestOnlyCheckedCompareOptOutBase,
904 Compare>::value ||
905 params_type::kIsKeyCompareStringAdapted) {
906 return true;
907 }
908
909 const auto compare = [&](field_type a, field_type b) {
910 const absl::weak_ordering cmp =
911 compare_internal::do_three_way_comparison(comp, key(a), key(b));
912 return cmp < 0 ? -1 : cmp > 0 ? 1 : 0;
913 };
914 int cmp = -1;
915 constexpr bool kCanHaveEquivKeys =
916 params_type::template can_have_multiple_equivalent_keys<key_type>();
917 for (field_type j = start(); j < finish(); ++j) {
918 if (j == i) {
919 if (cmp > 0) return false;
920 continue;
921 }
922 int new_cmp = compare(j, i);
923 if (new_cmp < cmp || (!kCanHaveEquivKeys && new_cmp == 0)) return false;
924 cmp = new_cmp;
925 }
926 return true;
927 }
928
929 // Emplaces a value at position i, shifting all existing values and
930 // children at positions >= i to the right by 1.
931 template <typename... Args>
932 void emplace_value(field_type i, allocator_type *alloc, Args &&...args);
933
934 // Removes the values at positions [i, i + to_erase), shifting all existing
935 // values and children after that range to the left by to_erase. Clears all
936 // children between [i, i + to_erase).
937 void remove_values(field_type i, field_type to_erase, allocator_type *alloc);
938
939 // Rebalances a node with its right sibling.
940 void rebalance_right_to_left(field_type to_move, btree_node *right,
941 allocator_type *alloc);
942 void rebalance_left_to_right(field_type to_move, btree_node *right,
943 allocator_type *alloc);
944
945 // Splits a node, moving a portion of the node's values to its right sibling.
946 void split(int insert_position, btree_node *dest, allocator_type *alloc);
947
948 // Merges a node with its right sibling, moving all of the values and the
949 // delimiting key in the parent node onto itself, and deleting the src node.
950 void merge(btree_node *src, allocator_type *alloc);
951
952 // Node allocation/deletion routines.
953 void init_leaf(field_type position, field_type max_count,
954 btree_node *parent) {
955 set_generation(0);
956 set_parent(parent);
957 set_position(position);
958 set_start(0);
959 set_finish(0);
960 set_max_count(max_count);
961 absl::container_internal::SanitizerPoisonMemoryRegion(
962 start_slot(), max_count * sizeof(slot_type));
963 }
964 void init_internal(field_type position, btree_node *parent) {
965 init_leaf(position, kNodeSlots, parent);
966 // Set `max_count` to a sentinel value to indicate that this node is
967 // internal.
968 set_max_count(kInternalNodeMaxCount);
969 absl::container_internal::SanitizerPoisonMemoryRegion(
970 &mutable_child(start()), (kNodeSlots + 1) * sizeof(btree_node *));
971 }
972
973 static void deallocate(const size_type size, btree_node *node,
974 allocator_type *alloc) {
975 absl::container_internal::SanitizerUnpoisonMemoryRegion(node, size);
976 absl::container_internal::Deallocate<Alignment()>(alloc, node, size);
977 }
978
979 // Deletes a node and all of its children.
980 static void clear_and_delete(btree_node *node, allocator_type *alloc);
981
982 private:
983 template <typename... Args>
984 void value_init(const field_type i, allocator_type *alloc, Args &&...args) {
985 next_generation();
986 absl::container_internal::SanitizerUnpoisonObject(slot(i));
987 params_type::construct(alloc, slot(i), std::forward<Args>(args)...);
988 }
989 void value_destroy(const field_type i, allocator_type *alloc) {
990 next_generation();
991 params_type::destroy(alloc, slot(i));
992 absl::container_internal::SanitizerPoisonObject(slot(i));
993 }
994 void value_destroy_n(const field_type i, const field_type n,
995 allocator_type *alloc) {
996 next_generation();
997 for (slot_type *s = slot(i), *end = slot(i + n); s != end; ++s) {
998 params_type::destroy(alloc, s);
999 absl::container_internal::SanitizerPoisonObject(s);
1000 }
1001 }
1002
1003 static void transfer(slot_type *dest, slot_type *src, allocator_type *alloc) {
1004 absl::container_internal::SanitizerUnpoisonObject(dest);
1005 params_type::transfer(alloc, dest, src);
1006 absl::container_internal::SanitizerPoisonObject(src);
1007 }
1008
1009 // Transfers value from slot `src_i` in `src_node` to slot `dest_i` in `this`.
1010 void transfer(const size_type dest_i, const size_type src_i,
1011 btree_node *src_node, allocator_type *alloc) {
1012 next_generation();
1013 transfer(slot(dest_i), src_node->slot(src_i), alloc);
1014 }
1015
1016 // Transfers `n` values starting at value `src_i` in `src_node` into the
1017 // values starting at value `dest_i` in `this`.
1018 void transfer_n(const size_type n, const size_type dest_i,
1019 const size_type src_i, btree_node *src_node,
1020 allocator_type *alloc) {
1021 next_generation();
1022 for (slot_type *src = src_node->slot(src_i), *end = src + n,
1023 *dest = slot(dest_i);
1024 src != end; ++src, ++dest) {
1025 transfer(dest, src, alloc);
1026 }
1027 }
1028
1029 // Same as above, except that we start at the end and work our way to the
1030 // beginning.
1031 void transfer_n_backward(const size_type n, const size_type dest_i,
1032 const size_type src_i, btree_node *src_node,
1033 allocator_type *alloc) {
1034 next_generation();
1035 for (slot_type *src = src_node->slot(src_i + n), *end = src - n,
1036 *dest = slot(dest_i + n);
1037 src != end; --src, --dest) {
1038 // If we modified the loop index calculations above to avoid the -1s here,
1039 // it would result in UB in the computation of `end` (and possibly `src`
1040 // as well, if n == 0), since slot() is effectively an array index and it
1041 // is UB to compute the address of any out-of-bounds array element except
1042 // for one-past-the-end.
1043 transfer(dest - 1, src - 1, alloc);
1044 }
1045 }
1046
1047 template <typename P>
1048 friend class btree;
1049 template <typename N, typename R, typename P>
1050 friend class btree_iterator;
1051 friend class BtreeNodePeer;
1052 friend struct btree_access;
1053 };
1054
1055 template <typename Node>
1056 bool AreNodesFromSameContainer(const Node *node_a, const Node *node_b) {
1057 // If either node is null, then give up on checking whether they're from the
1058 // same container. (If exactly one is null, then we'll trigger the
1059 // default-constructed assert in Equals.)
1060 if (node_a == nullptr || node_b == nullptr) return true;
1061 while (!node_a->is_root()) node_a = node_a->parent();
1062 while (!node_b->is_root()) node_b = node_b->parent();
1063 return node_a == node_b;
1064 }
1065
1066 class btree_iterator_generation_info_enabled {
1067 public:
1068 explicit btree_iterator_generation_info_enabled(uint32_t g)
1069 : generation_(g) {}
1070
1071 // Updates the generation. For use internally right before we return an
1072 // iterator to the user.
1073 template <typename Node>
1074 void update_generation(const Node *node) {
1075 if (node != nullptr) generation_ = node->generation();
1076 }
1077 uint32_t generation() const { return generation_; }
1078
1079 template <typename Node>
1080 void assert_valid_generation(const Node *node) const {
1081 if (node != nullptr && node->generation() != generation_) {
1082 ABSL_INTERNAL_LOG(
1083 FATAL,
1084 "Attempting to use an invalidated iterator. The corresponding b-tree "
1085 "container has been mutated since this iterator was constructed.");
1086 }
1087 }
1088
1089 private:
1090 // Used to check that the iterator hasn't been invalidated.
1091 uint32_t generation_;
1092 };
1093
1094 class btree_iterator_generation_info_disabled {
1095 public:
1096 explicit btree_iterator_generation_info_disabled(uint32_t) {}
1097 static void update_generation(const void *) {}
1098 static uint32_t generation() { return 0; }
1099 static void assert_valid_generation(const void *) {}
1100 };
1101
1102 #ifdef ABSL_BTREE_ENABLE_GENERATIONS
1103 using btree_iterator_generation_info = btree_iterator_generation_info_enabled;
1104 #else
1105 using btree_iterator_generation_info = btree_iterator_generation_info_disabled;
1106 #endif
1107
1108 template <typename Node, typename Reference, typename Pointer>
1109 class btree_iterator : private btree_iterator_generation_info {
1110 using field_type = typename Node::field_type;
1111 using key_type = typename Node::key_type;
1112 using size_type = typename Node::size_type;
1113 using params_type = typename Node::params_type;
1114 using is_map_container = typename params_type::is_map_container;
1115
1116 using node_type = Node;
1117 using normal_node = typename std::remove_const<Node>::type;
1118 using const_node = const Node;
1119 using normal_pointer = typename params_type::pointer;
1120 using normal_reference = typename params_type::reference;
1121 using const_pointer = typename params_type::const_pointer;
1122 using const_reference = typename params_type::const_reference;
1123 using slot_type = typename params_type::slot_type;
1124
1125 // In sets, all iterators are const.
1126 using iterator = absl::conditional_t<
1127 is_map_container::value,
1128 btree_iterator<normal_node, normal_reference, normal_pointer>,
1129 btree_iterator<normal_node, const_reference, const_pointer>>;
1130 using const_iterator =
1131 btree_iterator<const_node, const_reference, const_pointer>;
1132
1133 public:
1134 // These aliases are public for std::iterator_traits.
1135 using difference_type = typename Node::difference_type;
1136 using value_type = typename params_type::value_type;
1137 using pointer = Pointer;
1138 using reference = Reference;
1139 using iterator_category = std::bidirectional_iterator_tag;
1140
1141 btree_iterator() : btree_iterator(nullptr, -1) {}
1142 explicit btree_iterator(Node *n) : btree_iterator(n, n->start()) {}
1143 btree_iterator(Node *n, int p)
1144 : btree_iterator_generation_info(n != nullptr ? n->generation()
1145 : ~uint32_t{}),
1146 node_(n),
1147 position_(p) {}
1148
1149 // NOTE: this SFINAE allows for implicit conversions from iterator to
1150 // const_iterator, but it specifically avoids hiding the copy constructor so
1151 // that the trivial one will be used when possible.
1152 template <typename N, typename R, typename P,
1153 absl::enable_if_t<
1154 std::is_same<btree_iterator<N, R, P>, iterator>::value &&
1155 std::is_same<btree_iterator, const_iterator>::value,
1156 int> = 0>
1157 btree_iterator(const btree_iterator<N, R, P> other) // NOLINT
1158 : btree_iterator_generation_info(other),
1159 node_(other.node_),
1160 position_(other.position_) {}
1161
1162 bool operator==(const iterator &other) const {
1163 return Equals(other);
1164 }
1165 bool operator==(const const_iterator &other) const {
1166 return Equals(other);
1167 }
1168 bool operator!=(const iterator &other) const {
1169 return !Equals(other);
1170 }
1171 bool operator!=(const const_iterator &other) const {
1172 return !Equals(other);
1173 }
1174
1175 // Returns n such that n calls to ++other yields *this.
1176 // Precondition: n exists.
1177 difference_type operator-(const_iterator other) const {
1178 if (node_ == other.node_) {
1179 if (node_->is_leaf()) return position_ - other.position_;
1180 if (position_ == other.position_) return 0;
1181 }
1182 return distance_slow(other);
1183 }
1184
1185 // Accessors for the key/value the iterator is pointing at.
1186 reference operator*() const {
1187 ABSL_HARDENING_ASSERT(node_ != nullptr);
1188 assert_valid_generation(node_);
1189 ABSL_HARDENING_ASSERT(position_ >= node_->start());
1190 if (position_ >= node_->finish()) {
1191 ABSL_HARDENING_ASSERT(!IsEndIterator() && "Dereferencing end() iterator");
1192 ABSL_HARDENING_ASSERT(position_ < node_->finish());
1193 }
1194 return node_->value(static_cast<field_type>(position_));
1195 }
1196 pointer operator->() const { return &operator*(); }
1197
1198 btree_iterator &operator++() {
1199 increment();
1200 return *this;
1201 }
1202 btree_iterator &operator--() {
1203 decrement();
1204 return *this;
1205 }
1206 btree_iterator operator++(int) {
1207 btree_iterator tmp = *this;
1208 ++*this;
1209 return tmp;
1210 }
1211 btree_iterator operator--(int) {
1212 btree_iterator tmp = *this;
1213 --*this;
1214 return tmp;
1215 }
1216
1217 private:
1218 friend iterator;
1219 friend const_iterator;
1220 template <typename Params>
1221 friend class btree;
1222 template <typename Tree>
1223 friend class btree_container;
1224 template <typename Tree>
1225 friend class btree_set_container;
1226 template <typename Tree>
1227 friend class btree_map_container;
1228 template <typename Tree>
1229 friend class btree_multiset_container;
1230 template <typename TreeType, typename CheckerType>
1231 friend class base_checker;
1232 friend struct btree_access;
1233
1234 // This SFINAE allows explicit conversions from const_iterator to
1235 // iterator, but also avoids hiding the copy constructor.
1236 // NOTE: the const_cast is safe because this constructor is only called by
1237 // non-const methods and the container owns the nodes.
1238 template <typename N, typename R, typename P,
1239 absl::enable_if_t<
1240 std::is_same<btree_iterator<N, R, P>, const_iterator>::value &&
1241 std::is_same<btree_iterator, iterator>::value,
1242 int> = 0>
1243 explicit btree_iterator(const btree_iterator<N, R, P> other)
1244 : btree_iterator_generation_info(other.generation()),
1245 node_(const_cast<node_type *>(other.node_)),
1246 position_(other.position_) {}
1247
1248 bool Equals(const const_iterator other) const {
1249 ABSL_HARDENING_ASSERT(((node_ == nullptr && other.node_ == nullptr) ||
1250 (node_ != nullptr && other.node_ != nullptr)) &&
1251 "Comparing default-constructed iterator with "
1252 "non-default-constructed iterator.");
1253 // Note: we use assert instead of ABSL_HARDENING_ASSERT here because this
1254 // changes the complexity of Equals from O(1) to O(log(N) + log(M)) where
1255 // N/M are sizes of the containers containing node_/other.node_.
1256 assert(AreNodesFromSameContainer(node_, other.node_) &&
1257 "Comparing iterators from different containers.");
1258 assert_valid_generation(node_);
1259 other.assert_valid_generation(other.node_);
1260 return node_ == other.node_ && position_ == other.position_;
1261 }
1262
1263 bool IsEndIterator() const {
1264 if (position_ != node_->finish()) return false;
1265 node_type *node = node_;
1266 while (!node->is_root()) {
1267 if (node->position() != node->parent()->finish()) return false;
1268 node = node->parent();
1269 }
1270 return true;
1271 }
1272
1273 // Returns n such that n calls to ++other yields *this.
1274 // Precondition: n exists && (this->node_ != other.node_ ||
1275 // !this->node_->is_leaf() || this->position_ != other.position_).
1276 difference_type distance_slow(const_iterator other) const;
1277
1278 // Increment/decrement the iterator.
1279 void increment() {
1280 assert_valid_generation(node_);
1281 if (node_->is_leaf() && ++position_ < node_->finish()) {
1282 return;
1283 }
1284 increment_slow();
1285 }
1286 void increment_slow();
1287
1288 void decrement() {
1289 assert_valid_generation(node_);
1290 if (node_->is_leaf() && --position_ >= node_->start()) {
1291 return;
1292 }
1293 decrement_slow();
1294 }
1295 void decrement_slow();
1296
1297 const key_type &key() const {
1298 return node_->key(static_cast<size_type>(position_));
1299 }
1300 decltype(std::declval<Node *>()->slot(0)) slot() {
1301 return node_->slot(static_cast<size_type>(position_));
1302 }
1303
1304 void update_generation() {
1305 btree_iterator_generation_info::update_generation(node_);
1306 }
1307
1308 // The node in the tree the iterator is pointing at.
1309 Node *node_;
1310 // The position within the node of the tree the iterator is pointing at.
1311 // NOTE: this is an int rather than a field_type because iterators can point
1312 // to invalid positions (such as -1) in certain circumstances.
1313 int position_;
1314 };
1315
1316 template <typename Params>
1317 class btree {
1318 using node_type = btree_node<Params>;
1319 using is_key_compare_to = typename Params::is_key_compare_to;
1320 using field_type = typename node_type::field_type;
1321
1322 // We use a static empty node for the root/leftmost/rightmost of empty btrees
1323 // in order to avoid branching in begin()/end().
1324 struct alignas(node_type::Alignment()) EmptyNodeType : node_type {
1325 using field_type = typename node_type::field_type;
1326 node_type *parent;
1327 #ifdef ABSL_BTREE_ENABLE_GENERATIONS
1328 uint32_t generation = 0;
1329 #endif
1330 field_type position = 0;
1331 field_type start = 0;
1332 field_type finish = 0;
1333 // max_count must be != kInternalNodeMaxCount (so that this node is regarded
1334 // as a leaf node). max_count() is never called when the tree is empty.
1335 field_type max_count = node_type::kInternalNodeMaxCount + 1;
1336
1337 #ifdef _MSC_VER
1338 // MSVC has constexpr code generations bugs here.
1339 EmptyNodeType() : parent(this) {}
1340 #else
1341 explicit constexpr EmptyNodeType(node_type *p) : parent(p) {}
1342 #endif
1343 };
1344
1345 static node_type *EmptyNode() {
1346 #ifdef _MSC_VER
1347 static EmptyNodeType *empty_node = new EmptyNodeType;
1348 // This assert fails on some other construction methods.
1349 assert(empty_node->parent == empty_node);
1350 return empty_node;
1351 #else
1352 static constexpr EmptyNodeType empty_node(
1353 const_cast<EmptyNodeType *>(&empty_node));
1354 return const_cast<EmptyNodeType *>(&empty_node);
1355 #endif
1356 }
1357
1358 enum : uint32_t {
1359 kNodeSlots = node_type::kNodeSlots,
1360 kMinNodeValues = kNodeSlots / 2,
1361 };
1362
1363 struct node_stats {
1364 using size_type = typename Params::size_type;
1365
1366 node_stats(size_type l, size_type i) : leaf_nodes(l), internal_nodes(i) {}
1367
1368 node_stats &operator+=(const node_stats &other) {
1369 leaf_nodes += other.leaf_nodes;
1370 internal_nodes += other.internal_nodes;
1371 return *this;
1372 }
1373
1374 size_type leaf_nodes;
1375 size_type internal_nodes;
1376 };
1377
1378 public:
1379 using key_type = typename Params::key_type;
1380 using value_type = typename Params::value_type;
1381 using size_type = typename Params::size_type;
1382 using difference_type = typename Params::difference_type;
1383 using key_compare = typename Params::key_compare;
1384 using original_key_compare = typename Params::original_key_compare;
1385 using value_compare = typename Params::value_compare;
1386 using allocator_type = typename Params::allocator_type;
1387 using reference = typename Params::reference;
1388 using const_reference = typename Params::const_reference;
1389 using pointer = typename Params::pointer;
1390 using const_pointer = typename Params::const_pointer;
1391 using iterator =
1392 typename btree_iterator<node_type, reference, pointer>::iterator;
1393 using const_iterator = typename iterator::const_iterator;
1394 using reverse_iterator = std::reverse_iterator<iterator>;
1395 using const_reverse_iterator = std::reverse_iterator<const_iterator>;
1396 using node_handle_type = node_handle<Params, Params, allocator_type>;
1397
1398 // Internal types made public for use by btree_container types.
1399 using params_type = Params;
1400 using slot_type = typename Params::slot_type;
1401
1402 private:
1403 // Copies or moves (depending on the template parameter) the values in
1404 // other into this btree in their order in other. This btree must be empty
1405 // before this method is called. This method is used in copy construction,
1406 // copy assignment, and move assignment.
1407 template <typename Btree>
1408 void copy_or_move_values_in_order(Btree &other);
1409
1410 // Validates that various assumptions/requirements are true at compile time.
1411 constexpr static bool static_assert_validation();
1412
1413 public:
1414 btree(const key_compare &comp, const allocator_type &alloc)
1415 : root_(EmptyNode()), rightmost_(comp, alloc, EmptyNode()), size_(0) {}
1416
1417 btree(const btree &other) : btree(other, other.allocator()) {}
1418 btree(const btree &other, const allocator_type &alloc)
1419 : btree(other.key_comp(), alloc) {
1420 copy_or_move_values_in_order(other);
1421 }
1422 btree(btree &&other) noexcept
1423 : root_(absl::exchange(other.root_, EmptyNode())),
1424 rightmost_(std::move(other.rightmost_)),
1425 size_(absl::exchange(other.size_, 0u)) {
1426 other.mutable_rightmost() = EmptyNode();
1427 }
1428 btree(btree &&other, const allocator_type &alloc)
1429 : btree(other.key_comp(), alloc) {
1430 if (alloc == other.allocator()) {
1431 swap(other);
1432 } else {
1433 // Move values from `other` one at a time when allocators are different.
1434 copy_or_move_values_in_order(other);
1435 }
1436 }
1437
1438 ~btree() {
1439 // Put static_asserts in destructor to avoid triggering them before the type
1440 // is complete.
1441 static_assert(static_assert_validation(), "This call must be elided.");
1442 clear();
1443 }
1444
1445 // Assign the contents of other to *this.
1446 btree &operator=(const btree &other);
1447 btree &operator=(btree &&other) noexcept;
1448
1449 iterator begin() { return iterator(leftmost()); }
1450 const_iterator begin() const { return const_iterator(leftmost()); }
1451 iterator end() { return iterator(rightmost(), rightmost()->finish()); }
1452 const_iterator end() const {
1453 return const_iterator(rightmost(), rightmost()->finish());
1454 }
1455 reverse_iterator rbegin() { return reverse_iterator(end()); }
1456 const_reverse_iterator rbegin() const {
1457 return const_reverse_iterator(end());
1458 }
1459 reverse_iterator rend() { return reverse_iterator(begin()); }
1460 const_reverse_iterator rend() const {
1461 return const_reverse_iterator(begin());
1462 }
1463
1464 // Finds the first element whose key is not less than `key`.
1465 template <typename K>
1466 iterator lower_bound(const K &key) {
1467 return internal_end(internal_lower_bound(key).value);
1468 }
1469 template <typename K>
1470 const_iterator lower_bound(const K &key) const {
1471 return internal_end(internal_lower_bound(key).value);
1472 }
1473
1474 // Finds the first element whose key is not less than `key` and also returns
1475 // whether that element is equal to `key`.
1476 template <typename K>
1477 std::pair<iterator, bool> lower_bound_equal(const K &key) const;
1478
1479 // Finds the first element whose key is greater than `key`.
1480 template <typename K>
1481 iterator upper_bound(const K &key) {
1482 return internal_end(internal_upper_bound(key));
1483 }
1484 template <typename K>
1485 const_iterator upper_bound(const K &key) const {
1486 return internal_end(internal_upper_bound(key));
1487 }
1488
1489 // Finds the range of values which compare equal to key. The first member of
1490 // the returned pair is equal to lower_bound(key). The second member of the
1491 // pair is equal to upper_bound(key).
1492 template <typename K>
1493 std::pair<iterator, iterator> equal_range(const K &key);
1494 template <typename K>
1495 std::pair<const_iterator, const_iterator> equal_range(const K &key) const {
1496 return const_cast<btree *>(this)->equal_range(key);
1497 }
1498
1499 // Inserts a value into the btree only if it does not already exist. The
1500 // boolean return value indicates whether insertion succeeded or failed.
1501 // Requirement: if `key` already exists in the btree, does not consume `args`.
1502 // Requirement: `key` is never referenced after consuming `args`.
1503 template <typename K, typename... Args>
1504 std::pair<iterator, bool> insert_unique(const K &key, Args &&...args);
1505
1506 // Inserts with hint. Checks to see if the value should be placed immediately
1507 // before `position` in the tree. If so, then the insertion will take
1508 // amortized constant time. If not, the insertion will take amortized
1509 // logarithmic time as if a call to insert_unique() were made.
1510 // Requirement: if `key` already exists in the btree, does not consume `args`.
1511 // Requirement: `key` is never referenced after consuming `args`.
1512 template <typename K, typename... Args>
1513 std::pair<iterator, bool> insert_hint_unique(iterator position, const K &key,
1514 Args &&...args);
1515
1516 // Insert a range of values into the btree.
1517 // Note: the first overload avoids constructing a value_type if the key
1518 // already exists in the btree.
1519 template <typename InputIterator,
1520 typename = decltype(std::declval<const key_compare &>()(
1521 params_type::key(*std::declval<InputIterator>()),
1522 std::declval<const key_type &>()))>
1523 void insert_iterator_unique(InputIterator b, InputIterator e, int);
1524 // We need the second overload for cases in which we need to construct a
1525 // value_type in order to compare it with the keys already in the btree.
1526 template <typename InputIterator>
1527 void insert_iterator_unique(InputIterator b, InputIterator e, char);
1528
1529 // Inserts a value into the btree.
1530 template <typename ValueType>
1531 iterator insert_multi(const key_type &key, ValueType &&v);
1532
1533 // Inserts a value into the btree.
1534 template <typename ValueType>
1535 iterator insert_multi(ValueType &&v) {
1536 return insert_multi(params_type::key(v), std::forward<ValueType>(v));
1537 }
1538
1539 // Insert with hint. Check to see if the value should be placed immediately
1540 // before position in the tree. If it does, then the insertion will take
1541 // amortized constant time. If not, the insertion will take amortized
1542 // logarithmic time as if a call to insert_multi(v) were made.
1543 template <typename ValueType>
1544 iterator insert_hint_multi(iterator position, ValueType &&v);
1545
1546 // Insert a range of values into the btree.
1547 template <typename InputIterator>
1548 void insert_iterator_multi(InputIterator b,
1549 InputIterator e);
1550
1551 // Erase the specified iterator from the btree. The iterator must be valid
1552 // (i.e. not equal to end()). Return an iterator pointing to the node after
1553 // the one that was erased (or end() if none exists).
1554 // Requirement: does not read the value at `*iter`.
1555 iterator erase(iterator iter);
1556
1557 // Erases range. Returns the number of keys erased and an iterator pointing
1558 // to the element after the last erased element.
1559 std::pair<size_type, iterator> erase_range(iterator begin, iterator end);
1560
1561 // Finds an element with key equivalent to `key` or returns `end()` if `key`
1562 // is not present.
1563 template <typename K>
1564 iterator find(const K &key) {
1565 return internal_end(internal_find(key));
1566 }
1567 template <typename K>
1568 const_iterator find(const K &key) const {
1569 return internal_end(internal_find(key));
1570 }
1571
1572 // Clear the btree, deleting all of the values it contains.
1573 void clear();
1574
1575 // Swaps the contents of `this` and `other`.
1576 void swap(btree &other);
1577
1578 const key_compare &key_comp() const noexcept {
1579 return rightmost_.template get<0>();
1580 }
1581 template <typename K1, typename K2>
1582 bool compare_keys(const K1 &a, const K2 &b) const {
1583 return compare_internal::compare_result_as_less_than(key_comp()(a, b));
1584 }
1585
1586 value_compare value_comp() const {
1587 return value_compare(original_key_compare(key_comp()));
1588 }
1589
1590 // Verifies the structure of the btree.
1591 void verify() const;
1592
1593 // Size routines.
1594 size_type size() const { return size_; }
1595 size_type max_size() const { return (std::numeric_limits<size_type>::max)(); }
1596 bool empty() const { return size_ == 0; }
1597
1598 // The height of the btree. An empty tree will have height 0.
1599 size_type height() const {
1600 size_type h = 0;
1601 if (!empty()) {
1602 // Count the length of the chain from the leftmost node up to the
1603 // root. We actually count from the root back around to the level below
1604 // the root, but the calculation is the same because of the circularity
1605 // of that traversal.
1606 const node_type *n = root();
1607 do {
1608 ++h;
1609 n = n->parent();
1610 } while (n != root());
1611 }
1612 return h;
1613 }
1614
1615 // The number of internal, leaf and total nodes used by the btree.
1616 size_type leaf_nodes() const { return internal_stats(root()).leaf_nodes; }
1617 size_type internal_nodes() const {
1618 return internal_stats(root()).internal_nodes;
1619 }
1620 size_type nodes() const {
1621 node_stats stats = internal_stats(root());
1622 return stats.leaf_nodes + stats.internal_nodes;
1623 }
1624
1625 // The total number of bytes used by the btree.
1626 // TODO(b/169338300): update to support node_btree_*.
1627 size_type bytes_used() const {
1628 node_stats stats = internal_stats(root());
1629 if (stats.leaf_nodes == 1 && stats.internal_nodes == 0) {
1630 return sizeof(*this) + node_type::LeafSize(root()->max_count());
1631 } else {
1632 return sizeof(*this) + stats.leaf_nodes * node_type::LeafSize() +
1633 stats.internal_nodes * node_type::InternalSize();
1634 }
1635 }
1636
1637 // The average number of bytes used per value stored in the btree assuming
1638 // random insertion order.
1639 static double average_bytes_per_value() {
1640 // The expected number of values per node with random insertion order is the
1641 // average of the maximum and minimum numbers of values per node.
1642 const double expected_values_per_node = (kNodeSlots + kMinNodeValues) / 2.0;
1643 return node_type::LeafSize() / expected_values_per_node;
1644 }
1645
1646 // The fullness of the btree. Computed as the number of elements in the btree
1647 // divided by the maximum number of elements a tree with the current number
1648 // of nodes could hold. A value of 1 indicates perfect space
1649 // utilization. Smaller values indicate space wastage.
1650 // Returns 0 for empty trees.
1651 double fullness() const {
1652 if (empty()) return 0.0;
1653 return static_cast<double>(size()) / (nodes() * kNodeSlots);
1654 }
1655 // The overhead of the btree structure in bytes per node. Computed as the
1656 // total number of bytes used by the btree minus the number of bytes used for
1657 // storing elements divided by the number of elements.
1658 // Returns 0 for empty trees.
1659 double overhead() const {
1660 if (empty()) return 0.0;
1661 return (bytes_used() - size() * sizeof(value_type)) /
1662 static_cast<double>(size());
1663 }
1664
1665 // The allocator used by the btree.
1666 allocator_type get_allocator() const { return allocator(); }
1667
1668 private:
1669 friend struct btree_access;
1670
1671 // Internal accessor routines.
1672 node_type *root() { return root_; }
1673 const node_type *root() const { return root_; }
1674 node_type *&mutable_root() noexcept { return root_; }
1675 node_type *rightmost() { return rightmost_.template get<2>(); }
1676 const node_type *rightmost() const { return rightmost_.template get<2>(); }
1677 node_type *&mutable_rightmost() noexcept {
1678 return rightmost_.template get<2>();
1679 }
1680 key_compare *mutable_key_comp() noexcept {
1681 return &rightmost_.template get<0>();
1682 }
1683
1684 // The leftmost node is stored as the parent of the root node.
1685 node_type *leftmost() { return root()->parent(); }
1686 const node_type *leftmost() const { return root()->parent(); }
1687
1688 // Allocator routines.
1689 allocator_type *mutable_allocator() noexcept {
1690 return &rightmost_.template get<1>();
1691 }
1692 const allocator_type &allocator() const noexcept {
1693 return rightmost_.template get<1>();
1694 }
1695
1696 // Allocates a correctly aligned node of at least size bytes using the
1697 // allocator.
1698 node_type *allocate(size_type size) {
1699 return reinterpret_cast<node_type *>(
1700 absl::container_internal::Allocate<node_type::Alignment()>(
1701 mutable_allocator(), size));
1702 }
1703
1704 // Node creation/deletion routines.
1705 node_type *new_internal_node(field_type position, node_type *parent) {
1706 node_type *n = allocate(node_type::InternalSize());
1707 n->init_internal(position, parent);
1708 return n;
1709 }
1710 node_type *new_leaf_node(field_type position, node_type *parent) {
1711 node_type *n = allocate(node_type::LeafSize());
1712 n->init_leaf(position, kNodeSlots, parent);
1713 return n;
1714 }
1715 node_type *new_leaf_root_node(field_type max_count) {
1716 node_type *n = allocate(node_type::LeafSize(max_count));
1717 n->init_leaf(/*position=*/0, max_count, /*parent=*/n);
1718 return n;
1719 }
1720
1721 // Deletion helper routines.
1722 iterator rebalance_after_delete(iterator iter);
1723
1724 // Rebalances or splits the node iter points to.
1725 void rebalance_or_split(iterator *iter);
1726
1727 // Merges the values of left, right and the delimiting key on their parent
1728 // onto left, removing the delimiting key and deleting right.
1729 void merge_nodes(node_type *left, node_type *right);
1730
1731 // Tries to merge node with its left or right sibling, and failing that,
1732 // rebalance with its left or right sibling. Returns true if a merge
1733 // occurred, at which point it is no longer valid to access node. Returns
1734 // false if no merging took place.
1735 bool try_merge_or_rebalance(iterator *iter);
1736
1737 // Tries to shrink the height of the tree by 1.
1738 void try_shrink();
1739
1740 iterator internal_end(iterator iter) {
1741 return iter.node_ != nullptr ? iter : end();
1742 }
1743 const_iterator internal_end(const_iterator iter) const {
1744 return iter.node_ != nullptr ? iter : end();
1745 }
1746
1747 // Emplaces a value into the btree immediately before iter. Requires that
1748 // key(v) <= iter.key() and (--iter).key() <= key(v).
1749 template <typename... Args>
1750 iterator internal_emplace(iterator iter, Args &&...args);
1751
1752 // Returns an iterator pointing to the first value >= the value "iter" is
1753 // pointing at. Note that "iter" might be pointing to an invalid location such
1754 // as iter.position_ == iter.node_->finish(). This routine simply moves iter
1755 // up in the tree to a valid location. Requires: iter.node_ is non-null.
1756 template <typename IterType>
1757 static IterType internal_last(IterType iter);
1758
1759 // Returns an iterator pointing to the leaf position at which key would
1760 // reside in the tree, unless there is an exact match - in which case, the
1761 // result may not be on a leaf. When there's a three-way comparator, we can
1762 // return whether there was an exact match. This allows the caller to avoid a
1763 // subsequent comparison to determine if an exact match was made, which is
1764 // important for keys with expensive comparison, such as strings.
1765 template <typename K>
1766 SearchResult<iterator, is_key_compare_to::value> internal_locate(
1767 const K &key) const;
1768
1769 // Internal routine which implements lower_bound().
1770 template <typename K>
1771 SearchResult<iterator, is_key_compare_to::value> internal_lower_bound(
1772 const K &key) const;
1773
1774 // Internal routine which implements upper_bound().
1775 template <typename K>
1776 iterator internal_upper_bound(const K &key) const;
1777
1778 // Internal routine which implements find().
1779 template <typename K>
1780 iterator internal_find(const K &key) const;
1781
1782 // Verifies the tree structure of node.
1783 size_type internal_verify(const node_type *node, const key_type *lo,
1784 const key_type *hi) const;
1785
1786 node_stats internal_stats(const node_type *node) const {
1787 // The root can be a static empty node.
1788 if (node == nullptr || (node == root() && empty())) {
1789 return node_stats(0, 0);
1790 }
1791 if (node->is_leaf()) {
1792 return node_stats(1, 0);
1793 }
1794 node_stats res(0, 1);
1795 for (int i = node->start(); i <= node->finish(); ++i) {
1796 res += internal_stats(node->child(i));
1797 }
1798 return res;
1799 }
1800
1801 node_type *root_;
1802
1803 // A pointer to the rightmost node. Note that the leftmost node is stored as
1804 // the root's parent. We use compressed tuple in order to save space because
1805 // key_compare and allocator_type are usually empty.
1806 absl::container_internal::CompressedTuple<key_compare, allocator_type,
1807 node_type *>
1808 rightmost_;
1809
1810 // Number of values.
1811 size_type size_;
1812 };
1813
1814 ////
1815 // btree_node methods
1816 template <typename P>
1817 template <typename... Args>
1818 inline void btree_node<P>::emplace_value(const field_type i,
1819 allocator_type *alloc,
1820 Args &&...args) {
1821 assert(i >= start());
1822 assert(i <= finish());
1823 // Shift old values to create space for new value and then construct it in
1824 // place.
1825 if (i < finish()) {
1826 transfer_n_backward(finish() - i, /*dest_i=*/i + 1, /*src_i=*/i, this,
1827 alloc);
1828 }
1829 value_init(static_cast<field_type>(i), alloc, std::forward<Args>(args)...);
1830 set_finish(finish() + 1);
1831
1832 if (is_internal() && finish() > i + 1) {
1833 for (field_type j = finish(); j > i + 1; --j) {
1834 set_child(j, child(j - 1));
1835 }
1836 clear_child(i + 1);
1837 }
1838 }
1839
1840 template <typename P>
1841 inline void btree_node<P>::remove_values(const field_type i,
1842 const field_type to_erase,
1843 allocator_type *alloc) {
1844 // Transfer values after the removed range into their new places.
1845 value_destroy_n(i, to_erase, alloc);
1846 const field_type orig_finish = finish();
1847 const field_type src_i = i + to_erase;
1848 transfer_n(orig_finish - src_i, i, src_i, this, alloc);
1849
1850 if (is_internal()) {
1851 // Delete all children between begin and end.
1852 for (field_type j = 0; j < to_erase; ++j) {
1853 clear_and_delete(child(i + j + 1), alloc);
1854 }
1855 // Rotate children after end into new positions.
1856 for (field_type j = i + to_erase + 1; j <= orig_finish; ++j) {
1857 set_child(j - to_erase, child(j));
1858 clear_child(j);
1859 }
1860 }
1861 set_finish(orig_finish - to_erase);
1862 }
1863
1864 template <typename P>
1865 void btree_node<P>::rebalance_right_to_left(field_type to_move,
1866 btree_node *right,
1867 allocator_type *alloc) {
1868 assert(parent() == right->parent());
1869 assert(position() + 1 == right->position());
1870 assert(right->count() >= count());
1871 assert(to_move >= 1);
1872 assert(to_move <= right->count());
1873
1874 // 1) Move the delimiting value in the parent to the left node.
1875 transfer(finish(), position(), parent(), alloc);
1876
1877 // 2) Move the (to_move - 1) values from the right node to the left node.
1878 transfer_n(to_move - 1, finish() + 1, right->start(), right, alloc);
1879
1880 // 3) Move the new delimiting value to the parent from the right node.
1881 parent()->transfer(position(), right->start() + to_move - 1, right, alloc);
1882
1883 // 4) Shift the values in the right node to their correct positions.
1884 right->transfer_n(right->count() - to_move, right->start(),
1885 right->start() + to_move, right, alloc);
1886
1887 if (is_internal()) {
1888 // Move the child pointers from the right to the left node.
1889 for (field_type i = 0; i < to_move; ++i) {
1890 init_child(finish() + i + 1, right->child(i));
1891 }
1892 for (field_type i = right->start(); i <= right->finish() - to_move; ++i) {
1893 assert(i + to_move <= right->max_count());
1894 right->init_child(i, right->child(i + to_move));
1895 right->clear_child(i + to_move);
1896 }
1897 }
1898
1899 // Fixup `finish` on the left and right nodes.
1900 set_finish(finish() + to_move);
1901 right->set_finish(right->finish() - to_move);
1902 }
1903
1904 template <typename P>
1905 void btree_node<P>::rebalance_left_to_right(field_type to_move,
1906 btree_node *right,
1907 allocator_type *alloc) {
1908 assert(parent() == right->parent());
1909 assert(position() + 1 == right->position());
1910 assert(count() >= right->count());
1911 assert(to_move >= 1);
1912 assert(to_move <= count());
1913
1914 // Values in the right node are shifted to the right to make room for the
1915 // new to_move values. Then, the delimiting value in the parent and the
1916 // other (to_move - 1) values in the left node are moved into the right node.
1917 // Lastly, a new delimiting value is moved from the left node into the
1918 // parent, and the remaining empty left node entries are destroyed.
1919
1920 // 1) Shift existing values in the right node to their correct positions.
1921 right->transfer_n_backward(right->count(), right->start() + to_move,
1922 right->start(), right, alloc);
1923
1924 // 2) Move the delimiting value in the parent to the right node.
1925 right->transfer(right->start() + to_move - 1, position(), parent(), alloc);
1926
1927 // 3) Move the (to_move - 1) values from the left node to the right node.
1928 right->transfer_n(to_move - 1, right->start(), finish() - (to_move - 1), this,
1929 alloc);
1930
1931 // 4) Move the new delimiting value to the parent from the left node.
1932 parent()->transfer(position(), finish() - to_move, this, alloc);
1933
1934 if (is_internal()) {
1935 // Move the child pointers from the left to the right node.
1936 for (field_type i = right->finish() + 1; i > right->start(); --i) {
1937 right->init_child(i - 1 + to_move, right->child(i - 1));
1938 right->clear_child(i - 1);
1939 }
1940 for (field_type i = 1; i <= to_move; ++i) {
1941 right->init_child(i - 1, child(finish() - to_move + i));
1942 clear_child(finish() - to_move + i);
1943 }
1944 }
1945
1946 // Fixup the counts on the left and right nodes.
1947 set_finish(finish() - to_move);
1948 right->set_finish(right->finish() + to_move);
1949 }
1950
1951 template <typename P>
1952 void btree_node<P>::split(const int insert_position, btree_node *dest,
1953 allocator_type *alloc) {
1954 assert(dest->count() == 0);
1955 assert(max_count() == kNodeSlots);
1956 assert(position() + 1 == dest->position());
1957 assert(parent() == dest->parent());
1958
1959 // We bias the split based on the position being inserted. If we're
1960 // inserting at the beginning of the left node then bias the split to put
1961 // more values on the right node. If we're inserting at the end of the
1962 // right node then bias the split to put more values on the left node.
1963 if (insert_position == start()) {
1964 dest->set_finish(dest->start() + finish() - 1);
1965 } else if (insert_position == kNodeSlots) {
1966 dest->set_finish(dest->start());
1967 } else {
1968 dest->set_finish(dest->start() + count() / 2);
1969 }
1970 set_finish(finish() - dest->count());
1971 assert(count() >= 1);
1972
1973 // Move values from the left sibling to the right sibling.
1974 dest->transfer_n(dest->count(), dest->start(), finish(), this, alloc);
1975
1976 // The split key is the largest value in the left sibling.
1977 --mutable_finish();
1978 parent()->emplace_value(position(), alloc, finish_slot());
1979 value_destroy(finish(), alloc);
1980 parent()->set_child_noupdate_position(position() + 1, dest);
1981
1982 if (is_internal()) {
1983 for (field_type i = dest->start(), j = finish() + 1; i <= dest->finish();
1984 ++i, ++j) {
1985 assert(child(j) != nullptr);
1986 dest->init_child(i, child(j));
1987 clear_child(j);
1988 }
1989 }
1990 }
1991
1992 template <typename P>
1993 void btree_node<P>::merge(btree_node *src, allocator_type *alloc) {
1994 assert(parent() == src->parent());
1995 assert(position() + 1 == src->position());
1996
1997 // Move the delimiting value to the left node.
1998 value_init(finish(), alloc, parent()->slot(position()));
1999
2000 // Move the values from the right to the left node.
2001 transfer_n(src->count(), finish() + 1, src->start(), src, alloc);
2002
2003 if (is_internal()) {
2004 // Move the child pointers from the right to the left node.
2005 for (field_type i = src->start(), j = finish() + 1; i <= src->finish();
2006 ++i, ++j) {
2007 init_child(j, src->child(i));
2008 src->clear_child(i);
2009 }
2010 }
2011
2012 // Fixup `finish` on the src and dest nodes.
2013 set_finish(start() + 1 + count() + src->count());
2014 src->set_finish(src->start());
2015
2016 // Remove the value on the parent node and delete the src node.
2017 parent()->remove_values(position(), /*to_erase=*/1, alloc);
2018 }
2019
2020 template <typename P>
2021 void btree_node<P>::clear_and_delete(btree_node *node, allocator_type *alloc) {
2022 if (node->is_leaf()) {
2023 node->value_destroy_n(node->start(), node->count(), alloc);
2024 deallocate(LeafSize(node->max_count()), node, alloc);
2025 return;
2026 }
2027 if (node->count() == 0) {
2028 deallocate(InternalSize(), node, alloc);
2029 return;
2030 }
2031
2032 // The parent of the root of the subtree we are deleting.
2033 btree_node *delete_root_parent = node->parent();
2034
2035 // Navigate to the leftmost leaf under node, and then delete upwards.
2036 while (node->is_internal()) node = node->start_child();
2037 #ifdef ABSL_BTREE_ENABLE_GENERATIONS
2038 // When generations are enabled, we delete the leftmost leaf last in case it's
2039 // the parent of the root and we need to check whether it's a leaf before we
2040 // can update the root's generation.
2041 // TODO(ezb): if we change btree_node::is_root to check a bool inside the node
2042 // instead of checking whether the parent is a leaf, we can remove this logic.
2043 btree_node *leftmost_leaf = node;
2044 #endif
2045 // Use `size_type` because `pos` needs to be able to hold `kNodeSlots+1`,
2046 // which isn't guaranteed to be a valid `field_type`.
2047 size_type pos = node->position();
2048 btree_node *parent = node->parent();
2049 for (;;) {
2050 // In each iteration of the next loop, we delete one leaf node and go right.
2051 assert(pos <= parent->finish());
2052 do {
2053 node = parent->child(static_cast<field_type>(pos));
2054 if (node->is_internal()) {
2055 // Navigate to the leftmost leaf under node.
2056 while (node->is_internal()) node = node->start_child();
2057 pos = node->position();
2058 parent = node->parent();
2059 }
2060 node->value_destroy_n(node->start(), node->count(), alloc);
2061 #ifdef ABSL_BTREE_ENABLE_GENERATIONS
2062 if (leftmost_leaf != node)
2063 #endif
2064 deallocate(LeafSize(node->max_count()), node, alloc);
2065 ++pos;
2066 } while (pos <= parent->finish());
2067
2068 // Once we've deleted all children of parent, delete parent and go up/right.
2069 assert(pos > parent->finish());
2070 do {
2071 node = parent;
2072 pos = node->position();
2073 parent = node->parent();
2074 node->value_destroy_n(node->start(), node->count(), alloc);
2075 deallocate(InternalSize(), node, alloc);
2076 if (parent == delete_root_parent) {
2077 #ifdef ABSL_BTREE_ENABLE_GENERATIONS
2078 deallocate(LeafSize(leftmost_leaf->max_count()), leftmost_leaf, alloc);
2079 #endif
2080 return;
2081 }
2082 ++pos;
2083 } while (pos > parent->finish());
2084 }
2085 }
2086
2087 ////
2088 // btree_iterator methods
2089
2090 // Note: the implementation here is based on btree_node::clear_and_delete.
2091 template <typename N, typename R, typename P>
2092 auto btree_iterator<N, R, P>::distance_slow(const_iterator other) const
2093 -> difference_type {
2094 const_iterator begin = other;
2095 const_iterator end = *this;
2096 assert(begin.node_ != end.node_ || !begin.node_->is_leaf() ||
2097 begin.position_ != end.position_);
2098
2099 const node_type *node = begin.node_;
2100 // We need to compensate for double counting if begin.node_ is a leaf node.
2101 difference_type count = node->is_leaf() ? -begin.position_ : 0;
2102
2103 // First navigate to the leftmost leaf node past begin.
2104 if (node->is_internal()) {
2105 ++count;
2106 node = node->child(begin.position_ + 1);
2107 }
2108 while (node->is_internal()) node = node->start_child();
2109
2110 // Use `size_type` because `pos` needs to be able to hold `kNodeSlots+1`,
2111 // which isn't guaranteed to be a valid `field_type`.
2112 size_type pos = node->position();
2113 const node_type *parent = node->parent();
2114 for (;;) {
2115 // In each iteration of the next loop, we count one leaf node and go right.
2116 assert(pos <= parent->finish());
2117 do {
2118 node = parent->child(static_cast<field_type>(pos));
2119 if (node->is_internal()) {
2120 // Navigate to the leftmost leaf under node.
2121 while (node->is_internal()) node = node->start_child();
2122 pos = node->position();
2123 parent = node->parent();
2124 }
2125 if (node == end.node_) return count + end.position_;
2126 if (parent == end.node_ && pos == static_cast<size_type>(end.position_))
2127 return count + node->count();
2128 // +1 is for the next internal node value.
2129 count += node->count() + 1;
2130 ++pos;
2131 } while (pos <= parent->finish());
2132
2133 // Once we've counted all children of parent, go up/right.
2134 assert(pos > parent->finish());
2135 do {
2136 node = parent;
2137 pos = node->position();
2138 parent = node->parent();
2139 // -1 because we counted the value at end and shouldn't.
2140 if (parent == end.node_ && pos == static_cast<size_type>(end.position_))
2141 return count - 1;
2142 ++pos;
2143 } while (pos > parent->finish());
2144 }
2145 }
2146
2147 template <typename N, typename R, typename P>
2148 void btree_iterator<N, R, P>::increment_slow() {
2149 if (node_->is_leaf()) {
2150 assert(position_ >= node_->finish());
2151 btree_iterator save(*this);
2152 while (position_ == node_->finish() && !node_->is_root()) {
2153 assert(node_->parent()->child(node_->position()) == node_);
2154 position_ = node_->position();
2155 node_ = node_->parent();
2156 }
2157 // TODO(ezb): assert we aren't incrementing end() instead of handling.
2158 if (position_ == node_->finish()) {
2159 *this = save;
2160 }
2161 } else {
2162 assert(position_ < node_->finish());
2163 node_ = node_->child(static_cast<field_type>(position_ + 1));
2164 while (node_->is_internal()) {
2165 node_ = node_->start_child();
2166 }
2167 position_ = node_->start();
2168 }
2169 }
2170
2171 template <typename N, typename R, typename P>
2172 void btree_iterator<N, R, P>::decrement_slow() {
2173 if (node_->is_leaf()) {
2174 assert(position_ <= -1);
2175 btree_iterator save(*this);
2176 while (position_ < node_->start() && !node_->is_root()) {
2177 assert(node_->parent()->child(node_->position()) == node_);
2178 position_ = node_->position() - 1;
2179 node_ = node_->parent();
2180 }
2181 // TODO(ezb): assert we aren't decrementing begin() instead of handling.
2182 if (position_ < node_->start()) {
2183 *this = save;
2184 }
2185 } else {
2186 assert(position_ >= node_->start());
2187 node_ = node_->child(static_cast<field_type>(position_));
2188 while (node_->is_internal()) {
2189 node_ = node_->child(node_->finish());
2190 }
2191 position_ = node_->finish() - 1;
2192 }
2193 }
2194
2195 ////
2196 // btree methods
2197 template <typename P>
2198 template <typename Btree>
2199 void btree<P>::copy_or_move_values_in_order(Btree &other) {
2200 static_assert(std::is_same<btree, Btree>::value ||
2201 std::is_same<const btree, Btree>::value,
2202 "Btree type must be same or const.");
2203 assert(empty());
2204
2205 // We can avoid key comparisons because we know the order of the
2206 // values is the same order we'll store them in.
2207 auto iter = other.begin();
2208 if (iter == other.end()) return;
2209 insert_multi(iter.slot());
2210 ++iter;
2211 for (; iter != other.end(); ++iter) {
2212 // If the btree is not empty, we can just insert the new value at the end
2213 // of the tree.
2214 internal_emplace(end(), iter.slot());
2215 }
2216 }
2217
2218 template <typename P>
2219 constexpr bool btree<P>::static_assert_validation() {
2220 static_assert(std::is_nothrow_copy_constructible<key_compare>::value,
2221 "Key comparison must be nothrow copy constructible");
2222 static_assert(std::is_nothrow_copy_constructible<allocator_type>::value,
2223 "Allocator must be nothrow copy constructible");
2224 static_assert(std::is_trivially_copyable<iterator>::value,
2225 "iterator not trivially copyable.");
2226
2227 // Note: We assert that kTargetValues, which is computed from
2228 // Params::kTargetNodeSize, must fit the node_type::field_type.
2229 static_assert(
2230 kNodeSlots < (1 << (8 * sizeof(typename node_type::field_type))),
2231 "target node size too large");
2232
2233 // Verify that key_compare returns an absl::{weak,strong}_ordering or bool.
2234 static_assert(
2235 compare_has_valid_result_type<key_compare, key_type>(),
2236 "key comparison function must return absl::{weak,strong}_ordering or "
2237 "bool.");
2238
2239 // Test the assumption made in setting kNodeSlotSpace.
2240 static_assert(node_type::MinimumOverhead() >= sizeof(void *) + 4,
2241 "node space assumption incorrect");
2242
2243 return true;
2244 }
2245
2246 template <typename P>
2247 template <typename K>
2248 auto btree<P>::lower_bound_equal(const K &key) const
2249 -> std::pair<iterator, bool> {
2250 const SearchResult<iterator, is_key_compare_to::value> res =
2251 internal_lower_bound(key);
2252 const iterator lower = iterator(internal_end(res.value));
2253 const bool equal = res.HasMatch()
2254 ? res.IsEq()
2255 : lower != end() && !compare_keys(key, lower.key());
2256 return {lower, equal};
2257 }
2258
2259 template <typename P>
2260 template <typename K>
2261 auto btree<P>::equal_range(const K &key) -> std::pair<iterator, iterator> {
2262 const std::pair<iterator, bool> lower_and_equal = lower_bound_equal(key);
2263 const iterator lower = lower_and_equal.first;
2264 if (!lower_and_equal.second) {
2265 return {lower, lower};
2266 }
2267
2268 const iterator next = std::next(lower);
2269 if (!params_type::template can_have_multiple_equivalent_keys<K>()) {
2270 // The next iterator after lower must point to a key greater than `key`.
2271 // Note: if this assert fails, then it may indicate that the comparator does
2272 // not meet the equivalence requirements for Compare
2273 // (see https://en.cppreference.com/w/cpp/named_req/Compare).
2274 assert(next == end() || compare_keys(key, next.key()));
2275 return {lower, next};
2276 }
2277 // Try once more to avoid the call to upper_bound() if there's only one
2278 // equivalent key. This should prevent all calls to upper_bound() in cases of
2279 // unique-containers with heterogeneous comparators in which all comparison
2280 // operators have the same equivalence classes.
2281 if (next == end() || compare_keys(key, next.key())) return {lower, next};
2282
2283 // In this case, we need to call upper_bound() to avoid worst case O(N)
2284 // behavior if we were to iterate over equal keys.
2285 return {lower, upper_bound(key)};
2286 }
2287
2288 template <typename P>
2289 template <typename K, typename... Args>
2290 auto btree<P>::insert_unique(const K &key, Args &&...args)
2291 -> std::pair<iterator, bool> {
2292 if (empty()) {
2293 mutable_root() = mutable_rightmost() = new_leaf_root_node(1);
2294 }
2295
2296 SearchResult<iterator, is_key_compare_to::value> res = internal_locate(key);
2297 iterator iter = res.value;
2298
2299 if (res.HasMatch()) {
2300 if (res.IsEq()) {
2301 // The key already exists in the tree, do nothing.
2302 return {iter, false};
2303 }
2304 } else {
2305 iterator last = internal_last(iter);
2306 if (last.node_ && !compare_keys(key, last.key())) {
2307 // The key already exists in the tree, do nothing.
2308 return {last, false};
2309 }
2310 }
2311 return {internal_emplace(iter, std::forward<Args>(args)...), true};
2312 }
2313
2314 template <typename P>
2315 template <typename K, typename... Args>
2316 inline auto btree<P>::insert_hint_unique(iterator position, const K &key,
2317 Args &&...args)
2318 -> std::pair<iterator, bool> {
2319 if (!empty()) {
2320 if (position == end() || compare_keys(key, position.key())) {
2321 if (position == begin() || compare_keys(std::prev(position).key(), key)) {
2322 // prev.key() < key < position.key()
2323 return {internal_emplace(position, std::forward<Args>(args)...), true};
2324 }
2325 } else if (compare_keys(position.key(), key)) {
2326 ++position;
2327 if (position == end() || compare_keys(key, position.key())) {
2328 // {original `position`}.key() < key < {current `position`}.key()
2329 return {internal_emplace(position, std::forward<Args>(args)...), true};
2330 }
2331 } else {
2332 // position.key() == key
2333 return {position, false};
2334 }
2335 }
2336 return insert_unique(key, std::forward<Args>(args)...);
2337 }
2338
2339 template <typename P>
2340 template <typename InputIterator, typename>
2341 void btree<P>::insert_iterator_unique(InputIterator b, InputIterator e, int) {
2342 for (; b != e; ++b) {
2343 insert_hint_unique(end(), params_type::key(*b), *b);
2344 }
2345 }
2346
2347 template <typename P>
2348 template <typename InputIterator>
2349 void btree<P>::insert_iterator_unique(InputIterator b, InputIterator e, char) {
2350 for (; b != e; ++b) {
2351 // Use a node handle to manage a temp slot.
2352 auto node_handle =
2353 CommonAccess::Construct<node_handle_type>(get_allocator(), *b);
2354 slot_type *slot = CommonAccess::GetSlot(node_handle);
2355 insert_hint_unique(end(), params_type::key(slot), slot);
2356 }
2357 }
2358
2359 template <typename P>
2360 template <typename ValueType>
2361 auto btree<P>::insert_multi(const key_type &key, ValueType &&v) -> iterator {
2362 if (empty()) {
2363 mutable_root() = mutable_rightmost() = new_leaf_root_node(1);
2364 }
2365
2366 iterator iter = internal_upper_bound(key);
2367 if (iter.node_ == nullptr) {
2368 iter = end();
2369 }
2370 return internal_emplace(iter, std::forward<ValueType>(v));
2371 }
2372
2373 template <typename P>
2374 template <typename ValueType>
2375 auto btree<P>::insert_hint_multi(iterator position, ValueType &&v) -> iterator {
2376 if (!empty()) {
2377 const key_type &key = params_type::key(v);
2378 if (position == end() || !compare_keys(position.key(), key)) {
2379 if (position == begin() ||
2380 !compare_keys(key, std::prev(position).key())) {
2381 // prev.key() <= key <= position.key()
2382 return internal_emplace(position, std::forward<ValueType>(v));
2383 }
2384 } else {
2385 ++position;
2386 if (position == end() || !compare_keys(position.key(), key)) {
2387 // {original `position`}.key() < key < {current `position`}.key()
2388 return internal_emplace(position, std::forward<ValueType>(v));
2389 }
2390 }
2391 }
2392 return insert_multi(std::forward<ValueType>(v));
2393 }
2394
2395 template <typename P>
2396 template <typename InputIterator>
2397 void btree<P>::insert_iterator_multi(InputIterator b, InputIterator e) {
2398 for (; b != e; ++b) {
2399 insert_hint_multi(end(), *b);
2400 }
2401 }
2402
2403 template <typename P>
2404 auto btree<P>::operator=(const btree &other) -> btree & {
2405 if (this != &other) {
2406 clear();
2407
2408 *mutable_key_comp() = other.key_comp();
2409 if (absl::allocator_traits<
2410 allocator_type>::propagate_on_container_copy_assignment::value) {
2411 *mutable_allocator() = other.allocator();
2412 }
2413
2414 copy_or_move_values_in_order(other);
2415 }
2416 return *this;
2417 }
2418
2419 template <typename P>
2420 auto btree<P>::operator=(btree &&other) noexcept -> btree & {
2421 if (this != &other) {
2422 clear();
2423
2424 using std::swap;
2425 if (absl::allocator_traits<
2426 allocator_type>::propagate_on_container_move_assignment::value) {
2427 swap(root_, other.root_);
2428 // Note: `rightmost_` also contains the allocator and the key comparator.
2429 swap(rightmost_, other.rightmost_);
2430 swap(size_, other.size_);
2431 } else {
2432 if (allocator() == other.allocator()) {
2433 swap(mutable_root(), other.mutable_root());
2434 swap(*mutable_key_comp(), *other.mutable_key_comp());
2435 swap(mutable_rightmost(), other.mutable_rightmost());
2436 swap(size_, other.size_);
2437 } else {
2438 // We aren't allowed to propagate the allocator and the allocator is
2439 // different so we can't take over its memory. We must move each element
2440 // individually. We need both `other` and `this` to have `other`s key
2441 // comparator while moving the values so we can't swap the key
2442 // comparators.
2443 *mutable_key_comp() = other.key_comp();
2444 copy_or_move_values_in_order(other);
2445 }
2446 }
2447 }
2448 return *this;
2449 }
2450
2451 template <typename P>
2452 auto btree<P>::erase(iterator iter) -> iterator {
2453 iter.node_->value_destroy(static_cast<field_type>(iter.position_),
2454 mutable_allocator());
2455 iter.update_generation();
2456
2457 const bool internal_delete = iter.node_->is_internal();
2458 if (internal_delete) {
2459 // Deletion of a value on an internal node. First, transfer the largest
2460 // value from our left child here, then erase/rebalance from that position.
2461 // We can get to the largest value from our left child by decrementing iter.
2462 iterator internal_iter(iter);
2463 --iter;
2464 assert(iter.node_->is_leaf());
2465 internal_iter.node_->transfer(
2466 static_cast<size_type>(internal_iter.position_),
2467 static_cast<size_type>(iter.position_), iter.node_,
2468 mutable_allocator());
2469 } else {
2470 // Shift values after erased position in leaf. In the internal case, we
2471 // don't need to do this because the leaf position is the end of the node.
2472 const field_type transfer_from =
2473 static_cast<field_type>(iter.position_ + 1);
2474 const field_type num_to_transfer = iter.node_->finish() - transfer_from;
2475 iter.node_->transfer_n(num_to_transfer,
2476 static_cast<size_type>(iter.position_),
2477 transfer_from, iter.node_, mutable_allocator());
2478 }
2479 // Update node finish and container size.
2480 iter.node_->set_finish(iter.node_->finish() - 1);
2481 --size_;
2482
2483 // We want to return the next value after the one we just erased. If we
2484 // erased from an internal node (internal_delete == true), then the next
2485 // value is ++(++iter). If we erased from a leaf node (internal_delete ==
2486 // false) then the next value is ++iter. Note that ++iter may point to an
2487 // internal node and the value in the internal node may move to a leaf node
2488 // (iter.node_) when rebalancing is performed at the leaf level.
2489
2490 iterator res = rebalance_after_delete(iter);
2491
2492 // If we erased from an internal node, advance the iterator.
2493 if (internal_delete) {
2494 ++res;
2495 }
2496 return res;
2497 }
2498
2499 template <typename P>
2500 auto btree<P>::rebalance_after_delete(iterator iter) -> iterator {
2501 // Merge/rebalance as we walk back up the tree.
2502 iterator res(iter);
2503 bool first_iteration = true;
2504 for (;;) {
2505 if (iter.node_ == root()) {
2506 try_shrink();
2507 if (empty()) {
2508 return end();
2509 }
2510 break;
2511 }
2512 if (iter.node_->count() >= kMinNodeValues) {
2513 break;
2514 }
2515 bool merged = try_merge_or_rebalance(&iter);
2516 // On the first iteration, we should update `res` with `iter` because `res`
2517 // may have been invalidated.
2518 if (first_iteration) {
2519 res = iter;
2520 first_iteration = false;
2521 }
2522 if (!merged) {
2523 break;
2524 }
2525 iter.position_ = iter.node_->position();
2526 iter.node_ = iter.node_->parent();
2527 }
2528 res.update_generation();
2529
2530 // Adjust our return value. If we're pointing at the end of a node, advance
2531 // the iterator.
2532 if (res.position_ == res.node_->finish()) {
2533 res.position_ = res.node_->finish() - 1;
2534 ++res;
2535 }
2536
2537 return res;
2538 }
2539
2540 // Note: we tried implementing this more efficiently by erasing all of the
2541 // elements in [begin, end) at once and then doing rebalancing once at the end
2542 // (rather than interleaving deletion and rebalancing), but that adds a lot of
2543 // complexity, which seems to outweigh the performance win.
2544 template <typename P>
2545 auto btree<P>::erase_range(iterator begin, iterator end)
2546 -> std::pair<size_type, iterator> {
2547 size_type count = static_cast<size_type>(end - begin);
2548 assert(count >= 0);
2549
2550 if (count == 0) {
2551 return {0, begin};
2552 }
2553
2554 if (static_cast<size_type>(count) == size_) {
2555 clear();
2556 return {count, this->end()};
2557 }
2558
2559 if (begin.node_ == end.node_) {
2560 assert(end.position_ > begin.position_);
2561 begin.node_->remove_values(
2562 static_cast<field_type>(begin.position_),
2563 static_cast<field_type>(end.position_ - begin.position_),
2564 mutable_allocator());
2565 size_ -= count;
2566 return {count, rebalance_after_delete(begin)};
2567 }
2568
2569 const size_type target_size = size_ - count;
2570 while (size_ > target_size) {
2571 if (begin.node_->is_leaf()) {
2572 const size_type remaining_to_erase = size_ - target_size;
2573 const size_type remaining_in_node =
2574 static_cast<size_type>(begin.node_->finish() - begin.position_);
2575 const field_type to_erase = static_cast<field_type>(
2576 (std::min)(remaining_to_erase, remaining_in_node));
2577 begin.node_->remove_values(static_cast<field_type>(begin.position_),
2578 to_erase, mutable_allocator());
2579 size_ -= to_erase;
2580 begin = rebalance_after_delete(begin);
2581 } else {
2582 begin = erase(begin);
2583 }
2584 }
2585 begin.update_generation();
2586 return {count, begin};
2587 }
2588
2589 template <typename P>
2590 void btree<P>::clear() {
2591 if (!empty()) {
2592 node_type::clear_and_delete(root(), mutable_allocator());
2593 }
2594 mutable_root() = mutable_rightmost() = EmptyNode();
2595 size_ = 0;
2596 }
2597
2598 template <typename P>
2599 void btree<P>::swap(btree &other) {
2600 using std::swap;
2601 if (absl::allocator_traits<
2602 allocator_type>::propagate_on_container_swap::value) {
2603 // Note: `rightmost_` also contains the allocator and the key comparator.
2604 swap(rightmost_, other.rightmost_);
2605 } else {
2606 // It's undefined behavior if the allocators are unequal here.
2607 assert(allocator() == other.allocator());
2608 swap(mutable_rightmost(), other.mutable_rightmost());
2609 swap(*mutable_key_comp(), *other.mutable_key_comp());
2610 }
2611 swap(mutable_root(), other.mutable_root());
2612 swap(size_, other.size_);
2613 }
2614
2615 template <typename P>
2616 void btree<P>::verify() const {
2617 assert(root() != nullptr);
2618 assert(leftmost() != nullptr);
2619 assert(rightmost() != nullptr);
2620 assert(empty() || size() == internal_verify(root(), nullptr, nullptr));
2621 assert(leftmost() == (++const_iterator(root(), -1)).node_);
2622 assert(rightmost() == (--const_iterator(root(), root()->finish())).node_);
2623 assert(leftmost()->is_leaf());
2624 assert(rightmost()->is_leaf());
2625 }
2626
2627 template <typename P>
2628 void btree<P>::rebalance_or_split(iterator *iter) {
2629 node_type *&node = iter->node_;
2630 int &insert_position = iter->position_;
2631 assert(node->count() == node->max_count());
2632 assert(kNodeSlots == node->max_count());
2633
2634 // First try to make room on the node by rebalancing.
2635 node_type *parent = node->parent();
2636 if (node != root()) {
2637 if (node->position() > parent->start()) {
2638 // Try rebalancing with our left sibling.
2639 node_type *left = parent->child(node->position() - 1);
2640 assert(left->max_count() == kNodeSlots);
2641 if (left->count() < kNodeSlots) {
2642 // We bias rebalancing based on the position being inserted. If we're
2643 // inserting at the end of the right node then we bias rebalancing to
2644 // fill up the left node.
2645 field_type to_move =
2646 (kNodeSlots - left->count()) /
2647 (1 + (static_cast<field_type>(insert_position) < kNodeSlots));
2648 to_move = (std::max)(field_type{1}, to_move);
2649
2650 if (static_cast<field_type>(insert_position) - to_move >=
2651 node->start() ||
2652 left->count() + to_move < kNodeSlots) {
2653 left->rebalance_right_to_left(to_move, node, mutable_allocator());
2654
2655 assert(node->max_count() - node->count() == to_move);
2656 insert_position = static_cast<int>(
2657 static_cast<field_type>(insert_position) - to_move);
2658 if (insert_position < node->start()) {
2659 insert_position = insert_position + left->count() + 1;
2660 node = left;
2661 }
2662
2663 assert(node->count() < node->max_count());
2664 return;
2665 }
2666 }
2667 }
2668
2669 if (node->position() < parent->finish()) {
2670 // Try rebalancing with our right sibling.
2671 node_type *right = parent->child(node->position() + 1);
2672 assert(right->max_count() == kNodeSlots);
2673 if (right->count() < kNodeSlots) {
2674 // We bias rebalancing based on the position being inserted. If we're
2675 // inserting at the beginning of the left node then we bias rebalancing
2676 // to fill up the right node.
2677 field_type to_move = (kNodeSlots - right->count()) /
2678 (1 + (insert_position > node->start()));
2679 to_move = (std::max)(field_type{1}, to_move);
2680
2681 if (static_cast<field_type>(insert_position) <=
2682 node->finish() - to_move ||
2683 right->count() + to_move < kNodeSlots) {
2684 node->rebalance_left_to_right(to_move, right, mutable_allocator());
2685
2686 if (insert_position > node->finish()) {
2687 insert_position = insert_position - node->count() - 1;
2688 node = right;
2689 }
2690
2691 assert(node->count() < node->max_count());
2692 return;
2693 }
2694 }
2695 }
2696
2697 // Rebalancing failed, make sure there is room on the parent node for a new
2698 // value.
2699 assert(parent->max_count() == kNodeSlots);
2700 if (parent->count() == kNodeSlots) {
2701 iterator parent_iter(parent, node->position());
2702 rebalance_or_split(&parent_iter);
2703 parent = node->parent();
2704 }
2705 } else {
2706 // Rebalancing not possible because this is the root node.
2707 // Create a new root node and set the current root node as the child of the
2708 // new root.
2709 parent = new_internal_node(/*position=*/0, parent);
2710 parent->set_generation(root()->generation());
2711 parent->init_child(parent->start(), node);
2712 mutable_root() = parent;
2713 // If the former root was a leaf node, then it's now the rightmost node.
2714 assert(parent->start_child()->is_internal() ||
2715 parent->start_child() == rightmost());
2716 }
2717
2718 // Split the node.
2719 node_type *split_node;
2720 if (node->is_leaf()) {
2721 split_node = new_leaf_node(node->position() + 1, parent);
2722 node->split(insert_position, split_node, mutable_allocator());
2723 if (rightmost() == node) mutable_rightmost() = split_node;
2724 } else {
2725 split_node = new_internal_node(node->position() + 1, parent);
2726 node->split(insert_position, split_node, mutable_allocator());
2727 }
2728
2729 if (insert_position > node->finish()) {
2730 insert_position = insert_position - node->count() - 1;
2731 node = split_node;
2732 }
2733 }
2734
2735 template <typename P>
2736 void btree<P>::merge_nodes(node_type *left, node_type *right) {
2737 left->merge(right, mutable_allocator());
2738 if (rightmost() == right) mutable_rightmost() = left;
2739 }
2740
2741 template <typename P>
2742 bool btree<P>::try_merge_or_rebalance(iterator *iter) {
2743 node_type *parent = iter->node_->parent();
2744 if (iter->node_->position() > parent->start()) {
2745 // Try merging with our left sibling.
2746 node_type *left = parent->child(iter->node_->position() - 1);
2747 assert(left->max_count() == kNodeSlots);
2748 if (1U + left->count() + iter->node_->count() <= kNodeSlots) {
2749 iter->position_ += 1 + left->count();
2750 merge_nodes(left, iter->node_);
2751 iter->node_ = left;
2752 return true;
2753 }
2754 }
2755 if (iter->node_->position() < parent->finish()) {
2756 // Try merging with our right sibling.
2757 node_type *right = parent->child(iter->node_->position() + 1);
2758 assert(right->max_count() == kNodeSlots);
2759 if (1U + iter->node_->count() + right->count() <= kNodeSlots) {
2760 merge_nodes(iter->node_, right);
2761 return true;
2762 }
2763 // Try rebalancing with our right sibling. We don't perform rebalancing if
2764 // we deleted the first element from iter->node_ and the node is not
2765 // empty. This is a small optimization for the common pattern of deleting
2766 // from the front of the tree.
2767 if (right->count() > kMinNodeValues &&
2768 (iter->node_->count() == 0 || iter->position_ > iter->node_->start())) {
2769 field_type to_move = (right->count() - iter->node_->count()) / 2;
2770 to_move =
2771 (std::min)(to_move, static_cast<field_type>(right->count() - 1));
2772 iter->node_->rebalance_right_to_left(to_move, right, mutable_allocator());
2773 return false;
2774 }
2775 }
2776 if (iter->node_->position() > parent->start()) {
2777 // Try rebalancing with our left sibling. We don't perform rebalancing if
2778 // we deleted the last element from iter->node_ and the node is not
2779 // empty. This is a small optimization for the common pattern of deleting
2780 // from the back of the tree.
2781 node_type *left = parent->child(iter->node_->position() - 1);
2782 if (left->count() > kMinNodeValues &&
2783 (iter->node_->count() == 0 ||
2784 iter->position_ < iter->node_->finish())) {
2785 field_type to_move = (left->count() - iter->node_->count()) / 2;
2786 to_move = (std::min)(to_move, static_cast<field_type>(left->count() - 1));
2787 left->rebalance_left_to_right(to_move, iter->node_, mutable_allocator());
2788 iter->position_ += to_move;
2789 return false;
2790 }
2791 }
2792 return false;
2793 }
2794
2795 template <typename P>
2796 void btree<P>::try_shrink() {
2797 node_type *orig_root = root();
2798 if (orig_root->count() > 0) {
2799 return;
2800 }
2801 // Deleted the last item on the root node, shrink the height of the tree.
2802 if (orig_root->is_leaf()) {
2803 assert(size() == 0);
2804 mutable_root() = mutable_rightmost() = EmptyNode();
2805 } else {
2806 node_type *child = orig_root->start_child();
2807 child->make_root();
2808 mutable_root() = child;
2809 }
2810 node_type::clear_and_delete(orig_root, mutable_allocator());
2811 }
2812
2813 template <typename P>
2814 template <typename IterType>
2815 inline IterType btree<P>::internal_last(IterType iter) {
2816 assert(iter.node_ != nullptr);
2817 while (iter.position_ == iter.node_->finish()) {
2818 iter.position_ = iter.node_->position();
2819 iter.node_ = iter.node_->parent();
2820 if (iter.node_->is_leaf()) {
2821 iter.node_ = nullptr;
2822 break;
2823 }
2824 }
2825 iter.update_generation();
2826 return iter;
2827 }
2828
2829 template <typename P>
2830 template <typename... Args>
2831 inline auto btree<P>::internal_emplace(iterator iter, Args &&...args)
2832 -> iterator {
2833 if (iter.node_->is_internal()) {
2834 // We can't insert on an internal node. Instead, we'll insert after the
2835 // previous value which is guaranteed to be on a leaf node.
2836 --iter;
2837 ++iter.position_;
2838 }
2839 const field_type max_count = iter.node_->max_count();
2840 allocator_type *alloc = mutable_allocator();
2841
2842 const auto transfer_and_delete = [&](node_type *old_node,
2843 node_type *new_node) {
2844 new_node->transfer_n(old_node->count(), new_node->start(),
2845 old_node->start(), old_node, alloc);
2846 new_node->set_finish(old_node->finish());
2847 old_node->set_finish(old_node->start());
2848 new_node->set_generation(old_node->generation());
2849 node_type::clear_and_delete(old_node, alloc);
2850 };
2851 const auto replace_leaf_root_node = [&](field_type new_node_size) {
2852 assert(iter.node_ == root());
2853 node_type *old_root = iter.node_;
2854 node_type *new_root = iter.node_ = new_leaf_root_node(new_node_size);
2855 transfer_and_delete(old_root, new_root);
2856 mutable_root() = mutable_rightmost() = new_root;
2857 };
2858
2859 bool replaced_node = false;
2860 if (iter.node_->count() == max_count) {
2861 // Make room in the leaf for the new item.
2862 if (max_count < kNodeSlots) {
2863 // Insertion into the root where the root is smaller than the full node
2864 // size. Simply grow the size of the root node.
2865 replace_leaf_root_node(static_cast<field_type>(
2866 (std::min)(static_cast<int>(kNodeSlots), 2 * max_count)));
2867 replaced_node = true;
2868 } else {
2869 rebalance_or_split(&iter);
2870 }
2871 }
2872 (void)replaced_node;
2873 #ifdef ABSL_HAVE_ADDRESS_SANITIZER
2874 if (!replaced_node) {
2875 assert(iter.node_->is_leaf());
2876 if (iter.node_->is_root()) {
2877 replace_leaf_root_node(max_count);
2878 } else {
2879 node_type *old_node = iter.node_;
2880 const bool was_rightmost = rightmost() == old_node;
2881 const bool was_leftmost = leftmost() == old_node;
2882 node_type *parent = old_node->parent();
2883 const field_type position = old_node->position();
2884 node_type *new_node = iter.node_ = new_leaf_node(position, parent);
2885 parent->set_child_noupdate_position(position, new_node);
2886 transfer_and_delete(old_node, new_node);
2887 if (was_rightmost) mutable_rightmost() = new_node;
2888 // The leftmost node is stored as the parent of the root node.
2889 if (was_leftmost) root()->set_parent(new_node);
2890 }
2891 }
2892 #endif
2893 iter.node_->emplace_value(static_cast<field_type>(iter.position_), alloc,
2894 std::forward<Args>(args)...);
2895 assert(
2896 iter.node_->is_ordered_correctly(static_cast<field_type>(iter.position_),
2897 original_key_compare(key_comp())) &&
2898 "If this assert fails, then either (1) the comparator may violate "
2899 "transitivity, i.e. comp(a,b) && comp(b,c) -> comp(a,c) (see "
2900 "https://en.cppreference.com/w/cpp/named_req/Compare), or (2) a "
2901 "key may have been mutated after it was inserted into the tree.");
2902 ++size_;
2903 iter.update_generation();
2904 return iter;
2905 }
2906
2907 template <typename P>
2908 template <typename K>
2909 inline auto btree<P>::internal_locate(const K &key) const
2910 -> SearchResult<iterator, is_key_compare_to::value> {
2911 iterator iter(const_cast<node_type *>(root()));
2912 for (;;) {
2913 SearchResult<size_type, is_key_compare_to::value> res =
2914 iter.node_->lower_bound(key, key_comp());
2915 iter.position_ = static_cast<int>(res.value);
2916 if (res.IsEq()) {
2917 return {iter, MatchKind::kEq};
2918 }
2919 // Note: in the non-key-compare-to case, we don't need to walk all the way
2920 // down the tree if the keys are equal, but determining equality would
2921 // require doing an extra comparison on each node on the way down, and we
2922 // will need to go all the way to the leaf node in the expected case.
2923 if (iter.node_->is_leaf()) {
2924 break;
2925 }
2926 iter.node_ = iter.node_->child(static_cast<field_type>(iter.position_));
2927 }
2928 // Note: in the non-key-compare-to case, the key may actually be equivalent
2929 // here (and the MatchKind::kNe is ignored).
2930 return {iter, MatchKind::kNe};
2931 }
2932
2933 template <typename P>
2934 template <typename K>
2935 auto btree<P>::internal_lower_bound(const K &key) const
2936 -> SearchResult<iterator, is_key_compare_to::value> {
2937 if (!params_type::template can_have_multiple_equivalent_keys<K>()) {
2938 SearchResult<iterator, is_key_compare_to::value> ret = internal_locate(key);
2939 ret.value = internal_last(ret.value);
2940 return ret;
2941 }
2942 iterator iter(const_cast<node_type *>(root()));
2943 SearchResult<size_type, is_key_compare_to::value> res;
2944 bool seen_eq = false;
2945 for (;;) {
2946 res = iter.node_->lower_bound(key, key_comp());
2947 iter.position_ = static_cast<int>(res.value);
2948 if (iter.node_->is_leaf()) {
2949 break;
2950 }
2951 seen_eq = seen_eq || res.IsEq();
2952 iter.node_ = iter.node_->child(static_cast<field_type>(iter.position_));
2953 }
2954 if (res.IsEq()) return {iter, MatchKind::kEq};
2955 return {internal_last(iter), seen_eq ? MatchKind::kEq : MatchKind::kNe};
2956 }
2957
2958 template <typename P>
2959 template <typename K>
2960 auto btree<P>::internal_upper_bound(const K &key) const -> iterator {
2961 iterator iter(const_cast<node_type *>(root()));
2962 for (;;) {
2963 iter.position_ = static_cast<int>(iter.node_->upper_bound(key, key_comp()));
2964 if (iter.node_->is_leaf()) {
2965 break;
2966 }
2967 iter.node_ = iter.node_->child(static_cast<field_type>(iter.position_));
2968 }
2969 return internal_last(iter);
2970 }
2971
2972 template <typename P>
2973 template <typename K>
2974 auto btree<P>::internal_find(const K &key) const -> iterator {
2975 SearchResult<iterator, is_key_compare_to::value> res = internal_locate(key);
2976 if (res.HasMatch()) {
2977 if (res.IsEq()) {
2978 return res.value;
2979 }
2980 } else {
2981 const iterator iter = internal_last(res.value);
2982 if (iter.node_ != nullptr && !compare_keys(key, iter.key())) {
2983 return iter;
2984 }
2985 }
2986 return {nullptr, 0};
2987 }
2988
2989 template <typename P>
2990 typename btree<P>::size_type btree<P>::internal_verify(
2991 const node_type *node, const key_type *lo, const key_type *hi) const {
2992 assert(node->count() > 0);
2993 assert(node->count() <= node->max_count());
2994 if (lo) {
2995 assert(!compare_keys(node->key(node->start()), *lo));
2996 }
2997 if (hi) {
2998 assert(!compare_keys(*hi, node->key(node->finish() - 1)));
2999 }
3000 for (int i = node->start() + 1; i < node->finish(); ++i) {
3001 assert(!compare_keys(node->key(i), node->key(i - 1)));
3002 }
3003 size_type count = node->count();
3004 if (node->is_internal()) {
3005 for (field_type i = node->start(); i <= node->finish(); ++i) {
3006 assert(node->child(i) != nullptr);
3007 assert(node->child(i)->parent() == node);
3008 assert(node->child(i)->position() == i);
3009 count += internal_verify(node->child(i),
3010 i == node->start() ? lo : &node->key(i - 1),
3011 i == node->finish() ? hi : &node->key(i));
3012 }
3013 }
3014 return count;
3015 }
3016
3017 struct btree_access {
3018 template <typename BtreeContainer, typename Pred>
3019 static auto erase_if(BtreeContainer &container, Pred pred) ->
3020 typename BtreeContainer::size_type {
3021 const auto initial_size = container.size();
3022 auto &tree = container.tree_;
3023 auto *alloc = tree.mutable_allocator();
3024 for (auto it = container.begin(); it != container.end();) {
3025 if (!pred(*it)) {
3026 ++it;
3027 continue;
3028 }
3029 auto *node = it.node_;
3030 if (node->is_internal()) {
3031 // Handle internal nodes normally.
3032 it = container.erase(it);
3033 continue;
3034 }
3035 // If this is a leaf node, then we do all the erases from this node
3036 // at once before doing rebalancing.
3037
3038 // The current position to transfer slots to.
3039 int to_pos = it.position_;
3040 node->value_destroy(it.position_, alloc);
3041 while (++it.position_ < node->finish()) {
3042 it.update_generation();
3043 if (pred(*it)) {
3044 node->value_destroy(it.position_, alloc);
3045 } else {
3046 node->transfer(node->slot(to_pos++), node->slot(it.position_), alloc);
3047 }
3048 }
3049 const int num_deleted = node->finish() - to_pos;
3050 tree.size_ -= num_deleted;
3051 node->set_finish(to_pos);
3052 it.position_ = to_pos;
3053 it = tree.rebalance_after_delete(it);
3054 }
3055 return initial_size - container.size();
3056 }
3057 };
3058
3059 #undef ABSL_BTREE_ENABLE_GENERATIONS
3060
3061 } // namespace container_internal
3062 ABSL_NAMESPACE_END
3063 } // namespace absl
3064
3065 #endif // ABSL_CONTAINER_INTERNAL_BTREE_H_
3066