From 872baabee109f7ae4f23899591c355f0eae85947 Mon Sep 17 00:00:00 2001 From: Gary Grossman Date: Mon, 14 Mar 2022 10:32:24 -0700 Subject: [PATCH 1/2] rdoctask is deprecated; switch to rdoc/task --- Rakefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Rakefile b/Rakefile index c172a75..a2a19b7 100644 --- a/Rakefile +++ b/Rakefile @@ -21,7 +21,7 @@ Jeweler::Tasks.new do |gem| end Jeweler::RubygemsDotOrgTasks.new -require 'rake/rdoctask' +require 'rdoc/task' Rake::RDocTask.new do |rdoc| version = File.exist?('VERSION') ? File.read('VERSION') : "" From 23ee610c5b4db55020bce57ce468f09a7307cc04 Mon Sep 17 00:00:00 2001 From: Gary Grossman Date: Mon, 14 Mar 2022 10:32:38 -0700 Subject: [PATCH 2/2] Add each_with_index and each_set_index methods --- ext/bitset/bitset.c | 40 ++++++++++++++++++++++++++++++++++++---- spec/bitset_spec.rb | 28 ++++++++++++++++++++++++++++ 2 files changed, 64 insertions(+), 4 deletions(-) diff --git a/ext/bitset/bitset.c b/ext/bitset/bitset.c index ed94a8e..fdcc89a 100644 --- a/ext/bitset/bitset.c +++ b/ext/bitset/bitset.c @@ -310,7 +310,37 @@ static VALUE rb_bitset_each(VALUE self) { return self; } -static VALUE rb_bitset_marshall_dump(VALUE self) { +static VALUE rb_bitset_each_with_index(VALUE self) { + Bitset * bs = get_bitset(self); + int i; + + for(i = 0; i < bs->len; i++) { + rb_yield_values(2, get_bit(bs, i) > 0 ? Qtrue : Qfalse, INT2NUM(i)); + } + + return self; +} + +static VALUE rb_bitset_each_set_index(VALUE self) { + Bitset * bs = get_bitset(self); + int i, n = INTS(bs); + + for(i = 0; i < n; i++) { + uint64_t bits = bs->data[i]; + int j = 0; + while (bits) { + if (bits & 1) { + rb_yield(INT2NUM((i << 6) + j)); + } + bits >>= 1; + j++; + } + } + + return self; +} + +static VALUE rb_bitset_marshal_dump(VALUE self) { Bitset * bs = get_bitset(self); VALUE hash = rb_hash_new(); VALUE data = rb_str_new(bs->data, BYTES(bs)); @@ -321,7 +351,7 @@ static VALUE rb_bitset_marshall_dump(VALUE self) { return hash; } -static VALUE rb_bitset_marshall_load(VALUE self, VALUE hash) { +static VALUE rb_bitset_marshal_load(VALUE self, VALUE hash) { Bitset * bs = get_bitset(self); int len = NUM2INT(rb_hash_aref(hash, ID2SYM(rb_intern("len")))); @@ -361,8 +391,10 @@ void Init_bitset() { rb_define_alias(cBitset, "~", "not"); rb_define_method(cBitset, "hamming", rb_bitset_hamming, 1); rb_define_method(cBitset, "each", rb_bitset_each, 0); + rb_define_method(cBitset, "each_with_index", rb_bitset_each_with_index, 0); + rb_define_method(cBitset, "each_set_index", rb_bitset_each_set_index, 0); rb_define_method(cBitset, "to_s", rb_bitset_to_s, 0); rb_define_singleton_method(cBitset, "from_s", rb_bitset_from_s, 1); - rb_define_method(cBitset, "marshal_dump", rb_bitset_marshall_dump, 0); - rb_define_method(cBitset, "marshal_load", rb_bitset_marshall_load, 1); + rb_define_method(cBitset, "marshal_dump", rb_bitset_marshal_dump, 0); + rb_define_method(cBitset, "marshal_load", rb_bitset_marshal_load, 1); } diff --git a/spec/bitset_spec.rb b/spec/bitset_spec.rb index fb7d00c..dca4be5 100644 --- a/spec/bitset_spec.rb +++ b/spec/bitset_spec.rb @@ -235,6 +235,34 @@ end end + describe :each_with_index do + it 'iterates over the bits in the Bitset and provides index' do + bs = Bitset.new(4) + bs.set 0, 2 + + i = 0 + bs.each_with_index do |bit, idx| + bit.should == bs[i] + idx.should == i + i += 1 + end + i.should == 4 + end + end + + describe :each_set_index do + it 'iterates over the set bits in the Bitset by index' do + bs = Bitset.new(100) + bs.set 0, 2, 8, 13, 17, 55, 88, 98 + + a = [] + bs.each_set_index do |idx| + a << idx + end + a.should == [0, 2, 8, 13, 17, 55, 88, 98] + end + end + describe :to_s do it 'correctly prints out a binary string' do bs = Bitset.new(4)