예제 #1
0
        public static Tuple <int?, int?> BoundRange(this Functor <int> functor)
        {
            var binaryExpression = functor?.Expression?.Body as BinaryExpression;

            if (binaryExpression == null)
            {
                return(null);
            }
            var left  = ReduceExpression(binaryExpression.Left);
            var right = ReduceExpression(binaryExpression.Right);

            if (left == null || right == null)
            {
                return(null);
            }
            if (!((left is ParameterExpression || left is ConstantExpression) && (right is ParameterExpression || right is ConstantExpression)))
            {
                return(null);
            }
            if (binaryExpression.NodeType == ExpressionType.Equal)
            {
                var v = ToInt(left.As <ConstantExpression>() ?? right.As <ConstantExpression>());
                return(Tuple.Create(v, v + 1));
            }
            if (binaryExpression.NodeType == ExpressionType.LessThan)
            {
                var l = ToInt(left.As <ConstantExpression>());
                var r = ToInt(right.As <ConstantExpression>());
                if (l != null)
                {
                    return(Tuple.Create(l + 1, (int?)null));
                }
                if (r != null)
                {
                    return(Tuple.Create((int?)null, r));
                }
            }
            if (binaryExpression.NodeType == ExpressionType.LessThanOrEqual)
            {
                var l = ToInt(left.As <ConstantExpression>());
                var r = ToInt(right.As <ConstantExpression>());
                if (l != null)
                {
                    return(Tuple.Create(l, (int?)null));
                }
                if (r != null)
                {
                    return(Tuple.Create((int?)null, r + 1));
                }
            }
            if (binaryExpression.NodeType == ExpressionType.GreaterThan)
            {
                var l = ToInt(left.As <ConstantExpression>());
                var r = ToInt(right.As <ConstantExpression>());
                if (l != null)
                {
                    return(Tuple.Create((int?)null, l));
                }
                if (r != null)
                {
                    return(Tuple.Create(r + 1, (int?)null));
                }
            }
            if (binaryExpression.NodeType == ExpressionType.GreaterThanOrEqual)
            {
                var l = ToInt(left.As <ConstantExpression>());
                var r = ToInt(right.As <ConstantExpression>());
                if (l != null)
                {
                    return(Tuple.Create((int?)null, l + 1));
                }
                if (r != null)
                {
                    return(Tuple.Create(r, (int?)null));
                }
            }
            return(null);
        }
예제 #2
0
 public Set(Functor <T> condition) : this(new[] { new[] { condition } })
 {
 }