File tree 3 files changed +9
-7
lines changed
3 files changed +9
-7
lines changed Original file line number Diff line number Diff line change @@ -3,7 +3,7 @@ class BitArray
3
3
attr_reader :field
4
4
include Enumerable
5
5
6
- VERSION = "1.2 .0"
6
+ VERSION = "1.3 .0"
7
7
ELEMENT_WIDTH = 32
8
8
9
9
def initialize ( size , field = nil )
@@ -43,8 +43,9 @@ def each_byte
43
43
end
44
44
45
45
# Returns the total number of bits that are set
46
- # (The technique used here is about 6 times faster than using each or inject direct on the bitfield)
46
+ # Use Brian Kernighan's way, see
47
+ # https://graphics.stanford.edu/~seander/bithacks.html#CountBitsSetKernighan
47
48
def total_set
48
- @field . inject ( 0 ) { |a , byte | a += byte & 1 and byte >>= 1 until byte == 0 ; a }
49
+ @field . each_byte . inject ( 0 ) { |a , byte | ( a += 1 ; byte &= byte - 1 ) while byte > 0 ; a }
49
50
end
50
51
end
Original file line number Diff line number Diff line change @@ -3,7 +3,7 @@ class BitArray
3
3
attr_reader :field
4
4
include Enumerable
5
5
6
- VERSION = "1.2 .0"
6
+ VERSION = "1.3 .0"
7
7
8
8
def initialize ( size , field = nil , reverse_byte : true )
9
9
@size = size
@@ -47,9 +47,10 @@ def each_byte
47
47
end
48
48
49
49
# Returns the total number of bits that are set
50
- # (The technique used here is about 6 times faster than using each or inject direct on the bitfield)
50
+ # Use Brian Kernighan's way, see
51
+ # https://graphics.stanford.edu/~seander/bithacks.html#CountBitsSetKernighan
51
52
def total_set
52
- @field . bytes . inject ( 0 ) { |a , byte | a += byte & 1 and byte >>= 1 until byte == 0 ; a }
53
+ @field . each_byte . inject ( 0 ) { |a , byte | ( a += 1 ; byte &= byte - 1 ) while byte > 0 ; a }
53
54
end
54
55
55
56
def byte_position ( position )
Original file line number Diff line number Diff line change 1
1
require "minitest/autorun"
2
- require " bitarray"
2
+ require_relative "../lib/ bitarray"
3
3
4
4
class TestBitArray < Minitest ::Test
5
5
def setup
You can’t perform that action at this time.
0 commit comments