public static void Validate(LogicalCircuit logicalCircuit)
            {
                CircuitMap map = new CircuitMap(TableChank.Copy(logicalCircuit));

                // this will throw if bit width error occurred.
                map.Apply(1);
            }
        private void ReportFunctions(LogicalCircuit root)
        {
            try {
                CircuitMap               map   = new CircuitMap(root);
                CircuitState             state = map.Apply(1);
                Dictionary <string, int> func  = new Dictionary <string, int>();
                foreach (CircuitFunction f in state.Functions)
                {
                    string name = f.ReportName;
                    if (func.ContainsKey(name))
                    {
                        func[name]++;
                    }
                    else
                    {
                        func.Add(name, 1);
                    }
                }

                this.Functions = new List <string>(func.Keys);
                this.Functions.Sort(StringComparer.Ordinal);
                this.Usage = func;
            } catch (Exception exception) {
                this.BuildMapException = exception;
            }
        }
        private void CircuitMapCleanUpTest(CircuitProject circuitProject, string logicalCircuitName, int expectedFunctions)
        {
            ProjectTester.SwitchTo(circuitProject, logicalCircuitName);

            CircuitMap   map   = new CircuitMap(circuitProject.ProjectSet.Project.LogicalCircuit);
            CircuitState state = map.Apply(CircuitRunner.HistorySize);

            Assert.AreEqual(expectedFunctions, state.Functions.Count(), "wrong number of functions");
        }
        private void CircuitMapPerfTest(string project, string initialCircuit, int maxCount, int maxSeconds)
        {
            CircuitProject circuitProject = ProjectTester.Load(this.TestContext, project, initialCircuit);
            Stopwatch      stopwatch      = new Stopwatch();

            stopwatch.Start();
            for (int i = 0; i < maxCount; i++)
            {
                CircuitMap   circuitMap   = new CircuitMap(circuitProject.ProjectSet.Project.LogicalCircuit);
                CircuitState circuitState = circuitMap.Apply(CircuitRunner.HistorySize);
                Assert.IsNotNull(circuitState);
                circuitMap.TurnOn();
            }
            stopwatch.Stop();
            this.TestContext.WriteLine("{0} CircuitMap(s) created and applied in {1} - {2:N2} sec per each map", maxCount, stopwatch.Elapsed, stopwatch.Elapsed.TotalSeconds / maxCount);
            Assert.IsTrue(stopwatch.Elapsed < new TimeSpan(0, 0, maxSeconds), "CircuitMap was created and applied successfully but too slow");
        }
            public TableChank(LogicalCircuit logicalCircuit)
            {
                this.LogicalCircuit = TableChank.Copy(logicalCircuit);
                this.Plug();

                // Create map and state
                CircuitMap circuitMap = new CircuitMap(this.LogicalCircuit);

                this.CircuitState = circuitMap.Apply(CircuitRunner.HistorySize);

                this.Inputs.ForEach(s => s.Function  = circuitMap.FunctionConstant(s.Symbol));
                this.Outputs.ForEach(s => s.Function = circuitMap.FunctionProbe(s.Symbol));

                this.Inputs.Where(s => s.Function == null).ToList().ForEach(s => this.Inputs.Remove(s));
                this.Outputs.Where(s => s.Function == null).ToList().ForEach(s => this.Outputs.Remove(s));

                Tracer.Assert(this.Inputs.All(s => s.Function != null) && this.Outputs.All(s => s.Function != null));

                this.InputBitCount = this.Inputs.Sum(p => p.Pin.BitWidth);
                this.Count         = BigInteger.One << this.InputBitCount;

                circuitMap.TurnOn();
            }