mirror of
https://github.com/lisk77/comet.git
synced 2025-10-25 06:18:49 +00:00
wip: transitioning to the newer comet_structs and new ComponentStorage through a FlatMap (not the rust implementation)
This commit is contained in:
parent
5b43c7a319
commit
db405bfb2e
12 changed files with 214 additions and 802 deletions
|
|
@ -1,64 +0,0 @@
|
|||
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, V> {
|
||||
map: Vec<MapNode<K, V>
|
||||
}
|
||||
|
||||
impl<K, V> FlatMap<K, V> {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
map: Vec::new()
|
||||
}
|
||||
}
|
||||
|
||||
pub fn insert(key: K, value: V) {
|
||||
let node = MapNode::new(key, value);
|
||||
self.map.push(node);
|
||||
}
|
||||
|
||||
pub fn remove(key: K) {
|
||||
for node in self.map {
|
||||
if node.key() == key {
|
||||
self.map.remove(node);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get(key: K) -> Option<&V> {
|
||||
for node in self.map {
|
||||
if node.key() == key {
|
||||
return Some(&node.value);
|
||||
}
|
||||
}
|
||||
return None;
|
||||
}
|
||||
|
||||
pub fn get_mut(key: K) -> Option<&mut V> {
|
||||
for node in self.map {
|
||||
if node.key() == key {
|
||||
return Some(&mut node.value);
|
||||
}
|
||||
}
|
||||
return None;
|
||||
}
|
||||
}
|
||||
|
|
@ -1,5 +0,0 @@
|
|||
pub use column::Column;
|
||||
pub use sparse_set::SparseSet;
|
||||
|
||||
mod column;
|
||||
mod sparse_set;
|
||||
|
|
@ -1,91 +0,0 @@
|
|||
use crate::{Component};
|
||||
use std::{
|
||||
alloc::{
|
||||
handle_alloc_error,
|
||||
Layout
|
||||
},
|
||||
any::TypeId,
|
||||
collections::{
|
||||
HashMap,
|
||||
HashSet
|
||||
},
|
||||
hash::{
|
||||
DefaultHasher,
|
||||
Hash,
|
||||
Hasher
|
||||
},
|
||||
mem::MaybeUninit,
|
||||
ptr::NonNull
|
||||
};
|
||||
use std::ptr;
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct SparseSet {
|
||||
sparse: Vec<Option<usize>>,
|
||||
dense: Column,
|
||||
len: usize
|
||||
}
|
||||
|
||||
impl SparseSet {
|
||||
pub fn new<T: 'static>(capacity: usize) -> Self {
|
||||
Self {
|
||||
sparse: Vec::with_capacity(capacity),
|
||||
dense: Column::new::<T>(capacity),
|
||||
len: 0
|
||||
}
|
||||
}
|
||||
|
||||
pub fn set<T: 'static>(&mut self, index: usize, element: T) {
|
||||
if index >= self.sparse.len() {
|
||||
self.sparse.resize_with(index + 1, || None);
|
||||
}
|
||||
|
||||
if let Some(column_index) = self.sparse[index] {
|
||||
// Explicitly drop the existing component before replacing it
|
||||
unsafe {
|
||||
let existing_ptr = self.dense.data.get_unchecked_mut(column_index) as *mut T;
|
||||
ptr::drop_in_place(existing_ptr);
|
||||
ptr::write(existing_ptr, element);
|
||||
}
|
||||
} else {
|
||||
let column_index = unsafe { self.dense.data.push_uninit() };
|
||||
unsafe {
|
||||
self.dense.data.initialize_unchecked(column_index, &element as *const T as *mut u8);
|
||||
}
|
||||
self.sparse[index] = Some(column_index);
|
||||
self.len += 1;
|
||||
}
|
||||
}
|
||||
|
||||
pub fn remove<T: 'static>(&mut self, index: usize) -> Option<T> {
|
||||
if index >= self.sparse.len() || self.sparse[index] == None {
|
||||
return None;
|
||||
}
|
||||
|
||||
let column_index = self.sparse[index];
|
||||
let element = unsafe {
|
||||
self.dense.data.swap_remove_and_forget_unchecked(column_index.unwrap())
|
||||
};
|
||||
|
||||
self.sparse[index] = None;
|
||||
self.len -= 1;
|
||||
|
||||
Some(unsafe { ptr::read(element as *const T) })
|
||||
}
|
||||
|
||||
pub fn get<T: 'static>(&self, index: usize) -> Option<&T> {
|
||||
if index >= self.sparse.len() || self.sparse[index] == None {
|
||||
return None;
|
||||
}
|
||||
|
||||
self.dense.get::<T>(index)
|
||||
}
|
||||
|
||||
pub fn get_mut<T: 'static>(&mut self, index: usize) -> Option<&mut T> {
|
||||
if index >= self.sparse.len() || self.sparse[index] == None {
|
||||
return None;
|
||||
}
|
||||
|
||||
self.dense.get_mut::<T>(index)
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue