Skip to content

Commit 039d6dc

Browse files
authored
Rollup merge of #92706 - umanwizard:btree, r=dtolnay
Clarify explicitly that BTree{Map,Set} are ordered. One of the main reasons one would want to use a BTree{Map,Set} rather than a Hash{Map,Set} is because they maintain their keys in sorted order; but this was never explicitly stated in the top-level docs (it was only indirectly alluded to there, and stated explicitly in the docs for `iter`, `values`, etc.) This PR states the ordering guarantee more prominently.
2 parents c5041f8 + ad6408d commit 039d6dc

File tree

3 files changed

+11
-4
lines changed

3 files changed

+11
-4
lines changed

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

+5-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ pub(super) const MIN_LEN: usize = node::MIN_LEN_AFTER_SPLIT;
3434
// An empty map is represented either by the absence of a root node or by a
3535
// root node that is an empty leaf.
3636

37-
/// A map based on a [B-Tree].
37+
/// An ordered map based on a [B-Tree].
3838
///
3939
/// B-Trees represent a fundamental compromise between cache-efficiency and actually minimizing
4040
/// the amount of work performed in a search. In theory, a binary search tree (BST) is the optimal
@@ -68,6 +68,10 @@ pub(super) const MIN_LEN: usize = node::MIN_LEN_AFTER_SPLIT;
6868
/// incorrect results, aborts, memory leaks, or non-termination) but will not be undefined
6969
/// behavior.
7070
///
71+
/// Iterators obtained from functions such as [`BTreeMap::iter`], [`BTreeMap::values`], or
72+
/// [`BTreeMap::keys`] produce their items in order by key, and take worst-case logarithmic and
73+
/// amortized constant time per item returned.
74+
///
7175
/// [B-Tree]: https://en.wikipedia.org/wiki/B-tree
7276
/// [`Cell`]: core::cell::Cell
7377
/// [`RefCell`]: core::cell::RefCell

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

+4-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use super::Recover;
1515

1616
// FIXME(conventions): implement bounded iterators
1717

18-
/// A set based on a B-Tree.
18+
/// An ordered set based on a B-Tree.
1919
///
2020
/// See [`BTreeMap`]'s documentation for a detailed discussion of this collection's performance
2121
/// benefits and drawbacks.
@@ -27,6 +27,9 @@ use super::Recover;
2727
/// incorrect results, aborts, memory leaks, or non-termination) but will not be undefined
2828
/// behavior.
2929
///
30+
/// Iterators returned by [`BTreeSet::iter`] produce their items in order, and take worst-case
31+
/// logarithmic and amortized constant time per item returned.
32+
///
3033
/// [`Ord`]: core::cmp::Ord
3134
/// [`Cell`]: core::cell::Cell
3235
/// [`RefCell`]: core::cell::RefCell

library/alloc/src/collections/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,15 @@ pub mod vec_deque;
1414
#[cfg(not(no_global_oom_handling))]
1515
#[stable(feature = "rust1", since = "1.0.0")]
1616
pub mod btree_map {
17-
//! A map based on a B-Tree.
17+
//! An ordered map based on a B-Tree.
1818
#[stable(feature = "rust1", since = "1.0.0")]
1919
pub use super::btree::map::*;
2020
}
2121

2222
#[cfg(not(no_global_oom_handling))]
2323
#[stable(feature = "rust1", since = "1.0.0")]
2424
pub mod btree_set {
25-
//! A set based on a B-Tree.
25+
//! An ordered set based on a B-Tree.
2626
#[stable(feature = "rust1", since = "1.0.0")]
2727
pub use super::btree::set::*;
2828
}

0 commit comments

Comments
 (0)