On 19.11.2009 17:53, Marnen Laibow-Koser wrote:
Robert K. wrote:
I was trying to avoid the pitfall of your LazyM solution, which appears
to use matrix[i, j] instead of matrix[i][j] to address a cell. To my
mind this violates POLS.
I don’t find that particularly violating POLS. If you want Arrays
nested in a Hash you can do this as well
irb(main):001:0> matrix = Hash.new {|h,k| h[k] = []}
=> {}
irb(main):002:0> matrix[10][2]=13
=> 13
irb(main):003:0> matrix[10][2]
=> 13
irb(main):004:0> matrix
=> {10=>[nil, nil, 13]}
irb(main):005:0>
However, this has bad memory characteristics, especially with large
second index values.
You could of course instead do
irb(main):005:0> matrix = Hash.new {|h,k| h[k] = {}}
=> {}
irb(main):006:0> matrix[10][2]
=> nil
irb(main):007:0> matrix[10][2]=13
=> 13
irb(main):008:0> matrix[10][2]
=> 13
irb(main):009:0> matrix
=> {10=>{2=>13}}
irb(main):010:0>
I tend to believe that my solution with a single Hash and arrays as
indexes is more efficient although I cannot prove it right now.
Personally I would prefer having a single object that encapsulates the
storage mechanism away (so you can even change it later). That’s more
difficult with the addressing scheme (two sets of brackets) you seem to
favor:
class LazyM2
Proxy = Struct.new :m, :x do
def ; m.get(x,y) end
def []=(y,v); m.set(x,y,v) end
end
def initialize(default = nil)
@h = Hash.new(default)
end
def
Proxy.new self, x
end
def get(x,y)
@h[[x,y]]
end
def set(x,y,z)
@h[[x,y]]=z
end
def clear
@h.clear
end
end
matrix = LazyM2.new
matrix[1][20]=123
p matrix
As a side note: I’d rather delegate to instead of inherit from Hash.
The DynamicArray is not a Hash. For example, method #size remains in
the interface which might lead to surprises.
Probably not a bad idea. This was seat-of-the-pants code.
I didn’t knew that category of software. Learn something new every
day.
Cheers
robert