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) t
which amounts to a sequence of complete trees of increasing length.
val empty : 'a t
The empty wallet.
val is_empty : 'a t -> bool
is_empty w
is true iffw
is the empty wallet.
val singleton : 'a -> 'a t
singleton x
has length 1 and contains the elementx
.
val sample : (int -> 'a) -> int -> 'a t
sample f n
has lengthn
and containsf i
ati
.
val length : 'a t -> int
length w
is the number of elements ofw
.
val pop : 'a t -> 'a * 'a t
pop w
isx, w'
wherex
is the first element ofw
andw'
contains the remaining elements in the original order.- raises Invalid_argument
if
w
is empty.
val get : 'a t -> int -> 'a
get i w
is elementi
ofw
.- raises Invalid_argument
if
i
is out of bounds.
val set : int -> 'a -> 'a t -> 'a t
set i x w
isw
with elementi
replaced byx
.- raises Invalid_argument
if
i
is out of bounds.
val modify : int -> ('a -> 'a) -> 'a t -> 'a t
Given a
w
withx
ati
,modify i f w
isw
with elementi
replaced byf x
.- raises Invalid_argument
if
i
is out of bounds.
val for_all : ('a -> bool) -> 'a t -> bool
for_all f w
is true ifff x
is true for allx
inw
.
val for_all2 : ('a -> 'b -> bool) -> 'a t -> 'b t -> bool
for_all2 f wA wB
is true ifff xA xB
is true for each pair ofxA
andxB
taken from the same position ofwA
andwB
, respectively.- raises Invalid_argument
if
wA
andwB
have different size.
val search : ('a -> 'b option) -> 'a t -> 'b option
search f w
is the firstf x
different fromNone
forx
inw
, orNone
if no suchx
is found.
val mapj : (int -> 'a -> 'b) -> int -> 'a t -> 'b t
mapj f w
is the wallet withf i x
ati
wherex
is elementi
ofw
.
val map2 : ('a -> 'b -> 'c) -> 'a t -> 'b t -> 'c t
map2 f wA wB
is the wallet off xA xB
ati
wherexA
andxB
are the elementi
ofwA
andwB
, respectively.- raises Invalid_argument
if
wA
andwB
have different size.
val iter : ('a -> unit) -> 'a t -> unit
iter f w
callsf
on each element ofw
in order.
val iter2 : ('a -> 'b -> unit) -> 'a t -> 'b t -> unit
iter f wA wB
callsf
on each pair of successive elements ofwA
andwB
.- raises Invalid_argument
if
wA
andwB
have different size.
val fold : ('a -> 'b -> 'b) -> 'a t -> 'b -> 'b
fold f w
is the composition off x
forx
inw
, passed in order of occurrence.
val fold2 : ('a -> 'b -> 'c -> 'c) -> 'a t -> 'b t -> 'c -> 'c
fold2 f wA wB
is the composition off xA xB
forxA
andxB
pairwise taken fromwA
andwB
at the same successive positions.
val split : 'a t -> 'a t * 'a t
split w
splitsw
into 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 similarlycosplit
returns as its second component a complete tree which can be used by the primary functions.
val split_snd_length : int -> int
split_snd_length n
is the length ofsnd (split w)
for anyw
of 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 t
val copop : 'a t -> 'a * 'a t
val coget : 'a t -> int -> 'a
val coset : int -> 'a -> 'a t -> 'a t
val comodify : int -> ('a -> 'a) -> 'a t -> 'a t
val comapj_rev : (int -> 'a -> 'b) -> int -> 'a t -> 'b t
val coiter_rev : ('a -> unit) -> 'a t -> unit
val cofold_rev : ('a -> 'b -> 'b) -> 'a t -> 'b -> 'b
val cosplit : 'a t -> 'a t * 'a t