Class: Table
- Inherits:
-
Object
- Object
- Table
- Defined in:
- lib/table.rb
Overview
The multidimensional array class. Each element takes up 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
-
#xsize ⇒ Object
readonly
Returns the value of attribute xsize.
-
#ysize ⇒ Object
readonly
Returns the value of attribute ysize.
-
#zsize ⇒ Object
readonly
Returns the value of attribute zsize.
Instance Method Summary collapse
-
#[](x, y = nil, z = nil) ⇒ Object
Accesses the array’s elements.
- #[]=(*args) ⇒ Object
-
#initialize(xsize, ysize = nil, zsize = nil) ⇒ Table
constructor
A new instance of Table.
-
#resize(xsize, ysize = nil, zsize = nil) ⇒ Object
Change the size of the array.
Constructor Details
#initialize(xsize, ysize = nil, zsize = nil) ⇒ Table
Returns a new instance of Table.
7 8 9 10 11 12 13 14 |
# File 'lib/table.rb', line 7 def initialize(xsize, ysize=nil, zsize=nil) raise "invalid params - expected (xsize[, ysize[, zsize]])" unless params_valid?(xsize, ysize, zsize) @dimensions = dimensions(xsize, ysize, zsize) @items = [] resize(xsize, ysize, zsize) end |
Instance Attribute Details
#xsize ⇒ Object (readonly)
Returns the value of attribute xsize.
5 6 7 |
# File 'lib/table.rb', line 5 def xsize @xsize end |
#ysize ⇒ Object (readonly)
Returns the value of attribute ysize.
5 6 7 |
# File 'lib/table.rb', line 5 def ysize @ysize end |
#zsize ⇒ Object (readonly)
Returns the value of attribute zsize.
5 6 7 |
# File 'lib/table.rb', line 5 def zsize @zsize end |
Instance Method Details
#[](x, y = nil, z = nil) ⇒ Object
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.
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/table.rb', line 27 def [](x, y=nil, z=nil) raise unless params_valid?(x, y, z) raise "wrong # of indecies" if dimensions(x, y, z) != @dimensions return nil unless (0..@xsize).include?(x || 0) and (0..@ysize).include?(y || 0) and (0..@zsize).include?(z || 0) case @dimensions when 1 return @items[x] when 2 return @items.fetch(x){ [] }[y] when 3 return @items.fetch(x){ [] }.fetch(y){ [] }[z] end end |
#[]=(*args) ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/table.rb', line 45 def []=(*args) raise "wrong # of indecies" if not args.size == (@dimensions + 1) x, y, z = args.first(@dimensions) raise if not params_valid?(x, y, z) raise "wrong # of indecies" if dimensions(x, y, z) != @dimensions item = args[-1] case @dimensions when 1 @items[x] = item when 2 @items.fetch(x){ @items[x]=[] }[y] = item when 3 @items.fetch(x){ @items[x]=[] }.fetch(y){ @items[x][y]=[] }[z] = item end end |
#resize(xsize, ysize = nil, zsize = nil) ⇒ Object
Change the size of the array. All data from before the size change is retained.
17 18 19 20 21 22 23 |
# File 'lib/table.rb', line 17 def resize(xsize, ysize=nil, zsize=nil) raise "invalid params - expected (xsize[, ysize[, zsize]])" unless params_valid?(xsize, ysize, zsize) raise "wrong # of sizes" if dimensions(xsize, ysize, zsize) != @dimensions # Set and coerce the nsize variables @xsize, @ysize, @zsize = xsize || 1, ysize || 1, zsize || 1 end |