コード例 #1
0
        /// <inheritdoc cref="SumSatisfies(IEnumerable{uint}, ComparisonKinds, uint)"/>
        public static bool SumSatisfies(this IEnumerable <ulong> values, ComparisonKinds comparisonKinds, ulong target)
        {
            if (comparisonKinds is ComparisonKinds.All)
            {
                return(true);
            }

            if (comparisonKinds is ComparisonKinds.Equal or ComparisonKinds.Different)
            {
                return(comparisonKinds.Matches(values.Sum().GetComparisonResult(target)));
            }

            ulong sum = 0;
            bool  initialPredicateValidity = sum.SatisfiesComparison(target, comparisonKinds);

            foreach (ulong value in values)
            {
                sum += value;
                bool currentPredicateValidity = sum.SatisfiesComparison(target, comparisonKinds);
                if (currentPredicateValidity != initialPredicateValidity)
                {
                    return(currentPredicateValidity);
                }
            }

            return(initialPredicateValidity);
        }
コード例 #2
0
 private static bool SatisfiesComparison(int comparison, ComparisonKinds kinds)
 {
     return(kinds switch
     {
         ComparisonKinds.Less => comparison < 0,
         ComparisonKinds.Equal => comparison == 0,
         ComparisonKinds.Greater => comparison > 0,
         ComparisonKinds.Different => comparison != 0,
         ComparisonKinds.LessOrEqual => comparison <= 0,
         ComparisonKinds.GreaterOrEqual => comparison >= 0,
         ComparisonKinds.All => true,
         _ => false,
     });
コード例 #3
0
 /// <summary>Determines whether the comparison of <paramref name="value"/> and <paramref name="other"/> satisfies the given comparison.</summary>
 /// <param name="value">The provided value to compare.</param>
 /// <param name="other">The other value to compare.</param>
 /// <param name="kinds">The comparison kinds.</param>
 /// <returns><see langword="true"/> if the comparison of <paramref name="value"/> with <paramref name="other"/> is satsified, otherwise <see langword="false"/>.</returns>
 public static bool SatisfiesComparison(this IComparable value, object other, ComparisonKinds kinds)
 {
     return(SatisfiesComparison(value.CompareTo(other), kinds));
 }
コード例 #4
0
 /// <summary>Determines whether the comparison of <paramref name="value"/> and <paramref name="other"/> satisfies the given comparison.</summary>
 /// <typeparam name="T">The type of the value that <paramref name="value"/> can be compared with.</typeparam>
 /// <param name="value">The provided value to compare.</param>
 /// <param name="other">The other value to compare.</param>
 /// <param name="kinds">The comparison kinds.</param>
 /// <returns><see langword="true"/> if the comparison of <paramref name="value"/> with <paramref name="other"/> is satsified, otherwise <see langword="false"/>.</returns>
 public static bool SatisfiesComparison <T>(this IComparable <T> value, T other, ComparisonKinds kinds)
 {
     return(SatisfiesComparison(value.CompareTo(other), kinds));
 }