Files
spark/src/client/spark_api/utils/EqualityComparer.cs
T

168 lines
7.9 KiB
C#

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
namespace bts.utils {
public static class EqualityComparer {
const int _hash_seed = 63607, _hash_multiplier = 6451, _max_auto_phase_buckets = 16;
public static IEqualityComparer<T> Create<T>(Func<T, T, bool> equals) =>
new AnonymousEqualityComparer<T>(equals);
public static IEqualityComparer<T> Create<T>(Func<T, T, bool> equals, Func<T, int> get_hash_code) =>
new AnonymousEqualityComparer<T>(equals, get_hash_code);
public static IEqualityComparer<T> Create<T, TKey>(Func<T, TKey> key_selector) =>
Create(key_selector, System.Collections.Generic.EqualityComparer<TKey>.Default);
public static IEqualityComparer<T> Create<T, TKey>(Func<T, TKey> key_selector, IEqualityComparer<TKey> key_comparer) =>
Create<T>((x, y) => key_comparer.Equals(key_selector(x), (key_selector(y))), x => key_selector(x).GetHashCode());
public static IEqualityComparer<T?> CreateNullable<T, TKey>(Func<T, TKey> key_selector) where T : struct {
var eqc = System.Collections.Generic.EqualityComparer<TKey>.Default;
return Create<T?>((x, y) => x.HasValue ? y.HasValue && eqc.Equals(key_selector(x.Value), key_selector(y.Value)) : !y.HasValue);
}
public static IEqualityComparer<T?> CreateNullable<T>(Func<T, T, bool> equals) where T : struct =>
new AnonymousEqualityComparer<T?>(nullable_equals);
public static IEqualityComparer<T> null_check_wrapper<T>(IEqualityComparer<T> comparer) where T : class =>
new AnonymousEqualityComparer<T>((a, b) => NullCheckEquals(a, b, comparer.Equals), comparer.GetHashCode);
public static IEqualityComparer<T> to_safe<T>(this IEqualityComparer<T> comparer) where T : class =>
new SafeComparerWrapper<T>(comparer);
public static bool safe_sequence_equals<T>(IEnumerable<T> source, IEnumerable<T> other) =>
NullCheckEquals(source, other, (a, b) => a.SequenceEqual(b));
public static IEqualityComparer to_non_generic<T>(this IEqualityComparer<T> comparer) =>
new AnonymousEqualityComparer<T>(comparer.Equals, comparer.GetHashCode);
public static bool nullable_equals<T>(T? a, T? b) where T : struct =>
a.HasValue ? b.HasValue && a.Value.Equals(b.Value) : !b.HasValue;
public static bool nullable_equals<T>(T? a, T? b, Func<T, T, bool> equals) where T : struct =>
a.HasValue ? b.HasValue && equals(a.Value, b.Value) : !b.HasValue;
public static bool null_check_equals<T>(T a, T b) =>
a == null ? b == null : b != null && a.Equals(b);
public static bool NullCheckEquals<T>(T x, T y, Func<T, T, bool> non_null_equals) where T : class =>
x == null ? y == null :
y != null &&
non_null_equals(x, y);
public static bool equals_obj<T>(T value, object obj) where T : IEquatable<T> =>
obj is T other && value.Equals(other);
public static int safe_get_hash_code<T>(T obj) where T : class =>
obj == null ? 0 : obj.GetHashCode();
public static int aggregate_hash(int item1_hash, int item2_hash, int item3_hash) {
int rv = _hash_seed;
unchecked {
_accumulate_hash(ref rv, ref item1_hash);
_accumulate_hash(ref rv, ref item2_hash);
_accumulate_hash(ref rv, ref item3_hash);
}
return rv;
}
public static int aggregate_hash(params object[] items) =>
aggregate_hash(items.AsEnumerable());
public static int aggregate_hash<T>(params T[] items) =>
aggregate_hash(items.AsEnumerable());
public static int auto_phase(params int[] hash_codes) =>
auto_phase(Array.AsReadOnly(hash_codes));
public static int auto_phase<T>(IEnumerable<T> items) =>
auto_phase(items.Take(_max_auto_phase_buckets).ToList());
public static int auto_phase<T>(IReadOnlyList<T> hash_codes, int bit_span = 32) {
Log.assert(bit_span > 0 && bit_span <= 32);
// don't divide by zero
if (hash_codes.Count == 0)
return 0;
int size = Math.Min(hash_codes.Count, _max_auto_phase_buckets);
int bits_per_hash = bit_span / size;
int phase = 1 << (bits_per_hash - 1);
int rv = hash_codes[0].GetHashCode() % phase;
/// The idea is to split the 32 to bits into evenly sized buckets for each hash code item.
/// For example:
/// If there are 4 items in <paramref name="hash_codes"/>,
/// each item hash will occupy 8 bits in the cumulative hash (i.e., rv).
/// The cumulative hash is shifted by 8 bits with each iteration,
/// so the next item hash can be inserted in the front 8 bits.
for (int i = 1; i < size; i++) {
// nudge the bits left to make room for the next hash code
rv = rv << bits_per_hash;
// put this hash code in the front bits that were just zeroed out.
rv |= hash_codes[i].GetHashCode() % phase;
}
return rv;
}
public static int phase_hash(int raw_item_hash, int phase_mult, int mod = MathUtils.Primes.P_1087) =>
unchecked(raw_item_hash % mod + (mod * phase_mult));
public static int safe_aggregate_hash<T>(IEnumerable<T> items) =>
items == null ? 0 : aggregate_hash(items);
public static int aggregate_hash<T>(IEnumerable<T> items) {
int hash = _hash_seed;
unchecked {
foreach (var v in items)
hash = hash * _hash_multiplier + (v == null ? 0 : v.GetHashCode());
}
return hash;
}
// Note: be wary of the performance cost this method may have, as frequently creating large arrays can be expensive.
// If you wish to avoid this cost, consider making an overload like the method above, with the appropriate number of item-hash arguments
public static int aggregate_hash(params int[] item_hashes) {
int hash = _hash_seed;
unchecked {
foreach (int item_hash in item_hashes)
hash = hash * _hash_multiplier + item_hash;
}
return hash;
}
public static int aggregate_hash_limited<T>(IReadOnlyList<T> items, int max_count = 16) =>
items.Count < max_count ? aggregate_hash(items) : items.Count;
// WARNING: be sure to call this within in an unchecked scope
static void _accumulate_hash(ref int aggregate_hash, ref int item_hash) =>
aggregate_hash = aggregate_hash * _hash_multiplier + item_hash;
private class AnonymousEqualityComparer<T> : IEqualityComparer<T>, IEqualityComparer {
readonly Func<T, T, bool> _equals;
readonly Func<T, int> _get_hash_code;
public AnonymousEqualityComparer(Func<T, T, bool> equals) : this(equals, t => t.GetHashCode()) { }
public AnonymousEqualityComparer(Func<T, T, bool> equals, Func<T, int> get_hash_code) {
_equals = equals;
_get_hash_code = get_hash_code;
}
public bool Equals(T x, T y) => _equals(x, y);
public int GetHashCode(T obj) => _get_hash_code(obj);
bool IEqualityComparer.Equals(object x, object y) => x is T xt && y is T yt && _equals(xt, yt);
int IEqualityComparer.GetHashCode(object obj) => throw new NotImplementedException();
}
struct SafeComparerWrapper<T> : IEqualityComparer<T> where T : class {
readonly IEqualityComparer<T> _comparer;
public SafeComparerWrapper(IEqualityComparer<T> comparer) =>
_comparer = comparer;
public bool Equals(T x, T y) => x == null ? y == null : y != null && _comparer.Equals(x, y);
public int GetHashCode(T obj) => obj == null ? 0 : _comparer.GetHashCode(obj);
}
}
}