@@ -36,13 +36,42 @@ pub struct UniKeyMap<K> {
3636}
3737
3838/// From UniIndex to V
39- #[ derive( Debug , Clone , PartialEq , Eq ) ]
39+ #[ derive( Debug , Clone , Eq ) ]
4040pub struct UniValMap < V > {
4141 /// The mapping data. Thanks to Vec we get both fast accesses, and
4242 /// a memory-optimal representation if there are few deletions.
4343 data : Vec < Option < V > > ,
4444}
4545
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+
4675impl < V > Default for UniValMap < V > {
4776 fn default ( ) -> Self {
4877 Self { data : Vec :: default ( ) }
0 commit comments