Refactoring traits for garbage collection. Incomplete.

This commit is contained in:
Christian Lincoln 2026-10-07 19:58:09 +01:00
parent f3922ff84d
commit 76a9c2259e
2 changed files with 248 additions and 233 deletions

397
src/gc.rs
View file

@ -1,307 +1,324 @@
use std::hash::{DefaultHasher, Hash, Hasher};
use std::marker::{PhantomData};
use std::ops::{Deref, DerefMut};
use map::{Hashed, Hashes};
use std::hash::{DefaultHasher, Hash, Hasher};
use std::marker::PhantomData;
use std::ops::{Deref, DerefMut};
type Index = u32;
pub struct FreeList<T> {
items: Vec<Option<Header<T>>>,
free: Vec<Index>,
count: u32,
items: Vec<Option<Header<T>>>,
free: Vec<Index>,
_count: u32,
}
#[derive(Clone,Debug)]
#[derive(Clone, Debug)]
pub struct AnyId {
pub(crate) index: Index
pub(crate) index: Index,
}
impl PartialEq for AnyId {
fn eq(&self, other: &Self) -> bool {
self.index == other.index
}
fn eq(&self, other: &Self) -> bool {
self.index == other.index
}
}
impl<T> From<AnyId> for Id<T> {
fn from(value: AnyId) -> Self {
Id {
index: value.index,
phantom_data: PhantomData,
fn from(value: AnyId) -> Self {
Id {
index: value.index,
phantom_data: PhantomData,
}
}
}
}
#[derive(Debug)]
pub struct Id<T> {
pub(crate) index: Index,
phantom_data: PhantomData<T>,
pub(crate) index: Index,
phantom_data: PhantomData<T>,
}
pub struct RefId<'a, T> {
pub id: Id<T>,
it: &'a mut T
pub id: Id<T>,
it: &'a mut T,
}
impl<'a, T> Deref for RefId<'a, T> {
type Target = T;
type Target = T;
fn deref(&self) -> &Self::Target {
self.it
}
fn deref(&self) -> &Self::Target {
self.it
}
}
impl<'a, T> DerefMut for RefId<'a, T> {
fn deref_mut(&mut self) -> &mut Self::Target {
self.it
}
fn deref_mut(&mut self) -> &mut Self::Target {
self.it
}
}
impl<T> From<RefId<'_, T>> for Id<T> {
fn from(value: RefId<T>) -> Self {
value.id
}
fn from(value: RefId<T>) -> Self {
value.id
}
}
impl<T> Clone for Id<T> {
fn clone(&self) -> Self {
Id {
index: self.index,
phantom_data: PhantomData::default(),
fn clone(&self) -> Self {
Id {
index: self.index,
phantom_data: PhantomData::default(),
}
}
}
}
impl<T> PartialEq for Id<T> {
fn eq(&self, other: &Self) -> bool {
self.index == other.index
}
fn eq(&self, other: &Self) -> bool {
self.index == other.index
}
}
impl<T> Eq for Id<T> {}
impl<T> Hash for Id<T> {
fn hash<H: Hasher>(&self, state: &mut H) {
self.index.hash(state)
}
fn hash<H: Hasher>(&self, state: &mut H) {
self.index.hash(state)
}
}
impl<T> Id<T> {
pub fn maybe(self) -> MaybeId<T> {
MaybeId(self)
}
pub fn any(self) -> AnyId { AnyId {
index: self.index
} }
pub fn maybe(self) -> MaybeId<T> {
MaybeId(self)
}
pub fn any(self) -> AnyId {
AnyId { index: self.index }
}
}
#[derive(Debug)]
pub struct MaybeId<T>(Id<T>);
impl<T> From<Id<T>> for MaybeId<T> {
fn from(value: Id<T>) -> Self {
MaybeId(value)
}
fn from(value: Id<T>) -> Self {
MaybeId(value)
}
}
impl<T> Clone for MaybeId<T> {
fn clone(&self) -> Self {
if self.0.index != u32::MAX {
MaybeId(self.0.clone())
} else {
MaybeId::NULL
fn clone(&self) -> Self {
if self.0.index != u32::MAX {
MaybeId(self.0.clone())
} else {
MaybeId::NULL
}
}
}
}
impl<T> PartialEq for MaybeId<T> {
fn eq(&self, other: &Self) -> bool {
self.0 == other.0
}
fn eq(&self, other: &Self) -> bool {
self.0 == other.0
}
}
impl<T> MaybeId<T> {
pub const NULL: MaybeId<T> = MaybeId(Id { index: u32::MAX, phantom_data: PhantomData {}, });
pub fn unwrap(self) -> Id<T> {
if self.0.index == u32::MAX {
panic!()
} else {
self.0
pub const NULL: MaybeId<T> = MaybeId(Id {
index: u32::MAX,
phantom_data: PhantomData {},
});
pub fn unwrap(self) -> Id<T> {
if self.0.index == u32::MAX {
panic!()
} else {
self.0
}
}
}
pub fn exists(&self) -> Option<Id<T>> {
if self.0.index == u32::MAX {
None
} else {
Some(self.0.clone())
pub fn exists(&self) -> Option<Id<T>> {
if self.0.index == u32::MAX {
None
} else {
Some(self.0.clone())
}
}
}
}
impl<T> Default for FreeList<T> {
fn default() -> Self {
Self::new()
}
fn default() -> Self {
Self::new()
}
}
impl<T> FreeList<T> {
pub fn new() -> FreeList<T> {
FreeList {
items: Vec::new(),
free: Vec::new(),
count: 0,
pub fn new() -> FreeList<T> {
FreeList {
items: Vec::new(),
free: Vec::new(),
_count: 0,
}
}
}
pub fn get(&mut self, id: &Id<T>) -> &mut T {
&mut self.items[id.index as usize].as_mut().unwrap().it
}
pub fn get_ref(&mut self, id: Id<T>) -> RefId<'_, T> {
let it = self.get(&id);
RefId { id, it }
}
pub fn remove(&mut self, id: &Id<T>) -> T {
self.free.push(id.index);
self.items[id.index as usize].take().unwrap().it
}
pub fn make(&mut self, value: T) -> RefId<'_, T> { // todo: shouldn't panic if allocation fails
if let Some(free) = self.free.pop() {
self.items[free as usize] = Some(Header::new(value));
RefId {
id: Id {
index: free,
phantom_data: PhantomData,
},
it: &mut self.items[free as usize].as_mut().unwrap().it,
}
} else {
self.items.push(Some(Header::new(value)));
let index = (self.items.len() - 1) as Index;
RefId {
id: Id {
index,
phantom_data: PhantomData,
},
it: &mut self.items[index as usize].as_mut().unwrap().it,
}
pub fn get(&mut self, id: &Id<T>) -> &mut T {
&mut self.items[id.index as usize].as_mut().unwrap().it
}
pub fn get_ref(&mut self, id: Id<T>) -> RefId<'_, T> {
let it = self.get(&id);
RefId { id, it }
}
pub fn remove(&mut self, id: &Id<T>) -> T {
self.free.push(id.index);
self.items[id.index as usize].take().unwrap().it
}
pub fn make(&mut self, value: T) -> RefId<'_, T> {
// todo: shouldn't panic if allocation fails
if let Some(free) = self.free.pop() {
self.items[free as usize] = Some(Header::new(value));
RefId {
id: Id {
index: free,
phantom_data: PhantomData,
},
it: &mut self.items[free as usize].as_mut().unwrap().it,
}
} else {
self.items.push(Some(Header::new(value)));
let index = (self.items.len() - 1) as Index;
RefId {
id: Id {
index,
phantom_data: PhantomData,
},
it: &mut self.items[index as usize].as_mut().unwrap().it,
}
}
}
}
}
pub struct IdNode<T> {
item: T,
next: MaybeId<IdNode<T>>,
item: T,
next: MaybeId<IdNode<T>>,
}
impl<T,C> Traverse<C> for IdNode<T> where C: Allocates<Self> {
fn traverse(id: &Id<IdNode<T>>, context: &mut C) {
if let Some(ref next) = context.freelist().get(&id).next.exists() {
IdNode::traverse(next,context)
impl<T, C> Traverse<C> for IdNode<T>
where
C: Allocates<Self>,
{
fn traverse(id: &Id<IdNode<T>>, context: &mut C) {
if let Some(ref next) = context.freelist().get(&id).next.exists() {
IdNode::traverse(next, context)
}
}
}
fn collect(self) {
todo!()
}
}
#[derive(Clone)]
pub struct LinkedList<T> {
first: MaybeId<IdNode<T>>,
count: Index,
first: MaybeId<IdNode<T>>,
count: Index,
}
impl<T> Default for LinkedList<T> {
fn default() -> Self {
LinkedList {
first: MaybeId::NULL,
count: 0,
fn default() -> Self {
LinkedList {
first: MaybeId::NULL,
count: 0,
}
}
}
}
impl<T> LinkedList<T> {
pub fn new() -> LinkedList<T> {
LinkedList {
first: MaybeId::NULL,
count: 0,
}
}
pub fn add(&mut self, list: &mut FreeNodeList<T>, it: T) {
let old = std::mem::replace(&mut self.first,list.make(IdNode {
item: it,
next: MaybeId::NULL,
}).id.maybe());
list.get(&self.first.clone().unwrap()).next = old; // todo: this is painful
self.count += 1;
}
pub fn remove<F>(&mut self, list: &mut FreeNodeList<T>, criteria: F) -> T where T: PartialEq, F: Fn(&T) -> bool {
// Find some occurrence of 'item' in the list, remove and restitch the list, returning its id
let mut maybe_last = MaybeId::NULL;
let mut maybe_this = self.first.clone();
while let Some(this) = maybe_this.exists() {
let node = list.remove(&this);
let next = node.next.clone();
if criteria(&node.item) {
self.count -= 1;
if let Some(last) = maybe_last.exists() {
list.get(&last).next = node.next.clone();
} else {
self.first = node.next.clone();
pub fn new() -> LinkedList<T> {
LinkedList {
first: MaybeId::NULL,
count: 0,
}
return node.item;
}
maybe_last = maybe_this;
maybe_this = next;
}
panic!()
}
pub fn add(&mut self, list: &mut FreeNodeList<T>, it: T) {
let old = std::mem::replace(
&mut self.first,
list.make(IdNode {
item: it,
next: MaybeId::NULL,
})
.id
.maybe(),
);
list.get(&self.first.clone().unwrap()).next = old; // todo: this is painful
self.count += 1;
}
pub fn remove<F>(&mut self, list: &mut FreeNodeList<T>, criteria: F) -> T
where
T: PartialEq,
F: Fn(&T) -> bool,
{
// Find some occurrence of 'item' in the list, remove and restitch the list, returning its id
let mut maybe_last = MaybeId::NULL;
let mut maybe_this = self.first.clone();
while let Some(this) = maybe_this.exists() {
let node = list.remove(&this);
let next = node.next.clone();
if criteria(&node.item) {
self.count -= 1;
if let Some(last) = maybe_last.exists() {
list.get(&last).next = node.next.clone();
} else {
self.first = node.next.clone();
}
return node.item;
}
maybe_last = maybe_this;
maybe_this = next;
}
panic!()
}
}
pub type FreeNodeList<T> = FreeList<IdNode<T>>;
#[derive(Clone, Debug, Copy)]
enum State {
Cold,
Warm,
Cold,
_Warm,
}
#[derive(Debug, Clone)]
struct Header<T> {
state: State,
it: T,
_state: State,
it: T,
}
impl<T> Header<T> where {
fn new(it: T) -> Header<T> {
Header {
state: State::Cold,
it,
impl<T> Header<T> {
fn new(it: T) -> Header<T> {
Header {
_state: State::Cold,
it,
}
}
}
}
pub trait Allocates<T> where Self: Sized, T: Traverse<Self> {
fn collect();
fn freelist<'a>(&'a mut self) -> &'a mut FreeList<T>;
pub trait Allocates<T>
where
Self: Sized,
T: Traverse<Self>,
{
fn freelist<'a>(&'a mut self) -> &'a mut FreeList<T>;
fn get(&mut self, id: &Id<T>) -> &mut T {
self.freelist().get(id)
}
}
pub trait Traverse<C>: Sized {
fn traverse(id: &Id<Self>, context: &mut C);
fn collect(self);
fn traverse(id: &Id<Self>, context: &mut C);
// fn collect(self);
}
impl<T> Hashes for Id<T> {
fn hashed(&self) -> Hashed {
let mut hasher = DefaultHasher::new();
self.index.hash(&mut hasher);
hasher.finish() as Hashed
}
fn hashed(&self) -> Hashed {
let mut hasher = DefaultHasher::new();
self.index.hash(&mut hasher);
hasher.finish() as Hashed
}
}
impl Hashes for AnyId {
fn hashed(&self) -> Hashed {
let mut hasher = DefaultHasher::new();
self.index.hash(&mut hasher);
hasher.finish() as Hashed
}
fn hashed(&self) -> Hashed {
let mut hasher = DefaultHasher::new();
self.index.hash(&mut hasher);
hasher.finish() as Hashed
}
}