File tree Expand file tree Collapse file tree 2 files changed +17
-3
lines changed Expand file tree Collapse file tree 2 files changed +17
-3
lines changed Original file line number Diff line number Diff line change @@ -26,14 +26,21 @@ def [](position)
2626 end
2727
2828 # Iterate over each bit
29- def each ( &block )
29+ def each
30+ return to_enum ( :each ) unless block_given?
3031 @size . times { |position | yield self [ position ] }
3132 end
3233
3334 # Returns the field as a string like "0101010100111100," etc.
3435 def to_s
3536 @field . collect { |ea | ( "%0#{ ELEMENT_WIDTH } b" % ea ) . reverse } . join [ 0 ..@size -1 ]
3637 end
38+
39+ # Iterate over each byte
40+ def each_byte
41+ return to_enum ( :each_byte ) unless block_given?
42+ @field . each { |byte | yield byte }
43+ end
3744
3845 # Returns the total number of bits that are set
3946 # (The technique used here is about 6 times faster than using each or inject direct on the bitfield)
Original file line number Diff line number Diff line change @@ -26,7 +26,8 @@ def [](position)
2626 end
2727
2828 # Iterate over each bit
29- def each ( &block )
29+ def each
30+ return to_enum ( :each ) unless block_given?
3031 @size . times { |position | yield self [ position ] }
3132 end
3233
@@ -38,11 +39,17 @@ def to_s
3839 @field . bytes . collect { |ea | ( "%08b" % ea ) } . join [ 0 , @size ]
3940 end
4041 end
42+
43+ # Iterates over each byte
44+ def each_byte
45+ return to_enum ( :each_byte ) unless block_given?
46+ @field . bytes . each { |byte | yield byte }
47+ end
4148
4249 # Returns the total number of bits that are set
4350 # (The technique used here is about 6 times faster than using each or inject direct on the bitfield)
4451 def total_set
45- @field . bytes . inject ( 0 ) { |a , byte | a += byte & 1 and byte >>= 1 until byte == 0 ; a }
52+ each_byte . inject ( 0 ) { |a , byte | a += byte & 1 and byte >>= 1 until byte == 0 ; a }
4653 end
4754
4855 def byte_position ( position )
You can’t perform that action at this time.
0 commit comments