Skip to content

Cleanup #14

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 0 additions & 51 deletions lib/bitarray-array.rb

This file was deleted.

60 changes: 1 addition & 59 deletions lib/bitarray.rb
Original file line number Diff line number Diff line change
@@ -1,59 +1 @@
class BitArray
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved to lib/bitarray/bit_array.rb.

attr_reader :size
attr_reader :field
include Enumerable

VERSION = "1.3.0"

def initialize(size, field = nil, reverse_byte: true)
@size = size
@field = field || "\0" * (size / 8 + 1)
@reverse_byte = reverse_byte
end

# Set a bit (1/0)
def []=(position, value)
if value == 1
@field.setbyte(position >> 3, @field.getbyte(position >> 3) | (1 << (byte_position(position) % 8)))
else
@field.setbyte(position >> 3, @field.getbyte(position >> 3) & ~(1 << (byte_position(position) % 8)))
end
end

# Read a bit (1/0)
def [](position)
(@field.getbyte(position >> 3) & (1 << (byte_position(position) % 8))) > 0 ? 1 : 0
end

# Iterate over each bit
def each
return to_enum(:each) unless block_given?
@size.times { |position| yield self[position] }
end

# Returns the field as a string like "0101010100111100," etc.
def to_s
if @reverse_byte
@field.bytes.collect { |ea| ("%08b" % ea).reverse }.join[0, @size]
else
@field.bytes.collect { |ea| ("%08b" % ea) }.join[0, @size]
end
end

# Iterates over each byte
def each_byte
return to_enum(:each_byte) unless block_given?
@field.bytes.each{ |byte| yield byte }
end

# Returns the total number of bits that are set
# Use Brian Kernighan's way, see
# https://graphics.stanford.edu/~seander/bithacks.html#CountBitsSetKernighan
def total_set
@field.each_byte.inject(0) { |a, byte| (a += 1; byte &= byte - 1) while byte > 0 ; a }
end

def byte_position(position)
@reverse_byte ? position : 7 - position
end
end
require_relative "bitarray/bit_array"
58 changes: 58 additions & 0 deletions lib/bitarray/bit_array.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
class BitArray
attr_reader :field, :reverse_byte, :size
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added reverse_byte to the publicly accessible attributes.

include Enumerable

VERSION = "1.3.0"

def initialize(size, field = nil, reverse_byte: true)
@size = size
@field = field || "\0" * (size / 8 + 1)
@reverse_byte = reverse_byte
end

# Set a bit (1/0)
def []=(position, value)
if value == 1
@field.setbyte(position >> 3, @field.getbyte(position >> 3) | (1 << (byte_position(position) % 8)))
else
@field.setbyte(position >> 3, @field.getbyte(position >> 3) & ~(1 << (byte_position(position) % 8)))
end
end

# Read a bit (1/0)
def [](position)
(@field.getbyte(position >> 3) & (1 << (byte_position(position) % 8))) > 0 ? 1 : 0
end

# Iterate over each bit
def each
return to_enum(:each) unless block_given?
@size.times { |position| yield self[position] }
end

# Returns the field as a string like "0101010100111100," etc.
def to_s
if @reverse_byte
@field.bytes.collect { |ea| ("%08b" % ea).reverse }.join[0, @size]
else
@field.bytes.collect { |ea| ("%08b" % ea) }.join[0, @size]
end
end

# Iterates over each byte
def each_byte
return to_enum(:each_byte) unless block_given?
@field.bytes.each{ |byte| yield byte }
end

# Returns the total number of bits that are set
# Use Brian Kernighan's way, see
# https://graphics.stanford.edu/~seander/bithacks.html#CountBitsSetKernighan
def total_set
@field.each_byte.inject(0) { |a, byte| (a += 1; byte &= byte - 1) while byte > 0 ; a }
end

private def byte_position(position)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Marked this method private.

@reverse_byte ? position : 7 - position
end
end
File renamed without changes.