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

View file

@ -1,56 +1,54 @@
extern crate proc_macro;
use proc_macro::{Delimiter, Group, Punct, Ident, TokenStream, TokenTree, Literal, Spacing, Span};
use map::{DisplacementSet, MapEntry};
use proc_macro::token_stream::IntoIter;
use proc_macro::{Delimiter, Group, Ident, Literal, Punct, Spacing, Span, TokenStream, TokenTree};
use std::iter::Peekable;
use map::{DisplacementSet, Equivalent, Hashed, Hashes, MapEntry, TableEntry};
type Tokens = Peekable<IntoIter>;
fn brackets(stream: TokenStream) -> TokenStream {
TokenStream::from(TokenTree::Group(Group::new(Delimiter::Brace,stream)))
}
fn entry(input: &mut Tokens) -> Option<MapEntry<String, TokenStream>> {
if let Some(token) = input.peek() {
let index = match input.next() { // Take one literal as an index
if input.peek().is_some() {
let index = match input.next() {
// Take one literal as an index
Some(TokenTree::Literal(literal)) => literal.to_string(),
Some(_) => panic!("Expected literal index"),
None => return None,
};
match input.next() { // Look for '=' in "=>"
Some(TokenTree::Punct(punct)) if punct.as_char() == '=' => {},
match input.next() {
// Look for '=' in "=>"
Some(TokenTree::Punct(punct)) if punct.as_char() == '=' => {}
_ => panic!("Expected '=>'"),
};
match input.next() { // Look for '>' in "=>"
Some(TokenTree::Punct(punct)) if punct.as_char() == '>' => {},
match input.next() {
// Look for '>' in "=>"
Some(TokenTree::Punct(punct)) if punct.as_char() == '>' => {}
_ => panic!("Expected '=>'"),
};
let mut value = Vec::new();
let mut closure = false;
loop { // Accumulate value token stream until ',' is found
loop {
// Accumulate value token stream until ',' is found
match input.next() {
Some(TokenTree::Punct(punct)) if punct.as_char() == ',' && !closure => break,
Some(ref it @ TokenTree::Punct(ref punct)) if punct.as_char() == '|' => {
value.push(it.clone());
closure = !closure
},
}
Some(it) => value.push(it),
None => break,
}
}
let mut value_stream = TokenStream::new();
value_stream.extend(value);
Some(MapEntry(Some((
index,
value_stream,
))))
Some(MapEntry(Some((index, value_stream))))
} else {
None
}
}
#[proc_macro]
pub fn map(input: TokenStream) -> TokenStream { // todo: only works for string literals right now
pub fn map(input: TokenStream) -> TokenStream {
// todo: only works for string literals right now
let mut tokens = input.into_iter().peekable();
let mut entries = DisplacementSet::new();
while let Some(entry) = entry(&mut tokens) {
@ -65,11 +63,12 @@ pub fn map(input: TokenStream) -> TokenStream { // todo: only works for string l
TokenTree::Punct(Punct::new(',', Spacing::Alone)),
]);
inner.extend(entry.1);
let inner = TokenStream::from(TokenTree::Group(Group::new(Delimiter::Parenthesis,inner)));
let inner =
TokenStream::from(TokenTree::Group(Group::new(Delimiter::Parenthesis, inner)));
let mut option = TokenStream::new();
option.extend(vec![
TokenTree::Ident(Ident::new("Some", Span::mixed_site())),
TokenTree::Group(Group::new(Delimiter::Parenthesis,inner))
TokenTree::Group(Group::new(Delimiter::Parenthesis, inner)),
]);
option
} else {
@ -103,6 +102,5 @@ pub fn map(input: TokenStream) -> TokenStream { // todo: only works for string l
TokenTree::Punct(Punct::new(',', Spacing::Alone)),
])
}
let mut array = TokenStream::from(TokenTree::Group(Group::new(Delimiter::Bracket,elements)));
array
TokenStream::from(TokenTree::Group(Group::new(Delimiter::Bracket, elements)))
}

View file

@ -1,19 +1,19 @@
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,
_count: u32,
}
#[derive(Clone, Debug)]
pub struct AnyId {
pub(crate) index: Index
pub(crate) index: Index,
}
impl PartialEq for AnyId {
@ -39,7 +39,7 @@ pub struct Id<T> {
pub struct RefId<'a, T> {
pub id: Id<T>,
it: &'a mut T
it: &'a mut T,
}
impl<'a, T> Deref for RefId<'a, T> {
@ -51,7 +51,6 @@ impl<'a, T> Deref for RefId<'a, T> {
}
impl<'a, T> DerefMut for RefId<'a, T> {
fn deref_mut(&mut self) -> &mut Self::Target {
self.it
}
@ -90,9 +89,9 @@ impl<T> Id<T> {
pub fn maybe(self) -> MaybeId<T> {
MaybeId(self)
}
pub fn any(self) -> AnyId { AnyId {
index: self.index
} }
pub fn any(self) -> AnyId {
AnyId { index: self.index }
}
}
#[derive(Debug)]
@ -121,7 +120,10 @@ impl<T> PartialEq for MaybeId<T> {
}
impl<T> MaybeId<T> {
pub const NULL: MaybeId<T> = MaybeId(Id { index: u32::MAX, phantom_data: PhantomData {}, });
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!()
@ -149,7 +151,7 @@ impl<T> FreeList<T> {
FreeList {
items: Vec::new(),
free: Vec::new(),
count: 0,
_count: 0,
}
}
pub fn get(&mut self, id: &Id<T>) -> &mut T {
@ -163,7 +165,8 @@ impl<T> FreeList<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
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 {
@ -192,16 +195,15 @@ pub struct IdNode<T> {
next: MaybeId<IdNode<T>>,
}
impl<T,C> Traverse<C> for IdNode<T> where C: Allocates<Self> {
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)]
@ -227,14 +229,23 @@ impl<T> LinkedList<T> {
}
}
pub fn add(&mut self, list: &mut FreeNodeList<T>, it: T) {
let old = std::mem::replace(&mut self.first,list.make(IdNode {
let old = std::mem::replace(
&mut self.first,
list.make(IdNode {
item: it,
next: MaybeId::NULL,
}).id.maybe());
})
.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 {
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();
@ -262,32 +273,38 @@ pub type FreeNodeList<T> = FreeList<IdNode<T>>;
#[derive(Clone, Debug, Copy)]
enum State {
Cold,
Warm,
_Warm,
}
#[derive(Debug, Clone)]
struct Header<T> {
state: State,
_state: State,
it: T,
}
impl<T> Header<T> where {
impl<T> Header<T> {
fn new(it: T) -> Header<T> {
Header {
state: State::Cold,
_state: State::Cold,
it,
}
}
}
pub trait Allocates<T> where Self: Sized, T: Traverse<Self> {
fn collect();
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 collect(self);
}
impl<T> Hashes for Id<T> {