jon.recoil.org

Module Base.Option_arraySource

'a Option_array.t is a compact representation of 'a option array: it avoids allocating heap objects representing Some x, usually representing them with x instead. It uses a special representation for None that's guaranteed to never collide with any representation of Some x.

Sourcetype 'a t
include Sexplib0.Sexpable.S1 with type 'a t := 'a Base.Option_array.t
Sourceval t_of_sexp : 'a. (Sexplib0.Sexp.t -> 'a) -> Sexplib0.Sexp.t -> 'a Base.Option_array.t
Sourceval sexp_of_t : 'a. ('a -> Sexplib0.Sexp.t) -> 'a Base.Option_array.t -> Sexplib0.Sexp.t
Sourceval t_sexp_grammar : 'a. 'a Sexplib0.Sexp_grammar.t -> 'a Base.Option_array.t Sexplib0.Sexp_grammar.t @@ portable
Sourceval empty : _ Base.Option_array.t @@ portable
Sourceval get_empty : unit -> _ Base.Option_array.t @@ portable

For obtaining uncontended access to empty from a portable function.

Sourceval create : len:int -> _ Base.Option_array.t @@ portable

Initially filled with all None

include Base.Indexed_container.Generic with type ('a, _, _) t := 'a Base.Option_array.t and type 'a elt := 'a option
include Base.Container.Generic__''value'' with type ('a, _, _) t := 'a Base.Option_array.t with type 'a elt := 'a option
include sig ... end
include sig ... end
include sig ... end
include sig ... end
include sig ... end
include Base.Container.Generic_types__''value'' with type ('a, 'b, 'c) t := 'a Base.Option_array.t with type 'a elt := 'a option

These are all like their equivalents in Container except that an index starting at 0 is added as the first argument to f.

include sig ... end
include sig ... end
val iteri : 'a 'p1 'p2. 'a Base.Option_array.t -> f:(int -> 'a option -> unit) @ local -> unit
val existsi : 'a 'p1 'p2. 'a Base.Option_array.t -> f:(int -> 'a option -> bool) @ local -> bool
val for_alli : 'a 'p1 'p2. 'a Base.Option_array.t -> f:(int -> 'a option -> bool) @ local -> bool
val counti : 'a 'p1 'p2. 'a Base.Option_array.t -> f:(int -> 'a option -> bool) @ local -> int
include sig ... end
include sig ... end
val foldi : 'a 'p1 'p2 'acc. 'a Base.Option_array.t -> init:'acc -> f:(int -> 'acc -> 'a option -> 'acc) @ local -> 'acc
val iteri_until : 'a 'p1 'p2 'final. 'a Base.Option_array.t -> f:(int -> 'a option -> (unit, 'final) Container.Continue_or_stop.t) @ local -> finish:(int -> 'final) @ local -> 'final
val find_mapi : 'a 'p1 'p2 'b. 'a Base.Option_array.t -> f:(int -> 'a option -> 'b option) @ local -> 'b option
include sig ... end
val foldi_until : 'a 'p1 'p2 'acc 'final. 'a Base.Option_array.t -> init:'acc -> f: (int -> 'acc -> 'a option -> ('acc, 'final) Container.Continue_or_stop.t) @ local -> finish:(int -> 'acc -> 'final) @ local -> 'final
include sig ... end
val findi : 'a 'p1 'p2. 'a Base.Option_array.t -> f:(int -> 'a option -> bool) @ local -> (int * 'a option) option
include Base.Container.Generic with type ('a, 'b, 'c) t := 'a Base.Option_array.t and type 'a elt := 'a option
include sig ... end
include sig ... end
val is_empty : 'a 'p1 'p2. 'a Base.Option_array.t -> bool
val iter : 'a 'p1 'p2. 'a Base.Option_array.t -> f:('a option -> unit) @ local -> unit

iter must allow exceptions raised in f to escape, terminating the iteration cleanly. The same holds for all functions below taking an f.

val exists : 'a 'p1 'p2. 'a Base.Option_array.t -> f:('a option -> bool) @ local -> bool

Returns true if and only if there exists an element for which the provided function evaluates to true. This is a short-circuiting operation.

val for_all : 'a 'p1 'p2. 'a Base.Option_array.t -> f:('a option -> bool) @ local -> bool

Returns true if and only if the provided function evaluates to true for all elements. This is a short-circuiting operation.

val count : 'a 'p1 'p2. 'a Base.Option_array.t -> f:('a option -> bool) @ local -> int

Returns the number of elements for which the provided function evaluates to true.

val find : 'a 'p1 'p2. 'a Base.Option_array.t -> f:('a option -> bool) @ local -> 'a option option

Returns as an option the first element for which f evaluates to true.

val to_list : 'a 'p1 'p2. 'a Base.Option_array.t -> 'a option list
val min_elt : 'a 'p1 'p2. 'a Base.Option_array.t -> compare:('a option -> 'a option -> int) @ local -> 'a option option

Returns a min (resp. max) element from the collection using the provided compare function. In case of a tie, the first element encountered while traversing the collection is returned. The implementation uses fold so it has the same complexity as fold. Returns None iff the collection is empty.

val max_elt : 'a 'p1 'p2. 'a Base.Option_array.t -> compare:('a option -> 'a option -> int) @ local -> 'a option option
include sig ... end
val sum : 'a 'sum 'p1 'p2. (module Base__.Container_intf.Definitions.Summable__value_or_null with type t = 'sum) -> 'a Base.Option_array.t -> f:('a option -> 'sum) @ local -> 'sum

Returns the sum of f i for all i in the container. The order in which the elements will be summed is unspecified.

val iter_until : 'a 'p1 'p2 'final. 'a Base.Option_array.t -> f:('a option -> (unit, 'final) Container.Continue_or_stop.t) @ local -> finish:(unit -> 'final) @ local -> 'final

iter_until t ~f ~finish is a short-circuiting version of iter. If f returns Stop x the computation ceases and returns x. If f always returns Continue () the final result is computed by finish.

val fold : 'a 'p1 'p2 'acc. 'a Base.Option_array.t -> init:'acc -> f:('acc -> 'a option -> 'acc) @ local -> 'acc

fold t ~init ~f returns f (... f (f (f init e1) e2) e3 ...) en, where e1..en are the elements of t.

val fold_result : 'a 'p1 'p2 'acc 'e. 'a Base.Option_array.t -> init:'acc -> f:('acc -> 'a option -> ('acc, 'e) Stdlib.result) @ local -> ('acc, 'e) Stdlib.result

fold_result t ~init ~f is a short-circuiting version of fold that runs in the Result monad. If f returns an Error _, that value is returned without any additional invocations of f.

val find_map : 'a 'p1 'p2 'b. 'a Base.Option_array.t -> f:('a option -> 'b option) @ local -> 'b option

Returns the first evaluation of f that returns Some, and returns None if there is no such element.

include sig ... end
val fold_until : 'a 'p1 'p2 'acc 'final. 'a Base.Option_array.t -> init:'acc -> f:('acc -> 'a option -> ('acc, 'final) Container.Continue_or_stop.t) @ local -> finish:('acc -> 'final) @ local -> 'final

fold_until t ~init ~f ~finish is a short-circuiting version of fold. If f returns Stop _ the computation ceases and results in that value. If f returns Continue _, the fold will proceed. If f never returns Stop _, the final result is computed by finish.

Example:

  type maybe_negative =
    | Found_negative of int
    | All_nonnegative of { sum : int }

  (** [first_neg_or_sum list] returns the first negative number in [list], if any,
      otherwise returns the sum of the list. *)
  let first_neg_or_sum =
    List.fold_until ~init:0
      ~f:(fun sum x ->
        if x < 0
        then Stop (Found_negative x)
        else Continue (sum + x))
      ~finish:(fun sum -> All_nonnegative { sum })
  ;;

  let x = first_neg_or_sum [1; 2; 3; 4; 5]
  val x : maybe_negative = All_nonnegative {sum = 15}

  let y = first_neg_or_sum [1; 2; -3; 4; 5]
  val y : maybe_negative = Found_negative -3
include sig ... end
val mem : 'a 'p1 'p2. 'a Base.Option_array.t -> 'a option -> equal:('a option -> 'a option -> bool) @ local -> bool

Checks whether the provided element is there, using equal.

Sourceval length : _ Base.Option_array.t @ local -> int @@ portable
Sourceval init_some : int -> f:(int -> 'a) @ local -> 'a Base.Option_array.t @@ portable
Sourceval init : int -> f:(int -> 'a option) @ local -> 'a Base.Option_array.t @@ portable
Sourceval of_array : 'a option array -> 'a Base.Option_array.t @@ portable
Sourceval of_array_some : 'a array -> 'a Base.Option_array.t @@ portable
Sourceval to_array : 'a Base.Option_array.t -> 'a option Base.Array.t @@ portable
Sourceval get : 'a Base.Option_array.t -> int -> 'a option @@ portable

get t i returns the element number i of array t, raising if i is outside the range 0 to length t - 1.

Sourceval get_or_null : 'a Base.Option_array.t -> int -> 'a Basement.Or_null_shim.t @@ portable

Similar to get, but uses or_null to avoid allocation.

Sourceval get_local : 'a Base.Option_array.t @ local -> int -> 'a option @ local @@ portable

Similar to get, but allocates result in the caller's stack region instead of heap.

Sourceval get_some_exn : 'a Base.Option_array.t -> int -> 'a @@ portable

Raises if the element number i is None.

Sourceval is_none : _ Base.Option_array.t -> int -> bool @@ portable

is_none t i = Option.is_none (get t i)

Sourceval is_some : _ Base.Option_array.t -> int -> bool @@ portable

is_some t i = Option.is_some (get t i)

These can cause arbitrary behavior when used for an out-of-bounds array access.

Sourceval unsafe_get : 'a Base.Option_array.t -> int -> 'a option @@ portable
Sourceval unsafe_get_some_exn : 'a Base.Option_array.t -> int -> 'a @@ portable

unsafe_get_some_exn t i is unsafe because it does not bounds check i. It does, however check whether the value at index i is none or some, and raises if it is none.

Sourceval unsafe_get_some_assuming_some : 'a Base.Option_array.t -> int -> 'a @@ portable

unsafe_get_some_assuming_some t i is unsafe both because it does not bounds check i and because it does not check whether the value at index i is none or some, assuming that it is some.

Sourceval unsafe_is_some : _ Base.Option_array.t -> int -> bool @@ portable
Sourceval set : 'a Base.Option_array.t -> int -> 'a option -> unit @@ portable

set t i x modifies array t in place, replacing element number i with x, raising if i is outside the range 0 to length t - 1.

Sourceval set_some : 'a Base.Option_array.t -> int -> 'a -> unit @@ portable
Sourceval set_none : _ Base.Option_array.t -> int -> unit @@ portable
Sourceval swap : _ Base.Option_array.t -> int -> int -> unit @@ portable
Sourceval clear : _ Base.Option_array.t -> unit @@ portable

Replaces all the elements of the array with None.

Sourceval map : 'a Base.Option_array.t -> f:('a option -> 'b option) @ local -> 'b Base.Option_array.t @@ portable

map f [|a1; ...; an|] applies function f to a1, a2, ..., an, in order, and builds the option_array [|f a1; ...; f an|] with the results returned by f.

Sourceval map_some : 'a Base.Option_array.t -> f:('a -> 'b) @ local -> 'b Base.Option_array.t @@ portable

map_some t ~f is like map, but None elements always map to None and Some always map to Some.

Unsafe versions of set*. Can cause arbitrary behaviour when used for an out-of-bounds array access.

Sourceval unsafe_set : 'a Base.Option_array.t -> int -> 'a option -> unit @@ portable
Sourceval unsafe_set_some : 'a Base.Option_array.t -> int -> 'a -> unit @@ portable
Sourceval unsafe_set_none : _ Base.Option_array.t -> int -> unit @@ portable
include Base.Blit.S1 with type 'a t := 'a Base.Option_array.t
Sourceval blit : src:'a Base.Option_array.t @ local -> src_pos:int -> dst:'a Base.Option_array.t @ local -> dst_pos:int -> len:int -> unit
Sourceval blito : src:'a Base.Option_array.t @ local -> ?src_pos:int -> ?src_len:int -> dst:'a Base.Option_array.t @ local -> ?dst_pos:int -> unit -> unit
Sourceval unsafe_blit : src:'a Base.Option_array.t @ local -> src_pos:int -> dst:'a Base.Option_array.t @ local -> dst_pos:int -> len:int -> unit
Sourceval sub : 'a Base.Option_array.t @ local -> pos:int -> len:int -> 'a Base.Option_array.t
Sourceval subo : ?pos:int -> ?len:int -> 'a Base.Option_array.t @ local -> 'a Base.Option_array.t
Sourceval copy : 'a Base.Option_array.t -> 'a Base.Option_array.t @@ portable

Makes a (shallow) copy of the array.