package union-find-lattice
Install
dune-project
Dependency
Authors
Maintainers
Sources
sha256=ecaf1444cd0e9d21174d854da0d2eea56d685180cc1e098016e2c66bc809f8ab
sha512=f12612357504879f78094ffa623bf44ec87c7f7adeeea1809c0b74fdf41e51a88958634d51d3f8bd8881c577a635ef81beaff91d0361195f9a65a5724ce096a9
doc/union-find-lattice.persistent-array/PersistentArray/Versioned/index.html
Module PersistentArray.VersionedSource
Extension of persistent arrays with a version number. Allows diff and diff_key to also return the minimum version.
Every array operation get, set, size, diff reroot's the array at the given version. For best performance, avoid jumping between versions too often.
The type of persistent arrays
Array creation
init n f creates the array [|f 0; f 1; ...; f (n-1)|]. O(n) complexity.
make n x creates the array [|x; x; ...; x|] with length n. O(n) complexity.
of_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.
of_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.
get 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:(Format.formatter -> unit -> unit) ->
(Format.formatter -> 'a -> unit) ->
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.
append 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).
extend 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)|]
iter f t calls f on every element in order: f (get t 0); ...; f (get t (size t - 1))