array package

License

The array package is marked as Compatible, which means any game can use it.

This package contains 30 classes, all of them variations on a theme. You can create a densely packed fixed-size array of either 1, 2, or 3 dimensions, containing Lua numbers, booleans, signed/unsigned 8-, 16-, or 32-bit integers, or 32- or 64-bit floating point values.

ClassDim.Range
PackedArray1D1Dany number
PackedArray1D_B1Dtrue or false
PackedArray1D_S81D-128 <= integer <= 127
PackedArray1D_U81D0 <= integer <= 255
PackedArray1D_S161D-32768 <= integer <= 32767
PackedArray1D_U161D0 <= integer <= 65535
PackedArray1D_S321D-2147483648 <= integer <= 2147483647
PackedArray1D_U321D0 <= integer <= 4294967295
PackedArray1D_F321D-126 <= exponent <= 127, 23+1 digits (binary)
PackedArray1D_F641D-1022 <= exponent <= 1023, 52+1 digits (binary)
PackedArray2D2Dany number
PackedArray2D_B2Dtrue or false
PackedArray2D_S82D-128 <= integer <= 127
PackedArray2D_U82D0 <= integer <= 255
PackedArray2D_S162D-32768 <= integer <= 32767
PackedArray2D_U162D0 <= integer <= 65535
PackedArray2D_S322D-2147483648 <= integer <= 2147483647
PackedArray2D_U322D0 <= integer <= 4294967295
PackedArray2D_F322D-126 <= exponent <= 127, 23+1 digits (binary)
PackedArray2D_F642D-1022 <= exponent <= 1023, 52+1 digits (binary)
PackedArray3D3Dany number
PackedArray3D_B3Dtrue or false
PackedArray3D_S83D-128 <= integer <= 127
PackedArray3D_U83D0 <= integer <= 255
PackedArray3D_S163D-32768 <= integer <= 32767
PackedArray3D_U163D0 <= integer <= 65535
PackedArray3D_S323D-2147483648 <= integer <= 2147483647
PackedArray3D_U323D0 <= integer <= 4294967295
PackedArray3D_F323D-126 <= exponent <= 127, 23+1 digits (binary)
PackedArray3D_F643D-1022 <= exponent <= 1023, 52+1 digits (binary)

Each variant takes up the minimum necessary memory to store its contents, and accesses are performed in O(1) time.

All of the above classes implement the following:

array1d = SubCritical.Construct("PackedArray1D", size) array2d = SubCritical.Construct("PackedArray2D", width, height) array3d = SubCritical.Construct("PackedArray3D", width, height, depth)
Create an array of the given type and dimensions.
value = array1d:Get(x) value = array2d:Get(x, y) value = array3d:Get(x, y, z)
Get the value currently stored at the given location. Unset values default to false or 0, depending on the array's type.
Coordinates start from 0, not 1.
If the given coordinates are outside the array's bounds, returns the border value (see below).
width = array1d:GetSize() width,height = array2d:GetSize() width,height,depth = array3d:GetSize()
Returns the dimensions of the array.
array1d:Set(value, x) array2d:Set(value, x, y) array3d:Set(value, x, y, z)
Change the value currently stored at the given location.
Coordinates start from 0, not 1.
border = array:GetBorder()
Return the value that will currently be returned if an access outside the bounds of the array is attempted.
array:SetBorder(border)
Change the value that will be returned if an access outside the bounds of the array is attempted. Valid values are nil or any number. The default is nil, and the number will not be clamped to the range of the array. (Boolean arrays still have number/nil border values.)
dumped = array:Dump()
Get the array's contents, in big-endian form and packed as tightly as possible, suitable for serialization.
array:Undump(dumped)
Fill the array with a string we got from a previous call to Dump. The string must be exactly the right length, or an error will be thrown.

Here are some examples:

-- Create a PackedArray containing a 32x32 quarter-circle.
array = SubCritical.Construct("PackedArray2D_B", 32, 32)
for x=0,31 do
  for y=0,31 do
    array:Set(math.sqrt(x*x+y*y) <= 31, x, y)
  end
end
-- Create a PackedArray containing a multiplication table.
-- array[x][y] contains (x+1)*(y+1).
-- 12 * 12 = 144, which is less than 256, so our numbers will fit in a U8.
array = SubCritical.Construct("PackedArray2D_U8", 12, 12)
for x=1,12 do
  for y=1,12 do
    array:Set(x*y, x-1, y-1)
  end
end

Back to index