Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Sep 11th, 2012  |  syntax: None  |  size: 1.26 KB  |  hits: 8  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. //==============================================================================
  2. package bloom;
  3. //==============================================================================
  4. public class Bloom
  5. {
  6.    
  7.     private long[] bits;
  8.    
  9.     public Bloom ()
  10.     {
  11.         bits = new long[67108864];
  12.     }
  13.  
  14.     public static void main (String[] args)
  15.     {
  16.         Bloom bloom = new Bloom ();
  17.         bloom.add ("vienas");
  18.         bloom.add ("du");
  19.         System.out.println (bloom.has ("vienas"));
  20.         System.out.println (bloom.has ("du"));
  21.         System.out.println (bloom.has ("trys"));
  22.     }
  23.  
  24.     private boolean has (String object)
  25.     {
  26.         int index = object.hashCode ();
  27.         long positiveIndex = index + 2147483648L;
  28.         long element = bits[(int) (positiveIndex / 64)];
  29.         int bit = (int) (positiveIndex % 64);
  30.         return 1 == ((element >>> bit) & 1);
  31.     }
  32.  
  33.     private void add (String object)
  34.     {
  35.         int index = object.hashCode ();
  36.         long positiveIndex = index + 2147483648L;
  37.         int elementIndex = (int) (positiveIndex / 64);
  38.         int bit = (int) (positiveIndex % 64);
  39.         int valueToAdd = 1 << bit;
  40.         bits[elementIndex] |= valueToAdd;
  41.     }
  42.  
  43. }
  44. //==============================================================================