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);
Splits the collection into two at the given index.
Returns a newly allocated Self
. self
contains elements [0, at)
,
and the returned Self
contains elements [at, len)
.
Note that the capacity of self
does not change.
Panics if at > len
.
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.
use sortedvec::*;
let v = vec!["hello".to_string(), "world".to_string()];
let sv = SortedVec::from_vec(v, |x| &x[1..]);
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.
#Example
use sortedvec::*;
let sv = sortedvec![1, 5, 3, 10];
let res = sv.find(&5).unwrap();
assert_eq!(&5, res);
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.
Removes and returns the element at position index
within the vector,
shifting all elements after it to the left.
Panics if index
is out of bounds.
#[unstable(reason = "indices are opaque for this data structure")]
Shortens the vector, keeping the first len
elements and dropping
the rest.
If len
is greater than the vector's current length, this has no
effect.
Note that this method has no effect on the allocated capacity
of the vector.
Removes all elements but one that resolve to the same key generated
by the internal key function.
Removes the last element from a vector and returns it, or None
if it is empty.
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());
type Item = T
The type of the elements being iterated over.
Which kind of iterator are we turning this into?
Extends a collection with the contents of an iterator. Read more
The resulting type after dereferencing.
Feeds this value into the given [Hasher
]. Read more
Feeds a slice of this type into the given [Hasher
]. Read more
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
Mutably borrows from an owned value. Read more
🔬 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
)