Skip to content
This repository was archived by the owner on Sep 27, 2025. It is now read-only.

Commit d07d96d

Browse files
committed
Added to_binary_array method
1 parent 3abe25a commit d07d96d

File tree

5 files changed

+27
-5
lines changed

5 files changed

+27
-5
lines changed

Rakefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ Jeweler::Tasks.new do |gem|
1717
gem.summary = 'Bitset implementation.'
1818
gem.description = 'A fast C-based Bitset. It supports the standard set operations as well as operations you may expect on bit arrays. (popcount, for instance)'
1919
gem.email = "tbmcmullen@gmail.com"
20-
gem.authors = ["Tyler McMullen"]
20+
gem.authors = ["Tyler McMullen", "Brendon McLean"]
2121
end
2222
Jeweler::RubygemsDotOrgTasks.new
2323

VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
0.1.2
1+
0.1.3

bitset.gemspec

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@
55

66
Gem::Specification.new do |s|
77
s.name = %q{bitset}
8-
s.version = "0.1.2"
8+
s.version = "0.1.3"
99

1010
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11-
s.authors = ["Tyler McMullen"]
12-
s.date = %q{2011-05-07}
11+
s.authors = ["Tyler McMullen", "Brendon McLean"]
12+
s.date = %q{2011-10-06}
1313
s.description = %q{A fast C-based Bitset. It supports the standard set operations as well as operations you may expect on bit arrays. (popcount, for instance)}
1414
s.email = %q{tbmcmullen@gmail.com}
1515
s.extensions = ["ext/bitset/extconf.rb"]

ext/bitset/bitset.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,18 @@ static VALUE rb_bitset_to_a(VALUE self) {
349349
return array;
350350
}
351351

352+
static VALUE rb_bitset_to_binary_array(VALUE self) {
353+
Bitset * bs = get_bitset(self);
354+
int i;
355+
356+
VALUE array = rb_ary_new2(bs->len / 2);
357+
for(i = 0; i < bs->len; i++) {
358+
rb_ary_push(array, INT2NUM(get_bit(bs, i) > 0 ? 1 : 0));
359+
}
360+
361+
return array;
362+
}
363+
352364
void Init_bitset() {
353365
cBitset = rb_define_class("Bitset", rb_cObject);
354366
rb_include_module(cBitset, rb_mEnumerable);
@@ -380,4 +392,5 @@ void Init_bitset() {
380392
rb_define_method(cBitset, "marshal_dump", rb_bitset_marshall_dump, 0);
381393
rb_define_method(cBitset, "marshal_load", rb_bitset_marshall_load, 1);
382394
rb_define_method(cBitset, "to_a", rb_bitset_to_a, 0);
395+
rb_define_method(cBitset, "to_binary_array", rb_bitset_to_binary_array, 0);
383396
}

spec/bitset_spec.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,4 +278,13 @@
278278
bs.to_a.should == [1, 64, 65]
279279
end
280280
end
281+
282+
describe :to_binary_array do
283+
it "can convert to an array of 1s and 0s" do
284+
bs = Bitset.new(68)
285+
bs.set 1, 64, 65
286+
287+
bs.to_binary_array.values_at(1, 64, 65, 66).should == [1, 1, 1, 0]
288+
end
289+
end
281290
end

0 commit comments

Comments
 (0)