@@ -36,13 +36,42 @@ pub struct UniKeyMap<K> {
36
36
}
37
37
38
38
/// From UniIndex to V
39
- #[ derive( Debug , Clone , PartialEq , Eq ) ]
39
+ #[ derive( Debug , Clone , Eq ) ]
40
40
pub struct UniValMap < V > {
41
41
/// The mapping data. Thanks to Vec we get both fast accesses, and
42
42
/// a memory-optimal representation if there are few deletions.
43
43
data : Vec < Option < V > > ,
44
44
}
45
45
46
+ impl < V : PartialEq > UniValMap < V > {
47
+ /// Exact equality of two maps.
48
+ /// Less accurate but faster than `equivalent`, mostly because
49
+ /// of the fast path when the lengths are different.
50
+ pub fn identical ( & self , other : & Self ) -> bool {
51
+ self . data == other. data
52
+ }
53
+
54
+ /// Equality up to trailing `None`s of two maps, i.e.
55
+ /// do they represent the same mapping ?
56
+ pub fn equivalent ( & self , other : & Self ) -> bool {
57
+ let min_len = self . data . len ( ) . min ( other. data . len ( ) ) ;
58
+ self . data [ min_len..] . iter ( ) . all ( Option :: is_none)
59
+ && other. data [ min_len..] . iter ( ) . all ( Option :: is_none)
60
+ && ( self . data [ ..min_len] == other. data [ ..min_len] )
61
+ }
62
+ }
63
+
64
+ impl < V : PartialEq > PartialEq for UniValMap < V > {
65
+ /// 2023-05: We found that using `equivalent` rather than `identical`
66
+ /// in the equality testing of the `RangeMap` is neutral for most
67
+ /// benchmarks, while being quite beneficial for `zip-equal`
68
+ /// and to a lesser extent for `unicode`, `slice-get-unchecked` and
69
+ /// `backtraces` as well.
70
+ fn eq ( & self , other : & Self ) -> bool {
71
+ self . equivalent ( other)
72
+ }
73
+ }
74
+
46
75
impl < V > Default for UniValMap < V > {
47
76
fn default ( ) -> Self {
48
77
Self { data : Vec :: default ( ) }
0 commit comments