Module PersistentArray
module type S = sig ... endPersistent array implementation. I.E. array which keep track of their history and allow rollback. They appear immutable. This implementation is pretty much exactly the one described by [Cochon and Filliâtre, 2007].
include S
Array creation
val init : int -> (int -> 'a) -> 'a tinit n f creates the array [|f 0; f 1; ...; f (n-1)|]. O(n) complexity.
val make : int -> 'a -> 'a tmake n x creates the array [|x; x; ...; x|] with length n. O(n) complexity.
val of_array : 'a array -> 'a tof_array arr creates the persistent array containing the same elements as arr This copies the array, so future modifications to arr will not corrupt the persistent array. O(Array.length arr) complexity.
val of_list : 'a list -> 'a tof_list l creates a persistent array with the same elements as l. O(List.length l) complexity.
Array operations
All operations reroot the given array. This is constant time for mutliple accesses to the same version. When switching versions however, the cost is linear in the number of updates (set operations) between both version.
val size : 'a t -> intsize t is the size (number of elements) of the array t
val get : 'a t -> int -> 'aget t i returns the i-th element of the array. O(1) complexity (after reroot).
set t i v returns a new array whose values are the same as t except at position i, where the value is v. O(1) complexity (after reroot).
val pretty :
?pp_sep:(Stdlib.Format.formatter -> unit -> unit) ->
(Stdlib.Format.formatter -> 'a -> unit) ->
Stdlib.Format.formatter ->
'a t ->
unitpretty ~pp_sep pp_elt t prints the array at t, using pp_elt to print elements and pp_sep to separate them. pp_sep defaults to printing a semicolon and a space).
diff a b create a table of differences i -> (a.(i), b.(i)) between a and b Requires a and b to share a common root (i.e. derive from the same init or make).
The diff table may include identical values i -> (v,v), these correspond to changes that are reverted on the chain from a to b. For example: let b = set (set a i x) i a.(i).
For performance: have a be the one closest to reroot (i.e. the last modified value).
O(d) complexity (after reroot at a), where d is the number of updates (set operations) between a and b.
Versioned persistent arrays also return the element with the lowest version tag.
Array resizing
It is possible to extend persistent arrays (i.e. add elements at the back). These operations retro-actively modify all previous versions. They will all appear as though they always had the new size, so a get old_version i that would have failed before resize will now succeed.
val append : 'a t -> 'a array -> unitappend t arr extends t by adding the values from arr at the end. This modifies t and all previous versions of t. O(size t + Array.length arr) complexity (after reroot).
val extend : 'a t -> int -> 'a -> unitextend t n x extends t by appending n times the value x: [|t.(0); ...; t.(size t - 1); x; ...; x|]. This modifies t and all previous versions of t. O(size t + n) complexity.
Iterators
map f t creates a new persistent array, initialized by [|f (get t 0); ...; f (get t (size t) - 1)|]
val iter : ('a -> unit) -> 'a t -> unititer f t calls f on every element in order: f (get t 0); ...; f (get t (size t - 1))
val fold : ('acc -> 'a -> 'acc) -> 'acc -> 'a t -> 'accfold f init t is f (... f init (get t 0) ...) (get t (size t - 1))