Skip to content

Commit

Permalink
logical or and and
Browse files Browse the repository at this point in the history
  • Loading branch information
Josh Ferguson committed Apr 21, 2012
1 parent 7594f70 commit ace84a9
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 10 deletions.
9 changes: 9 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
require 'echoe'
require 'rake'
require 'rspec/core/rake_task'

task :default => :spec

Echoe.new("ewah-bitset") do |p|
p.author = "Josh Ferguson"
Expand All @@ -7,3 +11,8 @@ Echoe.new("ewah-bitset") do |p|
p.summary = "A wrapper around Lemire's EWAHBoolArray from https://github.com/lemire/EWAHBoolArray"
p.url = "http://www.github.com/yammer/ewah-bitset/"
end

RSpec::Core::RakeTask.new(:spec) do |t|
t.pattern = 'spec/**/*_spec.rb'
t.rspec_opts = ['--options', "\"spec/spec.opts\""]
end
15 changes: 10 additions & 5 deletions ext/ewah-bitset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,8 @@ extern "C" VALUE ewah_logical_or(VALUE self, VALUE other) {
Data_Get_Struct(self, EWAH, bitset);
Data_Get_Struct(other, EWAH, obitset);

VALUE klass = rb_const_get(rb_cObject, rb_intern("EwahBitset"));
VALUE newBitset = rb_class_new_instance(0, 0, klass);

VALUE newBitset = ewah_new(rb_path2class("EwahBitset"));

EWAH *newBits;
Data_Get_Struct(newBitset, EWAH, newBits);
bitset->bits->logicalor(*(obitset->bits), *(newBits->bits));
Expand All @@ -88,8 +87,7 @@ extern "C" VALUE ewah_logical_and(VALUE self, VALUE other) {
Data_Get_Struct(self, EWAH, bitset);
Data_Get_Struct(other, EWAH, obitset);

VALUE klass = rb_const_get(rb_cObject, rb_intern("EwahBitset"));
VALUE newBitset = rb_class_new_instance(0, 0, klass);
VALUE newBitset = ewah_new(rb_path2class("EwahBitset"));

EWAH *newBits;
Data_Get_Struct(newBitset, EWAH, newBits);
Expand All @@ -112,6 +110,12 @@ extern "C" VALUE ewah_equals(VALUE self, VALUE other) {
}

/* Information & Serialization */
extern "C" VALUE ewah_size_in_bits(VALUE self) {
EWAH *bitset;
Data_Get_Struct(self, EWAH, bitset);
return INT2FIX(bitset->bits->sizeInBits());
}

extern "C" VALUE ewah_size_in_bytes(VALUE self) {
EWAH *bitset;
Data_Get_Struct(self, EWAH, bitset);
Expand Down Expand Up @@ -166,5 +170,6 @@ extern "C" void Init_ewahbitset() {
rb_define_method(rb_cC, "to_binary_s", (ruby_method*) &ewah_to_binary_s, 0);
rb_define_method(rb_cC, "serialize", (ruby_method*) &ewah_serialize, 0);
rb_define_method(rb_cC, "deserialize", (ruby_method*) &ewah_deserialize, 1);
rb_define_method(rb_cC, "size_in_bits", (ruby_method*) ewah_size_in_bits, 0);
rb_define_method(rb_cC, "size_in_bytes", (ruby_method*) ewah_size_in_bytes, 0);
}
68 changes: 63 additions & 5 deletions spec/ewah_bitset_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
@bitset = EwahBitset.new
end

it "should initialize and set bits" do
it "should set bits" do
positions = []
0.upto(10) do |i|
@bitset.set(i * 10)
Expand All @@ -20,15 +20,73 @@
positions.should == bPositions
end

it "should compare properly" do
@other = EwahBitset.new
it "should reset" do
0.upto(10) do |i|
@bitset.set(i * 10)
end

@bitset.size_in_bits.should == 101
@bitset.reset
@bitset.size_in_bits.should == 0
end

it "should compare" do
other = EwahBitset.new

0.upto(10) do |i|
@bitset.set(i * 10)
@other.set(i * 10)
other.set(i * 10)
end

@bitset.should == other
end

it "should perform a logical or" do
other = EwahBitset.new

positions = []
0.upto(5) do |i|
@bitset.set(i * 10)
positions << i * 10
end

6.upto(10) do |i|
other.set(i * 10)
positions << i * 10
end

newbits = @bitset.logical_or(other)

bPositions = []
newbits.each do |pos|
bPositions << pos
end

positions.should == bPositions
end

it "should perform a logical and operation" do
other = EwahBitset.new

positions = []
0.upto(5) do |i|
@bitset.set(i * 10)
positions << i * 10
end

5.upto(10) do |i|
other.set(i * 10)
positions << i * 10
end

newbits = @bitset.logical_and(other)

bPositions = []
newbits.each do |pos|
bPositions << pos
end

@bitset.should == @other
(positions & bPositions).should == [50]
end

it "should serialize and deserialize" do
Expand Down
2 changes: 2 additions & 0 deletions spec/spec.opts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
--colour
--format progress

0 comments on commit ace84a9

Please sign in to comment.