예제 #1
0
        public int CompareTo(BinaryUnion other)
        {
            if (other == null)
            {
                return(1);
            }

            ISet <Expression> visitedExpressions = new HashSet <Expression>();
            ISet <Expression> expression1        = UnionHelper.Iterate(visitedExpressions, Left.AsSequence().Concat(Right)).ToSortedSet();

            visitedExpressions.Clear();
            ISet <Expression> expression2 = UnionHelper.Iterate(visitedExpressions, other.Left.AsSequence().Concat(other.Right)).ToSortedSet();

            return(expression1.SequenceCompare(expression2));
        }
예제 #2
0
파일: Union.cs 프로젝트: apodavalov/FLaG
        public bool Equals(Union other)
        {
            if (other == null)
            {
                return(false);
            }

            ISet <Expression> visitedExpressions = new HashSet <Expression>();
            ISet <Expression> expression1        = UnionHelper.Iterate(visitedExpressions, Expressions).ToSortedSet();

            visitedExpressions.Clear();
            ISet <Expression> expression2 = UnionHelper.Iterate(visitedExpressions, other.Expressions).ToSortedSet();

            return(expression1.SequenceEqual(expression2));
        }
예제 #3
0
파일: Union.cs 프로젝트: apodavalov/FLaG
        public int CompareTo(Union other)
        {
            if (other == null)
            {
                return(1);
            }

            ISet <Expression> visitedExpressions = new HashSet <Expression>();
            ISet <Expression> expression1        = UnionHelper.Iterate(visitedExpressions, Expressions).ToSortedSet();

            visitedExpressions.Clear();
            ISet <Expression> expression2 = UnionHelper.Iterate(visitedExpressions, other.Expressions).ToSortedSet();

            return(expression1.SequenceCompare(expression2));
        }
예제 #4
0
        public override Expression Optimize()
        {
            ISet <Expression> visitedExpressions = new HashSet <Expression>();

            return(new Union(UnionHelper.Iterate(visitedExpressions, Left.AsSequence().Concat(Right))).Optimize());
        }
예제 #5
0
        internal override void ToString(StringBuilder builder)
        {
            ISet <Expression> visitedExpressions = new HashSet <Expression>();

            UnionHelper.ToString(builder, UnionHelper.Iterate(visitedExpressions, Left.AsSequence().Concat(Right)).ToSortedSet().AsReadOnly(), Priority);
        }
예제 #6
0
파일: Union.cs 프로젝트: apodavalov/FLaG
        private static Expression ExtractFromBrackets(Expression expressionA, Expression expressionB)
        {
            IReadOnlyList <Expression> concatA = expressionA.As <Concat>()?.Expressions;

            if (concatA == null)
            {
                concatA = expressionA.AsSequence().ToList().AsReadOnly();
            }

            IReadOnlyList <Expression> concatB = expressionB.As <Concat>()?.Expressions;

            if (concatB == null)
            {
                concatB = expressionB.AsSequence().ToList().AsReadOnly();
            }

            int left  = 0;
            int right = 0;

            int count = Math.Min(concatA.Count, concatB.Count);

            while (left < count && concatA[left] == concatB[left])
            {
                left++;
            }

            while (right < count - left && concatA[concatA.Count - 1 - right] == concatB[concatB.Count - 1 - right])
            {
                right++;
            }

            if (left == 0 && right == 0)
            {
                return(null);
            }

            IList <Expression>          newConcat   = new List <Expression>();
            IList <Expression>          leftConcat  = new List <Expression>();
            IList <IList <Expression> > middleUnion = new List <IList <Expression> >();
            IList <Expression>          rightConcat = new List <Expression>();

            for (int i = 0; i < left; i++)
            {
                leftConcat.Add(concatA[i]);
            }

            for (int i = 0; i < right; i++)
            {
                rightConcat.Add(concatB[concatB.Count - 1 - i]);
            }

            rightConcat = rightConcat.AsReadOnly().FastReverse().ToList();

            IList <Expression> middleConcat = new List <Expression>();

            for (int i = left; i < concatA.Count - right; i++)
            {
                middleConcat.Add(concatA[i]);
            }

            middleUnion.Add(middleConcat);

            middleConcat = new List <Expression>();

            for (int i = left; i < concatB.Count - right; i++)
            {
                middleConcat.Add(concatB[i]);
            }

            middleUnion.Add(middleConcat);

            newConcat.AddRange(leftConcat);
            newConcat.Add(
                UnionHelper.MakeExpression(
                    middleUnion.Select(
                        c => ConcatHelper.MakeExpression(c.Where(e => e != Empty.Instance).ToList())
                        ).ToList()
                    )
                );
            newConcat.AddRange(rightConcat);

            return(ConcatHelper.MakeExpression(newConcat).Optimize());
        }
예제 #7
0
파일: Union.cs 프로젝트: apodavalov/FLaG
        public override Expression Optimize()
        {
            bool somethingChanged;

            ISet <Expression> visitedExpressions = new HashSet <Expression>();

            ISet <Expression> set = UnionHelper.Iterate(visitedExpressions, Expressions.Select(e => e.Optimize())).ToHashSet();

            do
            {
                somethingChanged = false;

                if (set.Any(e => e.CanBeEmpty()))
                {
                    set = set.Select(e => e.TryToLetItBeEmpty().Optimize()).ToHashSet();
                }

                if (set.Any(e => e != Empty.Instance && e.CanBeEmpty()))
                {
                    set = set.Where(e => e != Empty.Instance).ToHashSet();
                }

                IList <Expression> list = set.ToList();

                for (int i = 0; i < list.Count; i++)
                {
                    for (int j = 0; j < list.Count; j++)
                    {
                        if (i != j && IsASuperSetOfB(list[i], list[j]))
                        {
                            list.RemoveAt(j);
                            j--;

                            if (i > j)
                            {
                                i--;
                            }

                            somethingChanged = true;
                        }
                    }
                }

                for (int i = 0; i < list.Count; i++)
                {
                    for (int j = 0; j < list.Count; j++)
                    {
                        if (i != j)
                        {
                            Expression expression = ExtractFromBrackets(list[i], list[j]);

                            if (expression != null)
                            {
                                list.RemoveAt(j);

                                if (i > j)
                                {
                                    i--;
                                }

                                list.RemoveAt(i);

                                list.Add(expression);

                                j--;

                                somethingChanged = true;
                            }
                        }
                    }
                }

                set = list.ToHashSet();
            } while (somethingChanged);

            return(UnionHelper.MakeExpression(set));
        }
예제 #8
0
파일: Union.cs 프로젝트: apodavalov/FLaG
 internal override void ToString(StringBuilder builder)
 {
     UnionHelper.ToString(builder, Expressions, Priority);
 }