Class: CBloomFilter

Inherits:
Object
  • Object
show all
Defined in:
ext/cbloomfilter/cbloomfilter.c

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.new(*args) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'ext/cbloomfilter/cbloomfilter.c', line 80

static VALUE bf_s_new(int argc, VALUE *argv, VALUE self) {
    struct BloomFilter *bf;
    VALUE arg1, arg2, arg3, arg4, arg5, obj;
    int m, k, s, b, r, bytes;

    obj = Data_Make_Struct(self, struct BloomFilter, NULL, bits_free, bf);

    /* default = Fugou approach :-) */
    arg1 = INT2FIX(100000000);
    arg2 = INT2FIX(4);
    arg3 = INT2FIX(0);
    arg4 = INT2FIX(1);
    arg5 = INT2FIX(0);

    switch (argc) {
        case 5:
      if (argv[4] == Qtrue) {
        arg5 = INT2FIX(1);
      }
        case 4:
      arg4 = argv[3];
        case 3:
      arg3 = argv[2];
        case 2:
      arg2 = argv[1];
        case 1:
      arg1 = argv[0];
      break;
    }

    m = FIX2INT(arg1);
    k = FIX2INT(arg2);
    s = FIX2INT(arg3);
    b = FIX2INT(arg4);
    r = FIX2INT(arg5);

    if (b < 1 || b > 8)
        rb_raise(rb_eArgError, "bucket size");
    if (m < 1)
        rb_raise(rb_eArgError, "array size");
    if (k < 1)
        rb_raise(rb_eArgError, "hash length");
    if (s < 0)
        rb_raise(rb_eArgError, "random seed");

    bf->b = b;
    bf->m = m;
    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);

    /* initialize the bits with zeros */
    memset(bf->ptr, 0, bf->bytes);
    rb_iv_set(obj, "@hash_value", rb_hash_new());

    return obj;
}

Instance Method Details

#bObject



161
162
163
164
165
# File 'ext/cbloomfilter/cbloomfilter.c', line 161

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

#bitmapObject



308
309
310
311
312
313
314
315
316
317
318
319
320
# File 'ext/cbloomfilter/cbloomfilter.c', line 308

static VALUE bf_bitmap(VALUE self) {
    struct BloomFilter *bf;
    Data_Get_Struct(self, struct BloomFilter, bf);

    VALUE str = rb_str_new(0, bf->m);
    unsigned char* ptr = (unsigned char *) RSTRING_PTR(str);

    int i;
    for (i = 0; i < bf->m; i++)
        *ptr++ = bucket_get(bf, i);

    return str;
}

#clearObject



142
143
144
145
146
147
# File 'ext/cbloomfilter/cbloomfilter.c', line 142

static VALUE bf_clear(VALUE self) {
    struct BloomFilter *bf;
    Data_Get_Struct(self, struct BloomFilter, bf);
    memset(bf->ptr, 0, bf->bytes);
    return Qtrue;
}

#delete(key) ⇒ Object



221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
# File 'ext/cbloomfilter/cbloomfilter.c', line 221

static VALUE bf_delete(VALUE self, VALUE key) {
    int index, seed;
    int i, len, m, k, s;
    char *ckey;
    VALUE skey;
    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;

    for (i = 0; i <= k - 1; i++) {
        /* seeds for hash functions */
        seed = i + s;

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

        /*  set a bit at the index */
        bucket_unset(bf, index);
    }

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

#include?(*args) ⇒ Boolean

Returns:

  • (Boolean)


253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
# File 'ext/cbloomfilter/cbloomfilter.c', line 253

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

    rb_scan_args(argc, argv, "*", &tests);

    Data_Get_Struct(self, struct BloomFilter, bf);
    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;
      s = bf->s;

      for (i = 0; i <= k - 1; i++) {
          /* seeds for hash functions */
          seed = i + s;

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

          /* check the bit at the index */
          if (!bucket_check(bf, index)) {
              return Qfalse; /* i.e., it is a new entry ; escape the loop */
          }
      }

      return Qtrue;
    }

}

#insert(key) ⇒ Object



179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
# File 'ext/cbloomfilter/cbloomfilter.c', line 179

static VALUE bf_insert(VALUE self, VALUE key) {
    VALUE skey;
    int index, seed;
    int i, len, m, k, s;
    char *ckey;
    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;

    for (i = 0; i <= k - 1; i++) {
        /* seeds for hash functions */
        seed = i + s;

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

        /*  set a bit at the index */
        bucket_set(bf, index);
    }

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

#kObject



155
156
157
158
159
# File 'ext/cbloomfilter/cbloomfilter.c', line 155

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

#load(bitmap) ⇒ Object



322
323
324
325
326
327
328
329
330
331
332
333
334
# File 'ext/cbloomfilter/cbloomfilter.c', line 322

static VALUE bf_load(VALUE self, VALUE bitmap) {
    struct BloomFilter *bf;
    Data_Get_Struct(self, struct BloomFilter, bf);
    unsigned char* ptr = (unsigned char *) RSTRING_PTR(bitmap);

    int i;
    for (i = 0; i < bf->m; i++) {
      if (*ptr++)
        bucket_set(bf, i);
    }

    return Qnil;
}

#mObject



149
150
151
152
153
# File 'ext/cbloomfilter/cbloomfilter.c', line 149

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

#merge!(other) ⇒ Object



210
211
212
213
214
215
216
217
218
219
# File 'ext/cbloomfilter/cbloomfilter.c', line 210

static VALUE bf_merge(VALUE self, VALUE other) {
    struct BloomFilter *bf, *target;
    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;
}

#num_setObject



173
174
175
176
177
# File 'ext/cbloomfilter/cbloomfilter.c', line 173

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

#rObject



167
168
169
170
171
# File 'ext/cbloomfilter/cbloomfilter.c', line 167

static VALUE bf_r(VALUE self) {
    struct BloomFilter *bf;
    Data_Get_Struct(self, struct BloomFilter, bf);
    return bf->r == 0 ? Qfalse : Qtrue;
}

#to_sObject



292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
# File 'ext/cbloomfilter/cbloomfilter.c', line 292

static VALUE bf_to_s(VALUE self) {
    struct BloomFilter *bf;
    unsigned char *ptr;
    int i;
    VALUE str;

    Data_Get_Struct(self, struct BloomFilter, bf);
    str = rb_str_new(0, bf->m);

    ptr = (unsigned char *) RSTRING_PTR(str);
    for (i = 0; i < bf->m; i++)
        *ptr++ = bucket_get(bf, i) ? '1' : '0';

    return str;
}