pub struct SubSlice<K: Borrow<str>, V>(/* private fields */);Expand description
A SubSlice of a PrefixArray in which all items contain a common prefix (which may be the unit prefix "").
The SubSlice does not store what that common prefix is for performance reasons, it is up to the user to keep track of.
Implementations§
Source§impl<K: Borrow<str>, V> SubSlice<K, V>
impl<K: Borrow<str>, V> SubSlice<K, V>
Sourcepub fn from_mut_slice(
data: &mut [(K, V)],
) -> Result<&mut Self, DuplicatesPresent<'_>>
pub fn from_mut_slice( data: &mut [(K, V)], ) -> Result<&mut Self, DuplicatesPresent<'_>>
Makes a mutable ref to a SubSlice from a raw data slice. Sorts data internally.
This operation is O(n log n).
§Errors
This will return DuplicatesPresent if any duplicate keys are present in the slice.
§Examples
// Find items with prefix "a" using a stack allocated array
let mut data = [("abcde", 0), ("foo", 1), ("baz", 2)];
let subslice = SubSlice::from_mut_slice(&mut data).unwrap();
assert_eq!(subslice.find_all_with_prefix("a").to_vec(), &[("abcde", 0)]);// Duplicates will raise an error
let mut data = [("a", 5), ("b", 2), ("a", 6)];
assert!(SubSlice::from_mut_slice(&mut data).is_err());Sourcepub fn iter(&self) -> Iter<'_, K, V> ⓘ
pub fn iter(&self) -> Iter<'_, K, V> ⓘ
An immutable iterator over all the elements in this slice in sorted-by-key order.
Sourcepub fn to_vec(&self) -> Vec<(K, V)>
pub fn to_vec(&self) -> Vec<(K, V)>
Creates an owned copy of this SubSlice as a Vec.
If you wish to preserve PrefixArray semantics consider using ToOwned instead.
Sourcepub fn find_all_with_prefix<'a>(&'a self, prefix: &str) -> &'a Self
pub fn find_all_with_prefix<'a>(&'a self, prefix: &str) -> &'a Self
Returns the SubSlice where all K have the same prefix prefix.
Will return an empty array if there are no matches.
This operation is O(log n)
§Examples
let arr: PrefixArray<&str, u8>;
let slice = arr.find_all_with_prefix("a");
/* do something with items starting with a */
// instead of searching `arr` again, we can narrow what we
// already searched and stored in `slice` for efficiency
for (ab, _) in slice.find_all_with_prefix("ab") {
assert!(ab.starts_with("ab"));
}Sourcepub fn find_all_with_prefix_mut<'a>(&'a mut self, prefix: &str) -> &'a mut Self
pub fn find_all_with_prefix_mut<'a>(&'a mut self, prefix: &str) -> &'a mut Self
Returns a mutable SubSlice where all K have the same prefix prefix.
Will return an empty array if there are no matches.
This operation is O(log n)
§Examples
let mut arr: PrefixArray<&str, u8>;
for (_, v) in arr.find_all_with_prefix_mut("ab") {
*v += 1;
}Sourcepub fn common_prefix(&self) -> &str
pub fn common_prefix(&self) -> &str
Compute the common prefix of this SubSlice from the data.
Will return an empty string if this subslice is empty.
Note that this may be more specific than what was searched for, i/e:
let arr = PrefixArray::from_iter([("abcde", 0), ("abcdef", 1)]);
// Common prefix is *computed*, so even though we only
// searched for "a" we got something more specific
assert_eq!(arr.find_all_with_prefix("a").common_prefix(), "abcde");This operation is O(1), but it is not computationally free.
Sourcepub fn contains_key(&self, key: &str) -> bool
pub fn contains_key(&self, key: &str) -> bool
Sourcepub fn get(&self, key: &str) -> Option<&V>
pub fn get(&self, key: &str) -> Option<&V>
Gets an immutable ref to the value behind the given key if it exists
This operation is O(log n).
Sourcepub fn get_mut(&mut self, key: &str) -> Option<&mut V>
pub fn get_mut(&mut self, key: &str) -> Option<&mut V>
Gets a mutable ref to the value behind the given key if it exists.
This operation is O(log n).
Sourcepub fn get_key_value(&self, key: &str) -> Option<(&K, &V)>
pub fn get_key_value(&self, key: &str) -> Option<(&K, &V)>
Returns the key value pair corresponding to the given key.
This operation is O(log n).
Sourcepub fn iter_mut(&mut self) -> IterMut<'_, K, V> ⓘ
pub fn iter_mut(&mut self) -> IterMut<'_, K, V> ⓘ
An iterator visiting all key value pairs in sorted-by-key order, with mutable references to the values.