Hash Functions

 
  1. public class AdditiveFunction implements HashFunction {
  2.     /* (non-Javadoc)
  3.      * @see spellChecker.util.hash.HashFunction#getHashCode(java.lang.Object)
  4.      */
  5.     @Override
  6.     public int getHashCode(Object obj) {
  7.         // TODO Auto-generated method stub
  8.         if(!(obj instanceof char[])) {
  9.             return 0;
  10.         }
  11.         
  12.         char[] key = (char[])obj;
  13.         int hash = 0;
  14.         for(int i = 0; i < key.length; i++) {
  15.             hash += i * key[i];
  16.         }
  17.         return hash;
  18.     }
  19. }
  20. public class APFunction implements HashFunction {
  21.     /* (non-Javadoc)
  22.      * @see spellChecker.util.hash.HashFunction#getHashCode(java.lang.Object)
  23.      */
  24.     @Override
  25.     public int getHashCode(Object obj) {
  26.         // TODO Auto-generated method stub
  27.         if(!(obj instanceof char[])) {
  28.             return 0;
  29.         }
  30.         
  31.         char[] key = (char[])obj;
  32.         
  33.         int hash = 0xAAAAAAAA;
  34.      
  35.         for(int i = 0; i < key.length; i++) {
  36.             if((i & 1) == 0) {
  37.                 hash ^= ((hash << 7) ^ key[i] ^ (hash >> 3));
  38.             } else {
  39.                 hash ^= (~((hash << 11) ^ key[i] ^ (hash >> 5)));
  40.             }
  41.         }
  42.         return hash;
  43.     }
  44. }
  45. public class BKDRFunction implements HashFunction {
  46.     /* (non-Javadoc)
  47.      * @see spellChecker.util.hash.HashFunction#getHashCode(java.lang.Object)
  48.      */
  49.     @Override
  50.     public int getHashCode(Object obj) {
  51.         // TODO Auto-generated method stub
  52.         if(!(obj instanceof char[])) {
  53.             return 0;
  54.         }
  55.         
  56.         char[] key = (char[])obj;
  57.         int seed = 131;//31 131 1313 13131 131313 etc..
  58.         int hash = 0;
  59.         for(int i = 0; i < key.length; i++) {
  60.             hash = hash * seed + key[i];
  61.         }
  62.         return hash;
  63.     }
  64. }
  65. public class DEKFunction implements HashFunction {
  66.     /* (non-Javadoc)
  67.      * @see spellChecker.util.hash.HashFunction#getHashCode(java.lang.Object)
  68.      */
  69.     @Override
  70.     public int getHashCode(Object obj) {
  71.         // TODO Auto-generated method stub
  72.         if(!(obj instanceof char[])) {
  73.             return 0;
  74.         }
  75.         
  76.         char[] key = (char[])obj;
  77.         int hash = key.length;
  78.         for(int i = 0; i < key.length; i++) {
  79.             hash = ((hash << 5) ^ (hash >> 27)) ^ key[i];
  80.         }
  81.         return hash;
  82.     }
  83. }
  84. public class DJBFunction implements HashFunction {
  85.     /* (non-Javadoc)
  86.      * @see spellChecker.util.hash.HashFunction#getHashCode(java.lang.Object)
  87.      */
  88.     @Override
  89.     public int getHashCode(Object obj) {
  90.         // TODO Auto-generated method stub
  91.         if(!(obj instanceof char[])) {
  92.             return 0;
  93.         }
  94.         
  95.         char[] key = (char[])obj;
  96.         int hash = 5381;
  97.          
  98.         for(int i = 0; i < key.length; i++) {
  99.             hash += (hash << 5) + key[i];
  100.         }
  101.         return hash;
  102.     }
  103. }
  104. public class ELFFunction implements HashFunction {
  105.     /* (non-Javadoc)
  106.      * @see spellChecker.util.hash.HashFunction#getHashCode(java.lang.Object)
  107.      */
  108.     @Override
  109.     public int getHashCode(Object obj) {
  110.         // TODO Auto-generated method stub
  111.         if(!(obj instanceof char[])) {
  112.             return 0;
  113.         }
  114.         
  115.         char[] key = (char[])obj;
  116.         int hash = 0;
  117.         long x  = 0;
  118.      
  119.         for(int i = 0; i < key.length; i++) {
  120.             hash = (hash << 4) + key[i];
  121.             if((x = hash & 0xF0000000L) != 0) {
  122.                 hash ^= (x >> 24);
  123.                 hash &= ~x;
  124.             }
  125.         }
  126.         return hash;
  127.     }
  128. }
  129. public class JSFunction implements HashFunction {
  130.     /* (non-Javadoc)
  131.      * @see spellChecker.util.hash.HashFunction#getHashCode(java.lang.Object)
  132.      */
  133.     @Override
  134.     public int getHashCode(Object obj) {
  135.         // TODO Auto-generated method stub
  136.         if(!(obj instanceof char[])) {
  137.             return 0;
  138.         }
  139.         
  140.         char[] key = (char[])obj;
  141.         int hash = 1315423911;
  142.         for(int i = 0; i < key.length; i++) {
  143.             hash ^= ((hash << 5) + key[i] + (hash >> 2));
  144.         }
  145.         return hash;
  146.     }
  147. }
  148. public class PJWFunction implements HashFunction {
  149.     /*
  150.      * (non-Javadoc)
  151.      * 
  152.      * @see spellChecker.util.hash.HashFunction#getHashCode(java.lang.Object)
  153.      */
  154.     @Override
  155.     public int getHashCode(Object obj) {
  156.         // TODO Auto-generated method stub
  157.         if(!(obj instanceof char[])) {
  158.             return 0;
  159.         }
  160.         char[] key = (char[])obj;
  161.         int BitsInUnignedInt = 32;
  162.         int ThreeQuarters = (int)((BitsInUnignedInt  * 3) / 4);
  163.         int OneEighth = (int)(BitsInUnignedInt / 8);
  164.         int HighBits = (int)(0xFFFFFFFF) << (BitsInUnignedInt - OneEighth);
  165.         int hash = 0;
  166.         int test = 0;
  167.         for(int i = 0; i < key.length; i++) {
  168.             hash = (hash << OneEighth) + key[i];
  169.             if ((test = hash & HighBits) != 0)
  170.             {
  171.                 hash = ((hash ^ (test >> ThreeQuarters)) & (~HighBits));
  172.             }
  173.         }
  174.         return hash;
  175.     }
  176. }
  177. public class RSFunction implements HashFunction {
  178.     /* (non-Javadoc)
  179.      * @see spellChecker.util.hash.HashFunction#getHashCode(java.lang.Object)
  180.      */
  181.     @Override
  182.     public int getHashCode(Object obj) {
  183.         // TODO Auto-generated method stub
  184.         int b = 378551;
  185.         int a = 63689;
  186.         int hash = 0;
  187.         
  188.         if(!(obj instanceof char[])) {
  189.             return 0;
  190.         }
  191.         
  192.         char[] key = (char[])obj;
  193.         for(int i = 0; i < key.length; i++) {
  194.              hash = hash * a + key[i];
  195.              a  = a * b;
  196.         }
  197.         return hash;
  198.     }
  199. }
  200. public class SDBMFunction implements HashFunction {
  201.     /* (non-Javadoc)
  202.      * @see spellChecker.util.hash.HashFunction#getHashCode(java.lang.Object)
  203.      */
  204.     @Override
  205.     public int getHashCode(Object obj) {
  206.         // TODO Auto-generated method stub
  207.         if(!(obj instanceof char[])) {
  208.             return 0;
  209.         }
  210.         
  211.         char[] key = (char[])obj;
  212.         int hash = 0;
  213.         for(int i = 0; i < key.length; i++) {
  214.             hash = key[i] + (hash << 6) + (hash << 16) - hash;
  215.         }
  216.         return hash;
  217.     }
  218. }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值