Skip to content

Commit

Permalink
remove num_set, replace with "set_bits"
Browse files Browse the repository at this point in the history
  • Loading branch information
igrigorik committed Mar 17, 2012
1 parent 74dddcf commit 808ee11
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 17 deletions.
15 changes: 2 additions & 13 deletions ext/cbloomfilter/cbloomfilter.c
Expand Up @@ -19,7 +19,6 @@ struct BloomFilter {
int k; /* # of hash functions */
int s; /* # seed of hash functions */
int r; /* # raise on bucket overflow? */
int num_set; /* # of set bits */
unsigned char *ptr; /* bits data */
int bytes; /* size of byte data */
};
Expand Down Expand Up @@ -127,7 +126,6 @@ static VALUE bf_s_new(int argc, VALUE *argv, VALUE self) {
bf->k = k;
bf->s = s;
bf->r = r;
bf->num_set = 0;

bf->bytes = ((m * b) + 15) / 8;
bf->ptr = ALLOC_N(unsigned char, bf->bytes);
Expand Down Expand Up @@ -170,13 +168,7 @@ static VALUE bf_r(VALUE self) {
return bf->r == 0 ? Qfalse : Qtrue;
}

static VALUE bf_num_set(VALUE self) {
struct BloomFilter *bf;
Data_Get_Struct(self, struct BloomFilter, bf);
return INT2FIX(bf->num_set);
}

static VALUE bf_num_bit(VALUE self){
static VALUE bf_set_bits(VALUE self){
struct BloomFilter *bf;
int i,j,count = 0;
Data_Get_Struct(self, struct BloomFilter, bf);
Expand Down Expand Up @@ -215,7 +207,6 @@ static VALUE bf_insert(VALUE self, VALUE key) {
bucket_set(bf, index);
}

bf->num_set += 1;
return Qnil;
}

Expand Down Expand Up @@ -257,7 +248,6 @@ static VALUE bf_delete(VALUE self, VALUE key) {
bucket_unset(bf, index);
}

bf->num_set += 1;
return Qnil;
}

Expand Down Expand Up @@ -352,8 +342,7 @@ void Init_cbloomfilter(void) {
rb_define_method(cBloomFilter, "k", bf_k, 0);
rb_define_method(cBloomFilter, "b", bf_b, 0);
rb_define_method(cBloomFilter, "r", bf_r, 0);
rb_define_method(cBloomFilter, "num_set", bf_num_set, 0);
rb_define_method(cBloomFilter, "num_bit", bf_num_bit, 0);
rb_define_method(cBloomFilter, "set_bits", bf_set_bits, 0);
rb_define_method(cBloomFilter, "insert", bf_insert, 1);
rb_define_method(cBloomFilter, "delete", bf_delete, 1);
rb_define_method(cBloomFilter, "include?", bf_include, -1);
Expand Down
4 changes: 2 additions & 2 deletions lib/bloomfilter/native.rb
Expand Up @@ -37,8 +37,8 @@ def size; @bf.num_set; end
def merge!(o); @bf.merge!(o.bf); end

# Returns the number of bits that are set to 1 in the filter.
def bits_count
@bf.num_bit
def set_bits
@bf.set_bits
end

def bitmap
Expand Down
6 changes: 4 additions & 2 deletions spec/native_spec.rb
Expand Up @@ -47,11 +47,13 @@
it "should return the number of bits set to 1" do
bf = Native.new(:hashes => 4)
bf.insert("test")
bf.bits_count.should == 4
bf.set_bits.should == 4
bf.delete("test")
bf.set_bits.should == 0

bf = Native.new(:hashes => 1)
bf.insert("test")
bf.bits_count.should == 1
bf.set_bits.should == 1
end
end

Expand Down

0 comments on commit 808ee11

Please sign in to comment.