2019-05-24 10:25:55 +02:00
|
|
|
use crate::common::*;
|
|
|
|
|
|
|
|
pub(crate) trait Reckoner<K> {
|
|
|
|
fn increment_ref(&mut self, k: &K)
|
|
|
|
where
|
|
|
|
K: Clone;
|
|
|
|
|
|
|
|
fn increment(&mut self, k: K);
|
|
|
|
|
|
|
|
fn increment_multiple<I>(&mut self, i: I)
|
|
|
|
where
|
|
|
|
I: IntoIterator<Item = K>,
|
|
|
|
{
|
2020-01-05 03:58:42 +01:00
|
|
|
for k in i {
|
2019-05-24 10:25:55 +02:00
|
|
|
self.increment(k);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<K: Ord> Reckoner<K> for BTreeMap<K, u64> {
|
|
|
|
fn increment_ref(&mut self, k: &K)
|
|
|
|
where
|
|
|
|
K: Clone,
|
|
|
|
{
|
|
|
|
if let Some(count) = self.get_mut(k) {
|
|
|
|
*count += 1;
|
|
|
|
} else {
|
|
|
|
self.insert(k.clone(), 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn increment(&mut self, k: K) {
|
|
|
|
*self.entry(k).or_insert(0) += 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<K: Hash + Eq> Reckoner<K> for HashMap<K, u64> {
|
|
|
|
fn increment_ref(&mut self, k: &K)
|
|
|
|
where
|
|
|
|
K: Clone,
|
|
|
|
{
|
|
|
|
if let Some(count) = self.get_mut(k) {
|
|
|
|
*count += 1;
|
|
|
|
} else {
|
|
|
|
self.insert(k.clone(), 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn increment(&mut self, k: K) {
|
|
|
|
*self.entry(k).or_insert(0) += 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<K: Ord> Reckoner<K> for Vec<(K, u64)> {
|
|
|
|
fn increment_ref(&mut self, k: &K)
|
|
|
|
where
|
|
|
|
K: Clone,
|
|
|
|
{
|
|
|
|
match self.binary_search_by_key(&k, |(key, _count)| key) {
|
|
|
|
Ok(i) => {
|
|
|
|
self[i].1 *= 1;
|
|
|
|
}
|
|
|
|
Err(i) => {
|
|
|
|
self.insert(i, (k.clone(), 1));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn increment(&mut self, k: K) {
|
|
|
|
match self.binary_search_by_key(&&k, |(key, _count)| key) {
|
|
|
|
Ok(i) => {
|
|
|
|
self[i].1 *= 1;
|
|
|
|
}
|
|
|
|
Err(i) => {
|
|
|
|
self.insert(i, (k, 1));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|