Skip to content

Commit 95fac99

Browse files
Add some doc comments to SortedMap.
1 parent 4bedc31 commit 95fac99

File tree

1 file changed

+20
-5
lines changed

1 file changed

+20
-5
lines changed

src/librustc_data_structures/sorted_map.rs

+20-5
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,15 @@ use std::convert::From;
1414
use std::mem;
1515
use std::ops::{RangeBounds, Bound, Index, IndexMut};
1616

17-
#[derive(Clone, PartialEq, Eq, Hash, Debug, RustcEncodable, RustcDecodable)]
17+
/// `SortedMap` is a data structure with similar characteristics as BTreeMap but
18+
/// slightly different trade-offs: lookup, inseration, and removal are O(log(N))
19+
/// and elements can be iterated in order cheaply.
20+
///
21+
/// `SortedMap` can be faster than a `BTreeMap` for small sizes (<50) since it
22+
/// stores data in a more compact way. It also supports accessing contiguous
23+
/// ranges of elements as a slice, and slices of already sorted elements can be
24+
/// inserted efficiently.
25+
#[derive(Clone, PartialEq, Eq, Hash, Default, Debug, RustcEncodable, RustcDecodable)]
1826
pub struct SortedMap<K: Ord, V> {
1927
data: Vec<(K,V)>
2028
}
@@ -28,8 +36,11 @@ impl<K: Ord, V> SortedMap<K, V> {
2836
}
2937
}
3038

31-
// It is up to the caller to make sure that the elements are sorted by key
32-
// and that there are no duplicates.
39+
/// Construct a `SortedMap` from a presorted set of elements. This is faster
40+
/// than creating an empty map and then inserting the elements individually.
41+
///
42+
/// It is up to the caller to make sure that the elements are sorted by key
43+
/// and that there are no duplicates.
3344
#[inline]
3445
pub fn from_presorted_elements(elements: Vec<(K, V)>) -> SortedMap<K, V>
3546
{
@@ -150,8 +161,12 @@ impl<K: Ord, V> SortedMap<K, V> {
150161
self.data.iter_mut().map(|&mut (ref mut k, _)| k).for_each(f);
151162
}
152163

153-
// It is up to the caller to make sure that the elements are sorted by key
154-
// and that there are no duplicates.
164+
/// Inserts a presorted range of elements into the map. If the range can be
165+
/// inserted as a whole in between to existing elements of the map, this
166+
/// will be faster than inserting the elements individually.
167+
///
168+
/// It is up to the caller to make sure that the elements are sorted by key
169+
/// and that there are no duplicates.
155170
#[inline]
156171
pub fn insert_presorted(&mut self, mut elements: Vec<(K, V)>) {
157172
if elements.is_empty() {

0 commit comments

Comments
 (0)