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
/*
    Appellation: tape <module>
    Contrib: FL03 <jo3mccain@icloud.com>
    Description: The tape structure modifies traditional vectors, restricing the ability to remove entries from the tape.
*/
use super::Symbolic;
use contained_core::{ArrayLike, Insert, Iterable};
use serde::{Deserialize, Serialize};
use std::ops::{Index, IndexMut};

#[derive(Clone, Debug, Default, Deserialize, Eq, Hash, Ord, PartialEq, PartialOrd, Serialize)]
pub struct Tape<S: Symbolic = String>(Vec<S>);

impl<S: Symbolic> Tape<S> {
    pub fn new() -> Self {
        Self(Default::default())
    }
    /// Creates a new tape from an iterator; preserves the original order.
    pub fn norm(iter: impl IntoIterator<Item = S>) -> Self {
        Self::from_iter(iter)
    }
    /// Creates a new tape from an iterator; the tape is reversed.
    pub fn std(iter: impl IntoIterator<Item = S>) -> Self {
        let mut tape = Vec::from_iter(iter);
        tape.reverse();
        Self::from_iter(tape.clone())
    }
    /// Creates a new tape with the specified capacity.
    pub fn with_capacity(capacity: usize) -> Self {
        Self(Vec::with_capacity(capacity))
    }
    pub fn write(&mut self, index: usize, symbol: S) {
        self.0.insert(index, symbol)
    }
}

impl<S: Symbolic> ArrayLike<S> for Tape<S> {}

impl<S: Symbolic> AsMut<Vec<S>> for Tape<S> {
    fn as_mut(&mut self) -> &mut Vec<S> {
        self.0.as_mut()
    }
}

impl<S: Symbolic> AsRef<Vec<S>> for Tape<S> {
    fn as_ref(&self) -> &Vec<S> {
        self.0.as_ref()
    }
}

impl<S: Symbolic> Extend<S> for Tape<S> {
    fn extend<T: IntoIterator<Item = S>>(&mut self, iter: T) {
        self.as_mut().extend(iter);
    }
}

impl<S: Symbolic> FromIterator<S> for Tape<S> {
    fn from_iter<T: IntoIterator<Item = S>>(iter: T) -> Self {
        Self(Vec::from_iter(iter))
    }
}

impl<S: Symbolic> Index<usize> for Tape<S> {
    type Output = S;

    fn index(&self, index: usize) -> &Self::Output {
        &self.0[index]
    }
}

impl<S: Symbolic> IndexMut<usize> for Tape<S> {
    fn index_mut(&mut self, index: usize) -> &mut Self::Output {
        &mut self.0[index]
    }
}

impl<S: Symbolic> Insert<usize, S> for Tape<S> {
    fn insert(&mut self, index: usize, elem: S) {
        self.as_mut().insert(index, elem);
    }
}

impl<S: Symbolic> Iterable<usize, S> for Tape<S> {}

impl<S: Symbolic> IntoIterator for Tape<S> {
    type Item = S;
    type IntoIter = std::vec::IntoIter<S>;

    fn into_iter(self) -> Self::IntoIter {
        self.0.into_iter()
    }
}

impl<S: Symbolic> From<Tape<S>> for Vec<S> {
    fn from(tape: Tape<S>) -> Vec<S> {
        tape.as_ref().clone()
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_tape() {
        let mut tape = Tape::new();
        assert!(tape.is_empty());
        tape.push("a");
        assert_eq!(tape.len(), 1);
        assert_eq!(tape[0], "a");
        tape.append(&mut Tape::from_iter(vec!["b", "c"]));
        assert_eq!(tape.len(), 3);
        assert_eq!(tape[2], "c");
    }
}