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>
impl<K, V> HashTable<K, V>
Sourcepub fn iter(&self) -> HashTableBorrowedIter<'_, K, V> ⓘ
pub fn iter(&self) -> HashTableBorrowedIter<'_, K, V> ⓘ
Row-wise iterator that borrows the table
Sourcepub fn into_iter_columns(self) -> HashTableOwnedIntoIterColumn<K, V> ⓘ
pub fn into_iter_columns(self) -> HashTableOwnedIntoIterColumn<K, V> ⓘ
Column-wise iterator that takes ownership of the keys and values
Sourcepub fn iter_columns(&self) -> HashTableBorrowedIterColumn<'_, K, V> ⓘ
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>
impl<K, V> HashTable<K, V>
Sourcepub fn columns_len(&self) -> usize
pub fn columns_len(&self) -> usize
Returns the number of columns in this table.
Sourcepub fn with_capacity(columns: usize, rows: usize) -> Self
pub fn with_capacity(columns: usize, rows: usize) -> Self
Create new HashTable with specified amoutn of reserved capacity.
Sourcepub fn get_row(&self, row: usize) -> Option<HashTableRowBorrowed<'_, K, V>>
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.
Sourcepub fn get_row_mut(
&mut self,
row: usize,
) -> Option<HashTableMutableBorrowedRow<'_, K, V>>
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.
Sourcepub fn remove_row(
&mut self,
row: usize,
) -> Option<HashTableRowValueOwned<'_, K, V>>
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.
Sourcepub fn column_keys(&self) -> Keys<'_, K, usize> ⓘ
pub fn column_keys(&self) -> Keys<'_, K, usize> ⓘ
Get the column keys of this table
Source§impl<K, V> HashTable<K, V>
impl<K, V> HashTable<K, V>
Sourcepub fn with_columns(columns: impl IntoIterator<Item = K>) -> Self
pub fn with_columns(columns: impl IntoIterator<Item = K>) -> Self
Create a HashTable from iterator of column keys.
Sourcepub fn with_columns_and_capacity(
columns: impl IntoIterator<Item = K>,
rows: usize,
) -> Self
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.
Sourcepub fn remove_row_hashmap(&mut self, row: usize) -> Option<HashMap<&K, V>>
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.
Sourcepub fn get<Q>(&self, column: &Q, row: usize) -> Option<&V>
pub fn get<Q>(&self, column: &Q, row: usize) -> Option<&V>
Get an element from the table.
Will return None if the column does not exist in the table or row is out of range.
Sourcepub fn get_mut<Q>(&mut self, column: &Q, row: usize) -> Option<&mut V>
pub fn get_mut<Q>(&mut self, column: &Q, row: usize) -> Option<&mut V>
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.
Sourcepub fn get_column<'t, 'k, Q>(
&'t self,
column: &'k Q,
) -> Option<HashTableColumnBorrowed<'t, 'k, Q, V>>
pub fn get_column<'t, 'k, Q>( &'t self, column: &'k Q, ) -> Option<HashTableColumnBorrowed<'t, 'k, Q, V>>
Get a table column.
Will return None if the column does not exist in the table.
Sourcepub fn push_row<I>(&mut self, row: I)where
I: IntoIterator<Item = (K, V)>,
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.
Sourcepub fn push_row_with<F>(&mut self, row_generator: F)
pub fn push_row_with<F>(&mut self, row_generator: F)
Add a row to the table using a generator function that returns the value from the column key.
Sourcepub fn insert_column<I>(&mut self, column: K, values: I)where
I: IntoIterator<Item = V>,
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.
Sourcepub fn insert_column_with<F>(&mut self, column: K, values: F)where
F: FnMut(HashTableRowBorrowed<'_, K, V>) -> V,
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.
Sourcepub fn remove_column<Q>(
&mut self,
column: &Q,
) -> Option<HashTableColumnOwned<K, V>>
pub fn remove_column<Q>( &mut self, column: &Q, ) -> Option<HashTableColumnOwned<K, V>>
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.
Sourcepub fn from_column_iter<I, C>(iter: I) -> Self
pub fn from_column_iter<I, C>(iter: I) -> Self
Construct HashTable from an iterator of columns
Sourcepub fn from_column_keys_and_rows<CKI, RI, R>(columns: CKI, rows: RI) -> Self
pub fn from_column_keys_and_rows<CKI, RI, R>(columns: CKI, rows: RI) -> Self
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<'de, K, V> Deserialize<'de> for HashTable<K, V>
impl<'de, K, V> Deserialize<'de> for HashTable<K, V>
Source§fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>where
D: Deserializer<'de>,
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