Class: Table

Inherits:
Object
  • Object
show all
Defined in:
lib/openrgss/table.rb

Overview

The multidimensional array class. Each element is an integer of 2 signed bytes ranging from -32,768 to 32,767.

Ruby’s Array class does not run efficiently when handling large amounts of data, hence the inclusion of this class.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(xsize, ysize = 1, zsize = 1) ⇒ Table

Creates a Table object. Specifies the size of each dimension in the multidimensional array. 1-, 2-, and 3-dimensional arrays are possible. Arrays with no parameters are also permitted.



10
11
12
13
14
15
# File 'lib/openrgss/table.rb', line 10

def initialize(xsize, ysize=1, zsize=1)
  @xsize = xsize
  @ysize = ysize
  @zsize = zsize
  @data  = Array.new(@xsize*@ysize*@zsize, 0)
end

Instance Attribute Details

#xsizeObject (readonly)

Returns the value of attribute xsize.



6
7
8
# File 'lib/openrgss/table.rb', line 6

def xsize
  @xsize
end

#ysizeObject (readonly)

Returns the value of attribute ysize.



6
7
8
# File 'lib/openrgss/table.rb', line 6

def ysize
  @ysize
end

#zsizeObject (readonly)

Returns the value of attribute zsize.



6
7
8
# File 'lib/openrgss/table.rb', line 6

def zsize
  @zsize
end

Class Method Details

._load(s) ⇒ Object

:nodoc:



41
42
43
44
45
46
# File 'lib/openrgss/table.rb', line 41

def self._load(s) #:nodoc:
  Table.new(1).instance_eval {
    @size, @xsize, @ysize, @zsize, xx, *@data = s.unpack('LLLLLS*')
    self
  }
end

Instance Method Details

#[](x, y = 0, z = 0) ⇒ Object

:call-seq: self self[x, y] self[x, y, z]

Accesses the array’s elements. Pulls the same number of arguments as there are dimensions in the created array. Returns nil if the specified element does not exist.



30
31
32
33
# File 'lib/openrgss/table.rb', line 30

def [](x, y=0, z=0)
  return nil if x >= @xsize or y >= @ysize
  @data[x + y * @xsize + z * @xsize * @ysize]
end

#[]=(x, y = 0, z = 0, v) ⇒ Object

:nodoc:



35
36
37
# File 'lib/openrgss/table.rb', line 35

def []=(x, y=0, z=0, v) #:nodoc:
  @data[x + y * @xsize + z * @xsize * @ysize]=v
end

#_dump(d = 0) ⇒ Object

:nodoc:



48
49
50
# File 'lib/openrgss/table.rb', line 48

def _dump(d = 0) #:nodoc:
  [@size, @xsize, @ysize, @zsize, @xsize*@ysize*@zsize, *@data].pack('LLLLLS*')
end

#resize(xsize, ysize = 1, zsize = 1) ⇒ Object

Changes the size of the array. All data from before the size change is retained.



19
20
21
# File 'lib/openrgss/table.rb', line 19

def resize(xsize, ysize=1, zsize=1)

end