diff options
author | Till Hoeppner | 2015-07-29 00:01:46 +0200 |
---|---|---|
committer | Till Hoeppner | 2015-07-29 00:01:46 +0200 |
commit | 09f6509050f24e54c9859e0905e8b6731d91f39f (patch) | |
tree | 1916aaf8e8bb6d978000f4233bbe690228256ef7 /src/ageset.rs | |
parent | 434048f7252c2490f04866c9a08eff6eb1688dc3 (diff) | |
download | ilc-09f6509050f24e54c9859e0905e8b6731d91f39f.tar.gz ilc-09f6509050f24e54c9859e0905e8b6731d91f39f.tar.xz ilc-09f6509050f24e54c9859e0905e8b6731d91f39f.zip |
implement sort and dedup of logs
Diffstat (limited to 'src/ageset.rs')
-rw-r--r-- | src/ageset.rs | 41 |
1 files changed, 41 insertions, 0 deletions
diff --git a/src/ageset.rs b/src/ageset.rs new file mode 100644 index 0000000..084afba --- /dev/null +++ b/src/ageset.rs @@ -0,0 +1,41 @@ +use std::collections::HashSet; +use std::hash::Hash; + +use blist::BList; + +/// So... this is a rather weird thing. +/// It allows to semi-efficiently check the oldest (earliest insertion) +/// elements for certain criteria and remove them in the order of insertion +/// if the criteria is met. +pub struct AgeSet<T> { + fifo: BList<T>, + set: HashSet<T> +} + +impl<T> AgeSet<T> where T: Eq + Hash + Clone { + pub fn new() -> Self { + AgeSet { + fifo: BList::new(), + set: HashSet::new() + } + } + + pub fn contains(&self, t: &T) -> bool { + self.set.contains(t) + } + + pub fn prune<F>(&mut self, kill: F) where F: Fn(&T) -> bool { + while let Some(ref e) = self.fifo.front().map(T::clone) { + if kill(&e) { + let removed = self.fifo.pop_front().unwrap(); + self.set.remove(&e); + assert!(*e == removed); + } else { break } + } + } + + pub fn push(&mut self, t: T) { + self.fifo.push_back(t.clone()); + self.set.insert(t); + } +} |