Class: BitArray
- Inherits:
-
Object
- Object
- BitArray
- Includes:
- Enumerable
- Defined in:
- ext/bitarray.c,
ext/bitarray.c
Overview
An array of bits. Usage is similar to the standard Array class, but the only allowed elements are 1 and 0. BitArrays are not resizable.
Instance Method Summary collapse
-
#+(other_bitarray) ⇒ Object
Concatenation—Return a new BitArray built by concatenating the two BitArrays.
-
#[](*args) ⇒ Object
(also: #slice)
Bit Reference—Returns the bit at index, or returns a subarray starting at beg, and continuing for len bits, or returns a subarray specified by range.
-
#[]=(index) ⇒ Object
Bit Assignment—Sets the bit at index.
-
#clear_all_bits ⇒ Object
Sets all bits to 0.
-
#clear_bit(index) ⇒ Object
Sets the bit at index to 0.
-
#each {|bit| ... } ⇒ Object
Calls
blockonce for each bit in bitarray, passing that bit as a parameter. -
#initialize(arg) ⇒ Object
constructor
When called with a size, creates a new BitArray of the specified size, with all bits cleared.
-
#initialize_copy(orig) ⇒ Object
Produces a copy of bitarray.
-
#inspect ⇒ Object
(also: #to_s)
Create a printable version of bitarray.
-
#set_all_bits ⇒ Object
Sets all bits to 1.
-
#set_bit(index) ⇒ Object
Sets the bit at index to 1.
-
#size ⇒ Object
(also: #length)
Returns the number of bits in bitarray.
-
#to_a ⇒ Array
Creates a Ruby Array from bitarray.
-
#toggle_all_bits ⇒ Object
Toggle all bits.
-
#toggle_bit(index) ⇒ Object
Toggles the bit at index to 0.
-
#total_set ⇒ Integer
Return the number of set (1) bits in bitarray.
Constructor Details
#new(size) ⇒ Object #new(string) ⇒ Object #new(array) ⇒ Object
When called with a size, creates a new BitArray of the specified size, with all bits cleared. When called with a string or an array, creates a new BitArray from the argument.
If a string is given, it should consist of ones and zeroes. If there are any other characters in the string, the first invalid character and all following characters will be ignored.
b = BitArray.new("10101010") => 10101010
b = BitArray.new("1010abcd") => 1010
b = BitArray.new("abcd") =>
If an array is given, the BitArray is initialized from its elements using the following rules:
-
0, false, or nil => 0
-
anything else => 1
Note that the 0 is a number, not a string. “Anything else” means strings, symbols, non-zero numbers, subarrays, etc.
b = BitArray.new([0,0,0,1,1,0]) => 000110
b = BitArray.new([false, true, false]) => 010
b = BitArray.new([:a, :b, :c, [:d, :e]]) => 1111
328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 |
# File 'ext/bitarray.c', line 328
static VALUE
rb_bitarray_initialize(VALUE self, VALUE arg)
{
if (TYPE(arg) == T_FIXNUM || TYPE(arg) == T_BIGNUM) {
struct bitarray *ba;
Data_Get_Struct(self, struct bitarray, ba);
initialize_bitarray(ba, NUM2LONG(arg));
return self;
} else if (TYPE(arg) == T_STRING) {
return rb_bitarray_from_string(self, arg);
} else if (TYPE(arg) == T_ARRAY) {
return rb_bitarray_from_array(self, arg);
} else {
rb_raise(rb_eArgError, "must be size, string, or array");
}
}
|
Instance Method Details
#+(other_bitarray) ⇒ Object
Concatenation—Return a new BitArray built by concatenating the two BitArrays.
454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 |
# File 'ext/bitarray.c', line 454
static VALUE
rb_bitarray_concat(VALUE x, VALUE y)
{
/* Get the bitarrays from x and y */
struct bitarray *x_ba, *y_ba;
Data_Get_Struct(x, struct bitarray, x_ba);
Data_Get_Struct(y, struct bitarray, y_ba);
/* Create a new BitArray, and its bitarray structure*/
VALUE z = rb_bitarray_alloc(rb_bitarray_class);
struct bitarray *z_ba;
Data_Get_Struct(z, struct bitarray, z_ba);
initialize_bitarray_concat(z_ba, x_ba, y_ba);
return z;
}
|
#[](index) ⇒ Object #[](beg, len) ⇒ Object #[](range) ⇒ Object Also known as: slice
Bit Reference—Returns the bit at index, or returns a subarray starting at beg, and continuing for len bits, or returns a subarray specified by range. _Negative indices count backwards from the end of bitarray. If index is greater than the capacity of bitarray, an IndexError is raised.
624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 |
# File 'ext/bitarray.c', line 624
static VALUE
rb_bitarray_bitref(int argc, VALUE *argv, VALUE self)
{
/* We follow a form similar to rb_ary_aref in array.c */
/* Two arguments means we have a beginning and a length */
if (argc == 2) {
long beg = NUM2LONG(argv[0]);
long len = NUM2LONG(argv[1]);
return rb_bitarray_subseq(self, beg, len);
}
/* Make sure we have either 1 or 2 arguments. */
if (argc != 1) {
rb_scan_args(argc, argv, "11", 0, 0);
}
/* If we have a single argument, it can be either an index, or a range. */
VALUE arg = argv[0];
/* rb_ary_aref treats a fixnum argument specially, for a speedup in the
* most common case. We'll do the same.
*/
if (FIXNUM_P(arg)) {
return rb_bitarray_get_bit(self, FIX2LONG(arg));
}
struct bitarray *ba;
Data_Get_Struct(self, struct bitarray, ba);
/* Next we see if arg is a range. rb_range_beg_len is defined in range.c
* If arg is not a range, it returns Qfalse. If arg is a range, but it
* refers to invalid indices, it returns Qnil. Otherwise, it sets beg and
* end to the appropriate values.
*/
long beg, len;
switch (rb_range_beg_len(arg, &beg, &len, ba->bits, 0)) {
case Qfalse:
break;
case Qnil:
return Qnil;
default:
return rb_bitarray_subseq(self, beg, len);
}
return rb_bitarray_get_bit(self, NUM2LONG(arg));
}
|
#[]=(index) ⇒ Object
Bit Assignment—Sets the bit at index. value must be 0 or 1. Negative indices are allowed, and will count backwards from the end of bitarray.
If index is greater than the capacity of bitarray, an IndexError is raised. If value is something other than 0 or 1, a RuntimeError is raised.
743 744 745 746 747 748 749 750 751 |
# File 'ext/bitarray.c', line 743
static VALUE
rb_bitarray_assign_bit(VALUE self, VALUE index, VALUE value)
{
struct bitarray *ba;
Data_Get_Struct(self, struct bitarray, ba);
assign_bit(ba, NUM2LONG(index), NUM2INT(value));
return value;
}
|
#clear_all_bits ⇒ Object
Sets all bits to 0.
561 562 563 564 565 566 567 568 569 |
# File 'ext/bitarray.c', line 561
static VALUE
rb_bitarray_clear_all_bits(VALUE self)
{
struct bitarray *ba;
Data_Get_Struct(self, struct bitarray, ba);
clear_all_bits(ba);
return self;
}
|
#clear_bit(index) ⇒ Object
Sets the bit at index to 0. Negative indices count backwards from the end of bitarray. If index is greater than the capacity of bitarray, an IndexError is raised.
545 546 547 548 549 550 551 552 553 |
# File 'ext/bitarray.c', line 545
static VALUE
rb_bitarray_clear_bit(VALUE self, VALUE index)
{
struct bitarray *ba;
Data_Get_Struct(self, struct bitarray, ba);
clear_bit(ba, NUM2LONG(index));
return self;
}
|
#each {|bit| ... } ⇒ Object
816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 |
# File 'ext/bitarray.c', line 816
static VALUE
rb_bitarray_each(VALUE self)
{
struct bitarray *ba;
Data_Get_Struct(self, struct bitarray, ba);
long i;
RETURN_ENUMERATOR(self, 0, 0);
for (i = 0; i < bitarray_size(ba); i++) {
int bit_value = get_bit(ba, i);
rb_yield(INT2NUM(bit_value));
}
return self;
}
|
#clone ⇒ Object #dup ⇒ Object
Produces a copy of bitarray.
435 436 437 438 439 440 441 442 443 444 445 |
# File 'ext/bitarray.c', line 435
static VALUE
rb_bitarray_initialize_copy(VALUE self, VALUE orig)
{
struct bitarray *new_ba, *orig_ba;
Data_Get_Struct(self, struct bitarray, new_ba);
Data_Get_Struct(orig, struct bitarray, orig_ba);
initialize_bitarray_copy(new_ba, orig_ba);
return self;
}
|
#inspect ⇒ String #to_s ⇒ String Also known as: to_s
Create a printable version of bitarray.
760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 |
# File 'ext/bitarray.c', line 760
static VALUE
rb_bitarray_inspect(VALUE self)
{
struct bitarray *ba;
Data_Get_Struct(self, struct bitarray, ba);
long cstr_size = bitarray_size(ba) + 1;
char cstr[cstr_size];
long i;
for (i = 0; i < bitarray_size(ba); i++) {
cstr[i] = get_bit(ba, i) + '0';
}
cstr[cstr_size - 1] = '\0';
VALUE str = rb_str_new2(cstr);
return str;
}
|
#set_all_bits ⇒ Object
Sets all bits to 1.
527 528 529 530 531 532 533 534 535 |
# File 'ext/bitarray.c', line 527
static VALUE
rb_bitarray_set_all_bits(VALUE self)
{
struct bitarray *ba;
Data_Get_Struct(self, struct bitarray, ba);
set_all_bits(ba);
return self;
}
|
#set_bit(index) ⇒ Object
Sets the bit at index to 1. Negative indices count backwards from the end of bitarray. If index is greater than the capacity of bitarray, an IndexError is raised.
511 512 513 514 515 516 517 518 519 |
# File 'ext/bitarray.c', line 511
static VALUE
rb_bitarray_set_bit(VALUE self, VALUE index)
{
struct bitarray *ba;
Data_Get_Struct(self, struct bitarray, ba);
set_bit(ba, NUM2LONG(index));
return self;
}
|
#size ⇒ Integer #length ⇒ Integer Also known as: length
Returns the number of bits in bitarray.
479 480 481 482 483 484 485 486 |
# File 'ext/bitarray.c', line 479
static VALUE
rb_bitarray_size(VALUE self)
{
struct bitarray *ba;
Data_Get_Struct(self, struct bitarray, ba);
return LONG2NUM(bitarray_size(ba));
}
|
#to_a ⇒ Array
Creates a Ruby Array from bitarray.
785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 |
# File 'ext/bitarray.c', line 785
static VALUE
rb_bitarray_to_a(VALUE self)
{
struct bitarray *ba;
Data_Get_Struct(self, struct bitarray, ba);
long array_size = bitarray_size(ba);
VALUE c_array[array_size];
int i;
for (i = 0; i < array_size; i++) {
c_array[i] = INT2FIX(get_bit(ba, i));
}
return rb_ary_new4(array_size, c_array);
}
|
#toggle_all_bits ⇒ Object
Toggle all bits.
595 596 597 598 599 600 601 602 603 |
# File 'ext/bitarray.c', line 595
static VALUE
rb_bitarray_toggle_all_bits(VALUE self)
{
struct bitarray *ba;
Data_Get_Struct(self, struct bitarray, ba);
toggle_all_bits(ba);
return self;
}
|
#toggle_bit(index) ⇒ Object
Toggles the bit at index to 0. Negative indices count backwards from the end of bitarray. If index is greater than the capacity of bitarray, an IndexError is raised.
579 580 581 582 583 584 585 586 587 |
# File 'ext/bitarray.c', line 579
static VALUE
rb_bitarray_toggle_bit(VALUE self, VALUE index)
{
struct bitarray *ba;
Data_Get_Struct(self, struct bitarray, ba);
toggle_bit(ba, NUM2LONG(index));
return self;
}
|
#total_set ⇒ Integer
Return the number of set (1) bits in bitarray.
494 495 496 497 498 499 500 501 |
# File 'ext/bitarray.c', line 494
static VALUE
rb_bitarray_total_set(VALUE self)
{
struct bitarray *ba;
Data_Get_Struct(self, struct bitarray, ba);
return LONG2NUM(total_set(ba));
}
|