@@ -14,7 +14,15 @@ use std::convert::From;
14
14
use std:: mem;
15
15
use std:: ops:: { RangeBounds , Bound , Index , IndexMut } ;
16
16
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 ) ]
18
26
pub struct SortedMap < K : Ord , V > {
19
27
data : Vec < ( K , V ) >
20
28
}
@@ -28,8 +36,11 @@ impl<K: Ord, V> SortedMap<K, V> {
28
36
}
29
37
}
30
38
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.
33
44
#[ inline]
34
45
pub fn from_presorted_elements ( elements : Vec < ( K , V ) > ) -> SortedMap < K , V >
35
46
{
@@ -150,8 +161,12 @@ impl<K: Ord, V> SortedMap<K, V> {
150
161
self . data . iter_mut ( ) . map ( |& mut ( ref mut k, _) | k) . for_each ( f) ;
151
162
}
152
163
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.
155
170
#[ inline]
156
171
pub fn insert_presorted ( & mut self , mut elements : Vec < ( K , V ) > ) {
157
172
if elements. is_empty ( ) {
0 commit comments