Class: MagicCloud::BitMatrix
- Inherits:
-
Object
- Object
- MagicCloud::BitMatrix
- Defined in:
- lib/magic_cloud/bit_matrix.rb
Overview
Dead simple 2-dimensional “bit matrix”, storing 1s and 0s. Not memory effectife at all, but the fastest pure-Ruby solution I’ve tried.
Direct Known Subclasses
Instance Attribute Summary collapse
-
#bits ⇒ Object
readonly
Returns the value of attribute bits.
-
#height ⇒ Object
readonly
Returns the value of attribute height.
-
#width ⇒ Object
readonly
Returns the value of attribute width.
Instance Method Summary collapse
-
#at(x, y) ⇒ Object
returns true/false FIXME: maybe #put should also accept true/false.
- #dump ⇒ Object
-
#initialize(width, height) ⇒ BitMatrix
constructor
A new instance of BitMatrix.
- #put(x, y, px = 1) ⇒ Object
Constructor Details
#initialize(width, height) ⇒ BitMatrix
Returns a new instance of BitMatrix.
8 9 10 11 |
# File 'lib/magic_cloud/bit_matrix.rb', line 8 def initialize(width, height) @width, @height = width, height @bits = [0] * height*width end |
Instance Attribute Details
#bits ⇒ Object (readonly)
Returns the value of attribute bits.
13 14 15 |
# File 'lib/magic_cloud/bit_matrix.rb', line 13 def bits @bits end |
#height ⇒ Object (readonly)
Returns the value of attribute height.
13 14 15 |
# File 'lib/magic_cloud/bit_matrix.rb', line 13 def height @height end |
#width ⇒ Object (readonly)
Returns the value of attribute width.
13 14 15 |
# File 'lib/magic_cloud/bit_matrix.rb', line 13 def width @width end |
Instance Method Details
#at(x, y) ⇒ Object
returns true/false FIXME: maybe #put should also accept true/false
24 25 26 |
# File 'lib/magic_cloud/bit_matrix.rb', line 24 def at(x, y) bits[y*@width + x] != 0 # faster than .zero? end |
#dump ⇒ Object
28 29 30 31 32 |
# File 'lib/magic_cloud/bit_matrix.rb', line 28 def dump (0...height).map { |y| (0...width).map { |x| at(x, y) ? ' ' : 'x' }.join }.join("\n") end |
#put(x, y, px = 1) ⇒ Object
15 16 17 18 19 20 |
# File 'lib/magic_cloud/bit_matrix.rb', line 15 def put(x, y, px = 1) x < width or fail("#{x} outside matrix: #{width}") y < height or fail("#{y} outside matrix: #{height}") bits[y*@width + x] = 1 unless px == 0 # It's faster with unless end |