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).
This commit is contained in:
John Colvin committed 2021-09-06 18:27:32 -04:00
1 parent 3f2f19f73c
commit 9dc3c32769
1 file changed
+5 -4
@@ -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<T>(IEnumerable<T> items) =>
auto_phase(items.Take(_max_auto_phase_buckets).ToList());
public static int auto_phase<T>(IReadOnlyList<T> hash_codes) {
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 = 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);
}
}
}
}