Skip to content

Commit 59d683c

Browse files
committed
introduce the Comparable trait for btree internals
1 parent 9b643dc commit 59d683c

4 files changed

Lines changed: 47 additions & 61 deletions

File tree

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

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use core::borrow::Borrow;
1+
use core::cmp::Comparable;
22
use core::ops::RangeBounds;
33
use core::{hint, ptr};
44

@@ -267,7 +267,7 @@ impl<BorrowType: marker::BorrowType, K, V> NodeRef<BorrowType, K, V, marker::Lea
267267
) -> LeafRange<BorrowType, K, V>
268268
where
269269
Q: Ord,
270-
K: Borrow<Q>,
270+
K: Comparable<&Q>,
271271
R: RangeBounds<Q>,
272272
{
273273
match self.search_tree_for_bifurcation(&range) {
@@ -316,7 +316,7 @@ impl<'a, K: 'a, V: 'a> NodeRef<marker::Immut<'a>, K, V, marker::LeafOrInternal>
316316
pub(super) fn range_search<Q, R>(self, range: R) -> LeafRange<marker::Immut<'a>, K, V>
317317
where
318318
Q: ?Sized + Ord,
319-
K: Borrow<Q>,
319+
K: Comparable<&Q>,
320320
R: RangeBounds<Q>,
321321
{
322322
// SAFETY: our borrow type is immutable.
@@ -342,7 +342,7 @@ impl<'a, K: 'a, V: 'a> NodeRef<marker::ValMut<'a>, K, V, marker::LeafOrInternal>
342342
pub(super) fn range_search<Q, R>(self, range: R) -> LeafRange<marker::ValMut<'a>, K, V>
343343
where
344344
Q: ?Sized + Ord,
345-
K: Borrow<Q>,
345+
K: Comparable<&Q>,
346346
R: RangeBounds<Q>,
347347
{
348348
unsafe { self.find_leaf_edges_spanning_range(range) }
@@ -741,13 +741,12 @@ impl<BorrowType: marker::BorrowType, K, V>
741741
impl<BorrowType: marker::BorrowType, K, V> NodeRef<BorrowType, K, V, marker::LeafOrInternal> {
742742
/// Returns the leaf edge corresponding to the first point at which the
743743
/// given bound is true.
744-
pub(super) fn lower_bound<Q: ?Sized>(
744+
pub(super) fn lower_bound<Q: Clone>(
745745
self,
746-
mut bound: SearchBound<&Q>,
746+
mut bound: SearchBound<Q>,
747747
) -> Handle<NodeRef<BorrowType, K, V, marker::Leaf>, marker::Edge>
748748
where
749-
Q: Ord,
750-
K: Borrow<Q>,
749+
K: Comparable<Q>,
751750
{
752751
let mut node = self;
753752
loop {
@@ -764,13 +763,12 @@ impl<BorrowType: marker::BorrowType, K, V> NodeRef<BorrowType, K, V, marker::Lea
764763

765764
/// Returns the leaf edge corresponding to the last point at which the
766765
/// given bound is true.
767-
pub(super) fn upper_bound<Q: ?Sized>(
766+
pub(super) fn upper_bound<Q: Clone>(
768767
self,
769-
mut bound: SearchBound<&Q>,
768+
mut bound: SearchBound<Q>,
770769
) -> Handle<NodeRef<BorrowType, K, V, marker::Leaf>, marker::Edge>
771770
where
772-
Q: Ord,
773-
K: Borrow<Q>,
771+
K: Comparable<Q>,
774772
{
775773
let mut node = self;
776774
loop {

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

Lines changed: 35 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
use core::borrow::Borrow;
2-
use core::cmp::Ordering;
1+
use core::cmp::{Comparable, Ordering};
32
use core::ops::{Bound, RangeBounds};
43

54
use SearchBound::*;
@@ -46,16 +45,15 @@ impl<BorrowType: marker::BorrowType, K, V> NodeRef<BorrowType, K, V, marker::Lea
4645
///
4746
/// The result is meaningful only if the tree is ordered by key, like the tree
4847
/// in a `BTreeMap` is.
49-
pub(super) fn search_tree<Q: ?Sized>(
48+
pub(super) fn search_tree<Q: Clone>(
5049
mut self,
51-
key: &Q,
50+
key: Q,
5251
) -> SearchResult<BorrowType, K, V, marker::LeafOrInternal, marker::Leaf>
5352
where
54-
Q: Ord,
55-
K: Borrow<Q>,
53+
K: Comparable<Q>,
5654
{
5755
loop {
58-
self = match self.search_node(key) {
56+
self = match self.search_node(key.clone()) {
5957
Found(handle) => return Found(handle),
6058
GoDown(handle) => match handle.force() {
6159
Leaf(leaf) => return GoDown(leaf),
@@ -95,7 +93,7 @@ impl<BorrowType: marker::BorrowType, K, V> NodeRef<BorrowType, K, V, marker::Lea
9593
>
9694
where
9795
Q: Ord,
98-
K: Borrow<Q>,
96+
K: Comparable<&Q>,
9997
R: RangeBounds<Q>,
10098
{
10199
// Determine if map or set is being searched
@@ -156,27 +154,25 @@ impl<BorrowType: marker::BorrowType, K, V> NodeRef<BorrowType, K, V, marker::Lea
156154
/// the matching child node, if `self` is an internal node.
157155
///
158156
/// The result is meaningful only if the tree is ordered by key.
159-
pub(super) fn find_lower_bound_edge<'r, Q>(
157+
pub(super) fn find_lower_bound_edge<Q: Clone>(
160158
self,
161-
bound: SearchBound<&'r Q>,
162-
) -> (Handle<Self, marker::Edge>, SearchBound<&'r Q>)
159+
bound: SearchBound<Q>,
160+
) -> (Handle<Self, marker::Edge>, SearchBound<Q>)
163161
where
164-
Q: ?Sized + Ord,
165-
K: Borrow<Q>,
162+
K: Comparable<Q>,
166163
{
167164
let (edge_idx, bound) = self.find_lower_bound_index(bound);
168165
let edge = unsafe { Handle::new_edge(self, edge_idx) };
169166
(edge, bound)
170167
}
171168

172169
/// Clone of `find_lower_bound_edge` for the upper bound.
173-
pub(super) fn find_upper_bound_edge<'r, Q>(
170+
pub(super) fn find_upper_bound_edge<Q: Clone>(
174171
self,
175-
bound: SearchBound<&'r Q>,
176-
) -> (Handle<Self, marker::Edge>, SearchBound<&'r Q>)
172+
bound: SearchBound<Q>,
173+
) -> (Handle<Self, marker::Edge>, SearchBound<Q>)
177174
where
178-
Q: ?Sized + Ord,
179-
K: Borrow<Q>,
175+
K: Comparable<Q>,
180176
{
181177
let (edge_idx, bound) = unsafe { self.find_upper_bound_index(bound, 0) };
182178
let edge = unsafe { Handle::new_edge(self, edge_idx) };
@@ -192,13 +188,9 @@ impl<BorrowType, K, V, Type> NodeRef<BorrowType, K, V, Type> {
192188
///
193189
/// The result is meaningful only if the tree is ordered by key, like the tree
194190
/// in a `BTreeMap` is.
195-
pub(super) fn search_node<Q: ?Sized>(
196-
self,
197-
key: &Q,
198-
) -> SearchResult<BorrowType, K, V, Type, Type>
191+
pub(super) fn search_node<Q: Clone>(self, key: Q) -> SearchResult<BorrowType, K, V, Type, Type>
199192
where
200-
Q: Ord,
201-
K: Borrow<Q>,
193+
K: Comparable<Q>,
202194
{
203195
match unsafe { self.find_key_index(key, 0) } {
204196
IndexResult::KV(idx) => Found(unsafe { Handle::new_kv(self, idx) }),
@@ -214,19 +206,18 @@ impl<BorrowType, K, V, Type> NodeRef<BorrowType, K, V, Type> {
214206
///
215207
/// # Safety
216208
/// `start_index` must be a valid edge index for the node.
217-
unsafe fn find_key_index<Q: ?Sized>(&self, key: &Q, start_index: usize) -> IndexResult
209+
unsafe fn find_key_index<Q: Clone>(&self, key: Q, start_index: usize) -> IndexResult
218210
where
219-
Q: Ord,
220-
K: Borrow<Q>,
211+
K: Comparable<Q>,
221212
{
222213
let node = self.reborrow();
223214
let keys = node.keys();
224215
debug_assert!(start_index <= keys.len());
225216
for (offset, k) in unsafe { keys.get_unchecked(start_index..) }.iter().enumerate() {
226-
match key.cmp(k.borrow()) {
227-
Ordering::Greater => {}
217+
match k.compare(key.clone()) {
218+
Ordering::Less => {}
228219
Ordering::Equal => return IndexResult::KV(start_index + offset),
229-
Ordering::Less => return IndexResult::Edge(start_index + offset),
220+
Ordering::Greater => return IndexResult::Edge(start_index + offset),
230221
}
231222
}
232223
IndexResult::Edge(keys.len())
@@ -237,22 +228,18 @@ impl<BorrowType, K, V, Type> NodeRef<BorrowType, K, V, Type> {
237228
/// the matching child node, if `self` is an internal node.
238229
///
239230
/// The result is meaningful only if the tree is ordered by key.
240-
fn find_lower_bound_index<'r, Q>(
241-
&self,
242-
bound: SearchBound<&'r Q>,
243-
) -> (usize, SearchBound<&'r Q>)
231+
fn find_lower_bound_index<Q: Clone>(&self, bound: SearchBound<Q>) -> (usize, SearchBound<Q>)
244232
where
245-
Q: ?Sized + Ord,
246-
K: Borrow<Q>,
233+
K: Comparable<Q>,
247234
{
248235
match bound {
249-
Included(key) => match unsafe { self.find_key_index(key, 0) } {
236+
Included(key) => match unsafe { self.find_key_index(key.clone(), 0) } {
250237
IndexResult::KV(idx) => (idx, AllExcluded),
251-
IndexResult::Edge(idx) => (idx, bound),
238+
IndexResult::Edge(idx) => (idx, Included(key)),
252239
},
253-
Excluded(key) => match unsafe { self.find_key_index(key, 0) } {
240+
Excluded(key) => match unsafe { self.find_key_index(key.clone(), 0) } {
254241
IndexResult::KV(idx) => (idx + 1, AllIncluded),
255-
IndexResult::Edge(idx) => (idx, bound),
242+
IndexResult::Edge(idx) => (idx, Excluded(key)),
256243
},
257244
AllIncluded => (0, AllIncluded),
258245
AllExcluded => (self.len(), AllExcluded),
@@ -264,23 +251,22 @@ impl<BorrowType, K, V, Type> NodeRef<BorrowType, K, V, Type> {
264251
///
265252
/// # Safety
266253
/// `start_index` must be a valid edge index for the node.
267-
unsafe fn find_upper_bound_index<'r, Q>(
254+
unsafe fn find_upper_bound_index<Q: Clone>(
268255
&self,
269-
bound: SearchBound<&'r Q>,
256+
bound: SearchBound<Q>,
270257
start_index: usize,
271-
) -> (usize, SearchBound<&'r Q>)
258+
) -> (usize, SearchBound<Q>)
272259
where
273-
Q: ?Sized + Ord,
274-
K: Borrow<Q>,
260+
K: Comparable<Q>,
275261
{
276262
match bound {
277-
Included(key) => match unsafe { self.find_key_index(key, start_index) } {
263+
Included(key) => match unsafe { self.find_key_index(key.clone(), start_index) } {
278264
IndexResult::KV(idx) => (idx + 1, AllExcluded),
279-
IndexResult::Edge(idx) => (idx, bound),
265+
IndexResult::Edge(idx) => (idx, Included(key)),
280266
},
281-
Excluded(key) => match unsafe { self.find_key_index(key, start_index) } {
267+
Excluded(key) => match unsafe { self.find_key_index(key.clone(), start_index) } {
282268
IndexResult::KV(idx) => (idx, AllIncluded),
283-
IndexResult::Edge(idx) => (idx, bound),
269+
IndexResult::Edge(idx) => (idx, Excluded(key)),
284270
},
285271
AllIncluded => (self.len(), AllIncluded),
286272
AllExcluded => (start_index, AllExcluded),

library/alloc/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@
9898
#![feature(char_internals)]
9999
#![feature(clone_to_uninit)]
100100
#![feature(coerce_unsized)]
101+
#![feature(comparable_trait)]
101102
#![feature(const_clone)]
102103
#![feature(const_cmp)]
103104
#![feature(const_convert)]

library/alloctests/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#![feature(allocator_api)]
1818
#![feature(array_into_iter_constructors)]
1919
#![feature(char_internals)]
20+
#![feature(comparable_trait)]
2021
#![feature(const_alloc_error)]
2122
#![feature(const_cmp)]
2223
#![feature(const_convert)]

0 commit comments

Comments
 (0)