上个学期上Distributed Software Development的时候,最后一个项目实现Distributed Hashtable,需要对文件名字符串进行Hash处理,用到了一个经典的ELFHash Function,特此摘录:
public int ELFHash(String str, int number){
int hash = 0;
long x =0l;
char[] array = str.toCharArray();
for(int i=0; i<array.length; i++){
hash = (hash << 4) + array[i];
if((x = (hash & 0xF0000000L)) != 0){
hash ^= (x >> 24);
hash &= ~x;
}
}
int result = (hash & 0x7FFFFFFF) % number;
return result;
}