Exemplo n.º 1
0
        // Constructors

        public PairComparer(IComparerProvider provider, ComparisonRules comparisonRules)
            : base(provider, comparisonRules)
        {
            Pair <T1, T2> minValue, maxValue, deltaValue;
            bool          hasMinValue = false, hasMaxValue = false, hasDeltaValue = false;

            minValue = maxValue = deltaValue = new Pair <T1, T2>(default(T1), default(T2));

            if (BaseComparer1.ValueRangeInfo.HasMinValue & BaseComparer2.ValueRangeInfo.HasMinValue)
            {
                minValue    = new Pair <T1, T2>(BaseComparer1.ValueRangeInfo.MinValue, BaseComparer2.ValueRangeInfo.MinValue);
                hasMinValue = true;
            }
            if (BaseComparer1.ValueRangeInfo.HasMaxValue & BaseComparer2.ValueRangeInfo.HasMaxValue)
            {
                maxValue    = new Pair <T1, T2>(BaseComparer1.ValueRangeInfo.MaxValue, BaseComparer2.ValueRangeInfo.MaxValue);
                hasMaxValue = true;
            }
            if (BaseComparer1.ValueRangeInfo.HasDeltaValue & BaseComparer2.ValueRangeInfo.HasDeltaValue)
            {
                deltaValue    = new Pair <T1, T2>(BaseComparer1.ValueRangeInfo.DeltaValue, BaseComparer2.ValueRangeInfo.DeltaValue);
                hasDeltaValue = true;
            }
            ValueRangeInfo = new ValueRangeInfo <Pair <T1, T2> >(hasMinValue, minValue, hasMaxValue, maxValue, hasDeltaValue, deltaValue);
        }
Exemplo n.º 2
0
        // Constructors

        public GuidComparer(IComparerProvider provider, ComparisonRules comparisonRules)
            : base(provider, comparisonRules)
        {
            ValueRangeInfo = new ValueRangeInfo <Guid>(
                true, Guid.Empty,
                true, new Guid(0xFFFFFFFF, 0xFFFF, 0xFFFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF),
                false, Guid.Empty);
        }
Exemplo n.º 3
0
 /// <summary>
 /// Deserializes the instance of this class.
 /// </summary>
 /// <param name="info">Serialization info.</param>
 /// <param name="context">Streaming context.</param>
 private AdvancedComparerStruct(SerializationInfo info, StreamingContext context)
 {
     Comparer        = (AdvancedComparer <T>)info.GetValue("Comparer", typeof(AdvancedComparer <T>));
     Compare         = Comparer?.Compare;
     Equals          = Comparer?.Equals;
     GetHashCode     = Comparer?.GetHashCode;
     GetNearestValue = Comparer?.GetNearestValue;
     ValueRangeInfo  = Comparer?.ValueRangeInfo;
 }
Exemplo n.º 4
0
        // Constructors

        /// <summary>
        /// Initializes a new instance of this type.
        /// </summary>
        /// <param name="comparer">Comparer to provide the delegates for.</param>
        private AdvancedComparerStruct(AdvancedComparer <T> comparer)
        {
            Comparer        = comparer;
            Compare         = Comparer?.Compare;
            Equals          = Comparer?.Equals;
            GetHashCode     = Comparer?.GetHashCode;
            GetNearestValue = Comparer?.GetNearestValue;
            ValueRangeInfo  = Comparer?.ValueRangeInfo;
        }
Exemplo n.º 5
0
        // Constructors

        public EnumComparer(IComparerProvider provider, ComparisonRules comparisonRules)
            : base(provider, comparisonRules)
        {
            valueToIndex   = new Dictionary <TEnum, int>();
            values         = BuildValues(out var valueCount);
            maxIndex       = valueCount - 1;
            ValueRangeInfo = new ValueRangeInfo <TEnum>(
                true, values[0],
                true, values[valueCount - 1],
                false, default(TEnum));
        }
        // Constructors

        /// <summary>
        /// Initializes a new instance of this type.
        /// </summary>
        /// <param name="provider">Comparer provider this comparer is bound to.</param>
        /// <param name="comparisonRules">Comparison rules.</param>
        public AdvancedComparerBase(IComparerProvider provider, ComparisonRules comparisonRules)
        {
            ArgumentValidator.EnsureArgumentNotNull(provider, "provider");
            valueRangeInfo = new ValueRangeInfo <T>(
                false, default(T),
                false, default(T),
                false, default(T));
            this.provider              = provider;
            ComparisonRules            = comparisonRules;
            DefaultDirectionMultiplier = comparisonRules.Value.Direction == Direction.Negative ? -1 : 1;
        }
Exemplo n.º 7
0
 public EnumComparer(SerializationInfo info, StreamingContext context)
     : base(info, context)
 {
     valueToIndex   = new Dictionary <TEnum, int>();
     values         = BuildValues(out var valueCount);
     maxIndex       = valueCount - 1;
     ValueRangeInfo = new ValueRangeInfo <TEnum>(
         true, values[0],
         true, values[valueCount - 1],
         false, default(TEnum));
 }
Exemplo n.º 8
0
        // Constructors

        /// <summary>
        /// Initializes new instance of this type.
        /// </summary>
        public CastingComparer(AdvancedComparer <TSource> sourceComparer)
            : base(sourceComparer.Provider, sourceComparer.ComparisonRules)
        {
            this.sourceComparer = sourceComparer;
            ValueRangeInfo <TSource> vi = sourceComparer.ValueRangeInfo;

            ValueRangeInfo =
                new ValueRangeInfo <TTarget>(
                    vi.HasMinValue,
                    vi.HasMinValue ? toTarget(vi.MinValue) : default(TTarget),
                    vi.HasMaxValue,
                    vi.HasMaxValue ? toTarget(vi.MaxValue) : default(TTarget),
                    vi.HasDeltaValue,
                    vi.HasDeltaValue ? toTarget(vi.DeltaValue) : default(TTarget));
        }
Exemplo n.º 9
0
        // Constructors

        public EnumComparer(IComparerProvider provider, ComparisonRules comparisonRules)
            : base(provider, comparisonRules)
        {
            valueToIndex = new Dictionary <TEnum, int>();
            Array originalValues = Enum.GetValues(typeof(TEnum));
            int   valueCount     = originalValues.Length;

            if (valueCount < 1)
            {
                valueCount = 1;
                values     = new TEnum[] { default(TEnum) };
                valueToIndex.Add(values[0], 0);
            }
            else
            {
                TEnum[] allValues = new TEnum[valueCount];
                for (int i = 0; i < valueCount; i++)
                {
                    allValues[i] = (TEnum)originalValues.GetValue(i);
                }
                Array.Sort <TEnum>(allValues, (x, y) => BaseComparer.Compare(enumToSystem(x), enumToSystem(y)));
                for (int i = 0; i < valueCount - 1; i++)
                {
                    int j = i + 1;
                    if (BaseComparer.Equals(enumToSystem(allValues[i]), enumToSystem(allValues[j])))
                    {
                        valueCount--;
                        Array.Copy(allValues, j, allValues, i, valueCount - i);
                    }
                }
                values = new TEnum[valueCount];
                Array.Copy(allValues, values, valueCount);
                string[] names = Enum.GetNames(typeof(TEnum));
                for (int i = 0; i < names.Length; i++)
                {
                    TEnum current = (TEnum)originalValues.GetValue(i);
                    if (!valueToIndex.ContainsKey(current))
                    {
                        valueToIndex.Add(current, Array.IndexOf(values, current));
                    }
                }
            }
            maxIndex       = valueCount - 1;
            ValueRangeInfo = new ValueRangeInfo <TEnum>(
                true, values[0],
                true, values[valueCount - 1],
                false, default(TEnum));
        }
Exemplo n.º 10
0
        private void Initialize()
        {
            ValueRangeInfo <T> baseValueRangeInfo = BaseComparer.ValueRangeInfo;

            ValueRangeInfo =
                new ValueRangeInfo <T?>(
                    true, null,
                    baseValueRangeInfo.HasMaxValue,
                    baseValueRangeInfo.HasMaxValue ? baseValueRangeInfo.MaxValue : default(T),
                    baseValueRangeInfo.HasDeltaValue,
                    baseValueRangeInfo.HasDeltaValue ? baseValueRangeInfo.DeltaValue : default(T));

            currentBaseCompare     = BaseComparer.Compare;
            currentBaseEquals      = BaseComparer.Equals;
            currentBaseGetHashCode = BaseComparer.GetHashCode;
        }
Exemplo n.º 11
0
        private void Initialize()
        {
            ValueRangeInfo = new ValueRangeInfo <string>(true, null, false, null, false, null);
            CultureInfo culture = ComparisonRules.Value.Culture;

            if (culture != null)
            {
                stringCompare  = culture.CompareInfo.Compare;
                stringIsSuffix = culture.CompareInfo.IsSuffix;
            }
            else
            {
                stringCompare  = CompareOrdinal;
                stringIsSuffix = CultureInfo.InvariantCulture.CompareInfo.IsSuffix;
            }
        }
Exemplo n.º 12
0
        private void Initialize()
        {
            Pair <T1, T2> minValue, maxValue, deltaValue;
            bool          hasMinValue = false, hasMaxValue = false, hasDeltaValue = false;

            minValue = maxValue = deltaValue = new Pair <T1, T2>(default(T1), default(T2));

            if (BaseComparer1.ValueRangeInfo.HasMinValue & BaseComparer2.ValueRangeInfo.HasMinValue)
            {
                minValue    = new Pair <T1, T2>(BaseComparer1.ValueRangeInfo.MinValue, BaseComparer2.ValueRangeInfo.MinValue);
                hasMinValue = true;
            }
            if (BaseComparer1.ValueRangeInfo.HasMaxValue & BaseComparer2.ValueRangeInfo.HasMaxValue)
            {
                maxValue    = new Pair <T1, T2>(BaseComparer1.ValueRangeInfo.MaxValue, BaseComparer2.ValueRangeInfo.MaxValue);
                hasMaxValue = true;
            }
            if (BaseComparer1.ValueRangeInfo.HasDeltaValue & BaseComparer2.ValueRangeInfo.HasDeltaValue)
            {
                deltaValue    = new Pair <T1, T2>(BaseComparer1.ValueRangeInfo.DeltaValue, BaseComparer2.ValueRangeInfo.DeltaValue);
                hasDeltaValue = true;
            }
            ValueRangeInfo = new ValueRangeInfo <Pair <T1, T2> >(hasMinValue, minValue, hasMaxValue, maxValue, hasDeltaValue, deltaValue);
        }
Exemplo n.º 13
0
        // Constructors

        public DoubleComparer(IComparerProvider provider, ComparisonRules comparisonRules)
            : base(provider, comparisonRules)
        {
            ValueRangeInfo = new ValueRangeInfo <double>(true, double.MinValue, true, double.MaxValue, false, 0d);
        }
Exemplo n.º 14
0
        // Constructors

        public ArrayComparer(IComparerProvider provider, ComparisonRules comparisonRules)
            : base(provider, comparisonRules)
        {
            ValueRangeInfo = new ValueRangeInfo <T[]>(true, null, false, null, false, null);
        }
Exemplo n.º 15
0
        // Constructors

        public EnumerableInterfaceComparer(IComparerProvider provider, ComparisonRules comparisonRules)
            : base(provider, comparisonRules)
        {
            ValueRangeInfo = new ValueRangeInfo <TEnumerable>(true, null, false, null, false, null);
        }
Exemplo n.º 16
0
        // Constructors

        public SByteComparer(IComparerProvider provider, ComparisonRules comparisonRules)
            : base(provider, comparisonRules)
        {
            ValueRangeInfo = new ValueRangeInfo <sbyte>(true, SByte.MinValue, true, SByte.MaxValue, true, 1);
        }
Exemplo n.º 17
0
        // Constructors

        public SingleComparer(IComparerProvider provider, ComparisonRules comparisonRules)
            : base(provider, comparisonRules)
        {
            ValueRangeInfo = new ValueRangeInfo <float>(true, float.MinValue, true, float.MaxValue, false, default(float));
        }
Exemplo n.º 18
0
        // Constructors

        public BooleanComparer(IComparerProvider provider, ComparisonRules comparisonRules)
            : base(provider, comparisonRules)
        {
            ValueRangeInfo = new ValueRangeInfo <bool>(true, false, true, true, true, true);
        }
Exemplo n.º 19
0
    // Constructors

    public DecimalComparer(IComparerProvider provider, ComparisonRules comparisonRules)
      : base(provider, comparisonRules)
    {
      ValueRangeInfo = new ValueRangeInfo<decimal>(true, decimal.MinValue, true, decimal.MaxValue, false, 0m);
    }
Exemplo n.º 20
0
        // Constructors

        public CharComparer(IComparerProvider provider, ComparisonRules comparisonRules)
            : base(provider, comparisonRules)
        {
            ValueRangeInfo = new ValueRangeInfo <char>(true, Char.MinValue, true, Char.MaxValue, true, '\x0001');
        }
Exemplo n.º 21
0
        // Constructors

        public Int64Comparer(IComparerProvider provider, ComparisonRules comparisonRules)
            : base(provider, comparisonRules)
        {
            ValueRangeInfo = new ValueRangeInfo <long>(true, long.MinValue, true, long.MaxValue, true, 1);
        }
Exemplo n.º 22
0
        // Constructors

        public Int32Comparer(IComparerProvider provider, ComparisonRules comparisonRules)
            : base(provider, comparisonRules)
        {
            ValueRangeInfo = new ValueRangeInfo <int>(true, int.MinValue, true, int.MaxValue, true, 1);
        }
Exemplo n.º 23
0
        // Constructors

        public UInt16Comparer(IComparerProvider provider, ComparisonRules comparisonRules)
            : base(provider, comparisonRules)
        {
            ValueRangeInfo = new ValueRangeInfo <ushort>(true, ushort.MinValue, true, ushort.MaxValue, true, 1);
        }
Exemplo n.º 24
0
 /// <summary>
 /// Deserializes the instance of this class.
 /// </summary>
 /// <param name="info">Serialization info.</param>
 /// <param name="context">Streaming context.</param>
 private AdvancedComparer(SerializationInfo info, StreamingContext context)
     : base(info, context)
 {
     ComparerImplementation         = (IComparer <T>)info.GetValue("ComparerImplementation", WellKnownTypes.Object);
     EqualityComparerImplementation = (IEqualityComparer <T>)info.GetValue("EqualityComparerImplementation", WellKnownTypes.Object);
     // Below code is the same between both primary constructors
     if (Implementation != null)
     {
         Provider        = Implementation.Provider;
         ComparisonRules = Implementation.ComparisonRules;
         ValueTypeComparerBase <T> vtc = Implementation as ValueTypeComparerBase <T>;
         SystemComparer <T>        sc  = Implementation as SystemComparer <T>;
         // Trying to get faster delegates
         if (sc != null)
         {
             if (ComparisonRules.Value.Direction == Direction.Positive)
             {
                 Compare = SystemComparerStruct <T> .Instance.Compare;
             }
             Equals      = SystemComparerStruct <T> .Instance.Equals;
             GetHashCode = SystemComparerStruct <T> .Instance.GetHashCode;
         }
         else if (vtc != null)
         {
             if (ComparisonRules.Value.Direction == Direction.Positive && vtc.UsesDefaultCompare)
             {
                 Compare = SystemComparerStruct <T> .Instance.Compare;
             }
             if (vtc.UsesDefaultEquals)
             {
                 Equals = SystemComparerStruct <T> .Instance.Equals;
             }
             if (vtc.UsesDefaultGetHashCode)
             {
                 GetHashCode = SystemComparerStruct <T> .Instance.GetHashCode;
             }
         }
         // Setting interface comparers, if unassigned
         if (Compare == null)
         {
             Compare = ComparerImplementation.Compare;
         }
         if (Equals == null)
         {
             Equals = EqualityComparerImplementation.Equals;
         }
         if (GetHashCode == null)
         {
             GetHashCode = EqualityComparerImplementation.GetHashCode;
         }
         GetNearestValue = Implementation.GetNearestValue;
         ApplyRules      = Implementation.ApplyRules;
         ValueRangeInfo  = Implementation.ValueRangeInfo;
     }
     else
     {
         ComparisonRules = ComparisonRules.Positive;
         if (ComparerImplementation != null)
         {
             Compare = ComparerImplementation.Compare;
         }
         if (EqualityComparerImplementation != null)
         {
             Equals      = EqualityComparerImplementation.Equals;
             GetHashCode = EqualityComparerImplementation.GetHashCode;
         }
     }
 }