Skip to content

Commit 3f7ff29

Browse files
committed
Use each_byte to significantly reduce memory requirements
1 parent 2ae5283 commit 3f7ff29

File tree

3 files changed

+3
-3
lines changed

3 files changed

+3
-3
lines changed

lib/bitarray-array.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,6 @@ def each_byte
4545
# Returns the total number of bits that are set
4646
# (The technique used here is about 6 times faster than using each or inject direct on the bitfield)
4747
def total_set
48-
@field.inject(0) { |a, byte| a += byte & 1 and byte >>= 1 until byte == 0; a }
48+
@field.each_byte.inject(0) { |a, byte| a += byte & 1 and byte >>= 1 until byte == 0; a }
4949
end
5050
end

lib/bitarray.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ def each_byte
4949
# Returns the total number of bits that are set
5050
# (The technique used here is about 6 times faster than using each or inject direct on the bitfield)
5151
def total_set
52-
@field.bytes.inject(0) { |a, byte| a += byte & 1 and byte >>= 1 until byte == 0; a }
52+
@field.each_byte.inject(0) { |a, byte| a += byte & 1 and byte >>= 1 until byte == 0; a }
5353
end
5454

5555
def byte_position(position)

test/test_bitarray.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
require "minitest/autorun"
2-
require "bitarray"
2+
require_relative "../lib/bitarray"
33

44
class TestBitArray < Minitest::Test
55
def setup

0 commit comments

Comments
 (0)