Working positive rs

This commit is contained in:
elvis
2025-08-29 17:59:49 +02:00
parent 5812c75e37
commit 7dcdc3a727
13 changed files with 1489 additions and 372 deletions

View File

@ -1,33 +1,34 @@
use std::fmt::Debug;
use std::rc::Rc;
use super::process::Process;
use super::set::{BasicSet, Set};
use super::process::{BasicProcess, PositiveProcess, Process};
use super::set::{BasicSet, PositiveSet, Set};
use super::translator::{Translator, PrintableWithTranslator, Formatter};
#[derive(Clone, Debug)]
pub trait BasicChoices
where Self: Clone + Debug + Default + IntoIterator + PrintableWithTranslator {
type Process: BasicProcess;
fn append(&mut self, other: &mut Self);
fn replace(&mut self, other: Rc<Self::Process>);
fn shuffle(&mut self, choices: Self);
}
// -----------------------------------------------------------------------------
#[derive(Clone, Debug, Default)]
pub struct Choices {
context_moves: Vec<(Rc<Set>, Rc<Process>)>,
}
impl Choices {
pub fn new() -> Self {
Choices {
context_moves: vec![],
}
}
impl BasicChoices for Choices {
type Process = Process;
pub fn new_not_empty() -> Self {
Choices {
context_moves: vec![(Rc::new(Set::default()),
Rc::new(Process::Nill))],
}
}
pub fn append(&mut self, a: &mut Choices) {
fn append(&mut self, a: &mut Self) {
self.context_moves.append(&mut a.context_moves);
}
pub fn replace(&mut self, a: Rc<Process>) {
fn replace(&mut self, a: Rc<Self::Process>) {
self.context_moves = self
.context_moves
.iter_mut()
@ -35,7 +36,7 @@ impl Choices {
.collect::<Vec<_>>();
}
pub fn shuffle(&mut self, choices: Choices) {
fn shuffle(&mut self, choices: Self) {
match (
self.context_moves.is_empty(),
choices.context_moves.is_empty(),
@ -57,15 +58,11 @@ impl Choices {
}
}
}
pub fn iter(&self) -> std::slice::Iter<'_, (Rc<Set>, Rc<Process>)> {
self.context_moves.iter()
}
}
impl Default for Choices {
fn default() -> Self {
Self::new()
impl Choices {
fn iter(&self) -> std::slice::Iter<'_, (Rc<Set>, Rc<Process>)> {
self.context_moves.iter()
}
}
@ -125,3 +122,109 @@ impl PrintableWithTranslator for Choices {
write!(f, "]")
}
}
// -----------------------------------------------------------------------------
#[derive(Clone, Debug, Default)]
pub struct PositiveChoices {
context_moves: Vec<(Rc<PositiveSet>, Rc<PositiveProcess>)>,
}
impl BasicChoices for PositiveChoices {
type Process = PositiveProcess;
fn append(&mut self, a: &mut Self) {
self.context_moves.append(&mut a.context_moves);
}
fn replace(&mut self, a: Rc<Self::Process>) {
self.context_moves = self
.context_moves
.iter_mut()
.map(|(c1, _)| (Rc::clone(c1), Rc::clone(&a)))
.collect::<Vec<_>>();
}
fn shuffle(&mut self, choices: Self) {
match (
self.context_moves.is_empty(),
choices.context_moves.is_empty(),
) {
(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((
Rc::new(item_self.0.union(&item_choices.0)),
Rc::new(item_self.1.concat(&item_choices.1)),
));
}
}
self.context_moves = new_self;
}
}
}
}
impl IntoIterator for PositiveChoices {
type Item = (Rc<PositiveSet>, Rc<PositiveProcess>);
type IntoIter = std::vec::IntoIter<Self::Item>;
fn into_iter(self) -> Self::IntoIter {
self.context_moves.into_iter()
}
}
impl PrintableWithTranslator for PositiveChoices {
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, "]")
}
}
impl PositiveChoices {
fn iter(&self)
-> std::slice::Iter<'_, (Rc<PositiveSet>, Rc<PositiveProcess>)> {
self.context_moves.iter()
}
}
impl<const N: usize> From<[(Rc<PositiveSet>, Rc<PositiveProcess>); N]> for PositiveChoices {
fn from(arr: [(Rc<PositiveSet>, Rc<PositiveProcess>); N]) -> Self {
Self { context_moves: arr.to_vec() }
}
}
impl From<&[(Rc<PositiveSet>, Rc<PositiveProcess>)]> for PositiveChoices {
fn from(arr: &[(Rc<PositiveSet>, Rc<PositiveProcess>)]) -> Self {
Self { context_moves: arr.to_vec() }
}
}
impl From<Vec<(Rc<PositiveSet>, Rc<PositiveProcess>)>> for PositiveChoices {
fn from(arr: Vec<(Rc<PositiveSet>, Rc<PositiveProcess>)>) -> Self {
Self { context_moves: arr }
}
}