public override int[] calculate() { if (!startFirstTime()) { return(null); } int[] inputs = new int[InputPorts.Count]; // Go through each input and check its value. foreach (var rightIndexAndleftPort in InputPorts) { int rightIndex = rightIndexAndleftPort.Key; Tuple <CircuitElement, int> leftPort = rightIndexAndleftPort.Value; CircuitElement leftElement = leftPort.Item1; int leftIndex = leftPort.Item2; int[] childCalc = leftElement.calculate(); if (childCalc == null) { return(null); } inputs[rightIndex] = childCalc[leftIndex]; } outputValues = computeOutput(inputs.ToArray()); if (outputValues.Length != Outputs.Count) { throw new Exception("Gate returned " + outputValues.Length + " outputs; should have returned " + Outputs.Count); } endCalculation(); return(outputValues); }
/// <summary> /// returns a list of all Global Inputs to a given CircuitElement /// </summary> /// <param name="?"></param> /// <returns></returns> public List <INPUT> findGlobalInputs(CircuitElement e) { List <CircuitElement> visited = new List <CircuitElement>(); List <CircuitElement> toBeVisited = new List <CircuitElement>(); List <INPUT> inputList = new List <INPUT>(); visited.Add(e); while (toBeVisited.Count > 0) { CircuitElement cur = toBeVisited[0]; visited.Add(cur); toBeVisited.Remove(cur); if ((cur.Inputs == null || cur.Inputs.Count == 0) && cur is INPUT) { inputList.Add((INPUT)e); } else if (cur.Inputs != null) { foreach (CircuitElement input in cur.Inputs.Keys) { if (!visited.Contains(input) && !toBeVisited.Contains(input)) { toBeVisited.Add(input); } } } } return(inputList); }
public KeyValuePair <int, int> calculateGraphValues(CircuitSimLib.CircuitElement e) { if (e.Inputs == null || e.Inputs.Count == 0) { return(new KeyValuePair <int, int>(0, 1)); } int maxChild = e.Inputs.Count; int nextGenChild = Int32.MinValue; int generations = Int32.MinValue; foreach (CircuitSimLib.CircuitElement child in e.Inputs.Keys) { KeyValuePair <int, int> pair = calculateGraphValues(child); if (pair.Value > generations) { generations = pair.Value + 1; } nextGenChild += child.Inputs.Count; } if (nextGenChild > maxChild) { maxChild = nextGenChild; } return(new KeyValuePair <int, int>(maxChild, generations)); }
/// <summary> /// gives an output, using properties of a NOT bubble /// </summary> /// <returns></returns> public override int[] calculate() { PrevOutput = myOutput; CircuitElement child = Inputs.Keys.ElementAt(0); myOutput[0] = (int)Math.Abs(child.calculate()[Inputs[child][0]] - 1); PrevOutput = myOutput; return(myOutput); }
/// <summary> /// Constructor of an output, given a name and the gate in which it is connected /// </summary> /// <param name="name"></param> /// <param name="linkedGate"></param> public OUTPUT(String name, Rect bounds, CircuitElement linkedGate, double orientation = 0) { type = "Output"; myInputs = new Dictionary <CircuitElement, List <int> >(); myOutputs = new Dictionary <int, List <CircuitElement> >(); myName = name; myLinkedGate = linkedGate; myBounds = bounds; myOutput = new int[1]; myOrientation = orientation; }
/// <summary> /// Returns true if this element has the given parent /// </summary> /// <param name="parent"></param> /// <returns></returns> public bool hasParent(CircuitElement parent) { foreach (Dictionary <CircuitElement, List <int> > dict in Outputs.Values) { foreach (CircuitElement elem in dict.Keys) { if (elem == parent) { return(true); } } } return(false); }
/// <summary> /// Returns true if this element has the given parent /// </summary> /// <param name="parent"></param> /// <returns></returns> public bool hasParent(CircuitElement parent) { foreach (List <CircuitElement> list in Outputs.Values) { foreach (CircuitElement elem in list) { if (elem == parent) { return(true); } } } return(false); }
/// <summary> /// adds the parent circuitelement so that the child can refer to it (helper for addchild) /// (you are the left element) /// </summary> /// <param name="destElement"></param> private void addOutput(int sourceIndex, CircuitElement destElement, int destIndex) { if (!outputComponents.ContainsKey(sourceIndex)) { outputComponents[sourceIndex] = new Dictionary <CircuitElement, List <int> >(); } if (!outputComponents[sourceIndex].ContainsKey(destElement)) { outputComponents[sourceIndex][destElement] = new List <int>(); } if (!outputComponents[sourceIndex][destElement].Contains(destIndex)) { outputComponents[sourceIndex][destElement].Add(destIndex); } }
/// <summary> /// adds the parent circuitelement so that the child can refer to it. /// Only one of addChild and addParent needs to be called on the pair. /// </summary> /// <param name="outputToBeAdded"></param> public void addParent(CircuitElement outputToBeAdded, int index) { if (myOutputs.ContainsKey(index) && myOutputs[index].Contains(outputToBeAdded)) { return; } if (!myOutputs.ContainsKey(index)) { myOutputs[index] = new List <CircuitElement>(); } myOutputs[index].Add(outputToBeAdded); outputToBeAdded.addChild(this, index); }
/// <summary> /// wires two CircuitElements (makes an added element its input) /// Only one of addChild and addParent needs to be called on the pair. /// </summary> /// <param name="inputToBeAdded"></param> public void addChild(CircuitElement inputToBeAdded, int index) { if (myInputs.ContainsKey(inputToBeAdded) && myInputs[inputToBeAdded].Contains(index)) { return; } if (!myInputs.ContainsKey(inputToBeAdded)) { myInputs[inputToBeAdded] = new List <int>(); } myInputs[inputToBeAdded].Add(index); inputToBeAdded.addParent(this, index); }
/// <summary> /// Connects elements using a dictionary that describes the inputs for each Shape. /// </summary> /// <param name="elementAndItsInputs"></param> private void connectEverything(Dictionary <Sketch.Shape, Dictionary <int, Tuple <Shape, int> > > elementAndItsInputs) { foreach (var shapeAndInputs in elementAndItsInputs) { Shape rightShape = shapeAndInputs.Key; var inputPorts = shapeAndInputs.Value; foreach (var inputPort in inputPorts) { CircuitElement rightElement = shapesToElements[rightShape]; int rightIndex = inputPort.Key; CircuitElement leftElement = shapesToElements[inputPort.Value.Item1]; int leftIndex = inputPort.Value.Item2; rightElement.addInput(sourceElement: leftElement, sourceIndex: leftIndex, destIndex: rightIndex); } } }
/// <summary> /// wires two CircuitElements (makes an added element its input) /// </summary> /// <param name="sourceElement"></param> public void addInput(CircuitElement sourceElement, int sourceIndex, int destIndex) { inputConnections[destIndex] = Tuple.Create(sourceElement, sourceIndex); sourceElement.addOutput(sourceIndex, this, destIndex); }