Utility class for working with Arrays.

Static methods

@:has_untypedstaticinlinealloc<T> (len:Int):Array<T>

Allocates an array with length len.

staticbinarySearchCmp<T> (a:Array<T>, x:T, min:Int, max:Int, comparator:T ‑> T ‑> Int):Int

Searches the sorted array src for val in the range [min, max] using the binary search algorithm.

Returns:

the array index storing val or the bitwise complement (~) of the index where val would be inserted (guaranteed to be a negative number).
The insertion point is only valid for min = 0 and max = src.length - 1.

staticbinarySearchf (a:Array<Float>, x:Float, min:Int, max:Int):Int

Searches the sorted array src for val in the range [min, max] using the binary search algorithm.

Returns:

the array index storing val or the bitwise complement (~) of the index where val would be inserted (guaranteed to be a negative number).
The insertion point is only valid for min = 0 and max = src.length - 1.

staticbinarySearchi (a:Array<Int>, x:Int, min:Int, max:Int):Int

Searches the sorted array src for val in the range [min, max] using the binary search algorithm.

Returns:

the array index storing val or the bitwise complement (~) of the index where val would be inserted (guaranteed to be a negative number).
The insertion point is only valid for min = 0 and max = src.length - 1.

staticblit<T> (src:Array<T>, srcPos:Int, dst:Array<T>, dstPos:Int, n:Int):Void

Copies n elements from src, beginning at srcPos to dst, beginning at dstPos.

Copying takes place as if an intermediate buffer was used, allowing the destination and source to overlap.

staticinlinebruteforce<T> (input:Array<T>, visit:T ‑> T ‑> Void):Void

Brute-force search (aka exhaustive search). Calls visit on all pairs in input.

The function signature is: visit(firstElementInPair, otherElementInPair)

Example:

var elements = ["a", "b", "c"];
ArrayTools.bruteforce(elements, function(a, b) trace('($a,$b)')); //outputs (a,b), (a,c), (b,c);

staticequals<T> (a:Array<T>, b:Array<T>, eq:T ‑> T ‑> Bool):Bool

Compares the elements of a and b by using the given eq function.

staticinlineforEach<T> (src:Array<T>, f:T ‑> Int ‑> T):Void

Calls f on all elements.

The function signature is: f(input, index):output

  • input: current element
  • index: element index [0, src.length)
  • output: element to be stored at given index

staticinlinegetFront<T> (array:Array<T>, index:Int):T

Gets the element at index index, then exchanges it with element at the front of array (i.e. at index 0). Used to facilitate fast lookups of array elements that are frequently used.

@:value({ n : 0, first : 0 })staticinit<T> (a:Array<T>, val:T, first:Int = 0, n:Int = 0):Array<T>

Sets n elements in a to val starting at index first and returns a. If n is zero, n is set to the length of a.

@:value({ n : 0 })staticinlineiter<T> (src:Array<T>, f:T ‑> Void, n:Int = 0):Void

Calls 'f on all elements in the interval &#91;0, n&#41; in order. If n is omitted, n is set to src`.length.

staticinlinepairwise<T> (input:Array<T>, visit:Int ‑> T ‑> T ‑> Void):Void

Visits all elements in input as a pair by calling visit.

The function signature is: visit(currentPairIndex, firstPairElement, secondPairElement)

Example:

var points = [1.1, 1.2, 2.1, 2.2]; //format: [x0, y0, x1, y1, xn, yn, ...]
ArrayTools.pairwise(points, function(i, x, y) trace('point $i: x=$x, y=$y'));

staticquickPerm (n:Int):Array<Array<Int>>

A quick counting permutation algorithm, where n is the number of elements to permute.

@:value({ rvals : null })staticshuffle<T> (a:Array<T>, ?rvals:Array<Float>):Void

Shuffles the elements of the array a by using the Fisher-Yates algorithm.

Parameters:

rvals

a list of random double values in the range between [0, 1) defining the new positions of the elements. If omitted, random values are generated on-the-fly by calling Shuffle.frand().

staticsortRange (a:Array<Float>, cmp:Float ‑> Float ‑> Int, useInsertionSort:Bool, first:Int, n:Int):Void

Sorts the elements of the array a by using the quick sort algorithm.

Parameters:

cmp

a comparison function.

useInsertionSort

if true, the array is sorted using the insertion sort algorithm. This is faster for nearly sorted lists.

first

sort start index. The default value is 0.

n

the number of elements to sort (range: [first, first + n]). If omitted, n is set to a.length.

staticsplit<T> (a:Array<T>, n:Int, k:Int):Array<Array<T>>

Splits the input array a storing n elements into smaller chunks, each containing k elements.

staticinlineswap<T> (array:Array<T>, a:Int, b:Int):Void

Swaps the elements of array at indices a and b.

@:has_untypedstaticinlinetrim<T> (a:Array<T>, len:Int):Array<T>

Shrinks a to length len and returns a.