Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 18 additions & 31 deletions library/alloc/src/collections/btree/map.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use core::borrow::Borrow;
use core::cmp::Ordering;
use core::cmp::{Comparable, Ordering};
use core::error::Error;
use core::fmt::{self, Debug};
use core::hash::{Hash, Hasher};
Expand Down Expand Up @@ -332,8 +331,7 @@ impl<K, A: Allocator + Clone> BTreeMap<K, SetValZST, A> {

pub(super) fn get_or_insert_with<Q: ?Sized, F>(&mut self, q: &Q, f: F) -> &K
where
K: Borrow<Q> + Ord,
Q: Ord,
K: Comparable<Q> + Ord,
F: FnOnce(&Q) -> K,
{
let (map, dormant_map) = DormantMutRef::new(self);
Expand All @@ -343,7 +341,7 @@ impl<K, A: Allocator + Clone> BTreeMap<K, SetValZST, A> {
Found(handle) => handle.into_kv_mut().0,
GoDown(handle) => {
let key = f(q);
assert!(*key.borrow() == *q, "new value is not equal");
assert!(key.equivalent(q), "new value is not equal");
VacantEntry {
key,
handle: Some(handle),
Expand Down Expand Up @@ -710,8 +708,7 @@ impl<K, V, A: Allocator + Clone> BTreeMap<K, V, A> {
#[stable(feature = "rust1", since = "1.0.0")]
pub fn get<Q: ?Sized>(&self, key: &Q) -> Option<&V>
where
K: Borrow<Q> + Ord,
Q: Ord,
K: Comparable<Q> + Ord,
{
let root_node = self.root.as_ref()?.reborrow();
match root_node.search_tree(key) {
Expand Down Expand Up @@ -776,8 +773,7 @@ impl<K, V, A: Allocator + Clone> BTreeMap<K, V, A> {
#[stable(feature = "map_get_key_value", since = "1.40.0")]
pub fn get_key_value<Q: ?Sized>(&self, k: &Q) -> Option<(&K, &V)>
where
K: Borrow<Q> + Ord,
Q: Ord,
K: Comparable<Q> + Ord,
{
let root_node = self.root.as_ref()?.reborrow();
match root_node.search_tree(k) {
Expand Down Expand Up @@ -972,8 +968,7 @@ impl<K, V, A: Allocator + Clone> BTreeMap<K, V, A> {
#[cfg_attr(not(test), rustc_diagnostic_item = "btreemap_contains_key")]
pub fn contains_key<Q: ?Sized>(&self, key: &Q) -> bool
where
K: Borrow<Q> + Ord,
Q: Ord,
K: Comparable<Q> + Ord,
{
self.get(key).is_some()
}
Expand All @@ -999,8 +994,7 @@ impl<K, V, A: Allocator + Clone> BTreeMap<K, V, A> {
#[stable(feature = "rust1", since = "1.0.0")]
pub fn get_mut<Q: ?Sized>(&mut self, key: &Q) -> Option<&mut V>
where
K: Borrow<Q> + Ord,
Q: Ord,
K: Comparable<Q> + Ord,
{
let root_node = self.root.as_mut()?.borrow_mut();
match root_node.search_tree(key) {
Expand Down Expand Up @@ -1101,8 +1095,7 @@ impl<K, V, A: Allocator + Clone> BTreeMap<K, V, A> {
#[rustc_confusables("delete", "take")]
pub fn remove<Q: ?Sized>(&mut self, key: &Q) -> Option<V>
where
K: Borrow<Q> + Ord,
Q: Ord,
K: Comparable<Q> + Ord,
{
self.remove_entry(key).map(|(_, v)| v)
}
Expand All @@ -1126,8 +1119,7 @@ impl<K, V, A: Allocator + Clone> BTreeMap<K, V, A> {
#[stable(feature = "btreemap_remove_entry", since = "1.45.0")]
pub fn remove_entry<Q: ?Sized>(&mut self, key: &Q) -> Option<(K, V)>
where
K: Borrow<Q> + Ord,
Q: Ord,
K: Comparable<Q> + Ord,
{
let (map, dormant_map) = DormantMutRef::new(self);
let root_node = map.root.as_mut()?.borrow_mut();
Expand Down Expand Up @@ -1260,7 +1252,7 @@ impl<K, V, A: Allocator + Clone> BTreeMap<K, V, A> {
pub fn range<T: ?Sized, R>(&self, range: R) -> Range<'_, K, V>
where
T: Ord,
K: Borrow<T> + Ord,
K: Comparable<T>,
R: RangeBounds<T>,
{
if let Some(root) = &self.root {
Expand Down Expand Up @@ -1300,7 +1292,7 @@ impl<K, V, A: Allocator + Clone> BTreeMap<K, V, A> {
pub fn range_mut<T: ?Sized, R>(&mut self, range: R) -> RangeMut<'_, K, V>
where
T: Ord,
K: Borrow<T> + Ord,
K: Comparable<T>,
R: RangeBounds<T>,
{
if let Some(root) = &mut self.root {
Expand Down Expand Up @@ -1388,9 +1380,9 @@ impl<K, V, A: Allocator + Clone> BTreeMap<K, V, A> {
/// assert_eq!(b[&41], "e");
/// ```
#[stable(feature = "btree_split_off", since = "1.11.0")]
pub fn split_off<Q: ?Sized + Ord>(&mut self, key: &Q) -> Self
pub fn split_off<Q: ?Sized>(&mut self, key: &Q) -> Self
where
K: Borrow<Q> + Ord,
K: Comparable<Q> + Ord,
A: Clone,
{
if self.is_empty() {
Expand Down Expand Up @@ -2440,8 +2432,7 @@ impl<K: Debug, V: Debug, A: Allocator + Clone> Debug for BTreeMap<K, V, A> {
#[stable(feature = "rust1", since = "1.0.0")]
impl<K, Q: ?Sized, V, A: Allocator + Clone> Index<&Q> for BTreeMap<K, V, A>
where
K: Borrow<Q> + Ord,
Q: Ord,
K: Comparable<Q> + Ord,
{
type Output = V;

Expand Down Expand Up @@ -2694,8 +2685,7 @@ impl<K, V, A: Allocator + Clone> BTreeMap<K, V, A> {
#[unstable(feature = "btree_cursors", issue = "107540")]
pub fn lower_bound<Q: ?Sized>(&self, bound: Bound<&Q>) -> Cursor<'_, K, V>
where
K: Borrow<Q> + Ord,
Q: Ord,
K: Comparable<Q> + Ord,
{
let root_node = match self.root.as_ref() {
None => return Cursor { current: None, root: None },
Expand Down Expand Up @@ -2747,8 +2737,7 @@ impl<K, V, A: Allocator + Clone> BTreeMap<K, V, A> {
#[unstable(feature = "btree_cursors", issue = "107540")]
pub fn lower_bound_mut<Q: ?Sized>(&mut self, bound: Bound<&Q>) -> CursorMut<'_, K, V, A>
where
K: Borrow<Q> + Ord,
Q: Ord,
K: Comparable<Q> + Ord,
{
let (root, dormant_root) = DormantMutRef::new(&mut self.root);
let root_node = match root.as_mut() {
Expand Down Expand Up @@ -2817,8 +2806,7 @@ impl<K, V, A: Allocator + Clone> BTreeMap<K, V, A> {
#[unstable(feature = "btree_cursors", issue = "107540")]
pub fn upper_bound<Q: ?Sized>(&self, bound: Bound<&Q>) -> Cursor<'_, K, V>
where
K: Borrow<Q> + Ord,
Q: Ord,
K: Comparable<Q> + Ord,
{
let root_node = match self.root.as_ref() {
None => return Cursor { current: None, root: None },
Expand Down Expand Up @@ -2870,8 +2858,7 @@ impl<K, V, A: Allocator + Clone> BTreeMap<K, V, A> {
#[unstable(feature = "btree_cursors", issue = "107540")]
pub fn upper_bound_mut<Q: ?Sized>(&mut self, bound: Bound<&Q>) -> CursorMut<'_, K, V, A>
where
K: Borrow<Q> + Ord,
Q: Ord,
K: Comparable<Q> + Ord,
{
let (root, dormant_root) = DormantMutRef::new(&mut self.root);
let root_node = match root.as_mut() {
Expand Down
1 change: 1 addition & 0 deletions library/alloc/src/collections/btree/map/tests.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use core::assert_matches::assert_matches;
use std::borrow::Borrow;
use std::iter;
use std::ops::Bound::{Excluded, Included, Unbounded};
use std::panic::{AssertUnwindSafe, catch_unwind};
Expand Down
14 changes: 6 additions & 8 deletions library/alloc/src/collections/btree/navigate.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use core::borrow::Borrow;
use core::cmp::Comparable;
use core::ops::RangeBounds;
use core::{hint, ptr};

Expand Down Expand Up @@ -267,7 +267,7 @@ impl<BorrowType: marker::BorrowType, K, V> NodeRef<BorrowType, K, V, marker::Lea
) -> LeafRange<BorrowType, K, V>
where
Q: Ord,
K: Borrow<Q>,
K: Comparable<Q>,
R: RangeBounds<Q>,
{
match self.search_tree_for_bifurcation(&range) {
Expand Down Expand Up @@ -316,7 +316,7 @@ impl<'a, K: 'a, V: 'a> NodeRef<marker::Immut<'a>, K, V, marker::LeafOrInternal>
pub(super) fn range_search<Q, R>(self, range: R) -> LeafRange<marker::Immut<'a>, K, V>
where
Q: ?Sized + Ord,
K: Borrow<Q>,
K: Comparable<Q>,
R: RangeBounds<Q>,
{
// SAFETY: our borrow type is immutable.
Expand All @@ -342,7 +342,7 @@ impl<'a, K: 'a, V: 'a> NodeRef<marker::ValMut<'a>, K, V, marker::LeafOrInternal>
pub(super) fn range_search<Q, R>(self, range: R) -> LeafRange<marker::ValMut<'a>, K, V>
where
Q: ?Sized + Ord,
K: Borrow<Q>,
K: Comparable<Q>,
R: RangeBounds<Q>,
{
unsafe { self.find_leaf_edges_spanning_range(range) }
Expand Down Expand Up @@ -746,8 +746,7 @@ impl<BorrowType: marker::BorrowType, K, V> NodeRef<BorrowType, K, V, marker::Lea
mut bound: SearchBound<&Q>,
) -> Handle<NodeRef<BorrowType, K, V, marker::Leaf>, marker::Edge>
where
Q: Ord,
K: Borrow<Q>,
K: Comparable<Q>,
{
let mut node = self;
loop {
Expand All @@ -769,8 +768,7 @@ impl<BorrowType: marker::BorrowType, K, V> NodeRef<BorrowType, K, V, marker::Lea
mut bound: SearchBound<&Q>,
) -> Handle<NodeRef<BorrowType, K, V, marker::Leaf>, marker::Edge>
where
Q: Ord,
K: Borrow<Q>,
K: Comparable<Q>,
{
let mut node = self;
loop {
Expand Down
36 changes: 16 additions & 20 deletions library/alloc/src/collections/btree/search.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use core::borrow::Borrow;
use core::cmp::Ordering;
use core::cmp::{Comparable, Ordering};
use core::ops::{Bound, RangeBounds};

use SearchBound::*;
Expand Down Expand Up @@ -51,8 +50,7 @@ impl<BorrowType: marker::BorrowType, K, V> NodeRef<BorrowType, K, V, marker::Lea
key: &Q,
) -> SearchResult<BorrowType, K, V, marker::LeafOrInternal, marker::Leaf>
where
Q: Ord,
K: Borrow<Q>,
K: Comparable<Q>,
{
loop {
self = match self.search_node(key) {
Expand Down Expand Up @@ -95,7 +93,7 @@ impl<BorrowType: marker::BorrowType, K, V> NodeRef<BorrowType, K, V, marker::Lea
>
where
Q: Ord,
K: Borrow<Q>,
K: Comparable<Q>,
R: RangeBounds<Q>,
{
// Determine if map or set is being searched
Expand Down Expand Up @@ -161,8 +159,8 @@ impl<BorrowType: marker::BorrowType, K, V> NodeRef<BorrowType, K, V, marker::Lea
bound: SearchBound<&'r Q>,
) -> (Handle<Self, marker::Edge>, SearchBound<&'r Q>)
where
Q: ?Sized + Ord,
K: Borrow<Q>,
Q: ?Sized,
K: Comparable<Q>,
{
let (edge_idx, bound) = self.find_lower_bound_index(bound);
let edge = unsafe { Handle::new_edge(self, edge_idx) };
Expand All @@ -175,8 +173,8 @@ impl<BorrowType: marker::BorrowType, K, V> NodeRef<BorrowType, K, V, marker::Lea
bound: SearchBound<&'r Q>,
) -> (Handle<Self, marker::Edge>, SearchBound<&'r Q>)
where
Q: ?Sized + Ord,
K: Borrow<Q>,
Q: ?Sized,
K: Comparable<Q>,
{
let (edge_idx, bound) = unsafe { self.find_upper_bound_index(bound, 0) };
let edge = unsafe { Handle::new_edge(self, edge_idx) };
Expand All @@ -197,8 +195,7 @@ impl<BorrowType, K, V, Type> NodeRef<BorrowType, K, V, Type> {
key: &Q,
) -> SearchResult<BorrowType, K, V, Type, Type>
where
Q: Ord,
K: Borrow<Q>,
K: Comparable<Q>,
{
match unsafe { self.find_key_index(key, 0) } {
IndexResult::KV(idx) => Found(unsafe { Handle::new_kv(self, idx) }),
Expand All @@ -216,17 +213,16 @@ impl<BorrowType, K, V, Type> NodeRef<BorrowType, K, V, Type> {
/// `start_index` must be a valid edge index for the node.
unsafe fn find_key_index<Q: ?Sized>(&self, key: &Q, start_index: usize) -> IndexResult
where
Q: Ord,
K: Borrow<Q>,
K: Comparable<Q>,
{
let node = self.reborrow();
let keys = node.keys();
debug_assert!(start_index <= keys.len());
for (offset, k) in unsafe { keys.get_unchecked(start_index..) }.iter().enumerate() {
match key.cmp(k.borrow()) {
Ordering::Greater => {}
match k.compare(key) {
Ordering::Less => {}
Ordering::Equal => return IndexResult::KV(start_index + offset),
Ordering::Less => return IndexResult::Edge(start_index + offset),
Ordering::Greater => return IndexResult::Edge(start_index + offset),
}
}
IndexResult::Edge(keys.len())
Expand All @@ -242,8 +238,8 @@ impl<BorrowType, K, V, Type> NodeRef<BorrowType, K, V, Type> {
bound: SearchBound<&'r Q>,
) -> (usize, SearchBound<&'r Q>)
where
Q: ?Sized + Ord,
K: Borrow<Q>,
Q: ?Sized,
K: Comparable<Q>,
{
match bound {
Included(key) => match unsafe { self.find_key_index(key, 0) } {
Expand All @@ -270,8 +266,8 @@ impl<BorrowType, K, V, Type> NodeRef<BorrowType, K, V, Type> {
start_index: usize,
) -> (usize, SearchBound<&'r Q>)
where
Q: ?Sized + Ord,
K: Borrow<Q>,
Q: ?Sized,
K: Comparable<Q>,
{
match bound {
Included(key) => match unsafe { self.find_key_index(key, start_index) } {
Expand Down
Loading
Loading