I am trying to implement a hash table in C to enrich my understandings of data structures.
There are a lot of hash functions out there for hash table implementations.
To compare the hash functions, there is a test called the avalanche effect test.
To test the set of hash functions I currently have, I wrote a small program in Java:
public static void testHashAvalanche() {
Set<Long> collisionSet = new HashSet<>();
// The input for the hash function with 128 bytes.
byte[] bytes = new byte[128];
long count = 0;
long previous = 0;
long totalAvalanche = 0;
// Generate the inputs for hashing with a slight change of bit each time
for (int i = 0; i < 128; i++) {
// Byte value from 0 -> 255
for (int j = 0; j < 256; j++) {
long current = hash(bytes); // Any hash function with 64 bit output
int avalanche = calculateAvalanche(previous, current);
totalAvalanche += avalanche;
bytes[i]++;
count++;
previous = current;
}
}
System.out.println("Average Avalanche: " + (double) totalAvalanche / (double) count);
}
public static int calculateAvalanche(long a, long b) {
long difference = a ^ b;
return Long.bitCount(difference);
}
I would like to know whether this is a correct approach, or there are other ways to test the hash functions.
Thanks!