SubSlice

Struct SubSlice 

Source
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>

Source

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());
Source

pub fn iter(&self) -> Iter<'_, K, V>

An immutable iterator over all the elements in this slice in sorted-by-key order.

Source

pub fn to_vec(&self) -> Vec<(K, V)>
where K: Clone, V: Clone,

Creates an owned copy of this SubSlice as a Vec. If you wish to preserve PrefixArray semantics consider using ToOwned instead.

Source

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"));
}
Source

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;
}
Source

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.

Source

pub fn contains_key(&self, key: &str) -> bool

Returns whether this SubSlice contains the given key

This operation is O(log n).

§Examples
let arr = PrefixArray::from_iter([("1234", "abcde")]);

assert!(arr.contains_key("1234"));
Source

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).

Source

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).

Source

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).

Source

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.

Source

pub const fn is_empty(&self) -> bool

Returns whether this SubSlice is empty

§Examples
let arr = PrefixArray::<&str, ()>::new();

assert!(arr.is_empty());
Source

pub const fn len(&self) -> usize

Returns the length of this SubSlice

Trait Implementations§

Source§

impl<K: Borrow<str>, V> Borrow<SubSlice<K, V>> for PrefixArray<K, V>

Source§

fn borrow(&self) -> &SubSlice<K, V>

Immutably borrows from an owned value. Read more
Source§

impl<K: Borrow<str>, V> BorrowMut<SubSlice<K, V>> for PrefixArray<K, V>

Source§

fn borrow_mut(&mut self) -> &mut SubSlice<K, V>

Mutably borrows from an owned value. Read more
Source§

impl<K: Borrow<str> + Debug, V: Debug> Debug for SubSlice<K, V>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'a, K: Borrow<str>, V> IntoIterator for &'a SubSlice<K, V>

Source§

type Item = (&'a K, &'a V)

The type of the elements being iterated over.
Source§

type IntoIter = Iter<'a, K, V>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl<'a, K: Borrow<str>, V> IntoIterator for &'a mut SubSlice<K, V>

Source§

type Item = (&'a K, &'a mut V)

The type of the elements being iterated over.
Source§

type IntoIter = IterMut<'a, K, V>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl<K: PartialEq + Borrow<str>, V: PartialEq> PartialEq for SubSlice<K, V>

Source§

fn eq(&self, other: &SubSlice<K, V>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<K: Borrow<str> + Clone, V: Clone> ToOwned for SubSlice<K, V>

Source§

type Owned = PrefixArray<K, V>

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> PrefixArray<K, V>

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut PrefixArray<K, V>)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<K: Eq + Borrow<str>, V: Eq> Eq for SubSlice<K, V>

Source§

impl<K: Borrow<str>, V> StructuralPartialEq for SubSlice<K, V>

Auto Trait Implementations§

§

impl<K, V> Freeze for SubSlice<K, V>
where K: Freeze, V: Freeze,

§

impl<K, V> RefUnwindSafe for SubSlice<K, V>

§

impl<K, V> Send for SubSlice<K, V>
where K: Send, V: Send,

§

impl<K, V> !Sized for SubSlice<K, V>

§

impl<K, V> Sync for SubSlice<K, V>
where K: Sync, V: Sync,

§

impl<K, V> Unpin for SubSlice<K, V>
where K: Unpin, V: Unpin,

§

impl<K, V> UnwindSafe for SubSlice<K, V>
where K: UnwindSafe, V: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more