bitfield 0.9.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ Copyright (c) 2011, Fabian Becker
2
+ All rights reserved.
3
+
4
+ Redistribution and use in source and binary forms, with or without
5
+ modification, are permitted provided that the following conditions are met:
6
+ * Redistributions of source code must retain the above copyright
7
+ notice, this list of conditions and the following disclaimer.
8
+ * Redistributions in binary form must reproduce the above copyright
9
+ notice, this list of conditions and the following disclaimer in the
10
+ documentation and/or other materials provided with the distribution.
11
+
12
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
13
+ ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
14
+ WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
15
+ DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
16
+ DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
17
+ (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
18
+ LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
19
+ ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
20
+ (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
21
+ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
@@ -0,0 +1,15 @@
1
+ = BitField
2
+
3
+ This C extensions wraps boost::dynamic_bitset and makes it available
4
+ as a native Ruby class. The bitset behaves like an an array allowing
5
+ only values of 0 and 1. Using this extension is easy:
6
+
7
+ b = BitField.new 10 # Initializes BitField with size 10
8
+ puts b # Prints '0000000000' where the last bit is b[0]
9
+ b[0..3] = Array.new(4) {1} # Sets bits 0 to 3 to 1
10
+
11
+ Enjoy :)
12
+
13
+
14
+ = Licence
15
+ Copyright(c) 2011 Fabian Becker, released under 3-clause BSD licence.
@@ -0,0 +1,26 @@
1
+ require 'rake/extensiontask'
2
+
3
+ spec = Gem::Specification.new do |s|
4
+ s.name = 'bitfield'
5
+ s.platform = Gem::Platform::RUBY
6
+ s.extensions = FileList["ext/**/extconf.rb"]
7
+ s.version = '0.9.1'
8
+ s.date = "2011-10-25"
9
+ s.author = 'Fabian Becker'
10
+ s.email = 'halfdan@xnorfz.de'
11
+ s.homepage = 'https://github.com/halfdan/ruby-bitfield/'
12
+ s.summary = "Wraps boost::dynamic_bitset and makes it available as a Ruby class
13
+ for fast operations on a bitset"
14
+
15
+ s.description = 'This C extensions wraps boost::dynamic_bitset and makes it available
16
+ as a native Ruby class. The bitset behaves like an an array allowing
17
+ only values of 0 and 1.'
18
+
19
+ s.files = FileList["LICENSE", "README.rdoc", "Rakefile", "ext/**/*.cpp", "ext/**/*.hpp"]
20
+ end
21
+
22
+ # add your default gem packing task
23
+ Gem::PackageTask.new(spec) do |pkg|
24
+ end
25
+
26
+ Rake::ExtensionTask.new('bitfield', spec)
@@ -0,0 +1,266 @@
1
+ /**
2
+ * Copyright (c) 2011, Fabian Becker
3
+ * All rights reserved.
4
+ *
5
+ * Redistribution and use in source and binary forms, with or without
6
+ * modification, are permitted provided that the following conditions are met:
7
+ * * Redistributions of source code must retain the above copyright
8
+ * notice, this list of conditions and the following disclaimer.
9
+ * * Redistributions in binary form must reproduce the above copyright
10
+ * notice, this list of conditions and the following disclaimer in the
11
+ * documentation and/or other materials provided with the distribution.
12
+ *
13
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
14
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
15
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
16
+ * DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
17
+ * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
18
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
19
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
20
+ * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
22
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23
+ *
24
+ */
25
+ #include "ruby.h"
26
+ #include <boost/dynamic_bitset.hpp>
27
+ #include "bitfield.hpp"
28
+
29
+ static VALUE bf_init(VALUE self, VALUE size)
30
+ {
31
+ Check_Type(size, T_FIXNUM);
32
+ BitField *ptr;
33
+ Data_Get_Struct(self, BitField, ptr);
34
+ ptr->data.resize(FIX2INT(size));
35
+ return self;
36
+ }
37
+
38
+ static VALUE bf_new(VALUE self, VALUE size)
39
+ {
40
+ VALUE *argv;
41
+ Check_Type(size, T_FIXNUM);
42
+ BitField *ptr;
43
+ argv[0] = size;
44
+ rb_obj_call_init(self, 1, argv);
45
+ return self;
46
+ }
47
+
48
+ /**
49
+ * Returns the size of the BitField
50
+ *
51
+ */
52
+ static VALUE bf_size(VALUE self)
53
+ {
54
+ BitField *ptr;
55
+ Data_Get_Struct(self, BitField, ptr);
56
+ return INT2NUM(ptr->data.size());
57
+ }
58
+
59
+
60
+ /**
61
+ * Flips the n-th bit if position is given
62
+ * flips all bits otherwise (TODO)
63
+ */
64
+ static VALUE bf_flip(int argc, VALUE *argv, VALUE self)
65
+ {
66
+ BitField *ptr;
67
+ Data_Get_Struct(self, BitField, ptr);
68
+ if(argc == 1) {
69
+ Check_Type(argv[0], T_FIXNUM);
70
+ ptr->data[FIX2INT(argv[0])].flip();
71
+ } else if(argc == 0) {
72
+ ptr->data.flip();
73
+ } else {
74
+ rb_raise(rb_eArgError, "wrong number of arguments(%d for 1 or 0)", argc);
75
+ }
76
+ return Qnil;
77
+ }
78
+
79
+ /**
80
+ * Returns the number of bits that are set
81
+ *
82
+ */
83
+ static VALUE bf_count(VALUE self)
84
+ {
85
+ BitField *ptr;
86
+ Data_Get_Struct(self, BitField, ptr);
87
+ return INT2NUM(ptr->data.count());
88
+ }
89
+
90
+ static VALUE bf_bit_set(VALUE self, VALUE position, VALUE value)
91
+ {
92
+ BitField *ptr;
93
+ Data_Get_Struct(self, BitField, ptr);
94
+
95
+ if(rb_obj_is_kind_of(position, rb_cRange)
96
+ && rb_obj_is_kind_of(value, rb_cArray)) {
97
+ long beg, len;
98
+ VALUE tmp;
99
+ switch(rb_range_beg_len(position, &beg, &len, (long int)ptr->data.size()-1, 2)) {
100
+ case Qfalse:
101
+ case Qnil:
102
+ return Qfalse;
103
+ default:
104
+ for(long i = beg+len-1; i >= beg; i--) {
105
+ tmp = rb_ary_pop(value);
106
+ if(tmp != Qnil) {
107
+ ptr->data[i] = FIX2INT(tmp) % 2;
108
+ } else {
109
+ rb_raise(rb_eRangeError, "Array is smaller than given range.");
110
+ }
111
+ }
112
+ }
113
+ } else {
114
+ /* Sanity checks for position and value */
115
+ Check_Type(position, T_FIXNUM);
116
+ Check_Type(value, T_FIXNUM);
117
+
118
+ int pos = FIX2INT(position);
119
+ if(pos < 0 || pos >= ptr->data.size()) {
120
+ rb_raise(rb_eRangeError, "BitField out of range with value %d.", pos);
121
+ }
122
+ ptr->data[pos] = FIX2INT(value) % 2;
123
+ }
124
+ return Qnil;
125
+ }
126
+
127
+ static VALUE bf_bit_get(VALUE self, VALUE position)
128
+ {
129
+ BitField *ptr;
130
+ Data_Get_Struct(self, BitField, ptr);
131
+
132
+ /* Is position a range? */
133
+ if(rb_obj_is_kind_of(position, rb_cRange)) {
134
+ long beg, len;
135
+ VALUE range_ary = rb_ary_new();
136
+ switch(rb_range_beg_len(position, &beg, &len, (long int)ptr->data.size()-1, 2)) {
137
+ case Qfalse:
138
+ case Qnil:
139
+ return Qnil;
140
+ default:
141
+ for(long i = beg; i < beg+len; i++) {
142
+ rb_ary_push(range_ary, INT2NUM(ptr->data[i]));
143
+ }
144
+ }
145
+ return range_ary;
146
+ } else {
147
+ /* Sanity checks for position and value */
148
+ Check_Type(position, T_FIXNUM);
149
+ int pos = FIX2INT(position);
150
+ return INT2NUM(ptr->data[pos]);
151
+ }
152
+ }
153
+
154
+ static VALUE bf_to_s(VALUE self)
155
+ {
156
+ BitField *ptr;
157
+ Data_Get_Struct(self, BitField, ptr);
158
+ std::string buffer;
159
+ to_string(ptr->data, buffer);
160
+ return rb_str_new2(buffer.c_str());
161
+ }
162
+
163
+ static VALUE bf_initialize_clone(VALUE self, VALUE orig)
164
+ {
165
+ BitField *ptr, *ptr2;
166
+ Data_Get_Struct(self, BitField, ptr);
167
+ Data_Get_Struct(orig, BitField, ptr2);
168
+ ptr->data.resize(ptr2->data.size());
169
+ ptr->data = ptr2->data;
170
+ return self;
171
+ }
172
+
173
+ /*
174
+ *Correctly free the struct
175
+ */
176
+ void bf_free(BitField *ptr)
177
+ {
178
+ delete ptr;
179
+ }
180
+
181
+ static VALUE BitField_allocate(VALUE klass)
182
+ {
183
+ BitField *bf;
184
+ bf = ALLOC(BitField);
185
+ new(bf) BitField();
186
+ VALUE tdata = Data_Wrap_Struct(klass, 0, bf_free, bf);
187
+ return tdata;
188
+ }
189
+
190
+ VALUE rb_cBitField;
191
+
192
+ extern "C" {
193
+ void Init_bitfield() {
194
+ rb_cBitField = rb_define_class("BitField", rb_cObject);
195
+
196
+ rb_define_alloc_func(rb_cBitField, BitField_allocate);
197
+
198
+ /* We do the same for .clone and .dup */
199
+ rb_define_method(
200
+ rb_cBitField,
201
+ "initialize_clone",
202
+ reinterpret_cast<VALUE(*)(...)>(bf_initialize_clone),
203
+ 1
204
+ );
205
+ rb_define_method(
206
+ rb_cBitField,
207
+ "initialize_dup",
208
+ reinterpret_cast<VALUE(*)(...)>(bf_initialize_clone),
209
+ 1
210
+ );
211
+
212
+ /* Returns the size of the bitset */
213
+ rb_define_method(
214
+ rb_cBitField,
215
+ "size",
216
+ reinterpret_cast<VALUE(*)(...)>(bf_size),
217
+ 0
218
+ );
219
+ rb_define_method(
220
+ rb_cBitField,
221
+ "initialize",
222
+ reinterpret_cast<VALUE(*)(...)>(bf_init),
223
+ 1
224
+ );
225
+ rb_define_method(
226
+ rb_cBitField,
227
+ "[]=",
228
+ reinterpret_cast<VALUE(*)(...)>(bf_bit_set),
229
+ 2
230
+ );
231
+ rb_define_method(
232
+ rb_cBitField,
233
+ "[]",
234
+ reinterpret_cast<VALUE(*)(...)>(bf_bit_get),
235
+ 1
236
+ );
237
+ rb_define_method(
238
+ rb_cBitField,
239
+ "to_s",
240
+ reinterpret_cast<VALUE(*)(...)>(bf_to_s),
241
+ 0
242
+ );
243
+ rb_define_method(
244
+ rb_cBitField,
245
+ "flip",
246
+ reinterpret_cast<VALUE(*)(...)>(bf_flip),
247
+ -1
248
+ );
249
+ rb_define_method(
250
+ rb_cBitField,
251
+ "count",
252
+ reinterpret_cast<VALUE(*)(...)>(bf_count),
253
+ 0
254
+ );
255
+ }
256
+ }
257
+
258
+ /* Library Code */
259
+ BitField *BitFieldNew(int size)
260
+ {
261
+ BitField *bf;
262
+ bf = ALLOC(BitField);
263
+ new(bf) BitField();
264
+ bf->data.resize(size);
265
+ return bf;
266
+ }
@@ -0,0 +1,36 @@
1
+ /**
2
+ * Copyright (c) 2011, Fabian Becker
3
+ * All rights reserved.
4
+ *
5
+ * Redistribution and use in source and binary forms, with or without
6
+ * modification, are permitted provided that the following conditions are met:
7
+ * * Redistributions of source code must retain the above copyright
8
+ * notice, this list of conditions and the following disclaimer.
9
+ * * Redistributions in binary form must reproduce the above copyright
10
+ * notice, this list of conditions and the following disclaimer in the
11
+ * documentation and/or other materials provided with the distribution.
12
+ *
13
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
14
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
15
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
16
+ * DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
17
+ * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
18
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
19
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
20
+ * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
22
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23
+ *
24
+ */
25
+ #ifndef __BITFIELD_H__
26
+ #define __BITFIELD_H__
27
+
28
+ RUBY_EXTERN VALUE rb_cBitField;
29
+
30
+ typedef struct _bitfield {
31
+ boost::dynamic_bitset<> data;
32
+ } BitField;
33
+
34
+ BitField *BitFieldNew(int size);
35
+
36
+ #endif /* __BITFIELD_H__ */
@@ -0,0 +1,14 @@
1
+ # Loads mkmf which is used to make makefiles for Ruby extensions
2
+ require 'mkmf'
3
+
4
+ # Set the name of the extension
5
+ extension_name = 'bitfield'
6
+
7
+ # The destination
8
+ dir_config(extension_name)
9
+
10
+ # Require used libraries
11
+ have_library("stdc++")
12
+
13
+ # Do the work
14
+ create_makefile(extension_name)
metadata ADDED
@@ -0,0 +1,56 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: bitfield
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.9.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Fabian Becker
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2011-10-25 00:00:00.000000000 +02:00
13
+ default_executable:
14
+ dependencies: []
15
+ description: ! "This C extensions wraps boost::dynamic_bitset and makes it available\n
16
+ \ as a native Ruby class. The bitset behaves like an an array
17
+ allowing\n only values of 0 and 1."
18
+ email: halfdan@xnorfz.de
19
+ executables: []
20
+ extensions:
21
+ - ext/bitfield/extconf.rb
22
+ extra_rdoc_files: []
23
+ files:
24
+ - LICENSE
25
+ - README.rdoc
26
+ - Rakefile
27
+ - ext/bitfield/bitfield.cpp
28
+ - ext/bitfield/bitfield.hpp
29
+ - ext/bitfield/extconf.rb
30
+ has_rdoc: true
31
+ homepage: https://github.com/halfdan/ruby-bitfield/
32
+ licenses: []
33
+ post_install_message:
34
+ rdoc_options: []
35
+ require_paths:
36
+ - lib
37
+ required_ruby_version: !ruby/object:Gem::Requirement
38
+ none: false
39
+ requirements:
40
+ - - ! '>='
41
+ - !ruby/object:Gem::Version
42
+ version: '0'
43
+ required_rubygems_version: !ruby/object:Gem::Requirement
44
+ none: false
45
+ requirements:
46
+ - - ! '>='
47
+ - !ruby/object:Gem::Version
48
+ version: '0'
49
+ requirements: []
50
+ rubyforge_project:
51
+ rubygems_version: 1.6.2
52
+ signing_key:
53
+ specification_version: 3
54
+ summary: Wraps boost::dynamic_bitset and makes it available as a Ruby class for fast
55
+ operations on a bitset
56
+ test_files: []