fix: the code in flat_map should now be correct and changed formatting in renderer2d

This commit is contained in:
lisk77 2025-03-02 23:27:10 +01:00
parent ac63691fe3
commit 30cda18ce1
6 changed files with 483 additions and 11 deletions

View file

@ -0,0 +1,64 @@
pub struct MapNode<K, V> {
key: K,
value: V
}
impl<K, V> MapNode<K,V> {
pub fn new(key: K, value: V) -> Self {
Self {
key,
value
}
}
pub fn key(&self) -> &K {
&self.key
}
pub fn value(&self) -> &V {
&self.value
}
}
pub struct FlatMap<K: PartialEq, V> {
map: Vec<MapNode<K, V>>
}
impl<K: PartialEq, V> FlatMap<K, V> {
pub fn new() -> Self {
Self {
map: Vec::new()
}
}
pub fn insert(&mut self, key: K, value: V) {
let node = MapNode::new(key, value);
self.map.push(node);
}
pub fn remove(&mut self, key: K) {
for node in self.map {
if node.key() == *key {
self.map.retain(|&n| n.key() != *key);
}
}
}
pub fn get(&self, key: K) -> Option<&V> {
for node in self.map {
if node.key() == *key {
return Some(&node.value);
}
}
None
}
pub fn get_mut(&mut self, key: K) -> Option<&mut V> {
for mut node in self.map {
if node.key() == *key {
return Some(node.value);
}
}
None
}
}