Permalink
Browse files

Implementation of union and intersection with spec

  • Loading branch information...
1 parent 6ab061e commit 3b8012546d88ab2896eb8e30edb175e4e8762d03 @apohllo apohllo committed Mar 6, 2012
Showing with 84 additions and 4 deletions.
  1. +51 −3 ext/cbloomfilter/cbloomfilter.c
  2. +2 −0 lib/bloomfilter/native.rb
  3. +31 −1 spec/native_spec.rb
@@ -209,15 +209,63 @@ static VALUE bf_insert(VALUE self, VALUE key) {
static VALUE bf_merge(VALUE self, VALUE other) {
struct BloomFilter *bf, *target;
+ int i;
Data_Get_Struct(self, struct BloomFilter, bf);
Data_Get_Struct(other, struct BloomFilter, target);
- int i;
for (i = 0; i < bf->bytes; i++) {
bf->ptr[i] |= target->ptr[i];
}
return Qnil;
}
+static VALUE bf_and(VALUE self, VALUE other) {
+ struct BloomFilter *bf, *bf_other, *target;
+ VALUE klass, obj, args[5];
+ int i;
+
+ Data_Get_Struct(self, struct BloomFilter, bf);
+ Data_Get_Struct(other, struct BloomFilter, bf_other);
+ args[0] = INT2FIX(bf->m);
+ args[1] = INT2FIX(bf->k);
+ args[2] = INT2FIX(bf->s);
+ args[3] = INT2FIX(bf->b);
+ args[4] = INT2FIX(bf->r);
+ klass = rb_funcall(self,rb_intern("class"),0);
+ obj = bf_s_new(5,args,klass);
+ Data_Get_Struct(obj, struct BloomFilter, target);
+ // we assume that the parameters of both filters are the same
+ // TODO check if this is true
+ for (i = 0; i < bf->bytes; i++){
+ target->ptr[i] = bf->ptr[i] & bf_other->ptr[i];
+ }
+
+ return obj;
+}
+
+static VALUE bf_or(VALUE self, VALUE other) {
+ struct BloomFilter *bf, *bf_other, *target;
+ VALUE klass, obj, args[5];
+ int i;
+
+ Data_Get_Struct(self, struct BloomFilter, bf);
+ Data_Get_Struct(other, struct BloomFilter, bf_other);
+ args[0] = INT2FIX(bf->m);
+ args[1] = INT2FIX(bf->k);
+ args[2] = INT2FIX(bf->s);
+ args[3] = INT2FIX(bf->b);
+ args[4] = INT2FIX(bf->r);
+ klass = rb_funcall(self,rb_intern("class"),0);
+ obj = bf_s_new(5,args,klass);
+ Data_Get_Struct(obj, struct BloomFilter, target);
+ // we assume that the parameters of both filters are the same
+ // TODO check if this is true
+ for (i = 0; i < bf->bytes; i++){
+ target->ptr[i] = bf->ptr[i] | bf_other->ptr[i];
+ }
+
+ return obj;
+}
+
static VALUE bf_delete(VALUE self, VALUE key) {
int index, seed;
int i, len, m, k, s;
@@ -346,14 +394,14 @@ void Init_cbloomfilter(void) {
rb_define_method(cBloomFilter, "include?", bf_include, -1);
rb_define_method(cBloomFilter, "clear", bf_clear, 0);
rb_define_method(cBloomFilter, "merge!", bf_merge, 1);
+ rb_define_method(cBloomFilter, "&", bf_and, 1);
+ rb_define_method(cBloomFilter, "|", bf_or, 1);
rb_define_method(cBloomFilter, "to_s", bf_to_s, 0);
rb_define_method(cBloomFilter, "bitmap", bf_bitmap, 0);
rb_define_method(cBloomFilter, "load", bf_load, 1);
/* functions that have not been implemented, yet */
- // rb_define_method(cBloomFilter, "&", bf_and, 1);
- // rb_define_method(cBloomFilter, "|", bf_or, 1);
// rb_define_method(cBloomFilter, "<=>", bf_cmp, 1);
}
@@ -35,6 +35,8 @@ def delete(key); @bf.delete(key); end
def clear; @bf.clear; end
def size; @bf.num_set; end
def merge!(o); @bf.merge!(o.bf); end
+ def &(o); @bf.&(o.bf); end
+ def |(o); @bf.|(o.bf); end
def bitmap
@bf.bitmap
View
@@ -42,6 +42,36 @@
bf.include?("abcd").should be_false
bf.include?("test", "test1", '12345').should be_true
end
+
+ it "should return intersection with other filter" do
+ bf1 = Native.new
+ bf1.insert("test")
+ bf1.insert("test1")
+
+ bf2 = Native.new
+ bf2.insert("test")
+ bf2.insert("test2")
+
+ bf3 = bf1 & bf2
+ bf3.include?("test").should be_true
+ bf3.include?("test1").should be_false
+ bf3.include?("test2").should be_false
+ end
+
+ it "should return union with other filter" do
+ bf1 = Native.new
+ bf1.insert("test")
+ bf1.insert("test1")
+
+ bf2 = Native.new
+ bf2.insert("test")
+ bf2.insert("test2")
+
+ bf3 = bf1 | bf2
+ bf3.include?("test").should be_true
+ bf3.include?("test1").should be_true
+ bf3.include?("test2").should be_true
+ end
end
context "behave like counting bloom filter" do
@@ -76,4 +106,4 @@
bf.include?('baz').should be_false
end
end
-end
+end

0 comments on commit 3b80125

Please sign in to comment.