[−][src]Struct ramp_table::RampTable
Contains a mapping from index (key) to a set of values. Each set of values is stored in order of increasing index (key).
RampTable
is optimized for certain usage patterns. For those patterns, it is an efficient
representation. For other usage patterns, RampTable
may be inefficient or unsuitable.
Common usages are for adjacency lists in graphs, sparse matrices, and in building YACC grammars.
A RampTable
stores its data in two vectors: index
and values
. The index
table contains
offsets into values
. These offsets are stored in non-decreasing order. The numeric difference
between two consecutive entries in values
gives the length of the slice within `values that
corresponds to the items for the lower index.
The usage pattern that RampTable
is designed for is appending a sequence of (key, value)
pairs, where each key is in non-decreasing order. The result is a representation that is
compact (has high data density), has constant-time lookup, and efficiently represents datasets
that have a wide variety of counts of items.
This representation also allows acquiring references to slices that span more than one key.
Terminology:
- key - An integer which identifies an ordered list of values in the table.
- value - One item that is present in the table. Each value is associated with exactly one key.
Note that it is possible to add unowned values to the end of the RampTable
. A well-formed
table will not have any unowned values.
Fields
index: Vec<u32>
contains the index into values[] where each entry starts
values: Vec<T>
Methods
impl<T> RampTable<T>
[src]
pub fn new() -> Self
[src]
Creates a new, empty RampTable
.
pub fn with_capacity(keys_capacity: usize, values_capacity: usize) -> Self
[src]
Creates a new, empty RampTable
, with space preallocated for keys and values.
pub fn is_empty(&self) -> bool
[src]
Returns true
if there are no keys in this RampTable
. Equivalent to self.len() == 0
.
Note that a RampTable
may still contain unassociated values even if is_empty
returns
`true.
pub fn len(&self) -> usize
[src]
The number of keys in this RampTable
. All keys are numbered sequentially.
pub fn push_value(&mut self, value: T)
[src]
Pushes a value onto the end of the RampTable
. The item is not yet associated with any
key. In order to associate all unowned values with the next key, call finish_key
.
pub fn finish_key(&mut self) -> usize
[src]
Adds a new key to the end of the key list, and implicitly associates all unassociated values with that key.
Example:
let mut rt = RampTable::new(); rt.push_value("foo"); rt.push_value("bar"); rt.finish_key(); assert_eq!(rt.entry_values(0), &["foo", "bar"]);
pub fn clear(&mut self)
[src]
Deletes all keys and values from the RampTable
.
pub fn iter(
&self
) -> impl Iterator<Item = &[T]> + '_ + DoubleEndedIterator + ExactSizeIterator
[src]
&self
) -> impl Iterator<Item = &[T]> + '_ + DoubleEndedIterator + ExactSizeIterator
Iterates slices of values, one slice for each key.
Example:
let mut rt = RampTable::new(); rt.push_value("foo"); rt.push_value("bar"); rt.finish_key(); // 0 rt.finish_key(); // 1 rt.push_value("alpha"); rt.push_value("bravo"); rt.finish_key(); // 2 let mut ii = rt.iter(); assert_eq!(ii.next(), Some(&["foo", "bar"][..])); assert_eq!(ii.next(), Some(&[][..])); assert_eq!(ii.next(), Some(&["alpha", "bravo"][..])); assert_eq!(ii.next(), None);
ⓘImportant traits for IterMut<'a, T>pub fn iter_mut(&mut self) -> IterMut<T>
[src]
Iterates mutable slices of values, one slice for each key.
Example:
let mut rt = RampTable::new(); rt.push_value("foo"); rt.push_value("bar"); rt.finish_key(); // 0 rt.iter_mut().next().unwrap()[1] = "BAR"; assert_eq!(rt.entry_values(0), &["foo", "BAR"][..]);
pub fn entry_values_range(&self, index: usize) -> Range<usize>
[src]
pub fn entry_values(&self, index: usize) -> &[T]
[src]
pub fn entry_values_mut(&mut self, index: usize) -> &mut [T]
[src]
pub fn all_values(&self) -> &[T]
[src]
Returns a slice over all values in the table. The returned slice covers values in all keys as well as any unassociated values at the end of the table.
pub fn all_values_mut(&mut self) -> &mut [T]
[src]
Returns a mutable slice over all values in the table. The returned slice covers values in all keys as well as any unassociated values at the end of the table.
pub fn iter_pairs(&self) -> impl Iterator<Item = (usize, &T)>
[src]
Iterates pairs of (key, value)
items. The key
values are guaranteed to be iterated in
non-decreasing order.
pub fn iter_pairs_manual(&self) -> impl Iterator<Item = (usize, &T)>
[src]
pub fn num_values(&self) -> usize
[src]
Returns the total number of values (in all entries) in the table.
pub fn push_entry_copy(&mut self, values: &[T]) where
T: Copy,
[src]
T: Copy,
Pushes a new key-value entry in the table, copying values from a slice.
If the table contained any previously unassociated values, then these
become part of this new entry (and they precede the values passed in
values
).
pub fn push_entry_clone(&mut self, values: &[T]) where
T: Clone,
[src]
T: Clone,
Pushes a new key-value entry in the table, cloning values from a slice.
If the table contained any previously unassociated values, then these
become part of this new entry (and they precede the values passed in
values
).
pub fn push_entry_extend<I: Iterator<Item = T>>(&mut self, values: I)
[src]
Pushes a new key-value entry in the table, by iterating values.
If the table contained any previously unassociated values, then these
become part of this new entry (and they precede the values passed in
values
).
Trait Implementations
impl<T: Clone> Clone for RampTable<T>
[src]
impl<T: Debug> Debug for RampTable<T>
[src]
impl<T> Default for RampTable<T>
[src]
impl<T: Eq> Eq for RampTable<T>
[src]
impl<T> Index<usize> for RampTable<T>
[src]
impl<T> IndexMut<usize> for RampTable<T>
[src]
impl<T: PartialEq> PartialEq<RampTable<T>> for RampTable<T>
[src]
impl<T> StructuralEq for RampTable<T>
[src]
impl<T> StructuralPartialEq for RampTable<T>
[src]
Auto Trait Implementations
impl<T> RefUnwindSafe for RampTable<T> where
T: RefUnwindSafe,
T: RefUnwindSafe,
impl<T> Send for RampTable<T> where
T: Send,
T: Send,
impl<T> Sync for RampTable<T> where
T: Sync,
T: Sync,
impl<T> Unpin for RampTable<T> where
T: Unpin,
T: Unpin,
impl<T> UnwindSafe for RampTable<T> where
T: UnwindSafe,
T: UnwindSafe,
Blanket Implementations
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> From<T> for T
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T> ToOwned for T where
T: Clone,
[src]
T: Clone,
type Owned = T
The resulting type after obtaining ownership.
fn to_owned(&self) -> T
[src]
fn clone_into(&self, target: &mut T)
[src]
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,