[ Log On ]
  • Home
  • Tst
  • Cha
  • Enc
  • Code
  • IP
  • Fun
  • Sub
  • DigF
  • Cis
  • Com
  • Db
  • About
  • Netsim

Bloom Filter Example

[Back] A Bloom filter is used to create a probabilistic guess on whether an item is in a data structure, and was created by Burton Howard Bloom. Within the test, the query will define if the value is "possibly in the set" or "definitely not in the set". Each element that is added is hashed with two or more hashing methods, and the values generated are used to set the bits in a bit array. In this example we use a 32-bit bit vector, and use Murmur 2 and FNV for the hashes. [Theory]

Add word:
Check word:

The results are then:

Bloom

Method

In this demo, the first value is taken from Murmur 2, and the second one is from FNV. Each of these are used to generate a 32-bit bit vector.

Examples

                01234567890123456789012345678901
Add fred:       00000000000000100000010000000000  fred [21,14]
Add bert:       00000000100000100000010000000100  bert [29,8]
Add greg:       00000000100100100000011000000100  greg [11,22]

We now have bit position 8, 11, 14, 21, 22 and 29 set.

Now we can test for amy:
    amy is not there [16,12]
New we can test for greg:
    greg may be in there [11,22]
   

Presentation

Code

 using Hashlib;

   class Bloom
    {
        public BitArray bits = new BitArray(32);
 
        public Int16 hash1(String word)
        {
            var hash2 = HashFactory.Hash64.CreateMurmur2();
            var val =(uint) hash2.ComputeString(word, Encoding.ASCII).GetULong();

            uint res = val % 32;

            return Convert.ToInt16(res);
        }
 
        public Int16 hash2(String word)
        {

            var hash3 = HashFactory.Hash64.CreateFNV();
            var val = (uint)hash3.ComputeString(word, Encoding.ASCII).GetULong();

            uint res = val % 32;

            return Convert.ToInt16(res);
        }
        public void add(String InString)
        {

            Int16 Point1 = this.hash1(InString);
            Int16 Point2 = this.hash2(InString);
            this.bits[Point1] = true;
            this.bits[Point2] = true;
 
        }
        public bool contains(String InString)
        {
            Int16 Point1 = this.hash1(InString);
            Int16 Point2 = this.hash2(InString);
            if (this.bits[Point1] && this.bits[Point2]) return true;
            else return false;
            
        }
        public string checkFor(String inkey)
        {
            if (this.contains(inkey)) return(inkey + " may be in there");
            else return(inkey + " is not there"); 
        }
    }