some stdmap functions

This commit is contained in:
bgkillas 2025-08-19 11:24:57 -04:00
parent fb56e2f1d9
commit ec3857c6b9
2 changed files with 37 additions and 14 deletions

View file

@ -441,7 +441,7 @@ impl<K: Default, V: Default> Default for StdMapNode<K, V> {
parent: ptr::null_mut(), parent: ptr::null_mut(),
right: ptr::null_mut(), right: ptr::null_mut(),
color: false, color: false,
end: false, end: true,
unk: [0, 0], unk: [0, 0],
key: Default::default(), key: Default::default(),
value: Default::default(), value: Default::default(),
@ -449,12 +449,27 @@ impl<K: Default, V: Default> Default for StdMapNode<K, V> {
} }
} }
impl<K, V> StdMapNode<K, V> {
pub fn new(key: K, value: V) -> Self {
Self {
left: ptr::null_mut(),
parent: ptr::null_mut(),
right: ptr::null_mut(),
color: false,
end: true,
unk: [0, 0],
key,
value,
}
}
}
#[repr(C)] #[repr(C)]
pub struct StdMap<K, V> { pub struct StdMap<K: 'static, V: 'static> {
pub root: *mut StdMapNode<K, V>, pub root: &'static mut StdMapNode<K, V>,
pub len: usize, pub len: usize,
} }
impl<K: Default, V: Default> Default for StdMap<K, V> { impl<K: Default + 'static, V: Default + 'static> Default for StdMap<K, V> {
fn default() -> Self { fn default() -> Self {
Self { Self {
root: Box::leak(Box::new(StdMapNode::default())), root: Box::leak(Box::new(StdMapNode::default())),
@ -471,7 +486,7 @@ impl<K: Debug + 'static, V: Debug + 'static> Debug for StdMap<K, V> {
} }
#[derive(Debug)] #[derive(Debug)]
pub struct StdMapIter<K, V> { pub struct StdMapIter<K: 'static, V: 'static> {
pub root: *mut StdMapNode<K, V>, pub root: *mut StdMapNode<K, V>,
pub current: *const StdMapNode<K, V>, pub current: *const StdMapNode<K, V>,
pub parents: Vec<*const StdMapNode<K, V>>, pub parents: Vec<*const StdMapNode<K, V>>,
@ -501,10 +516,20 @@ impl<K: 'static, V: 'static> Iterator for StdMapIter<K, V> {
} }
impl<K: 'static, V: 'static> StdMap<K, V> { impl<K: 'static, V: 'static> StdMap<K, V> {
pub fn iter(&self) -> impl Iterator<Item = (&'static K, &'static V)> { pub fn insert(&mut self, key: K, value: V) -> Option<V> {
if self.is_empty() {
self.len += 1;
let node = StdMapNode::new(key, value);
self.root.parent = Box::leak(Box::new(node)) as *mut _;
None
} else {
todo!()
}
}
pub fn iter(&self) -> impl Iterator<Item = (&'static K, &'static V)> + '_ {
StdMapIter { StdMapIter {
root: self.root, root: (self.root as *const StdMapNode<K, V>).cast_mut(),
current: unsafe { self.root.as_ref().unwrap().parent }, current: self.root.parent,
parents: Vec::with_capacity(8), parents: Vec::with_capacity(8),
} }
} }
@ -523,14 +548,14 @@ impl<K: 'static, V: 'static> StdMap<K, V> {
} }
impl<K: 'static + Ord, V: 'static> StdMap<K, V> { impl<K: 'static + Ord, V: 'static> StdMap<K, V> {
pub fn get(&self, key: &K) -> Option<&'static V> { pub fn get(&self, key: &K) -> Option<&'static V> {
let mut node = unsafe { self.root.as_ref()?.parent.as_ref()? }; let mut node = unsafe { self.root.parent.as_ref()? };
loop { loop {
let next = match key.cmp(&node.key) { let next = match key.cmp(&node.key) {
Ordering::Less => node.left, Ordering::Less => node.left,
Ordering::Greater => node.right, Ordering::Greater => node.right,
Ordering::Equal => return Some(&node.value), Ordering::Equal => return Some(&node.value),
}; };
if next == self.root { if next == (self.root as *const StdMapNode<K, V>).cast_mut() {
return None; return None;
} }
node = unsafe { next.as_ref()? }; node = unsafe { next.as_ref()? };

View file

@ -80,10 +80,8 @@ fn test_com_create() {
let mut com_buffer = ComponentBuffer::default(); let mut com_buffer = ComponentBuffer::default();
let com = &mut com_buffer as *mut _; let com = &mut com_buffer as *mut _;
em.component_buffers.push(com); em.component_buffers.push(com);
let mut node = crate::noita::types::StdMapNode::default(); cm.component_buffer_indices
node.key = StdString::from_str("WalletComponent"); .insert(StdString::from_str("WalletComponent"), 0);
node.value = 0usize;
unsafe { cm.component_buffer_indices.root.as_mut().unwrap() }.parent = &mut node as *mut _;
} }
let ent = em.create(); let ent = em.create();
{ {