class Bitset
Public Class Methods
from_s(p1)
click to toggle source
static VALUE rb_bitset_from_s(VALUE self, VALUE s) { int length = RSTRING_LEN(s); char* data = StringValuePtr(s); Bitset * new_bs = bitset_new(); bitset_setup(new_bs, length); int i; for (i = 0; i < length; i++) { if (data[i] == '1') { set_bit(new_bs, i); } } return Data_Wrap_Struct(cBitset, 0, bitset_free, new_bs); }
new(p1)
click to toggle source
static VALUE rb_bitset_initialize(VALUE self, VALUE len) { Bitset * bs = get_bitset(self); bitset_setup(bs, NUM2INT(len)); return self; }
Public Instance Methods
[](p1)
click to toggle source
static VALUE rb_bitset_aref(VALUE self, VALUE index) { Bitset * bs = get_bitset(self); int idx = NUM2INT(index); validate_index(bs, idx); return get_bit(bs, idx) > 0 ? Qtrue : Qfalse; }
[]=(p1, p2)
click to toggle source
static VALUE rb_bitset_aset(VALUE self, VALUE index, VALUE value) { Bitset * bs = get_bitset(self); int idx = NUM2INT(index); validate_index(bs, idx); assign_bit(bs, idx, value); return Qtrue; }
cardinality()
click to toggle source
static VALUE rb_bitset_cardinality(VALUE self) { Bitset * bs = get_bitset(self); int i; int max = ((bs->len-1) >> 6) + 1; int count = 0; for(i = 0; i < max; i++) { uint64_t segment = bs->data[i]; if(i+1 == max && (bs->len & 0x3F)) segment &= ((((uint64_t) 1) << (bs->len & 0x3F)) - 1); count += __builtin_popcountll(segment); } return INT2NUM(count); }
clear(*args)
click to toggle source
static VALUE rb_bitset_clear(int argc, VALUE * argv, VALUE self) { int i; Bitset * bs = get_bitset(self); if (argc == 1 && rb_obj_is_kind_of(argv[0], rb_const_get(rb_cObject, rb_intern("Array")))) { for(i = 0; i < RARRAY_LEN(argv[0]); i++) { VALUE index = RARRAY_PTR(argv[0])[i]; int idx = NUM2INT(index); validate_index(bs, idx); clear_bit(bs, idx); } } else { for(i = 0; i < argc; i++) { VALUE index = argv[i]; int idx = NUM2INT(index); validate_index(bs, idx); clear_bit(bs, idx); } } return Qtrue; }
clear?(*args)
click to toggle source
static VALUE rb_bitset_clear_p(int argc, VALUE * argv, VALUE self) { int i; Bitset * bs = get_bitset(self); for(i = 0; i < argc; i++) { VALUE index = argv[i]; int idx = NUM2INT(index); validate_index(bs, idx); if(get_bit(bs, idx) > 0) return Qfalse; } return Qtrue; }
difference(p1)
click to toggle source
static VALUE rb_bitset_difference(VALUE self, VALUE other) { Bitset * bs = get_bitset(self); Bitset * other_bs = get_bitset(other); Bitset * new_bs = bitset_new(); bitset_setup(new_bs, bs->len); int max = ((bs->len-1) >> 6) + 1; int i; for(i = 0; i < max; i++) { uint64_t segment = bs->data[i]; uint64_t other_segment = other_bs->data[i]; new_bs->data[i] = segment & ~other_segment; } return Data_Wrap_Struct(cBitset, 0, bitset_free, new_bs); }
Also aliased as: -
each()
click to toggle source
static VALUE rb_bitset_each(VALUE self) { Bitset * bs = get_bitset(self); int i; for(i = 0; i < bs->len; i++) { rb_yield(get_bit(bs, i) > 0 ? Qtrue : Qfalse); } return self; }
hamming(p1)
click to toggle source
static VALUE rb_bitset_hamming(VALUE self, VALUE other) { Bitset * bs = get_bitset(self); Bitset * other_bs = get_bitset(other); int max = ((bs->len-1) >> 6) + 1; int count = 0; int i; for(i = 0; i < max; i++) { uint64_t segment = bs->data[i]; uint64_t other_segment = other_bs->data[i]; count += __builtin_popcountll(segment ^ other_segment); } return INT2NUM(count); }
intersect(p1)
click to toggle source
static VALUE rb_bitset_intersect(VALUE self, VALUE other) { Bitset * bs = get_bitset(self); Bitset * other_bs = get_bitset(other); Bitset * new_bs = bitset_new(); bitset_setup(new_bs, bs->len); int max = ((bs->len-1) >> 6) + 1; int i; for(i = 0; i < max; i++) { uint64_t segment = bs->data[i]; uint64_t other_segment = other_bs->data[i]; new_bs->data[i] = segment & other_segment; } return Data_Wrap_Struct(cBitset, 0, bitset_free, new_bs); }
Also aliased as: &
marshal_dump()
click to toggle source
static VALUE rb_bitset_marshall_dump(VALUE self) { Bitset * bs = get_bitset(self); VALUE hash = rb_hash_new(); VALUE data = rb_str_new(bs->data, BYTES(bs)); rb_hash_aset(hash, ID2SYM(rb_intern("len")), UINT2NUM(bs->len)); rb_hash_aset(hash, ID2SYM(rb_intern("data")), data); return hash; }
marshal_load(p1)
click to toggle source
static VALUE rb_bitset_marshall_load(VALUE self, VALUE hash) { Bitset * bs = get_bitset(self); int len = NUM2INT(rb_hash_aref(hash, ID2SYM(rb_intern("len")))); VALUE data = rb_hash_aref(hash, ID2SYM(rb_intern("data"))); bitset_setup(bs, len); bs->data = (uint64_t *) calloc(INTS(bs), sizeof(uint64_t)); memcpy(bs->data, RSTRING_PTR(data), BYTES(bs)); return Qnil; }
not()
click to toggle source
static VALUE rb_bitset_not(VALUE self) { Bitset * bs = get_bitset(self); Bitset * new_bs = bitset_new(); bitset_setup(new_bs, bs->len); int max = ((bs->len-1) >> 6) + 1; int i; for(i = 0; i < max; i++) { uint64_t segment = bs->data[i]; new_bs->data[i] = ~segment; } return Data_Wrap_Struct(cBitset, 0, bitset_free, new_bs); }
Also aliased as: ~
set(*args)
click to toggle source
static VALUE rb_bitset_set(int argc, VALUE * argv, VALUE self) { int i; Bitset * bs = get_bitset(self); if (argc == 1 && rb_obj_is_kind_of(argv[0], rb_const_get(rb_cObject, rb_intern("Array")))) { for(i = 0; i < RARRAY_LEN(argv[0]); i++) { VALUE index = RARRAY_PTR(argv[0])[i]; int idx = NUM2INT(index); validate_index(bs, idx); set_bit(bs, idx); } } else { for(i = 0; i < argc; i++) { VALUE index = argv[i]; int idx = NUM2INT(index); validate_index(bs, idx); set_bit(bs, idx); } } return Qtrue; }
set?(*args)
click to toggle source
static VALUE rb_bitset_set_p(int argc, VALUE * argv, VALUE self) { int i; Bitset * bs = get_bitset(self); for(i = 0; i < argc; i++) { VALUE index = argv[i]; int idx = NUM2INT(index); validate_index(bs, idx); if(get_bit(bs, idx) == 0) return Qfalse; } return Qtrue; }
size()
click to toggle source
static VALUE rb_bitset_size(VALUE self, VALUE len) { Bitset * bs = get_bitset(self); return INT2NUM(bs->len); }
to_a()
click to toggle source
static VALUE rb_bitset_to_a(VALUE self) { Bitset * bs = get_bitset(self); int i; VALUE array = rb_ary_new2(bs->len / 2); for(i = 0; i < bs->len; i++) { if (get_bit(bs, i) > 0) { rb_ary_push(array, INT2NUM(i)); } } return array; }
to_binary_array()
click to toggle source
static VALUE rb_bitset_to_binary_array(VALUE self) { Bitset * bs = get_bitset(self); int i; VALUE array = rb_ary_new2(bs->len / 2); for(i = 0; i < bs->len; i++) { rb_ary_push(array, INT2NUM(get_bit(bs, i) > 0 ? 1 : 0)); } return array; }
to_s()
click to toggle source
static VALUE rb_bitset_to_s(VALUE self) { Bitset * bs = get_bitset(self); int i; char * data = malloc(bs->len + 1); for(i = 0; i < bs->len; i++) { data[i] = get_bit(bs, i) ? '1' : '0'; } data[bs->len] = 0; return rb_str_new2(data); }
union(p1)
click to toggle source
static VALUE rb_bitset_union(VALUE self, VALUE other) { Bitset * bs = get_bitset(self); Bitset * other_bs = get_bitset(other); Bitset * new_bs = bitset_new(); bitset_setup(new_bs, bs->len); int max = ((bs->len-1) >> 6) + 1; int i; for(i = 0; i < max; i++) { uint64_t segment = bs->data[i]; uint64_t other_segment = other_bs->data[i]; new_bs->data[i] = segment | other_segment; } return Data_Wrap_Struct(cBitset, 0, bitset_free, new_bs); }
Also aliased as: |
xor(p1)
click to toggle source
static VALUE rb_bitset_xor(VALUE self, VALUE other) { Bitset * bs = get_bitset(self); Bitset * other_bs = get_bitset(other); Bitset * new_bs = bitset_new(); bitset_setup(new_bs, bs->len); int max = ((bs->len-1) >> 6) + 1; int i; for(i = 0; i < max; i++) { uint64_t segment = bs->data[i]; uint64_t other_segment = other_bs->data[i]; new_bs->data[i] = segment ^ other_segment; } return Data_Wrap_Struct(cBitset, 0, bitset_free, new_bs); }
Also aliased as: ^, symmetric_difference