Exemplo n.º 1
0
        public override int Compare(T[] x, T[] y)
        {
            if (x == null)
            {
                return(y == null ? 0 : -DefaultDirectionMultiplier);
            }
            if (y == null)
            {
                return(DefaultDirectionMultiplier);
            }

            var minLength = x.Length;
            var result    = -minLength - 1;

            if (minLength > y.Length)
            {
                minLength = y.Length;
                result    = minLength + 1;
            }
            for (var i = 0; i < minLength;)
            {
                var r = BaseComparer.Compare(x[i], y[i]);
                i++;
                if (r == 0)
                {
                    continue;
                }
                return(r > 0 ? i : -i);
            }
            return(result * (int)ComparisonRules.GetDefaultRuleDirection(minLength));
        }
Exemplo n.º 2
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.º 3
0
        // 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]);
        }
Exemplo n.º 4
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);
        }
        // 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.º 6
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));
        }
Exemplo n.º 7
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.º 8
0
 protected override IAdvancedComparer <Type> CreateNew(ComparisonRules rules)
 {
     return(new TypeComparer(Provider, ComparisonRules.Combine(rules)));
 }
Exemplo n.º 9
0
 protected override IAdvancedComparer <T> CreateNew(ComparisonRules rules)
 => new BaseComparerWrapper <T, TBase>(Provider, ComparisonRules.Combine(rules));
Exemplo n.º 10
0
 protected override IAdvancedComparer <TupleDescriptor> CreateNew(ComparisonRules rules)
 => new TupleDescriptorComparer(Provider, ComparisonRules.Combine(rules));
Exemplo n.º 11
0
 protected override IAdvancedComparer <ushort> CreateNew(ComparisonRules rules)
 {
     return(new UInt16Comparer(Provider, ComparisonRules.Combine(rules)));
 }
Exemplo n.º 12
0
        // Constructors

        public ArrayComparer(IComparerProvider provider, ComparisonRules comparisonRules)
            : base(provider, comparisonRules)
        {
            ValueRangeInfo = new ValueRangeInfo <T[]>(true, null, false, null, false, null);
        }
Exemplo n.º 13
0
 protected override IAdvancedComparer <Pair <T1, T2> > CreateNew(ComparisonRules rules)
 {
     return(new PairComparer <T1, T2>(Provider, ComparisonRules.Combine(rules)));
 }
Exemplo n.º 14
0
 /// <inheritdoc/>
 public AdvancedComparer <T> ApplyRules(ComparisonRules rules)
 {
     return(cachedComparers.GetValue(rules,
                                     (_rules, _this) => new AdvancedComparer <T>(_this.CreateNew(_rules)),
                                     this));
 }
Exemplo n.º 15
0
 /// <summary>
 /// Creates new comparer of the same type, but using different comparison rules.
 /// </summary>
 /// <param name="rules">Comparison rules for the new comparer (relatively to this one).</param>
 /// <returns>New comparer of the same type, but using different comparison rules.</returns>
 protected abstract IAdvancedComparer <T> CreateNew(ComparisonRules rules);
Exemplo n.º 16
0
        // Constructors

        public EnumerableInterfaceComparer(IComparerProvider provider, ComparisonRules comparisonRules)
            : base(provider, comparisonRules)
        {
            ValueRangeInfo = new ValueRangeInfo <TEnumerable>(true, null, false, null, false, null);
        }
Exemplo n.º 17
0
 protected override IAdvancedComparer <TEnumerable> CreateNew(ComparisonRules rules)
 => new EnumerableInterfaceComparer <TEnumerable, T>(Provider, ComparisonRules.Combine(rules));
Exemplo n.º 18
0
 protected override IAdvancedComparer <bool> CreateNew(ComparisonRules rules)
 {
     return(new BooleanComparer(Provider, ComparisonRules.Combine(rules)));
 }
Exemplo n.º 19
0
 protected override IAdvancedComparer <T[]> CreateNew(ComparisonRules rules)
 {
     return(new ArrayComparer <T>(Provider, ComparisonRules.Combine(rules)));
 }
Exemplo n.º 20
0
        // Constructors

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

        internal SystemComparer(IComparerProvider provider, ComparisonRules comparisonRules)
            : base(provider, comparisonRules)
        {
        }
Exemplo n.º 22
0
 protected override IAdvancedComparer <float> CreateNew(ComparisonRules rules)
 => new SingleComparer(Provider, ComparisonRules.Combine(rules));
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
        // 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.º 25
0
        // Constructors

        public TupleDescriptorComparer(IComparerProvider provider, ComparisonRules comparisonRules)
            : base(provider, comparisonRules)
        {
        }
Exemplo n.º 26
0
 protected override IAdvancedComparer <T?> CreateNew(ComparisonRules rules)
 {
     return(new NullableComparer <T>(Provider, ComparisonRules.Combine(rules)));
 }
Exemplo n.º 27
0
        // Constructors

        public BaseComparerWrapper(IComparerProvider provider, ComparisonRules comparisonRules)
            : base(provider, comparisonRules)
        {
        }
Exemplo n.º 28
0
 /// <inheritdoc/>
 protected override IAdvancedComparer <TTarget> CreateNew(ComparisonRules rules)
 {
     return(new CastingComparer <TSource, TTarget>(sourceComparer.ApplyRules(rules)));
 }
Exemplo n.º 29
0
        // Constructors

        public TypeComparer(IComparerProvider provider, ComparisonRules comparisonRules)
            : base(provider, comparisonRules)
        {
            Initialize();
        }
Exemplo n.º 30
0
        // Constructors

        public SByteComparer(IComparerProvider provider, ComparisonRules comparisonRules)
            : base(provider, comparisonRules)
        {
            ValueRangeInfo = new ValueRangeInfo <sbyte>(true, SByte.MinValue, true, SByte.MaxValue, true, 1);
        }