Struct HashTable

Source
pub struct HashTable<K, V> { /* private fields */ }
Expand description

This data structure represents a 2-dimensional grid of values. Each element is indexed by a hashable key and a row index. It’s also possible to access a whole row or column of the table.

§Example


let timestamps = HashTable::from_column_keys_and_rows(
    ["hour", "minute", "second"],
    [
        [7, 15, 13],
        [8, 30, 32],
        [9, 45, 16]
    ]
);

for row in timestamps {
    let hour = *row.get("hour").unwrap();
    let minute = *row.get("minute").unwrap();
    let second = *row.get("second").unwrap();
    assert!(
        (hour == 7 && minute == 15 && second == 13) ||
        (hour == 8 && minute == 30 && second == 32) ||
        (hour == 9 && minute == 45 && second == 16)
    );
}

Implementations§

Source§

impl<K, V> HashTable<K, V>

Source

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

Row-wise iterator that borrows the table

Source

pub fn into_iter_columns(self) -> HashTableOwnedIntoIterColumn<K, V>

Column-wise iterator that takes ownership of the keys and values

Source

pub fn iter_columns(&self) -> HashTableBorrowedIterColumn<'_, K, V>

Column-wise iterator that borrows the values from the table

Source§

impl<K, V> HashTable<K, V>

Source

pub fn columns_len(&self) -> usize

Returns the number of columns in this table.

Source

pub fn rows_len(&self) -> usize

Returns the number of rows in this table.

Source

pub fn with_capacity(columns: usize, rows: usize) -> Self

Create new HashTable with specified amoutn of reserved capacity.

Source

pub fn get_row(&self, row: usize) -> Option<HashTableRowBorrowed<'_, K, V>>

Get a row of the table.

Returns None if row is bigger than or equal to the number of rows.

Source

pub fn get_row_mut( &mut self, row: usize, ) -> Option<HashTableMutableBorrowedRow<'_, K, V>>

Get row with mutable access.

Returns None if row is bigger than or equal to the number of row.

Source

pub fn remove_row( &mut self, row: usize, ) -> Option<HashTableRowValueOwned<'_, K, V>>

Remove a row and take ownership of its values.

This still borrows the hashtable immutably to allow getting the values by a key. Keys can be converted to an owned variant, usually by cloning them.

Source

pub fn column_keys(&self) -> Keys<'_, K, usize>

Get the column keys of this table

Source§

impl<K, V> HashTable<K, V>
where K: Hash + Eq,

Source

pub fn with_columns(columns: impl IntoIterator<Item = K>) -> Self

Create a HashTable from iterator of column keys.

Source

pub fn with_columns_and_capacity( columns: impl IntoIterator<Item = K>, rows: usize, ) -> Self

Create a HashTable from iterator of column keys and with allocated capacity for at least the specified amount of rows.

Source

pub fn remove_row_hashmap(&mut self, row: usize) -> Option<HashMap<&K, V>>

Remove row from the hashtable, taking ownership of teh values. Returns a HashMap

The difference from Self::remove_row is that this only allocates a hashmap that references the keys instead of allocating values and taking a reference to a hashmap of indices used internally.

Source

pub fn get<Q>(&self, column: &Q, row: usize) -> Option<&V>
where K: Borrow<Q>, Q: Hash + Eq + ?Sized,

Get an element from the table.

Will return None if the column does not exist in the table or row is out of range.

Source

pub fn get_mut<Q>(&mut self, column: &Q, row: usize) -> Option<&mut V>
where K: Borrow<Q>, Q: Hash + Eq + ?Sized,

Get an element from the table with mutable access.

Will return None if the column does not exist in the table or row is out of range.

Source

pub fn get_column<'t, 'k, Q>( &'t self, column: &'k Q, ) -> Option<HashTableColumnBorrowed<'t, 'k, Q, V>>
where K: Borrow<Q>, Q: Hash + Eq,

Get a table column.

Will return None if the column does not exist in the table.

Source

pub fn push_row<I>(&mut self, row: I)
where I: IntoIterator<Item = (K, V)>,

Add a row to the table from an iterator of key-value pairs.

Source

pub fn push_row_with<F>(&mut self, row_generator: F)
where F: FnMut(&K) -> V,

Add a row to the table using a generator function that returns the value from the column key.

Source

pub fn insert_column<I>(&mut self, column: K, values: I)
where I: IntoIterator<Item = V>,

Add a column with values provided through an iterator.

The iterator must have the same amount of elements as there are currently rows in the table.

This function has O(n^2) complexity, where n is the number of rows. It is recommended to insert the needed columns beforehand.

Source

pub fn insert_column_with<F>(&mut self, column: K, values: F)
where F: FnMut(HashTableRowBorrowed<'_, K, V>) -> V,

Add a column using a generator function that returns a value based on the values of the row.

This function has O(n^2) complexity, where n is the number of rows. It is recommended to insert the needed columns beforehand.

Source

pub fn remove_column<Q>( &mut self, column: &Q, ) -> Option<HashTableColumnOwned<K, V>>
where K: Borrow<Q>, Q: Hash + Eq + ?Sized,

Remove a column from the table and take ownership of the key and values.

Will return None if the column does not exist in the table.

Source

pub fn from_column_iter<I, C>(iter: I) -> Self
where I: IntoIterator<Item = C>, C: Into<HashTableColumnOwned<K, V>>,

Construct HashTable from an iterator of columns

Source

pub fn from_column_keys_and_rows<CKI, RI, R>(columns: CKI, rows: RI) -> Self
where CKI: IntoIterator<Item = K>, RI: IntoIterator<Item = R>, R: IntoIterator<Item = V>,

Construct a HashTable from an iterator of column keys and an iterator of rows of values

The only check of row lengths performed is whether there is a remainder from division of the total amount of values (sum of row lengths) by the amount of columns. If there is a nonzero remainder, these values get dropped.

This means that with 2 columns teh row iterator can have two rows with lengths 1 and 3 and the first element of the second row will be moved to teh end of the first row.

Trait Implementations§

Source§

impl<K: Clone, V: Clone> Clone for HashTable<K, V>

Source§

fn clone(&self) -> HashTable<K, V>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<K: Debug, V: Debug> Debug for HashTable<K, V>

Source§

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

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

impl<K, V> Default for HashTable<K, V>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl<'de, K, V> Deserialize<'de> for HashTable<K, V>
where K: Hash + Eq + Deserialize<'de>, V: Deserialize<'de>,

Source§

fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where D: Deserializer<'de>,

Deserializes a HashTable from sequence of key-value maps

Will fall back to deserialize_hashtable_from_map if deserializer decides to provide a map

Source§

impl<K, V> From<HashTableFromColumns<K, V>> for HashTable<K, V>

Source§

fn from(value: HashTableFromColumns<K, V>) -> Self

Converts to this type from the input type.
Source§

impl<K, V, R> FromIterator<R> for HashTable<K, V>
where K: Hash + Eq, R: IntoIterator<Item = (K, V)>,

Source§

fn from_iter<T: IntoIterator<Item = R>>(iter: T) -> Self

Creates a value from an iterator. Read more
Source§

impl<K, V, Q> Index<(&Q, usize)> for HashTable<K, V>
where K: Hash + Eq + Borrow<Q>, Q: Hash + Eq,

Source§

type Output = V

The returned type after indexing.
Source§

fn index(&self, index: (&Q, usize)) -> &Self::Output

Performs the indexing (container[index]) operation. Read more
Source§

impl<K, V, Q> IndexMut<(&Q, usize)> for HashTable<K, V>
where K: Hash + Eq + Borrow<Q>, Q: Hash + Eq,

Source§

fn index_mut(&mut self, index: (&Q, usize)) -> &mut Self::Output

Performs the mutable indexing (container[index]) operation. Read more
Source§

impl<'t, K, V> IntoIterator for &'t HashTable<K, V>

Source§

type Item = HashTableRowBorrowed<'t, K, V>

The type of the elements being iterated over.
Source§

type IntoIter = HashTableBorrowedIter<'t, 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, V> IntoIterator for HashTable<K, V>
where K: Clone + Hash + Eq,

Source§

fn into_iter(self) -> Self::IntoIter

Row-wise iterator that takes ownership of both keys and values, cloning the keys each time and allocating a new hashmap.

Source§

type Item = HashMap<K, V>

The type of the elements being iterated over.
Source§

type IntoIter = HashTableIntoIter<K, V>

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

impl<K, V> Serialize for HashTable<K, V>
where K: Serialize, V: Serialize,

Source§

fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where S: Serializer,

Serialize this value into the given Serde serializer. Read more

Auto Trait Implementations§

§

impl<K, V> Freeze for HashTable<K, V>

§

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

§

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

§

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

§

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

§

impl<K, V> UnwindSafe for HashTable<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
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

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

fn clone_into(&self, target: &mut T)

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

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,