diff --git a/CHANGELOG.md b/CHANGELOG.md index b5e600c1c1..21e7f6aad4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/src/lib.rs b/src/lib.rs index 5bf5fa57c8..46ff4c5db7 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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; diff --git a/src/linear_map.rs b/src/linear_map.rs index 2e376a521d..48242f7416 100644 --- a/src/linear_map.rs +++ b/src/linear_map.rs @@ -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 { + 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 = LinearMapInner>; -pub struct LinearMap { - 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 = LinearMapInner; impl LinearMap { /// Creates an empty `LinearMap`. @@ -27,9 +42,19 @@ impl LinearMap { 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 { + self + } + + /// Get a mutable reference to the `LinearMap`, erasing the `N` const-generic. + pub fn as_mut_view(&mut self) -> &mut LinearMapView { + self + } } -impl LinearMap +impl LinearMapInner where K: Eq, { @@ -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. @@ -346,7 +371,7 @@ where } } -impl<'a, K, V, Q, const N: usize> ops::Index<&'a Q> for LinearMap +impl<'a, K, V, Q, S: Storage> ops::Index<&'a Q> for LinearMapInner where K: Borrow + Eq, Q: Eq + ?Sized, @@ -358,7 +383,7 @@ where } } -impl<'a, K, V, Q, const N: usize> ops::IndexMut<&'a Q> for LinearMap +impl<'a, K, V, Q, S: Storage> ops::IndexMut<&'a Q> for LinearMapInner where K: Borrow + Eq, Q: Eq + ?Sized, @@ -389,7 +414,7 @@ where } } -impl fmt::Debug for LinearMap +impl fmt::Debug for LinearMapInner where K: Eq + fmt::Debug, V: fmt::Debug, @@ -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 where K: Eq, @@ -444,7 +472,7 @@ where } } -impl<'a, K, V, const N: usize> IntoIterator for &'a LinearMap +impl<'a, K, V, S: Storage> IntoIterator for &'a LinearMapInner where K: Eq, { @@ -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)>, @@ -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)>, @@ -485,12 +519,13 @@ impl<'a, K, V> Iterator for IterMut<'a, K, V> { } } -impl PartialEq> for LinearMap +impl PartialEq> + for LinearMapInner where K: Eq, V: PartialEq, { - fn eq(&self, other: &LinearMap) -> bool { + fn eq(&self, other: &LinearMapInner) -> bool { self.len() == other.len() && self .iter() @@ -498,7 +533,7 @@ where } } -impl Eq for LinearMap +impl Eq for LinearMapInner where K: Eq, V: PartialEq, diff --git a/src/ser.rs b/src/ser.rs index cdd4b6bf4b..52064a1c78 100644 --- a/src/ser.rs +++ b/src/ser.rs @@ -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}; @@ -110,7 +110,7 @@ where } } -impl Serialize for LinearMap +impl Serialize for LinearMapInner where K: Eq + Serialize, V: Serialize,