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
+
1
5
use core:: { borrow:: Borrow , fmt, mem, ops, slice} ;
2
6
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
+ }
4
17
5
18
/// A fixed capacity map/dictionary that performs lookups via linear search.
6
19
///
7
20
/// 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 > > ;
8
22
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 > ;
12
27
13
28
impl < K , V , const N : usize > LinearMap < K , V , N > {
14
29
/// Creates an empty `LinearMap`.
@@ -27,9 +42,19 @@ impl<K, V, const N: usize> LinearMap<K, V, N> {
27
42
pub const fn new ( ) -> Self {
28
43
Self { buffer : Vec :: new ( ) }
29
44
}
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
+ }
30
55
}
31
56
32
- impl < K , V , const N : usize > LinearMap < K , V , N >
57
+ impl < K , V , S : Storage > LinearMapInner < K , V , S >
33
58
where
34
59
K : Eq ,
35
60
{
46
71
/// assert_eq!(map.capacity(), 8);
47
72
/// ```
48
73
pub fn capacity ( & self ) -> usize {
49
- N
74
+ self . buffer . borrow ( ) . capacity ( )
50
75
}
51
76
52
77
/// Clears the map, removing all key-value pairs.
@@ -346,7 +371,7 @@ where
346
371
}
347
372
}
348
373
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 >
350
375
where
351
376
K : Borrow < Q > + Eq ,
352
377
Q : Eq + ?Sized ,
@@ -358,7 +383,7 @@ where
358
383
}
359
384
}
360
385
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 >
362
387
where
363
388
K : Borrow < Q > + Eq ,
364
389
Q : Eq + ?Sized ,
@@ -389,7 +414,7 @@ where
389
414
}
390
415
}
391
416
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 >
393
418
where
394
419
K : Eq + fmt:: Debug ,
395
420
V : fmt:: Debug ,
@@ -413,6 +438,9 @@ where
413
438
}
414
439
}
415
440
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`].
416
444
pub struct IntoIter < K , V , const N : usize >
417
445
where
418
446
K : Eq ,
@@ -444,7 +472,7 @@ where
444
472
}
445
473
}
446
474
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 >
448
476
where
449
477
K : Eq ,
450
478
{
@@ -456,6 +484,9 @@ where
456
484
}
457
485
}
458
486
487
+ /// An iterator over the items of a [`LinearMap`]
488
+ ///
489
+ /// This struct is created by calling the [`iter`](LinearMap::iter) method on [`LinearMap`].
459
490
#[ derive( Clone , Debug ) ]
460
491
pub struct Iter < ' a , K , V > {
461
492
iter : slice:: Iter < ' a , ( K , V ) > ,
@@ -472,6 +503,9 @@ impl<'a, K, V> Iterator for Iter<'a, K, V> {
472
503
}
473
504
}
474
505
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`].
475
509
#[ derive( Debug ) ]
476
510
pub struct IterMut < ' a , K , V > {
477
511
iter : slice:: IterMut < ' a , ( K , V ) > ,
@@ -485,20 +519,21 @@ impl<'a, K, V> Iterator for IterMut<'a, K, V> {
485
519
}
486
520
}
487
521
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 >
489
524
where
490
525
K : Eq ,
491
526
V : PartialEq ,
492
527
{
493
- fn eq ( & self , other : & LinearMap < K , V , N2 > ) -> bool {
528
+ fn eq ( & self , other : & LinearMapInner < K , V , S2 > ) -> bool {
494
529
self . len ( ) == other. len ( )
495
530
&& self
496
531
. iter ( )
497
532
. all ( |( key, value) | other. get ( key) . map_or ( false , |v| * value == * v) )
498
533
}
499
534
}
500
535
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 >
502
537
where
503
538
K : Eq ,
504
539
V : PartialEq ,
0 commit comments