// 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); }
// 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); }
/// <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; }
// 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; }
// 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; }
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)); }
// 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)); }
// 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)); }
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; }
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; } }
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); }
// Constructors public DoubleComparer(IComparerProvider provider, ComparisonRules comparisonRules) : base(provider, comparisonRules) { ValueRangeInfo = new ValueRangeInfo <double>(true, double.MinValue, true, double.MaxValue, false, 0d); }
// Constructors public ArrayComparer(IComparerProvider provider, ComparisonRules comparisonRules) : base(provider, comparisonRules) { ValueRangeInfo = new ValueRangeInfo <T[]>(true, null, false, null, false, null); }
// Constructors public EnumerableInterfaceComparer(IComparerProvider provider, ComparisonRules comparisonRules) : base(provider, comparisonRules) { ValueRangeInfo = new ValueRangeInfo <TEnumerable>(true, null, false, null, false, null); }
// Constructors public SByteComparer(IComparerProvider provider, ComparisonRules comparisonRules) : base(provider, comparisonRules) { ValueRangeInfo = new ValueRangeInfo <sbyte>(true, SByte.MinValue, true, SByte.MaxValue, true, 1); }
// Constructors public SingleComparer(IComparerProvider provider, ComparisonRules comparisonRules) : base(provider, comparisonRules) { ValueRangeInfo = new ValueRangeInfo <float>(true, float.MinValue, true, float.MaxValue, false, default(float)); }
// Constructors public BooleanComparer(IComparerProvider provider, ComparisonRules comparisonRules) : base(provider, comparisonRules) { ValueRangeInfo = new ValueRangeInfo <bool>(true, false, true, true, true, true); }
// Constructors public DecimalComparer(IComparerProvider provider, ComparisonRules comparisonRules) : base(provider, comparisonRules) { ValueRangeInfo = new ValueRangeInfo<decimal>(true, decimal.MinValue, true, decimal.MaxValue, false, 0m); }
// Constructors public CharComparer(IComparerProvider provider, ComparisonRules comparisonRules) : base(provider, comparisonRules) { ValueRangeInfo = new ValueRangeInfo <char>(true, Char.MinValue, true, Char.MaxValue, true, '\x0001'); }
// Constructors public Int64Comparer(IComparerProvider provider, ComparisonRules comparisonRules) : base(provider, comparisonRules) { ValueRangeInfo = new ValueRangeInfo <long>(true, long.MinValue, true, long.MaxValue, true, 1); }
// Constructors public Int32Comparer(IComparerProvider provider, ComparisonRules comparisonRules) : base(provider, comparisonRules) { ValueRangeInfo = new ValueRangeInfo <int>(true, int.MinValue, true, int.MaxValue, true, 1); }
// Constructors public UInt16Comparer(IComparerProvider provider, ComparisonRules comparisonRules) : base(provider, comparisonRules) { ValueRangeInfo = new ValueRangeInfo <ushort>(true, ushort.MinValue, true, ushort.MaxValue, true, 1); }
/// <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; } } }