Skip to content

Commit fe087ec

Browse files
authored
Rollup merge of #77395 - ssomers:btree_love_the_leaf_edge_comments, r=Mark-Simulacrum
BTreeMap: admit the existence of leaf edges in comments The btree code is ambiguous about leaf edges (i.e., edges within leaf nodes). Iteration relies on them heavily, but some of the comments suggest there are no leaf edges (extracted from #77025) r? @Mark-Simulacrum
2 parents 583269d + df76cf8 commit fe087ec

File tree

1 file changed

+12
-22
lines changed
  • library/alloc/src/collections/btree

1 file changed

+12
-22
lines changed

library/alloc/src/collections/btree/node.rs

+12-22
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,8 @@
99
// struct Node<K, V, height: usize> {
1010
// keys: [K; 2 * B - 1],
1111
// vals: [V; 2 * B - 1],
12-
// edges: if height > 0 {
13-
// [Box<Node<K, V, height - 1>>; 2 * B]
14-
// } else { () },
15-
// parent: Option<NonNull<Node<K, V, height + 1>>>,
16-
// parent_idx: u16,
12+
// edges: [if height > 0 { Box<Node<K, V, height - 1>> } else { () }; 2 * B],
13+
// parent: Option<(NonNull<Node<K, V, height + 1>>, u16)>,
1714
// len: u16,
1815
// }
1916
// ```
@@ -28,8 +25,8 @@
2825
//
2926
// - Trees must have uniform depth/height. This means that every path down to a leaf from a
3027
// given node has exactly the same length.
31-
// - A node of length `n` has `n` keys, `n` values, and (in an internal node) `n + 1` edges.
32-
// This implies that even an empty internal node has at least one edge.
28+
// - A node of length `n` has `n` keys, `n` values, and `n + 1` edges.
29+
// This implies that even an empty node has at least one edge.
3330

3431
use core::cmp::Ordering;
3532
use core::marker::PhantomData;
@@ -298,9 +295,8 @@ impl<'a, K, V> NodeRef<marker::Mut<'a>, K, V, marker::Internal> {
298295
}
299296

300297
impl<BorrowType, K, V, Type> NodeRef<BorrowType, K, V, Type> {
301-
/// Finds the length of the node. This is the number of keys or values. In an
302-
/// internal node, the number of edges is `len() + 1`.
303-
/// For any node, the number of possible edge handles is also `len() + 1`.
298+
/// Finds the length of the node. This is the number of keys or values.
299+
/// The number of edges is `len() + 1`.
304300
/// Note that, despite being safe, calling this function can have the side effect
305301
/// of invalidating mutable references that unsafe code has created.
306302
pub fn len(&self) -> usize {
@@ -321,9 +317,6 @@ impl<BorrowType, K, V, Type> NodeRef<BorrowType, K, V, Type> {
321317
}
322318

323319
/// Exposes the leaf portion of any leaf or internal node.
324-
/// If the node is a leaf, this function simply opens up its data.
325-
/// If the node is an internal node, so not a leaf, it does have all the data a leaf has
326-
/// (header, keys and values), and this function exposes that.
327320
///
328321
/// Returns a raw ptr to avoid invalidating other references to this node,
329322
/// which is possible when BorrowType is marker::ValMut.
@@ -471,9 +464,6 @@ impl<'a, K, V, Type> NodeRef<marker::Mut<'a>, K, V, Type> {
471464
}
472465

473466
/// Exposes the leaf portion of any leaf or internal node for writing.
474-
/// If the node is a leaf, this function simply opens up its data.
475-
/// If the node is an internal node, so not a leaf, it does have all the data a leaf has
476-
/// (header, keys and values), and this function exposes that.
477467
///
478468
/// We don't need to return a raw ptr because we have unique access to the entire node.
479469
fn as_leaf_mut(&mut self) -> &'a mut LeafNode<K, V> {
@@ -679,9 +669,9 @@ impl<'a, K: 'a, V: 'a> NodeRef<marker::Mut<'a>, K, V, marker::Internal> {
679669
}
680670

681671
impl<'a, K: 'a, V: 'a> NodeRef<marker::Mut<'a>, K, V, marker::LeafOrInternal> {
682-
/// Removes a key/value pair from the end of this node and returns the pair.
683-
/// If this is an internal node, also removes the edge that was to the right
684-
/// of that pair and returns the orphaned node that this edge owned.
672+
/// Removes a key/value pair from the end of the node and returns the pair.
673+
/// Also removes the edge that was to the right of that pair and, if the node
674+
/// is internal, returns the orphaned subtree that this edge owned.
685675
fn pop(&mut self) -> (K, V, Option<Root<K, V>>) {
686676
debug_assert!(self.len() > 0);
687677

@@ -705,9 +695,9 @@ impl<'a, K: 'a, V: 'a> NodeRef<marker::Mut<'a>, K, V, marker::LeafOrInternal> {
705695
}
706696
}
707697

708-
/// Removes a key/value pair from the beginning of this node and returns the pair.
709-
/// If this is an internal node, also removes the edge that was to the left
710-
/// of that pair and returns the orphaned node that this edge owned.
698+
/// Removes a key/value pair from the beginning of the node and returns the pair.
699+
/// Also removes the edge that was to the left of that pair and, if the node is
700+
/// internal, returns the orphaned subtree that this edge owned.
711701
fn pop_front(&mut self) -> (K, V, Option<Root<K, V>>) {
712702
debug_assert!(self.len() > 0);
713703

0 commit comments

Comments
 (0)