Hi,
I've been working with tables in Ruby lately using vgvgf's Table class. The advantage of this class is that it can handle a huge amount of info better than the Array class, but it can only contain Fixnum and nothing else. The problem I have with that is that I'd like to store ruby objects into a table, like this:
table[1,2,3] = MyObject.new
It's impossible right now because of the way the Table class is written. But, I was wondering if it is possible to modify the class so that it can support any kind of objects, just like Array and Hash does?
Here's the source code, written by vgvgf.
It's seems that it's the packing of the data that is problematic here. I'm not very familiar with this but it looks relatively simple.
So, is there a way to modify this class to do this?
Thanks,
- Dargor
I've been working with tables in Ruby lately using vgvgf's Table class. The advantage of this class is that it can handle a huge amount of info better than the Array class, but it can only contain Fixnum and nothing else. The problem I have with that is that I'd like to store ruby objects into a table, like this:
table[1,2,3] = MyObject.new
It's impossible right now because of the way the Table class is written. But, I was wondering if it is possible to modify the class so that it can support any kind of objects, just like Array and Hash does?
Here's the source code, written by vgvgf.
[ruby]class Table
def initialize(x, y = 1, z = 1)
@xsize, @ysize, @zsize = x, y, z
@data = Array.new(x * y * z, 0)
end
def [](x, y = 0, z = 0)
@data[x + y * @xsize + z * @xsize * @ysize]
end
def []=(*args)
x = args[0]
y = args.size > 2 ? args[1] :0
z = args.size > 3 ? args[2] :0
v = args.pop
@data[x + y * @xsize + z * @xsize * @ysize] = v
end
def _dump(d = 0)
s = [3].pack('L')
s += [@xsize].pack('L') + [@ysize].pack('L') + [@zsize].pack('L')
s += [@xsize * @ysize * @zsize].pack('L')
for z in 0...@zsize
for y in 0...@ysize
for x in 0...@xsize
s += [@data[x + y * @xsize + z * @xsize * @ysize]].pack('S')
end
end
end
s
end
def self._load(s)
size = s[0, 4].unpack('L')[0]
nx = s[4, 4].unpack('L')[0]
ny = s[8, 4].unpack('L')[0]
nz = s[12, 4].unpack('L')[0]
data = []
pointer = 20
loop do
data.push(*s[pointer, 2].unpack('S'))
pointer += 2
break if pointer > s.size - 1
end
t = Table.new(nx, ny, nz)
n = 0
for z in 0...nz
for y in 0...ny
for x in 0...nx
t[x, y, z] = data[n]
n += 1
end
end
end
t
end
attr_readerxsize, :ysize, :zsize, :data)
end
[/ruby]
def initialize(x, y = 1, z = 1)
@xsize, @ysize, @zsize = x, y, z
@data = Array.new(x * y * z, 0)
end
def [](x, y = 0, z = 0)
@data[x + y * @xsize + z * @xsize * @ysize]
end
def []=(*args)
x = args[0]
y = args.size > 2 ? args[1] :0
z = args.size > 3 ? args[2] :0
v = args.pop
@data[x + y * @xsize + z * @xsize * @ysize] = v
end
def _dump(d = 0)
s = [3].pack('L')
s += [@xsize].pack('L') + [@ysize].pack('L') + [@zsize].pack('L')
s += [@xsize * @ysize * @zsize].pack('L')
for z in 0...@zsize
for y in 0...@ysize
for x in 0...@xsize
s += [@data[x + y * @xsize + z * @xsize * @ysize]].pack('S')
end
end
end
s
end
def self._load(s)
size = s[0, 4].unpack('L')[0]
nx = s[4, 4].unpack('L')[0]
ny = s[8, 4].unpack('L')[0]
nz = s[12, 4].unpack('L')[0]
data = []
pointer = 20
loop do
data.push(*s[pointer, 2].unpack('S'))
pointer += 2
break if pointer > s.size - 1
end
t = Table.new(nx, ny, nz)
n = 0
for z in 0...nz
for y in 0...ny
for x in 0...nx
t[x, y, z] = data[n]
n += 1
end
end
end
t
end
attr_readerxsize, :ysize, :zsize, :data)
end
[/ruby]
It's seems that it's the packing of the data that is problematic here. I'm not very familiar with this but it looks relatively simple.
So, is there a way to modify this class to do this?
Thanks,
- Dargor