Class: Bitset
Class Method Summary collapse
Instance Method Summary collapse
- #[](index) ⇒ Object
- #[]=(index, value) ⇒ Object
- #cardinality ⇒ Object
- #clear(*args) ⇒ Object
- #clear?(*args) ⇒ Boolean
- #difference(other) ⇒ Object (also: #-)
- #each ⇒ Object
- #hamming(other) ⇒ Object
- #initialize(len) ⇒ Object constructor
- #intersect(other) ⇒ Object (also: #&)
- #marshal_dump ⇒ Object
- #marshal_load(hash) ⇒ Object
- #not ⇒ Object (also: #~)
- #set(*args) ⇒ Object
- #set?(*args) ⇒ Boolean
- #size(len) ⇒ Object
- #to_a ⇒ Object
- #to_binary_array ⇒ Object
- #to_s ⇒ Object
- #union(other) ⇒ Object (also: #|)
- #xor(other) ⇒ Object (also: #^, #symmetric_difference)
Constructor Details
#initialize(len) ⇒ Object
45 46 47 48 49 |
# File 'ext/bitset/bitset.c', line 45
static VALUE rb_bitset_initialize(VALUE self, VALUE len) {
Bitset * bs = get_bitset(self);
bitset_setup(bs, NUM2INT(len));
return self;
}
|
Class Method Details
.from_s(s) ⇒ Object
289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 |
# File 'ext/bitset/bitset.c', line 289
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);
}
|
Instance Method Details
#[](index) ⇒ Object
89 90 91 92 93 94 |
# File 'ext/bitset/bitset.c', line 89
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;
}
|
#[]=(index, value) ⇒ Object
96 97 98 99 100 101 102 |
# File 'ext/bitset/bitset.c', line 96
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 ⇒ Object
174 175 176 177 178 179 180 181 182 183 184 185 186 |
# File 'ext/bitset/bitset.c', line 174
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) ⇒ Object
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 |
# File 'ext/bitset/bitset.c', line 126
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) ⇒ Boolean
148 149 150 151 152 153 154 155 156 157 158 159 |
# File 'ext/bitset/bitset.c', line 148
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(other) ⇒ Object Also known as: -
224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 |
# File 'ext/bitset/bitset.c', line 224
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);
}
|
#each ⇒ Object
322 323 324 325 326 327 328 329 330 331 |
# File 'ext/bitset/bitset.c', line 322
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(other) ⇒ Object
306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 |
# File 'ext/bitset/bitset.c', line 306
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(other) ⇒ Object Also known as: &
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 |
# File 'ext/bitset/bitset.c', line 188
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);
}
|
#marshal_dump ⇒ Object
333 334 335 336 337 338 339 340 341 342 |
# File 'ext/bitset/bitset.c', line 333
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(hash) ⇒ Object
344 345 346 347 348 349 350 351 352 353 354 355 356 |
# File 'ext/bitset/bitset.c', line 344
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 ⇒ Object Also known as: ~
260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 |
# File 'ext/bitset/bitset.c', line 260
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);
}
|
#set(*args) ⇒ Object
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 |
# File 'ext/bitset/bitset.c', line 104
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) ⇒ Boolean
161 162 163 164 165 166 167 168 169 170 171 172 |
# File 'ext/bitset/bitset.c', line 161
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(len) ⇒ Object
51 52 53 54 |
# File 'ext/bitset/bitset.c', line 51
static VALUE rb_bitset_size(VALUE self, VALUE len) {
Bitset * bs = get_bitset(self);
return INT2NUM(bs->len);
}
|
#to_a ⇒ Object
358 359 360 361 362 363 364 365 366 367 368 369 370 |
# File 'ext/bitset/bitset.c', line 358
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 ⇒ Object
372 373 374 375 376 377 378 379 380 381 382 |
# File 'ext/bitset/bitset.c', line 372
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 ⇒ Object
276 277 278 279 280 281 282 283 284 285 286 287 |
# File 'ext/bitset/bitset.c', line 276
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(other) ⇒ Object Also known as: |
206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 |
# File 'ext/bitset/bitset.c', line 206
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);
}
|
#xor(other) ⇒ Object Also known as: ^, symmetric_difference
242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 |
# File 'ext/bitset/bitset.c', line 242
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);
}
|