A two-dimensional array based on a rectangular sequential array

Example:

var o = new 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

new (width:Int, height:Int, ?source:Array<T>)

Creates a two-dimensional array with dimensions width and height.

The minimum size is 2x2.

Parameters:

source

initial values for populating this two-dimensional array;
source.length should match width × height.

Variables

cols:Int

Equals this.width.

height:Int

The height (#rows).

The minimum value is 2.

@:value(HashKey.next())read onlykey:Int = HashKey.next()

A unique identifier for this object.

A hash table transforms this key into an index of an array element by using a hash function.

@:value(false)reuseIterator:Bool = false

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.

rows:Int

Equals this.height.

read onlysize:Int

The number of elements in this two-dimensional array.

Always equals this.cols × this.rows.

width:Int

The width (#columns).

The minimum value is 2.

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.

inlinecall (f:T ‑> Void):Array2<T>

Calls 'f` on all elements in order.

inlinecellOf (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.

inlinecellToIndex (cell:Array2Cell):Int

Computes an array index into the linear array from the given cell coordinates.

@:value({ gc : false })clear (gc:Bool = false):Void

Clears this two-dimensional array by nullifying all elements.

The gc parameter has no effect.

@:value({ copier : null, byRef : true })clone (byRef:Bool = true, ?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.

contains (val:T):Bool

Returns true if this two-dimensional array contains val.

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).

copyCol (i:Int, j:Int):Array2<T>

Copies column elements from column i to column j.

copyRow (i:Int, j:Int):Array2<T>

Copies row elements from row i to row j.

@:value({ manhatten : false })inlinecountNeighbors (x:Int, y:Int, f:T ‑> Bool, manhatten:Bool = false):Int

Counts the number of neighbors at column x and row y, calling f on all adjacent cells. if manhatten is true, only counts in 4 directions (N,S,W,E), otherwise 8 (NW,N,NE,W,E,SW,S,SE).

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

inlineforEach (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).

inlineget (x:Int, y:Int):T

Returns the element that is stored in column x and row y.

inlinegetAtCell (cell:Array2Cell):T

Returns the element that is stored in column cell.x and row cell.y.

inlinegetAtIndex (i:Int):T

Returns the element at index i.

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.

inlinegetData ():NativeArray<T>

Returns a reference to the internal container storing the elements of this collection.

Useful for fast iteration or low-level operations.

inlinegetIndex (x:Int, y:Int):Int

Computes an index into the linear array from the x and y index.

inlinegetIndexAtCell (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.

inlineinRange (x:Int, y:Int):Bool

Returns true if x and y are valid indices.

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].

inlineindexToCell (i:Int, out:Array2Cell):Array2Cell

Transforms the index i into cell coordinates.

Parameters:

out

stores the result.

Returns:

a reference to out.

isEmpty ():Bool

Unsupported operation; always returns false.

inlineiter (f:T ‑> Void):Array2<T>

Calls 'f` on all elements in order.

iterator ():Itr<T>

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.

inlinereplace (x:Int, y:Int, f:T ‑> T):Void

Passes the original value at x,y to f and replaces it with the value returned by f.

Example:

var a = new Array2<Int>(3, 3);
a.replace(1, 2, v -> v + 1); // Haxe4
a.replace(1, 2, function(v) return v + 1);

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).

inlineset (x:Int, y:Int, val:T):Array2<T>

Replaces the element at column x and row y with val.

setAll (val:T):Array2<T>

Sets all elements to val.

inlinesetAtCell (cell:Array2Cell, val:T):Array2<T>

Replaces the element that is stored in column cell.x and row cell.y with val.

inlinesetAtIndex (i:Int, val:T):Array2<T>

Replaces the element at index i with val.

setCol (x:Int, input:Array<T>):Array2<T>

Overwrites all elements in column x with elements from input.

setRow (y:Int, input:Array<T>):Array2<T>

Overwrites all elements in row y with elements from input.

@:value({ wrap : true })shiftDown (wrap:Bool = true):Array2<T>

Shifts all rows down by one position.

Parameters:

wrap

if true rows are wrapped, so row at index [this.rows - 1] is not lost but prepended to the topmost row.

@:value({ wrap : true })shiftLeft (wrap:Bool = true):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.

@:value({ wrap : true })shiftRight (wrap:Bool = true):Array2<T>

Shifts all columns to the right by one position.

Parameters:

wrap

if true columns are wrapped, so the column at index [this.cols - 1] is not lost but prepended to the leftmost column.

@:value({ wrap : true })shiftUp (wrap:Bool = true):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.

@:value({ rvals : null })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 Shuffle.frand().

inlineswap (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.

swapCol (i:Int, j:Int):Array2<T>

Swaps column elements at column i with column elements at row j.

swapRow (i:Int, j:Int):Array2<T>

Swaps row elements at row i with row elements at row j.

toArray ():Array<T>

Returns an array containing all elements in this two-dimensional array.

Order: Row-major order (row-by-row).

toString ():String

Prints out all elements.

transpose ():Array2<T>

Transposes this two-dimensional array.