Advertisement
Guest User

hash functions

a guest
Jan 21st, 2020
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.38 KB | None | 0 0
  1. class ETHHash extends HashTable {
  2.   public ETHHash(int size) {
  3.     super(size);
  4.   }
  5.  
  6.   @Override
  7.   public int hash(String item) {
  8.     if (item != null) {
  9.       int bn = 1;
  10.       for (int i = 0; i < item.length(); i++) {
  11.         bn = Integer.valueOf(item.charAt(i))*((bn % 257) + 1);
  12.       }
  13.       return bn % getCapacity();
  14.     }
  15.     return 0;
  16.   }
  17. }
  18.  
  19. class GNUCPPHash extends HashTable {
  20.   public GNUCPPHash(int size) {
  21.     super(size);
  22.   }
  23.  
  24.   @Override
  25.   public int hash(String item) {
  26.     if (item != null) {
  27.       int bn = 0;
  28.       for (int i = 0; i < item.length(); i++) {
  29.         bn = 4*bn + Integer.valueOf(item.charAt(i));
  30.       }
  31.       return (((1 << 31) - 1) & bn) % getCapacity();
  32.     }
  33.     return 0;
  34.   }
  35. }
  36.  
  37. class GNUCC1Hash extends HashTable {
  38.   public GNUCC1Hash(int size) {
  39.     super(size);
  40.   }
  41.  
  42.   @Override
  43.   public int hash(String item) {
  44.     if (item != null) {
  45.       int bn = item.length();
  46.       for (int i = 0; i < item.length(); i++) {
  47.         bn = 613*bn + Integer.valueOf(item.charAt(i));
  48.       }
  49.       return (((1 << 30) - 1) & bn) % getCapacity();
  50.     }
  51.     return 0;
  52.   }
  53. }
  54.  
  55. class HashCodeHash extends HashTable {
  56.   public HashCodeHash(int size) {
  57.     super(size);
  58.   }
  59.  
  60.   @Override
  61.   public int hash(String item) {
  62.     if (item != null) {
  63.       return Math.abs(item.hashCode()) % getCapacity();
  64.     }
  65.     return 0;
  66.   }
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement