Tiny expressions-based language for elementary cellular automata simulation
1pub type Tc = (bool, bool, bool);
2pub struct TcIter(usize);
3impl TcIter {
4 pub fn new() -> TcIter {
5 TcIter(0)
6 }
7}
8impl Iterator for TcIter {
9 type Item = Tc;
10
11 fn next(&mut self) -> Option<Self::Item> {
12 let r = match self.0 {
13 0 => Some((false, false, false)),
14 1 => Some((false, false, true)),
15 2 => Some((false, true, false)),
16 3 => Some((false, true, true)),
17 4 => Some((true, false, false)),
18 5 => Some((true, false, true)),
19 6 => Some((true, true, false)),
20 7 => Some((true, true, true)),
21 _ => None
22 };
23
24 self.0 += 1;
25 r
26 }
27}
28
29use std::collections::HashMap;
30
31pub type Inner = HashMap<Tc, bool>;
32#[derive(Debug)]
33pub struct Table(Inner);
34impl Table {
35 pub fn get(&self, tc: Tc) -> bool {
36 *self.0.get(&tc).expect(
37 "What you are seeing is expected to be logically impossible. You encountered a bug."
38 )
39 }
40
41 pub fn add_tc(&mut self, tc: Tc, val: bool) {
42 self.0.insert(tc, val);
43 }
44
45 pub fn new() -> Table {
46 Table(Inner::new())
47 }
48}