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
use crate::states::{State, States};
use crate::turing::Tape;
use crate::{Scope, Symbolic};
use serde::{Deserialize, Serialize};
#[derive(Clone, Debug, Default, Deserialize, Eq, Hash, Ord, PartialEq, PartialOrd, Serialize)]
pub struct Operator<S: Symbolic = String> {
index: usize,
pub(crate) state: State<States>,
tape: Tape<S>,
}
impl<S: Symbolic> Scope<S> for Operator<S> {
fn new(index: usize, state: State<States>, tape: Tape<S>) -> Self {
Self { index, state, tape }
}
fn insert(&mut self, elem: S) {
self.tape.insert(self.position(), elem);
}
fn position(&self) -> usize {
self.index
}
fn set_position(&mut self, index: usize) {
self.index = index;
}
fn set_state(&mut self, state: State<States>) {
self.state = state;
}
fn set_symbol(&mut self, elem: S) {
self.tape.set(self.position(), elem);
}
fn state(&self) -> &State<States> {
&self.state
}
fn tape(&self) -> &Tape<S> {
&self.tape
}
}
impl<S: Ord + Symbolic> std::fmt::Display for Operator<S> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"{}, {}, {:?}",
self.index,
self.state.to_string(),
self.tape
)
}
}
impl<S: Symbolic> TryFrom<(usize, State<States>, Tape<S>)> for Operator<S> {
type Error = String;
fn try_from(d: (usize, State<States>, Tape<S>)) -> Result<Self, Self::Error> {
if d.0 > d.2.len() {
return Err("Starting index is out of bounds...".to_string());
}
Ok(Self::new(d.0, d.1, d.2))
}
}
impl<S: Symbolic> From<Operator<S>> for (usize, State<States>, Tape<S>) {
fn from(d: Operator<S>) -> Self {
(d.index, d.state, d.tape)
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::turing::{Move, Tapes};
#[test]
fn test_builder() {
let tape = Tape::new(["a", "b", "c"]);
let a = Operator::build(Tapes::normal(tape.clone()));
let b = Operator::build(Tapes::standard(tape));
assert_ne!(a, b);
}
#[test]
fn test_operations() {
let tape = Tape::new(["a", "b", "c"]);
let mut actor = Operator::new(0, State::from(States::Valid), tape);
actor.shift(Move::Left, "b");
assert_eq!(actor.tape(), &Tape::new(["b", "a", "b", "c"]));
for _ in 0..actor.tape().len() {
actor.shift(Move::Right, "b");
}
assert_eq!(actor.tape(), &Tape::new(["b", "a", "b", "c", "b"]));
}
}