1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
use std::borrow::Borrow;
use std::io;
use std::ops::AddAssign;
use bytehash::ByteHash;
use num::{One, Zero};
use super::Associative;
use crate::{Compound, Content, Sink, Source};
pub trait Counter: AddAssign + Copy + Zero + One {}
impl<T> Counter for T where T: AddAssign + Copy + Zero + One {}
#[derive(PartialEq, Eq, Clone)]
pub struct Cardinality<T>(T);
impl<T> Associative for Cardinality<T>
where
T: Counter,
{
fn op(&mut self, b: &Self) {
self.0 += b.0;
}
}
impl<Anything, U> From<&Anything> for Cardinality<U>
where
U: Counter,
{
fn from(_: &Anything) -> Self {
Cardinality(U::one())
}
}
impl<H, U> Content<H> for Cardinality<U>
where
H: ByteHash,
U: Content<H> + Counter,
{
fn persist(&mut self, sink: &mut Sink<H>) -> io::Result<()> {
self.0.persist(sink)
}
fn restore(source: &mut Source<H>) -> io::Result<Self> {
Ok(Cardinality(U::restore(source)?))
}
}
pub trait Count<U, H> {
fn count(&self) -> U;
}
impl<U, C, H> Count<U, H> for C
where
U: Counter,
H: ByteHash,
C: Compound<H>,
C::Annotation: Borrow<Cardinality<U>>,
{
fn count(&self) -> U {
self.annotation()
.map(|ann| ann.borrow().0)
.unwrap_or_else(U::zero)
}
}