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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
use crate::{
gate::GateKind, implemented::RustImpls, util::ContainerSizeGames, Component, ComponentBuilder,
Gate, GateLike, Sequal,
};
impl<Rust> Default for ComponentBuilder<Rust> {
fn default() -> Self {
Self {
gates: Vec::default(),
sequals: Vec::default(),
outputs: usize::default(),
}
}
}
impl<Rust> ComponentBuilder<Rust> {
pub fn gate(mut self, gate: Gate<Rust>, sequals: Vec<Sequal>) -> Self {
self.push_sequals(sequals);
self.gates.push(gate);
self
}
fn push_sequals(&mut self, sequals: Vec<Sequal>) -> &mut Self {
self.outputs += sequals
.iter()
.filter(|x| matches!(x, Sequal::End { .. }))
.count();
self.sequals.push(sequals);
self
}
pub fn inputs(mut self, inputs: Vec<Sequal>) -> Component<Rust> {
self.push_sequals(inputs);
Component::from_raw_parts(self.gates, self.sequals, self.outputs)
}
}
impl<Rust> Component<Rust> {
pub fn builder() -> ComponentBuilder<Rust> {
ComponentBuilder::default()
}
pub fn single_gate(gate: Gate<Rust>, num_of_outputs: usize) -> Self {
let inputs = gate.inputs.len();
Self::builder()
.gate(gate, (0..num_of_outputs).map(Sequal::end).collect())
.inputs((0..inputs).map(|entry| Sequal::gate(0, entry)).collect())
}
pub fn from_raw_parts(
gates: Vec<Gate<Rust>>,
sequals: Vec<Vec<Sequal>>,
outputs: usize,
) -> Self {
debug_assert_eq!(
gates.len(),
sequals.len() - 1,
"read the documentation of this function"
);
Self {
gates,
sequals,
outputs,
}
}
}
impl Sequal {
pub fn gate(index: usize, entry: usize) -> Self {
Self::Gate { index, entry }
}
pub fn end(output: usize) -> Self {
Self::End { output }
}
}
impl<Rust> Gate<Rust>
where
Rust: GateLike,
{
fn new(kind: GateKind<Rust>) -> Self {
Self {
inputs: Vec::zeroed(kind.num_of_inputs()),
inputs_filled: 0,
kind,
}
}
pub fn component(component: Component<Rust>) -> Self {
Self::new(GateKind::Custom(component))
}
pub fn rust(gate: Rust) -> Self {
Self::new(GateKind::Rust(RustImpls::User(gate)))
}
pub fn dup(amount: usize) -> Self {
Self::new(GateKind::Rust(RustImpls::Dup(amount)))
}
pub fn mem() -> Self {
Self::new(GateKind::Rust(RustImpls::Mem(bool::default())))
}
pub fn not() -> Self {
Self::new(GateKind::Rust(RustImpls::Not))
}
pub fn nand() -> Self {
Self::new(GateKind::Rust(RustImpls::Nand))
}
pub fn and() -> Self {
Self::new(GateKind::Rust(RustImpls::And))
}
pub fn or() -> Self {
Self::new(GateKind::Rust(RustImpls::Or))
}
pub fn nor() -> Self {
Self::new(GateKind::Rust(RustImpls::Nor))
}
pub fn xor() -> Self {
Self::new(GateKind::Rust(RustImpls::Xor))
}
}