Files
ReactionSystems/rsprocess/src/choices.rs

246 lines
6.9 KiB
Rust
Raw Normal View History

2025-08-29 17:59:49 +02:00
use std::fmt::Debug;
2025-10-31 16:36:28 +01:00
use std::sync::Arc;
2025-08-29 17:59:49 +02:00
use super::process::{BasicProcess, PositiveProcess, Process};
use super::set::{BasicSet, PositiveSet, Set};
use super::translator::{Formatter, PrintableWithTranslator, Translator};
2025-08-29 17:59:49 +02:00
pub trait BasicChoices
where
Self: Clone + Debug + Default + IntoIterator + PrintableWithTranslator,
{
2025-08-29 17:59:49 +02:00
type Process: BasicProcess;
fn append(&mut self, other: &mut Self);
2025-10-31 16:36:28 +01:00
fn replace(&mut self, other: Arc<Self::Process>);
2025-08-29 17:59:49 +02:00
fn shuffle(&mut self, choices: Self);
}
// -----------------------------------------------------------------------------
#[derive(Clone, Debug, Default)]
pub struct Choices {
2025-10-31 16:36:28 +01:00
context_moves: Vec<(Arc<Set>, Arc<Process>)>,
}
2025-08-29 17:59:49 +02:00
impl BasicChoices for Choices {
type Process = Process;
2025-08-29 17:59:49 +02:00
fn append(&mut self, a: &mut Self) {
self.context_moves.append(&mut a.context_moves);
}
2025-10-31 16:36:28 +01:00
fn replace(&mut self, a: Arc<Self::Process>) {
self.context_moves = self
.context_moves
.iter_mut()
2025-10-31 16:36:28 +01:00
.map(|(c1, _)| (Arc::clone(c1), Arc::clone(&a)))
.collect::<Vec<_>>();
}
2025-08-29 17:59:49 +02:00
fn shuffle(&mut self, choices: Self) {
match (
self.context_moves.is_empty(),
choices.context_moves.is_empty(),
) {
2025-09-11 02:49:14 +02:00
| (true, true) => {},
| (true, false) => self.context_moves = choices.context_moves,
| (false, true) => {},
| (false, false) => {
let mut new_self = vec![];
for item_self in &self.context_moves {
for item_choices in &choices.context_moves {
new_self.push((
2025-10-31 16:36:28 +01:00
Arc::new(item_self.0.union(&item_choices.0)),
Arc::new(item_self.1.concat(&item_choices.1)),
));
}
}
self.context_moves = new_self;
2025-09-11 02:49:14 +02:00
},
}
}
}
2025-08-29 17:59:49 +02:00
impl Choices {
2025-10-31 16:36:28 +01:00
fn iter(&self) -> std::slice::Iter<'_, (Arc<Set>, Arc<Process>)> {
self.context_moves.iter()
}
}
impl IntoIterator for Choices {
2025-10-31 16:36:28 +01:00
type Item = (Arc<Set>, Arc<Process>);
type IntoIter = std::vec::IntoIter<Self::Item>;
fn into_iter(self) -> Self::IntoIter {
self.context_moves.into_iter()
}
}
2025-10-31 16:36:28 +01:00
impl<const N: usize> From<[(Arc<Set>, Arc<Process>); N]> for Choices {
fn from(arr: [(Arc<Set>, Arc<Process>); N]) -> Self {
Choices {
context_moves: arr.to_vec(),
}
}
}
2025-10-31 16:36:28 +01:00
impl From<&[(Arc<Set>, Arc<Process>)]> for Choices {
fn from(arr: &[(Arc<Set>, Arc<Process>)]) -> Self {
Choices {
context_moves: arr.to_vec(),
}
}
}
2025-10-31 16:36:28 +01:00
impl From<Vec<(Arc<Set>, Arc<Process>)>> for Choices {
fn from(arr: Vec<(Arc<Set>, Arc<Process>)>) -> Self {
Choices { context_moves: arr }
}
}
impl PrintableWithTranslator for Choices {
2025-09-10 22:41:40 +02:00
fn print(
&self,
f: &mut std::fmt::Formatter,
translator: &Translator,
) -> std::fmt::Result {
write!(f, "[")?;
let mut it = self.iter().peekable();
while let Some(el) = it.next() {
if it.peek().is_none() {
write!(
f,
"[set: {}, process: {}]",
Formatter::from(translator, &*el.0),
Formatter::from(translator, &*el.1)
)?;
} else {
write!(
f,
"[set: {}, process: {}], ",
Formatter::from(translator, &*el.0),
Formatter::from(translator, &*el.1)
)?;
}
}
write!(f, "]")
}
}
2025-08-29 17:59:49 +02:00
// -----------------------------------------------------------------------------
#[derive(Clone, Debug, Default)]
pub struct PositiveChoices {
2025-10-31 16:36:28 +01:00
context_moves: Vec<(Arc<PositiveSet>, Arc<PositiveProcess>)>,
2025-08-29 17:59:49 +02:00
}
impl BasicChoices for PositiveChoices {
type Process = PositiveProcess;
fn append(&mut self, a: &mut Self) {
self.context_moves.append(&mut a.context_moves);
2025-08-29 17:59:49 +02:00
}
2025-10-31 16:36:28 +01:00
fn replace(&mut self, a: Arc<Self::Process>) {
self.context_moves = self
.context_moves
.iter_mut()
2025-10-31 16:36:28 +01:00
.map(|(c1, _)| (Arc::clone(c1), Arc::clone(&a)))
.collect::<Vec<_>>();
2025-08-29 17:59:49 +02:00
}
fn shuffle(&mut self, choices: Self) {
match (
self.context_moves.is_empty(),
choices.context_moves.is_empty(),
) {
2025-09-11 02:49:14 +02:00
| (true, true) => {},
| (true, false) => self.context_moves = choices.context_moves,
| (false, true) => {},
| (false, false) => {
let mut new_self = vec![];
for item_self in &self.context_moves {
for item_choices in &choices.context_moves {
new_self.push((
2025-10-31 16:36:28 +01:00
Arc::new(item_self.0.union(&item_choices.0)),
Arc::new(item_self.1.concat(&item_choices.1)),
));
}
}
self.context_moves = new_self;
2025-09-11 02:49:14 +02:00
},
}
2025-08-29 17:59:49 +02:00
}
}
impl IntoIterator for PositiveChoices {
2025-10-31 16:36:28 +01:00
type Item = (Arc<PositiveSet>, Arc<PositiveProcess>);
2025-08-29 17:59:49 +02:00
type IntoIter = std::vec::IntoIter<Self::Item>;
fn into_iter(self) -> Self::IntoIter {
self.context_moves.into_iter()
2025-08-29 17:59:49 +02:00
}
}
impl PrintableWithTranslator for PositiveChoices {
2025-09-10 22:41:40 +02:00
fn print(
&self,
f: &mut std::fmt::Formatter,
translator: &Translator,
) -> std::fmt::Result {
write!(f, "[")?;
let mut it = self.iter().peekable();
while let Some(el) = it.next() {
if it.peek().is_none() {
write!(
f,
"[set: {}, process: {}]",
Formatter::from(translator, &*el.0),
Formatter::from(translator, &*el.1)
)?;
} else {
write!(
f,
"[set: {}, process: {}], ",
Formatter::from(translator, &*el.0),
Formatter::from(translator, &*el.1)
)?;
}
}
write!(f, "]")
2025-08-29 17:59:49 +02:00
}
}
impl PositiveChoices {
2025-09-10 22:41:40 +02:00
fn iter(
&self,
2025-10-31 16:36:28 +01:00
) -> std::slice::Iter<'_, (Arc<PositiveSet>, Arc<PositiveProcess>)> {
self.context_moves.iter()
2025-08-29 17:59:49 +02:00
}
}
2025-10-31 16:36:28 +01:00
impl<const N: usize> From<[(Arc<PositiveSet>, Arc<PositiveProcess>); N]>
2025-09-10 22:41:40 +02:00
for PositiveChoices
{
2025-10-31 16:36:28 +01:00
fn from(arr: [(Arc<PositiveSet>, Arc<PositiveProcess>); N]) -> Self {
Self {
context_moves: arr.to_vec(),
}
2025-08-29 17:59:49 +02:00
}
}
2025-10-31 16:36:28 +01:00
impl From<&[(Arc<PositiveSet>, Arc<PositiveProcess>)]> for PositiveChoices {
fn from(arr: &[(Arc<PositiveSet>, Arc<PositiveProcess>)]) -> Self {
Self {
context_moves: arr.to_vec(),
}
2025-08-29 17:59:49 +02:00
}
}
2025-10-31 16:36:28 +01:00
impl From<Vec<(Arc<PositiveSet>, Arc<PositiveProcess>)>> for PositiveChoices {
fn from(arr: Vec<(Arc<PositiveSet>, Arc<PositiveProcess>)>) -> Self {
Self { context_moves: arr }
2025-08-29 17:59:49 +02:00
}
}