bitset 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.1
1
+ 0.0.2
data/bitset.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{bitset}
8
- s.version = "0.0.0"
8
+ s.version = "0.0.2"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Tyler McMullen"]
12
- s.date = %q{2011-02-17}
12
+ s.date = %q{2011-02-19}
13
13
  s.description = %q{A fast C-based Bitset. It supports the standard set operations as well as operations you may expect on bit arrays. (popcount, for instance)}
14
14
  s.email = %q{tbmcmullen@gmail.com}
15
15
  s.extensions = ["ext/bitset/extconf.rb"]
@@ -22,6 +22,7 @@ Gem::Specification.new do |s|
22
22
  "README.rdoc",
23
23
  "Rakefile",
24
24
  "VERSION",
25
+ "bitset.gemspec",
25
26
  "ext/bitset/bitset.c",
26
27
  "ext/bitset/extconf.rb",
27
28
  "spec/bitset_spec.rb"
data/ext/bitset/bitset.c CHANGED
@@ -234,6 +234,52 @@ static VALUE rb_bitset_xor(VALUE self, VALUE other) {
234
234
  return Data_Wrap_Struct(cBitset, 0, bitset_free, new_bs);
235
235
  }
236
236
 
237
+ static VALUE rb_bitset_not(VALUE self) {
238
+ Bitset * bs = get_bitset(self);
239
+
240
+ Bitset * new_bs = bitset_new();
241
+ bitset_setup(new_bs, bs->len);
242
+
243
+ int max = ((bs->len-1) >> 6) + 1;
244
+ int i;
245
+ for(i = 0; i < max; i++) {
246
+ uint64_t segment = bs->data[i];
247
+ new_bs->data[i] = ~segment;
248
+ }
249
+
250
+ return Data_Wrap_Struct(cBitset, 0, bitset_free, new_bs);
251
+ }
252
+
253
+ static VALUE rb_bitset_to_s(VALUE self) {
254
+ Bitset * bs = get_bitset(self);
255
+
256
+ int i;
257
+ char * data = malloc(bs->len + 1);
258
+ for(i = 0; i < bs->len; i++) {
259
+ data[i] = get_bit(bs, i) ? '1' : '0';
260
+ }
261
+ data[bs->len] = 0;
262
+
263
+ return rb_str_new2(data);
264
+ }
265
+
266
+ static VALUE rb_bitset_from_s(VALUE self, VALUE s) {
267
+ int length = RSTRING_LEN(s);
268
+ char* data = StringValuePtr(s);
269
+
270
+ Bitset * new_bs = bitset_new();
271
+ bitset_setup(new_bs, length);
272
+
273
+ int i;
274
+ for (i = 0; i < length; i++) {
275
+ if (data[i] == '1') {
276
+ set_bit(new_bs, i);
277
+ }
278
+ }
279
+
280
+ return Data_Wrap_Struct(cBitset, 0, bitset_free, new_bs);
281
+ }
282
+
237
283
  static VALUE rb_bitset_hamming(VALUE self, VALUE other) {
238
284
  Bitset * bs = get_bitset(self);
239
285
  Bitset * other_bs = get_bitset(other);
@@ -283,6 +329,10 @@ void Init_bitset() {
283
329
  rb_define_method(cBitset, "xor", rb_bitset_xor, 1);
284
330
  rb_define_alias(cBitset, "^", "xor");
285
331
  rb_define_alias(cBitset, "symmetric_difference", "xor");
332
+ rb_define_method(cBitset, "not", rb_bitset_not, 0);
333
+ rb_define_alias(cBitset, "!", "not");
286
334
  rb_define_method(cBitset, "hamming", rb_bitset_hamming, 1);
287
335
  rb_define_method(cBitset, "each", rb_bitset_each, 0);
336
+ rb_define_method(cBitset, "to_s", rb_bitset_to_s, 0);
337
+ rb_define_singleton_method(cBitset, "from_s", rb_bitset_from_s, 1);
288
338
  }
data/spec/bitset_spec.rb CHANGED
@@ -1,4 +1,4 @@
1
- require './bitset'
1
+ require 'bitset'
2
2
 
3
3
  describe Bitset do
4
4
  it 'can be initialized' do
@@ -189,6 +189,17 @@ describe Bitset do
189
189
  end
190
190
  end
191
191
 
192
+ describe :not do
193
+ it "returns a new Bitset with is the not of one Bitset" do
194
+ bs1 = Bitset.new(8)
195
+ bs1.set 1, 4, 7
196
+
197
+ bs2 = bs1.not
198
+ bs2.set?(0, 2, 3, 5, 6).should == true
199
+ bs2.clear?(1, 4, 7).should == true
200
+ end
201
+ end
202
+
192
203
  describe :hamming do
193
204
  it 'returns the hamming distance of two Bitsets' do
194
205
  bs1 = Bitset.new(8)
@@ -214,4 +225,19 @@ describe Bitset do
214
225
  i.should == 4
215
226
  end
216
227
  end
228
+
229
+ describe :to_s do
230
+ it 'correctly prints out a binary string' do
231
+ bs = Bitset.new(4)
232
+ bs.set 0, 2
233
+ bs.to_s.should == "1010"
234
+ end
235
+ end
236
+
237
+ describe :from_s do
238
+ it 'correctly creates a bitmap from a binary string' do
239
+ bs = Bitset.from_s("10101")
240
+ bs.set?(0, 2, 4).should == true
241
+ end
242
+ end
217
243
  end
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 0
8
- - 1
9
- version: 0.0.1
8
+ - 2
9
+ version: 0.0.2
10
10
  platform: ruby
11
11
  authors:
12
12
  - Tyler McMullen
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2011-02-17 00:00:00 -08:00
17
+ date: 2011-02-19 00:00:00 -08:00
18
18
  default_executable:
19
19
  dependencies: []
20
20