Module Prime_wallet
A constructive stack-like container (prime.unstable).
This container provides functionality similar to arrays or stacks with constructive updates. Random access, including modifications, have logarithmic complexity and functions are provided to add or remove elements from the front.
A wallet is represented as an optional element and a wallet of pairs,
type 'a t = Empty | Even of ('a * 'a) t | Odd 'a * ('a * 'a) twhich amounts to a sequence of complete trees of increasing length.
val empty : 'a tThe empty wallet.
val is_empty : 'a t -> boolis_empty wis true iffwis the empty wallet.
val singleton : 'a -> 'a tsingleton xhas length 1 and contains the elementx.
val sample : (int -> 'a) -> int -> 'a tsample f nhas lengthnand containsf iati.
val length : 'a t -> intlength wis the number of elements ofw.
val pop : 'a t -> 'a * 'a tpop wisx, w'wherexis the first element ofwandw'contains the remaining elements in the original order.- raises Invalid_argument
if
wis empty.
val get : 'a t -> int -> 'aget i wis elementiofw.- raises Invalid_argument
if
iis out of bounds.
val set : int -> 'a -> 'a t -> 'a tset i x wiswwith elementireplaced byx.- raises Invalid_argument
if
iis out of bounds.
val modify : int -> ('a -> 'a) -> 'a t -> 'a tGiven a
wwithxati,modify i f wiswwith elementireplaced byf x.- raises Invalid_argument
if
iis out of bounds.
val for_all : ('a -> bool) -> 'a t -> boolfor_all f wis true ifff xis true for allxinw.
val for_all2 : ('a -> 'b -> bool) -> 'a t -> 'b t -> boolfor_all2 f wA wBis true ifff xA xBis true for each pair ofxAandxBtaken from the same position ofwAandwB, respectively.- raises Invalid_argument
if
wAandwBhave different size.
val search : ('a -> 'b option) -> 'a t -> 'b optionsearch f wis the firstf xdifferent fromNoneforxinw, orNoneif no suchxis found.
val mapj : (int -> 'a -> 'b) -> int -> 'a t -> 'b tmapj f wis the wallet withf i xatiwherexis elementiofw.
val map2 : ('a -> 'b -> 'c) -> 'a t -> 'b t -> 'c tmap2 f wA wBis the wallet off xA xBatiwherexAandxBare the elementiofwAandwB, respectively.- raises Invalid_argument
if
wAandwBhave different size.
val iter : ('a -> unit) -> 'a t -> unititer f wcallsfon each element ofwin order.
val iter2 : ('a -> 'b -> unit) -> 'a t -> 'b t -> unititer f wA wBcallsfon each pair of successive elements ofwAandwB.- raises Invalid_argument
if
wAandwBhave different size.
val fold : ('a -> 'b -> 'b) -> 'a t -> 'b -> 'bfold f wis the composition off xforxinw, passed in order of occurrence.
val fold2 : ('a -> 'b -> 'c -> 'c) -> 'a t -> 'b t -> 'c -> 'cfold2 f wA wBis the composition off xA xBforxAandxBpairwise taken fromwAandwBat the same successive positions.
val split : 'a t -> 'a t * 'a tsplit wsplitswinto two wallets of comparable where the second is a complete tree. In particular the second tree can be passed to algorithms using the opposite functions below, and similarlycosplitreturns as its second component a complete tree which can be used by the primary functions.
val split_snd_length : int -> intsplit_snd_length nis the length ofsnd (split w)for anywof lengthn.
Opposite Functions
These functions are not needed for normal usage. They are variants of the main functions with internal trees reversed, and can be used to share split elements when two wallets are combined into a symmetric datastructure.
val copush : 'a -> 'a t -> 'a tval copop : 'a t -> 'a * 'a tval coget : 'a t -> int -> 'aval coset : int -> 'a -> 'a t -> 'a tval comodify : int -> ('a -> 'a) -> 'a t -> 'a tval comapj_rev : (int -> 'a -> 'b) -> int -> 'a t -> 'b tval coiter_rev : ('a -> unit) -> 'a t -> unitval cofold_rev : ('a -> 'b -> 'b) -> 'a t -> 'b -> 'bval cosplit : 'a t -> 'a t * 'a t