mod array;
mod vec;
pub use array::map::ArrayMap;
#[cfg(feature = "macros")]
pub use linear_collections_macros::{array_map, vec_map, vec_set};
pub use vec::map::VecMap;
pub use vec::set::VecSet;
#[cfg(feature = "serde")]
mod serde;
mod test;
pub(crate) trait AsMutSlice<K: Eq, V: Sized + PartialEq> {
fn as_mut_slice(&mut self) -> &mut [(K, V)];
}
#[allow(private_bounds)]
pub trait LinearMap<K: Eq, V: Sized + PartialEq>: AsMutSlice<K, V> {
type Backing;
fn as_slice(&self) -> &[(K, V)];
fn into_inner(self) -> Self::Backing;
fn contains_key(&self, key: &K) -> bool {
for (k, _) in self.as_slice().iter() {
if k == key {
return true;
}
}
false
}
fn contains_value(&self, value: &V) -> bool {
for (_, v) in self.as_slice().iter() {
if v == value {
return true;
}
}
false
}
fn get<'a>(&'a self, key: &'a K) -> Option<&'a V> {
self.as_slice()
.iter()
.find(|(k, _)| k == key)
.map(|(_, v)| v)
}
fn get_mut<'a>(&'a mut self, key: &'a K) -> Option<&'a mut V> {
self.as_mut_slice()
.iter_mut()
.find(|(k, _)| k == key)
.map(|(_, v)| v)
}
fn nth_value<'a>(&'a self, index: usize) -> Option<&'a V>
where
K: 'a,
{
self.as_slice().get(index).map(|(_, v)| v)
}
fn nth_value_mut<'a>(&'a mut self, index: usize) -> Option<&'a mut V>
where
K: 'a,
{
self.as_mut_slice().get_mut(index).map(|(_, v)| v)
}
fn nth_key<'a>(&'a self, index: usize) -> Option<&'a K>
where
V: 'a,
{
self.as_slice().get(index).map(|(k, _)| k)
}
fn nth_key_mut<'a>(&'a mut self, index: usize) -> Option<&'a mut K>
where
V: 'a,
{
self.as_mut_slice().get_mut(index).map(|(k, _)| k)
}
fn replace(&mut self, key: &K, value: V) {
self.as_mut_slice()
.iter_mut()
.find(|(k, _)| k == key)
.map(|(_, v)| *v = value);
}
fn merge_from_iter<'a>(&'a mut self, iter: impl Iterator<Item = &'a (K, V)>)
where
K: 'a,
V: 'a + Clone,
{
iter.for_each(|(k, v)| self.replace(&k, v.clone().to_owned()))
}
}