Skip to content

Commit 6c13556

Browse files
authoredApr 4, 2021
Rollup merge of #82726 - ssomers:btree_node_rearange, r=Mark-Simulacrum
BTree: move blocks around in node.rs Without changing any names or implementation, reorder some members: - Move down the ones defined long ago on the demised `struct Root`, to below the definition of their current host `struct NodeRef`. - Move up some defined on `struct NodeRef` that are interspersed with those defined on `struct Handle`. - Move up the `correct_…` methods squeezed between the two flavours of `push`. - Move the unchecked static downcasts (`cast_to_…`) after the upcasts (`forget_`) and the (weirdly named) dynamic downcasts (`force`). r? ````@Mark-Simulacrum````
·
1.89.01.53.0
2 parents 869726d + e7f340e commit 6c13556

File tree

1 file changed

+165
-167
lines changed
  • library/alloc/src/collections/btree

1 file changed

+165
-167
lines changed
 

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

Lines changed: 165 additions & 167 deletions
Original file line numberDiff line numberDiff line change
@@ -128,106 +128,6 @@ impl<K, V> InternalNode<K, V> {
128128
/// is not a separate type and has no destructor.
129129
type BoxedNode<K, V> = NonNull<LeafNode<K, V>>;
130130

131-
/// The root node of an owned tree.
132-
///
133-
/// Note that this does not have a destructor, and must be cleaned up manually.
134-
pub type Root<K, V> = NodeRef<marker::Owned, K, V, marker::LeafOrInternal>;
135-
136-
impl<K, V> Root<K, V> {
137-
/// Returns a new owned tree, with its own root node that is initially empty.
138-
pub fn new() -> Self {
139-
NodeRef::new_leaf().forget_type()
140-
}
141-
}
142-
143-
impl<K, V> NodeRef<marker::Owned, K, V, marker::Leaf> {
144-
fn new_leaf() -> Self {
145-
Self::from_new_leaf(LeafNode::new())
146-
}
147-
148-
fn from_new_leaf(leaf: Box<LeafNode<K, V>>) -> Self {
149-
NodeRef { height: 0, node: NonNull::from(Box::leak(leaf)), _marker: PhantomData }
150-
}
151-
}
152-
153-
impl<K, V> NodeRef<marker::Owned, K, V, marker::Internal> {
154-
fn new_internal(child: Root<K, V>) -> Self {
155-
let mut new_node = unsafe { InternalNode::new() };
156-
new_node.edges[0].write(child.node);
157-
unsafe { NodeRef::from_new_internal(new_node, child.height + 1) }
158-
}
159-
160-
/// # Safety
161-
/// `height` must not be zero.
162-
unsafe fn from_new_internal(internal: Box<InternalNode<K, V>>, height: usize) -> Self {
163-
debug_assert!(height > 0);
164-
let node = NonNull::from(Box::leak(internal)).cast();
165-
let mut this = NodeRef { height, node, _marker: PhantomData };
166-
this.borrow_mut().correct_all_childrens_parent_links();
167-
this
168-
}
169-
}
170-
171-
impl<K, V, Type> NodeRef<marker::Owned, K, V, Type> {
172-
/// Mutably borrows the owned root node. Unlike `reborrow_mut`, this is safe
173-
/// because the return value cannot be used to destroy the root, and there
174-
/// cannot be other references to the tree.
175-
pub fn borrow_mut(&mut self) -> NodeRef<marker::Mut<'_>, K, V, Type> {
176-
NodeRef { height: self.height, node: self.node, _marker: PhantomData }
177-
}
178-
179-
/// Slightly mutably borrows the owned root node.
180-
pub fn borrow_valmut(&mut self) -> NodeRef<marker::ValMut<'_>, K, V, Type> {
181-
NodeRef { height: self.height, node: self.node, _marker: PhantomData }
182-
}
183-
184-
/// Irreversibly transitions to a reference that permits traversal and offers
185-
/// destructive methods and little else.
186-
pub fn into_dying(self) -> NodeRef<marker::Dying, K, V, Type> {
187-
NodeRef { height: self.height, node: self.node, _marker: PhantomData }
188-
}
189-
}
190-
191-
impl<K, V> NodeRef<marker::Owned, K, V, marker::LeafOrInternal> {
192-
/// Adds a new internal node with a single edge pointing to the previous root node,
193-
/// make that new node the root node, and return it. This increases the height by 1
194-
/// and is the opposite of `pop_internal_level`.
195-
pub fn push_internal_level(&mut self) -> NodeRef<marker::Mut<'_>, K, V, marker::Internal> {
196-
super::mem::take_mut(self, |old_root| NodeRef::new_internal(old_root).forget_type());
197-
198-
// `self.borrow_mut()`, except that we just forgot we're internal now:
199-
NodeRef { height: self.height, node: self.node, _marker: PhantomData }
200-
}
201-
202-
/// Removes the internal root node, using its first child as the new root node.
203-
/// As it is intended only to be called when the root node has only one child,
204-
/// no cleanup is done on any of the keys, values and other children.
205-
/// This decreases the height by 1 and is the opposite of `push_internal_level`.
206-
///
207-
/// Requires exclusive access to the `Root` object but not to the root node;
208-
/// it will not invalidate other handles or references to the root node.
209-
///
210-
/// Panics if there is no internal level, i.e., if the root node is a leaf.
211-
pub fn pop_internal_level(&mut self) {
212-
assert!(self.height > 0);
213-
214-
let top = self.node;
215-
216-
// SAFETY: we asserted to be internal.
217-
let internal_self = unsafe { self.borrow_mut().cast_to_internal_unchecked() };
218-
// SAFETY: we borrowed `self` exclusively and its borrow type is exclusive.
219-
let internal_node = unsafe { &mut *NodeRef::as_internal_ptr(&internal_self) };
220-
// SAFETY: the first edge is always initialized.
221-
self.node = unsafe { internal_node.edges[0].assume_init_read() };
222-
self.height -= 1;
223-
self.clear_parent_link();
224-
225-
unsafe {
226-
Global.deallocate(top.cast(), Layout::new::<InternalNode<K, V>>());
227-
}
228-
}
229-
}
230-
231131
// N.B. `NodeRef` is always covariant in `K` and `V`, even when the `BorrowType`
232132
// is `Mut`. This is technically wrong, but cannot result in any unsafety due to
233133
// internal use of `NodeRef` because we stay completely generic over `K` and `V`.
@@ -292,6 +192,11 @@ pub struct NodeRef<BorrowType, K, V, Type> {
292192
_marker: PhantomData<(BorrowType, Type)>,
293193
}
294194

195+
/// The root node of an owned tree.
196+
///
197+
/// Note that this does not have a destructor, and must be cleaned up manually.
198+
pub type Root<K, V> = NodeRef<marker::Owned, K, V, marker::LeafOrInternal>;
199+
295200
impl<'a, K: 'a, V: 'a, Type> Copy for NodeRef<marker::Immut<'a>, K, V, Type> {}
296201
impl<'a, K: 'a, V: 'a, Type> Clone for NodeRef<marker::Immut<'a>, K, V, Type> {
297202
fn clone(&self) -> Self {
@@ -307,6 +212,34 @@ unsafe impl<'a, K: Send + 'a, V: Send + 'a, Type> Send for NodeRef<marker::ValMu
307212
unsafe impl<K: Send, V: Send, Type> Send for NodeRef<marker::Owned, K, V, Type> {}
308213
unsafe impl<K: Send, V: Send, Type> Send for NodeRef<marker::Dying, K, V, Type> {}
309214

215+
impl<K, V> NodeRef<marker::Owned, K, V, marker::Leaf> {
216+
fn new_leaf() -> Self {
217+
Self::from_new_leaf(LeafNode::new())
218+
}
219+
220+
fn from_new_leaf(leaf: Box<LeafNode<K, V>>) -> Self {
221+
NodeRef { height: 0, node: NonNull::from(Box::leak(leaf)), _marker: PhantomData }
222+
}
223+
}
224+
225+
impl<K, V> NodeRef<marker::Owned, K, V, marker::Internal> {
226+
fn new_internal(child: Root<K, V>) -> Self {
227+
let mut new_node = unsafe { InternalNode::new() };
228+
new_node.edges[0].write(child.node);
229+
unsafe { NodeRef::from_new_internal(new_node, child.height + 1) }
230+
}
231+
232+
/// # Safety
233+
/// `height` must not be zero.
234+
unsafe fn from_new_internal(internal: Box<InternalNode<K, V>>, height: usize) -> Self {
235+
debug_assert!(height > 0);
236+
let node = NonNull::from(Box::leak(internal)).cast();
237+
let mut this = NodeRef { height, node, _marker: PhantomData };
238+
this.borrow_mut().correct_all_childrens_parent_links();
239+
this
240+
}
241+
}
242+
310243
impl<BorrowType, K, V> NodeRef<BorrowType, K, V, marker::Internal> {
311244
/// Unpack a node reference that was packed as `NodeRef::parent`.
312245
fn from_internal(node: NonNull<InternalNode<K, V>>, height: usize) -> Self {
@@ -420,6 +353,19 @@ impl<BorrowType: marker::BorrowType, K, V, Type> NodeRef<BorrowType, K, V, Type>
420353
}
421354
}
422355

356+
impl<BorrowType, K, V, Type> NodeRef<BorrowType, K, V, Type> {
357+
/// Could be a public implementation of PartialEq, but only used in this module.
358+
fn eq(&self, other: &Self) -> bool {
359+
let Self { node, height, _marker } = self;
360+
if node.eq(&other.node) {
361+
debug_assert_eq!(*height, other.height);
362+
true
363+
} else {
364+
false
365+
}
366+
}
367+
}
368+
423369
impl<'a, K: 'a, V: 'a, Type> NodeRef<marker::Immut<'a>, K, V, Type> {
424370
/// Exposes the leaf portion of any leaf or internal node in an immutable tree.
425371
fn into_leaf(self) -> &'a LeafNode<K, V> {
@@ -461,20 +407,6 @@ impl<K, V> NodeRef<marker::Dying, K, V, marker::LeafOrInternal> {
461407
}
462408
}
463409

464-
impl<'a, K, V> NodeRef<marker::Mut<'a>, K, V, marker::LeafOrInternal> {
465-
/// Unsafely asserts to the compiler the static information that this node is a `Leaf`.
466-
unsafe fn cast_to_leaf_unchecked(self) -> NodeRef<marker::Mut<'a>, K, V, marker::Leaf> {
467-
debug_assert!(self.height == 0);
468-
NodeRef { height: self.height, node: self.node, _marker: PhantomData }
469-
}
470-
471-
/// Unsafely asserts to the compiler the static information that this node is an `Internal`.
472-
unsafe fn cast_to_internal_unchecked(self) -> NodeRef<marker::Mut<'a>, K, V, marker::Internal> {
473-
debug_assert!(self.height > 0);
474-
NodeRef { height: self.height, node: self.node, _marker: PhantomData }
475-
}
476-
}
477-
478410
impl<'a, K, V, Type> NodeRef<marker::Mut<'a>, K, V, Type> {
479411
/// Temporarily takes out another, mutable reference to the same node. Beware, as
480412
/// this method is very dangerous, doubly so since it may not immediately appear
@@ -577,6 +509,22 @@ impl<'a, K: 'a, V: 'a, Type> NodeRef<marker::Mut<'a>, K, V, Type> {
577509
}
578510
}
579511

512+
impl<'a, K, V> NodeRef<marker::Mut<'a>, K, V, marker::Internal> {
513+
/// # Safety
514+
/// Every item returned by `range` is a valid edge index for the node.
515+
unsafe fn correct_childrens_parent_links<R: Iterator<Item = usize>>(&mut self, range: R) {
516+
for i in range {
517+
debug_assert!(i <= self.len());
518+
unsafe { Handle::new_edge(self.reborrow_mut(), i) }.correct_parent_link();
519+
}
520+
}
521+
522+
fn correct_all_childrens_parent_links(&mut self) {
523+
let len = self.len();
524+
unsafe { self.correct_childrens_parent_links(0..=len) };
525+
}
526+
}
527+
580528
impl<'a, K: 'a, V: 'a> NodeRef<marker::Mut<'a>, K, V, marker::LeafOrInternal> {
581529
/// Sets the node's link to its parent edge,
582530
/// without invalidating other references to the node.
@@ -596,6 +544,71 @@ impl<K, V> NodeRef<marker::Owned, K, V, marker::LeafOrInternal> {
596544
}
597545
}
598546

547+
impl<K, V> NodeRef<marker::Owned, K, V, marker::LeafOrInternal> {
548+
/// Returns a new owned tree, with its own root node that is initially empty.
549+
pub fn new() -> Self {
550+
NodeRef::new_leaf().forget_type()
551+
}
552+
553+
/// Adds a new internal node with a single edge pointing to the previous root node,
554+
/// make that new node the root node, and return it. This increases the height by 1
555+
/// and is the opposite of `pop_internal_level`.
556+
pub fn push_internal_level(&mut self) -> NodeRef<marker::Mut<'_>, K, V, marker::Internal> {
557+
super::mem::take_mut(self, |old_root| NodeRef::new_internal(old_root).forget_type());
558+
559+
// `self.borrow_mut()`, except that we just forgot we're internal now:
560+
NodeRef { height: self.height, node: self.node, _marker: PhantomData }
561+
}
562+
563+
/// Removes the internal root node, using its first child as the new root node.
564+
/// As it is intended only to be called when the root node has only one child,
565+
/// no cleanup is done on any of the keys, values and other children.
566+
/// This decreases the height by 1 and is the opposite of `push_internal_level`.
567+
///
568+
/// Requires exclusive access to the `Root` object but not to the root node;
569+
/// it will not invalidate other handles or references to the root node.
570+
///
571+
/// Panics if there is no internal level, i.e., if the root node is a leaf.
572+
pub fn pop_internal_level(&mut self) {
573+
assert!(self.height > 0);
574+
575+
let top = self.node;
576+
577+
// SAFETY: we asserted to be internal.
578+
let internal_self = unsafe { self.borrow_mut().cast_to_internal_unchecked() };
579+
// SAFETY: we borrowed `self` exclusively and its borrow type is exclusive.
580+
let internal_node = unsafe { &mut *NodeRef::as_internal_ptr(&internal_self) };
581+
// SAFETY: the first edge is always initialized.
582+
self.node = unsafe { internal_node.edges[0].assume_init_read() };
583+
self.height -= 1;
584+
self.clear_parent_link();
585+
586+
unsafe {
587+
Global.deallocate(top.cast(), Layout::new::<InternalNode<K, V>>());
588+
}
589+
}
590+
}
591+
592+
impl<K, V, Type> NodeRef<marker::Owned, K, V, Type> {
593+
/// Mutably borrows the owned root node. Unlike `reborrow_mut`, this is safe
594+
/// because the return value cannot be used to destroy the root, and there
595+
/// cannot be other references to the tree.
596+
pub fn borrow_mut(&mut self) -> NodeRef<marker::Mut<'_>, K, V, Type> {
597+
NodeRef { height: self.height, node: self.node, _marker: PhantomData }
598+
}
599+
600+
/// Slightly mutably borrows the owned root node.
601+
pub fn borrow_valmut(&mut self) -> NodeRef<marker::ValMut<'_>, K, V, Type> {
602+
NodeRef { height: self.height, node: self.node, _marker: PhantomData }
603+
}
604+
605+
/// Irreversibly transitions to a reference that permits traversal and offers
606+
/// destructive methods and little else.
607+
pub fn into_dying(self) -> NodeRef<marker::Dying, K, V, Type> {
608+
NodeRef { height: self.height, node: self.node, _marker: PhantomData }
609+
}
610+
}
611+
599612
impl<'a, K: 'a, V: 'a> NodeRef<marker::Mut<'a>, K, V, marker::Leaf> {
600613
/// Adds a key-value pair to the end of the node.
601614
pub fn push(&mut self, key: K, val: V) {
@@ -610,22 +623,6 @@ impl<'a, K: 'a, V: 'a> NodeRef<marker::Mut<'a>, K, V, marker::Leaf> {
610623
}
611624
}
612625

613-
impl<'a, K, V> NodeRef<marker::Mut<'a>, K, V, marker::Internal> {
614-
/// # Safety
615-
/// Every item returned by `range` is a valid edge index for the node.
616-
unsafe fn correct_childrens_parent_links<R: Iterator<Item = usize>>(&mut self, range: R) {
617-
for i in range {
618-
debug_assert!(i <= self.len());
619-
unsafe { Handle::new_edge(self.reborrow_mut(), i) }.correct_parent_link();
620-
}
621-
}
622-
623-
fn correct_all_childrens_parent_links(&mut self) {
624-
let len = self.len();
625-
unsafe { self.correct_childrens_parent_links(0..=len) };
626-
}
627-
}
628-
629626
impl<'a, K: 'a, V: 'a> NodeRef<marker::Mut<'a>, K, V, marker::Internal> {
630627
/// Adds a key-value pair, and an edge to go to the right of that pair,
631628
/// to the end of the node.
@@ -645,6 +642,20 @@ impl<'a, K: 'a, V: 'a> NodeRef<marker::Mut<'a>, K, V, marker::Internal> {
645642
}
646643
}
647644

645+
impl<BorrowType, K, V> NodeRef<BorrowType, K, V, marker::Leaf> {
646+
/// Removes any static information asserting that this node is a `Leaf` node.
647+
pub fn forget_type(self) -> NodeRef<BorrowType, K, V, marker::LeafOrInternal> {
648+
NodeRef { height: self.height, node: self.node, _marker: PhantomData }
649+
}
650+
}
651+
652+
impl<BorrowType, K, V> NodeRef<BorrowType, K, V, marker::Internal> {
653+
/// Removes any static information asserting that this node is an `Internal` node.
654+
pub fn forget_type(self) -> NodeRef<BorrowType, K, V, marker::LeafOrInternal> {
655+
NodeRef { height: self.height, node: self.node, _marker: PhantomData }
656+
}
657+
}
658+
648659
impl<BorrowType, K, V> NodeRef<BorrowType, K, V, marker::LeafOrInternal> {
649660
/// Checks whether a node is an `Internal` node or a `Leaf` node.
650661
pub fn force(
@@ -669,6 +680,20 @@ impl<BorrowType, K, V> NodeRef<BorrowType, K, V, marker::LeafOrInternal> {
669680
}
670681
}
671682

683+
impl<'a, K, V> NodeRef<marker::Mut<'a>, K, V, marker::LeafOrInternal> {
684+
/// Unsafely asserts to the compiler the static information that this node is a `Leaf`.
685+
unsafe fn cast_to_leaf_unchecked(self) -> NodeRef<marker::Mut<'a>, K, V, marker::Leaf> {
686+
debug_assert!(self.height == 0);
687+
NodeRef { height: self.height, node: self.node, _marker: PhantomData }
688+
}
689+
690+
/// Unsafely asserts to the compiler the static information that this node is an `Internal`.
691+
unsafe fn cast_to_internal_unchecked(self) -> NodeRef<marker::Mut<'a>, K, V, marker::Internal> {
692+
debug_assert!(self.height > 0);
693+
NodeRef { height: self.height, node: self.node, _marker: PhantomData }
694+
}
695+
}
696+
672697
/// A reference to a specific key-value pair or edge within a node. The `Node` parameter
673698
/// must be a `NodeRef`, while the `Type` can either be `KV` (signifying a handle on a key-value
674699
/// pair) or `Edge` (signifying a handle on an edge).
@@ -722,19 +747,6 @@ impl<BorrowType, K, V, NodeType> Handle<NodeRef<BorrowType, K, V, NodeType>, mar
722747
}
723748
}
724749

725-
impl<BorrowType, K, V, NodeType> NodeRef<BorrowType, K, V, NodeType> {
726-
/// Could be a public implementation of PartialEq, but only used in this module.
727-
fn eq(&self, other: &Self) -> bool {
728-
let Self { node, height, _marker } = self;
729-
if node.eq(&other.node) {
730-
debug_assert_eq!(*height, other.height);
731-
true
732-
} else {
733-
false
734-
}
735-
}
736-
}
737-
738750
impl<BorrowType, K, V, NodeType, HandleType> PartialEq
739751
for Handle<NodeRef<BorrowType, K, V, NodeType>, HandleType>
740752
{
@@ -754,16 +766,6 @@ impl<BorrowType, K, V, NodeType, HandleType>
754766
}
755767
}
756768

757-
impl<'a, K, V, Type> Handle<NodeRef<marker::Mut<'a>, K, V, marker::LeafOrInternal>, Type> {
758-
/// Unsafely asserts to the compiler the static information that the handle's node is a `Leaf`.
759-
pub unsafe fn cast_to_leaf_unchecked(
760-
self,
761-
) -> Handle<NodeRef<marker::Mut<'a>, K, V, marker::Leaf>, Type> {
762-
let node = unsafe { self.node.cast_to_leaf_unchecked() };
763-
Handle { node, idx: self.idx, _marker: PhantomData }
764-
}
765-
}
766-
767769
impl<'a, K, V, NodeType, HandleType> Handle<NodeRef<marker::Mut<'a>, K, V, NodeType>, HandleType> {
768770
/// Temporarily takes out another, mutable handle on the same location. Beware, as
769771
/// this method is very dangerous, doubly so since it may not immediately appear
@@ -1466,20 +1468,6 @@ impl<'a, K: 'a, V: 'a> BalancingContext<'a, K, V> {
14661468
}
14671469
}
14681470

1469-
impl<BorrowType, K, V> NodeRef<BorrowType, K, V, marker::Leaf> {
1470-
/// Removes any static information asserting that this node is a `Leaf` node.
1471-
pub fn forget_type(self) -> NodeRef<BorrowType, K, V, marker::LeafOrInternal> {
1472-
NodeRef { height: self.height, node: self.node, _marker: PhantomData }
1473-
}
1474-
}
1475-
1476-
impl<BorrowType, K, V> NodeRef<BorrowType, K, V, marker::Internal> {
1477-
/// Removes any static information asserting that this node is an `Internal` node.
1478-
pub fn forget_type(self) -> NodeRef<BorrowType, K, V, marker::LeafOrInternal> {
1479-
NodeRef { height: self.height, node: self.node, _marker: PhantomData }
1480-
}
1481-
}
1482-
14831471
impl<BorrowType, K, V> Handle<NodeRef<BorrowType, K, V, marker::Leaf>, marker::Edge> {
14841472
pub fn forget_node_type(
14851473
self,
@@ -1531,6 +1519,16 @@ impl<BorrowType, K, V, Type> Handle<NodeRef<BorrowType, K, V, marker::LeafOrInte
15311519
}
15321520
}
15331521

1522+
impl<'a, K, V, Type> Handle<NodeRef<marker::Mut<'a>, K, V, marker::LeafOrInternal>, Type> {
1523+
/// Unsafely asserts to the compiler the static information that the handle's node is a `Leaf`.
1524+
pub unsafe fn cast_to_leaf_unchecked(
1525+
self,
1526+
) -> Handle<NodeRef<marker::Mut<'a>, K, V, marker::Leaf>, Type> {
1527+
let node = unsafe { self.node.cast_to_leaf_unchecked() };
1528+
Handle { node, idx: self.idx, _marker: PhantomData }
1529+
}
1530+
}
1531+
15341532
impl<'a, K, V> Handle<NodeRef<marker::Mut<'a>, K, V, marker::LeafOrInternal>, marker::Edge> {
15351533
/// Move the suffix after `self` from one node to another one. `right` must be empty.
15361534
/// The first edge of `right` remains unchanged.

0 commit comments

Comments
 (0)
Please sign in to comment.