debot_utils/
limitied_size_map.rs

1use std::collections::{HashMap, VecDeque};
2use std::hash::Hash;
3use std::ops::AddAssign;
4
5#[derive(Clone, Default, Debug)]
6pub struct ValuePair<T: AddAssign + Default + Clone> {
7    pub first: T,
8    pub second: T,
9}
10
11impl<T: AddAssign + Default + Clone> ValuePair<T> {
12    pub fn new(first: T, second: T) -> Self {
13        ValuePair { first, second }
14    }
15}
16
17impl<T: AddAssign + Default + Clone> AddAssign for ValuePair<T> {
18    fn add_assign(&mut self, other: Self) {
19        self.first += other.first;
20        self.second += other.second;
21    }
22}
23
24#[derive(Clone, Default, Debug)]
25pub struct DynamicLimitedSizeMap<K, V>
26where
27    K: Eq + Hash + Clone,
28    V: AddAssign + Default + Clone,
29{
30    max_size: usize,
31    keys_order: VecDeque<K>,
32    map: HashMap<K, V>,
33}
34
35impl<K, V> DynamicLimitedSizeMap<K, ValuePair<V>>
36where
37    K: Eq + Hash + Clone,
38    V: AddAssign + Default + Clone + From<u32>,
39{
40    pub fn new(max_size: usize) -> Self {
41        DynamicLimitedSizeMap {
42            max_size,
43            keys_order: VecDeque::with_capacity(max_size),
44            map: HashMap::new(),
45        }
46    }
47
48    pub fn add(&mut self, key: K, increment1: V, increment2: V) {
49        if !self.map.contains_key(&key) && self.map.len() == self.max_size {
50            if let Some(oldest_key) = self.keys_order.pop_front() {
51                self.map.remove(&oldest_key);
52            }
53        }
54
55        let entry = self.map.entry(key.clone()).or_insert_with(|| ValuePair {
56            first: 1.into(),
57            second: 1.into(),
58        });
59
60        entry.first += increment1;
61        entry.second += increment2;
62
63        if !self.keys_order.contains(&key) {
64            self.keys_order.push_back(key);
65        }
66    }
67
68    pub fn get(&self, key: &K) -> Option<&ValuePair<V>> {
69        self.map.get(key)
70    }
71
72    pub fn get_mut(&mut self, key: &K) -> Option<&mut ValuePair<V>> {
73        self.map.get_mut(key)
74    }
75
76    pub fn iter(&self) -> std::collections::hash_map::Iter<K, ValuePair<V>> {
77        self.map.iter()
78    }
79
80    pub fn iter_mut(&mut self) -> std::collections::hash_map::IterMut<K, ValuePair<V>> {
81        self.map.iter_mut()
82    }
83}
84
85#[derive(Clone, Default, Debug)]
86pub struct LimitedSizeMap<K, V>
87where
88    K: Eq + Hash + Clone,
89    V: Default + Clone,
90{
91    max_size: usize,
92    keys_order: VecDeque<K>,
93    map: HashMap<K, V>,
94}
95
96impl<K, V> LimitedSizeMap<K, V>
97where
98    K: Eq + Hash + Clone,
99    V: Default + Clone,
100{
101    pub fn new(max_size: usize) -> Self {
102        LimitedSizeMap {
103            max_size,
104            keys_order: VecDeque::with_capacity(max_size),
105            map: HashMap::new(),
106        }
107    }
108
109    pub fn insert(&mut self, key: K, value: V) {
110        if !self.map.contains_key(&key) && self.map.len() == self.max_size {
111            if let Some(oldest_key) = self.keys_order.pop_front() {
112                self.map.remove(&oldest_key);
113            }
114        }
115
116        self.map.insert(key.clone(), value);
117
118        if !self.keys_order.contains(&key) {
119            self.keys_order.push_back(key);
120        }
121    }
122
123    pub fn get(&self, key: &K) -> Option<&V> {
124        self.map.get(key)
125    }
126
127    pub fn get_mut(&mut self, key: &K) -> Option<&mut V> {
128        self.map.get_mut(key)
129    }
130
131    pub fn iter(&self) -> std::collections::hash_map::Iter<K, V> {
132        self.map.iter()
133    }
134
135    pub fn iter_mut(&mut self) -> std::collections::hash_map::IterMut<K, V> {
136        self.map.iter_mut()
137    }
138}