1
- use super :: either_iter:: EitherIter ;
2
1
use crate :: fx:: FxHashMap ;
3
2
use arrayvec:: ArrayVec ;
3
+ use itertools:: Either ;
4
4
use std:: fmt;
5
5
use std:: hash:: Hash ;
6
6
use std:: ops:: Index ;
7
7
8
- // For pointer-sized arguments arrays
9
- // are faster than set/map for up to 64
10
- // arguments.
11
- //
12
- // On the other hand such a big array
13
- // hurts cache performance, makes passing
14
- // sso structures around very expensive.
15
- //
16
- // Biggest performance benefit is gained
17
- // for reasonably small arrays that stay
18
- // small in vast majority of cases.
19
- //
20
- // '8' is chosen as a sane default, to be
21
- // reevaluated later.
8
+ /// For pointer-sized arguments arrays
9
+ /// are faster than set/map for up to 64
10
+ /// arguments.
11
+ ///
12
+ /// On the other hand such a big array
13
+ /// hurts cache performance, makes passing
14
+ /// sso structures around very expensive.
15
+ ///
16
+ /// Biggest performance benefit is gained
17
+ /// for reasonably small arrays that stay
18
+ /// small in vast majority of cases.
19
+ ///
20
+ /// '8' is chosen as a sane default, to be
21
+ /// reevaluated later.
22
22
const SSO_ARRAY_SIZE : usize = 8 ;
23
23
24
24
/// Small-storage-optimized implementation of a map.
@@ -138,35 +138,35 @@ impl<K, V> SsoHashMap<K, V> {
138
138
/// The iterator element type is `&'a K`.
139
139
pub fn keys ( & self ) -> impl Iterator < Item = & ' _ K > {
140
140
match self {
141
- SsoHashMap :: Array ( array) => EitherIter :: Left ( array. iter ( ) . map ( |( k, _v) | k) ) ,
142
- SsoHashMap :: Map ( map) => EitherIter :: Right ( map. keys ( ) ) ,
141
+ SsoHashMap :: Array ( array) => Either :: Left ( array. iter ( ) . map ( |( k, _v) | k) ) ,
142
+ SsoHashMap :: Map ( map) => Either :: Right ( map. keys ( ) ) ,
143
143
}
144
144
}
145
145
146
146
/// An iterator visiting all values in arbitrary order.
147
147
/// The iterator element type is `&'a V`.
148
148
pub fn values ( & self ) -> impl Iterator < Item = & ' _ V > {
149
149
match self {
150
- SsoHashMap :: Array ( array) => EitherIter :: Left ( array. iter ( ) . map ( |( _k, v) | v) ) ,
151
- SsoHashMap :: Map ( map) => EitherIter :: Right ( map. values ( ) ) ,
150
+ SsoHashMap :: Array ( array) => Either :: Left ( array. iter ( ) . map ( |( _k, v) | v) ) ,
151
+ SsoHashMap :: Map ( map) => Either :: Right ( map. values ( ) ) ,
152
152
}
153
153
}
154
154
155
155
/// An iterator visiting all values mutably in arbitrary order.
156
156
/// The iterator element type is `&'a mut V`.
157
157
pub fn values_mut ( & mut self ) -> impl Iterator < Item = & ' _ mut V > {
158
158
match self {
159
- SsoHashMap :: Array ( array) => EitherIter :: Left ( array. iter_mut ( ) . map ( |( _k, v) | v) ) ,
160
- SsoHashMap :: Map ( map) => EitherIter :: Right ( map. values_mut ( ) ) ,
159
+ SsoHashMap :: Array ( array) => Either :: Left ( array. iter_mut ( ) . map ( |( _k, v) | v) ) ,
160
+ SsoHashMap :: Map ( map) => Either :: Right ( map. values_mut ( ) ) ,
161
161
}
162
162
}
163
163
164
164
/// Clears the map, returning all key-value pairs as an iterator. Keeps the
165
165
/// allocated memory for reuse.
166
166
pub fn drain ( & mut self ) -> impl Iterator < Item = ( K , V ) > + ' _ {
167
167
match self {
168
- SsoHashMap :: Array ( array) => EitherIter :: Left ( array. drain ( ..) ) ,
169
- SsoHashMap :: Map ( map) => EitherIter :: Right ( map. drain ( ) ) ,
168
+ SsoHashMap :: Array ( array) => Either :: Left ( array. drain ( ..) ) ,
169
+ SsoHashMap :: Map ( map) => Either :: Right ( map. drain ( ) ) ,
170
170
}
171
171
}
172
172
}
@@ -406,16 +406,16 @@ where
406
406
}
407
407
408
408
impl < K , V > IntoIterator for SsoHashMap < K , V > {
409
- type IntoIter = EitherIter <
410
- <ArrayVec < ( K , V ) , 8 > as IntoIterator >:: IntoIter ,
409
+ type IntoIter = Either <
410
+ <ArrayVec < ( K , V ) , SSO_ARRAY_SIZE > as IntoIterator >:: IntoIter ,
411
411
<FxHashMap < K , V > as IntoIterator >:: IntoIter ,
412
412
> ;
413
413
type Item = <Self :: IntoIter as Iterator >:: Item ;
414
414
415
415
fn into_iter ( self ) -> Self :: IntoIter {
416
416
match self {
417
- SsoHashMap :: Array ( array) => EitherIter :: Left ( array. into_iter ( ) ) ,
418
- SsoHashMap :: Map ( map) => EitherIter :: Right ( map. into_iter ( ) ) ,
417
+ SsoHashMap :: Array ( array) => Either :: Left ( array. into_iter ( ) ) ,
418
+ SsoHashMap :: Map ( map) => Either :: Right ( map. into_iter ( ) ) ,
419
419
}
420
420
}
421
421
}
@@ -435,9 +435,9 @@ fn adapt_array_mut_it<K, V>(pair: &mut (K, V)) -> (&K, &mut V) {
435
435
}
436
436
437
437
impl < ' a , K , V > IntoIterator for & ' a SsoHashMap < K , V > {
438
- type IntoIter = EitherIter <
438
+ type IntoIter = Either <
439
439
std:: iter:: Map <
440
- <& ' a ArrayVec < ( K , V ) , 8 > as IntoIterator >:: IntoIter ,
440
+ <& ' a ArrayVec < ( K , V ) , SSO_ARRAY_SIZE > as IntoIterator >:: IntoIter ,
441
441
fn ( & ' a ( K , V ) ) -> ( & ' a K , & ' a V ) ,
442
442
> ,
443
443
<& ' a FxHashMap < K , V > as IntoIterator >:: IntoIter ,
@@ -446,16 +446,16 @@ impl<'a, K, V> IntoIterator for &'a SsoHashMap<K, V> {
446
446
447
447
fn into_iter ( self ) -> Self :: IntoIter {
448
448
match self {
449
- SsoHashMap :: Array ( array) => EitherIter :: Left ( array. into_iter ( ) . map ( adapt_array_ref_it) ) ,
450
- SsoHashMap :: Map ( map) => EitherIter :: Right ( map. iter ( ) ) ,
449
+ SsoHashMap :: Array ( array) => Either :: Left ( array. into_iter ( ) . map ( adapt_array_ref_it) ) ,
450
+ SsoHashMap :: Map ( map) => Either :: Right ( map. iter ( ) ) ,
451
451
}
452
452
}
453
453
}
454
454
455
455
impl < ' a , K , V > IntoIterator for & ' a mut SsoHashMap < K , V > {
456
- type IntoIter = EitherIter <
456
+ type IntoIter = Either <
457
457
std:: iter:: Map <
458
- <& ' a mut ArrayVec < ( K , V ) , 8 > as IntoIterator >:: IntoIter ,
458
+ <& ' a mut ArrayVec < ( K , V ) , SSO_ARRAY_SIZE > as IntoIterator >:: IntoIter ,
459
459
fn ( & ' a mut ( K , V ) ) -> ( & ' a K , & ' a mut V ) ,
460
460
> ,
461
461
<& ' a mut FxHashMap < K , V > as IntoIterator >:: IntoIter ,
@@ -464,8 +464,8 @@ impl<'a, K, V> IntoIterator for &'a mut SsoHashMap<K, V> {
464
464
465
465
fn into_iter ( self ) -> Self :: IntoIter {
466
466
match self {
467
- SsoHashMap :: Array ( array) => EitherIter :: Left ( array. into_iter ( ) . map ( adapt_array_mut_it) ) ,
468
- SsoHashMap :: Map ( map) => EitherIter :: Right ( map. iter_mut ( ) ) ,
467
+ SsoHashMap :: Array ( array) => Either :: Left ( array. into_iter ( ) . map ( adapt_array_mut_it) ) ,
468
+ SsoHashMap :: Map ( map) => Either :: Right ( map. iter_mut ( ) ) ,
469
469
}
470
470
}
471
471
}
0 commit comments