Module Deque
A double-ended queue (abbreviated deque) is an ordered collection for which elements can be added and removed from the front and the back of the list. This library provides a purely functional, fully persistent implementation, such that the main operations are all worst-case constant time.
The following datastructures were invented by Kaplan and Tarjan and are described in their brilliant paper "Purely Functional, Real-Time Deques with Catenation".
module Dequeue : sig ... end
A double-ended queue with O(1)
cons
/uncons
,snoc
/unsnoc
andrev
; missing a fastappend
. Most similar to a finger tree asnth
is also O(log min(i, N - i)).
module Steque : sig ... end
A stack-ended queue with O(1)
cons
/uncons
,snoc
andappend
; missingunsnoc
andrev
.
module Deck : sig ... end
A double-ended queue with all operations in O(1):
cons
/uncons
,snoc
/unsnoc
andappend
.
val empty : 'a t
The
empty
deque.
val is_empty : 'a t -> bool
is_empty xs
returnstrue
when the dequexs
contains no elements,false
if at least one.
val singleton : 'a -> 'a t
singleton x
returns a deque containing only a single elementx
.
val uncons : 'a t -> ('a * 'a t) option
uncons xs
pops the left-most element of the dequexs
. O(1)- returns
None
if the deque is empty.
val unsnoc : 'a t -> ('a t * 'a) option
unsnoc xs
pops the right-most element of the dequexs
. O(1)- returns
None
if the deque is empty.
val length : 'a t -> int
length xs
returns the number of elements contained inxs
. O(N)
List
val hd : 'a t -> 'a
hd xs
returns the left-most element ofxs
.- raises Failure
if the deque is empty.
val nth : 'a t -> int -> 'a
nth xs n
returns then
-th element of the dequexs
. The left-most element is at position 0.- raises Failure
if the deque is too short.
- raises Invalid_argument
if
n
is negative.
val nth_opt : 'a t -> int -> 'a option
nth xs n
returns then
-th element of the dequexs
. The left-most element is at position 0.- returns
None if the deque is too short.
- raises Invalid_argument
if
n
is negative.
val make : int -> 'a -> 'a t
make len x
replicates the valuex
in a new deque of sizelen
.- raises Invalid_argument
if
len
is negative.
val init : int -> (int -> 'a) -> 'a t
init len f
creates a new deque of sizelen
such that itsi
th element isf i
, evaluated left to right.- raises Invalid_argument
if
len
is negative.
Comparisons
val (=) : 'a t -> 'a t -> bool
xs = ys
is satisfied when the elements ofxs
are in the same order, and are structurally equal to the elements ofys
.
Catenation
Iterators
val iter : ('a -> unit) -> 'a t -> unit
iter f xs
applies the functionf
in turn to each element ofxs
from left to right.
val iteri : (int -> 'a -> unit) -> 'a t -> unit
Same as
iter
, but the functionf
also receives the index of each element as first argument (counting from 0).
val map : ('a -> 'b) -> 'a t -> 'b t
map f xs
creates a new deque where each elementx
ofxs
has been replaced byf x
.
val mapi : (int -> 'a -> 'b) -> 'a t -> 'b t
Same as
map
, but the functionf
also receives the index of each element as its first argument (counting from 0).
val filter_map : ('a -> 'b option) -> 'a t -> 'b t
filter_map f xs
appliesf
to each element ofxs
, filtering out theNone
results and returns a deque of all theSome
values.
val concat_map : ('a -> 'b t) -> 'a t -> 'b t
concat_map f xs
gives the same result asconcat (map f xs)
.
val fold_left_map : ('a -> 'b -> 'a * 'c) -> 'a -> 'b t -> 'a * 'c t
fold_left_map f z xs
is a combination offold_left
andmap
that threads an accumulatorz
through calls tof
.
val fold_left : ('a -> 'b -> 'a) -> 'a -> 'b t -> 'a
fold_left f z xs
computesf (... (f (f z x_0) x_1) ...) x_n
wherex_0...x_n
are the elements of the dequexs
in left to right order.
val fold_right : ('a -> 'b -> 'b) -> 'a t -> 'b -> 'b
fold_right f xs z
computesf x_0 (f x_1 (... (f x_n z)))
wherex_0...x_n
are the elements of the dequexs
in left to right order.
Iterators on two deques
val iter2 : ('a -> 'b -> unit) -> 'a t -> 'b t -> unit
iter2 f xs ys
callsf x_i y_i
for each element ofxs
andys
at the same indexi
, in left to right order.- raises Invalid_argument
if the two deques have different lengths.
Scanning
val for_all : ('a -> bool) -> 'a t -> bool
for_all f xs
checks if all elements of the dequexs
satisfy the predicatef
. It computes the conjunctionf x_0 && ... && f x_n
, or returnstrue
if the deque was empty.
val exists : ('a -> bool) -> 'a t -> bool
exists f xs
checks if at least one element of the dequexs
satisfies the predicatef
. It computes the disjunctionf x_0 || ... || f x_n
, or returnsfalse
if the deque was empty.
val for_all2 : ('a -> 'b -> bool) -> 'a t -> 'b t -> bool
Same as
for_all
, but for a two-arguments predicate.- raises Invalid_argument
if the two deques have different lenths.
val exists2 : ('a -> 'b -> bool) -> 'a t -> 'b t -> bool
Same as
exists
, but for a two-arguments predicate.- raises Invalid_argument
if the two deques have different lenths.
val mem : 'a -> 'a t -> bool
mem x xs
is true if and only ifx
is structurally equal(=)
to an element ofxs
.
val memq : 'a -> 'a t -> bool
memq x xs
is true if and only ifx
is physically equal(==)
to an element ofxs
.
Searching
val find : ('a -> bool) -> 'a t -> 'a
find f xs
returns the left-most element ofxs
that satisfies the predicatef
.- raises Not_found
otherwise.
val find_opt : ('a -> bool) -> 'a t -> 'a option
find f xs
returns the left-most element ofxs
that satisfies the predicatef
.- returns
None otherwise.
val find_map : ('a -> 'b option) -> 'a t -> 'b option
find_map f xs
appliesf
to the elements ofxs
from left to right, and returns the first result of the formSome v
, orNone
if none exists.
val filter : ('a -> bool) -> 'a t -> 'a t
filter f xs
returns all the elements of the dequexs
that satisfy the predicatef
. The order of the elements in the deque is preserved.
Association
val assoc : 'a -> ('a * 'b) t -> 'b
assoc key xs
returns the left-most value associated withkey
in the deque of key-value pairsxs
.- raises Not_found
if there is no value with such a
key
.
val assoc_opt : 'a -> ('a * 'b) t -> 'b option
assoc key xs
returns the left-most value associated withkey
in the deque of key-value pairsxs
.- returns
None if there is no value with such a
key
.
val assq : 'a -> ('a * 'b) t -> 'b
Same as
assoc
, but uses physical equality rather than structural equality for key comparison.- raises Not_found
if there is no value associated with
key
.
val assq_opt : 'a -> ('a * 'b) t -> 'b option
Same as
assoc_opt
, but uses physical equality rather than structural equality for key comparison.- returns
None if there is no value associated with
key
.
val mem_assoc : 'a -> ('a * 'b) t -> bool
mem_assoc key xs
returnstrue
whenxs
contains a pair withkey
, andfalse
otherwise.
val mem_assq : 'a -> ('a * 'b) t -> bool
Same as
mem_assoc
, but uses physical equality rather than structural equality.
Pairs
Sorting
val sort : ('a -> 'a -> int) -> 'a t -> 'a t
sort cmp xs
sorts the dequexs
in increasing order, according to the comparison functioncmp
. This comparison functioncmp
must return0
when its argument are equal, a positive integer if the first is greater and a negative integer if the first is smaller. SeeArray
.sort for a complete specification.
Conversions
val to_array : 'a t -> 'a array
to_array xs
is an array containing all the elements of the dequexs
.
val of_array : 'a array -> 'a t
of_array arr
creates a deque from the elements of the arrayarr
.
val to_list : 'a t -> 'a list
to_list xs
returns a list of the elements of the dequexs
.
val of_list : 'a list -> 'a t
of_list lst
creates a deque from the elements of the listlst
.
val to_seq : 'a t -> 'a Stdlib.Seq.t
Iterate on a deque.
val of_seq : 'a Stdlib.Seq.t -> 'a t
Create a deque from a sequence.