Skip to content

Commit

Permalink
Initial.
Browse files Browse the repository at this point in the history
  • Loading branch information
Tyler McMullen committed Feb 17, 2011
0 parents commit edd34d3
Show file tree
Hide file tree
Showing 7 changed files with 597 additions and 0 deletions.
42 changes: 42 additions & 0 deletions .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
20 changes: 20 additions & 0 deletions 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.
19 changes: 19 additions & 0 deletions 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.

36 changes: 36 additions & 0 deletions 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
275 changes: 275 additions & 0 deletions ext/bitset/bitset.c
@@ -0,0 +1,275 @@
#include "ruby.h"

#include <stdint.h>
#include <string.h>
#include <stdio.h>

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);
}
2 changes: 2 additions & 0 deletions ext/bitset/extconf.rb
@@ -0,0 +1,2 @@
require 'mkmf'
create_makefile 'bitset'

0 comments on commit edd34d3

Please sign in to comment.