ingramj-bitarray 0.4.1 → 0.4.2
Sign up to get free protection for your applications and to get access to all the features.
- data/VERSION +1 -1
- data/bitarray.gemspec +2 -2
- data/ext/bitarray.c +33 -9
- data/test/bm.rb +4 -0
- data/test/test.rb +7 -0
- metadata +2 -2
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.4.
|
1
|
+
0.4.2
|
data/bitarray.gemspec
CHANGED
@@ -2,11 +2,11 @@
|
|
2
2
|
|
3
3
|
Gem::Specification.new do |s|
|
4
4
|
s.name = %q{bitarray}
|
5
|
-
s.version = "0.4.
|
5
|
+
s.version = "0.4.2"
|
6
6
|
|
7
7
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
8
8
|
s.authors = ["James E. Ingram"]
|
9
|
-
s.date = %q{2009-05-
|
9
|
+
s.date = %q{2009-05-30}
|
10
10
|
s.description = %q{A bit array class for Ruby, implemented as a C extension. Includes methods for setting and clearing individual bits, and all bits at once. Also has the standard array access methods, [] and []=, and it mixes in Enumerable.}
|
11
11
|
s.email = %q{ingramj@gmail.com}
|
12
12
|
s.extensions = ["ext/extconf.rb"]
|
data/ext/bitarray.c
CHANGED
@@ -22,7 +22,7 @@ struct bit_array {
|
|
22
22
|
/* Bit Array manipulation functions. */
|
23
23
|
|
24
24
|
/* Set the specified bit to 1. Return 1 on success, 0 on failure. */
|
25
|
-
static int
|
25
|
+
static inline int
|
26
26
|
set_bit(struct bit_array *ba, long bit)
|
27
27
|
{
|
28
28
|
if (bit < 0) bit += ba->bits;
|
@@ -36,7 +36,7 @@ set_bit(struct bit_array *ba, long bit)
|
|
36
36
|
|
37
37
|
|
38
38
|
/* Set all bits to 1. */
|
39
|
-
static int
|
39
|
+
static inline int
|
40
40
|
set_all_bits(struct bit_array *ba)
|
41
41
|
{
|
42
42
|
memset(ba->array, 0xff, (ba->array_size * UINT_BYTES));
|
@@ -45,7 +45,7 @@ set_all_bits(struct bit_array *ba)
|
|
45
45
|
|
46
46
|
|
47
47
|
/* Clear the specified bit to 0. Return 1 on success, 0 on failure. */
|
48
|
-
static int
|
48
|
+
static inline int
|
49
49
|
clear_bit(struct bit_array *ba, long bit)
|
50
50
|
{
|
51
51
|
if (bit < 0) bit += ba->bits;
|
@@ -59,7 +59,7 @@ clear_bit(struct bit_array *ba, long bit)
|
|
59
59
|
|
60
60
|
|
61
61
|
/* Clear all bits to 0. */
|
62
|
-
static int
|
62
|
+
static inline int
|
63
63
|
clear_all_bits(struct bit_array *ba)
|
64
64
|
{
|
65
65
|
memset(ba->array, 0x00, (ba->array_size * UINT_BYTES));
|
@@ -68,7 +68,7 @@ clear_all_bits(struct bit_array *ba)
|
|
68
68
|
|
69
69
|
|
70
70
|
/* Toggle the state of the specified bit. Return 1 on success, 0 on failure. */
|
71
|
-
static int
|
71
|
+
static inline int
|
72
72
|
toggle_bit(struct bit_array *ba, long bit)
|
73
73
|
{
|
74
74
|
if (bit < 0) bit += ba->bits;
|
@@ -82,7 +82,7 @@ toggle_bit(struct bit_array *ba, long bit)
|
|
82
82
|
|
83
83
|
|
84
84
|
/* Toggle the state of all bits. */
|
85
|
-
static int
|
85
|
+
static inline int
|
86
86
|
toggle_all_bits(struct bit_array *ba)
|
87
87
|
{
|
88
88
|
long i;
|
@@ -95,7 +95,7 @@ toggle_all_bits(struct bit_array *ba)
|
|
95
95
|
|
96
96
|
/* Assign the specified value to a bit. Return 1 on success, 0 on invalid bit
|
97
97
|
* index, and -1 on invalid value. */
|
98
|
-
static int
|
98
|
+
static inline int
|
99
99
|
assign_bit(struct bit_array *ba, long bit, int value)
|
100
100
|
{
|
101
101
|
if (value == 0) {
|
@@ -109,7 +109,7 @@ assign_bit(struct bit_array *ba, long bit, int value)
|
|
109
109
|
|
110
110
|
|
111
111
|
/* Get the state of the specified bit. Return -1 on failure. */
|
112
|
-
static int
|
112
|
+
static inline int
|
113
113
|
get_bit(struct bit_array *ba, long bit)
|
114
114
|
{
|
115
115
|
if (bit < 0) bit += ba->bits;
|
@@ -127,7 +127,7 @@ get_bit(struct bit_array *ba, long bit)
|
|
127
127
|
|
128
128
|
|
129
129
|
/* Return the number of set bits in the array. */
|
130
|
-
static long
|
130
|
+
static inline long
|
131
131
|
total_set(struct bit_array *ba)
|
132
132
|
{
|
133
133
|
/* This is basically the algorithm from K&R, with a running total for all
|
@@ -610,6 +610,29 @@ rb_bitarray_inspect(VALUE self)
|
|
610
610
|
}
|
611
611
|
|
612
612
|
|
613
|
+
/* call-seq:
|
614
|
+
* bitarray.to_a -> an_array
|
615
|
+
*
|
616
|
+
* Creates a Ruby Array from bitarray.
|
617
|
+
*/
|
618
|
+
static VALUE
|
619
|
+
rb_bitarray_to_a(VALUE self)
|
620
|
+
{
|
621
|
+
struct bit_array *ba;
|
622
|
+
Data_Get_Struct(self, struct bit_array, ba);
|
623
|
+
|
624
|
+
long array_size = ba->bits;
|
625
|
+
VALUE c_array[array_size];
|
626
|
+
|
627
|
+
int i;
|
628
|
+
for (i = 0; i < array_size; i++) {
|
629
|
+
c_array[i] = INT2FIX(get_bit(ba, i));
|
630
|
+
}
|
631
|
+
|
632
|
+
return rb_ary_new4(array_size, c_array);
|
633
|
+
}
|
634
|
+
|
635
|
+
|
613
636
|
/* call-seq:
|
614
637
|
* bitarray.each {|bit| block } -> bitarray
|
615
638
|
*
|
@@ -671,6 +694,7 @@ Init_bitarray()
|
|
671
694
|
rb_define_method(rb_bitarray_class, "[]", rb_bitarray_bitref, -1);
|
672
695
|
rb_define_alias(rb_bitarray_class, "slice", "[]");
|
673
696
|
rb_define_method(rb_bitarray_class, "[]=", rb_bitarray_assign_bit, 2);
|
697
|
+
rb_define_method(rb_bitarray_class, "to_a", rb_bitarray_to_a, 0);
|
674
698
|
rb_define_method(rb_bitarray_class, "inspect", rb_bitarray_inspect, 0);
|
675
699
|
rb_define_alias(rb_bitarray_class, "to_s", "inspect");
|
676
700
|
rb_define_method(rb_bitarray_class, "each", rb_bitarray_each, 0);
|
data/test/bm.rb
CHANGED
@@ -26,6 +26,10 @@ Benchmark.bm(28) { |bm|
|
|
26
26
|
bm.report("BitField to_s") { 10000.times { bf.to_s } }
|
27
27
|
bm.report("BitArray to_s") { 10000.times { ba.to_s } }
|
28
28
|
|
29
|
+
puts "---------------------------- To Array (10,000 iterations)"
|
30
|
+
bm.report("BitField to_a") { 10000.times { bf.to_a } }
|
31
|
+
bm.report("BitArray to_a") { 10000.times { ba.to_a } }
|
32
|
+
|
29
33
|
puts "---------------------------- Total Set (100,000 iterations)"
|
30
34
|
bf = BitField.new(256)
|
31
35
|
ba = BitArray.new(256)
|
data/test/test.rb
CHANGED
@@ -133,5 +133,12 @@ class TestLibraryFileName < Test::Unit::TestCase
|
|
133
133
|
ba3 = ba2 + ba1
|
134
134
|
assert_equal "111111100000000000000000000000000000000", ba3.to_s
|
135
135
|
end
|
136
|
+
|
137
|
+
def test_to_a
|
138
|
+
ba = BitArray.new(16)
|
139
|
+
ba[1] = 1
|
140
|
+
ba[5] = 1
|
141
|
+
assert_equal [0,1,0,0,0,1,0,0,0,0,0,0,0,0,0,0], ba.to_a
|
142
|
+
end
|
136
143
|
end
|
137
144
|
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ingramj-bitarray
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.4.
|
4
|
+
version: 0.4.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- James E. Ingram
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2009-05-
|
12
|
+
date: 2009-05-30 00:00:00 -07:00
|
13
13
|
default_executable:
|
14
14
|
dependencies: []
|
15
15
|
|