Beispiel #1
0
        /// <summary>
        /// Counts the number of bits set for each output of both input comparator networks
        /// if the number of set bits is different the n1 cannot be subsumed by n2 and vice versa.
        /// </summary>
        /// <param name="n1">The comparator network 1.</param>
        /// <param name="n2">The comparator network 2.</param>
        /// <returns>True if subsume test should be done, False otherwise.</returns>
        private static bool ShouldCheckSubsumption(IComparatorNetwork n1, IComparatorNetwork n2)
        {
#if DEBUG
            if (n1.OutputsPopCount > n2.OutputsPopCount)
            {
                IComparatorNetwork.OutputCountBigger++;
                return(false);
            }

            if (!SequencesAreCompatible(n1.SequencesWithOnes, n2.SequencesWithOnes))
            {
                IComparatorNetwork.SubsumeNoCheck1++;
                return(false);
            }

            if (!SequencesAreCompatible(n1.Where0SetCount, n2.Where0SetCount))
            {
                IComparatorNetwork.SubsumeNoCheck2++;
                return(false);
            }

            return(true);
#endif
            if (n1.OutputsPopCount > n2.OutputsPopCount)
            {
                return(false);
            }

            return(SequencesAreCompatible(n1.SequencesWithOnes, n2.SequencesWithOnes) &&
                   SequencesAreCompatible(n1.Where0SetCount, n2.Where0SetCount));
        }
Beispiel #2
0
 private static void PrintComparatorNet(IComparatorNetwork net)
 {
     foreach (var c in net.Comparators)
     {
         Trace.Write($"({c.X},{c.Y}) ");
     }
     Trace.WriteLine(string.Empty);
 }
Beispiel #3
0
        /// <inheritdoc/>
        public bool IsRedundant(IComparatorNetwork n)
        {
            //if (this.Outputs.SequenceEqual(n.Outputs))
            if (this.isRedundant)
            {
#if DEBUG
                IComparatorNetwork.RedundantNumber++;
#endif
                return(true);
            }

            return(false);
        }
Beispiel #4
0
        /// <inheritdoc/>
        public bool IsSubsumed(IComparatorNetwork n)
        {
#if DEBUG
            IComparatorNetwork.SubsumeTotal++;
#endif
            if (!ShouldCheckSubsumption(n, this))
            {
#if DEBUG
                IComparatorNetwork.SubsumeNoCheckTotal++;
#endif
                return(false);
            }

#if PERMUTE
            // Create matrix for permutations
            var positions = GetPositions(this.Where0, this.Where1, n.Where0, n.Where1);

            if (positions == null)
            {
                return(false);
            }

#if DEBUG
            IComparatorNetwork.SubsumeNumber++;
#endif

#if PRUNE_GRAPH_MATCHING
            var graphMatcher = new BipartiteGraphMatching();
            return(graphMatcher.GetAllPerfectMatchings(positions, this.Outputs, n.Outputs, n.OutputsDual));
#endif

            var permutation = new int[IComparatorNetwork.Inputs];
            permutation.Populate(-1);
            var succeed = TryPermutations(positions, permutation, this.Outputs, n.Outputs, this.Comparators.Length);
            //var succeed = TryPermutationsIteratively(positions, positionsDual, permutation, this.Outputs, n.Outputs, n.OutputsDual, this.Comparators.Length);
#if DEBUG
            if (succeed)
            {
                IComparatorNetwork.SubsumeSucceed++;
            }
#endif

            return(succeed);
#endif
            return(true);
        }
Beispiel #5
0
        private static void PrintComparatorNet(IComparatorNetwork net)
        {
            var set = new HashSet <int>();

            foreach (var c in net.Comparators)
            {
                var x = c.X + 1;
                var y = c.Y + 1;

                if (!set.Add(x) || !set.Add(y))
                {
                    Trace.WriteLine("");
                    set = new HashSet <int>(new [] { x, y });
                }

                Trace.Write($"({x},{y}) ");
            }
            Trace.WriteLine(string.Empty);
        }