Re[3]: Компаратор
От: _FRED_ Россия
Дата: 05.07.11 05:50
Оценка:
Здравствуйте, Nikolay_P_I, Вы писали:

N_P>А можно поделиться окончательным результатом для использования сторонними разработчиками не разжигая аппетит ? Пожалуйста


А там уже ничего интересного и не осталось. Create/Convert для IEqualityComparer<>:
  Скрытый текст
  public static partial class Comparers
  {
    public static EqualityComparer<T> Create<T>(Func<T, T, bool> comparison, Func<T, int> hashCode) {
      Argument.NotNull(comparison, "comparison");
      Argument.NotNull(hashCode, "hashCode");
      return new ComparisonEqualityComparer<T>(comparison, hashCode);
    }

    public static EqualityComparer<T> Create<T>(Func<T, T, bool> equals) {
      Argument.NotNull(equals, "equals");
      return Create(equals, EqualityComparer<T>.Default.GetHashCode);
    }

    public static EqualityComparer<TOutput> Convert<TInput, TOutput>(this IEqualityComparer<TInput> comparer, Converter<TOutput, TInput> converter) {
      Argument.NotNull(comparer, "comparer");
      Argument.NotNull(converter, "converter");
      Func<TOutput, TOutput, bool> comparison = (x, y) => comparer.Equals(converter(x), converter(y));
      Func<TOutput, int> hashCode = x => comparer.GetHashCode(converter(x));
      return Create(comparison, hashCode);
    }

    [Serializable]
    private sealed class ComparisonEqualityComparer<T> : EqualityComparer<T>
    {
      public ComparisonEqualityComparer(Func<T, T, bool> comparison, Func<T, int> hashCode) {
        Argument.NotNull(comparison, "comparison");
        Argument.NotNull(hashCode, "hashCode");

        Comparison = comparison;
        HashCode = hashCode;
      }

      private Func<T, T, bool> Comparison{get;set;}
      private Func<T, int> HashCode {get;set;}

      [ContractInvariantMethod]
      private void Invariant() {
        Contract.Invariant(Comparison != null);
        Contract.Invariant(HashCode != null);
      }

      public override bool Equals(T x, T y) {
        return Comparison(x, y);
      }

      public override int GetHashCode(T obj) {
        return HashCode(obj);
      }
    }
  }


И преобразование интерфейсов компараторов (IEqualityComparer<T>, IEqualityComparer) в универсальный (EqualityComparer<T>)
  Скрытый текст
  public static partial class Comparers
  {
    public static EqualityComparer<T> ToTyped<T>(this IEqualityComparer<T> comparer) {
      Argument.NotNull(comparer, "comparer");
      return comparer as EqualityComparer<T> ?? new TypedEqualityComparer<T>(comparer);
    }

    public static EqualityComparer<T> ToTyped<T>(this IEqualityComparer comparer) {
      Argument.NotNull(comparer, "comparer");
      return comparer as EqualityComparer<T> ?? new UntypedEqualityComparer<T>(comparer);
    }

    [Serializable]
    private sealed class TypedEqualityComparer<T> : EqualityComparer<T>
    {
      public TypedEqualityComparer(IEqualityComparer<T> comparer) {
        Argument.NotNull(comparer, "comparer");
        Comparer = comparer;
      }

      private IEqualityComparer<T> Comparer { get; set; }

      [ContractInvariantMethod]
      private void Invariant() {
        Contract.Invariant(Comparer != null);
      }

      public override bool Equals(T x, T y) {
        return Comparer.Equals(x, y);
      }

      public override int GetHashCode(T obj) {
        return Comparer.GetHashCode(obj);
      }
    }

    [Serializable]
    private sealed class UntypedEqualityComparer<T> : EqualityComparer<T>
    {
      public UntypedEqualityComparer(IEqualityComparer comparer) {
        Argument.NotNull(comparer, "comparer");
        Comparer = comparer;
      }

      private IEqualityComparer Comparer { get; set; }

      [ContractInvariantMethod]
      private void Invariant() {
        Contract.Invariant(Comparer != null);
      }

      public override bool Equals(T x, T y) {
        return Comparer.Equals(x, y);
      }

      public override int GetHashCode(T obj) {
        Argument.NotNull(obj, "obj");
        return Comparer.GetHashCode(obj);
      }
    }
  }


  public static partial class Comparers
  {
    public static Comparer<T> ToTyped<T>(this IComparer<T> comparer) {
      Argument.NotNull(comparer, "comparer");
      return comparer as Comparer<T> ?? new TypedComparer<T>(comparer);
    }

    public static Comparer<T> ToTyped<T>(this IComparer comparer) {
      Argument.NotNull(comparer, "comparer");
      return comparer as Comparer<T> ?? new UntypedComparer<T>(comparer);
    }

    [Serializable]
    private sealed class TypedComparer<T> : Comparer<T>
    {
      public TypedComparer(IComparer<T> comparer) {
        Argument.NotNull(comparer, "comparer");
        Comparer = comparer;
      }

      private IComparer<T> Comparer { get; set; }

      [ContractInvariantMethod]
      private void Invariant() {
        Contract.Invariant(Comparer != null);
      }

      public override int Compare(T x, T y) {
        return Comparer.Compare(x, y);
      }
    }

    [Serializable]
    private sealed class UntypedComparer<T> : Comparer<T>
    {
      public UntypedComparer(IComparer comparer) {
        Argument.NotNull(comparer, "comparer");
        Comparer = comparer;
      }

      private IComparer Comparer { get; set; }

      [ContractInvariantMethod]
      private void Invariant() {
        Contract.Invariant(Comparer != null);
      }

      public override int Compare(T x, T y) {
        return Comparer.Compare(x, y);
      }
    }
  }
Help will always be given at Hogwarts to those who ask for it.
 
Подождите ...
Wait...
Пока на собственное сообщение не было ответов, его можно удалить.