pub struct GateGraphBuilder { /* private fields */ }Expand description
Data structure that represents a graph of logic gates, it can be initialized to simulate the circuit.
Conceptually the logic gates are represented as nodes in a graph with dependency edges to other nodes.
Inputs are represented by constants(ON, OFF) and levers.
Outputs are represented by OutputHandles which allow you to query the state of gates and are created by GateGraphBuilder::output.
Once the graph is initialized, it transforms into an InitializedGateGraph which cannot be modified. The initialization process optimizes the gate graph so that expressive abstractions that potentially generate lots of constants or useless gates can be used without fear. All constants and dead gates will be optimized away and the remaining graph simplified very aggressively.
Zero overhead abstractions!
§Examples
Simple gates.
let mut g = GateGraphBuilder::new();
// Providing each gate with a string name allows for some very neat debugging.
// If you don't want them affecting performance, you can disable feature "debug_gates",
// all of the strings will be optimized away.
let or = g.or2(ON, OFF, "or");
let or_output = g.output1(or, "or_output");
let and = g.and2(ON, OFF, "and");
let and_output = g.output1(and, "and_output");
let ig = &g.init();
// `b0()` accesses the 0th bit of the output.
// Outputs can have as many bits as you want
// and be accessed with methods like `u8()`, `char()` or `i128()`.
assert_eq!(or_output.b0(ig), true);
assert_eq!(and_output.b0(ig), false);Levers!
let l1 = g.lever("l1");
let l2 = g.lever("l2");
let or = g.or2(l1.bit(), l2.bit(), "or");
let or_output = g.output1(or, "or_output");
let and = g.and2(l1.bit(), l2.bit(), "and");
let and_output = g.output1(and, "and_output");
let ig = &mut g.init();
assert_eq!(or_output.b0(ig), false);
assert_eq!(and_output.b0(ig), false);
// `_stable` means that the graph will run until gate states have stopped changing.
// This might not be what you want if you have a circuit that never stabilizes,
// like 3 not gates connected in a circle!
// See [InitializedGateGraph::run_until_stable].
ig.flip_lever_stable(l1);
assert_eq!(or_output.b0(ig), true);
assert_eq!(and_output.b0(ig), false);
ig.flip_lever_stable(l2);
assert_eq!(or_output.b0(ig), true);
assert_eq!(and_output.b0(ig), true);let r = g.lever("l1");
let s = g.lever("l2");
let q = g.nor2(r.bit(), OFF, "q");
let nq = g.nor2(s.bit(), q, "nq");
let q_output = g.output1(q, "q");
let nq_output = g.output1(nq, "nq");
// `d1()` replaces the dependency at index 1 with nq.
// We used OFF as a placeholder above.
g.d1(q, nq);
let ig = &mut g.init();
// With latches, the initial state should be treated as undefined,
// so remember to always reset your latches at the beginning of the simulation.
ig.pulse_lever_stable(r);
assert_eq!(q_output.b0(ig), false);
assert_eq!(nq_output.b0(ig), true);
ig.pulse_lever_stable(s);
assert_eq!(q_output.b0(ig), true);
assert_eq!(nq_output.b0(ig), false);
ig.pulse_lever_stable(r);
assert_eq!(q_output.b0(ig), false);
assert_eq!(nq_output.b0(ig), true);Implementations§
Source§impl GateGraphBuilder
impl GateGraphBuilder
Sourcepub fn new() -> GateGraphBuilder
pub fn new() -> GateGraphBuilder
Returns a new GateGraphBuilder containing only OFF and ON.
Sourcepub fn dpush(&mut self, target: GateIndex, new_dep: GateIndex)
pub fn dpush(&mut self, target: GateIndex, new_dep: GateIndex)
Appends new_dep to the list of dependencies of gate target.
§Panics
Will panic if target can’t have a variable number of dependencies.
Sourcepub fn dx(&mut self, target: GateIndex, new_dep: GateIndex, x: usize)
pub fn dx(&mut self, target: GateIndex, new_dep: GateIndex, x: usize)
Sets the dependency at index x in target dependencies to new_dep.
§Panics
Will panic if target has less than x + 1 dependencies, you probably want GateGraphBuilder::dpush instead.
Will panic if target is Not and x > 0.
Will panic if target can’t have dependencies.
Sourcepub fn d0(&mut self, target: GateIndex, new_dep: GateIndex)
pub fn d0(&mut self, target: GateIndex, new_dep: GateIndex)
Sets the dependency at index 0 in target dependencies to new_dep.
§Panics
Will panic if target has less than 1 dependency, you probably want GateGraphBuilder::dpush instead.
Will panic if target can’t have dependencies.
Sourcepub fn d1(&mut self, target: GateIndex, new_dep: GateIndex)
pub fn d1(&mut self, target: GateIndex, new_dep: GateIndex)
Sets the dependency at index 1 in target dependencies to new_dep.
§Panics
Will panic if target has less than 1 dependency, you probably want GateGraphBuilder::dpush instead.
Will panic if target can’t have more than 1 dependency.
Sourcepub fn lever<S: Into<String>>(&mut self, name: S) -> LeverHandle
pub fn lever<S: Into<String>>(&mut self, name: S) -> LeverHandle
Returns the LeverHandle of a new lever gate.
Providing a good name allows for a great debugging experience. You can disable the “debug_gates” feature to slightly increase performance.
Sourcepub fn not1<S: Into<String>>(&mut self, dep: GateIndex, name: S) -> GateIndex
pub fn not1<S: Into<String>>(&mut self, dep: GateIndex, name: S) -> GateIndex
Returns the GateIndex of a new not gate with 1 dependency.
Providing a good name allows for a great debugging experience. You can disable the “debug_gates” feature to slightly increase performance.
Sourcepub fn or<S: Into<String>>(&mut self, name: S) -> GateIndex
pub fn or<S: Into<String>>(&mut self, name: S) -> GateIndex
Returns the GateIndex of a new or gate with no dependencies. Dependencies can be added with GateGraphBuilder::dpush
Providing a good name allows for a great debugging experience, you can disable the “debug_gates” feature to slightly increase performance
Sourcepub fn or1<S: Into<String>>(&mut self, dep: GateIndex, name: S) -> GateIndex
pub fn or1<S: Into<String>>(&mut self, dep: GateIndex, name: S) -> GateIndex
Returns the GateIndex of a new gate with 1 dependency.
Providing a good name allows for a great debugging experience, you can disable the “debug_gates” feature to slightly increase performance.
Sourcepub fn or2<S: Into<String>>(
&mut self,
dep1: GateIndex,
dep2: GateIndex,
name: S,
) -> GateIndex
pub fn or2<S: Into<String>>( &mut self, dep1: GateIndex, dep2: GateIndex, name: S, ) -> GateIndex
Returns the GateIndex of a new gate with 2 dependencies.
Providing a good name allows for a great debugging experience, you can disable the “debug_gates” feature to slightly increase performance.
Sourcepub fn orx<S: Into<String>, I: Iterator<Item = GateIndex> + Clone>(
&mut self,
iter: I,
name: S,
) -> GateIndex
pub fn orx<S: Into<String>, I: Iterator<Item = GateIndex> + Clone>( &mut self, iter: I, name: S, ) -> GateIndex
Returns the GateIndex of a new gate with x dependencies. the dependencies are taken in order from iter.
Providing a good name allows for a great debugging experience, you can disable the “debug_gates” feature to slightly increase performance.
Sourcepub fn nor<S: Into<String>>(&mut self, name: S) -> GateIndex
pub fn nor<S: Into<String>>(&mut self, name: S) -> GateIndex
Returns the GateIndex of a new nor gate with no dependencies. Dependencies can be added with GateGraphBuilder::dpush
Providing a good name allows for a great debugging experience, you can disable the “debug_gates” feature to slightly increase performance
Sourcepub fn nor1<S: Into<String>>(&mut self, dep: GateIndex, name: S) -> GateIndex
pub fn nor1<S: Into<String>>(&mut self, dep: GateIndex, name: S) -> GateIndex
Returns the GateIndex of a new gate with 1 dependency.
Providing a good name allows for a great debugging experience, you can disable the “debug_gates” feature to slightly increase performance.
Sourcepub fn nor2<S: Into<String>>(
&mut self,
dep1: GateIndex,
dep2: GateIndex,
name: S,
) -> GateIndex
pub fn nor2<S: Into<String>>( &mut self, dep1: GateIndex, dep2: GateIndex, name: S, ) -> GateIndex
Returns the GateIndex of a new gate with 2 dependencies.
Providing a good name allows for a great debugging experience, you can disable the “debug_gates” feature to slightly increase performance.
Sourcepub fn norx<S: Into<String>, I: Iterator<Item = GateIndex> + Clone>(
&mut self,
iter: I,
name: S,
) -> GateIndex
pub fn norx<S: Into<String>, I: Iterator<Item = GateIndex> + Clone>( &mut self, iter: I, name: S, ) -> GateIndex
Returns the GateIndex of a new gate with x dependencies. the dependencies are taken in order from iter.
Providing a good name allows for a great debugging experience, you can disable the “debug_gates” feature to slightly increase performance.
Sourcepub fn and<S: Into<String>>(&mut self, name: S) -> GateIndex
pub fn and<S: Into<String>>(&mut self, name: S) -> GateIndex
Returns the GateIndex of a new and gate with no dependencies. Dependencies can be added with GateGraphBuilder::dpush
Providing a good name allows for a great debugging experience, you can disable the “debug_gates” feature to slightly increase performance
Sourcepub fn and1<S: Into<String>>(&mut self, dep: GateIndex, name: S) -> GateIndex
pub fn and1<S: Into<String>>(&mut self, dep: GateIndex, name: S) -> GateIndex
Returns the GateIndex of a new gate with 1 dependency.
Providing a good name allows for a great debugging experience, you can disable the “debug_gates” feature to slightly increase performance.
Sourcepub fn and2<S: Into<String>>(
&mut self,
dep1: GateIndex,
dep2: GateIndex,
name: S,
) -> GateIndex
pub fn and2<S: Into<String>>( &mut self, dep1: GateIndex, dep2: GateIndex, name: S, ) -> GateIndex
Returns the GateIndex of a new gate with 2 dependencies.
Providing a good name allows for a great debugging experience, you can disable the “debug_gates” feature to slightly increase performance.
Sourcepub fn andx<S: Into<String>, I: Iterator<Item = GateIndex> + Clone>(
&mut self,
iter: I,
name: S,
) -> GateIndex
pub fn andx<S: Into<String>, I: Iterator<Item = GateIndex> + Clone>( &mut self, iter: I, name: S, ) -> GateIndex
Returns the GateIndex of a new gate with x dependencies. the dependencies are taken in order from iter.
Providing a good name allows for a great debugging experience, you can disable the “debug_gates” feature to slightly increase performance.
Sourcepub fn nand<S: Into<String>>(&mut self, name: S) -> GateIndex
pub fn nand<S: Into<String>>(&mut self, name: S) -> GateIndex
Returns the GateIndex of a new nand gate with no dependencies. Dependencies can be added with GateGraphBuilder::dpush
Providing a good name allows for a great debugging experience, you can disable the “debug_gates” feature to slightly increase performance
Sourcepub fn nand1<S: Into<String>>(&mut self, dep: GateIndex, name: S) -> GateIndex
pub fn nand1<S: Into<String>>(&mut self, dep: GateIndex, name: S) -> GateIndex
Returns the GateIndex of a new gate with 1 dependency.
Providing a good name allows for a great debugging experience, you can disable the “debug_gates” feature to slightly increase performance.
Sourcepub fn nand2<S: Into<String>>(
&mut self,
dep1: GateIndex,
dep2: GateIndex,
name: S,
) -> GateIndex
pub fn nand2<S: Into<String>>( &mut self, dep1: GateIndex, dep2: GateIndex, name: S, ) -> GateIndex
Returns the GateIndex of a new gate with 2 dependencies.
Providing a good name allows for a great debugging experience, you can disable the “debug_gates” feature to slightly increase performance.
Sourcepub fn nandx<S: Into<String>, I: Iterator<Item = GateIndex> + Clone>(
&mut self,
iter: I,
name: S,
) -> GateIndex
pub fn nandx<S: Into<String>, I: Iterator<Item = GateIndex> + Clone>( &mut self, iter: I, name: S, ) -> GateIndex
Returns the GateIndex of a new gate with x dependencies. the dependencies are taken in order from iter.
Providing a good name allows for a great debugging experience, you can disable the “debug_gates” feature to slightly increase performance.
Sourcepub fn xor<S: Into<String>>(&mut self, name: S) -> GateIndex
pub fn xor<S: Into<String>>(&mut self, name: S) -> GateIndex
Returns the GateIndex of a new xor gate with no dependencies. Dependencies can be added with GateGraphBuilder::dpush
Providing a good name allows for a great debugging experience, you can disable the “debug_gates” feature to slightly increase performance
Sourcepub fn xor1<S: Into<String>>(&mut self, dep: GateIndex, name: S) -> GateIndex
pub fn xor1<S: Into<String>>(&mut self, dep: GateIndex, name: S) -> GateIndex
Returns the GateIndex of a new gate with 1 dependency.
Providing a good name allows for a great debugging experience, you can disable the “debug_gates” feature to slightly increase performance.
Sourcepub fn xor2<S: Into<String>>(
&mut self,
dep1: GateIndex,
dep2: GateIndex,
name: S,
) -> GateIndex
pub fn xor2<S: Into<String>>( &mut self, dep1: GateIndex, dep2: GateIndex, name: S, ) -> GateIndex
Returns the GateIndex of a new gate with 2 dependencies.
Providing a good name allows for a great debugging experience, you can disable the “debug_gates” feature to slightly increase performance.
Sourcepub fn xorx<S: Into<String>, I: Iterator<Item = GateIndex> + Clone>(
&mut self,
iter: I,
name: S,
) -> GateIndex
pub fn xorx<S: Into<String>, I: Iterator<Item = GateIndex> + Clone>( &mut self, iter: I, name: S, ) -> GateIndex
Returns the GateIndex of a new gate with x dependencies. the dependencies are taken in order from iter.
Providing a good name allows for a great debugging experience, you can disable the “debug_gates” feature to slightly increase performance.
Sourcepub fn xnor<S: Into<String>>(&mut self, name: S) -> GateIndex
pub fn xnor<S: Into<String>>(&mut self, name: S) -> GateIndex
Returns the GateIndex of a new xnor gate with no dependencies. Dependencies can be added with GateGraphBuilder::dpush
Providing a good name allows for a great debugging experience, you can disable the “debug_gates” feature to slightly increase performance
Sourcepub fn xnor1<S: Into<String>>(&mut self, dep: GateIndex, name: S) -> GateIndex
pub fn xnor1<S: Into<String>>(&mut self, dep: GateIndex, name: S) -> GateIndex
Returns the GateIndex of a new gate with 1 dependency.
Providing a good name allows for a great debugging experience, you can disable the “debug_gates” feature to slightly increase performance.
Sourcepub fn xnor2<S: Into<String>>(
&mut self,
dep1: GateIndex,
dep2: GateIndex,
name: S,
) -> GateIndex
pub fn xnor2<S: Into<String>>( &mut self, dep1: GateIndex, dep2: GateIndex, name: S, ) -> GateIndex
Returns the GateIndex of a new gate with 2 dependencies.
Providing a good name allows for a great debugging experience, you can disable the “debug_gates” feature to slightly increase performance.
Sourcepub fn xnorx<S: Into<String>, I: Iterator<Item = GateIndex> + Clone>(
&mut self,
iter: I,
name: S,
) -> GateIndex
pub fn xnorx<S: Into<String>, I: Iterator<Item = GateIndex> + Clone>( &mut self, iter: I, name: S, ) -> GateIndex
Returns the GateIndex of a new gate with x dependencies. the dependencies are taken in order from iter.
Providing a good name allows for a great debugging experience, you can disable the “debug_gates” feature to slightly increase performance.
Sourcepub fn init(self) -> InitializedGateGraph
pub fn init(self) -> InitializedGateGraph
Returns a new InitializedGateGraph created from self after running optimizations.
Sourcepub fn init_unoptimized(self) -> InitializedGateGraph
pub fn init_unoptimized(self) -> InitializedGateGraph
Returns a new InitializedGateGraph created from self without running optimizations.
Sourcepub fn output<S: Into<String>>(
&mut self,
bits: &[GateIndex],
name: S,
) -> OutputHandle
pub fn output<S: Into<String>>( &mut self, bits: &[GateIndex], name: S, ) -> OutputHandle
Returns a new OutputHandle with name name for the gates in bits.
See OutputHandle for gate querying methods.
Sourcepub fn output1<S: Into<String>>(
&mut self,
bit: GateIndex,
name: S,
) -> OutputHandle
pub fn output1<S: Into<String>>( &mut self, bit: GateIndex, name: S, ) -> OutputHandle
Returns a new OutputHandle with name name for a single gate bit.
See OutputHandle for gate querying methods.
Sourcepub fn probe<S: Into<String>>(&mut self, bits: &[GateIndex], name: S)
pub fn probe<S: Into<String>>(&mut self, bits: &[GateIndex], name: S)
“Probes” the gates in bits, meaning that whenever the state of any of them changes,
the new state of the group will be printed to stdout along with name.
§Example
let mut g = GateGraphBuilder::new();
let l1 = g.lever("l1");
let l2 = g.lever("l2");
let or = g.xor2(l1.bit(), l2.bit(), "or");
let xor = g.xor2(l1.bit(), l2.bit(), "xor");
g.probe(&[or,xor],"or_xor");
let xor_output = g.output1(xor, "xor_output");
let ig = &mut g.init();
assert_eq!(xor_output.b0(ig), false);
ig.set_lever_stable(l1);
assert_eq!(xor_output.b0(ig), true);
ig.set_lever_stable(l2);
assert_eq!(xor_output.b0(ig), false);
ig.reset_lever_stable(l1);
assert_eq!(xor_output.b0(ig), true);
ig.reset_lever_stable(l2);
assert_eq!(xor_output.b0(ig), false);In the terminal you’ll see:
or_xor: 3
or_xor: 1
or_xor: 3
or_xor: 0Trait Implementations§
Source§impl Clone for GateGraphBuilder
impl Clone for GateGraphBuilder
Source§fn clone(&self) -> GateGraphBuilder
fn clone(&self) -> GateGraphBuilder
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for GateGraphBuilder
impl Debug for GateGraphBuilder
Auto Trait Implementations§
impl Freeze for GateGraphBuilder
impl RefUnwindSafe for GateGraphBuilder
impl Send for GateGraphBuilder
impl Sync for GateGraphBuilder
impl Unpin for GateGraphBuilder
impl UnwindSafe for GateGraphBuilder
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§unsafe fn clone_to_uninit(&self, dst: *mut T)
unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit)