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
|
/*
wearechat - generic multiplayer game with voip
Copyright (C) 2025 metamuffin
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, version 3 of the License only.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
use egui::Widget;
use std::{
collections::{HashMap, HashSet},
hash::Hash,
sync::RwLock,
};
pub struct DemandMap<K, V> {
inner: RwLock<DemandMapState<K, V>>,
}
struct DemandMapState<K, V> {
values: HashMap<K, (V, usize)>,
needed: HashSet<K>,
size_metric: usize,
}
impl<K: Hash + Eq + Clone, V: Clone> DemandMap<K, V> {
pub fn new() -> Self {
Self {
inner: DemandMapState {
needed: HashSet::new(),
values: HashMap::new(),
size_metric: 0,
}
.into(),
}
}
pub fn needed(&self) -> Vec<K> {
self.inner.read().unwrap().needed.iter().cloned().collect()
}
pub fn insert(&self, key: K, value: V, size: usize) {
let mut s = self.inner.write().unwrap();
s.needed.remove(&key);
if let Some((_, old_size)) = s.values.insert(key, (value, size)) {
s.size_metric -= old_size;
}
s.size_metric += size;
}
pub fn try_get(&self, key: K) -> Option<V> {
let mut s = self.inner.write().unwrap();
if let Some((k, _)) = s.values.get(&key) {
Some(k.to_owned())
} else {
s.needed.insert(key);
None
}
}
pub fn regenerate_all(&self) {
let mut s = self.inner.write().unwrap();
let keys = s.values.keys().cloned().collect::<Vec<_>>();
s.needed.extend(keys);
}
pub fn clear(&self) {
let mut s = self.inner.write().unwrap();
s.values.clear();
s.size_metric = 0;
}
}
impl<K, V> Widget for &DemandMap<K, V> {
fn ui(self, ui: &mut egui::Ui) -> egui::Response {
let state = self.inner.read().unwrap();
ui.label(state.needed.len().to_string());
ui.label(state.values.len().to_string());
ui.label(humansize::format_size(
state.size_metric,
humansize::DECIMAL,
));
ui.end_row();
ui.response()
}
}
|