pub struct SortedVec<T, F> { /* fields omitted */ }A Vec wrapper type for orderable elements, providing log(N) lookups.
SortedVec is a Vec whose elements are sorted with respect some comperator
function. This provides some behaviour in between regular vectors and hashmaps, namely:
- a compact memory representation
- fast iteration
- semi-fast lookups:
O(log(N)), compared to a hashmap's O(1) and a vector's O(n)
- slow insertions and deletions:
O(n)
Its main use case is for small lookup tables where inserts and deletions are infrequent.
use sortedvec::SortedVec;
struct A {
val: f64,
key: u32,
}
let mut sv = SortedVec::new(|a: &A| a.key);
sv.insert(A { val: 3.14, key: 0 });
sv.insert(A { val: 0.00, key: 10 });
sv.insert(A { val: 5.00, key: 4 });
assert_eq!(3, sv.len());
assert!(sv.find(&5).is_none());
let search_result = sv.find(&4).unwrap();
assert_eq!(5.00, search_result.val);
Creates a new, empty SortedVec. Does not allocate until elements
are inserted.
Creates a sorted vector from an existing vector, which does
not need to be sorted beforehand, and a comparison function.
Inserts a new value into the vector, maintaining the internal
order invariant. Note that this is an O(n) operation.
Provides access to the internal comperator function.
Tries to find an element in the vector with the given 'key'. It has
logarithmic worst case time complexity. The
elements' keys are computed using the internal comperator function,
which is exposed through the SortedVec::comperator method.
Checks whether there is a value with that key in the vector. This is
done in O(log(n)) time.
Destructs the SortedVec to its inner Vec, which is guaranteed
to be sorted with respect to the comperator function.
Returns the number of elements the vector can hold without
reallocating.
let vec: Vec<i32> = Vec::with_capacity(10);
assert_eq!(vec.capacity(), 10);
Extracts a slice containing the entire vector.
Equivalent to &s[..].
use std::io::{self, Write};
let buffer = vec![1, 2, 3, 5, 8];
io::sink().write(buffer.as_slice()).unwrap();
Returns the number of elements in the vector, also referred to
as its 'length'.
let a = vec![1, 2, 3];
assert_eq!(a.len(), 3);
Returns true if the vector contains no elements.
let mut v = Vec::new();
assert!(v.is_empty());
v.push(1);
assert!(!v.is_empty());
Extends a collection with the contents of an iterator. Read more
type Item = T
The type of the elements being iterated over.
Which kind of iterator are we turning this into?
Feeds this value into the given [Hasher]. Read more
Feeds a slice of this type into the given [Hasher]. Read more
The resulting type after dereferencing.
Immutably borrows from an owned value. Read more
The type of the elements being iterated over.
Which kind of iterator are we turning this into?
🔬 This is a nightly-only experimental API. (try_from)
The type returned in the event of a conversion error.
🔬 This is a nightly-only experimental API. (try_from)
Immutably borrows from an owned value. Read more
🔬 This is a nightly-only experimental API. (get_type_id)
this method will likely be replaced by an associated static
🔬 This is a nightly-only experimental API. (try_from)
The type returned in the event of a conversion error.
🔬 This is a nightly-only experimental API. (try_from)
Mutably borrows from an owned value. Read more