1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#[derive(Debug)]
pub struct BoundedHashMap<K: Hash + Eq, V>
{
map: HashMap<K, V>,
maximum_capacity: usize,
}
impl<K: Hash + Eq, V> BoundedHashMap<K, V>
{
#[inline(always)]
pub fn new(maximum_capacity: usize) -> Self
{
let allocate_hash_map_with_slightly_more_capacity_as_insert_always_reserves_1 = maximum_capacity + 1;
Self
{
map: HashMap::with_capacity(allocate_hash_map_with_slightly_more_capacity_as_insert_always_reserves_1),
maximum_capacity,
}
}
#[inline(always)]
pub fn contains_key(&self, key: &K) -> bool
{
self.map.contains_key(key)
}
#[inline(always)]
pub fn get_mut(&mut self, key: &K) -> Option<&mut V>
{
self.map.get_mut(key)
}
#[inline(always)]
pub fn len(&self) -> usize
{
self.map.len()
}
#[inline(always)]
pub fn is_full(&self) -> bool
{
self.len() == self.maximum_capacity
}
#[inline(always)]
pub fn is_over_filled(&self) -> bool
{
self.len() > self.maximum_capacity
}
#[inline(always)]
pub fn insert_uniquely_and_return_mutable_reference(&mut self, key: K, value: V) -> &mut V
{
debug_assert!(!self.map.contains_key(&key), "Already present");
self.map.entry(key).or_insert(value)
}
#[inline(always)]
pub fn insert(&mut self, key: K, value: V) -> Option<V>
{
self.map.insert(key, value)
}
#[inline(always)]
pub fn remove(&mut self, key: &K) -> Option<V>
{
self.map.remove(key)
}
#[inline(always)]
pub fn remove_entry(&mut self, key: &K) -> Option<(K, V)>
{
self.map.remove_entry(key)
}
}