From 9dc3c32769245ef0849a8b5175aacbee04cb61e1 Mon Sep 17 00:00:00 2001 From: John Colvin Date: Sun, 29 Aug 2021 13:46:56 -0400 Subject: [PATCH] Add bit_span param to auto_phase (hash fn) Brought over from DJ_ICE bc it seems like a useful param for custom hash impls (though there isn't an immediate need for it). --- src/client/spark_api/utils/EqualityComparer.cs | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/client/spark_api/utils/EqualityComparer.cs b/src/client/spark_api/utils/EqualityComparer.cs index 2556dbd05..a30f11b9b 100644 --- a/src/client/spark_api/utils/EqualityComparer.cs +++ b/src/client/spark_api/utils/EqualityComparer.cs @@ -77,16 +77,17 @@ namespace bts.utils { public static int auto_phase(params int[] hash_codes) => auto_phase(Array.AsReadOnly(hash_codes)); - + public static int auto_phase(IEnumerable items) => auto_phase(items.Take(_max_auto_phase_buckets).ToList()); - public static int auto_phase(IReadOnlyList hash_codes) { + public static int auto_phase(IReadOnlyList 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 = 32 / size; + 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. @@ -164,4 +165,4 @@ namespace bts.utils { public int GetHashCode(T obj) => obj == null ? 0 : _comparer.GetHashCode(obj); } } -} \ No newline at end of file +}