Neo-Bahamut
Member
MultiArray Version: 2.2
By: Neo-Bahamut
Introduction
I think most of you scripters know what a Table and an Array is, right?
Both have advantages and disadantages. Tables can only hold Integers and Arrays have only one dimension.
This script creates the MultiArray class which is like an Array but can have any number of dimensions.
Methods:
Script
Instructions
MultiArrays have a fixed size as Tables. They also can't be too big (too big means each size multiplicated is a Bignum)
FAQ
Nothing asked
Compatibility
Should be compatibel with anything that doesn't make a MultiArray-class
Terms and Conditions
I think this will be only used for writing scripts and not just be put into a game ;D
So if you use this as a help for your script, please write me with you in the Credits (or at least to "Thanks")
If somebody uses a script which uses this one, please add me too to your credits.
By: Neo-Bahamut
Introduction
I think most of you scripters know what a Table and an Array is, right?
Both have advantages and disadantages. Tables can only hold Integers and Arrays have only one dimension.
This script creates the MultiArray class which is like an Array but can have any number of dimensions.
Methods:
- MultiArray.new(D1, D2, D3, D4...) (creates a new MultiArray)
- [1, 2, 3, 4] (returns the value at that position)
- [1, 2, 3, 4] = object (sets the value at that position to object)
- .fill(object[, duplicate = true]) fills each position with object. If duplicate is true, the .dup method of object is called evertime
- all methods of Enumerable
Script
Code:
#============================================================================
# ** MultiArray
#----------------------------------------------------------------------------
# Neo-Bahamut
# V 2.4
# 23.06.2009
#============================================================================
#=============================================================================
# ** MultiArray
#-----------------------------------------------------------------------------
# Some kind of Table-Array-Fusion.
#=============================================================================
class MultiArray
include Enumerable
SizeError = Exception.new("Too large amount of data.")
CopyError = Exception.new("Object can not be duplicated.")
RangeError = Exception.new("Index out of Range.")
#--------------------------------------------------------------------------
# * Initialization
#--------------------------------------------------------------------------
def initialize(*args)
@sizes = args
size = 1
args.each {|n| size *= n}
if size.is_a?(Bignum)
raise SizeError
end
@array = Array.new(size)
end
#--------------------------------------------------------------------------
# * Gets an element.
#--------------------------------------------------------------------------
def [](*args)
raise RangeError if args.size > @sizes.size
args.each_index {|n| if args[n] >= @sizes[n] then raise RangeError end}
index = 0
for n in 0...args.size
i = args[n]
for m in [email=0...@sizes.size]0...@sizes.size[/email]
break if m >= n
i *= @sizes[m]
end
index += i
end
return @array[index]
end
#--------------------------------------------------------------------------
# * Changes an element.
#--------------------------------------------------------------------------
def []=(*args)
object = args.pop
raise RangeError if args.size > @sizes.size
args.each_index {|n| if args[n] >= @sizes[n] then raise RangeError end}
index = 0
for n in 0...args.size
i = args[n]
for m in [email=0...@sizes.size]0...@sizes.size[/email]
break if m >= n
i *= @sizes[m]
end
index += i
end
@array[index] = object
end
#--------------------------------------------------------------------------
# * Method description
# obj: The Object the Array will be filled with.
# dup: If true, obj is duplicated for each tile
#--------------------------------------------------------------------------
def fill(obj, dup = true)
begin
for n in [email=0...@array.size]0...@array.size[/email]
i = dup ? obj.dup : obj
@array[n] = i
end
rescue
raise CopyError
end
end
#--------------------------------------------------------------------------
# * Iterates for each element.
#--------------------------------------------------------------------------
def each(&block)
@array.each(&block)
end
end
Instructions
Code:
marray = MultiArray.new(1, 2, 3)
marray[1, 2, 2] = 1
p marray[1, 2, 2] # => 1
MultiArrays have a fixed size as Tables. They also can't be too big (too big means each size multiplicated is a Bignum)
FAQ
Nothing asked
Compatibility
Should be compatibel with anything that doesn't make a MultiArray-class
Terms and Conditions
I think this will be only used for writing scripts and not just be put into a game ;D
So if you use this as a help for your script, please write me with you in the Credits (or at least to "Thanks")
If somebody uses a script which uses this one, please add me too to your credits.