Module ArrayExtras


module ArrayExtras: sig  end
Utility functions for dealing with arrays



Searching


val find_index : ('a -> bool) -> 'a array -> int
find_index f arr Returns the index of the first element in the array that satisfies the predicate
Raises Not_found if f never returns true
val find_index_from : ('a -> bool) -> 'a array -> int -> int
Same as find_index, but starts looking at the given index.
Raises Not_found if f never returns true


Searching sorted arrays

These modules provide efficient O(lg n) searching of sorted arrays. Rather than having every function take a comparison-function argument, functors are used.

module type Comparable = sig  end
Modules of this type provide functions to test the ordering of elements
module Sorted: functor (Comp : Comparable) -> sig  end
The actual functions for searching in sorted arrays


Stepping through elements


val ensure : ('a -> bool) -> 'a array -> bool
ensure f a returns true if f is true for all elements in a. It stops after the first false result, making it more efficient than Array.fold_left for validation.
val ensure_range : ('a -> bool) -> 'a array -> int -> int -> bool
ensure_range f a i len applies f to the len-gth elements in a starting at index i and returns true if f is true for all the elements, otherwise false.


Transformation


val swap : 'a array -> int -> int -> unit
Swap two elements of an array
val rev : 'a array -> unit
Reverses the elements of the array


Stacks



Normally you'd use lists for things like this, but these functions are useful once in a while.

val pop : 'a array -> 'a * 'a array
Returns the first element of the array and a new array with the rest of the elements of the original.
val push : 'a -> 'a array -> 'a array
Returns a new array with the element pushed onto the front of it.


Sorting


val munge : cmp:('a -> 'a -> int) -> f:('b -> 'a) -> 'b array -> 'b array
Implements munging/Schwartzian transform on an array. Maps 'a array to 'b array and returns the original 'a array ordered by a sort of 'b array.
val stable_munge : cmp:('a -> 'a -> int) -> f:('b -> 'a) -> 'b array -> 'b array
The same using a stable sort
val fast_munge : cmp:('a -> 'a -> int) -> f:('b -> 'a) -> 'b array -> 'b array
The same using the fastest array sorting