Permalink
Browse files

Bloom filter: Use BitArray instead of byte[].

  • Loading branch information...
1 parent 5327c4c commit a808a9db8c6097f56d67b3f2e75117d16bc24454 @schani schani committed Mar 18, 2013
Showing with 6 additions and 5 deletions.
  1. +6 −5 sf-2013-03-18-bloom-filter/bloom.cs
@@ -1,5 +1,6 @@
using System;
using System.IO;
+using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
@@ -159,20 +160,20 @@ public UInt32 Hash (string input)
public class BloomFilter
{
IStringHash[] hashes;
- byte[] table;
+ BitArray table;
public BloomFilter (int size, IEnumerable<IStringHash> _hashes)
{
hashes = _hashes.ToArray ();
- table = new byte [size];
+ table = new BitArray (size);
}
public void Add (string str)
{
foreach (var hash in hashes)
{
var h = hash.Hash (str) % table.Length;
- table [h] = 1;
+ table.Set ((int)h, true);
}
}
@@ -181,7 +182,7 @@ public bool Lookup (string str)
foreach (var hash in hashes)
{
var h = hash.Hash (str) % table.Length;
- if (table [h] == 0)
+ if (!table.Get ((int)h))
return false;
}
return true;
@@ -192,7 +193,7 @@ public float Occupancy ()
int occupied = 0;
for (int i = 0; i < table.Length; ++i)
{
- if (table [i] != 0)
+ if (table.Get (i))
++occupied;
}
return (float)occupied / (float)table.Length;

0 comments on commit a808a9d

Please sign in to comment.