2025-08-24 02:01:24 +02:00
|
|
|
|
use serde::{Deserialize, Serialize};
|
2025-08-23 23:40:19 +02:00
|
|
|
|
use std::collections::BTreeSet;
|
|
|
|
|
|
use std::hash::Hash;
|
2025-08-24 02:01:24 +02:00
|
|
|
|
use std::fmt;
|
|
|
|
|
|
|
|
|
|
|
|
use super::translator::{IdType, Translator, PrintableWithTranslator};
|
2025-08-23 23:40:19 +02:00
|
|
|
|
|
2025-08-26 16:56:08 +02:00
|
|
|
|
|
|
|
|
|
|
/// Basic trait for all Set implementations.
|
|
|
|
|
|
/// Implement IntoIterator for &Self to have .iter() (not required directly by
|
|
|
|
|
|
/// the trait).
|
|
|
|
|
|
pub trait BasicSet
|
|
|
|
|
|
where Self: Clone + Eq + Ord + Default + Serialize + IntoIterator
|
|
|
|
|
|
+ PrintableWithTranslator,
|
|
|
|
|
|
for<'de> Self: Deserialize<'de>
|
|
|
|
|
|
{
|
|
|
|
|
|
fn is_subset(&self, other: &Self) -> bool;
|
|
|
|
|
|
fn is_disjoint(&self, other: &Self) -> bool;
|
|
|
|
|
|
fn union(&self, other: &Self) -> Self;
|
|
|
|
|
|
fn push(&mut self, other: &Self);
|
|
|
|
|
|
fn extend(&mut self, other: Option<&Self>);
|
|
|
|
|
|
fn intersection(&self, other: &Self) -> Self;
|
|
|
|
|
|
fn subtraction(&self, other: &Self) -> Self;
|
|
|
|
|
|
fn len(&self) -> usize;
|
|
|
|
|
|
fn is_empty(&self) -> bool;
|
2025-08-23 23:40:19 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-08-26 16:56:08 +02:00
|
|
|
|
pub trait ExtensionsSet {
|
|
|
|
|
|
fn iter(
|
|
|
|
|
|
&self
|
|
|
|
|
|
) -> <&Self as IntoIterator>::IntoIter
|
|
|
|
|
|
where for<'b> &'b Self: IntoIterator;
|
|
|
|
|
|
|
|
|
|
|
|
fn split<'a>(
|
|
|
|
|
|
&'a self,
|
|
|
|
|
|
trace: &'a [Self]
|
|
|
|
|
|
) -> Option<(&'a [Self], &'a [Self])>
|
|
|
|
|
|
where Self: Sized;
|
2025-08-23 23:40:19 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-08-26 16:56:08 +02:00
|
|
|
|
/// Implementations for all sets.
|
|
|
|
|
|
impl<T: BasicSet> ExtensionsSet for T {
|
|
|
|
|
|
fn iter(&self) -> <&T as IntoIterator>::IntoIter
|
|
|
|
|
|
where for<'b> &'b T: IntoIterator {
|
|
|
|
|
|
self.into_iter()
|
2025-08-23 23:40:19 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-08-26 16:56:08 +02:00
|
|
|
|
/// Returns the prefix and the loop from a trace.
|
|
|
|
|
|
fn split<'a>(
|
|
|
|
|
|
&'a self,
|
|
|
|
|
|
trace: &'a [Self]
|
|
|
|
|
|
) -> Option<(&'a [Self], &'a [Self])> {
|
|
|
|
|
|
let position = trace.iter().rposition(|x| x == self);
|
|
|
|
|
|
position.map(|pos| trace.split_at(pos))
|
2025-08-23 23:40:19 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2025-08-26 16:56:08 +02:00
|
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
/// Basic set of entities.
|
|
|
|
|
|
#[derive(Clone, Debug, Default, PartialOrd, Eq, Ord, Serialize, Deserialize)]
|
|
|
|
|
|
pub struct Set {
|
|
|
|
|
|
pub identifiers: BTreeSet<IdType>,
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
impl BasicSet for Set {
|
|
|
|
|
|
fn is_subset(&self, other: &Self) -> bool {
|
|
|
|
|
|
self.identifiers.is_subset(&other.identifiers)
|
2025-08-23 23:40:19 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-08-26 16:56:08 +02:00
|
|
|
|
fn is_disjoint(&self, other: &Self) -> bool {
|
|
|
|
|
|
self.identifiers.is_disjoint(&other.identifiers)
|
2025-08-23 23:40:19 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// returns the new set a \cup b
|
2025-08-26 16:56:08 +02:00
|
|
|
|
fn union(&self, other: &Self) -> Self {
|
|
|
|
|
|
let mut ret: Set = other.clone();
|
2025-08-23 23:40:19 +02:00
|
|
|
|
ret.identifiers.extend(self.identifiers.iter());
|
|
|
|
|
|
ret
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-08-26 16:56:08 +02:00
|
|
|
|
fn push(&mut self, b: &Self) {
|
|
|
|
|
|
self.identifiers.extend(b.iter())
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
fn extend(&mut self, other: Option<&Self>) {
|
|
|
|
|
|
if let Some(other) = other {
|
|
|
|
|
|
self.identifiers.extend(other);
|
2025-08-23 23:40:19 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// returns the new set a \cap b
|
2025-08-26 16:56:08 +02:00
|
|
|
|
fn intersection(&self, other: &Self) -> Self {
|
2025-08-23 23:40:19 +02:00
|
|
|
|
// TODO maybe find more efficient way without copy/clone
|
2025-08-26 16:56:08 +02:00
|
|
|
|
let res: BTreeSet<_> = other
|
2025-08-23 23:40:19 +02:00
|
|
|
|
.identifiers
|
|
|
|
|
|
.intersection(&self.identifiers)
|
|
|
|
|
|
.copied()
|
|
|
|
|
|
.collect();
|
|
|
|
|
|
Set { identifiers: res }
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// returns the new set a ∖ b
|
2025-08-26 16:56:08 +02:00
|
|
|
|
fn subtraction(&self, other: &Self) -> Self {
|
2025-08-23 23:40:19 +02:00
|
|
|
|
// TODO maybe find more efficient way without copy/clone
|
|
|
|
|
|
let res: BTreeSet<_> = self
|
|
|
|
|
|
.identifiers
|
2025-08-26 16:56:08 +02:00
|
|
|
|
.difference(&other.identifiers)
|
2025-08-23 23:40:19 +02:00
|
|
|
|
.copied()
|
|
|
|
|
|
.collect();
|
|
|
|
|
|
Set { identifiers: res }
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-08-26 16:56:08 +02:00
|
|
|
|
fn len(&self) -> usize {
|
2025-08-23 23:40:19 +02:00
|
|
|
|
self.identifiers.len()
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-08-26 16:56:08 +02:00
|
|
|
|
fn is_empty(&self) -> bool {
|
2025-08-23 23:40:19 +02:00
|
|
|
|
self.identifiers.is_empty()
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
impl PartialEq for Set {
|
|
|
|
|
|
fn eq(&self, other: &Self) -> bool {
|
|
|
|
|
|
self.identifiers.eq(&other.identifiers)
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
impl Hash for Set {
|
|
|
|
|
|
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
|
|
|
|
|
|
self.identifiers.hash(state)
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
impl IntoIterator for Set {
|
|
|
|
|
|
type Item = IdType;
|
|
|
|
|
|
type IntoIter = std::collections::btree_set::IntoIter<Self::Item>;
|
|
|
|
|
|
|
|
|
|
|
|
fn into_iter(self) -> Self::IntoIter {
|
|
|
|
|
|
self.identifiers.into_iter()
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-08-24 02:01:24 +02:00
|
|
|
|
|
2025-08-26 16:56:08 +02:00
|
|
|
|
impl<'a> IntoIterator for &'a Set {
|
|
|
|
|
|
type Item = &'a IdType;
|
|
|
|
|
|
type IntoIter = std::collections::btree_set::Iter<'a, IdType>;
|
|
|
|
|
|
|
|
|
|
|
|
fn into_iter(self) -> Self::IntoIter {
|
|
|
|
|
|
self.identifiers.iter()
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-08-24 02:01:24 +02:00
|
|
|
|
impl PrintableWithTranslator for Set {
|
|
|
|
|
|
fn print(
|
|
|
|
|
|
&self,
|
|
|
|
|
|
f: &mut fmt::Formatter,
|
|
|
|
|
|
translator: &Translator,
|
|
|
|
|
|
) -> fmt::Result {
|
|
|
|
|
|
write!(f, "{{")?;
|
|
|
|
|
|
let mut it = self.iter().peekable();
|
|
|
|
|
|
while let Some(el) = it.next() {
|
|
|
|
|
|
if it.peek().is_none() {
|
|
|
|
|
|
write!(f,
|
|
|
|
|
|
"{}",
|
|
|
|
|
|
translator.decode(*el).unwrap_or("Missing".into()))?;
|
|
|
|
|
|
} else {
|
|
|
|
|
|
write!(f,
|
|
|
|
|
|
"{}, ",
|
|
|
|
|
|
translator.decode(*el).unwrap_or("Missing".into()))?;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
write!(f, "}}")
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-08-26 16:56:08 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
impl<const N: usize> From<[IdType; N]> for Set {
|
|
|
|
|
|
fn from(arr: [IdType; N]) -> Self {
|
|
|
|
|
|
Set {
|
|
|
|
|
|
identifiers: BTreeSet::from(arr),
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
impl From<&[IdType]> for Set {
|
|
|
|
|
|
fn from(arr: &[IdType]) -> Self {
|
|
|
|
|
|
Set {
|
|
|
|
|
|
identifiers: BTreeSet::from_iter(arr.to_vec()),
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
impl From<Vec<IdType>> for Set {
|
|
|
|
|
|
fn from(arr: Vec<IdType>) -> Self {
|
|
|
|
|
|
Set {
|
|
|
|
|
|
identifiers: BTreeSet::from_iter(arr),
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|