mirror of
https://github.com/lisk77/comet.git
synced 2025-10-25 06:18:49 +00:00
fix: the code in flat_map should now be correct and changed formatting in renderer2d
This commit is contained in:
parent
ac63691fe3
commit
30cda18ce1
6 changed files with 483 additions and 11 deletions
64
crates/comet_structs/src/flat_map.rs
Normal file
64
crates/comet_structs/src/flat_map.rs
Normal 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
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue