Skip to content

Commit 7c1ec23

Browse files
authoredNov 7, 2020
Rollup merge of rust-lang#78437 - ssomers:btree_no_ord_at_node_level, r=Mark-Simulacrum
BTreeMap: stop mistaking node for an orderly place A second mistake in rust-lang#77612 was to ignore the node module's rightful comment "this module doesn't care whether the entries are sorted". And there's a much simpler way to visit the keys in order, if you check this separately from a single pass checking everything. r? ``@Mark-Simulacrum``
2 parents e35a8b9 + e099138 commit 7c1ec23

File tree

2 files changed

+22
-41
lines changed

2 files changed

+22
-41
lines changed
 

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

+22-4
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ fn test_all_refs<'a, T: 'a>(dummy: &mut T, iter: impl Iterator<Item = &'a mut T>
4242
}
4343
}
4444

45-
impl<'a, K: 'a, V: 'a> BTreeMap<K, V> {
45+
impl<K, V> BTreeMap<K, V> {
4646
/// Panics if the map (or the code navigating it) is corrupted.
4747
fn check(&self)
4848
where
@@ -54,14 +54,14 @@ impl<'a, K: 'a, V: 'a> BTreeMap<K, V> {
5454
assert!(root_node.ascend().is_err());
5555
root_node.assert_back_pointers();
5656

57-
let counted = root_node.assert_ascending();
58-
assert_eq!(self.length, counted);
5957
assert_eq!(self.length, root_node.calc_length());
6058

6159
root_node.assert_min_len(if root_node.height() > 0 { 1 } else { 0 });
6260
} else {
6361
assert_eq!(self.length, 0);
6462
}
63+
64+
self.assert_ascending();
6565
}
6666

6767
/// Returns the height of the root, if any.
@@ -79,10 +79,28 @@ impl<'a, K: 'a, V: 'a> BTreeMap<K, V> {
7979
String::from("not yet allocated")
8080
}
8181
}
82+
83+
/// Asserts that the keys are in strictly ascending order.
84+
fn assert_ascending(&self)
85+
where
86+
K: Copy + Debug + Ord,
87+
{
88+
let mut num_seen = 0;
89+
let mut keys = self.keys();
90+
if let Some(mut previous) = keys.next() {
91+
num_seen = 1;
92+
for next in keys {
93+
assert!(previous < next, "{:?} >= {:?}", previous, next);
94+
previous = next;
95+
num_seen += 1;
96+
}
97+
}
98+
assert_eq!(num_seen, self.len());
99+
}
82100
}
83101

84102
impl<'a, K: 'a, V: 'a> NodeRef<marker::Immut<'a>, K, V, marker::LeafOrInternal> {
85-
pub fn assert_min_len(self, min_len: usize) {
103+
fn assert_min_len(self, min_len: usize) {
86104
assert!(self.len() >= min_len, "{} < {}", self.len(), min_len);
87105
if let node::ForceResult::Internal(node) = self.force() {
88106
for idx in 0..=node.len() {

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

-37
Original file line numberDiff line numberDiff line change
@@ -17,43 +17,6 @@ impl<'a, K: 'a, V: 'a> NodeRef<marker::Immut<'a>, K, V, marker::LeafOrInternal>
1717
}
1818
}
1919

20-
/// Asserts that the keys are in strictly ascending order.
21-
/// Returns how many keys it encountered.
22-
pub fn assert_ascending(self) -> usize
23-
where
24-
K: Copy + Debug + Ord,
25-
{
26-
struct SeriesChecker<T> {
27-
num_seen: usize,
28-
previous: Option<T>,
29-
}
30-
impl<T: Copy + Debug + Ord> SeriesChecker<T> {
31-
fn is_ascending(&mut self, next: T) {
32-
if let Some(previous) = self.previous {
33-
assert!(previous < next, "{:?} >= {:?}", previous, next);
34-
}
35-
self.previous = Some(next);
36-
self.num_seen += 1;
37-
}
38-
}
39-
40-
let mut checker = SeriesChecker { num_seen: 0, previous: None };
41-
self.visit_nodes_in_order(|pos| match pos {
42-
navigate::Position::Leaf(node) => {
43-
for idx in 0..node.len() {
44-
let key = *unsafe { node.key_at(idx) };
45-
checker.is_ascending(key);
46-
}
47-
}
48-
navigate::Position::InternalKV(kv) => {
49-
let key = *kv.into_kv().0;
50-
checker.is_ascending(key);
51-
}
52-
navigate::Position::Internal(_) => {}
53-
});
54-
checker.num_seen
55-
}
56-
5720
pub fn dump_keys(self) -> String
5821
where
5922
K: Debug,

0 commit comments

Comments
 (0)
Please sign in to comment.