Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LinearMap: add LinearMapView, similar to VecView on top of #486 #491

Merged
merged 3 commits into from
Jul 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
- Added std `Entry` methods to indexmap `Entry`.
- Added `StringView`, the `!Sized` version of `String`.
- Added `MpMcQueueView`, the `!Sized` version of `MpMcQueue`.
- Added `LinearMapView`, the `!Sized` version of `LinearMap`.

### Changed

Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ mod deque;
mod histbuf;
mod indexmap;
mod indexset;
mod linear_map;
pub mod linear_map;
mod slice;
pub mod storage;
pub mod string;
Expand Down
61 changes: 48 additions & 13 deletions src/linear_map.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,29 @@
//! A fixed capacity map/dictionary that performs lookups via linear search.
//!
//! Note that as this map doesn't use hashing so most operations are *O*(n) instead of *O*(1).

use core::{borrow::Borrow, fmt, mem, ops, slice};

use crate::Vec;
use crate::{
storage::{OwnedStorage, Storage, ViewStorage},
vec::VecInner,
Vec,
};

/// Base struct for [`LinearMap`] and [`LinearMapView`]
pub struct LinearMapInner<K, V, S: Storage> {
pub(crate) buffer: VecInner<(K, V), S>,
}

/// A fixed capacity map/dictionary that performs lookups via linear search.
///
/// Note that as this map doesn't use hashing so most operations are *O*(n) instead of *O*(1).
pub type LinearMap<K, V, const N: usize> = LinearMapInner<K, V, OwnedStorage<N>>;

pub struct LinearMap<K, V, const N: usize> {
pub(crate) buffer: Vec<(K, V), N>,
}
/// A dynamic capacity map/dictionary that performs lookups via linear search.
///
/// Note that as this map doesn't use hashing so most operations are *O*(n) instead of *O*(1).
pub type LinearMapView<K, V> = LinearMapInner<K, V, ViewStorage>;

impl<K, V, const N: usize> LinearMap<K, V, N> {
/// Creates an empty `LinearMap`.
Expand All @@ -27,9 +42,19 @@ impl<K, V, const N: usize> LinearMap<K, V, N> {
pub const fn new() -> Self {
Self { buffer: Vec::new() }
}

/// Get a reference to the `LinearMap`, erasing the `N` const-generic.
pub fn as_view(&self) -> &LinearMapView<K, V> {
self
}

/// Get a mutable reference to the `LinearMap`, erasing the `N` const-generic.
pub fn as_mut_view(&mut self) -> &mut LinearMapView<K, V> {
self
}
}

impl<K, V, const N: usize> LinearMap<K, V, N>
impl<K, V, S: Storage> LinearMapInner<K, V, S>
where
K: Eq,
{
Expand All @@ -46,7 +71,7 @@ where
/// assert_eq!(map.capacity(), 8);
/// ```
pub fn capacity(&self) -> usize {
N
self.buffer.borrow().capacity()
}

/// Clears the map, removing all key-value pairs.
Expand Down Expand Up @@ -346,7 +371,7 @@ where
}
}

impl<'a, K, V, Q, const N: usize> ops::Index<&'a Q> for LinearMap<K, V, N>
impl<'a, K, V, Q, S: Storage> ops::Index<&'a Q> for LinearMapInner<K, V, S>
where
K: Borrow<Q> + Eq,
Q: Eq + ?Sized,
Expand All @@ -358,7 +383,7 @@ where
}
}

impl<'a, K, V, Q, const N: usize> ops::IndexMut<&'a Q> for LinearMap<K, V, N>
impl<'a, K, V, Q, S: Storage> ops::IndexMut<&'a Q> for LinearMapInner<K, V, S>
where
K: Borrow<Q> + Eq,
Q: Eq + ?Sized,
Expand Down Expand Up @@ -389,7 +414,7 @@ where
}
}

impl<K, V, const N: usize> fmt::Debug for LinearMap<K, V, N>
impl<K, V, S: Storage> fmt::Debug for LinearMapInner<K, V, S>
where
K: Eq + fmt::Debug,
V: fmt::Debug,
Expand All @@ -413,6 +438,9 @@ where
}
}

/// An iterator that moves out of a [`LinearMap`].
///
/// This struct is created by calling the [`into_iter`](LinearMap::into_iter) method on [`LinearMap`].
pub struct IntoIter<K, V, const N: usize>
where
K: Eq,
Expand Down Expand Up @@ -444,7 +472,7 @@ where
}
}

impl<'a, K, V, const N: usize> IntoIterator for &'a LinearMap<K, V, N>
impl<'a, K, V, S: Storage> IntoIterator for &'a LinearMapInner<K, V, S>
where
K: Eq,
{
Expand All @@ -456,6 +484,9 @@ where
}
}

/// An iterator over the items of a [`LinearMap`]
///
/// This struct is created by calling the [`iter`](LinearMap::iter) method on [`LinearMap`].
#[derive(Clone, Debug)]
pub struct Iter<'a, K, V> {
iter: slice::Iter<'a, (K, V)>,
Expand All @@ -472,6 +503,9 @@ impl<'a, K, V> Iterator for Iter<'a, K, V> {
}
}

/// An iterator over the items of a [`LinearMap`] that allows modifying the items
///
/// This struct is created by calling the [`iter_mut`](LinearMap::iter_mut) method on [`LinearMap`].
#[derive(Debug)]
pub struct IterMut<'a, K, V> {
iter: slice::IterMut<'a, (K, V)>,
Expand All @@ -485,20 +519,21 @@ impl<'a, K, V> Iterator for IterMut<'a, K, V> {
}
}

impl<K, V, const N: usize, const N2: usize> PartialEq<LinearMap<K, V, N2>> for LinearMap<K, V, N>
impl<K, V, S1: Storage, S2: Storage> PartialEq<LinearMapInner<K, V, S2>>
for LinearMapInner<K, V, S1>
where
K: Eq,
V: PartialEq,
{
fn eq(&self, other: &LinearMap<K, V, N2>) -> bool {
fn eq(&self, other: &LinearMapInner<K, V, S2>) -> bool {
self.len() == other.len()
&& self
.iter()
.all(|(key, value)| other.get(key).map_or(false, |v| *value == *v))
}
}

impl<K, V, const N: usize> Eq for LinearMap<K, V, N>
impl<K, V, S: Storage> Eq for LinearMapInner<K, V, S>
where
K: Eq,
V: PartialEq,
Expand Down
6 changes: 3 additions & 3 deletions src/ser.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use core::hash::{BuildHasher, Hash};

use crate::{
binary_heap::Kind as BinaryHeapKind, storage::Storage, string::StringInner, vec::VecInner,
BinaryHeap, Deque, HistoryBuffer, IndexMap, IndexSet, LinearMap,
binary_heap::Kind as BinaryHeapKind, linear_map::LinearMapInner, storage::Storage,
string::StringInner, vec::VecInner, BinaryHeap, Deque, HistoryBuffer, IndexMap, IndexSet,
};
use serde::ser::{Serialize, SerializeMap, SerializeSeq, Serializer};

Expand Down Expand Up @@ -110,7 +110,7 @@ where
}
}

impl<K, V, const N: usize> Serialize for LinearMap<K, V, N>
impl<K, V, S: Storage> Serialize for LinearMapInner<K, V, S>
where
K: Eq + Serialize,
V: Serialize,
Expand Down
Loading