mirror of
https://github.com/meilisearch/meilisearch.git
synced 2024-11-24 02:55:06 +08:00
67 lines
1.4 KiB
Rust
67 lines
1.4 KiB
Rust
use std::hash::Hash;
|
|
|
|
use hashbrown::HashMap;
|
|
|
|
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(),
|
|
limit: limit,
|
|
len: 0,
|
|
}
|
|
}
|
|
|
|
pub fn register(&mut self, key: K) -> bool {
|
|
let seen = self.inner.entry(key).or_insert(0);
|
|
if *seen < self.limit {
|
|
*seen += 1;
|
|
self.len += 1;
|
|
true
|
|
} else {
|
|
false
|
|
}
|
|
}
|
|
|
|
pub fn register_without_key(&mut self) -> bool {
|
|
self.len += 1;
|
|
true
|
|
}
|
|
|
|
pub fn len(&self) -> usize {
|
|
self.len
|
|
}
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
|
|
#[test]
|
|
fn easy_distinct_map() {
|
|
let mut map = DistinctMap::new(2);
|
|
for x in &[1, 1, 1, 2, 3, 4, 5, 6, 6, 6, 6, 6] {
|
|
map.register(x);
|
|
}
|
|
assert_eq!(map.len(), 8);
|
|
|
|
let mut map = DistinctMap::new(2);
|
|
assert_eq!(map.register(1), true);
|
|
assert_eq!(map.register(1), true);
|
|
assert_eq!(map.register(1), false);
|
|
assert_eq!(map.register(1), false);
|
|
|
|
assert_eq!(map.register(2), true);
|
|
assert_eq!(map.register(3), true);
|
|
assert_eq!(map.register(2), true);
|
|
assert_eq!(map.register(2), false);
|
|
|
|
assert_eq!(map.len(), 5);
|
|
}
|
|
}
|