diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..2288186 --- /dev/null +++ b/.gitignore @@ -0,0 +1,42 @@ +# rcov generated +coverage + +# rdoc generated +rdoc + +# yard generated +doc +.yardoc + +# bundler +.bundle + +# jeweler generated +pkg + +# Have editor/IDE/OS specific files you need to ignore? Consider using a global gitignore: +# +# * Create a file at ~/.gitignore +# * Include files you want ignored +# * Run: git config --global core.excludesfile ~/.gitignore +# +# After doing this, these files will be ignored in all your git projects, +# saving you from having to 'pollute' every project you touch with them +# +# Not sure what to needs to be ignored for particular editors/OSes? Here's some ideas to get you started. (Remember, remove the leading # of the line) +# +# For MacOS: +# +#.DS_Store +# +# For TextMate +#*.tmproj +#tmtags +# +# For emacs: +#*~ +#\#* +#.\#* +# +# For vim: +#*.swp diff --git a/LICENSE.txt b/LICENSE.txt new file mode 100644 index 0000000..27f8f3d --- /dev/null +++ b/LICENSE.txt @@ -0,0 +1,20 @@ +Copyright (c) 2011 Tyler McMullen + +Permission is hereby granted, free of charge, to any person obtaining +a copy of this software and associated documentation files (the +"Software"), to deal in the Software without restriction, including +without limitation the rights to use, copy, modify, merge, publish, +distribute, sublicense, and/or sell copies of the Software, and to +permit persons to whom the Software is furnished to do so, subject to +the following conditions: + +The above copyright notice and this permission notice shall be +included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE +LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION +WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/README.rdoc b/README.rdoc new file mode 100644 index 0000000..ec96a36 --- /dev/null +++ b/README.rdoc @@ -0,0 +1,19 @@ += bitset + +Description goes here. + +== Contributing to bitset + +* Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet +* Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it +* Fork the project +* Start a feature/bugfix branch +* Commit and push until you are happy with your contribution +* Make sure to add tests for it. This is important so I don't break it in a future version unintentionally. +* Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it. + +== Copyright + +Copyright (c) 2011 Tyler McMullen. See LICENSE.txt for +further details. + diff --git a/Rakefile b/Rakefile new file mode 100644 index 0000000..a5a66d2 --- /dev/null +++ b/Rakefile @@ -0,0 +1,36 @@ +require 'rake' +require 'rubygems' + +require 'rspec/core/rake_task' +RSpec::Core::RakeTask.new(:spec) do |t| + t.rspec_opts = ["--color"] + t.fail_on_error = false +end + +task :default => :spec + +require 'jeweler' +Jeweler::Tasks.new do |gem| + gem.name = "bitset" + gem.homepage = "http://github.com/tyler/bitset" + gem.license = "MIT" + gem.summary = 'Bitset implementation.' + 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)' + gem.email = "tbmcmullen@gmail.com" + gem.authors = ["Tyler McMullen"] +end +Jeweler::RubygemsDotOrgTasks.new + +require 'rake/rdoctask' +Rake::RDocTask.new do |rdoc| + version = File.exist?('VERSION') ? File.read('VERSION') : "" + + rdoc.rdoc_dir = 'rdoc' + rdoc.title = "bitset #{version}" + rdoc.rdoc_files.include('README*') + rdoc.rdoc_files.include('ext/**/*.c') +end + +task :build do + ruby 'ext/bitset/extconf.rb' +end diff --git a/ext/bitset/bitset.c b/ext/bitset/bitset.c new file mode 100644 index 0000000..c412c4e --- /dev/null +++ b/ext/bitset/bitset.c @@ -0,0 +1,275 @@ +#include "ruby.h" + +#include +#include +#include + +VALUE cBitset; + +typedef struct { + int len; + uint64_t * data; +} Bitset; + +Bitset * bitset_new() { + return (Bitset *) malloc(sizeof(Bitset)); +} + +void bitset_setup(Bitset * bs, int len) { + bs->len = len; + bs->data = (uint64_t *) calloc((((bs->len-1) >> 6) + 1), sizeof(uint64_t)); // 2^6=64 +} + +void bitset_free(Bitset * bs) { + if(bs->data) + free(bs->data); + free(bs); +} + + +Bitset * get_bitset(VALUE obj) { + Bitset * bs; + Data_Get_Struct(obj, Bitset, bs); + return bs; +} + +static VALUE rb_bitset_alloc(VALUE klass) { + VALUE obj; + obj = Data_Wrap_Struct(klass, 0, bitset_free, bitset_new()); + return obj; +} + +static VALUE rb_bitset_initialize(VALUE self, VALUE len) { + Bitset * bs = get_bitset(self); + bitset_setup(bs, NUM2INT(len)); + return self; +} + +static VALUE rb_bitset_size(VALUE self, VALUE len) { + Bitset * bs = get_bitset(self); + return INT2NUM(bs->len); +} + +void raise_index_error() { + VALUE rb_eIndexError = rb_const_get(rb_cObject, rb_intern("IndexError")); + rb_raise(rb_eIndexError, "Index out of bounds"); +} + +#define _bit_segment(bit) ((bit) >> 6) +#define _bit_mask(bit) (1 << ((bit) & 0x3f)) + +void validate_index(Bitset * bs, int idx) { + if(idx < 0 || idx >= bs->len) + raise_index_error(); +} + +uint64_t get_bit(Bitset * bs, int idx) { + uint64_t segment = bs->data[_bit_segment(idx)]; + return segment & _bit_mask(idx); +} + +void set_bit(Bitset * bs, int idx) { + bs->data[_bit_segment(idx)] |= _bit_mask(idx); +} + +void clear_bit(Bitset * bs, int idx) { + bs->data[_bit_segment(idx)] &= ~_bit_mask(idx); +} + +void assign_bit(Bitset * bs, int idx, VALUE value) { + if(NIL_P(value) || value == Qfalse) + clear_bit(bs, idx); + else + set_bit(bs, idx); +} + +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; +} + +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; +} + +static VALUE rb_bitset_set(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); + set_bit(bs, idx); + } + return Qtrue; +} + +static VALUE rb_bitset_clear(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); + clear_bit(bs, idx); + } + return Qtrue; +} + +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; +} + +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; +} + +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) + segment &= ((1 << (bs->len & 0x3F)) - 1); + count += __builtin_popcountll(segment); + } + return INT2NUM(count); +} + +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); +} + +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); +} + +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); +} + +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); +} + +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); +} + +void Init_bitset() { + cBitset = rb_define_class("Bitset", rb_cObject); + rb_define_alloc_func(cBitset, rb_bitset_alloc); + rb_define_method(cBitset, "initialize", rb_bitset_initialize, 1); + rb_define_method(cBitset, "size", rb_bitset_size, 0); + rb_define_method(cBitset, "[]", rb_bitset_aref, 1); + rb_define_method(cBitset, "[]=", rb_bitset_aset, 2); + rb_define_method(cBitset, "set", rb_bitset_set, -1); + rb_define_method(cBitset, "clear", rb_bitset_clear, -1); + rb_define_method(cBitset, "set?", rb_bitset_set_p, -1); + rb_define_method(cBitset, "clear?", rb_bitset_clear_p, -1); + rb_define_method(cBitset, "cardinality", rb_bitset_cardinality, 0); + rb_define_method(cBitset, "intersect", rb_bitset_intersect, 1); + rb_define_alias(cBitset, "&", "intersect"); + rb_define_method(cBitset, "union", rb_bitset_union, 1); + rb_define_alias(cBitset, "|", "union"); + rb_define_method(cBitset, "difference", rb_bitset_difference, 1); + rb_define_alias(cBitset, "-", "difference"); + rb_define_method(cBitset, "xor", rb_bitset_xor, 1); + rb_define_alias(cBitset, "^", "xor"); + rb_define_alias(cBitset, "symmetric_difference", "xor"); + rb_define_method(cBitset, "hamming", rb_bitset_hamming, 1); +} diff --git a/ext/bitset/extconf.rb b/ext/bitset/extconf.rb new file mode 100644 index 0000000..1f9bb2a --- /dev/null +++ b/ext/bitset/extconf.rb @@ -0,0 +1,2 @@ +require 'mkmf' +create_makefile 'bitset' diff --git a/spec/bitset_spec.rb b/spec/bitset_spec.rb new file mode 100644 index 0000000..2b2b8be --- /dev/null +++ b/spec/bitset_spec.rb @@ -0,0 +1,203 @@ +require './bitset' + +describe Bitset do + it 'can be initialized' do + Bitset.new(64) + end + + describe :size do + it 'returns the correct size' do + Bitset.new(64).size.should == 64 + Bitset.new(73).size.should == 73 + end + end + + describe :[] do + it 'returns True for set bits' do + bs = Bitset.new(8) + bs[0] = true + bs[0].should == true + end + + it 'returns False for unset bits' do + bs = Bitset.new(8) + bs[0].should == false + end + + it 'raises an error when accessing out of bound indexes' do + bs = Bitset.new(8) + expect { bs[8] }.to raise_error(IndexError) + end + end + + describe :[]= do + it 'sets True for truthy values' do + bs = Bitset.new(8) + + bs[0] = true + bs[0].should == true + + bs[1] = 123 + bs[1].should == true + + bs[2] = "woo" + bs[2].should == true + end + + it 'sets False for falsey values' do + bs = Bitset.new(8) + + bs[0] = false + bs[0].should == false + + bs[1] = nil + bs[1].should == false + end + + it 'raises an error when setting out of bound indexes' do + bs = Bitset.new(8) + expect { bs[8] = true }.to raise_error(IndexError) + end + end + + describe :set do + it 'sets True for all given indexes' do + bs = Bitset.new(8) + bs.set 1,2,3 + + bs[1].should == true + bs[2].should == true + bs[3].should == true + end + end + + describe :clear do + it 'sets False for all given indexes' do + bs = Bitset.new(8) + bs.set 1,2,3 + bs.clear 1,3 + + bs[1].should == false + bs[2].should == true + bs[3].should == false + end + end + + describe :set? do + it 'returns True if all bits indexed are set' do + bs = Bitset.new(8) + bs.set 1, 4, 5 + bs.set?(1,4,5).should == true + end + + it 'returns False if not all bits indexed are set' do + bs = Bitset.new(8) + bs.set 1, 4 + bs.set?(1,4,5).should == false + end + end + + describe :clear? do + it 'returns True if all bits indexed are clear' do + bs = Bitset.new(8) + bs.set 1, 4, 5 + bs.clear?(0,2,3,6).should == true + end + + it 'returns False if not all bits indexed are clear' do + bs = Bitset.new(8) + bs.set 1, 4 + bs.clear?(1,2,6).should == false + end + end + + describe :cardinality do + it 'returns the number of bits set' do + bs = Bitset.new(8) + bs.cardinality.should == 0 + + bs[0] = true + bs.cardinality.should == 1 + + bs[1] = true + bs.cardinality.should == 2 + + bs[2] = true + bs.cardinality.should == 3 + end + + it '... even for large numbers of bits' do + bs = Bitset.new(10_000) + bs.set(*(0...5000).to_a) + bs.cardinality.should == 5000 + end + end + + describe :& do + it 'returns a new Bitset which is the intersection of two Bitsets' do + bs1 = Bitset.new(8) + bs1.set 1, 4, 7 + + bs2 = Bitset.new(8) + bs2.set 1, 2, 4, 6 + + bs3 = bs1 & bs2 + bs3.set?(1,4).should == true + bs3.clear?(0,2,3,5,6,7).should == true + end + end + + describe :| do + it 'returns a new Bitset which is the union of two Bitsets' do + bs1 = Bitset.new(8) + bs1.set 1, 4, 7 + + bs2 = Bitset.new(8) + bs2.set 1, 2, 4, 6 + + bs3 = bs1 | bs2 + bs3.set?(1,2,4,6,7).should == true + bs3.clear?(0,3,5).should == true + end + end + + describe :- do + it 'returns a new Bitset which is the difference of two Bitsets' do + bs1 = Bitset.new(8) + bs1.set 1, 4, 7 + + bs2 = Bitset.new(8) + bs2.set 1, 2, 4, 6 + + bs3 = bs1 - bs2 + bs3.set?(7).should == true + bs3.clear?(0,1,2,3,4,5,6).should == true + end + end + + describe :^ do + it 'returns a new Bitset which is the xor of two Bitsets' do + bs1 = Bitset.new(8) + bs1.set 1, 4, 7 + + bs2 = Bitset.new(8) + bs2.set 1, 2, 4, 6 + + bs3 = bs1 ^ bs2 + bs3.set?(2,6,7).should == true + bs3.clear?(0,1,3,4,5).should == true + end + end + + describe :hamming do + it 'returns the hamming distance of two Bitsets' do + bs1 = Bitset.new(8) + bs1.set 1, 4, 7 + + bs2 = Bitset.new(8) + bs2.set 1, 2, 4, 6 + + bs1.hamming(bs2).should == 3 + end + end +end