package varray
Module Varray.RootSource
Root (Varray) nests an existing Varray to improve the performances of random insertion and deletion. However, it does so at the price that random access and insertion and deletion at the ends will be a constant time slower!
Parameters
Signature
The type of a varray.
Dynamic collection
push_back t x adds a new element x at the end of the varray t. O(k) amortized.
pop_back t removes and returns the rightmost element of the varray t. O(k) amortized.
push_front t x inserts a new element x at position 0, on the left side of the varray t. Every previous element of t is shifted one to the right. O(k) amortized.
pop_front t removes and returns the leftmost element at position 0 of the varray t. Every element of t is shifted one to the right. O(k) amortized.
insert_at t i x inserts the element x at position i in the varray t. Every element on the right of i is shifted by one. O(k² × k√N)
insert_at t 0 xis the same aspush_front t xinsert_at t (length t) xis the same aspush_back t x
pop_at t i removes and returns the element t.(i). Every element on the right of i is shifted by one to the left. O(k² × k√N)
pop_at t 0is the same aspop_front tpop_at t (length t - 1)is the same aspop_back t
delete_at t i removes the element t.(i). Every element on the right of i is shifted by one to the left. O(k² × k√N)
Freeze during traversals
The previous operations all fail when the varray is being traversed:
protect t fn marks t as protected during the execution of fn (). All operations that would update the length of t by pushing or poping elements will raise a Failure indicating that the traversal is unsafe.
Array
get t i returns the ith element of the varray. Indexing starts from 0 upto length t - 1. O(k)
set t i v updates the value of the ith element to x. O(k)
make n x returns a new varray of length n, where all the elements are initialized to the value x.
init n f returns a new array of length n, where the element at position i is initialized to f i.
Copying elements
append a b returns a new varray by concatening the elements of a with those of b.
concat ts returns a new varray whose elements are in the same order as the values from the list of varrays ts.
sub t i n returns a new varray of length n, containing the elements from the range i, i+n-1 of the varray t.
copy t returns a new varray containing the same sequence of elements as t.
fill t pos len x modifies the varray t in place, by setting the value x in the range pos, pos + len - 1.
blit src src_pos dst dst_pos len updates the varray dst in place, by copying the range src_pos, src_pos + len - 1 of values from src into the destination range dst_pos, dst_pos + len - 1 of dst.
Traversals
iter f t calls the function f on all elements of t, from left to right.
iteri f t calls f i t.(i) on all the indexes i of t, from left to right.
map f t returns a new varray, whose elements are f x for each x from the varray t.
mapi f t returns a new varray, whose elements are f i t.(i) for each index i of the varray t.
fold_left f z t computes f (... (f (f z t.(0)) t.(1)) ...) t.(length t - 1).
fold_right f t z computes f t.(0) (f t.(1) (... (f z t.(length t - 1)))).
fold_left_map is a combination of fold_left and map, that threads an accumulator.
Iterators on two varrays
iter2 f xs ys calls f xs.(i) ys.(i) for each index i from left to right.
map2 f xs ys returns a new varray whose ith element is f xs.(i) ys.(i).
Predicates
for_all f t holds when f is satisfied by all the elements of t.
for_all2 f xs ys holds when f xs.(i) ys.(i) is satisfied by all indexes i.
exists f t holds when f is satisfied by one of the elements of t.
exists2 f xs ys holds when an index i exists such that f xs.(i) ys.(i) is satisfied.
find_opt f t returns the leftmost element of t that satisfies f.
find_map f t returns the first result of f of the form Some v.
mem x t is true when x is equal ( = ) to an element of the varray t.
Same as mem, but memq x t uses physical equality ( == ) for comparison.
Sort
sort cmp t updates t inplace by sorting the elements in increasing order according to cmp.
Same as sort, but equal elements are kept in the same relative order.
Conversions
of_array arr returns a new varray containing all the elements of the array arr.
to_array t returns a new array containing all the elements of the varray t.
of_list xs returns a new varray containing all the elements of the list xs.
Internals
This part can be ignored as it exposes no user-facing functionality!.. but the design pattern is neat. The Root functor requires access to internal operations, that should neither be called nor implemented by a user of this library.
The internal operations, safely concealed behind an abstract signature!
This could not have been written as:
module Unsafe : UNSAFE with type 'a array = 'a array
and type 'a elt = 'a elt Since the signature UNSAFE can't be type constrained without also having all its internal functions be public. The Internals functor circumvent this rule by exposing an opaque signature parametrized by the type constraints.