Skip to content

Commit

Permalink
general updates
Browse files Browse the repository at this point in the history
  • Loading branch information
blu-dev committed Nov 5, 2023
1 parent 6d8d0cf commit c38e7bc
Show file tree
Hide file tree
Showing 5 changed files with 229 additions and 260 deletions.
33 changes: 11 additions & 22 deletions src/app/modules/work.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,22 @@ use crate::*;

use thiserror::Error;

macro_rules! const_partial_eq {
($id:ident) => {
impl const std::cmp::PartialEq for $id {
fn eq(&self, other: &Self) -> bool {
(*self as u8) == (*other as u8)
}
}
};
}
// macro_rules! const_partial_eq {
// ($id:ident) => {
// impl const std::cmp::PartialEq for $id {
// fn eq(&self, other: &Self) -> bool {
// (*self as u8) == (*other as u8)
// }
// }
// };
// }

/// Enum representing the category fo data that a [`WorkId`] refers to. This field
/// determines where data is stored. [`WorkKind::Transition`] and [`WorkKind::TransitionGroup`]
/// are not checked by any calls to the [`WorkModule`] accessors, however [`WorkKind::Instance`]
/// and [`WorkKind::Status`] are used.
#[repr(u8)]
#[derive(Debug, Copy, Clone, Eq)]
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum WorkKind {
Instance = 0x0,
Status = 0x1,
Expand All @@ -32,8 +32,6 @@ pub enum WorkKind {
None = 0xFF,
}

const_partial_eq!(WorkKind);

impl From<u8> for WorkKind {
fn from(arg: u8) -> Self {
match arg {
Expand All @@ -50,7 +48,7 @@ impl From<u8> for WorkKind {
/// has no impact on where the data is stored, and is not check by any calls to the
/// [`WorkModule`] accessors.
#[repr(u8)]
#[derive(Debug, Copy, Clone, Eq)]
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum WorkType {
Float = 0x0,
Int = 0x1,
Expand All @@ -59,8 +57,6 @@ pub enum WorkType {
None = 0xFF,
}

const_partial_eq!(WorkType);

impl From<u8> for WorkType {
fn from(arg: u8) -> Self {
match arg {
Expand All @@ -84,13 +80,6 @@ pub struct WorkId(u32);

impl WorkId {
pub(crate) const fn from_parts(ty: WorkType, kind: WorkKind, index: u32) -> Self {
if ty == WorkType::None {
panic!("WorkType cannot be None");
}
if kind == WorkKind::None {
panic!("WorkKind cannot be None");
}

let ty = (ty as u8 as u32) << 0x1C;
let kind = (kind as u8 as u32) << 0x18;
let index = index & 0x00FF_FFFF;
Expand Down
152 changes: 102 additions & 50 deletions src/cpp/binary_tree.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use std::{cmp::Ordering, ops::{Index, IndexMut}};
use std::{
cmp::Ordering,
ops::{Index, IndexMut},
};

#[repr(C)]
pub struct TreeNode<O: PartialOrd, V> {
Expand All @@ -8,15 +11,15 @@ pub struct TreeNode<O: PartialOrd, V> {
is_black: bool,
padding: [u8; 7],
key: O,
value: V
value: V,
}

pub struct RecursiveTreeNodeIter<'a, O: PartialOrd, V> {
node: &'a TreeNode<O, V>,
done_left: bool,
done_right: bool,
done_self: bool,
current_node: Option<Box<RecursiveTreeNodeIter<'a, O, V>>>
current_node: Option<Box<RecursiveTreeNodeIter<'a, O, V>>>,
}

impl<'a, O: PartialOrd, V> RecursiveTreeNodeIter<'a, O, V> {
Expand All @@ -26,7 +29,7 @@ impl<'a, O: PartialOrd, V> RecursiveTreeNodeIter<'a, O, V> {
done_left: false,
done_right: false,
done_self: false,
current_node: None
current_node: None,
}
}
}
Expand All @@ -37,7 +40,10 @@ impl<'a, O: PartialOrd, V> Iterator for RecursiveTreeNodeIter<'a, O, V> {
fn next(&mut self) -> Option<Self::Item> {
if !self.done_left {
if self.current_node.is_none() {
self.current_node = self.node.left().map(|node| Box::new(RecursiveTreeNodeIter::new(node)));
self.current_node = self
.node
.left()
.map(|node| Box::new(RecursiveTreeNodeIter::new(node)));
}
match &mut self.current_node {
Some(current_node) => match current_node.next() {
Expand All @@ -58,7 +64,10 @@ impl<'a, O: PartialOrd, V> Iterator for RecursiveTreeNodeIter<'a, O, V> {
}
if !self.done_right {
if self.current_node.is_none() {
self.current_node = self.node.right().map(|node| Box::new(RecursiveTreeNodeIter::new(node)));
self.current_node = self
.node
.right()
.map(|node| Box::new(RecursiveTreeNodeIter::new(node)));
}
match &mut self.current_node {
Some(current_node) => match current_node.next() {
Expand All @@ -79,39 +88,27 @@ impl<'a, O: PartialOrd, V> Iterator for RecursiveTreeNodeIter<'a, O, V> {

impl<O: PartialOrd, V> TreeNode<O, V> {
pub fn left(&self) -> Option<&TreeNode<O, V>> {
unsafe {
self.left.as_ref()
}
unsafe { self.left.as_ref() }
}

pub fn left_mut(&mut self) -> Option<&mut TreeNode<O, V>> {
unsafe {
self.left.as_mut()
}
unsafe { self.left.as_mut() }
}

pub fn right(&self) -> Option<&TreeNode<O, V>> {
unsafe {
self.right.as_ref()
}
unsafe { self.right.as_ref() }
}

pub fn right_mut(&mut self) -> Option<&mut TreeNode<O, V>> {
unsafe {
self.right.as_mut()
}
unsafe { self.right.as_mut() }
}

pub fn parent(&self) -> Option<&TreeNode<O, V>> {
unsafe {
self.parent.as_ref()
}
unsafe { self.parent.as_ref() }
}

pub fn parent_mut(&mut self) -> Option<&mut TreeNode<O, V>> {
unsafe {
self.parent.as_mut()
}
unsafe { self.parent.as_mut() }
}

pub fn is_black(&self) -> bool {
Expand All @@ -135,16 +132,16 @@ impl<O: PartialOrd, V> TreeNode<O, V> {
Some(Ordering::Less) => self.right().map(|x| x.get(key)).flatten(),
Some(Ordering::Equal) => Some(self.value()),
Some(Ordering::Greater) => self.left().map(|x| x.get(key)).flatten(),
None => None
}
None => None,
}
}

pub fn get_mut(&mut self, key: &O) -> Option<&mut V> {
match key.partial_cmp(self.key()) {
Some(Ordering::Less) => self.right_mut().map(|x| x.get_mut(key)).flatten(),
Some(Ordering::Equal) => Some(self.value_mut()),
Some(Ordering::Greater) => self.left_mut().map(|x| x.get_mut(key)).flatten(),
None => None
None => None,
}
}
}
Expand All @@ -153,20 +150,54 @@ impl<O: PartialOrd, V> TreeNode<O, V> {
pub struct Tree<O: PartialOrd, V> {
end: *mut TreeNode<O, V>,
root: *mut TreeNode<O, V>,
length: usize
length: usize,
}

impl<O: PartialOrd, V> Tree<O, V> {
fn root(&self) -> Option<&TreeNode<O, V>> {
unsafe {
self.root.as_ref()
unsafe fn get_for_insert(&mut self, key: &O) -> (*mut TreeNode<O, V>, *mut *mut TreeNode<O, V>) {
let mut node = self.root;
let mut p_node = &mut self.root as *mut _;

if node.is_null() {
return (node, p_node);
}

let mut current_node = node;
'outer: loop {
loop {
node = current_node;
if key.partial_cmp(&(*node).key).unwrap() != Ordering::Less {
break;
}

current_node = (*node).right;
p_node = &mut (*node).right;
if current_node.is_null() {
break 'outer;
}
}

if key.partial_cmp(&(*node).key).unwrap() != Ordering::Greater {
break;
}

current_node = (*node).left;
p_node = &mut (*node).left;

if (*p_node).is_null() {
break;
}
}

(node, p_node)
}

fn root(&self) -> Option<&TreeNode<O, V>> {
unsafe { self.root.as_ref() }
}

fn root_mut(&mut self) -> Option<&mut TreeNode<O, V>> {
unsafe {
self.root.as_mut()
}
unsafe { self.root.as_mut() }
}

pub fn len(&self) -> usize {
Expand All @@ -178,29 +209,53 @@ impl<O: PartialOrd, V> Tree<O, V> {
}

pub fn get(&self, key: &O) -> Option<&V> {
self.root()
.map(|root| root.get(key))
.flatten()
self.root().map(|root| root.get(key)).flatten()
}

pub fn get_mut(&mut self, key: &O) -> Option<&mut V> {
self.root_mut()
.map(|root| root.get_mut(key))
.flatten()
self.root_mut().map(|root| root.get_mut(key)).flatten()
}

pub fn insert(&mut self, key: O, value: V) {
let this = self as *mut Self;
let (inner, insert_node) = unsafe { (*this).get_for_insert(&key) };

let node = Box::leak(Box::new(TreeNode {
right: std::ptr::null_mut(),
left: std::ptr::null_mut(),
parent: inner,
is_black: false, // init in the below function, C++ sucks donkey dick
padding: [0; 7],
key,
value,
}));

unsafe {
*insert_node = node;

if !(*(*this).end).right.is_null() {
(*this).end = (*(*this).end).right;
}

balance_after_insert(self.root as _, *insert_node as _);
}
self.length += 1;
}
}

extern "C" {
#[link_name = "_ZNSt3__127__tree_balance_after_insertIPNS_16__tree_node_baseIPvEEEEvT_S5_"]
fn balance_after_insert(base: *mut u8, node: *mut u8);
}

pub struct TreeKeyValueIter<'a, O: PartialOrd, V> {
tree: &'a Tree<O, V>,
root: Option<RecursiveTreeNodeIter<'a, O, V>>
root: Option<RecursiveTreeNodeIter<'a, O, V>>,
}

impl<'a, O: PartialOrd, V> TreeKeyValueIter<'a, O, V> {
pub fn new(tree: &'a Tree<O, V>) -> Self {
Self {
tree,
root: None
}
Self { tree, root: None }
}
}

Expand All @@ -211,10 +266,7 @@ impl<'a, O: PartialOrd, V> Iterator for TreeKeyValueIter<'a, O, V> {
if self.root.is_none() {
self.root = self.tree.root().map(|x| RecursiveTreeNodeIter::new(x));
}
self.root
.as_mut()
.map(|root| root.next())
.flatten()
self.root.as_mut().map(|root| root.next()).flatten()
}
}

Expand All @@ -230,4 +282,4 @@ impl<O: PartialOrd, V> IndexMut<O> for Tree<O, V> {
fn index_mut(&mut self, index: O) -> &mut Self::Output {
self.get_mut(&index).expect("Failed to find key in tree!")
}
}
}
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#![feature(pointer_byte_offsets)]
#![allow(incomplete_features)]
#![allow(improper_ctypes)] // For simd
#![allow(non_snake_case)]
pub mod app;
pub mod cpp;
mod lib_impl;
Expand Down
Loading

0 comments on commit c38e7bc

Please sign in to comment.