annotate Implab/CustomEqualityComparer.cs @ 222:98eeb63cedb2

sync
author cin
date Tue, 22 Aug 2017 09:34:47 +0300
parents c0bf853aa04f
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
55
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
1 using Implab;
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
2 using System;
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
3 using System.Collections.Generic;
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
4 using System.Linq;
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
5 using System.Text;
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
6 using System.Threading.Tasks;
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
7
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
8 namespace Implab {
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
9 /// <summary>
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
10 /// Обертка для создания <c>IEqualityComparer</c> с использованием делегатов или лямда-выражений.
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
11 /// </summary>
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
12 /// <typeparam name="T">Тип сравниваемых значений</typeparam>
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
13 public class CustomEqualityComparer<T> : IEqualityComparer<T> {
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
14 Func<T, T, bool> m_equals;
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
15 Func<T, int> m_hash;
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
16
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
17 /// <summary>
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
18 /// Создает новый объект с указанными функциями сравнения на раветво и получения хеш-кода.
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
19 /// </summary>
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
20 /// <param name="equality">Функция проверки на равенство</param>
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
21 /// <param name="hash">Функция получения хешкода</param>
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
22 public CustomEqualityComparer(Func<T, T, bool> equality, Func<T, int> hash) {
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
23 Safe.ArgumentNotNull(equality, "equality");
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
24 Safe.ArgumentNotNull(hash, "hash");
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
25 m_hash = hash;
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
26 m_equals = equality;
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
27 }
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
28
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
29 /// <summary>
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
30 /// Сравнивает два знаечния на ревенство.
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
31 /// </summary>
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
32 /// <param name="x"></param>
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
33 /// <param name="y"></param>
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
34 /// <returns>Результат сравнения на равенство</returns>
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
35 public bool Equals(T x, T y) {
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
36 return m_equals(x,y);
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
37 }
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
38
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
39 /// <summary>
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
40 /// Получает хеш-код для указанного значения.
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
41 /// </summary>
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
42 /// <param name="obj"></param>
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
43 /// <remarks>Равные знаечния *должны* иметь одинаковый хеш-код.</remarks>
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
44 /// <returns>Хеш-код</returns>
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
45 public int GetHashCode(T obj) {
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
46 return m_hash(obj);
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
47 }
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
48 }
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
49 }