A two-dimensional array based on a rectangular sequential array
Example:
var o = new de.polygonal.ds.Array2<String>(3, 3);
o.forEach(function(_, x, y) return '$x,$y');
trace(o); //outputs:
[ Array2 cols=3 rows=3
0 -> 0,0 | 1,0 | 2,0
1 -> 0,1 | 1,1 | 2,1
2 -> 0,2 | 1,2 | 2,2
]
Constructor
Variables
read onlykey:Int
A unique identifier for this object.
A hash table transforms this key into an index of an array element by using a hash function.
reuseIterator:Bool
If true, reuses the iterator object instead of allocating a new one when calling this.iterator().
The default is false.
If this value is true, nested iterations will fail as only one iteration is allowed at a time.
read onlysize:Int
Methods
appendCol (input:Array<T>):Array2<T>
Appends the elements of the input array in the range [0, this.rows] by adding a new column.
appendRow (input:Array<T>):Array2<T>
Appends the elements of the input array in the range [0, this.cols] by adding a new row.
inline cellOf (val:T, out:Array2Cell):Array2Cell
Returns the cell coordinates of the first occurrence of val or null if val does not exist.
Parameters:
out | stores the result. |
|---|
Returns:
a reference to out.
inline cellToIndex (cell:Array2Cell):Int
Computes an array index into the linear array from the given cell coordinates.
clear (?gc:Bool):Void
Clears this two-dimensional array by nullifying all elements.
The gc parameter has no effect.
clone (?byRef:Bool, ?copier:T ‑> T):Collection<T>
Creates and returns a shallow copy (structure only - default) or deep copy (structure & elements) of this two-dimensional array.
If byRef is true, primitive elements are copied by value whereas objects are copied by reference.
If byRef is false, the copier function is used for copying elements. If omitted, clone() is called on each element assuming all elements implement Cloneable.
copy (src:Array2<T>, srcX:Int, srcY:Int, dstX:Int, dstY:Int):Array2<T>
Copies all elements from src at position (srcX,srcY) to this two-dimensional array at position (dstX,dstY).
inline countNeighbors (x:Int, y:Int, f:T ‑> Bool):Int
Counts the number of neighbors at column x and row y, calling f on all adjacent cells.
Example:
var a = new Array2<Int>(3, 3);
a.set(0, 0, 1);
a.set(2, 1, 1);
var count = a.countNeighbors(1, 1, function(value) return value == 1); //outputs 2
inline forEach (f:T ‑> Int ‑> Int ‑> T):Array2<T>
Calls f on all elements.
The function signature is: f(input, x, y):output
- input: element at (x,y)
- x: current x index
- y: current y index
- output: element to be stored at (x,y)
free ():Void
Destroys this object by explicitly nullifying all elements for GC'ing used resources.
Improves GC efficiency/performance (optional).
inline getAtCell (cell:Array2Cell):T
Returns the element that is stored in column cell.x and row cell.y.
getCol (x:Int, out:Array<T>):Array<T>
Copies all elements stored in column x by reference to the out array.
Parameters:
out | stores the result. |
|---|
Returns:
a reference to the out array.
inline getData ():NativeArray<T>
Returns a reference to the internal container storing the elements of this collection.
Useful for fast iteration or low-level operations.
inline getIndexAtCell (cell:Array2Cell):Int
Computes an index into the linear array for the given cell.
getRect (minX:Int, minY:Int, maxX:Int, maxY:Int, out:Array<T>):Array<T>
Copies all elements inside the rectangular region bounded by [minX, minY] and [maxX, maxY] by reference to the out array.
Parameters:
out | stores the result. |
|---|
Returns:
a reference to the out array.
getRow (y:Int, out:Array<T>):Array<T>
Copies all elements stored in row y by reference to the out array.
Parameters:
out | stores the result. |
|---|
Returns:
a reference to the out array.
indexOf (val:T):Int
Returns the index of the first occurrence of val or returns -1 if val does not exist.
The index is in the range [0, this.size - 1].
inline indexToCell (i:Int, out:Array2Cell):Array2Cell
Transforms the index i into cell coordinates.
Parameters:
out | stores the result. |
|---|
Returns:
a reference to out.
Returns a new Array2Iterator object to iterate over all elements contained in this two-dimensional array.
Order: Row-major order (row-by-row).
See:
ofNestedArray (input:Array<Array<T>>):Array2<T>
Copies all elements from the nested two-dimensional array input into this two-dimensional array by reference.
prependCol (input:Array<T>):Array2<T>
Prepends the elements of the input array in the range [0, this.rows] by adding a new column.
prependRow (input:Array<T>):Array2<T>
Prepends the elements of the input array in the range [0, this.cols] by adding a new row.
remove (val:T):Bool
Nullifies all occurrences of val.
The size is not altered.
Returns:
true if at least one occurrence of val was nullified.
resize (width:Int, height:Int):Array2<T>
Resizes this two-dimensional array.
Parameters:
width | the new width (minimum is 2). |
|---|---|
height | the new height (minimum is 2). |
inline setAtCell (cell:Array2Cell, val:T):Array2<T>
Replaces the element that is stored in column cell.x and row cell.y with val.
setCol (x:Int, input:Array<T>):Array2<T>
Overwrites all elements in column x with elements from input.
shiftDown (?wrap:Bool):Array2<T>
Shifts all rows down by one position.
Parameters:
wrap | if true rows are wrapped, so row at index [ |
|---|
shiftLeft (?wrap:Bool):Array2<T>
Shifts all columns to the left by one position.
Parameters:
wrap | if true columns are wrapped so the column at index 0 is not lost but appended to the rightmost column. |
|---|
shiftRight (?wrap:Bool):Array2<T>
Shifts all columns to the right by one position.
Parameters:
wrap | if true columns are wrapped, so the column at index [ |
|---|
shiftUp (?wrap:Bool):Array2<T>
Shifts all rows up by one position.
Parameters:
wrap | if true rows are wrapped, so the row at index 0 is not lost but appended to the bottommost row. |
|---|
shuffle (?rvals:Array<Float>):Array2<T>
Shuffles the elements of this collection by using the Fisher-Yates algorithm.
Parameters:
rvals | a list of random double values in the interval [0, 1) defining the new positions of the elements.
If omitted, random values are generated on-the-fly by calling |
|---|
inline swap (x0:Int, y0:Int, x1:Int, y1:Int):Array2<T>
Swaps the element at column/row x0, y0 with the element at column/row x1, y1.