rustc_data_structures/sso/
map.rs
1use std::fmt;
2use std::hash::Hash;
3use std::ops::Index;
4
5use arrayvec::ArrayVec;
6use either::Either;
7
8use crate::fx::FxHashMap;
9
10const SSO_ARRAY_SIZE: usize = 8;
25
26#[derive(Clone)]
70pub enum SsoHashMap<K, V> {
71 Array(ArrayVec<(K, V), SSO_ARRAY_SIZE>),
72 Map(FxHashMap<K, V>),
73}
74
75impl<K, V> SsoHashMap<K, V> {
76 #[inline]
78 pub fn new() -> Self {
79 SsoHashMap::Array(ArrayVec::new())
80 }
81
82 pub fn with_capacity(cap: usize) -> Self {
84 if cap <= SSO_ARRAY_SIZE {
85 Self::new()
86 } else {
87 SsoHashMap::Map(FxHashMap::with_capacity_and_hasher(cap, Default::default()))
88 }
89 }
90
91 pub fn clear(&mut self) {
94 match self {
95 SsoHashMap::Array(array) => array.clear(),
96 SsoHashMap::Map(map) => map.clear(),
97 }
98 }
99
100 pub fn capacity(&self) -> usize {
102 match self {
103 SsoHashMap::Array(_) => SSO_ARRAY_SIZE,
104 SsoHashMap::Map(map) => map.capacity(),
105 }
106 }
107
108 pub fn len(&self) -> usize {
110 match self {
111 SsoHashMap::Array(array) => array.len(),
112 SsoHashMap::Map(map) => map.len(),
113 }
114 }
115
116 pub fn is_empty(&self) -> bool {
118 match self {
119 SsoHashMap::Array(array) => array.is_empty(),
120 SsoHashMap::Map(map) => map.is_empty(),
121 }
122 }
123
124 #[inline]
127 pub fn iter(&self) -> <&Self as IntoIterator>::IntoIter {
128 self.into_iter()
129 }
130
131 #[inline]
135 pub fn iter_mut(&mut self) -> impl Iterator<Item = (&'_ K, &'_ mut V)> {
136 self.into_iter()
137 }
138
139 pub fn keys(&self) -> impl Iterator<Item = &'_ K> {
142 match self {
143 SsoHashMap::Array(array) => Either::Left(array.iter().map(|(k, _v)| k)),
144 SsoHashMap::Map(map) => Either::Right(map.keys()),
145 }
146 }
147
148 pub fn values(&self) -> impl Iterator<Item = &'_ V> {
151 match self {
152 SsoHashMap::Array(array) => Either::Left(array.iter().map(|(_k, v)| v)),
153 SsoHashMap::Map(map) => Either::Right(map.values()),
154 }
155 }
156
157 pub fn values_mut(&mut self) -> impl Iterator<Item = &'_ mut V> {
160 match self {
161 SsoHashMap::Array(array) => Either::Left(array.iter_mut().map(|(_k, v)| v)),
162 SsoHashMap::Map(map) => Either::Right(map.values_mut()),
163 }
164 }
165
166 pub fn drain(&mut self) -> impl Iterator<Item = (K, V)> + '_ {
169 match self {
170 SsoHashMap::Array(array) => Either::Left(array.drain(..)),
171 SsoHashMap::Map(map) => Either::Right(map.drain()),
172 }
173 }
174}
175
176impl<K: Eq + Hash, V> SsoHashMap<K, V> {
177 fn migrate_if_full(&mut self) {
180 if let SsoHashMap::Array(array) = self {
181 if array.is_full() {
182 *self = SsoHashMap::Map(array.drain(..).collect());
183 }
184 }
185 }
186
187 pub fn reserve(&mut self, additional: usize) {
191 match self {
192 SsoHashMap::Array(array) => {
193 if SSO_ARRAY_SIZE < (array.len() + additional) {
194 let mut map: FxHashMap<K, V> = array.drain(..).collect();
195 map.reserve(additional);
196 *self = SsoHashMap::Map(map);
197 }
198 }
199 SsoHashMap::Map(map) => map.reserve(additional),
200 }
201 }
202
203 pub fn shrink_to_fit(&mut self) {
207 if let SsoHashMap::Map(map) = self {
208 if map.len() <= SSO_ARRAY_SIZE {
209 *self = SsoHashMap::Array(map.drain().collect());
210 } else {
211 map.shrink_to_fit();
212 }
213 }
214 }
215
216 pub fn retain<F>(&mut self, mut f: F)
218 where
219 F: FnMut(&K, &mut V) -> bool,
220 {
221 match self {
222 SsoHashMap::Array(array) => array.retain(|(k, v)| f(k, v)),
223 SsoHashMap::Map(map) => map.retain(f),
224 }
225 }
226
227 pub fn insert(&mut self, key: K, value: V) -> Option<V> {
236 match self {
237 SsoHashMap::Array(array) => {
238 for (k, v) in array.iter_mut() {
239 if *k == key {
240 let old_value = std::mem::replace(v, value);
241 return Some(old_value);
242 }
243 }
244 if let Err(error) = array.try_push((key, value)) {
245 let mut map: FxHashMap<K, V> = array.drain(..).collect();
246 let (key, value) = error.element();
247 map.insert(key, value);
248 *self = SsoHashMap::Map(map);
249 }
250 None
251 }
252 SsoHashMap::Map(map) => map.insert(key, value),
253 }
254 }
255
256 pub fn remove(&mut self, key: &K) -> Option<V> {
259 match self {
260 SsoHashMap::Array(array) => {
261 array.iter().position(|(k, _v)| k == key).map(|index| array.swap_remove(index).1)
262 }
263
264 SsoHashMap::Map(map) => map.remove(key),
265 }
266 }
267
268 pub fn remove_entry(&mut self, key: &K) -> Option<(K, V)> {
271 match self {
272 SsoHashMap::Array(array) => {
273 array.iter().position(|(k, _v)| k == key).map(|index| array.swap_remove(index))
274 }
275 SsoHashMap::Map(map) => map.remove_entry(key),
276 }
277 }
278
279 pub fn get(&self, key: &K) -> Option<&V> {
281 match self {
282 SsoHashMap::Array(array) => {
283 for (k, v) in array {
284 if k == key {
285 return Some(v);
286 }
287 }
288 None
289 }
290 SsoHashMap::Map(map) => map.get(key),
291 }
292 }
293
294 pub fn get_mut(&mut self, key: &K) -> Option<&mut V> {
296 match self {
297 SsoHashMap::Array(array) => {
298 for (k, v) in array {
299 if k == key {
300 return Some(v);
301 }
302 }
303 None
304 }
305 SsoHashMap::Map(map) => map.get_mut(key),
306 }
307 }
308
309 pub fn get_key_value(&self, key: &K) -> Option<(&K, &V)> {
311 match self {
312 SsoHashMap::Array(array) => {
313 for (k, v) in array {
314 if k == key {
315 return Some((k, v));
316 }
317 }
318 None
319 }
320 SsoHashMap::Map(map) => map.get_key_value(key),
321 }
322 }
323
324 pub fn contains_key(&self, key: &K) -> bool {
326 match self {
327 SsoHashMap::Array(array) => array.iter().any(|(k, _v)| k == key),
328 SsoHashMap::Map(map) => map.contains_key(key),
329 }
330 }
331
332 #[inline]
334 pub fn entry(&mut self, key: K) -> Entry<'_, K, V> {
335 Entry { ssomap: self, key }
336 }
337}
338
339impl<K, V> Default for SsoHashMap<K, V> {
340 #[inline]
341 fn default() -> Self {
342 Self::new()
343 }
344}
345
346impl<K: Eq + Hash, V> FromIterator<(K, V)> for SsoHashMap<K, V> {
347 fn from_iter<I: IntoIterator<Item = (K, V)>>(iter: I) -> SsoHashMap<K, V> {
348 let mut map: SsoHashMap<K, V> = Default::default();
349 map.extend(iter);
350 map
351 }
352}
353
354impl<K: Eq + Hash, V> Extend<(K, V)> for SsoHashMap<K, V> {
355 fn extend<I>(&mut self, iter: I)
356 where
357 I: IntoIterator<Item = (K, V)>,
358 {
359 for (key, value) in iter.into_iter() {
360 self.insert(key, value);
361 }
362 }
363
364 #[inline]
365 fn extend_one(&mut self, (k, v): (K, V)) {
366 self.insert(k, v);
367 }
368
369 fn extend_reserve(&mut self, additional: usize) {
370 match self {
371 SsoHashMap::Array(array) => {
372 if SSO_ARRAY_SIZE < (array.len() + additional) {
373 let mut map: FxHashMap<K, V> = array.drain(..).collect();
374 map.extend_reserve(additional);
375 *self = SsoHashMap::Map(map);
376 }
377 }
378 SsoHashMap::Map(map) => map.extend_reserve(additional),
379 }
380 }
381}
382
383impl<'a, K, V> Extend<(&'a K, &'a V)> for SsoHashMap<K, V>
384where
385 K: Eq + Hash + Copy,
386 V: Copy,
387{
388 fn extend<T: IntoIterator<Item = (&'a K, &'a V)>>(&mut self, iter: T) {
389 self.extend(iter.into_iter().map(|(k, v)| (*k, *v)))
390 }
391
392 #[inline]
393 fn extend_one(&mut self, (&k, &v): (&'a K, &'a V)) {
394 self.insert(k, v);
395 }
396
397 #[inline]
398 fn extend_reserve(&mut self, additional: usize) {
399 Extend::<(K, V)>::extend_reserve(self, additional)
400 }
401}
402
403impl<K, V> IntoIterator for SsoHashMap<K, V> {
404 type IntoIter = Either<
405 <ArrayVec<(K, V), SSO_ARRAY_SIZE> as IntoIterator>::IntoIter,
406 <FxHashMap<K, V> as IntoIterator>::IntoIter,
407 >;
408 type Item = <Self::IntoIter as Iterator>::Item;
409
410 fn into_iter(self) -> Self::IntoIter {
411 match self {
412 SsoHashMap::Array(array) => Either::Left(array.into_iter()),
413 SsoHashMap::Map(map) => Either::Right(map.into_iter()),
414 }
415 }
416}
417
418#[inline(always)]
420fn adapt_array_ref_it<K, V>(pair: &(K, V)) -> (&K, &V) {
421 let (a, b) = pair;
422 (a, b)
423}
424
425#[inline(always)]
427fn adapt_array_mut_it<K, V>(pair: &mut (K, V)) -> (&K, &mut V) {
428 let (a, b) = pair;
429 (a, b)
430}
431
432impl<'a, K, V> IntoIterator for &'a SsoHashMap<K, V> {
433 type IntoIter = Either<
434 std::iter::Map<
435 <&'a ArrayVec<(K, V), SSO_ARRAY_SIZE> as IntoIterator>::IntoIter,
436 fn(&'a (K, V)) -> (&'a K, &'a V),
437 >,
438 <&'a FxHashMap<K, V> as IntoIterator>::IntoIter,
439 >;
440 type Item = <Self::IntoIter as Iterator>::Item;
441
442 fn into_iter(self) -> Self::IntoIter {
443 match self {
444 SsoHashMap::Array(array) => Either::Left(array.into_iter().map(adapt_array_ref_it)),
445 SsoHashMap::Map(map) => Either::Right(map.iter()),
446 }
447 }
448}
449
450impl<'a, K, V> IntoIterator for &'a mut SsoHashMap<K, V> {
451 type IntoIter = Either<
452 std::iter::Map<
453 <&'a mut ArrayVec<(K, V), SSO_ARRAY_SIZE> as IntoIterator>::IntoIter,
454 fn(&'a mut (K, V)) -> (&'a K, &'a mut V),
455 >,
456 <&'a mut FxHashMap<K, V> as IntoIterator>::IntoIter,
457 >;
458 type Item = <Self::IntoIter as Iterator>::Item;
459
460 fn into_iter(self) -> Self::IntoIter {
461 match self {
462 SsoHashMap::Array(array) => Either::Left(array.into_iter().map(adapt_array_mut_it)),
463 SsoHashMap::Map(map) => Either::Right(map.iter_mut()),
464 }
465 }
466}
467
468impl<K, V> fmt::Debug for SsoHashMap<K, V>
469where
470 K: fmt::Debug,
471 V: fmt::Debug,
472{
473 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
474 f.debug_map().entries(self.iter()).finish()
475 }
476}
477
478impl<'a, K, V> Index<&'a K> for SsoHashMap<K, V>
479where
480 K: Eq + Hash,
481{
482 type Output = V;
483
484 #[inline]
485 fn index(&self, key: &K) -> &V {
486 self.get(key).expect("no entry found for key")
487 }
488}
489
490pub struct Entry<'a, K, V> {
492 ssomap: &'a mut SsoHashMap<K, V>,
493 key: K,
494}
495
496impl<'a, K: Eq + Hash, V> Entry<'a, K, V> {
497 pub fn and_modify<F>(self, f: F) -> Self
500 where
501 F: FnOnce(&mut V),
502 {
503 if let Some(value) = self.ssomap.get_mut(&self.key) {
504 f(value);
505 }
506 self
507 }
508
509 #[inline]
512 pub fn or_insert(self, value: V) -> &'a mut V {
513 self.or_insert_with(|| value)
514 }
515
516 pub fn or_insert_with<F: FnOnce() -> V>(self, default: F) -> &'a mut V {
519 self.ssomap.migrate_if_full();
520 match self.ssomap {
521 SsoHashMap::Array(array) => {
522 let key_ref = &self.key;
523 let found_index = array.iter().position(|(k, _v)| k == key_ref);
524 let index = if let Some(index) = found_index {
525 index
526 } else {
527 let index = array.len();
528 array.try_push((self.key, default())).unwrap();
529 index
530 };
531 &mut array[index].1
532 }
533 SsoHashMap::Map(map) => map.entry(self.key).or_insert_with(default),
534 }
535 }
536
537 #[inline]
539 pub fn key(&self) -> &K {
540 &self.key
541 }
542}
543
544impl<'a, K: Eq + Hash, V: Default> Entry<'a, K, V> {
545 #[inline]
548 pub fn or_default(self) -> &'a mut V {
549 self.or_insert_with(Default::default)
550 }
551}