Skip to content

Commit e24928a

Browse files
committed
Expose field attribute
1 parent f112c54 commit e24928a

File tree

1 file changed

+8
-7
lines changed

1 file changed

+8
-7
lines changed

lib/bitarray.rb

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
class BitArray
22
attr_reader :size
3+
attr_reader :field
34
include Enumerable
4-
5+
56
ELEMENT_WIDTH = 32
6-
7+
78
def initialize(size)
89
@size = size
910
@field = Array.new(((size - 1) / ELEMENT_WIDTH) + 1, 0)
1011
end
11-
12+
1213
# Set a bit (1/0)
1314
def []=(position, value)
1415
if value == 1
@@ -17,22 +18,22 @@ def []=(position, value)
1718
@field[position / ELEMENT_WIDTH] ^= 1 << (position % ELEMENT_WIDTH)
1819
end
1920
end
20-
21+
2122
# Read a bit (1/0)
2223
def [](position)
2324
@field[position / ELEMENT_WIDTH] & 1 << (position % ELEMENT_WIDTH) > 0 ? 1 : 0
2425
end
25-
26+
2627
# Iterate over each bit
2728
def each(&block)
2829
@size.times { |position| yield self[position] }
2930
end
30-
31+
3132
# Returns the field as a string like "0101010100111100," etc.
3233
def to_s
3334
inject("") { |a, b| a + b.to_s }
3435
end
35-
36+
3637
# Returns the total number of bits that are set
3738
# (The technique used here is about 6 times faster than using each or inject direct on the bitfield)
3839
def total_set

0 commit comments

Comments
 (0)