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+
15use 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
1328impl < 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 >
3358where
3459 K : Eq ,
3560{
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 >
350375where
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 >
362387where
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 >
393418where
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`].
416444pub struct IntoIter < K , V , const N : usize >
417445where
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 >
448476where
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 ) ]
460491pub 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 ) ]
476510pub 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 >
489524where
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 >
502537where
503538 K : Eq ,
504539 V : PartialEq ,
0 commit comments