haar_lib/algebra/
parenthesis.rs1pub use crate::algebra::traits::*;
6use crate::impl_algebra;
7
8#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
10pub struct ParenSeq {
11 pub close: u64,
13 pub open: u64,
15}
16
17impl ParenSeq {
18 pub fn empty() -> Self {
20 Self { close: 0, open: 0 }
21 }
22
23 pub fn is_correct(self) -> bool {
25 self.close == 0 && self.open == 0
26 }
27
28 pub fn open(n: u64) -> Self {
30 Self { close: 0, open: n }
31 }
32
33 pub fn close(n: u64) -> Self {
35 Self { close: n, open: 0 }
36 }
37
38 pub fn concat(self, right: Self) -> Self {
40 let Self { close: a, open: b } = self;
41 let Self { close: c, open: d } = right;
42
43 Self {
44 close: a + c.saturating_sub(b),
45 open: d + b.saturating_sub(c),
46 }
47 }
48}
49
50#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
52pub struct Composition;
53
54impl_algebra!(Composition; set: ParenSeq; op: |_, a: ParenSeq, b| a.concat(b);
55 id: |_| ParenSeq::empty(); assoc;
56);