2018-12-02 13:11:02 +01:00
|
|
|
use hashbrown::HashMap;
|
2019-10-18 13:05:28 +02:00
|
|
|
use std::hash::Hash;
|
2018-12-02 13:11:02 +01:00
|
|
|
|
2018-11-28 17:12:24 +01:00
|
|
|
pub struct DistinctMap<K> {
|
|
|
|
inner: HashMap<K, usize>,
|
|
|
|
limit: usize,
|
|
|
|
len: usize,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<K: Hash + Eq> DistinctMap<K> {
|
|
|
|
pub fn new(limit: usize) -> Self {
|
|
|
|
DistinctMap {
|
|
|
|
inner: HashMap::new(),
|
2019-05-22 11:00:58 +02:00
|
|
|
limit,
|
2018-11-28 17:12:24 +01:00
|
|
|
len: 0,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-16 14:21:41 +01:00
|
|
|
pub fn len(&self) -> usize {
|
|
|
|
self.len
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct BufferedDistinctMap<'a, K> {
|
|
|
|
internal: &'a mut DistinctMap<K>,
|
|
|
|
inner: HashMap<K, usize>,
|
|
|
|
len: usize,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, K: Hash + Eq> BufferedDistinctMap<'a, K> {
|
|
|
|
pub fn new(internal: &'a mut DistinctMap<K>) -> BufferedDistinctMap<'a, K> {
|
|
|
|
BufferedDistinctMap {
|
2019-05-22 11:00:58 +02:00
|
|
|
internal,
|
2018-12-16 14:21:41 +01:00
|
|
|
inner: HashMap::new(),
|
|
|
|
len: 0,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-13 11:54:09 +01:00
|
|
|
pub fn register(&mut self, key: K) -> bool {
|
2018-12-16 14:21:41 +01:00
|
|
|
let internal_seen = self.internal.inner.get(&key).unwrap_or(&0);
|
|
|
|
let inner_seen = self.inner.entry(key).or_insert(0);
|
|
|
|
let seen = *internal_seen + *inner_seen;
|
|
|
|
|
|
|
|
if seen < self.internal.limit {
|
|
|
|
*inner_seen += 1;
|
2018-11-28 17:12:24 +01:00
|
|
|
self.len += 1;
|
|
|
|
true
|
|
|
|
} else {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-13 11:54:09 +01:00
|
|
|
pub fn register_without_key(&mut self) -> bool {
|
2018-11-28 17:12:24 +01:00
|
|
|
self.len += 1;
|
|
|
|
true
|
|
|
|
}
|
|
|
|
|
2018-12-16 14:21:41 +01:00
|
|
|
pub fn transfert_to_internal(&mut self) {
|
|
|
|
for (k, v) in self.inner.drain() {
|
|
|
|
let value = self.internal.inner.entry(k).or_insert(0);
|
|
|
|
*value += v;
|
|
|
|
}
|
|
|
|
|
|
|
|
self.internal.len += self.len;
|
|
|
|
self.len = 0;
|
|
|
|
}
|
|
|
|
|
2018-11-28 17:12:24 +01:00
|
|
|
pub fn len(&self) -> usize {
|
2018-12-16 14:21:41 +01:00
|
|
|
self.internal.len() + self.len
|
2018-11-28 17:12:24 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn easy_distinct_map() {
|
|
|
|
let mut map = DistinctMap::new(2);
|
2018-12-16 14:21:41 +01:00
|
|
|
let mut buffered = BufferedDistinctMap::new(&mut map);
|
|
|
|
|
2018-11-28 17:12:24 +01:00
|
|
|
for x in &[1, 1, 1, 2, 3, 4, 5, 6, 6, 6, 6, 6] {
|
2018-12-16 14:21:41 +01:00
|
|
|
buffered.register(x);
|
2018-11-28 17:12:24 +01:00
|
|
|
}
|
2018-12-16 14:21:41 +01:00
|
|
|
buffered.transfert_to_internal();
|
2018-11-28 17:12:24 +01:00
|
|
|
assert_eq!(map.len(), 8);
|
|
|
|
|
|
|
|
let mut map = DistinctMap::new(2);
|
2018-12-16 14:21:41 +01:00
|
|
|
let mut buffered = BufferedDistinctMap::new(&mut map);
|
|
|
|
assert_eq!(buffered.register(1), true);
|
|
|
|
assert_eq!(buffered.register(1), true);
|
|
|
|
assert_eq!(buffered.register(1), false);
|
|
|
|
assert_eq!(buffered.register(1), false);
|
2018-11-28 17:12:24 +01:00
|
|
|
|
2018-12-16 14:21:41 +01:00
|
|
|
assert_eq!(buffered.register(2), true);
|
|
|
|
assert_eq!(buffered.register(3), true);
|
|
|
|
assert_eq!(buffered.register(2), true);
|
|
|
|
assert_eq!(buffered.register(2), false);
|
2018-11-28 17:12:24 +01:00
|
|
|
|
2018-12-16 14:21:41 +01:00
|
|
|
buffered.transfert_to_internal();
|
2018-11-28 17:12:24 +01:00
|
|
|
assert_eq!(map.len(), 5);
|
|
|
|
}
|
|
|
|
}
|