Skip to content

Commit 4002864

Browse files
authored
Merge pull request #491 from Nitrokey/linearmap-view-dedup
LinearMap: add LinearMapView, similar to VecView on top of #486
2 parents 55ed7c2 + d4dde75 commit 4002864

File tree

4 files changed

+53
-17
lines changed

4 files changed

+53
-17
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
2727
- Added std `Entry` methods to indexmap `Entry`.
2828
- Added `StringView`, the `!Sized` version of `String`.
2929
- Added `MpMcQueueView`, the `!Sized` version of `MpMcQueue`.
30+
- Added `LinearMapView`, the `!Sized` version of `LinearMap`.
3031

3132
### Changed
3233

src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ mod deque;
107107
mod histbuf;
108108
mod indexmap;
109109
mod indexset;
110-
mod linear_map;
110+
pub mod linear_map;
111111
mod slice;
112112
pub mod storage;
113113
pub mod string;

src/linear_map.rs

Lines changed: 48 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,29 @@
1+
//! A fixed capacity map/dictionary that performs lookups via linear search.
2+
//!
3+
//! Note that as this map doesn't use hashing so most operations are *O*(n) instead of *O*(1).
4+
15
use core::{borrow::Borrow, fmt, mem, ops, slice};
26

3-
use crate::Vec;
7+
use crate::{
8+
storage::{OwnedStorage, Storage, ViewStorage},
9+
vec::VecInner,
10+
Vec,
11+
};
12+
13+
/// Base struct for [`LinearMap`] and [`LinearMapView`]
14+
pub struct LinearMapInner<K, V, S: Storage> {
15+
pub(crate) buffer: VecInner<(K, V), S>,
16+
}
417

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

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

1328
impl<K, V, const N: usize> LinearMap<K, V, N> {
1429
/// Creates an empty `LinearMap`.
@@ -27,9 +42,19 @@ impl<K, V, const N: usize> LinearMap<K, V, N> {
2742
pub const fn new() -> Self {
2843
Self { buffer: Vec::new() }
2944
}
45+
46+
/// Get a reference to the `LinearMap`, erasing the `N` const-generic.
47+
pub fn as_view(&self) -> &LinearMapView<K, V> {
48+
self
49+
}
50+
51+
/// Get a mutable reference to the `LinearMap`, erasing the `N` const-generic.
52+
pub fn as_mut_view(&mut self) -> &mut LinearMapView<K, V> {
53+
self
54+
}
3055
}
3156

32-
impl<K, V, const N: usize> LinearMap<K, V, N>
57+
impl<K, V, S: Storage> LinearMapInner<K, V, S>
3358
where
3459
K: Eq,
3560
{
@@ -46,7 +71,7 @@ where
4671
/// assert_eq!(map.capacity(), 8);
4772
/// ```
4873
pub fn capacity(&self) -> usize {
49-
N
74+
self.buffer.borrow().capacity()
5075
}
5176

5277
/// Clears the map, removing all key-value pairs.
@@ -346,7 +371,7 @@ where
346371
}
347372
}
348373

349-
impl<'a, K, V, Q, const N: usize> ops::Index<&'a Q> for LinearMap<K, V, N>
374+
impl<'a, K, V, Q, S: Storage> ops::Index<&'a Q> for LinearMapInner<K, V, S>
350375
where
351376
K: Borrow<Q> + Eq,
352377
Q: Eq + ?Sized,
@@ -358,7 +383,7 @@ where
358383
}
359384
}
360385

361-
impl<'a, K, V, Q, const N: usize> ops::IndexMut<&'a Q> for LinearMap<K, V, N>
386+
impl<'a, K, V, Q, S: Storage> ops::IndexMut<&'a Q> for LinearMapInner<K, V, S>
362387
where
363388
K: Borrow<Q> + Eq,
364389
Q: Eq + ?Sized,
@@ -389,7 +414,7 @@ where
389414
}
390415
}
391416

392-
impl<K, V, const N: usize> fmt::Debug for LinearMap<K, V, N>
417+
impl<K, V, S: Storage> fmt::Debug for LinearMapInner<K, V, S>
393418
where
394419
K: Eq + fmt::Debug,
395420
V: fmt::Debug,
@@ -413,6 +438,9 @@ where
413438
}
414439
}
415440

441+
/// An iterator that moves out of a [`LinearMap`].
442+
///
443+
/// This struct is created by calling the [`into_iter`](LinearMap::into_iter) method on [`LinearMap`].
416444
pub struct IntoIter<K, V, const N: usize>
417445
where
418446
K: Eq,
@@ -444,7 +472,7 @@ where
444472
}
445473
}
446474

447-
impl<'a, K, V, const N: usize> IntoIterator for &'a LinearMap<K, V, N>
475+
impl<'a, K, V, S: Storage> IntoIterator for &'a LinearMapInner<K, V, S>
448476
where
449477
K: Eq,
450478
{
@@ -456,6 +484,9 @@ where
456484
}
457485
}
458486

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

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

488-
impl<K, V, const N: usize, const N2: usize> PartialEq<LinearMap<K, V, N2>> for LinearMap<K, V, N>
522+
impl<K, V, S1: Storage, S2: Storage> PartialEq<LinearMapInner<K, V, S2>>
523+
for LinearMapInner<K, V, S1>
489524
where
490525
K: Eq,
491526
V: PartialEq,
492527
{
493-
fn eq(&self, other: &LinearMap<K, V, N2>) -> bool {
528+
fn eq(&self, other: &LinearMapInner<K, V, S2>) -> bool {
494529
self.len() == other.len()
495530
&& self
496531
.iter()
497532
.all(|(key, value)| other.get(key).map_or(false, |v| *value == *v))
498533
}
499534
}
500535

501-
impl<K, V, const N: usize> Eq for LinearMap<K, V, N>
536+
impl<K, V, S: Storage> Eq for LinearMapInner<K, V, S>
502537
where
503538
K: Eq,
504539
V: PartialEq,

src/ser.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
use core::hash::{BuildHasher, Hash};
22

33
use crate::{
4-
binary_heap::Kind as BinaryHeapKind, storage::Storage, string::StringInner, vec::VecInner,
5-
BinaryHeap, Deque, HistoryBuffer, IndexMap, IndexSet, LinearMap,
4+
binary_heap::Kind as BinaryHeapKind, linear_map::LinearMapInner, storage::Storage,
5+
string::StringInner, vec::VecInner, BinaryHeap, Deque, HistoryBuffer, IndexMap, IndexSet,
66
};
77
use serde::ser::{Serialize, SerializeMap, SerializeSeq, Serializer};
88

@@ -110,7 +110,7 @@ where
110110
}
111111
}
112112

113-
impl<K, V, const N: usize> Serialize for LinearMap<K, V, N>
113+
impl<K, V, S: Storage> Serialize for LinearMapInner<K, V, S>
114114
where
115115
K: Eq + Serialize,
116116
V: Serialize,

0 commit comments

Comments
 (0)