From 0967a4e3fe51efe2895a2bc5f047e9af8fb3efca Mon Sep 17 00:00:00 2001 From: Botahamec Date: Sat, 29 Jul 2023 22:02:07 -0400 Subject: Add cset operations --- src/csets.rs | 69 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 68 insertions(+), 1 deletion(-) diff --git a/src/csets.rs b/src/csets.rs index 8c6767c..0f4b4d0 100644 --- a/src/csets.rs +++ b/src/csets.rs @@ -1,7 +1,31 @@ use std::collections::HashSet; -pub trait CharacterSet { +pub trait CharacterSet: Sized { fn contains(&self, ch: char) -> bool; + + fn union(self, other: Other) -> CharacterSetUnion { + CharacterSetUnion { + first: self, + second: other, + } + } + + fn intersection( + self, + other: Other, + ) -> CharacterSetIntersection { + CharacterSetIntersection { + first: self, + second: other, + } + } + + fn difference(self, other: Other) -> CharacterSetDifference { + CharacterSetDifference { + first: self, + second: other, + } + } } pub struct AnyCharacter; @@ -75,3 +99,46 @@ impl CharacterSet for HashSet { self.contains(&ch) } } + +pub struct CharacterSetUnion { + first: A, + second: B, +} + +impl CharacterSet for CharacterSetUnion { + fn contains(&self, ch: char) -> bool { + self.first.contains(ch) || self.second.contains(ch) + } +} + +pub struct CharacterSetIntersection { + first: A, + second: B, +} + +impl CharacterSet for CharacterSetIntersection { + fn contains(&self, ch: char) -> bool { + self.first.contains(ch) && self.second.contains(ch) + } +} + +pub struct CharacterSetDifference { + first: A, + second: B, +} + +impl CharacterSet for CharacterSetDifference { + fn contains(&self, ch: char) -> bool { + self.first.contains(ch) && !self.second.contains(ch) + } +} + +pub struct CharacterSetComplement { + inner: Inner, +} + +impl CharacterSet for CharacterSetComplement { + fn contains(&self, ch: char) -> bool { + !self.inner.contains(ch) + } +} -- cgit v1.2.3