blob: 9095888d564887cd5c6e2c66c01406d11c2f81ad (
plain)
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
|
//! This module implements the "counting semiring", which counts the
//! number of ways a sentence can be derived by a grammar.
use super::*;
/// The counting semiring is a thin wrapper around the natural numbers
/// with the usual addition and multiplication.
#[derive(Copy, Clone, Debug, Ord, PartialOrd, Hash, Eq, PartialEq)]
pub struct Count(usize);
impl Count {
/// Wrap the count in the wrapper.
pub fn new(count: usize) -> Self {
Self(count)
}
/// Extract the count out.
pub fn value(&self) -> usize {
self.0
}
}
impl Semiring for Count {
fn zero() -> Self {
Self::new(0)
}
fn one() -> Self {
Self::new(1)
}
fn add(&mut self, other: &Self) {
self.0 = self.value() + other.value();
}
fn mul(&mut self, other: &Self) {
self.0 = self.value() * other.value();
}
fn valuation(_pos: usize, _grammar: &Grammar) -> Self {
Self::new(1)
}
}
|