Created
May 31, 2016 19:22
-
-
Save Luminarys/773774d79872693e2d82fbe3109f2748 to your computer and use it in GitHub Desktop.
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
#[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