-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
205 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
require_relative "bitarray/bit_array" | ||
require_relative "bitarray/bit_array_file" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
require_relative "bit_array" | ||
|
||
# Read-only access to a BitArray dumped to disk. | ||
# This is considerably slower than using the RAM-based BitArray, but | ||
# avoids the memory requirements and initial setup time. | ||
class BitArrayFile | ||
HEADER_LENGTH = BitArray::HEADER_LENGTH | ||
|
||
attr_reader :io, :reverse_byte, :size | ||
|
||
def initialize(filename: nil, io: nil) | ||
if io | ||
@io = io | ||
elsif filename | ||
@io = File.open(filename, "r") | ||
else | ||
raise ArgumentError.new("Must specify a filename or io argument") | ||
end | ||
|
||
@io.seek(0) | ||
@size, @reverse_byte = @io.read(9).unpack("QC") | ||
@reverse_byte = @reverse_byte != 0 | ||
end | ||
|
||
# Read a bit (1/0) | ||
def [](position) | ||
seek_to(position >> 3) | ||
(@io.getbyte & (1 << (byte_position(position) % 8))) > 0 ? 1 : 0 | ||
end | ||
|
||
private def byte_position(position) | ||
@reverse_byte ? position : 7 - position | ||
end | ||
|
||
private def seek_to(position) | ||
@io.seek(position + HEADER_LENGTH) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
require "minitest/autorun" | ||
require "tempfile" | ||
require_relative "../lib/bitarray" | ||
|
||
class TestBitArrayFile < Minitest::Test | ||
def setup | ||
ba = BitArray.new(35) | ||
[1, 5, 6, 7, 10, 16, 33].each { |i| ba[i] = 1} | ||
@file = Tempfile.new("bit_array_file.dat") | ||
ba.dump(@file) | ||
@file.rewind | ||
end | ||
|
||
def teardown | ||
@file.close | ||
@file.unlink | ||
end | ||
|
||
def test_from_filename | ||
baf = BitArrayFile.new(filename: @file.path) | ||
for i in 0...35 | ||
expected = [1, 5, 6, 7, 10, 16, 33].include?(i) ? 1 : 0 | ||
assert_equal expected, baf[i] | ||
end | ||
end | ||
|
||
def test_from_io | ||
baf = BitArrayFile.new(io: @file) | ||
for i in 0...35 | ||
expected = [1, 5, 6, 7, 10, 16, 33].include?(i) ? 1 : 0 | ||
assert_equal expected, baf[i] | ||
end | ||
end | ||
end | ||
|
||
class TestBitArrayFileWhenNonReversedByte < Minitest::Test | ||
def setup | ||
ba = BitArray.new(35, reverse_byte: false) | ||
[1, 5, 6, 7, 10, 16, 33].each { |i| ba[i] = 1} | ||
@file = Tempfile.new("bit_array_file.dat") | ||
ba.dump(@file) | ||
@file.rewind | ||
end | ||
|
||
def teardown | ||
@file.close | ||
@file.unlink | ||
end | ||
|
||
def test_from_filename | ||
baf = BitArrayFile.new(filename: @file.path) | ||
for i in 0...35 | ||
expected = [1, 5, 6, 7, 10, 16, 33].include?(i) ? 1 : 0 | ||
assert_equal expected, baf[i] | ||
end | ||
end | ||
|
||
def test_from_io | ||
baf = BitArrayFile.new(io: @file) | ||
for i in 0...35 | ||
expected = [1, 5, 6, 7, 10, 16, 33].include?(i) ? 1 : 0 | ||
assert_equal expected, baf[i] | ||
end | ||
end | ||
end |