// 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 /// <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 WrappingComparer(IComparerProvider provider, ComparisonRules comparisonRules) : base(provider, comparisonRules) { ArgumentValidator.EnsureArgumentNotNull(provider, "provider"); BaseComparer1 = provider.GetComparer <TBase1>().ApplyRules(comparisonRules[0]); BaseComparer2 = provider.GetComparer <TBase2>().ApplyRules(comparisonRules[1]); }
// 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); }
// 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; }
// 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 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)); }
/// <summary> /// Performs post-deserialization actions. /// </summary> /// <param name="sender"></param> public virtual void OnDeserialization(object sender) { if (provider == null) { provider = ComparerProvider.Default; } else if (provider.GetType() == typeof(ComparerProvider)) { provider = ComparerProvider.Default; } else if (provider is SystemComparerProvider) { provider = ComparerProvider.System; } cachedComparers = ThreadSafeDictionary <ComparisonRules, AdvancedComparer <T> > .Create(new object()); }
private void Test <T>(IComparerProvider <T> builder) where T : INestedObject { var comparer = builder.GetComparer(); for (var i = 0; i < 10; i++) { var x = (T)(object)_fixture.Create <SealedNestedObject>(); var y = (T)(object)_fixture.Create <SealedNestedObject>(); var expected = string.CompareOrdinal(x.Text, y.Text).Normalize(); var actual = comparer.Compare(x, y).Normalize(); actual.Should().Be(expected, $"\nx: {x.ToJson()},\ny: {y.ToJson()}"); } }
private static void Test <T>(IComparerProvider comparersBuilder, IComparer <T> referenceComparer, int times, int count) { if (referenceComparer == null) { referenceComparer = Helper.DefaultComparer <T>(); } var typedComparer = comparersBuilder.GetComparer <T>(); Parallel.Invoke( () => { Comparison_of_null_with_object_produces_negative_value(referenceComparer, typedComparer); Comparison_of_object_with_null_produces_positive_value(referenceComparer, typedComparer); Comparison_when_both_null_produces_0(referenceComparer, typedComparer); Comparison_with_itself_produces_0(referenceComparer, typedComparer); Comparison_with_same_produces_0(referenceComparer, typedComparer); }, () => Comparisons_work_identical(referenceComparer, typedComparer, times), () => Sorting_must_work_the_same_as_for_reference_comparer(referenceComparer, typedComparer, count), () => Mutate_class_members_and_test_comparison(referenceComparer, typedComparer) ); }
// Constructors public EnumerableInterfaceComparer(IComparerProvider provider, ComparisonRules comparisonRules) : base(provider, comparisonRules) { ValueRangeInfo = new ValueRangeInfo <TEnumerable>(true, null, false, null, false, null); }
// Constructors public ArrayComparer(IComparerProvider provider, ComparisonRules comparisonRules) : base(provider, comparisonRules) { ValueRangeInfo = new ValueRangeInfo <T[]>(true, null, false, null, false, null); }
// 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 SByteComparer(IComparerProvider provider, ComparisonRules comparisonRules) : base(provider, comparisonRules) { ValueRangeInfo = new ValueRangeInfo <sbyte>(true, SByte.MinValue, true, SByte.MaxValue, true, 1); }
// Constructors public BaseComparerWrapper(IComparerProvider provider, ComparisonRules comparisonRules) : base(provider, comparisonRules) { }
// Constructors public Int32Comparer(IComparerProvider provider, ComparisonRules comparisonRules) : base(provider, comparisonRules) { ValueRangeInfo = new ValueRangeInfo <int>(true, int.MinValue, true, int.MaxValue, true, 1); }
// Constructors public AssemblyComparer(IComparerProvider provider, ComparisonRules comparisonRules) : base(provider, comparisonRules) { }
// Constructors public UInt16Comparer(IComparerProvider provider, ComparisonRules comparisonRules) : base(provider, comparisonRules) { ValueRangeInfo = new ValueRangeInfo <ushort>(true, ushort.MinValue, true, ushort.MaxValue, true, 1); }
/// <summary> /// Sets the source provider. /// </summary> /// <typeparam name="TKey">The type of the key.</typeparam> /// <typeparam name="TItem">The type of the item.</typeparam> /// <param name="comparerAgent">The comparer agent.</param> /// <param name="sourceProvider">The source provider of the comparer agent.</param> /// <returns>The comparer agent.</returns> public static IComparerAgent <TKey, TItem> SetSourceProvider <TKey, TItem>(this IComparerAgent <TKey, TItem> comparerAgent, IComparerProvider <TItem> sourceProvider) { comparerAgent.SourceProvider = sourceProvider; return(comparerAgent); }
// Constructors public DecimalComparer(IComparerProvider provider, ComparisonRules comparisonRules) : base(provider, comparisonRules) { ValueRangeInfo = new ValueRangeInfo<decimal>(true, decimal.MinValue, true, decimal.MaxValue, false, 0m); }
/// <summary> /// Sets the destination provider. /// </summary> /// <typeparam name="TKey">The type of the key.</typeparam> /// <typeparam name="TItem">The type of the item.</typeparam> /// <param name="comparerAgent">The comparer agent.</param> /// <param name="destinationProvider">The destination provider of the comparer agent.</param> /// <returns>The comparer agent.</returns> public static IComparerAgent <TKey, TItem> SetDestinationProvider <TKey, TItem>(this IComparerAgent <TKey, TItem> comparerAgent, IComparerProvider <TItem> destinationProvider) { comparerAgent.DestinationProvider = destinationProvider; return(comparerAgent); }
// Constructors public Int64Comparer(IComparerProvider provider, ComparisonRules comparisonRules) : base(provider, comparisonRules) { ValueRangeInfo = new ValueRangeInfo <long>(true, long.MinValue, true, long.MaxValue, true, 1); }
// Constructors public CharComparer(IComparerProvider provider, ComparisonRules comparisonRules) : base(provider, comparisonRules) { ValueRangeInfo = new ValueRangeInfo <char>(true, Char.MinValue, true, Char.MaxValue, true, '\x0001'); }
// Constructors public ValueTypeComparerBase(IComparerProvider provider, ComparisonRules comparisonRules) : base(provider, comparisonRules) { }
// Constructors public BooleanComparer(IComparerProvider provider, ComparisonRules comparisonRules) : base(provider, comparisonRules) { ValueRangeInfo = new ValueRangeInfo <bool>(true, false, true, true, true, true); }
// Constructors internal SystemComparer(IComparerProvider provider, ComparisonRules comparisonRules) : base(provider, comparisonRules) { }
/// <summary> /// Sets the source provider. /// </summary> /// <typeparam name="TKey">The type of the key.</typeparam> /// <param name="comparerAgent">The comparer agent.</param> /// <param name="sourceProvider">The source provider of the comparer agent.</param> /// <returns>The comparer agent.</returns> public static IKeyComparerAgent <TKey> SetSourceProvider <TKey>(this IKeyComparerAgent <TKey> comparerAgent, IComparerProvider <TKey> sourceProvider) { comparerAgent.SourceProvider = sourceProvider; return(comparerAgent); }
// Constructors public TupleDescriptorComparer(IComparerProvider provider, ComparisonRules comparisonRules) : base(provider, comparisonRules) { }
/// <summary> /// Sets the destination provider. /// </summary> /// <typeparam name="TKey">The type of the key.</typeparam> /// <param name="comparerAgent">The comparer agent.</param> /// <param name="destinationProvider">The destination provider of the comparer agent.</param> /// <returns>The comparer agent.</returns> public static IKeyComparerAgent <TKey> SetDestinationProvider <TKey>(this IKeyComparerAgent <TKey> comparerAgent, IComparerProvider <TKey> destinationProvider) { comparerAgent.DestinationProvider = destinationProvider; return(comparerAgent); }
// Constructors public TypeComparer(IComparerProvider provider, ComparisonRules comparisonRules) : base(provider, comparisonRules) { Initialize(); }
public TypePairComparer(IComparerProvider provider, ComparisonRules comparisonRules) : base(provider, comparisonRules) { }