package core_kernel

  1. Overview
  2. Docs
Legend:
Library
Module
Module type
Parameter
Class
Class type

A growable array of 'a. Designed for efficiency and simplicity.

This interface is generated lazily: if you need a standard function we haven't added, feel free to add or ping the authors.

By default, Vec operations use integers as indices. The functor Make can be used to create a specialized version from any module implementing Intable.S.

type 'a t
include Ppx_compare_lib.Comparable.S1 with type 'a t := 'a t
val compare : ('a -> 'a -> int) -> 'a t -> 'a t -> int
include Ppx_compare_lib.Equal.S1 with type 'a t := 'a t
val equal : ('a -> 'a -> bool) -> 'a t -> 'a t -> bool
include Sexplib0.Sexpable.S1 with type 'a t := 'a t
val t_of_sexp : (Sexplib0.Sexp.t -> 'a) -> Sexplib0.Sexp.t -> 'a t
val sexp_of_t : ('a -> Sexplib0.Sexp.t) -> 'a t -> Sexplib0.Sexp.t
include Core.Invariant.S1 with type 'a t := 'a t
val invariant : ('a -> unit) -> 'a t -> unit
val create : ?initial_capacity:int -> unit -> 'a t
val init : int -> f:(int -> 'a) -> 'a t

init n ~f returns a fresh vector of length n, with element number i initialized to the result of f i. In other words, init n ~f tabulates the results of f applied to the integers 0 to n-1.

Raise Invalid_argument if n < 0.

val get : 'a t -> int -> 'a

Raises if the index is invalid.

val maybe_get : 'a t -> int -> 'a option
val maybe_get_local : 'a t -> int -> 'a Gel.t option
val set : 'a t -> int -> 'a -> unit

Raises if the index is invalid.

include Core.Container.S1 with type 'a t := 'a t
val mem : 'a t -> 'a -> equal:('a -> 'a -> bool) -> bool

Checks whether the provided element is there, using equal.

val length : 'a t -> int
val is_empty : 'a t -> bool
val iter : 'a t -> f:('a -> unit) -> unit
val fold : 'a t -> init:'acc -> f:('acc -> 'a -> 'acc) -> '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 t -> init:'acc -> f:('acc -> 'a -> ('acc, 'e) Base.Result.t) -> ('acc, 'e) Base.Result.t

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 fold_until : 'a t -> init:'acc -> f:('acc -> 'a -> ('acc, 'final) Base.Container.Continue_or_stop.t) -> finish:('acc -> 'final) -> '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
val for_all : 'a t -> f:('a -> bool) -> 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 t -> f:('a -> bool) -> int

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

Returns the sum of f i for all i in the container.

val sum : (module Base.Container.Summable with type t = 'sum) -> 'a t -> f:('a -> 'sum) -> 'sum
val find : 'a t -> f:('a -> bool) -> 'a option

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

val find_map : 'a t -> f:('a -> 'b option) -> 'b option

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

val to_array : 'a t -> 'a array
val min_elt : 'a t -> compare:('a -> 'a -> int) -> 'a option

Returns a minimum (resp maximum) element from the collection using the provided compare function, or None if the collection is empty. 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.

val max_elt : 'a t -> compare:('a -> 'a -> int) -> 'a option
include Core.Blit.S1 with type 'a t := 'a t
val blit : src:'a t -> src_pos:int -> dst:'a t -> dst_pos:int -> len:int -> unit
val blito : src:'a t -> ?src_pos:int -> ?src_len:int -> dst:'a t -> ?dst_pos:int -> unit -> unit
val unsafe_blit : src:'a t -> src_pos:int -> dst:'a t -> dst_pos:int -> len:int -> unit
val sub : 'a t -> pos:int -> len:int -> 'a t
val subo : ?pos:int -> ?len:int -> 'a t -> 'a t
val find_exn : 'a t -> f:('a -> bool) -> 'a

Finds the first 'a for which f is true *

val sort : ?pos:int -> ?len:int -> 'a t -> compare:('a -> 'a -> int) -> unit

sort uses constant heap space. To sort only part of the array, specify pos to be the index to start sorting from and len indicating how many elements to sort.

val is_sorted : 'a t -> compare:('a -> 'a -> int) -> bool
val next_free_index : 'a t -> int
val push_back : 'a t -> 'a -> unit
val push_back_index : 'a t -> 'a -> int
val grow_to : 'a t -> len:int -> default:'a -> unit

Grows the vec to the specified length if it is currently shorter. Sets all new indices to default.

val grow_to_include : 'a t -> int -> default:'a -> unit

Equivalent to grow_to t (index + 1) ~default.

val grow_to' : 'a t -> len:int -> default:(int -> 'a) -> unit

Grows the vec to the specified length if it is currently shorter. Sets all new indices to default idx.

val grow_to_include' : 'a t -> int -> default:(int -> 'a) -> unit

Equivalent to grow_to' t (index + 1) ~default.

val shrink_to : 'a t -> len:int -> unit

Shortens the vec to the specified length if it is currently longer. Raises if len < 0.

val remove_exn : 'a t -> int -> unit

remove vec i Removes the i-th element of the vector. This is not a fast implementation, and runs in O(N) time. (ie: it calls caml_modify under the hood)

val find_and_remove : 'a t -> f:('a -> bool) -> 'a option

Find the first element that satisfies f. If exists, remove the element from the vector and return it. This is not a fast implementation, and runs in O(N) time.

val pop_back_exn : 'a t -> 'a
val pop_back_unit_exn : 'a t -> unit
val peek_back : 'a t -> 'a option
val peek_back_exn : 'a t -> 'a
val foldi : 'a t -> init:'accum -> f:(int -> 'accum -> 'a -> 'accum) -> 'accum
val foldi_local_accum : 'a t -> init:'accum -> f:(int -> 'accum -> 'a -> 'accum) -> 'accum
val iteri : 'a t -> f:(int -> 'a -> unit) -> unit
val to_list : 'a t -> 'a list
val to_local_list : 'a t -> 'a list
val to_alist : 'a t -> (int * 'a) list
val to_sequence : 'a t -> 'a Core.Sequence.t

The input vec is copied internally so that future modifications of it do not change the sequence.

val to_sequence_mutable : 'a t -> 'a Core.Sequence.t

The input vec is shared with the sequence and modifications of it will result in modification of the sequence.

val of_list : 'a list -> 'a t
val of_array : 'a array -> 'a t
val of_sequence : 'a Core.Sequence.t -> 'a t
val take_while : 'a t -> f:('a -> bool) -> 'a t

take_while t ~f returns a fresh vec containing the longest prefix of t for which f is true.

module Inplace : sig ... end
val capacity : _ t -> int

The number of elements we can hold without growing.

val clear : _ t -> unit

clear t discards all elements from t in O(length) time.

val clear_imm : 'a t -> 'a Core.Type_immediacy.Always.t -> unit

clear_imm t discards all elements from 'a t in O(1) time if 'a is immediate.

val copy : 'a t -> 'a t

copy t returns a copy of t, that is, a fresh vec containing the same elements as t.

val exists : 'a t -> f:('a -> bool) -> bool

exists t ~f returns true if f evaluates true on any element, else false

val swap : _ t -> int -> int -> unit

swap the values at the provided indices

val swap_to_last_and_pop : 'a t -> int -> 'a

swap_to_last_and_pop t i is equivalent to swap t i (length t - 1); pop_back_exn t. It raises if i is out of bounds.

module With_structure_details : sig ... end
val unsafe_get : 'a t -> int -> 'a
val unsafe_set : 'a t -> int -> 'a -> unit
module Expert : sig ... end
module Stable : sig ... end
module type S = sig ... end
module Make (M : Core.Intable.S) : S with type index := M.t

Generate a specialised version of Vec with a custom index type.

OCaml

Innovation. Community. Security.