]>
git.proxmox.com Git - rustc.git/blob - compiler/rustc_data_structures/src/mini_map.rs
1 use crate::fx
::FxHashMap
;
2 use arrayvec
::ArrayVec
;
6 /// Small-storage-optimized implementation of a map
7 /// made specifically for caching results.
9 /// Stores elements in a small array up to a certain length
10 /// and switches to `HashMap` when that length is exceeded.
11 pub enum MiniMap
<K
, V
> {
12 Array(ArrayVec
<[(K
, V
); 8]>),
16 impl<K
: Eq
+ Hash
, V
> MiniMap
<K
, V
> {
17 /// Creates an empty `MiniMap`.
18 pub fn new() -> Self {
19 MiniMap
::Array(ArrayVec
::new())
22 /// Inserts or updates value in the map.
23 pub fn insert(&mut self, key
: K
, value
: V
) {
25 MiniMap
::Array(array
) => {
26 for pair
in array
.iter_mut() {
32 if let Err(error
) = array
.try_push((key
, value
)) {
33 let mut map
: FxHashMap
<K
, V
> = array
.drain(..).collect();
34 let (key
, value
) = error
.element();
35 map
.insert(key
, value
);
36 *self = MiniMap
::Map(map
);
39 MiniMap
::Map(map
) => {
40 map
.insert(key
, value
);
45 /// Return value by key if any.
46 pub fn get(&self, key
: &K
) -> Option
<&V
> {
48 MiniMap
::Array(array
) => {
56 MiniMap
::Map(map
) => {