Skip to content

Instantly share code, notes, and snippets.

@Luminarys
Created May 31, 2016 19:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Luminarys/773774d79872693e2d82fbe3109f2748 to your computer and use it in GitHub Desktop.
Save Luminarys/773774d79872693e2d82fbe3109f2748 to your computer and use it in GitHub Desktop.
#[derive(Default)]
pub struct Registry {
counter: usize,
free: Vec<usize>,
reclaimed: Vec<usize>,
reclaimable: bool,
}
impl Registry {
pub fn new() -> Registry {
Registry {
counter: 1,
free: vec![],
reclaimed: vec![],
reclaimable: true,
}
}
pub fn no_reclaim(&mut self) {
self.reclaim();
self.reclaimable = false;
}
pub fn get_id(&mut self) -> usize {
match self.free.pop() {
Some(num) => num,
None => {
self.counter += 1;
self.counter - 1
}
}
}
pub fn return_id(&mut self, id: usize) {
if self.reclaimable {
self.reclaimed.push(id);
} else {
self.free.push(id);
}
}
pub fn reclaim(&mut self) {
self.free.append(&mut self.reclaimed);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment