debot_utils/
limitied_size_map.rs

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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
use std::collections::{HashMap, VecDeque};
use std::hash::Hash;
use std::ops::AddAssign;

#[derive(Clone, Default, Debug)]
pub struct ValuePair<T: AddAssign + Default + Clone> {
    pub first: T,
    pub second: T,
}

impl<T: AddAssign + Default + Clone> ValuePair<T> {
    pub fn new(first: T, second: T) -> Self {
        ValuePair { first, second }
    }
}

impl<T: AddAssign + Default + Clone> AddAssign for ValuePair<T> {
    fn add_assign(&mut self, other: Self) {
        self.first += other.first;
        self.second += other.second;
    }
}

#[derive(Clone, Default, Debug)]
pub struct DynamicLimitedSizeMap<K, V>
where
    K: Eq + Hash + Clone,
    V: AddAssign + Default + Clone,
{
    max_size: usize,
    keys_order: VecDeque<K>,
    map: HashMap<K, V>,
}

impl<K, V> DynamicLimitedSizeMap<K, ValuePair<V>>
where
    K: Eq + Hash + Clone,
    V: AddAssign + Default + Clone + From<u32>,
{
    pub fn new(max_size: usize) -> Self {
        DynamicLimitedSizeMap {
            max_size,
            keys_order: VecDeque::with_capacity(max_size),
            map: HashMap::new(),
        }
    }

    pub fn add(&mut self, key: K, increment1: V, increment2: V) {
        if !self.map.contains_key(&key) && self.map.len() == self.max_size {
            if let Some(oldest_key) = self.keys_order.pop_front() {
                self.map.remove(&oldest_key);
            }
        }

        let entry = self.map.entry(key.clone()).or_insert_with(|| ValuePair {
            first: 1.into(),
            second: 1.into(),
        });

        entry.first += increment1;
        entry.second += increment2;

        if !self.keys_order.contains(&key) {
            self.keys_order.push_back(key);
        }
    }

    pub fn get(&self, key: &K) -> Option<&ValuePair<V>> {
        self.map.get(key)
    }

    pub fn get_mut(&mut self, key: &K) -> Option<&mut ValuePair<V>> {
        self.map.get_mut(key)
    }

    pub fn iter(&self) -> std::collections::hash_map::Iter<K, ValuePair<V>> {
        self.map.iter()
    }

    pub fn iter_mut(&mut self) -> std::collections::hash_map::IterMut<K, ValuePair<V>> {
        self.map.iter_mut()
    }
}

#[derive(Clone, Default, Debug)]
pub struct LimitedSizeMap<K, V>
where
    K: Eq + Hash + Clone,
    V: Default + Clone,
{
    max_size: usize,
    keys_order: VecDeque<K>,
    map: HashMap<K, V>,
}

impl<K, V> LimitedSizeMap<K, V>
where
    K: Eq + Hash + Clone,
    V: Default + Clone,
{
    pub fn new(max_size: usize) -> Self {
        LimitedSizeMap {
            max_size,
            keys_order: VecDeque::with_capacity(max_size),
            map: HashMap::new(),
        }
    }

    pub fn insert(&mut self, key: K, value: V) {
        if !self.map.contains_key(&key) && self.map.len() == self.max_size {
            if let Some(oldest_key) = self.keys_order.pop_front() {
                self.map.remove(&oldest_key);
            }
        }

        self.map.insert(key.clone(), value);

        if !self.keys_order.contains(&key) {
            self.keys_order.push_back(key);
        }
    }

    pub fn get(&self, key: &K) -> Option<&V> {
        self.map.get(key)
    }

    pub fn get_mut(&mut self, key: &K) -> Option<&mut V> {
        self.map.get_mut(key)
    }

    pub fn iter(&self) -> std::collections::hash_map::Iter<K, V> {
        self.map.iter()
    }

    pub fn iter_mut(&mut self) -> std::collections::hash_map::IterMut<K, V> {
        self.map.iter_mut()
    }
}