예제 #1
0
파일: Unity.cs 프로젝트: qwdingyu/C-
        public override bool Validating(object value)
        {
            T newvalue;

            try
            {
                newvalue = CastValue <T>(value);
            }
            catch
            {
                return(false);
            }

            if (Min.CompareTo(default(T)) > 0)
            {
                if (newvalue.CompareTo(Min) < 0)
                {
                    return(false);
                }
            }
            if (Max.CompareTo(default(T)) > 0)
            {
                if (newvalue.CompareTo(Max) > 0)
                {
                    return(false);
                }
            }
            return(true);
        }
예제 #2
0
        public void Summarize(object value)
        {
            string myvalue = value.ToString();

            Count++;
            if (Count > 1)
            {
                if (Function == SummaryFunctionsText.Max)
                {
                    Max = (Max.CompareTo(myvalue) > 0) ? Max : myvalue;
                }
                else if (Function == SummaryFunctionsText.Min)
                {
                    Min = (Min.CompareTo(myvalue) > 0) ? myvalue : Min;
                }
            }
            else
            {
                if (Function == SummaryFunctionsText.Max)
                {
                    Max = myvalue;
                }
                else if (Function == SummaryFunctionsText.Min)
                {
                    Min = myvalue;
                }
            }
        }
예제 #3
0
        internal bool FitsValue(ulong value) // TODO: doc and make public
        {
            if (!IsNumeric)
            {
                throw new InvalidOperationException(Numeric.ERR_NOT_ISNUMERIC);
            }

            return(Max.CompareTo(value) >= 0 && Min.CompareTo(value) <= 0);
        }
예제 #4
0
        public NumericValue(T value, T min, T max)
        {
            _value = value.CompareTo(max) > 0 ? max : value.CompareTo(min) < 0 ? min : value;
            Max    = max;
            Min    = min;

            if (Max.CompareTo(Min) < 0)
            {
                throw new ArgumentException();
            }
        }
예제 #5
0
        /// <summary> Returns the range that represents the intersection of this range and <code>value</code>. </summary>
        /// <param name="value">The range to intersect with.</param>
        /// <returns>
        ///     A range that contains the values that are common in both ranges,
        ///     or null if there is no intersection.
        /// </returns>
        public Range <T> Intersect([NotNull] Range <T> value)
        {
            if (!Overlaps(value))
            {
                return(null);
            }

            var start = Min.CompareTo(value.Min) > 0 ? Min : value.Min;

            return(Max.CompareTo(value.Max) < 0 ? new Range <T>(start, Max) : new Range <T>(start, value.Max));
        }
예제 #6
0
        /// <inheritdoc />
        public override string ToString()
        {
            var result = new StringBuilder();

            result.Append(Min.CompareTo(IncludedMin) == 0 ? '[' : ')');
            result.Append(Min);
            result.Append("..");
            result.Append(Max);
            result.Append(Max.CompareTo(IncludedMax) == 0 ? ']' : '(');
            return(result.ToString());
        }
예제 #7
0
 public bool Includes(T value)
 {
     if (Min.CompareTo(value) > 0)
     {
         return(false);
     }
     if (Max.CompareTo(value) < 0)
     {
         return(false);
     }
     return(true);
 }
예제 #8
0
        public NumericValue(T value, T min, T max)
        {
            //This is made to prevent editor overriding values changed using text editors. StructDataEditor still cant go beyond bounds, but external text editors can.
            _value = value;
            //_value = value.CompareTo(max) > 0 ? max : value.CompareTo(min) < 0 ? min : value;
            Max = max;
            Min = min;

            if (Max.CompareTo(Min) < 0)
            {
                throw new ArgumentException();
            }
        }
예제 #9
0
파일: TcpStream.cs 프로젝트: lulzzz/Tarzan
 internal Range <T> Intersect(Range <T> other)
 {
     if (IsOverlapped(other))
     {
         var newMin = Min.CompareTo(other.Min) > 0 ? Min : other.Min;
         var newMax = Max.CompareTo(other.Max) < 0 ? Max : other.Max;
         return(new Range <T>(newMin, newMax));
     }
     else
     {
         return(null);
     }
 }
예제 #10
0
        public IEnumerable <T> Iterate([NotNull] Func <T, T> incrementor)
        {
            yield return(Min);

            var item = incrementor(Min);

            while (Max.CompareTo(item) >= 0)
            {
                yield return(item);

                item = incrementor(item);
            }
        }
예제 #11
0
 protected Pv2DigitParamType(string paramName, string description, string groupName, string formatString,
                             string units, TValue min, TValue max, TValue defaultValue, Pv2ParamFlags flags) : base(paramName,
                                                                                                                    description, groupName, defaultValue, flags)
 {
     FormatString = formatString;
     Units        = units;
     Min          = min;
     Max          = max;
     if (Max.CompareTo(Min) < 0)
     {
         throw new ArgumentException($"Max < Min {Max} <= {Min}");
     }
     Validate(DefaultValue);
 }
예제 #12
0
        public bool GetIntersection(Interval other, out Interval result)
        {
            int a = Min.CompareTo(other.Min);
            int b = Max.CompareTo(other.Max);
            int c = Min.CompareTo(other.Max);
            int d = Max.CompareTo(other.Min);

            result = new Interval(
                (a >= 0) ? Min : other.Min,
                (b <= 0) ? Max : other.Max
                );

            return(Math.Sign(c) != Math.Sign(d));
        }
예제 #13
0
        /// <summary>
        /// Sets Min and Max properties according to given range type and current value
        /// </summary>
        /// <param name="rangeKind">The type of range</param>
        public void SetRange(RangeKind rangeKind)
        {
            var rangeValue = _ranges[rangeKind];

            Min = Minus(Value, rangeValue);
            Max = Plus(Value, rangeValue);
            // check if we are outside the (DefaultMin, DefaultMax)
            if (Min.CompareTo(DefaultMin) < 0)
            {
                Min = DefaultMin;
            }
            if (Max.CompareTo(DefaultMax) > 0)
            {
                Max = DefaultMax;
            }
        }
예제 #14
0
        /// <summary> Returns the range that represents the union of this range and <code>value</code>. </summary>
        /// <param name="value">The range to union with.</param>
        /// <returns>A range that contains both ranges.</returns>
        public Range <T> Union([NotNull] Range <T> value)
        {
            // If either one is a subset of the other, then it is the union
            if (Contains(value))
            {
                return(this);
            }

            if (value.Contains(this))
            {
                return(value);
            }

            var start = Min.CompareTo(value.Min) < 0 ? Min : value.Min;

            return(Max.CompareTo(value.Max) > 0 ? new Range <T>(start, Max) : new Range <T>(start, value.Max));
        }
예제 #15
0
        public IEnumerable <Range <T> > Split([NotNull] T position)
        {
            if (!Contains(position))
            {
                yield break;
            }

            if ((Min.CompareTo(position) == 0) || (Max.CompareTo(position) == 0))
            {
                // The position is at a boundary, so a split does not happen
                yield return(this);
            }
            else
            {
                yield return(new Range <T>(Min, position));

                yield return(new Range <T>(position, Max));
            }
        }
예제 #16
0
        public Range <T> Complement([NotNull] Range <T> value)
        {
            if (Contains(value))
            {
                return(null);
            }

            if (Overlaps(value))
            {
                // If value's start and end straddle our start, move our start up to be values end.
                var start = (Min.CompareTo(value.Min) > 0) && (Min.CompareTo(value.Max) < 0) ? value.Max : Min;

                // If value's start and end straddle our end, move our end back down to be values start.
                return((Max.CompareTo(value.Min) > 0) && (Max.CompareTo(value.Max) < 0)
                    ? new Range <T>(start, value.Min)
                    : new Range <T>(start, Max));
            }

            return(this);
        }
예제 #17
0
 /// <summary>
 /// checks if the given value is in range of this entry
 /// </summary>
 /// <param name="value"></param>
 /// <returns></returns>
 public virtual bool InRange(TDimension value)
 {
     if (hasValue)
     {
         if (Value == null && value == null)
         {
             return(true);
         }
         else if (Value == null)
         {
             return(false);
         }
         else
         {
             return(Value.CompareTo(value) == 0);
         }
     }
     else
     {
         return(Min.CompareTo(value) <= 0 && Max.CompareTo(value) > 0);
     }
 }
예제 #18
0
        internal bool FitsIntValue <TIntegral>(TIntegral value) // TODO: doc and make public?
        {
            if (!IsNumeric)
            {
                throw new InvalidOperationException(Numeric.ERR_NOT_ISNUMERIC);
            }

            var conv = value as IConvertible;

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

            var tcVal = conv.GetTypeCode();

            if (!Numeric.IsInteger(tcVal))
            {
                return(false);
            }

            // (check if TypeCodes are identical first before proceeding to more expensive operations)
            if ((int)tcVal == tc)
            {
                return(true);
            }

            if (Numeric.IsUnsigned(tcVal))
            {
                ulong v64 = conv.ToUInt64(null);
                return(Max.CompareTo(v64) >= 0 && Min.CompareTo(v64) <= 0);
            }
            else
            {
                long v64 = conv.ToInt64(null);
                return(Max.CompareTo(v64) >= 0 && Min.CompareTo(v64) <= 0);
            }
        }
예제 #19
0
        public List <T> CheckOverlapping(Range <T> other)
        {
            if (Min.CompareTo(other.Max) <= 0 && other.Min.CompareTo(Max) <= 0)
            {
                T begin = Min;
                T end   = Max;

                if (Max.CompareTo(other.Max) >= 0)
                {
                    begin = other.Max;
                }
                if (Min.CompareTo(other.Min) <= 0)
                {
                    end = other.Min;
                }

                List <T> res = new List <T>();
                res.Add(begin);
                res.Add(end);
                return(res);
            }
            return(null);
        }
예제 #20
0
        /// <summary>
        /// Compares <b><i>this</i></b> to <paramref name="other"/> using <see cref="CompareTo"/> calls against multiple properties:
        /// <li><see cref="Min"/></li>
        /// <li><see cref="Max"/></li>
        /// <li><see cref="Average"/></li>
        /// Returns -1 or 1 if <b>any</b> properties return that value and <b>none</b> return the other;
        /// otherwise, returns 0.
        /// </summary>
        /// <param name="other"></param>
        /// <returns></returns>
        public int CompareTo([NotNull] AggregateExecutionTime other)
        {
            if (other == null)
            {
                throw new ArgumentNullException(nameof(other));
            }

            return(Average.CompareTo(other.Average));

            var minCompare = Min.CompareTo(other.Min);
            var maxCompare = Max.CompareTo(other.Max);
            var avgCompare = Average.CompareTo(other.Average);

            var compares = new int[] { minCompare, maxCompare, avgCompare };

            Console.WriteLine(
                new Dictionary <object, object>()
            {
                [$"Min: {Min}, {other.Min}"]         = minCompare,
                [$"Max: {Max}, {other.Max}"]         = maxCompare,
                [$"Avg: {Average}, {other.Average}"] = avgCompare
            }.Prettify()
                );
            if (compares.Any(it => it > 0) && compares.All(it => it >= 0))
            {
                return(1);
            }
            else if (compares.Any(it => it < 0) && compares.All(it => it <= 0))
            {
                return(-1);
            }
            else
            {
                return(0);
            }
        }
예제 #21
0
 /// <summary> Indicates if the range contains <code>value</code>. </summary>
 /// <param name="value">A range to test.</param>
 /// <returns>true if the entire range in <code>value</code> is within this range.</returns>
 public bool Contains([NotNull] Range <T> value)
 {
     return((Min.CompareTo(value.Min) <= 0) && (Max.CompareTo(value.Max) >= 0));
 }
예제 #22
0
 public bool Includes(T item)
 {
     return(Min.CompareTo(item) <= 0 && Max.CompareTo(item) >= 0);
 }
예제 #23
0
 /// <summary> Indicates if the range contains <code>value</code>. </summary>
 /// <param name="value">The value to look for.</param>
 /// <returns>true if the range contains <code>value</code>, false otherwise.</returns>
 public bool Contains([NotNull] T value)
 {
     return((Min.CompareTo(value) <= 0) && (Max.CompareTo(value) >= 0));
 }