Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit d4e21f5

Browse files
committedNov 13, 2024
btree: simplify the backdoor between set and map
The internal `btree::Recover` trait acted as a private API between `BTreeSet` and `BTreeMap`, but we can use `pub(_)` restrictions these days, and some of the methods don't need special handling anymore. * `BTreeSet::get` can use `BTreeMap::get_key_value` * `BTreeSet::take` can use `BTreeMap::remove_entry` * `BTreeSet::replace` does need help, but this now uses a `pub(super)` method on `BTreeMap` instead of the trait. * `btree::Recover` is now removed.
1 parent 75609d6 commit d4e21f5

File tree

4 files changed

+11
-48
lines changed

4 files changed

+11
-48
lines changed
 

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

+6-34
Original file line numberDiff line numberDiff line change
@@ -289,40 +289,12 @@ impl<K: Clone, V: Clone, A: Allocator + Clone> Clone for BTreeMap<K, V, A> {
289289
}
290290
}
291291

292-
impl<K, Q: ?Sized, A: Allocator + Clone> super::Recover<Q> for BTreeMap<K, SetValZST, A>
293-
where
294-
K: Borrow<Q> + Ord,
295-
Q: Ord,
296-
{
297-
type Key = K;
298-
299-
fn get(&self, key: &Q) -> Option<&K> {
300-
let root_node = self.root.as_ref()?.reborrow();
301-
match root_node.search_tree(key) {
302-
Found(handle) => Some(handle.into_kv().0),
303-
GoDown(_) => None,
304-
}
305-
}
306-
307-
fn take(&mut self, key: &Q) -> Option<K> {
308-
let (map, dormant_map) = DormantMutRef::new(self);
309-
let root_node = map.root.as_mut()?.borrow_mut();
310-
match root_node.search_tree(key) {
311-
Found(handle) => Some(
312-
OccupiedEntry {
313-
handle,
314-
dormant_map,
315-
alloc: (*map.alloc).clone(),
316-
_marker: PhantomData,
317-
}
318-
.remove_kv()
319-
.0,
320-
),
321-
GoDown(_) => None,
322-
}
323-
}
324-
325-
fn replace(&mut self, key: K) -> Option<K> {
292+
/// Internal functionality for `BTreeSet`.
293+
impl<K, A: Allocator + Clone> BTreeMap<K, SetValZST, A> {
294+
pub(super) fn replace(&mut self, key: K) -> Option<K>
295+
where
296+
K: Ord,
297+
{
326298
let (map, dormant_map) = DormantMutRef::new(self);
327299
let root_node =
328300
map.root.get_or_insert_with(|| Root::new((*map.alloc).clone())).borrow_mut();

‎alloc/src/collections/btree/mod.rs

-8
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,3 @@ mod search;
1212
pub mod set;
1313
mod set_val;
1414
mod split;
15-
16-
trait Recover<Q: ?Sized> {
17-
type Key;
18-
19-
fn get(&self, key: &Q) -> Option<&Self::Key>;
20-
fn take(&mut self, key: &Q) -> Option<Self::Key>;
21-
fn replace(&mut self, key: Self::Key) -> Option<Self::Key>;
22-
}

‎alloc/src/collections/btree/set.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ use core::iter::{FusedIterator, Peekable};
77
use core::mem::ManuallyDrop;
88
use core::ops::{BitAnd, BitOr, BitXor, Bound, RangeBounds, Sub};
99

10-
use super::Recover;
1110
use super::map::{BTreeMap, Keys};
1211
use super::merge_iter::MergeIterInner;
1312
use super::set_val::SetValZST;
@@ -635,7 +634,7 @@ impl<T, A: Allocator + Clone> BTreeSet<T, A> {
635634
T: Borrow<Q> + Ord,
636635
Q: Ord,
637636
{
638-
Recover::get(&self.map, value)
637+
self.map.get_key_value(value).map(|(k, _)| k)
639638
}
640639

641640
/// Returns `true` if `self` has no elements in common with `other`.
@@ -926,7 +925,7 @@ impl<T, A: Allocator + Clone> BTreeSet<T, A> {
926925
where
927926
T: Ord,
928927
{
929-
Recover::replace(&mut self.map, value)
928+
self.map.replace(value)
930929
}
931930

932931
/// If the set contains an element equal to the value, removes it from the
@@ -978,7 +977,7 @@ impl<T, A: Allocator + Clone> BTreeSet<T, A> {
978977
T: Borrow<Q> + Ord,
979978
Q: Ord,
980979
{
981-
Recover::take(&mut self.map, value)
980+
self.map.remove_entry(value).map(|(k, _)| k)
982981
}
983982

984983
/// Retains only the elements specified by the predicate.

‎alloc/src/collections/btree/set_val.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@
33
/// * `BTreeMap<T, ()>` (possible user-defined map)
44
/// * `BTreeMap<T, SetValZST>` (internal set representation)
55
#[derive(Debug, Eq, PartialEq, Ord, PartialOrd, Hash, Clone, Default)]
6-
pub struct SetValZST;
6+
pub(super) struct SetValZST;
77

88
/// A trait to differentiate between `BTreeMap` and `BTreeSet` values.
99
/// Returns `true` only for type `SetValZST`, `false` for all other types (blanket implementation).
1010
/// `TypeId` requires a `'static` lifetime, use of this trait avoids that restriction.
1111
///
1212
/// [`TypeId`]: std::any::TypeId
13-
pub trait IsSetVal {
13+
pub(super) trait IsSetVal {
1414
fn is_set_val() -> bool;
1515
}
1616

0 commit comments

Comments
 (0)
Failed to load comments.