package sek

  1. Overview
  2. Docs

The submodule Ephemeral, also available under the name E, offers an implementation of ephemeral (mutable) sequences.

type 'a t

A sequence s of type 'a t is a mutable data structure which represents a mathematical sequence of elements of type 'a.

val create : 'a -> 'a t

create default constructs and returns a new empty sequence. The default value default is used to overwrite array slots that become empty during operations such as clear, pop, or split.

val make : 'a -> length -> 'a -> 'a t

make default n v constructs and returns a fresh sequence whose length is n and which consists of n copies of the value v. It is equivalent to of_array default (Array.make n v).

val init : 'a -> length -> (index -> 'a) -> 'a t

init default n f constructs and returns a fresh sequence whose length is n and whose elements are the values produced by the calls f 0, f 1, ... f (n-1), in this order. It is equivalent to of_array default (Array.init n f).

val default : 'a t -> 'a

default s returns a value that was supplied as an argument in a call to create that contributed to the construction of the sequence s.

val length : 'a t -> length

length s returns the length of the sequence s.

val is_empty : 'a t -> bool

is_empty s returns true if the sequence s is empty and false otherwise. It is equivalent to length s = 0.

val clear : 'a t -> unit

clear s empties the sequence s.

val copy : 'a t -> 'a t

copy s constructs and returns a copy s' of the sequence s. The sequences s and s' initially have the same elements, and can thereafter be modified independently of one another. Although copy itself is very fast, it does have an indirect cost. As a result of the copy, the sequences s and s' lose the unique ownership of their chunks; this causes additional copies to take place during subsequent update operations on either s or s'.

val assign : 'a t -> 'a t -> unit

If s1 and s2 are distinct sequences, then assign s1 s2 moves s2's elements into s1, overwriting s1's previous content, and clears s2. If s1 and s2 are the same sequence, then assign s1 s2 has no effect.

val push : side -> 'a t -> 'a -> unit

push side s x pushes the element x onto the front or back end of the sequence s. The parameter side determines which end of the sequence is acted upon.

val pop : side -> 'a t -> 'a

If the sequence s is nonempty, then pop side s pops an element x off the front or back end of the sequence s and returns x. The parameter side determines which end of the sequence is acted upon. If the sequence s is empty, the exception Empty is raised.

val pop_opt : side -> 'a t -> 'a option

If the sequence s is nonempty, then pop_opt side s pops an element x off the front or back end of the sequence s and returns Some x. The parameter side determines which end of the sequence is acted upon. If the sequence s is empty, None is returned.

val peek : side -> 'a t -> 'a

If the sequence s is nonempty, then peek side s reads the element x found at the front or back end of the sequence s and returns x. The parameter side determines which end of the sequence is acted upon. If the sequence s is empty, the exception Empty is raised.

val peek_opt : side -> 'a t -> 'a option

If the sequence s is nonempty, then peek_opt side s reads the element x found at the front or back end of the sequence s and returns Some x. The parameter side determines which end of the sequence is acted upon. If the sequence s is empty, None is returned.

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

get s i returns the element x located at index i in the sequence s. The index i must be comprised between 0 included and length s excluded.

val set : 'a t -> index -> 'a -> unit

set s i x replaces the element located at index i in the sequence s with the element x. The index i must be comprised between 0 included and length s excluded. The sequence s is updated in place.

val concat : 'a t -> 'a t -> 'a t

concat s1 s2 creates and returns a new sequence whose content is the concatenation of the sequences s1 and s2. The sequences s1 and s2 are cleared. The sequences s1 and s2 must be distinct. concat is slightly less efficient than append, whose use should be preferred.

val append : side -> 'a t -> 'a t -> unit

append back s1 s2 is equivalent to assign s1 (concat s1 s2). Thus, s1 is assigned the concatenation of the sequences s1 and s2, while s2 is cleared. In other words, append back s1 s2 appends the sequence s2 at the back end of the sequence s1.

append front s1 s2 is equivalent to assign s1 (concat s2 s1). Thus, s1 is assigned the concatenation of the sequences s2 and s1, while s2 is cleared. In other words, append front s1 s2 prepends the sequence s2 at the front end of the sequence s1.

In either case, the sequences s1 and s2 must be distinct.

val split : 'a t -> index -> 'a t * 'a t

split s i splits the sequence s at index i. It returns two new sequences s1 and s2 such that the length of s1 is i and the concatenation of s1 and s2 is s. The sequence s is cleared. The index i must be comprised between 0 and length s, both included. split is slightly less efficient than carve, whose use should be preferred.

val carve : side -> 'a t -> index -> 'a t

carve back s i is equivalent to let s1, s2 = split s i in assign s s1; s2. Thus, it splits the sequence s at index i into two parts: the first part is written to s, while the second part is returned.

carve front s i is equivalent to let s1, s2 = split s i in assign s s2; s1. Thus, it splits the sequence s at index i into two parts: the second part is written to s, while the first part is returned.

In either case, the index i must be comprised between 0 and length s, both included.

val iter : direction -> ('a -> unit) -> 'a t -> unit

iter direction f s applies the function f in turn to every element x of the sequence s. The parameter direction determines in what order the elements are presented. The function f is not allowed to modify the sequence s while iteration is ongoing.

val iteri : direction -> (index -> 'a -> unit) -> 'a t -> unit

iteri direction f s applies the function f in turn to every index i and matching element x of the sequence s. The parameter direction determines in what order the elements are presented. The function f is not allowed to modify the sequence s while iteration is ongoing.

val fold_left : ('a -> 'b -> 'a) -> 'a -> 'b t -> 'a

fold_left f a s applies the function f in turn to each element of the sequence s, in the forward direction. An accumulator is threaded through the calls to f. The function f is not allowed to modify the sequence s while iteration is ongoing. Subject to this condition, fold_left f a s is equivalent to List.fold_left f a (to_list s).

val fold_right : ('a -> 'b -> 'b) -> 'a t -> 'b -> 'b

fold_left f a s applies the function f in turn to each element of the sequence s, in the backward direction. An accumulator is threaded through the calls to f. The function f is not allowed to modify the sequence s while iteration is ongoing. Subject to this condition, fold_right f s a is equivalent to List.fold_right f (to_list s) a.

val to_list : 'a t -> 'a list

to_list s returns a list whose elements are the elements of the sequence s.

val to_array : 'a t -> 'a array

to_array s returns a fresh array whose elements are the elements of the sequence s.

val of_array_segment : 'a -> 'a array -> index -> length -> 'a t

of_array_segment default a head size creates a new sequence out of the array segment defined by the array a, the start index head, and the size size. The data is copied, so the array a can still be used afterwards.

val of_array : 'a -> 'a array -> 'a t

of_array default a creates a new sequence out of the array a. The data is copied, so the array a can still be used afterwards.

val format : Format.formatter -> int t -> unit

format is a printer for sequences of integers. It can be installed in the OCaml toplevel loop by #install_printer format. It is intended to be used only while debugging the library.

val check : 'a t -> unit

In a release build, this function does nothing. In a development build, it checks that the data structure's internal invariant is satisfied.

OCaml

Innovation. Community. Security.