Ok Trickster, I see now that my original submissions are redundant. This time however I added to the Table class and scripted a descendant Object_Table class. It stores the objects numerical id then retrieves the original object using the included module
ObjectSpace's _id2ref method. However, since object ids are often greater than 32,767 I had to create a secondary Table to properly store the values. So, what's the verdict on this one?
[spoiler=RGSS.Table]
#==============================================================================
# ** RGSS.Table
#------------------------------------------------------------------------------
# Description:
# ------------
# Adds new features to the Table class. [] and []= methods now accept
# coordinate key arrays and negative coordinate indexes. It also adds
# iteration methods, the Enumerable module and several miscellaneous methods
# all objects in the array are of class Numeric.
#
# Modified Methods:
# -----------------
# initialize
# []
# []=
# _dump
# self._load
#
# Methods Added:
# ------------
# Enumerable (see below)
# each
# each_coordinates
# each_with_coordinates
# coordinates
# each_index
# index
# clear
# nitems
# compact
# compact!
# plane
# column
# row
#==============================================================================
#==============================================================================
# ** Table
#==============================================================================
class Table
#-------------------------------------------------------------------------
# Include
# * Names : all?, any?, collect, map, each_with_index, find, detect,
# find_all, select, grep, inject, member?, include?, max,
# min, partition, reject, sort, sort_by, to_a, entries, zip
#-------------------------------------------------------------------------
include Enumerable
#--------------------------------------------------------------------------
# * Public Instance Variables
#--------------------------------------------------------------------------
attr_accessor :negitive_read # allow negitive index reading?
#--------------------------------------------------------------------------
# * Object Initialization
#--------------------------------------------------------------------------
alias_method :icarus_table_initialize, :initialize
def initialize(*args)
icarus_table_initialize(*args)
# Default value for negitive index reading
@negitive_read = true
# Store number of axis for each coordinate method
@axis = args.size
end
#-------------------------------------------------------------------------
# * []
#-------------------------------------------------------------------------
alias_method :icarus_table_nth, :[]
def [](*args)
# Remove nil arguments from the end of the array
args.delete_at(-1) while args[-1].nil?
# If negitive reading is enabled
if @negitive_read
# Modify arguments if they are negative indexes
args[0] += self.xsize unless args[0] >= 0
args[1] += self.ysize unless args[1].nil? or args[1] >= 0
args[2] += self.zsize unless args[2].nil? or args[2] >= 0
end
# Call original method with modified arguments
return icarus_table_nth(*args)
end
#-------------------------------------------------------------------------
# * []=
#-------------------------------------------------------------------------
alias_method :icarus_table_nthequals, :[]=
def []=(*args)
# Modify arguments if they are negative indexes
args[0] += self.xsize unless args[0] >= 0
args[1] += self.ysize unless args[2].nil? or args[1] >= 0
args[2] += self.zsize unless args[3].nil? or args[2] >= 0
# Call original method with modified arguments
icarus_table_nthequals(*args)
end
#-------------------------------------------------------------------------
# * _dump
#-------------------------------------------------------------------------
alias_method :icarus_table_dump, :_dump
def _dump(i)
return icarus_table_dump(i) + '~~' +
Marshal.dump(@negitive_read) + '~~' + Marshal.dump(@axis)
end
#-------------------------------------------------------------------------
# * self._load
#-------------------------------------------------------------------------
class << self
alias_method :icarus_table_load, :_load
end
def self._load(string)
array = string.split('~~')
table = self.icarus_table_load(array[0])
unless array[1].nil?
table.instance_variable_set(:@negitive_read, Marshal.load(array[1]))
table.instance_variable_set(:@axis, Marshal.load(array[2]))
end
return table
end
#-------------------------------------------------------------------------
# * Name : Each
# Info : Iterates through all coordinates, yielding each value
# Author : Icarus Featherfight
# Call Info : A block
# Comments : Provides the basis for included Enumerable methods
#-------------------------------------------------------------------------
def each
each_coordinates{|key| yield self[*key]}
end
#-------------------------------------------------------------------------
# * Name : Each Coordinates
# Info : Iterates through all coordinates in the table
# Author : Icarus Featherfight
# Call Info : A block
# Comments : See Each or Coordinates methods for proper block setup
#-------------------------------------------------------------------------
def each_coordinates
for x in 0...self.xsize
if @axis == 1
yield [x]
else
for y in 0...self.ysize
if @axis == 2
yield [x, y]
else
for z in 0...self.zsize
yield [x, y, z]
end
end
end
end
end
return nil
end
#-------------------------------------------------------------------------
# * Name : Each With Coordinates
# Info : Iterates through table, yielding each value and key
# Author : Icarus Featherfight
# Call Info : One numeric value to be found
#-------------------------------------------------------------------------
def each_with_coordinates
each_coordinates{|key| yield (self[*key], key)}
end
#-------------------------------------------------------------------------
# * Name : Coordinates
# Info : Finds the first coordinates to contain a value
# Author : Icarus Featherfight
# Call Info : One numeric value to be found
#-------------------------------------------------------------------------
def coordinates(value)
each_coordinates{|key| return key if self[*key] == value}
end
#-------------------------------------------------------------------------
# * Name : Each Index
# Info : Iterates through all indexes in a table array
# Author : Icarus Featherfight
# Call Info : A block
#-------------------------------------------------------------------------
def each_index
self.to_a.each_index{|index| yield(index)}
end
#-------------------------------------------------------------------------
# * Name : Index
# Info : Finds the first index to contain a value
# Author : Icarus Featherfight
# Call Info : One numeric value to be found
#-------------------------------------------------------------------------
def index(value)
each_with_index{|val, index| return index if val == value}
end
#-------------------------------------------------------------------------
# * Name : Clear
# Info : Reinitializes table
# Author : Icarus Featherfight
# Call Info : No arguments
# Comments : See Array.clear
#-------------------------------------------------------------------------
def clear
# Reset table
if self.ysize == 1 then initialize(self.xsize)
elsif self.zsize == 1 then initialize(self.xsize, self.ysize)
else initialize(self.xsize, self.ysize, self.zsize)
end
return self
end
#-------------------------------------------------------------------------
# * Name : NItems
# Info : Iterates through index, counting all non-zero elements
# Author : Icarus Featherfight
# Call Info : No arguments
#-------------------------------------------------------------------------
def nitems
n = 0
each{|value| n += 1 unless value == false}
return n
end
#-------------------------------------------------------------------------
# * Name : Compact
# Info : Returns a new table with nil elements removed from positive
# extremities of table
# Author : Icarus Featherfight
# Call Info : No arguments
#-------------------------------------------------------------------------
def compact
return self.dup.compact!
end
#-------------------------------------------------------------------------
# * Name : Compact!
# Info : Removes nil elements from positive extremities of table
# Author : Icarus Featherfight
# Call Info : No arguments
#-------------------------------------------------------------------------
def compact!
if self.zsize == 1
x = self.xsize
y = self.ysize
x -= 1 while row(x - 1).all?{|item| item.nil?} && x > 1
y -= 1 while column(y - 1).all?{|item| item.nil?} && y > 1
resize(x, y)
else
planes = []
for z in 0...self.zsize
planes[z] = plane(z).compact!
end
planes.delete_at(-1) while planes[-1].all?{|item| item.nil?}
xmax, ymax = 1, 1
planes.each{|table|
xmax = table.xsize if xmax < table.xsize
ymax = table.ysize if ymax < table.ysize
}
resize(xmax, ymax, planes.size)
end
return self
end
#-------------------------------------------------------------------------
# * Name : Plane
# Info : On a 3D table returns the 2D plane at z value
# Author : Icarus Featherfight
# Call Info : A numerical z
#-------------------------------------------------------------------------
def plane(z)
return if self.ysize == 1 || self.zsize == 1 || z.nil? || z >= self.zsize
if self.class == Object_Table
plane = Object_Table.new(self.xsize, self.ysize)
else
plane = Table.new(self.xsize, self.ysize)
end
for x in 0...self.xsize
for y in 0...self.ysize
plane[x, y] = self[x, y, z]
end
end
return plane
end
#-------------------------------------------------------------------------
# * Name : Column
# Info : On a 3D or 2D table returns the array at y and z values.
# Author : Icarus Featherfight
# Call Info : A numerical y and an optional z for 3D tables
#-------------------------------------------------------------------------
def column(x, z = nil)
column = []
for y in 0...self.ysize
column[y] = self[x, y, z]
end
end
#-------------------------------------------------------------------------
# * Name : Row
# Info : On a 3D or 2D table returns the array at x and z values.
# Author : Icarus Featherfight
# Call Info : A numerical x and an optional z for 3D tables
#-------------------------------------------------------------------------
def row(y, z = nil)
row = []
for x in 0...self.xsize
row[x] = self[x, y, z]
end
end
end
Expand to see the code.