- public class AdditiveFunction implements HashFunction {
- /* (non-Javadoc)
- * @see spellChecker.util.hash.HashFunction#getHashCode(java.lang.Object)
- */
- @Override
- public int getHashCode(Object obj) {
- // TODO Auto-generated method stub
- if(!(obj instanceof char[])) {
- return 0;
- }
- char[] key = (char[])obj;
- int hash = 0;
- for(int i = 0; i < key.length; i++) {
- hash += i * key[i];
- }
- return hash;
- }
- }
- public class APFunction implements HashFunction {
- /* (non-Javadoc)
- * @see spellChecker.util.hash.HashFunction#getHashCode(java.lang.Object)
- */
- @Override
- public int getHashCode(Object obj) {
- // TODO Auto-generated method stub
- if(!(obj instanceof char[])) {
- return 0;
- }
- char[] key = (char[])obj;
- int hash = 0xAAAAAAAA;
- for(int i = 0; i < key.length; i++) {
- if((i & 1) == 0) {
- hash ^= ((hash << 7) ^ key[i] ^ (hash >> 3));
- } else {
- hash ^= (~((hash << 11) ^ key[i] ^ (hash >> 5)));
- }
- }
- return hash;
- }
- }
- public class BKDRFunction implements HashFunction {
- /* (non-Javadoc)
- * @see spellChecker.util.hash.HashFunction#getHashCode(java.lang.Object)
- */
- @Override
- public int getHashCode(Object obj) {
- // TODO Auto-generated method stub
- if(!(obj instanceof char[])) {
- return 0;
- }
- char[] key = (char[])obj;
- int seed = 131;//31 131 1313 13131 131313 etc..
- int hash = 0;
- for(int i = 0; i < key.length; i++) {
- hash = hash * seed + key[i];
- }
- return hash;
- }
- }
- public class DEKFunction implements HashFunction {
- /* (non-Javadoc)
- * @see spellChecker.util.hash.HashFunction#getHashCode(java.lang.Object)
- */
- @Override
- public int getHashCode(Object obj) {
- // TODO Auto-generated method stub
- if(!(obj instanceof char[])) {
- return 0;
- }
- char[] key = (char[])obj;
- int hash = key.length;
- for(int i = 0; i < key.length; i++) {
- hash = ((hash << 5) ^ (hash >> 27)) ^ key[i];
- }
- return hash;
- }
- }
- public class DJBFunction implements HashFunction {
- /* (non-Javadoc)
- * @see spellChecker.util.hash.HashFunction#getHashCode(java.lang.Object)
- */
- @Override
- public int getHashCode(Object obj) {
- // TODO Auto-generated method stub
- if(!(obj instanceof char[])) {
- return 0;
- }
- char[] key = (char[])obj;
- int hash = 5381;
- for(int i = 0; i < key.length; i++) {
- hash += (hash << 5) + key[i];
- }
- return hash;
- }
- }
- public class ELFFunction implements HashFunction {
- /* (non-Javadoc)
- * @see spellChecker.util.hash.HashFunction#getHashCode(java.lang.Object)
- */
- @Override
- public int getHashCode(Object obj) {
- // TODO Auto-generated method stub
- if(!(obj instanceof char[])) {
- return 0;
- }
- char[] key = (char[])obj;
- int hash = 0;
- long x = 0;
- for(int i = 0; i < key.length; i++) {
- hash = (hash << 4) + key[i];
- if((x = hash & 0xF0000000L) != 0) {
- hash ^= (x >> 24);
- hash &= ~x;
- }
- }
- return hash;
- }
- }
- public class JSFunction implements HashFunction {
- /* (non-Javadoc)
- * @see spellChecker.util.hash.HashFunction#getHashCode(java.lang.Object)
- */
- @Override
- public int getHashCode(Object obj) {
- // TODO Auto-generated method stub
- if(!(obj instanceof char[])) {
- return 0;
- }
- char[] key = (char[])obj;
- int hash = 1315423911;
- for(int i = 0; i < key.length; i++) {
- hash ^= ((hash << 5) + key[i] + (hash >> 2));
- }
- return hash;
- }
- }
- public class PJWFunction implements HashFunction {
- /*
- * (non-Javadoc)
- *
- * @see spellChecker.util.hash.HashFunction#getHashCode(java.lang.Object)
- */
- @Override
- public int getHashCode(Object obj) {
- // TODO Auto-generated method stub
- if(!(obj instanceof char[])) {
- return 0;
- }
- char[] key = (char[])obj;
- int BitsInUnignedInt = 32;
- int ThreeQuarters = (int)((BitsInUnignedInt * 3) / 4);
- int OneEighth = (int)(BitsInUnignedInt / 8);
- int HighBits = (int)(0xFFFFFFFF) << (BitsInUnignedInt - OneEighth);
- int hash = 0;
- int test = 0;
- for(int i = 0; i < key.length; i++) {
- hash = (hash << OneEighth) + key[i];
- if ((test = hash & HighBits) != 0)
- {
- hash = ((hash ^ (test >> ThreeQuarters)) & (~HighBits));
- }
- }
- return hash;
- }
- }
- public class RSFunction implements HashFunction {
- /* (non-Javadoc)
- * @see spellChecker.util.hash.HashFunction#getHashCode(java.lang.Object)
- */
- @Override
- public int getHashCode(Object obj) {
- // TODO Auto-generated method stub
- int b = 378551;
- int a = 63689;
- int hash = 0;
- if(!(obj instanceof char[])) {
- return 0;
- }
- char[] key = (char[])obj;
- for(int i = 0; i < key.length; i++) {
- hash = hash * a + key[i];
- a = a * b;
- }
- return hash;
- }
- }
- public class SDBMFunction implements HashFunction {
- /* (non-Javadoc)
- * @see spellChecker.util.hash.HashFunction#getHashCode(java.lang.Object)
- */
- @Override
- public int getHashCode(Object obj) {
- // TODO Auto-generated method stub
- if(!(obj instanceof char[])) {
- return 0;
- }
- char[] key = (char[])obj;
- int hash = 0;
- for(int i = 0; i < key.length; i++) {
- hash = key[i] + (hash << 6) + (hash << 16) - hash;
- }
- return hash;
- }
- }
Hash Functions
最新推荐文章于 2022-05-04 23:46:55 发布