Class Shape
Implementation of Shape to control the size of arrays (@see Array)
A helper class for managing the size of Arrays.
Having the Shape of an array in a separate class makes it much easier to implement a flexible interface for interacting with Arrays.
A Shape is basically a table which defines the size of the Array,
the dimensions of the Array is #Shape
while each axis size may
be queried by Shape[axis]
.
Additionally a Shape may have a single dimension with size 0
which
may only be used to align two shapes, i.e. the 0
axis is inferred
from the total size of the aligning Shape.
Methods
Shape:initialize (...) | Initialization routine for the Shape object. |
Shape:copy () | Copy the shape by duplicating the dimension sizes |
Shape:reverse () | Reverse the dimension sizes, Shape( 2, 3, 4):reverse() == Shape( 4, 3, 2) |
Shape:size ([axis=0]) | Query the size of the Shape, either a given dimension, or the total size |
Shape:remove (axis) | Removes a dimension from the Shape |
Shape:zero () | Query index of the first zero index |
Shape:align (other) | Return a new shape such that the shapes are equal in size In case either shape has a 0-size dimension that size will be calculated so that the total size is the same. |
Metamethods
Shape:__tostring () | Convert the Shape to a pretty-printed string |
Shape:__eq (a, b) | Checks whether two Shapes are the same (with respect to dimensions) |
Methods
- Shape:initialize (...)
-
Initialization routine for the Shape object. Examples:
Shape(2, 3) -- a shape with 2 dimensions of given sizes Shape(2, 3, 4) -- a shape with 3 dimensions
Parameters:
- ... a comma-separated list of integers
Returns:
-
a new Shape object with the given shape
- Shape:copy ()
-
Copy the shape by duplicating the dimension sizes
Returns:
-
a new Shape with the same content
- Shape:reverse ()
-
Reverse the dimension sizes,
Shape( 2, 3, 4):reverse() == Shape( 4, 3, 2)
Returns:
-
a new Shape
- Shape:size ([axis=0])
-
Query the size of the Shape, either a given dimension, or the total size
Parameters:
- axis int the dimension one wish to query (0 for total) (default 0)
Returns:
-
the size of the dimension (as an integer)
- Shape:remove (axis)
-
Removes a dimension from the Shape
Parameters:
- axis
Returns:
-
a new shape with the given axis removed
- Shape:zero ()
-
Query index of the first zero index
Returns:
-
the first axis with a zero size, if none,
0
is returned - Shape:align (other)
-
Return a new shape such that the shapes are equal in size
In case either shape has a 0-size dimension that size will be
calculated so that the total size is the same.
Parameters:
- other the shape to compare with
Returns:
-
a new Shape which is a copy of
other
if they already are aligned