bitfield 0.9.3 → 0.9.4
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/Rakefile +1 -1
- data/ext/bitfield/bitfield.cpp +61 -3
- data/spec/bitfield_spec.rb +26 -0
- metadata +4 -4
data/Rakefile
CHANGED
@@ -4,7 +4,7 @@ spec = Gem::Specification.new do |spec|
|
|
4
4
|
spec.name = 'bitfield'
|
5
5
|
spec.platform = Gem::Platform::RUBY
|
6
6
|
spec.extensions = FileList["ext/**/extconf.rb"]
|
7
|
-
spec.version = '0.9.
|
7
|
+
spec.version = '0.9.4'
|
8
8
|
spec.date = Time.now.strftime "%Y-%m-%d"
|
9
9
|
spec.author = 'Fabian Becker'
|
10
10
|
spec.email = 'halfdan@xnorfz.de'
|
data/ext/bitfield/bitfield.cpp
CHANGED
@@ -205,7 +205,7 @@ static VALUE bf_bit_get(VALUE self, VALUE position)
|
|
205
205
|
if(i < 0 || i >= ptr->data.size()) {
|
206
206
|
rb_ary_push(range_ary, Qnil);
|
207
207
|
} else {
|
208
|
-
rb_ary_push(range_ary,
|
208
|
+
rb_ary_push(range_ary, LONG2NUM(ptr->data[i]));
|
209
209
|
}
|
210
210
|
}
|
211
211
|
}
|
@@ -219,7 +219,7 @@ static VALUE bf_bit_get(VALUE self, VALUE position)
|
|
219
219
|
if(pos < 0 || pos >= ptr->data.size()) {
|
220
220
|
return Qnil;
|
221
221
|
} else {
|
222
|
-
return
|
222
|
+
return LONG2NUM(ptr->data[pos]);
|
223
223
|
}
|
224
224
|
}
|
225
225
|
}
|
@@ -242,6 +242,52 @@ static VALUE bf_to_s(VALUE self)
|
|
242
242
|
return rb_str_new2(buffer.c_str());
|
243
243
|
}
|
244
244
|
|
245
|
+
|
246
|
+
/*
|
247
|
+
* call-seq:
|
248
|
+
* each {|item| block } -> ary
|
249
|
+
* each -> an_enumerator
|
250
|
+
*
|
251
|
+
* Calls block once for each element in +self+, passing that
|
252
|
+
* element as a parameter. If no block is given, an
|
253
|
+
* enumerator is returned instead.
|
254
|
+
*/
|
255
|
+
static VALUE bf_each(VALUE self)
|
256
|
+
{
|
257
|
+
long i, size;
|
258
|
+
BitField *ptr;
|
259
|
+
Data_Get_Struct(self, BitField, ptr);
|
260
|
+
size = ptr->data.size();
|
261
|
+
|
262
|
+
RETURN_ENUMERATOR(self, 0, 0);
|
263
|
+
for (i=0; i<size; i++) {
|
264
|
+
rb_yield(bf_bit_get(self, LONG2NUM(i)));
|
265
|
+
}
|
266
|
+
return self;
|
267
|
+
}
|
268
|
+
|
269
|
+
/*
|
270
|
+
* call-seq:
|
271
|
+
* each_index {|item| block } -> ary
|
272
|
+
* each_index -> an_enumerator
|
273
|
+
*
|
274
|
+
* Same as BitField#each, but passes the index of the element
|
275
|
+
* instead of the element itself.
|
276
|
+
*/
|
277
|
+
static VALUE bf_each_index(VALUE self)
|
278
|
+
{
|
279
|
+
long i, size;
|
280
|
+
BitField *ptr;
|
281
|
+
Data_Get_Struct(self, BitField, ptr);
|
282
|
+
size = ptr->data.size();
|
283
|
+
|
284
|
+
RETURN_ENUMERATOR(self, 0, 0);
|
285
|
+
for (i=0; i<size; i++) {
|
286
|
+
rb_yield(LONG2NUM(i));
|
287
|
+
}
|
288
|
+
return self;
|
289
|
+
}
|
290
|
+
|
245
291
|
static VALUE bf_initialize_clone(VALUE self, VALUE orig)
|
246
292
|
{
|
247
293
|
BitField *ptr, *ptr2;
|
@@ -275,8 +321,8 @@ extern "C" {
|
|
275
321
|
void Init_bitfield()
|
276
322
|
{
|
277
323
|
rb_cBitField = rb_define_class("BitField", rb_cObject);
|
278
|
-
|
279
324
|
rb_define_alloc_func(rb_cBitField, BitField_allocate);
|
325
|
+
rb_include_module(rb_cBitField, rb_mEnumerable);
|
280
326
|
|
281
327
|
/* We do the same for .clone and .dup */
|
282
328
|
rb_define_method(
|
@@ -335,6 +381,18 @@ extern "C" {
|
|
335
381
|
RUBY_METHOD_FUNC(bf_count),
|
336
382
|
0
|
337
383
|
);
|
384
|
+
rb_define_method(
|
385
|
+
rb_cBitField,
|
386
|
+
"each",
|
387
|
+
RUBY_METHOD_FUNC(bf_each),
|
388
|
+
0
|
389
|
+
);
|
390
|
+
rb_define_method(
|
391
|
+
rb_cBitField,
|
392
|
+
"each_index",
|
393
|
+
RUBY_METHOD_FUNC(bf_each_index),
|
394
|
+
0
|
395
|
+
);
|
338
396
|
}
|
339
397
|
}
|
340
398
|
|
data/spec/bitfield_spec.rb
CHANGED
@@ -26,6 +26,14 @@ describe BitField do
|
|
26
26
|
BitField.new(10).should respond_to(:count)
|
27
27
|
end
|
28
28
|
|
29
|
+
it "has an each method" do
|
30
|
+
BitField.new(10).should respond_to(:each)
|
31
|
+
end
|
32
|
+
|
33
|
+
it "has an each_index method" do
|
34
|
+
BitField.new(10).should respond_to(:each_index)
|
35
|
+
end
|
36
|
+
|
29
37
|
it "has a [] method" do
|
30
38
|
BitField.new(10).should respond_to(:[])
|
31
39
|
end
|
@@ -37,6 +45,10 @@ describe BitField do
|
|
37
45
|
it "has an initialize_clone method" do
|
38
46
|
BitField.new(10).should respond_to(:initialize_clone)
|
39
47
|
end
|
48
|
+
|
49
|
+
it "is an Enumerable" do
|
50
|
+
BitField.include? Enumerable
|
51
|
+
end
|
40
52
|
end
|
41
53
|
|
42
54
|
describe BitField, "#[]" do
|
@@ -122,3 +134,17 @@ describe BitField, "#count" do
|
|
122
134
|
obj.count.should == 5
|
123
135
|
end
|
124
136
|
end
|
137
|
+
|
138
|
+
describe BitField, "#each" do
|
139
|
+
it "returns an enumerator if no block is given" do
|
140
|
+
obj = BitField.new 10
|
141
|
+
obj.each.is_a? Enumerator
|
142
|
+
end
|
143
|
+
end
|
144
|
+
|
145
|
+
describe BitField, "#each_index" do
|
146
|
+
it "returns an enumerator if no block is given" do
|
147
|
+
obj = BitField.new 10
|
148
|
+
obj.each_index.is_a? Enumerator
|
149
|
+
end
|
150
|
+
end
|
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.4
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,11 +9,11 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2011-10-
|
12
|
+
date: 2011-10-30 00:00:00.000000000Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rspec
|
16
|
-
requirement: &
|
16
|
+
requirement: &81756290 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ! '>='
|
@@ -21,7 +21,7 @@ dependencies:
|
|
21
21
|
version: '0'
|
22
22
|
type: :development
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *81756290
|
25
25
|
description: ! "This C extension wraps boost::dynamic_bitset and makes it available\n
|
26
26
|
\ as a native Ruby class. The bitset behaves like an an array
|
27
27
|
allowing\n only values of 0 and 1."
|