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 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
use crate::KTuple;
//use log::trace;
use std::collections::HashSet;
use std::fmt::{Debug, Display, Error, Formatter};
///
/// A set type consisting of terminal strings (called k-tuples)
///
#[derive(Clone, Default, Eq)]
pub struct KTuples(pub HashSet<KTuple>, usize, bool);
impl KTuples {
pub fn new(k: usize) -> Self {
Self(HashSet::new(), k, false)
}
pub fn of(tuples: &[KTuple], k: usize) -> Self {
let mut tuples = Self(tuples.iter().cloned().collect(), k, false);
tuples.update_completeness();
tuples
}
pub fn insert(&mut self, tuple: KTuple) {
self.2 &= tuple.is_k_complete();
self.0.insert(tuple);
}
pub fn remove(&mut self, tuple: &KTuple) {
self.0.remove(tuple);
}
pub fn append(&mut self, other: &mut Self) -> bool {
let count = self.0.len();
for t in other.0.drain() {
self.insert(t);
}
count != self.0.len()
}
pub fn union(&self, other: &Self) -> Self {
let mut tuples = Self(
self.0.union(&other.0).cloned().collect::<HashSet<KTuple>>(),
self.1,
false,
);
tuples.update_completeness();
tuples
}
pub fn len(&self) -> usize {
self.0.len()
}
pub fn is_empty(&self) -> bool {
self.0.is_empty()
}
pub fn is_disjoint(&self, other: &Self) -> bool {
self.0.is_disjoint(&other.0)
}
pub fn eps(k: usize) -> Self {
let mut set = HashSet::new();
set.insert(KTuple::eps(k));
Self(set, k, false)
}
pub fn end(k: usize) -> Self {
let mut set = HashSet::new();
set.insert(KTuple::end(k));
Self(set, k, true)
}
///
/// Creates a new object from a slice of KTuple objects.
///
/// ```
/// use parol::{KTuple, KTuples, CompiledTerminal};
/// use parol::analysis::k_tuple::TerminalMappings;
///
/// {
/// let tuples1 = KTuples::of(&vec![KTuple::of(vec![CompiledTerminal::eps()], 1)], 1);
/// let tuples2 = KTuples::of(&vec![KTuple::of(vec![CompiledTerminal::eps()], 1)], 1);
/// let result = tuples1.k_concat(&tuples2, 1);
/// let expected = KTuples::of(&vec![KTuple::of(vec![CompiledTerminal::eps()], 1)], 1);
/// assert_eq!(expected, result, "[ε] + [ε] = [ε]");
/// }
/// {
/// let tuples1 = KTuples::of(&vec![KTuple::of(vec![CompiledTerminal(1)], 1)], 1);
/// let tuples2 = KTuples::of(&vec![KTuple::of(vec![CompiledTerminal::eps()], 1)], 1);
/// let result = tuples1.k_concat(&tuples2, 1);
/// let expected = KTuples::of(&vec![KTuple::of(vec![CompiledTerminal(1)], 1)], 1);
/// assert_eq!(expected, result, "[a] + [ε] = [a]");
/// }
/// {
/// let tuples1 = KTuples::of(&vec![KTuple::of(vec![CompiledTerminal::eps()], 1)], 1);
/// let tuples2 = KTuples::of(&vec![KTuple::of(vec![CompiledTerminal(1)], 1)], 1);
/// let result = tuples1.k_concat(&tuples2, 1);
/// let expected = KTuples::of(&vec![KTuple::of(vec![CompiledTerminal(1)], 1)], 1);
/// assert_eq!(expected, result, "[ε] + [a] = [a]");
/// }
/// {
/// let tuples1 = KTuples::of(&vec![KTuple::of(vec![CompiledTerminal(1)], 1)], 1);
/// let tuples2 = KTuples::of(&vec![KTuple::of(vec![CompiledTerminal(2)], 1)], 1);
/// let result = tuples1.k_concat(&tuples2, 1);
/// let expected = KTuples::of(&vec![KTuple::of(vec![CompiledTerminal(1)], 1)], 1);
/// assert_eq!(expected, result, "1: [a] + [b] = [a]");
/// }
/// {
/// let tuples1 = KTuples::of(&vec![KTuple::of(vec![CompiledTerminal(1)], 2)], 2);
/// let tuples2 = KTuples::of(&vec![KTuple::of(vec![CompiledTerminal(2)], 2)], 2);
/// let result = tuples1.k_concat(&tuples2, 2);
/// let expected = KTuples::of(&vec![KTuple::of(vec![CompiledTerminal(1), CompiledTerminal(2)], 2)], 2);
/// assert_eq!(expected, result, "2: [a] + [b] = [ab]");
/// }
///
/// ```
pub fn k_concat(mut self, other: &Self, k: usize) -> Self {
// trace!("KTuples::k_concat {} with {} at k={}", self, other, k);
if !self.2 {
let mut to_remove = Vec::<KTuple>::with_capacity(self.0.len()); // Maximum possible size
let mut to_insert = HashSet::<KTuple>::with_capacity(self.0.len()); // Start size
for i in &self.0 {
if !i.is_k_complete() {
to_remove.push(i.clone());
for j in &other.0 {
to_insert.insert(i.clone().k_concat(j, k));
}
}
}
for i in &to_remove {
self.remove(i);
}
for i in to_insert {
self.insert(i);
}
}
// trace!("KTuples::k_concat => {}", result);
self
}
pub fn to_string(&self, terminals: &[String]) -> String {
format!(
"{{{}}}(k={})",
self.sorted()
.iter()
.map(|t| t.to_string(terminals))
.collect::<Vec<String>>()
.join(", "),
self.1
)
}
pub fn set_k(mut self, k: usize) -> Self {
self.0 = self.0.drain().map(|t| t.set_k(k)).collect();
if k > self.1 {
self.2 = false;
} else {
self.update_completeness();
}
self.1 = k;
self
}
pub fn sorted(&self) -> Vec<KTuple> {
let mut sorted_k_tuples: Vec<KTuple> = self.0.iter().cloned().collect();
sorted_k_tuples.sort_by(|a, b| a.partial_cmp(b).unwrap());
sorted_k_tuples
}
fn update_completeness(&mut self) {
self.2 = self.0.iter().all(|t| t.is_k_complete());
}
}
impl Debug for KTuples {
fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error> {
// Use the implementation of Display
write!(f, "{}", format!("{}", self))
}
}
impl Display for KTuples {
fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error> {
write!(
f,
"{{{}}}(k={})",
self.0
.iter()
.map(|e| format!("{}", e))
.collect::<Vec<String>>()
.join(", "),
self.1
)
}
}
impl PartialEq for KTuples {
fn eq(&self, other: &Self) -> bool {
self.0.eq(&other.0)
}
}