Guess I should put this under version control LOL
This commit is contained in:
commit
2035318460
73 changed files with 8836 additions and 0 deletions
36
styles/src/stretch/id.rs
Normal file
36
styles/src/stretch/id.rs
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
//! Identifier for a Node
|
||||
//!
|
||||
//!
|
||||
|
||||
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
|
||||
pub(crate) struct Id {
|
||||
id: u32,
|
||||
generation: u32,
|
||||
}
|
||||
|
||||
pub(crate) struct Allocator {
|
||||
new_id: u32,
|
||||
free_ids: Vec<Id>,
|
||||
}
|
||||
|
||||
impl Allocator {
|
||||
pub fn new() -> Self {
|
||||
Allocator { new_id: 0, free_ids: Vec::new() }
|
||||
}
|
||||
|
||||
pub fn allocate(&mut self) -> Id {
|
||||
// TODO: better balancing
|
||||
match self.free_ids.pop() {
|
||||
Some(id) => Id { id: id.id, generation: id.generation + 1 },
|
||||
None => {
|
||||
let id = self.new_id;
|
||||
self.new_id += 1;
|
||||
Id { id, generation: 0 }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn free(&mut self, ids: &[Id]) {
|
||||
self.free_ids.extend(ids);
|
||||
}
|
||||
}
|
||||
Reference in a new issue