-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Big rework of linker and related systems. Cont
- Loading branch information
Showing
8 changed files
with
373 additions
and
225 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,170 @@ | ||
use std::{ops::{IndexMut, Index}, marker::PhantomData, iter::Enumerate}; | ||
|
||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] | ||
pub struct UUID<IndexMarker>(usize, PhantomData<IndexMarker>); | ||
|
||
impl<IndexMarker> Default for UUID<IndexMarker> { | ||
fn default() -> Self { | ||
Self::INVALID | ||
} | ||
} | ||
|
||
impl<IndexMarker> UUID<IndexMarker> { | ||
pub const INVALID : Self = UUID(usize::MAX, PhantomData); | ||
} | ||
|
||
#[derive(Default)] | ||
pub struct ArenaAllocator<T, IndexMarker> { | ||
data : Vec<Option<T>>, | ||
free_slots : Vec<usize>, | ||
_ph : PhantomData<IndexMarker> | ||
} | ||
|
||
impl<T, IndexMarker> ArenaAllocator<T, IndexMarker> { | ||
pub fn new() -> Self { | ||
Self{data : Vec::new(), free_slots : Vec::new(), _ph : PhantomData} | ||
} | ||
pub fn alloc(&mut self, v : T) -> UUID<IndexMarker> { | ||
UUID(if let Some(empty_slot) = self.free_slots.pop() { | ||
assert!(self.data[empty_slot].is_none()); | ||
self.data[empty_slot] = Some(v); | ||
empty_slot | ||
} else { | ||
let l = self.data.len(); | ||
self.data.push(Some(v)); | ||
l | ||
}, PhantomData) | ||
} | ||
pub fn reserve(&mut self) -> UUID<IndexMarker> { | ||
UUID(if let Some(empty_slot) = self.free_slots.pop() { | ||
assert!(self.data[empty_slot].is_none()); | ||
self.data[empty_slot] = None; | ||
empty_slot | ||
} else { | ||
let l = self.data.len(); | ||
self.data.push(None); | ||
l | ||
}, PhantomData) | ||
} | ||
pub fn alloc_reservation(&mut self, UUID(uuid, _) : UUID<IndexMarker>, v : T) { | ||
assert!(self.data[uuid].is_none()); | ||
self.data[uuid] = Some(v); | ||
} | ||
pub fn free(&mut self, UUID(uuid, _) : UUID<IndexMarker>) -> T { | ||
self.free_slots.push(uuid); | ||
std::mem::replace(&mut self.data[uuid], None).unwrap() | ||
} | ||
} | ||
|
||
impl<T, IndexMarker> Index<UUID<IndexMarker>> for ArenaAllocator<T, IndexMarker> { | ||
type Output = T; | ||
|
||
fn index(&self, UUID(uuid, _): UUID<IndexMarker>) -> &Self::Output { | ||
assert!(self.data[uuid].is_some()); | ||
self.data[uuid].as_ref().unwrap() | ||
} | ||
} | ||
|
||
impl<T, IndexMarker> IndexMut<UUID<IndexMarker>> for ArenaAllocator<T, IndexMarker> { | ||
fn index_mut(&mut self, UUID(uuid, _): UUID<IndexMarker>) -> &mut Self::Output { | ||
assert!(self.data[uuid].is_some()); | ||
self.data[uuid].as_mut().unwrap() | ||
} | ||
} | ||
|
||
pub struct ArenaIterator<'a, T, IndexMarker> { | ||
it: Enumerate<std::slice::Iter<'a, Option<T>>>, | ||
_ph : PhantomData<IndexMarker> | ||
} | ||
|
||
impl<'a, T, IndexMarker> Iterator for ArenaIterator<'a, T, IndexMarker> { | ||
type Item = (UUID<IndexMarker>, &'a T); | ||
|
||
fn next(&mut self) -> Option<Self::Item> { | ||
loop { | ||
match self.it.next() { | ||
None => { | ||
return None; | ||
}, | ||
Some((_pos, None)) => {}, | ||
Some((pos, Some(val))) => { | ||
return Some((UUID(pos, PhantomData), val)); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
pub struct ArenaIteratorMut<'a, T, IndexMarker> { | ||
it: Enumerate<std::slice::IterMut<'a, Option<T>>>, | ||
_ph : PhantomData<IndexMarker> | ||
} | ||
|
||
impl<'a, T, IndexMarker> Iterator for ArenaIteratorMut<'a, T, IndexMarker> { | ||
type Item = (UUID<IndexMarker>, &'a mut T); | ||
|
||
fn next(&mut self) -> Option<Self::Item> { | ||
loop { | ||
match self.it.next() { | ||
None => { | ||
return None; | ||
}, | ||
Some((_pos, None)) => {}, | ||
Some((pos, Some(val))) => { | ||
return Some((UUID(pos, PhantomData), val)); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
impl<'a, T, IndexMarker> IntoIterator for &'a ArenaAllocator<T, IndexMarker> { | ||
type Item = (UUID<IndexMarker>, &'a T); | ||
|
||
type IntoIter = ArenaIterator<'a, T, IndexMarker>; | ||
|
||
fn into_iter(self) -> Self::IntoIter { | ||
ArenaIterator{it : self.data.iter().enumerate(), _ph : PhantomData} | ||
} | ||
} | ||
|
||
impl<'a, T, IndexMarker> IntoIterator for &'a mut ArenaAllocator<T, IndexMarker> { | ||
type Item = (UUID<IndexMarker>, &'a mut T); | ||
|
||
type IntoIter = ArenaIteratorMut<'a, T, IndexMarker>; | ||
|
||
fn into_iter(self) -> Self::IntoIter { | ||
ArenaIteratorMut{it : self.data.iter_mut().enumerate(), _ph : PhantomData} | ||
} | ||
} | ||
|
||
pub struct ArenaVector<T : Default, IndexMarker> { | ||
data : Vec<T>, | ||
_ph : PhantomData<IndexMarker> | ||
} | ||
|
||
impl<T : Default, IndexMarker> ArenaVector<T, IndexMarker> { | ||
pub fn insert(&mut self, UUID(uuid, _) : UUID<IndexMarker>, value : T) { | ||
if uuid >= self.data.len() { | ||
self.data.resize_with(uuid+1, Default::default); | ||
} | ||
self.data[uuid] = value; | ||
} | ||
pub fn remove(&mut self, UUID(uuid, _) : UUID<IndexMarker>) { | ||
self.data[uuid] = Default::default(); | ||
} | ||
} | ||
|
||
impl<T : Default, IndexMarker> Index<UUID<IndexMarker>> for ArenaVector<T, IndexMarker> { | ||
type Output = T; | ||
|
||
fn index(&self, UUID(uuid, _): UUID<IndexMarker>) -> &Self::Output { | ||
&self.data[uuid] | ||
} | ||
} | ||
|
||
impl<T : Default, IndexMarker> IndexMut<UUID<IndexMarker>> for ArenaVector<T, IndexMarker> { | ||
fn index_mut(&mut self, UUID(uuid, _): UUID<IndexMarker>) -> &mut Self::Output { | ||
&mut self.data[uuid] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.