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.
7 8 9 10 |
# File 'lib/magic_cloud/bit_matrix.rb', line 7 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.
12 13 14 |
# File 'lib/magic_cloud/bit_matrix.rb', line 12 def bits @bits end |
#height ⇒ Object (readonly)
Returns the value of attribute height.
12 13 14 |
# File 'lib/magic_cloud/bit_matrix.rb', line 12 def height @height end |
#width ⇒ Object (readonly)
Returns the value of attribute width.
12 13 14 |
# File 'lib/magic_cloud/bit_matrix.rb', line 12 def width @width end |
Instance Method Details
#at(x, y) ⇒ Object
returns true/false FIXME: maybe #put should also accept true/false
23 24 25 |
# File 'lib/magic_cloud/bit_matrix.rb', line 23 def at(x, y) bits[y*@width + x] != 0 # faster than .zero? end |
#dump ⇒ Object
27 28 29 30 31 |
# File 'lib/magic_cloud/bit_matrix.rb', line 27 def dump (0...height).map{|y| (0...width).map{|x| at(x, y) ? ' ' : 'x'}.join }.join("\n") end |
#put(x, y, px = 1) ⇒ Object
14 15 16 17 18 19 |
# File 'lib/magic_cloud/bit_matrix.rb', line 14 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 |