0.04
This commit is contained in:
+17
-45
@@ -82,64 +82,36 @@ impl Circuit {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn create_n_bit_comparator_gates(n: usize) -> Vec<Gate> {
|
fn create_n_bit_comparator_gates(n: usize) -> Vec<Gate> {
|
||||||
let mut indices: Vec<usize> = vec![0; n];
|
let mut all_gates: Vec<Gate> = Vec::with_capacity(3*n);
|
||||||
let mut all_gates: Vec<Gate> = Vec::with_capacity(1+3*n);
|
|
||||||
let mut and_gate_indices: Vec<usize> = vec![0; n];
|
let mut and_gate_indices: Vec<usize> = vec![0; n];
|
||||||
|
let mut eq_gate_indices: Vec<usize> = Vec::with_capacity(n-1);
|
||||||
|
|
||||||
|
const EMPTY_VEC: Vec<usize> = Vec::new();
|
||||||
|
|
||||||
for curr in 0..n {
|
for curr in 0..n {
|
||||||
// Gate(A_current > B_current)
|
// Gate(A_curr > B_curr)
|
||||||
let gt_gate = Gate::new(GateType::Bigger, vec![], vec![curr, curr + n]);
|
let mut and_gate_input_indices = vec!(all_gates.len());
|
||||||
let gt_gate_index = all_gates.len();
|
println!("GT = {}", all_gates.len());
|
||||||
all_gates.push(gt_gate);
|
all_gates.push(Gate::new(GateType::Bigger, EMPTY_VEC, vec![curr, curr + n]));
|
||||||
|
|
||||||
// print!("( ({}) ", format!("{} > {}", format!("A{}", n-1-curr), format!("B{}", n-1-curr)));
|
|
||||||
|
|
||||||
let mut current_bit_gate_indices: Vec<usize> = Vec::with_capacity(curr+1);
|
|
||||||
current_bit_gate_indices.push(gt_gate_index);
|
|
||||||
|
|
||||||
// Bit to the left of curr. The one at array-index 0 doesn't have one.
|
// Bit to the left of curr. The one at array-index 0 doesn't have one.
|
||||||
if curr != 0 {
|
if curr != 0 {
|
||||||
// Gate(A_i = B_i)
|
// Gate(A_curr-1 = B_curr-1)
|
||||||
let eq_gate = Gate::new(GateType::Equal, vec![], vec![curr - 1, curr - 1 + n]);
|
eq_gate_indices.push(all_gates.len());
|
||||||
// remember which key-index (see below) this bit belongs to.
|
println!("EQ = {}", all_gates.len());
|
||||||
indices[n - curr] = all_gates.len();
|
all_gates.push(Gate::new(GateType::Equal, EMPTY_VEC, vec![curr - 1, curr - 1 + n]));
|
||||||
all_gates.push(eq_gate);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
for i in 0..curr {
|
and_gate_input_indices.extend(eq_gate_indices.iter());
|
||||||
// Translate i to the key'th bit position because the array index does not match the bit index
|
|
||||||
// i.e.
|
|
||||||
//
|
|
||||||
// let input_bits = vec![
|
|
||||||
// A2 A1 A0 <- A_key
|
|
||||||
// 0 1 2 <- i iterates all array indices less than curr
|
|
||||||
// false, false, true,
|
|
||||||
//
|
|
||||||
// B2 B1 B0
|
|
||||||
// 3 4 5
|
|
||||||
// false, true, false
|
|
||||||
// ];
|
|
||||||
let key = n - 1 - i;
|
|
||||||
|
|
||||||
// print!("&& ({}) ", format!("{} = {}", format!("A{}", key), format!("B{}", key)));
|
|
||||||
|
|
||||||
// Index of Gate(A_i = B_i).
|
|
||||||
// You could maybe find a solution without the indices vector but it's insanely cheap anyway and maybe even better
|
|
||||||
let eq_gate_index = indices[key];
|
|
||||||
current_bit_gate_indices.push(eq_gate_index);
|
|
||||||
}
|
|
||||||
|
|
||||||
// The AND spanning all gates for this bit
|
// The AND spanning all gates for this bit
|
||||||
let and_gate_index = all_gates.len();
|
and_gate_indices.push(all_gates.len());
|
||||||
let and_gate = Gate::new(GateType::And, current_bit_gate_indices, vec![]);
|
println!("&& = {}", all_gates.len());
|
||||||
|
all_gates.push(Gate::new(GateType::And, and_gate_input_indices, EMPTY_VEC));
|
||||||
// println!(")");
|
|
||||||
all_gates.push(and_gate);
|
|
||||||
and_gate_indices.push(and_gate_index);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// the OR spanning all ANDs
|
// the OR spanning all ANDs
|
||||||
let or_gate = Gate::new(GateType::Or, and_gate_indices, vec![]);
|
let or_gate = Gate::new(GateType::Or, and_gate_indices, EMPTY_VEC);
|
||||||
all_gates.push(or_gate);
|
all_gates.push(or_gate);
|
||||||
|
|
||||||
return all_gates;
|
return all_gates;
|
||||||
|
|||||||
Reference in New Issue
Block a user