fix: removed the garbage github copilot code out of flat_map and making iter_mut irrelevant

This commit is contained in:
lisk77 2025-03-05 07:16:42 +01:00
parent f784fb2c02
commit 38b41985dc
5 changed files with 27 additions and 82 deletions

View file

@ -83,7 +83,7 @@ impl World {
/// Deletes an entity by its ID. /// Deletes an entity by its ID.
pub fn delete_entity(&mut self, entity_id: usize) { pub fn delete_entity(&mut self, entity_id: usize) {
self.entities[entity_id] = None; self.entities[entity_id] = None;
for (key, value) in self.components.iter_mut() { for (_, value) in self.components.iter_mut() {
value.remove::<u8>(entity_id); value.remove::<u8>(entity_id);
} }
self.id_queue.sorted_enqueue(entity_id as u32); self.id_queue.sorted_enqueue(entity_id as u32);
@ -164,7 +164,7 @@ impl World {
pub fn add_component<C: Component + 'static>(&mut self, entity_id: usize, component: C) { pub fn add_component<C: Component + 'static>(&mut self, entity_id: usize, component: C) {
assert_ne!(self.entities.get(entity_id), None, "There is no entity with this ID ({}) in the world!", entity_id); assert_ne!(self.entities.get(entity_id), None, "There is no entity with this ID ({}) in the world!", entity_id);
self.components.set_component(entity_id, component); self.components.set_component(entity_id, component);
let component_index = self.components.keys().iter_mut().position(|x| *x == *C::type_id()).unwrap(); let component_index = self.components.keys().iter_mut().position(|x| *x == C::type_id()).unwrap();
self.get_entity_mut(entity_id).unwrap().add_component(component_index); self.get_entity_mut(entity_id).unwrap().add_component(component_index);

View file

@ -1,6 +1,6 @@
use std::any::{Any, TypeId}; use std::any::{Any, TypeId};
use comet_log::*; use comet_log::*;
use crate::{FlatMap, IterMut, SparseSet}; use crate::{FlatMap, SparseSet};
pub type ComponentStorage = FlatMap<TypeId, SparseSet>; pub type ComponentStorage = FlatMap<TypeId, SparseSet>;
@ -9,7 +9,6 @@ impl ComponentStorage {
pub fn register_component<T: 'static>(&mut self, capacity: usize) { pub fn register_component<T: 'static>(&mut self, capacity: usize) {
if !self.contains(&TypeId::of::<T>()) { if !self.contains(&TypeId::of::<T>()) {
self.insert(TypeId::of::<T>(), SparseSet::new::<T>(capacity)); self.insert(TypeId::of::<T>(), SparseSet::new::<T>(capacity));
info!("Component {:?} has been registered", TypeId::of::<T>());
} }
else { else {
error!("Component {:?} already exists", TypeId::of::<T>()); error!("Component {:?} already exists", TypeId::of::<T>());
@ -19,7 +18,6 @@ impl ComponentStorage {
pub fn deregister_component<T: 'static>(&mut self) { pub fn deregister_component<T: 'static>(&mut self) {
if self.contains(&TypeId::of::<T>()) { if self.contains(&TypeId::of::<T>()) {
self.remove(&TypeId::of::<T>()); self.remove(&TypeId::of::<T>());
info!("Component {:?} has been deregistered", TypeId::of::<T>());
} }
else { else {
error!("Component {:?} does not exist", TypeId::of::<T>()); error!("Component {:?} does not exist", TypeId::of::<T>());
@ -64,11 +62,4 @@ impl ComponentStorage {
None None
} }
} }
pub fn iter_mut(&mut self) -> IterMut<'_, &TypeId, &SparseSet> {
IterMut {
keys_iter: self.keys_mut(),
values_iter: self.values_mut(),
}
}
} }

View file

@ -1,83 +1,54 @@
#[derive(Clone)]
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
}
}
#[derive(Clone)] #[derive(Clone)]
pub struct FlatMap<K: PartialEq, V> { pub struct FlatMap<K: PartialEq, V> {
map: Vec<MapNode<K, V>> map: Vec<(K,V)>
} }
impl<K: PartialEq, V> FlatMap<K, V> { impl<K: PartialEq + Clone, V: Clone> FlatMap<K, V> {
pub fn new() -> Self { pub fn new() -> Self {
Self { Self {
map: Vec::new() map: Vec::new()
} }
} }
pub fn keys(&self) -> Vec<&K> { pub fn keys(&self) -> Vec<K> {
self.map.iter().map(|node| node.key()).collect::<Vec<&K>>() self.map.iter().map(|node| node.0.clone()).collect::<Vec<K>>()
} }
pub fn values(&self) -> Vec<&V> { pub fn values(&self) -> Vec<V> {
self.map.iter().map(|node| node.value()).collect::<Vec<&V>>() self.map.iter().map(|node| node.1.clone()).collect::<Vec<V>>()
}
pub fn keys_mut(&mut self) -> impl Iterator<Item = &mut K> {
self.map.iter_mut().map(|node| &mut node.key)
}
pub fn values_mut(&mut self) -> impl Iterator<Item = &mut V> {
self.map.iter_mut().map(|node| &mut node.value)
} }
pub fn insert(&mut self, key: K, value: V) { pub fn insert(&mut self, key: K, value: V) {
let node = MapNode::new(key, value); self.map.push((key,value));
self.map.push(node);
} }
pub fn remove(&mut self, key: &K) { pub fn remove(&mut self, key: &K) {
self.map.retain(|node| node.key() != key); self.map.retain(|node| node.0 != *key);
} }
pub fn get(&self, key: &K) -> Option<&V> { pub fn get(&self, key: &K) -> Option<&V> {
self.map.iter() if let Some(node) = self.map.iter().find(|node| node.0 == *key) {
.find(|node| node.key() == key) Some(&node.1)
.map(|node| node.value()) }
else {
None
}
} }
pub fn get_mut(&mut self, key: &K) -> Option<&mut V> { pub fn get_mut(&mut self, key: &K) -> Option<&mut V> {
self.map.iter_mut() if let Some(node) = self.map.iter_mut().find(|node| node.0 == *key) {
.find(|node| node.key() == key) Some(&mut node.1)
.map(|node| node.value_mut()) }
else {
None
}
} }
pub fn contains(&self, key: &K) -> bool { pub fn contains(&self, key: &K) -> bool {
self.map.iter().any(|node| node.key() == key) self.map.iter().any(|node| node.0 == *key)
} }
}
// Add this method to MapNode to allow mutable access to value pub fn iter_mut(&mut self) -> impl Iterator<Item = &mut (K, V)> {
impl<K, V> MapNode<K, V> { self.map.iter_mut()
fn value_mut(&mut self) -> &mut V {
&mut self.value
} }
} }

View file

@ -1,15 +0,0 @@
pub struct IterMut<'a, K, V> {
pub(crate) keys_iter: std::slice::IterMut<'a, K>,
pub(crate) values_iter: std::slice::IterMut<'a, V>,
}
impl<'a, K, V> Iterator for IterMut<'a, K, V> {
type Item = (&'a mut K, &'a mut V);
fn next(&mut self) -> Option<Self::Item> {
match (self.keys_iter.next(), self.values_iter.next()) {
(Some(key), Some(value)) => Some((key, value)),
_ => None,
}
}
}

View file

@ -2,10 +2,8 @@ pub use column::Column;
pub use sparse_set::SparseSet; pub use sparse_set::SparseSet;
pub use flat_map::FlatMap; pub use flat_map::FlatMap;
pub use component_storage::ComponentStorage; pub use component_storage::ComponentStorage;
pub use iter_mut::IterMut;
mod column; mod column;
mod sparse_set; mod sparse_set;
mod flat_map; mod flat_map;
mod component_storage; mod component_storage;
mod iter_mut;