I need a hash function for a string (of bytes) that
Has a low collision ratio (even for short strings)
Can be computed quickly (
O(n)time is a must but I want it as fast as possible)Given
hash(string1)andhash(string2), computinghash(append(string1, string2))can be done inO(1).
The best I could come up with so far is this: (in Java pseudocode)
public static int[] HASH_ENTROPY = new int[] {...} // 255 large prime numbers public int hash() int hash = 0; for (int i=0; i < this.array.length; i++) hash += HASH_ENTROPY[this.array[i] + 128]; return hash; Are there any better algorithms? This one performs well with #1 and #3 but I'm wondering if it's too slow having to access the random elements in the array.