bitfield 0.9.2 → 0.9.3
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/README.rdoc +1 -1
- data/Rakefile +21 -13
- data/ext/bitfield/bitfield.cpp +71 -18
- data/spec/bitfield_spec.rb +124 -0
- data/spec/rspec.opts +1 -0
- data/spec/spec_helper.rb +5 -0
- metadata +27 -7
data/README.rdoc
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
= BitField
|
2
2
|
|
3
|
-
|
3
|
+
This C extension wraps boost::dynamic_bitset and makes it available
|
4
4
|
as a native Ruby class. The bitset behaves like an an array allowing
|
5
5
|
only values of 0 and 1. Using this extension is easy:
|
6
6
|
|
data/Rakefile
CHANGED
@@ -1,22 +1,30 @@
|
|
1
1
|
require 'rake/extensiontask'
|
2
2
|
|
3
|
-
spec = Gem::Specification.new do |
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
3
|
+
spec = Gem::Specification.new do |spec|
|
4
|
+
spec.name = 'bitfield'
|
5
|
+
spec.platform = Gem::Platform::RUBY
|
6
|
+
spec.extensions = FileList["ext/**/extconf.rb"]
|
7
|
+
spec.version = '0.9.3'
|
8
|
+
spec.date = Time.now.strftime "%Y-%m-%d"
|
9
|
+
spec.author = 'Fabian Becker'
|
10
|
+
spec.email = 'halfdan@xnorfz.de'
|
11
|
+
spec.homepage = 'https://github.com/halfdan/ruby-bitfield/'
|
12
|
+
spec.summary = "Wraps boost::dynamic_bitset and makes it available as a Ruby class
|
13
13
|
for fast operations on a bitset"
|
14
14
|
|
15
|
-
|
15
|
+
spec.description = 'This C extension wraps boost::dynamic_bitset and makes it available
|
16
16
|
as a native Ruby class. The bitset behaves like an an array allowing
|
17
17
|
only values of 0 and 1.'
|
18
|
-
|
19
|
-
|
18
|
+
|
19
|
+
spec.required_ruby_version = '>= 1.8.1'
|
20
|
+
|
21
|
+
spec.files = FileList["LICENSE", "README.rdoc", "Rakefile", "ext/**/*.cpp", "ext/**/*.hpp", "spec/*"]
|
22
|
+
|
23
|
+
spec.add_development_dependency 'rspec'
|
24
|
+
spec.test_files = ["spec/bitfield_spec.rb"]
|
25
|
+
|
26
|
+
spec.extra_rdoc_files = %w( README.rdoc ext/bitfield/bitfield.cpp )
|
27
|
+
spec.rdoc_options.concat ['--main','README.rdoc', '-E cpp=c']
|
20
28
|
end
|
21
29
|
|
22
30
|
# add your default gem packing task
|
data/ext/bitfield/bitfield.cpp
CHANGED
@@ -35,6 +35,13 @@ static VALUE bf_init(VALUE self, VALUE size)
|
|
35
35
|
return self;
|
36
36
|
}
|
37
37
|
|
38
|
+
/*
|
39
|
+
* Document-method: new
|
40
|
+
*
|
41
|
+
* call-seq: new(size)
|
42
|
+
*
|
43
|
+
* Creates a new BitField with the _size_ that was given.
|
44
|
+
*/
|
38
45
|
static VALUE bf_new(VALUE self, VALUE size)
|
39
46
|
{
|
40
47
|
VALUE *argv;
|
@@ -45,9 +52,12 @@ static VALUE bf_new(VALUE self, VALUE size)
|
|
45
52
|
return self;
|
46
53
|
}
|
47
54
|
|
48
|
-
|
49
|
-
*
|
55
|
+
/*
|
56
|
+
* Document-method: size
|
50
57
|
*
|
58
|
+
* call-seq: size()
|
59
|
+
*
|
60
|
+
* Returns the size of the BitField
|
51
61
|
*/
|
52
62
|
static VALUE bf_size(VALUE self)
|
53
63
|
{
|
@@ -56,10 +66,13 @@ static VALUE bf_size(VALUE self)
|
|
56
66
|
return INT2NUM(ptr->data.size());
|
57
67
|
}
|
58
68
|
|
59
|
-
|
60
|
-
|
61
|
-
*
|
62
|
-
*
|
69
|
+
/*
|
70
|
+
* Document-method: flip
|
71
|
+
*
|
72
|
+
* call-seq: flip(n=nil) -> nil
|
73
|
+
*
|
74
|
+
* Flips all bits in the BitField if _n_ is nil
|
75
|
+
* Flips n if n <> nil
|
63
76
|
*/
|
64
77
|
static VALUE bf_flip(int argc, VALUE *argv, VALUE self)
|
65
78
|
{
|
@@ -68,7 +81,12 @@ static VALUE bf_flip(int argc, VALUE *argv, VALUE self)
|
|
68
81
|
if(argc == 1)
|
69
82
|
{
|
70
83
|
Check_Type(argv[0], T_FIXNUM);
|
71
|
-
|
84
|
+
long pos = FIX2INT(argv[0]);
|
85
|
+
if(pos < 0 || pos >= ptr->data.size()) {
|
86
|
+
return Qfalse;
|
87
|
+
} else {
|
88
|
+
ptr->data[pos].flip();
|
89
|
+
}
|
72
90
|
}
|
73
91
|
else if(argc == 0)
|
74
92
|
{
|
@@ -81,9 +99,12 @@ static VALUE bf_flip(int argc, VALUE *argv, VALUE self)
|
|
81
99
|
return Qnil;
|
82
100
|
}
|
83
101
|
|
84
|
-
|
85
|
-
*
|
102
|
+
/*
|
103
|
+
* Document-method: count
|
86
104
|
*
|
105
|
+
* call-seq: count()
|
106
|
+
*
|
107
|
+
* Returns the number of bits that are set
|
87
108
|
*/
|
88
109
|
static VALUE bf_count(VALUE self)
|
89
110
|
{
|
@@ -92,6 +113,18 @@ static VALUE bf_count(VALUE self)
|
|
92
113
|
return INT2NUM(ptr->data.count());
|
93
114
|
}
|
94
115
|
|
116
|
+
/*
|
117
|
+
* Document-method: []=
|
118
|
+
*
|
119
|
+
* call-seq:
|
120
|
+
* [index]= value
|
121
|
+
* [range]= value-array
|
122
|
+
*
|
123
|
+
* Element Assignment - Sets the element at _index_ or replaces the subset
|
124
|
+
* specified by _range_. Values can be only be positive numerics. Even
|
125
|
+
* values will be set to 0 (n % 2 = 0) and uneven values will be set to 1
|
126
|
+
* (n % 2 = 1).
|
127
|
+
*/
|
95
128
|
static VALUE bf_bit_set(VALUE self, VALUE position, VALUE value)
|
96
129
|
{
|
97
130
|
BitField *ptr;
|
@@ -140,6 +173,17 @@ static VALUE bf_bit_set(VALUE self, VALUE position, VALUE value)
|
|
140
173
|
return Qnil;
|
141
174
|
}
|
142
175
|
|
176
|
+
/*
|
177
|
+
* Document-method: []
|
178
|
+
*
|
179
|
+
* call-seq:
|
180
|
+
* [index] -> value
|
181
|
+
* [range] -> new_ary
|
182
|
+
*
|
183
|
+
* Element Reference - Returns the element at _index_ or the subset
|
184
|
+
* specified by _range_. Negative or indices greater than the size of
|
185
|
+
* the BitField will be nil.
|
186
|
+
*/
|
143
187
|
static VALUE bf_bit_get(VALUE self, VALUE position)
|
144
188
|
{
|
145
189
|
BitField *ptr;
|
@@ -180,6 +224,15 @@ static VALUE bf_bit_get(VALUE self, VALUE position)
|
|
180
224
|
}
|
181
225
|
}
|
182
226
|
|
227
|
+
/*
|
228
|
+
* Document-method: to_s
|
229
|
+
*
|
230
|
+
* call-seq:
|
231
|
+
* inspect -> string
|
232
|
+
* to_s -> string
|
233
|
+
*
|
234
|
+
* Creates a string representation of +self+.
|
235
|
+
*/
|
183
236
|
static VALUE bf_to_s(VALUE self)
|
184
237
|
{
|
185
238
|
BitField *ptr;
|
@@ -229,13 +282,13 @@ extern "C" {
|
|
229
282
|
rb_define_method(
|
230
283
|
rb_cBitField,
|
231
284
|
"initialize_clone",
|
232
|
-
|
285
|
+
RUBY_METHOD_FUNC(bf_initialize_clone),
|
233
286
|
1
|
234
287
|
);
|
235
288
|
rb_define_method(
|
236
289
|
rb_cBitField,
|
237
290
|
"initialize_dup",
|
238
|
-
|
291
|
+
RUBY_METHOD_FUNC(bf_initialize_clone),
|
239
292
|
1
|
240
293
|
);
|
241
294
|
|
@@ -243,43 +296,43 @@ extern "C" {
|
|
243
296
|
rb_define_method(
|
244
297
|
rb_cBitField,
|
245
298
|
"size",
|
246
|
-
|
299
|
+
RUBY_METHOD_FUNC(bf_size),
|
247
300
|
0
|
248
301
|
);
|
249
302
|
rb_define_method(
|
250
303
|
rb_cBitField,
|
251
304
|
"initialize",
|
252
|
-
|
305
|
+
RUBY_METHOD_FUNC(bf_init),
|
253
306
|
1
|
254
307
|
);
|
255
308
|
rb_define_method(
|
256
309
|
rb_cBitField,
|
257
310
|
"[]=",
|
258
|
-
|
311
|
+
RUBY_METHOD_FUNC(bf_bit_set),
|
259
312
|
2
|
260
313
|
);
|
261
314
|
rb_define_method(
|
262
315
|
rb_cBitField,
|
263
316
|
"[]",
|
264
|
-
reinterpret_cast<VALUE(*)(...)>(bf_bit_get),
|
317
|
+
reinterpret_cast<VALUE(*)(...)>(RUBY_METHOD_FUNC(bf_bit_get)),
|
265
318
|
1
|
266
319
|
);
|
267
320
|
rb_define_method(
|
268
321
|
rb_cBitField,
|
269
322
|
"to_s",
|
270
|
-
|
323
|
+
RUBY_METHOD_FUNC(bf_to_s),
|
271
324
|
0
|
272
325
|
);
|
273
326
|
rb_define_method(
|
274
327
|
rb_cBitField,
|
275
328
|
"flip",
|
276
|
-
|
329
|
+
RUBY_METHOD_FUNC(bf_flip),
|
277
330
|
-1
|
278
331
|
);
|
279
332
|
rb_define_method(
|
280
333
|
rb_cBitField,
|
281
334
|
"count",
|
282
|
-
|
335
|
+
RUBY_METHOD_FUNC(bf_count),
|
283
336
|
0
|
284
337
|
);
|
285
338
|
}
|
@@ -0,0 +1,124 @@
|
|
1
|
+
require_relative 'spec_helper'
|
2
|
+
|
3
|
+
describe BitField do
|
4
|
+
it "can be initialized with a size" do
|
5
|
+
obj = BitField.new 10
|
6
|
+
obj.should be_a(BitField)
|
7
|
+
end
|
8
|
+
|
9
|
+
it "has a to_s method" do
|
10
|
+
BitField.new(10).should respond_to(:to_s)
|
11
|
+
end
|
12
|
+
|
13
|
+
it "has a clone method" do
|
14
|
+
BitField.new(10).should respond_to(:clone)
|
15
|
+
end
|
16
|
+
|
17
|
+
it "has a size method" do
|
18
|
+
BitField.new(10).should respond_to(:size)
|
19
|
+
end
|
20
|
+
|
21
|
+
it "has a flip method" do
|
22
|
+
BitField.new(10).should respond_to(:flip)
|
23
|
+
end
|
24
|
+
|
25
|
+
it "has a count method" do
|
26
|
+
BitField.new(10).should respond_to(:count)
|
27
|
+
end
|
28
|
+
|
29
|
+
it "has a [] method" do
|
30
|
+
BitField.new(10).should respond_to(:[])
|
31
|
+
end
|
32
|
+
|
33
|
+
it "has a []= method" do
|
34
|
+
BitField.new(10).should respond_to(:[]=)
|
35
|
+
end
|
36
|
+
|
37
|
+
it "has an initialize_clone method" do
|
38
|
+
BitField.new(10).should respond_to(:initialize_clone)
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
describe BitField, "#[]" do
|
43
|
+
it "returns an array for a range" do
|
44
|
+
b = BitField.new(10)
|
45
|
+
b[0..4].should == [0,0,0,0,0]
|
46
|
+
end
|
47
|
+
|
48
|
+
it "return a value for a position" do
|
49
|
+
b = BitField.new(10)
|
50
|
+
b[0].should == 0
|
51
|
+
end
|
52
|
+
|
53
|
+
it "returns nil if the position is out of bounds" do
|
54
|
+
b = BitField.new(10)
|
55
|
+
b[-1].should be_nil
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
describe BitField, "#[]=" do
|
60
|
+
it "sets a value n for position x" do
|
61
|
+
b = BitField.new(10)
|
62
|
+
b[5] = 1
|
63
|
+
end
|
64
|
+
|
65
|
+
it "assigns even values as 0" do
|
66
|
+
b = BitField.new(10)
|
67
|
+
b[5] = 2**10
|
68
|
+
b[5].should == 0
|
69
|
+
end
|
70
|
+
|
71
|
+
it "assigns uneven values as 1" do
|
72
|
+
b = BitField.new(10)
|
73
|
+
b[9] = 2**10+1
|
74
|
+
b[9].should == 1
|
75
|
+
end
|
76
|
+
|
77
|
+
it "sets a range of values if an array is assigned" do
|
78
|
+
b = BitField.new(10)
|
79
|
+
b[0..4] = Array.new(5) { 1 }
|
80
|
+
b[0..4].should == Array.new(5) { 1 }
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
describe BitField, "#size" do
|
85
|
+
it "returns the size of the bitfield" do
|
86
|
+
size = 10
|
87
|
+
obj = BitField.new size
|
88
|
+
obj.size.should == size
|
89
|
+
end
|
90
|
+
end
|
91
|
+
|
92
|
+
describe BitField, "#to_s" do
|
93
|
+
it "returns the bitfield as string" do
|
94
|
+
b = BitField.new 10
|
95
|
+
b[0] = 1
|
96
|
+
b[2] = 1
|
97
|
+
b[9] = 1
|
98
|
+
b.to_s.should eq("1000000101")
|
99
|
+
end
|
100
|
+
end
|
101
|
+
|
102
|
+
describe BitField, "#flip" do
|
103
|
+
it "flips all bits in the bitfield" do
|
104
|
+
obj = BitField.new 10
|
105
|
+
obj.flip
|
106
|
+
obj.to_s.should eq("1111111111")
|
107
|
+
end
|
108
|
+
|
109
|
+
it "flips bit n in the bitfield" do
|
110
|
+
obj = BitField.new 10
|
111
|
+
obj.flip 5
|
112
|
+
obj[5].should == 1
|
113
|
+
end
|
114
|
+
end
|
115
|
+
|
116
|
+
describe BitField, "#count" do
|
117
|
+
it "counts all 1s in the bitfield" do
|
118
|
+
obj = BitField.new 10
|
119
|
+
5.times do |i|
|
120
|
+
obj[i] = 1
|
121
|
+
end
|
122
|
+
obj.count.should == 5
|
123
|
+
end
|
124
|
+
end
|
data/spec/rspec.opts
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--color
|
data/spec/spec_helper.rb
ADDED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: bitfield
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.9.
|
4
|
+
version: 0.9.3
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,8 +9,19 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2011-10-
|
13
|
-
dependencies:
|
12
|
+
date: 2011-10-29 00:00:00.000000000Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rspec
|
16
|
+
requirement: &83808550 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *83808550
|
14
25
|
description: ! "This C extension wraps boost::dynamic_bitset and makes it available\n
|
15
26
|
\ as a native Ruby class. The bitset behaves like an an array
|
16
27
|
allowing\n only values of 0 and 1."
|
@@ -18,18 +29,26 @@ email: halfdan@xnorfz.de
|
|
18
29
|
executables: []
|
19
30
|
extensions:
|
20
31
|
- ext/bitfield/extconf.rb
|
21
|
-
extra_rdoc_files:
|
32
|
+
extra_rdoc_files:
|
33
|
+
- README.rdoc
|
34
|
+
- ext/bitfield/bitfield.cpp
|
22
35
|
files:
|
23
36
|
- LICENSE
|
24
37
|
- README.rdoc
|
25
38
|
- Rakefile
|
26
39
|
- ext/bitfield/bitfield.cpp
|
27
40
|
- ext/bitfield/bitfield.hpp
|
41
|
+
- spec/rspec.opts
|
42
|
+
- spec/bitfield_spec.rb
|
43
|
+
- spec/spec_helper.rb
|
28
44
|
- ext/bitfield/extconf.rb
|
29
45
|
homepage: https://github.com/halfdan/ruby-bitfield/
|
30
46
|
licenses: []
|
31
47
|
post_install_message:
|
32
|
-
rdoc_options:
|
48
|
+
rdoc_options:
|
49
|
+
- --main
|
50
|
+
- README.rdoc
|
51
|
+
- -E cpp=c
|
33
52
|
require_paths:
|
34
53
|
- lib
|
35
54
|
required_ruby_version: !ruby/object:Gem::Requirement
|
@@ -37,7 +56,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
37
56
|
requirements:
|
38
57
|
- - ! '>='
|
39
58
|
- !ruby/object:Gem::Version
|
40
|
-
version:
|
59
|
+
version: 1.8.1
|
41
60
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
42
61
|
none: false
|
43
62
|
requirements:
|
@@ -51,4 +70,5 @@ signing_key:
|
|
51
70
|
specification_version: 3
|
52
71
|
summary: Wraps boost::dynamic_bitset and makes it available as a Ruby class for fast
|
53
72
|
operations on a bitset
|
54
|
-
test_files:
|
73
|
+
test_files:
|
74
|
+
- spec/bitfield_spec.rb
|