Beispiel #1
0
        internal static List <Piece> UniteList(List <Piece> pieces)
        {
            if (pieces.Count == 0)
            {
                return(new List <Piece>());
            }
            var remainders = new List <Piece> {
                pieces[0]
            };

            for (int i = 1; i < pieces.Count; i++)
            {
                var newRemainders = new List <Piece>();
                foreach (var rem in remainders)
                {
                    newRemainders.AddRange(PieceFunctions.Unite(rem, pieces[i]));
                }
                remainders = newRemainders;
            }

            return(remainders);
        }
Beispiel #2
0
        internal static SetNode Subtract(SetNode A, SetNode B)
        {
            if (A.Type == SetNode.NodeType.OPERATOR || B.Type == SetNode.NodeType.OPERATOR)
            {
                return(A - B);
            }
            var(goodAPieces, badAPieces) = GatherEvaluablePieces(A as Set);
            var(goodBPieces, badBPieces) = GatherEvaluablePieces(B as Set);
            var newGoodPieces = new List <Piece>();

            newGoodPieces.AddRange(goodAPieces);
            foreach (var goodB in goodBPieces)
            {
                var newNewGoodPieces = new List <Piece>();
                foreach (var newGoodPiece in newGoodPieces)
                {
                    newNewGoodPieces.AddRange(PieceFunctions.Subtract(newGoodPiece, goodB));
                }
                newGoodPieces = newNewGoodPieces;
            }

            newGoodPieces.AddRange(badAPieces);
            var newSet = new Set {
                Pieces = newGoodPieces
            };

            if (badBPieces.Count == 0)
            {
                return(newSet);
            }
            else
            {
                return(new OperatorSet(OperatorSet.OperatorType.COMPLEMENT, newSet, new Set {
                    Pieces = badBPieces
                }));
            }
        }
        public static SetNode Invert(SetNode A)
        {
            if (A.Type == SetNode.NodeType.OPERATOR)
            {
                return(new OperatorSet(OperatorSet.OperatorType.INVERSION, A));
            }
            var(goodAPieces, badAPieces) = GatherEvaluablePieces(A as Set);
            var remainders = new List <Piece> {
                Piece.CreateUniverse()
            };

            foreach (var good in goodAPieces)
            {
                var newRemainders = new List <Piece>();
                foreach (var rem in remainders)
                {
                    newRemainders.AddRange(PieceFunctions.Subtract(rem, good));
                }
                remainders = newRemainders;
            }

            var newSet = new Set {
                Pieces = remainders
            };

            if (badAPieces.Count == 0)
            {
                return(newSet);
            }
            else
            {
                return(new OperatorSet(OperatorSet.OperatorType.COMPLEMENT, newSet, new Set {
                    Pieces = badAPieces
                }));
            }
        }
        /// <summary>
        /// Intersects two nodes. If
        /// </summary>
        /// <param name="A"></param>
        /// <param name="B"></param>
        /// <returns></returns>
        public static SetNode Intersect(SetNode A, SetNode B)
        {
            if (A.Type == SetNode.NodeType.OPERATOR || B.Type == SetNode.NodeType.OPERATOR)
            {
                return(new OperatorSet(OperatorSet.OperatorType.INTERSECTION, A, B));
            }
            var(goodAPieces, badAPieces) = GatherEvaluablePieces(A as Set);
            var(goodBPieces, badBPieces) = GatherEvaluablePieces(B as Set);
            if (goodAPieces.Count * goodBPieces.Count == 0)
            {
                return(new OperatorSet(OperatorSet.OperatorType.INTERSECTION, A, B));
            }
            var newPieces = new List <Piece>();

            foreach (var goodAPiece in goodAPieces)
            {
                foreach (var goodBPiece in goodBPieces)
                {
                    var piece = PieceFunctions.Intersect(goodAPiece, goodBPiece);
                    if (!(piece is null) && PieceFunctions.IsPieceCorrect(piece))
                    {
                        newPieces.Add(piece);
                    }
                }
            }
            var union = UniteList(newPieces);
            var badA  = new Set {
                Pieces = badAPieces
            };
            var badB = new Set {
                Pieces = badBPieces
            };

            if (union.Count == 0)
            {
                if (badA.IsEmpty() || badB.IsEmpty())
                {
                    return(new Set());
                }
                else
                {
                    return(new OperatorSet(OperatorSet.OperatorType.INTERSECTION, badA, badB));
                }
            }
            var united = new Set {
                Pieces = union
            };

            if (badBPieces.Count + badAPieces.Count == 0)
            {
                return(united);
            }

            /*
             * A = A1 or A2 (A1 - good, A2 - bad)
             * B = B1 or B2 (B1 - good, B2 - bad)
             * A & B = (A1 & B1) | (A1 & B2) | (A2 & B1) | (A2 & B2)
             */
            var goodA = new Set {
                Pieces = goodAPieces
            };
            var goodB = new Set {
                Pieces = goodBPieces
            };

            return(OperatorSet.Or(
                       united,                               // A1 & B1
                       OperatorSet.Or(
                           OperatorSet.And(                  // A2 & B2
                               badA,
                               badB
                               ),
                           OperatorSet.Or(
                               OperatorSet.And(badA, goodB), // A2 & B1
                               OperatorSet.And(badB, goodA)  // A1 & B2
                               ))));
        }