bitfield 0.9.1 → 0.9.2
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +4 -4
- data/Rakefile +3 -3
- data/ext/bitfield/bitfield.cpp +161 -131
- metadata +4 -6
data/README.rdoc
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
= BitField
|
2
2
|
|
3
|
-
This C
|
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
|
|
@@ -8,8 +8,8 @@ only values of 0 and 1. Using this extension is easy:
|
|
8
8
|
puts b # Prints '0000000000' where the last bit is b[0]
|
9
9
|
b[0..3] = Array.new(4) {1} # Sets bits 0 to 3 to 1
|
10
10
|
|
11
|
-
|
12
|
-
|
11
|
+
Note that in contrast to the Ruby Array behaviour the BitField does not
|
12
|
+
automatically grow in size.
|
13
13
|
|
14
14
|
= Licence
|
15
|
-
Copyright(c) 2011 Fabian Becker, released under
|
15
|
+
Copyright(c) 2011 Fabian Becker, released under 2-clause BSD licence.
|
data/Rakefile
CHANGED
@@ -4,15 +4,15 @@ spec = Gem::Specification.new do |s|
|
|
4
4
|
s.name = 'bitfield'
|
5
5
|
s.platform = Gem::Platform::RUBY
|
6
6
|
s.extensions = FileList["ext/**/extconf.rb"]
|
7
|
-
s.version = '0.9.
|
8
|
-
s.date = "2011-10-
|
7
|
+
s.version = '0.9.2'
|
8
|
+
s.date = "2011-10-26"
|
9
9
|
s.author = 'Fabian Becker'
|
10
10
|
s.email = 'halfdan@xnorfz.de'
|
11
11
|
s.homepage = 'https://github.com/halfdan/ruby-bitfield/'
|
12
12
|
s.summary = "Wraps boost::dynamic_bitset and makes it available as a Ruby class
|
13
13
|
for fast operations on a bitset"
|
14
14
|
|
15
|
-
s.description = 'This C
|
15
|
+
s.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
18
|
|
data/ext/bitfield/bitfield.cpp
CHANGED
@@ -32,17 +32,17 @@ static VALUE bf_init(VALUE self, VALUE size)
|
|
32
32
|
BitField *ptr;
|
33
33
|
Data_Get_Struct(self, BitField, ptr);
|
34
34
|
ptr->data.resize(FIX2INT(size));
|
35
|
-
|
35
|
+
return self;
|
36
36
|
}
|
37
37
|
|
38
38
|
static VALUE bf_new(VALUE self, VALUE size)
|
39
39
|
{
|
40
40
|
VALUE *argv;
|
41
|
-
|
41
|
+
Check_Type(size, T_FIXNUM);
|
42
42
|
BitField *ptr;
|
43
43
|
argv[0] = size;
|
44
|
-
|
45
|
-
|
44
|
+
rb_obj_call_init(self, 1, argv);
|
45
|
+
return self;
|
46
46
|
}
|
47
47
|
|
48
48
|
/**
|
@@ -51,27 +51,32 @@ static VALUE bf_new(VALUE self, VALUE size)
|
|
51
51
|
*/
|
52
52
|
static VALUE bf_size(VALUE self)
|
53
53
|
{
|
54
|
-
|
55
|
-
|
56
|
-
|
54
|
+
BitField *ptr;
|
55
|
+
Data_Get_Struct(self, BitField, ptr);
|
56
|
+
return INT2NUM(ptr->data.size());
|
57
57
|
}
|
58
58
|
|
59
59
|
|
60
60
|
/**
|
61
|
-
* Flips the n-th bit if position is given
|
61
|
+
* Flips the n-th bit if position is given
|
62
62
|
* flips all bits otherwise (TODO)
|
63
63
|
*/
|
64
64
|
static VALUE bf_flip(int argc, VALUE *argv, VALUE self)
|
65
65
|
{
|
66
66
|
BitField *ptr;
|
67
67
|
Data_Get_Struct(self, BitField, ptr);
|
68
|
-
if(argc == 1)
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
68
|
+
if(argc == 1)
|
69
|
+
{
|
70
|
+
Check_Type(argv[0], T_FIXNUM);
|
71
|
+
ptr->data[FIX2INT(argv[0])].flip();
|
72
|
+
}
|
73
|
+
else if(argc == 0)
|
74
|
+
{
|
75
|
+
ptr->data.flip();
|
76
|
+
}
|
77
|
+
else
|
78
|
+
{
|
79
|
+
rb_raise(rb_eArgError, "wrong number of arguments(%d for 1 or 0)", argc);
|
75
80
|
}
|
76
81
|
return Qnil;
|
77
82
|
}
|
@@ -89,75 +94,99 @@ static VALUE bf_count(VALUE self)
|
|
89
94
|
|
90
95
|
static VALUE bf_bit_set(VALUE self, VALUE position, VALUE value)
|
91
96
|
{
|
92
|
-
|
93
|
-
|
97
|
+
BitField *ptr;
|
98
|
+
Data_Get_Struct(self, BitField, ptr);
|
94
99
|
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
100
|
+
if(rb_obj_is_kind_of(position, rb_cRange)
|
101
|
+
&& rb_obj_is_kind_of(value, rb_cArray))
|
102
|
+
{
|
103
|
+
long beg, len;
|
104
|
+
VALUE tmp;
|
105
|
+
switch(rb_range_beg_len(position, &beg, &len, (long int)ptr->data.size()-1, 2))
|
106
|
+
{
|
107
|
+
case Qfalse:
|
108
|
+
case Qnil:
|
109
|
+
return Qfalse;
|
110
|
+
default:
|
111
|
+
for(long i = beg+len-1; i >= beg; i--)
|
112
|
+
{
|
113
|
+
tmp = rb_ary_pop(value);
|
114
|
+
if(tmp != Qnil)
|
115
|
+
{
|
116
|
+
/* Is the array value a number? */
|
117
|
+
Check_Type(tmp, T_FIXNUM);
|
118
|
+
ptr->data[i] = FIX2INT(tmp) % 2;
|
119
|
+
}
|
120
|
+
else
|
121
|
+
{
|
122
|
+
rb_raise(rb_eRangeError, "Array is smaller than given range.");
|
123
|
+
}
|
124
|
+
}
|
125
|
+
}
|
126
|
+
}
|
127
|
+
else
|
128
|
+
{
|
129
|
+
/* Sanity checks for position and value */
|
130
|
+
Check_Type(position, T_FIXNUM);
|
131
|
+
Check_Type(value, T_FIXNUM);
|
117
132
|
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
133
|
+
int pos = FIX2INT(position);
|
134
|
+
if(pos < 0 || pos >= ptr->data.size())
|
135
|
+
{
|
136
|
+
rb_raise(rb_eRangeError, "BitField out of range with value %d.", pos);
|
137
|
+
}
|
138
|
+
ptr->data[pos] = FIX2INT(value) % 2;
|
139
|
+
}
|
140
|
+
return Qnil;
|
125
141
|
}
|
126
142
|
|
127
143
|
static VALUE bf_bit_get(VALUE self, VALUE position)
|
128
144
|
{
|
129
|
-
|
130
|
-
|
145
|
+
BitField *ptr;
|
146
|
+
Data_Get_Struct(self, BitField, ptr);
|
131
147
|
|
132
|
-
|
133
|
-
|
148
|
+
/* Is position a range? */
|
149
|
+
if(rb_obj_is_kind_of(position, rb_cRange))
|
150
|
+
{
|
134
151
|
long beg, len;
|
135
152
|
VALUE range_ary = rb_ary_new();
|
136
|
-
switch(rb_range_beg_len(position, &beg, &len, (long int)ptr->data.size()-1, 2))
|
153
|
+
switch(rb_range_beg_len(position, &beg, &len, (long int)ptr->data.size()-1, 2))
|
154
|
+
{
|
137
155
|
case Qfalse:
|
138
156
|
case Qnil:
|
139
157
|
return Qnil;
|
140
158
|
default:
|
141
|
-
for(long i = beg; i < beg+len; i++)
|
142
|
-
|
159
|
+
for(long i = beg; i < beg+len; i++)
|
160
|
+
{
|
161
|
+
if(i < 0 || i >= ptr->data.size()) {
|
162
|
+
rb_ary_push(range_ary, Qnil);
|
163
|
+
} else {
|
164
|
+
rb_ary_push(range_ary, INT2NUM(ptr->data[i]));
|
165
|
+
}
|
143
166
|
}
|
144
167
|
}
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
168
|
+
return range_ary;
|
169
|
+
}
|
170
|
+
else
|
171
|
+
{
|
172
|
+
/* Sanity checks for position and value */
|
173
|
+
Check_Type(position, T_FIXNUM);
|
174
|
+
long pos = FIX2INT(position);
|
175
|
+
if(pos < 0 || pos >= ptr->data.size()) {
|
176
|
+
return Qnil;
|
177
|
+
} else {
|
178
|
+
return INT2NUM(ptr->data[pos]);
|
179
|
+
}
|
180
|
+
}
|
152
181
|
}
|
153
182
|
|
154
183
|
static VALUE bf_to_s(VALUE self)
|
155
184
|
{
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
185
|
+
BitField *ptr;
|
186
|
+
Data_Get_Struct(self, BitField, ptr);
|
187
|
+
std::string buffer;
|
188
|
+
to_string(ptr->data, buffer);
|
189
|
+
return rb_str_new2(buffer.c_str());
|
161
190
|
}
|
162
191
|
|
163
192
|
static VALUE bf_initialize_clone(VALUE self, VALUE orig)
|
@@ -170,8 +199,8 @@ static VALUE bf_initialize_clone(VALUE self, VALUE orig)
|
|
170
199
|
return self;
|
171
200
|
}
|
172
201
|
|
173
|
-
/*
|
174
|
-
*Correctly free the struct
|
202
|
+
/*
|
203
|
+
*Correctly free the struct
|
175
204
|
*/
|
176
205
|
void bf_free(BitField *ptr)
|
177
206
|
{
|
@@ -181,86 +210,87 @@ void bf_free(BitField *ptr)
|
|
181
210
|
static VALUE BitField_allocate(VALUE klass)
|
182
211
|
{
|
183
212
|
BitField *bf;
|
184
|
-
|
185
|
-
|
213
|
+
bf = ALLOC(BitField);
|
214
|
+
new(bf) BitField();
|
186
215
|
VALUE tdata = Data_Wrap_Struct(klass, 0, bf_free, bf);
|
187
|
-
|
216
|
+
return tdata;
|
188
217
|
}
|
189
218
|
|
190
219
|
VALUE rb_cBitField;
|
191
220
|
|
192
221
|
extern "C" {
|
193
|
-
|
194
|
-
|
222
|
+
void Init_bitfield()
|
223
|
+
{
|
224
|
+
rb_cBitField = rb_define_class("BitField", rb_cObject);
|
195
225
|
|
196
226
|
rb_define_alloc_func(rb_cBitField, BitField_allocate);
|
197
227
|
|
198
228
|
/* 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
229
|
rb_define_method(
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
|
210
|
-
|
230
|
+
rb_cBitField,
|
231
|
+
"initialize_clone",
|
232
|
+
reinterpret_cast<VALUE(*)(...)>(bf_initialize_clone),
|
233
|
+
1
|
234
|
+
);
|
235
|
+
rb_define_method(
|
236
|
+
rb_cBitField,
|
237
|
+
"initialize_dup",
|
238
|
+
reinterpret_cast<VALUE(*)(...)>(bf_initialize_clone),
|
239
|
+
1
|
240
|
+
);
|
211
241
|
|
212
242
|
/* Returns the size of the bitset */
|
213
|
-
|
214
|
-
|
215
|
-
|
216
|
-
|
217
|
-
|
218
|
-
|
219
|
-
|
220
|
-
|
221
|
-
|
222
|
-
|
223
|
-
|
224
|
-
|
225
|
-
|
226
|
-
|
227
|
-
|
228
|
-
|
229
|
-
|
230
|
-
|
231
|
-
|
232
|
-
|
233
|
-
|
234
|
-
|
235
|
-
|
236
|
-
|
237
|
-
|
238
|
-
|
239
|
-
|
240
|
-
|
241
|
-
|
242
|
-
|
243
|
-
|
244
|
-
|
245
|
-
|
246
|
-
|
247
|
-
|
248
|
-
|
249
|
-
|
250
|
-
|
251
|
-
|
252
|
-
|
253
|
-
|
254
|
-
|
255
|
-
|
243
|
+
rb_define_method(
|
244
|
+
rb_cBitField,
|
245
|
+
"size",
|
246
|
+
reinterpret_cast<VALUE(*)(...)>(bf_size),
|
247
|
+
0
|
248
|
+
);
|
249
|
+
rb_define_method(
|
250
|
+
rb_cBitField,
|
251
|
+
"initialize",
|
252
|
+
reinterpret_cast<VALUE(*)(...)>(bf_init),
|
253
|
+
1
|
254
|
+
);
|
255
|
+
rb_define_method(
|
256
|
+
rb_cBitField,
|
257
|
+
"[]=",
|
258
|
+
reinterpret_cast<VALUE(*)(...)>(bf_bit_set),
|
259
|
+
2
|
260
|
+
);
|
261
|
+
rb_define_method(
|
262
|
+
rb_cBitField,
|
263
|
+
"[]",
|
264
|
+
reinterpret_cast<VALUE(*)(...)>(bf_bit_get),
|
265
|
+
1
|
266
|
+
);
|
267
|
+
rb_define_method(
|
268
|
+
rb_cBitField,
|
269
|
+
"to_s",
|
270
|
+
reinterpret_cast<VALUE(*)(...)>(bf_to_s),
|
271
|
+
0
|
272
|
+
);
|
273
|
+
rb_define_method(
|
274
|
+
rb_cBitField,
|
275
|
+
"flip",
|
276
|
+
reinterpret_cast<VALUE(*)(...)>(bf_flip),
|
277
|
+
-1
|
278
|
+
);
|
279
|
+
rb_define_method(
|
280
|
+
rb_cBitField,
|
281
|
+
"count",
|
282
|
+
reinterpret_cast<VALUE(*)(...)>(bf_count),
|
283
|
+
0
|
284
|
+
);
|
285
|
+
}
|
256
286
|
}
|
257
287
|
|
258
288
|
/* Library Code */
|
259
289
|
BitField *BitFieldNew(int size)
|
260
290
|
{
|
261
|
-
|
262
|
-
|
263
|
-
|
264
|
-
|
265
|
-
|
291
|
+
BitField *bf;
|
292
|
+
bf = ALLOC(BitField);
|
293
|
+
new(bf) BitField();
|
294
|
+
bf->data.resize(size);
|
295
|
+
return bf;
|
266
296
|
}
|
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.2
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,10 +9,9 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2011-10-
|
13
|
-
default_executable:
|
12
|
+
date: 2011-10-26 00:00:00.000000000Z
|
14
13
|
dependencies: []
|
15
|
-
description: ! "This C
|
14
|
+
description: ! "This C extension wraps boost::dynamic_bitset and makes it available\n
|
16
15
|
\ as a native Ruby class. The bitset behaves like an an array
|
17
16
|
allowing\n only values of 0 and 1."
|
18
17
|
email: halfdan@xnorfz.de
|
@@ -27,7 +26,6 @@ files:
|
|
27
26
|
- ext/bitfield/bitfield.cpp
|
28
27
|
- ext/bitfield/bitfield.hpp
|
29
28
|
- ext/bitfield/extconf.rb
|
30
|
-
has_rdoc: true
|
31
29
|
homepage: https://github.com/halfdan/ruby-bitfield/
|
32
30
|
licenses: []
|
33
31
|
post_install_message:
|
@@ -48,7 +46,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
48
46
|
version: '0'
|
49
47
|
requirements: []
|
50
48
|
rubyforge_project:
|
51
|
-
rubygems_version: 1.
|
49
|
+
rubygems_version: 1.8.10
|
52
50
|
signing_key:
|
53
51
|
specification_version: 3
|
54
52
|
summary: Wraps boost::dynamic_bitset and makes it available as a Ruby class for fast
|