Guess I should put this under version control LOL

This commit is contained in:
Ryan McGrath 2019-05-23 22:11:07 -07:00
commit 2035318460
No known key found for this signature in database
GPG key ID: 811674B62B666830
73 changed files with 8836 additions and 0 deletions

36
styles/src/stretch/id.rs Normal file
View 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);
}
}