Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix crc32 overload #39

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 22 additions & 23 deletions ext/cbloomfilter/cbloomfilter.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
*/

#include "ruby.h"
#include "crc32.h"

#if !defined(RSTRING_LEN)
# define RSTRING_LEN(x) (RSTRING(x)->len)
Expand Down Expand Up @@ -82,6 +81,21 @@ int bucket_get(struct BloomFilter *bf, int index) {
return (c & mask) >> bit_offset;
}

static unsigned int key2index(VALUE key, int seed, int m) {
VALUE res;
int index;
unsigned int h;

if (rb_block_given_p())
res = rb_yield_values(2, key, INT2FIX(seed));
else
rb_raise(rb_eArgError, "a block is required");

h = FIX2UINT(res);
index = (int) (h % (unsigned int) m);
return index;
}

static VALUE bf_s_new(int argc, VALUE *argv, VALUE self) {
struct BloomFilter *bf;
VALUE arg1, arg2, arg3, arg4, arg5, obj;
Expand Down Expand Up @@ -193,17 +207,11 @@ static VALUE bf_set_bits(VALUE self){
}

static VALUE bf_insert(VALUE self, VALUE key) {
VALUE skey;
int index, seed;
int i, len, m, k, s;
char *ckey;
int i, m, k, s;
struct BloomFilter *bf;
Data_Get_Struct(self, struct BloomFilter, bf);

skey = rb_obj_as_string(key);
ckey = StringValuePtr(skey);
len = (int) (RSTRING_LEN(skey)); /* length of the string in bytes */

m = bf->m;
k = bf->k;
s = bf->s;
Expand All @@ -213,7 +221,7 @@ static VALUE bf_insert(VALUE self, VALUE key) {
seed = i + s;

/* hash */
index = (int) (crc32((unsigned int) (seed), ckey, len) % (unsigned int) (m));
index = key2index(key, seed, m);

/* set a bit at the index */
bucket_set(bf, index);
Expand All @@ -222,6 +230,7 @@ static VALUE bf_insert(VALUE self, VALUE key) {
return Qnil;
}


static VALUE bf_merge(VALUE self, VALUE other) {
struct BloomFilter *bf, *target;
int i;
Expand Down Expand Up @@ -279,16 +288,10 @@ static VALUE bf_or(VALUE self, VALUE other) {

static VALUE bf_delete(VALUE self, VALUE key) {
int index, seed;
int i, len, m, k, s;
char *ckey;
VALUE skey;
int i, m, k, s;
struct BloomFilter *bf;
Data_Get_Struct(self, struct BloomFilter, bf);

skey = rb_obj_as_string(key);
ckey = StringValuePtr(skey);
len = (int) (RSTRING_LEN(skey)); /* length of the string in bytes */

m = bf->m;
k = bf->k;
s = bf->s;
Expand All @@ -298,7 +301,7 @@ static VALUE bf_delete(VALUE self, VALUE key) {
seed = i + s;

/* hash */
index = (int) (crc32((unsigned int) (seed), ckey, len) % (unsigned int) (m));
index = key2index(key, seed, m);

/* set a bit at the index */
bucket_unset(bf, index);
Expand All @@ -310,8 +313,7 @@ static VALUE bf_delete(VALUE self, VALUE key) {

static VALUE bf_include(int argc, VALUE* argv, VALUE self) {
int index, seed;
int i, len, m, k, s, tests_idx, vlen;
char *ckey;
int i, m, k, s, tests_idx, vlen;
VALUE tests, key, skey;
struct BloomFilter *bf;

Expand All @@ -321,9 +323,6 @@ static VALUE bf_include(int argc, VALUE* argv, VALUE self) {
vlen = RARRAY_LEN(tests);
for(tests_idx = 0; tests_idx < vlen; tests_idx++) {
key = rb_ary_entry(tests, tests_idx);
skey = rb_obj_as_string(key);
ckey = StringValuePtr(skey);
len = (int) (RSTRING_LEN(skey)); /* length of the string in bytes */

m = bf->m;
k = bf->k;
Expand All @@ -334,7 +333,7 @@ static VALUE bf_include(int argc, VALUE* argv, VALUE self) {
seed = i + s;

/* hash */
index = (int) (crc32((unsigned int) (seed), ckey, len) % (unsigned int) (m));
index = key2index(key, seed, m);

/* check the bit at the index */
if (!bucket_check(bf, index)) {
Expand Down
32 changes: 0 additions & 32 deletions ext/cbloomfilter/crc32.c

This file was deleted.

78 changes: 0 additions & 78 deletions ext/cbloomfilter/crc32.h

This file was deleted.

21 changes: 18 additions & 3 deletions lib/bloomfilter/native.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
require 'zlib'

module BloomFilter
BloomFilter::ConfigurationMismatch = Class.new(ArgumentError)

Expand All @@ -22,18 +24,31 @@ def initialize(opts = {})
@bf = CBloomFilter.new(@opts[:size], @opts[:hashes], @opts[:seed], @opts[:bucket], @opts[:raise])
end

def key2index(key, seed)
Zlib.crc32(key.to_s, seed)
end

def insert(key)
@bf.insert(key)
@bf.insert(key) do |key, seed|
key2index(key, seed)
end
end
alias :[]= :insert

def include?(*keys)
@bf.include?(*keys)
@bf.include?(*keys) do |key, seed|
key2index(key, seed)
end
end
alias :key? :include?
alias :[] :include?

def delete(key); @bf.delete(key); end
def delete(key)
@bf.delete(key) do |key, seed|
key2index(key, seed)
end
end

def clear; @bf.clear; end
def size; @bf.set_bits; end
def merge!(o); @bf.merge!(o.bf); end
Expand Down
2 changes: 1 addition & 1 deletion lib/bloomfilter/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module BloomFilter
VERSION = "2.1.2"
VERSION = "2.2.0"
end