class MagicCloud::BitMatrix
Dead simple 2-dimensional “bit matrix”, storing 1s and 0s. Not memory effectife at all, but the fastest pure-Ruby solution I've tried.
Attributes
bits[R]
height[R]
width[R]
Public Class Methods
new(width, height)
click to toggle source
# File lib/magic_cloud/bit_matrix.rb, line 7 def initialize(width, height) @width, @height = width, height @bits = [0] * height*width end
Public Instance Methods
at(x, y)
click to toggle source
returns true/false FIXME: maybe put
should also accept true/false
# File lib/magic_cloud/bit_matrix.rb, line 23 def at(x, y) bits[y*@width + x] != 0 # faster than .zero? end
dump()
click to toggle source
# 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)
click to toggle source
# 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