Skip to content

Commit 441fd22

Browse files
committed
Auto merge of #75329 - ssomers:btree_cleanup_8, r=Mark-Simulacrum
BTreeMap: better distinguish the root holder from the root node Renames and intermediate variables
2 parents a9025c5 + ef753fc commit 441fd22

File tree

2 files changed

+51
-42
lines changed

2 files changed

+51
-42
lines changed

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

+40-33
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@
22

33
use core::borrow::Borrow;
44
use core::cmp::Ordering;
5-
use core::fmt::Debug;
5+
use core::fmt::{self, Debug};
66
use core::hash::{Hash, Hasher};
77
use core::iter::{FromIterator, FusedIterator, Peekable};
88
use core::marker::PhantomData;
99
use core::mem::{self, ManuallyDrop};
1010
use core::ops::Bound::{Excluded, Included, Unbounded};
1111
use core::ops::{Index, RangeBounds};
12-
use core::{fmt, ptr};
12+
use core::ptr;
1313

1414
use super::node::{self, marker, ForceResult::*, Handle, InsertResult::*, NodeRef};
1515
use super::search::{self, SearchResult::*};
@@ -154,7 +154,7 @@ impl<K: Clone, V: Clone> Clone for BTreeMap<K, V> {
154154

155155
{
156156
let root = out_tree.root.as_mut().unwrap(); // unwrap succeeds because we just wrapped
157-
let mut out_node = match root.as_mut().force() {
157+
let mut out_node = match root.node_as_mut().force() {
158158
Leaf(leaf) => leaf,
159159
Internal(_) => unreachable!(),
160160
};
@@ -210,7 +210,7 @@ impl<K: Clone, V: Clone> Clone for BTreeMap<K, V> {
210210
// Ord` constraint, which this method lacks.
211211
BTreeMap { root: None, length: 0 }
212212
} else {
213-
clone_subtree(self.root.as_ref().unwrap().as_ref()) // unwrap succeeds because not empty
213+
clone_subtree(self.root.as_ref().unwrap().node_as_ref()) // unwrap succeeds because not empty
214214
}
215215
}
216216
}
@@ -223,14 +223,16 @@ where
223223
type Key = K;
224224

225225
fn get(&self, key: &Q) -> Option<&K> {
226-
match search::search_tree(self.root.as_ref()?.as_ref(), key) {
226+
let root_node = self.root.as_ref()?.node_as_ref();
227+
match search::search_tree(root_node, key) {
227228
Found(handle) => Some(handle.into_kv().0),
228229
GoDown(_) => None,
229230
}
230231
}
231232

232233
fn take(&mut self, key: &Q) -> Option<K> {
233-
match search::search_tree(self.root.as_mut()?.as_mut(), key) {
234+
let root_node = self.root.as_mut()?.node_as_mut();
235+
match search::search_tree(root_node, key) {
234236
Found(handle) => Some(
235237
OccupiedEntry { handle, length: &mut self.length, _marker: PhantomData }
236238
.remove_kv()
@@ -242,7 +244,7 @@ where
242244

243245
fn replace(&mut self, key: K) -> Option<K> {
244246
let root = Self::ensure_is_owned(&mut self.root);
245-
match search::search_tree::<marker::Mut<'_>, K, (), K>(root.as_mut(), &key) {
247+
match search::search_tree::<marker::Mut<'_>, K, (), K>(root.node_as_mut(), &key) {
246248
Found(handle) => Some(mem::replace(handle.into_kv_mut().0, key)),
247249
GoDown(handle) => {
248250
VacantEntry { key, handle, length: &mut self.length, _marker: PhantomData }
@@ -565,7 +567,8 @@ impl<K: Ord, V> BTreeMap<K, V> {
565567
K: Borrow<Q>,
566568
Q: Ord,
567569
{
568-
match search::search_tree(self.root.as_ref()?.as_ref(), key) {
570+
let root_node = self.root.as_ref()?.node_as_ref();
571+
match search::search_tree(root_node, key) {
569572
Found(handle) => Some(handle.into_kv().1),
570573
GoDown(_) => None,
571574
}
@@ -592,7 +595,8 @@ impl<K: Ord, V> BTreeMap<K, V> {
592595
K: Borrow<Q>,
593596
Q: Ord,
594597
{
595-
match search::search_tree(self.root.as_ref()?.as_ref(), k) {
598+
let root_node = self.root.as_ref()?.node_as_ref();
599+
match search::search_tree(root_node, k) {
596600
Found(handle) => Some(handle.into_kv()),
597601
GoDown(_) => None,
598602
}
@@ -617,8 +621,8 @@ impl<K: Ord, V> BTreeMap<K, V> {
617621
/// ```
618622
#[unstable(feature = "map_first_last", issue = "62924")]
619623
pub fn first_key_value(&self) -> Option<(&K, &V)> {
620-
let front = self.root.as_ref()?.as_ref().first_leaf_edge();
621-
front.right_kv().ok().map(Handle::into_kv)
624+
let root_node = self.root.as_ref()?.node_as_ref();
625+
root_node.first_leaf_edge().right_kv().ok().map(Handle::into_kv)
622626
}
623627

624628
/// Returns the first entry in the map for in-place manipulation.
@@ -643,8 +647,8 @@ impl<K: Ord, V> BTreeMap<K, V> {
643647
/// ```
644648
#[unstable(feature = "map_first_last", issue = "62924")]
645649
pub fn first_entry(&mut self) -> Option<OccupiedEntry<'_, K, V>> {
646-
let front = self.root.as_mut()?.as_mut().first_leaf_edge();
647-
let kv = front.right_kv().ok()?;
650+
let root_node = self.root.as_mut()?.node_as_mut();
651+
let kv = root_node.first_leaf_edge().right_kv().ok()?;
648652
Some(OccupiedEntry {
649653
handle: kv.forget_node_type(),
650654
length: &mut self.length,
@@ -694,8 +698,8 @@ impl<K: Ord, V> BTreeMap<K, V> {
694698
/// ```
695699
#[unstable(feature = "map_first_last", issue = "62924")]
696700
pub fn last_key_value(&self) -> Option<(&K, &V)> {
697-
let back = self.root.as_ref()?.as_ref().last_leaf_edge();
698-
back.left_kv().ok().map(Handle::into_kv)
701+
let root_node = self.root.as_ref()?.node_as_ref();
702+
root_node.last_leaf_edge().left_kv().ok().map(Handle::into_kv)
699703
}
700704

701705
/// Returns the last entry in the map for in-place manipulation.
@@ -720,8 +724,8 @@ impl<K: Ord, V> BTreeMap<K, V> {
720724
/// ```
721725
#[unstable(feature = "map_first_last", issue = "62924")]
722726
pub fn last_entry(&mut self) -> Option<OccupiedEntry<'_, K, V>> {
723-
let back = self.root.as_mut()?.as_mut().last_leaf_edge();
724-
let kv = back.left_kv().ok()?;
727+
let root_node = self.root.as_mut()?.node_as_mut();
728+
let kv = root_node.last_leaf_edge().left_kv().ok()?;
725729
Some(OccupiedEntry {
726730
handle: kv.forget_node_type(),
727731
length: &mut self.length,
@@ -805,7 +809,8 @@ impl<K: Ord, V> BTreeMap<K, V> {
805809
K: Borrow<Q>,
806810
Q: Ord,
807811
{
808-
match search::search_tree(self.root.as_mut()?.as_mut(), key) {
812+
let root_node = self.root.as_mut()?.node_as_mut();
813+
match search::search_tree(root_node, key) {
809814
Found(handle) => Some(handle.into_kv_mut().1),
810815
GoDown(_) => None,
811816
}
@@ -899,7 +904,8 @@ impl<K: Ord, V> BTreeMap<K, V> {
899904
K: Borrow<Q>,
900905
Q: Ord,
901906
{
902-
match search::search_tree(self.root.as_mut()?.as_mut(), key) {
907+
let root_node = self.root.as_mut()?.node_as_mut();
908+
match search::search_tree(root_node, key) {
903909
Found(handle) => Some(
904910
OccupiedEntry { handle, length: &mut self.length, _marker: PhantomData }
905911
.remove_entry(),
@@ -995,7 +1001,7 @@ impl<K: Ord, V> BTreeMap<K, V> {
9951001
R: RangeBounds<T>,
9961002
{
9971003
if let Some(root) = &self.root {
998-
let (f, b) = range_search(root.as_ref(), range);
1004+
let (f, b) = range_search(root.node_as_ref(), range);
9991005

10001006
Range { front: Some(f), back: Some(b) }
10011007
} else {
@@ -1041,7 +1047,7 @@ impl<K: Ord, V> BTreeMap<K, V> {
10411047
R: RangeBounds<T>,
10421048
{
10431049
if let Some(root) = &mut self.root {
1044-
let (f, b) = range_search(root.as_mut(), range);
1050+
let (f, b) = range_search(root.node_as_mut(), range);
10451051

10461052
RangeMut { front: Some(f), back: Some(b), _marker: PhantomData }
10471053
} else {
@@ -1071,7 +1077,7 @@ impl<K: Ord, V> BTreeMap<K, V> {
10711077
pub fn entry(&mut self, key: K) -> Entry<'_, K, V> {
10721078
// FIXME(@porglezomp) Avoid allocating if we don't insert
10731079
let root = Self::ensure_is_owned(&mut self.root);
1074-
match search::search_tree(root.as_mut(), &key) {
1080+
match search::search_tree(root.node_as_mut(), &key) {
10751081
Found(handle) => {
10761082
Occupied(OccupiedEntry { handle, length: &mut self.length, _marker: PhantomData })
10771083
}
@@ -1083,7 +1089,7 @@ impl<K: Ord, V> BTreeMap<K, V> {
10831089

10841090
fn from_sorted_iter<I: Iterator<Item = (K, V)>>(&mut self, iter: I) {
10851091
let root = Self::ensure_is_owned(&mut self.root);
1086-
let mut cur_node = root.as_mut().last_leaf_edge().into_node();
1092+
let mut cur_node = root.node_as_mut().last_leaf_edge().into_node();
10871093
// Iterate through all key-value pairs, pushing them into nodes at the right level.
10881094
for (key, value) in iter {
10891095
// Try to push key-value pair into the current leaf node.
@@ -1133,7 +1139,7 @@ impl<K: Ord, V> BTreeMap<K, V> {
11331139

11341140
fn fix_right_edge(root: &mut node::Root<K, V>) {
11351141
// Handle underfull nodes, start from the top.
1136-
let mut cur_node = root.as_mut();
1142+
let mut cur_node = root.node_as_mut();
11371143
while let Internal(internal) = cur_node.force() {
11381144
// Check if right-most child is underfull.
11391145
let mut last_edge = internal.last_edge();
@@ -1201,8 +1207,8 @@ impl<K: Ord, V> BTreeMap<K, V> {
12011207
}
12021208

12031209
{
1204-
let mut left_node = left_root.as_mut();
1205-
let mut right_node = right_root.as_mut();
1210+
let mut left_node = left_root.node_as_mut();
1211+
let mut right_node = right_root.node_as_mut();
12061212

12071213
loop {
12081214
let mut split_edge = match search::search_node(left_node, key) {
@@ -1280,7 +1286,8 @@ impl<K: Ord, V> BTreeMap<K, V> {
12801286
DrainFilter { pred, inner: self.drain_filter_inner() }
12811287
}
12821288
pub(super) fn drain_filter_inner(&mut self) -> DrainFilterInner<'_, K, V> {
1283-
let front = self.root.as_mut().map(|r| r.as_mut().first_leaf_edge());
1289+
let root_node = self.root.as_mut().map(|r| r.node_as_mut());
1290+
let front = root_node.map(|rn| rn.first_leaf_edge());
12841291
DrainFilterInner {
12851292
length: &mut self.length,
12861293
cur_leaf_edge: front,
@@ -1315,7 +1322,7 @@ impl<K: Ord, V> BTreeMap<K, V> {
13151322
res
13161323
}
13171324

1318-
self.length = dfs(self.root.as_ref().unwrap().as_ref());
1325+
self.length = dfs(self.root.as_ref().unwrap().node_as_ref());
13191326
}
13201327

13211328
/// Creates a consuming iterator visiting all the keys, in sorted order.
@@ -2251,7 +2258,7 @@ impl<K, V> BTreeMap<K, V> {
22512258
#[stable(feature = "rust1", since = "1.0.0")]
22522259
pub fn iter(&self) -> Iter<'_, K, V> {
22532260
if let Some(root) = &self.root {
2254-
let (f, b) = full_range_search(root.as_ref());
2261+
let (f, b) = full_range_search(root.node_as_ref());
22552262

22562263
Iter { range: Range { front: Some(f), back: Some(b) }, length: self.length }
22572264
} else {
@@ -2283,7 +2290,7 @@ impl<K, V> BTreeMap<K, V> {
22832290
#[stable(feature = "rust1", since = "1.0.0")]
22842291
pub fn iter_mut(&mut self) -> IterMut<'_, K, V> {
22852292
if let Some(root) = &mut self.root {
2286-
let (f, b) = full_range_search(root.as_mut());
2293+
let (f, b) = full_range_search(root.node_as_mut());
22872294

22882295
IterMut {
22892296
range: RangeMut { front: Some(f), back: Some(b), _marker: PhantomData },
@@ -2895,7 +2902,7 @@ impl<'a, K: 'a, V: 'a> Handle<NodeRef<marker::Mut<'a>, K, V, marker::LeafOrInter
28952902
impl<K, V> node::Root<K, V> {
28962903
/// Removes empty levels on the top, but keep an empty leaf if the entire tree is empty.
28972904
fn fix_top(&mut self) {
2898-
while self.height() > 0 && self.as_ref().len() == 0 {
2905+
while self.height() > 0 && self.node_as_ref().len() == 0 {
28992906
self.pop_internal_level();
29002907
}
29012908
}
@@ -2904,7 +2911,7 @@ impl<K, V> node::Root<K, V> {
29042911
self.fix_top();
29052912

29062913
{
2907-
let mut cur_node = self.as_mut();
2914+
let mut cur_node = self.node_as_mut();
29082915

29092916
while let Internal(node) = cur_node.force() {
29102917
let mut last_kv = node.last_kv();
@@ -2930,7 +2937,7 @@ impl<K, V> node::Root<K, V> {
29302937
self.fix_top();
29312938

29322939
{
2933-
let mut cur_node = self.as_mut();
2940+
let mut cur_node = self.node_as_mut();
29342941

29352942
while let Internal(node) = cur_node.force() {
29362943
let mut first_kv = node.first_kv();

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

+11-9
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,8 @@ impl<K, V> Root<K, V> {
163163
Root { node: BoxedNode::from_leaf(Box::new(unsafe { LeafNode::new() })), height: 0 }
164164
}
165165

166-
pub fn as_ref(&self) -> NodeRef<marker::Immut<'_>, K, V, marker::LeafOrInternal> {
166+
/// Borrows and returns an immutable reference to the node owned by the root.
167+
pub fn node_as_ref(&self) -> NodeRef<marker::Immut<'_>, K, V, marker::LeafOrInternal> {
167168
NodeRef {
168169
height: self.height,
169170
node: self.node.as_ptr(),
@@ -172,7 +173,8 @@ impl<K, V> Root<K, V> {
172173
}
173174
}
174175

175-
pub fn as_mut(&mut self) -> NodeRef<marker::Mut<'_>, K, V, marker::LeafOrInternal> {
176+
/// Borrows and returns a mutable reference to the node owned by the root.
177+
pub fn node_as_mut(&mut self) -> NodeRef<marker::Mut<'_>, K, V, marker::LeafOrInternal> {
176178
NodeRef {
177179
height: self.height,
178180
node: self.node.as_ptr(),
@@ -226,12 +228,12 @@ impl<K, V> Root<K, V> {
226228

227229
self.node = unsafe {
228230
BoxedNode::from_ptr(
229-
self.as_mut().cast_unchecked::<marker::Internal>().first_edge().descend().node,
231+
self.node_as_mut().cast_unchecked::<marker::Internal>().first_edge().descend().node,
230232
)
231233
};
232234
self.height -= 1;
233235
unsafe {
234-
(*self.as_mut().as_leaf_mut()).parent = ptr::null();
236+
(*self.node_as_mut().as_leaf_mut()).parent = ptr::null();
235237
}
236238

237239
unsafe {
@@ -616,7 +618,7 @@ impl<'a, K, V> NodeRef<marker::Mut<'a>, K, V, marker::LeafOrInternal> {
616618
let edge =
617619
ptr::read(internal.as_internal().edges.get_unchecked(idx + 1).as_ptr());
618620
let mut new_root = Root { node: edge, height: internal.height - 1 };
619-
(*new_root.as_mut().as_leaf_mut()).parent = ptr::null();
621+
(*new_root.node_as_mut().as_leaf_mut()).parent = ptr::null();
620622
Some(new_root)
621623
}
622624
};
@@ -648,7 +650,7 @@ impl<'a, K, V> NodeRef<marker::Mut<'a>, K, V, marker::LeafOrInternal> {
648650
);
649651

650652
let mut new_root = Root { node: edge, height: internal.height - 1 };
651-
(*new_root.as_mut().as_leaf_mut()).parent = ptr::null();
653+
(*new_root.node_as_mut().as_leaf_mut()).parent = ptr::null();
652654

653655
for i in 0..old_len {
654656
Handle::new_edge(internal.reborrow_mut(), i).correct_parent_link();
@@ -868,7 +870,7 @@ impl<'a, K, V> Handle<NodeRef<marker::Mut<'a>, K, V, marker::Leaf>, marker::Edge
868870
} else {
869871
unsafe {
870872
Handle::new_edge(
871-
right.as_mut().cast_unchecked::<marker::Leaf>(),
873+
right.node_as_mut().cast_unchecked::<marker::Leaf>(),
872874
self.idx - (B + 1),
873875
)
874876
.insert_fit(key, val)
@@ -943,7 +945,7 @@ impl<'a, K, V> Handle<NodeRef<marker::Mut<'a>, K, V, marker::Internal>, marker::
943945
} else {
944946
unsafe {
945947
Handle::new_edge(
946-
right.as_mut().cast_unchecked::<marker::Internal>(),
948+
right.node_as_mut().cast_unchecked::<marker::Internal>(),
947949
self.idx - (B + 1),
948950
)
949951
.insert_fit(key, val, edge);
@@ -1117,7 +1119,7 @@ impl<'a, K, V> Handle<NodeRef<marker::Mut<'a>, K, V, marker::Internal>, marker::
11171119
let mut new_root = Root { node: BoxedNode::from_internal(new_node), height };
11181120

11191121
for i in 0..(new_len + 1) {
1120-
Handle::new_edge(new_root.as_mut().cast_unchecked(), i).correct_parent_link();
1122+
Handle::new_edge(new_root.node_as_mut().cast_unchecked(), i).correct_parent_link();
11211123
}
11221124

11231125
(self.node, k, v, new_root)

0 commit comments

Comments
 (0)