public void AddConnection(GateConnection connection) { // create a connection from output to input if (OutputConnectionCounterparties.ContainsKey(connection.FromAddr)) { // the "from" in this connection already has a counterparty. want to remove it var oldTo = OutputConnectionCounterparties[connection.FromAddr]; InputConnectionCounterparties.Remove(oldTo); InputAddrs.Add(oldTo); } if (InputConnectionCounterparties.ContainsKey(connection.ToAddr)) { // the "to" in this connection already has a counterparty. want to remove it var oldFrom = InputConnectionCounterparties[connection.ToAddr]; OutputConnectionCounterparties.Remove(oldFrom); OutputAddrs.Add(oldFrom); } OutputConnectionCounterparties[connection.FromAddr] = connection.ToAddr; OutputAddrs.Remove(connection.FromAddr); InputConnectionCounterparties[connection.ToAddr] = connection.FromAddr; InputAddrs.Remove(connection.ToAddr); TopologicalOrderImpl = null; }
public void RemoveGate(Gate gate) { for (int i = 0; i < gate.InputCount; i++) { var inAddr = gate.GetLocalInputAddress(i); Debug.Assert(!InputConnectionCounterparties.ContainsKey(inAddr)); InputAddrs.Remove(inAddr); } for (int i = 0; i < gate.OutputCount; i++) { var outAddr = gate.GetLocalOutputAddress(i); Debug.Assert(!OutputConnectionCounterparties.ContainsKey(outAddr)); OutputAddrs.Remove(outAddr); } Gates.Remove(gate); }
public override string ToString() { List <Gate> gates = TopologicalOrder; // that should also assign all gates to a number string str = ""; str += "Input Addrs: " + string.Join(" ", InputAddrs.Select(addr => addr.Render())) + "\n"; str += "Output Addrs: " + string.Join(" ", OutputAddrs.Select(addr => addr.Render())) + "\n"; str += string.Join("\n", gates); str += "\n"; str += string.Join("\n", OutputConnectionCounterparties.Select(kv => kv.Key + " -> " + kv.Value)); str += "\n"; return(str); }
public void AddGate(Gate gate, IEnumerable <GateConnection> connections) { // insert before checks to allow loopback Gates.Add(gate); for (int i = 0; i < gate.InputCount; i++) { InputAddrs.Add(new InputGateAddress(gate, i)); } for (int i = 0; i < gate.OutputCount; i++) { OutputAddrs.Add(new OutputGateAddress(gate, i)); } foreach (var connection in connections) { AddConnection(connection); } TopologicalOrderImpl = null; }
public void JoinWith(Circuit c, IList <GateConnection> joins) { // add all of the gates in the other circuit. we need to make sure that there are there is no overlap Debug.Assert(!Gates.Intersect(c.Gates).Any()); Gates.UnionWith(c.Gates); InputAddrs.UnionWith(c.InputAddrs); OutputAddrs.UnionWith(c.OutputAddrs); foreach (var inAddr in c.InputConnectionCounterparties.Keys) { var outAddr = c.InputConnectionCounterparties[inAddr]; InputConnectionCounterparties[inAddr] = outAddr; OutputConnectionCounterparties[outAddr] = inAddr; } foreach (var join in joins) { AddConnection(join); } TopologicalOrderImpl = null; }
// DOES NOT DEAL WITH CIRCUITS WITH CYCLES!!! private List <Gate> AssignTopologicalRanks() { // grab an item from the input set var inEnum = InputAddrs.GetEnumerator(); inEnum.MoveNext(); var gateDeque = new LinkedList <Gate>(); while (inEnum.MoveNext()) { gateDeque.AddFirst(inEnum.Current.Gate); } var sortList = new List <Gate>(); int nextRank = 0; while (gateDeque.Count > 0) { var current = gateDeque.First.Value; if (current.TopologicalRank != Gate.NO_RANK) { // already assigned a rank to this. can move on to next gateDeque.RemoveFirst(); continue; } // check to see if all of the predecessors have a rank assigned. If any don't then add them to the front of the queue bool shouldAssignRank = true; for (int i = 0; i < current.InputCount; i++) { var addr = current.GetLocalInputAddress(i); if (InputAddrs.Contains(addr)) { continue; } var counterparty = InputConnectionCounterparties[addr].Gate; if (counterparty.TopologicalRank == Gate.NO_RANK) { gateDeque.AddFirst(counterparty); shouldAssignRank = false; } } if (shouldAssignRank) { current.TopologicalRank = nextRank; nextRank++; sortList.Add(current); gateDeque.RemoveFirst(); // add all successors for (int i = 0; i < current.OutputCount; i++) { var addr = current.GetLocalOutputAddress(i); if (OutputAddrs.Contains(addr)) { continue; } gateDeque.AddLast(OutputConnectionCounterparties[addr].Gate); } } // otherwise a predecessor still needs a rank } return(sortList); }