Legend:
Library
Module
Module type
Parameter
Class
Class type
Extensible vectors with constant-time append/prepend.
This module implements extensible arrays which work very much like ropes as described in Boehm, H., Atkinson, R., and Plass, M. 1995. Ropes: an alternative to strings. Softw. Pract. Exper. 25, 12 (Dec. 1995), 1315-1330.
These vectors have some interesting properties:
lower space overhead than other structures based on balanced trees such as Vec. The overhead can be adjusted, allowing to make get faster at the expense of set and viceversa.
appending or prepending a small vector to an arbitrarily large one in amortized constant time
concat, substring, insert, remove operations in amortized logarithmic time
access to and modification of vectors in logarithmic time
Functional nature and persistence
All operations but destructive_set (provided for efficient ephemeral usage) are non-destructive: the original vect is never modified. When a new vect is returned as the result of an operation, it will share as much data as possible with its "parent". For instance, if a vect of length n undergoes m operations (assume n >> m) like set, append or prepend, the modified vector will only require O(m) space in addition to that taken by the original vect.
However, Vect is an amortized data structure, and its use in a persistent setting can easily degrade its amortized time bounds. It is thus mainly intended to be used ephemerally. In some cases, it is possible to use Vect persistently with the same amortized bounds by explicitly rebalancing vects to be reused using balance. Special care must be taken to avoid calling balance too frequently; in the limit, calling balance after each modification would defeat the purpose of amortization.
This module is not thread-safe.
author Mauricio Fernandez
type'a t
The type of a polymorphic vect.
exceptionOut_of_bounds
Raised when an operation violates the bounds of the vect.
init n f returns a fresh vect of length n, with element number i initialized to the result of f i. In other terms, init n f tabulates the results of f applied to the integers 0 to n-1.
balance r returns a balanced copy of the r vect. Note that vects are automatically rebalanced when their height exceeds a given threshold, but balance allows to invoke that operation explicity.
concat r u concatenates the r and u vects. In general, it operates in O(log(min n1 n2)) amortized time. Small vects are treated specially and can be appended/prepended in amortized O(1) time.
destructive_set n e v sets the element of index n in the v vect to e. This operation is destructive, and will also affect vects sharing the modified leaf with v. Use with caution.
insert n r u returns a copy of the u vect where r has been inserted between the elements with index n and n + 1 in the original vect. The length of the new vect is length u + length r. Operates in amortized O(log(size r) + log(size u)) time.
remove m n r returns the vect resulting from deleting the elements with indexes ranging from m to m + n - 1 (included) from the original vect r. The length of the new vect is length r - n. Operates in amortized O(log(size r)) time.
Returns an enumeration of the elements of a vector, from last to first. Behavior of the enumeration is undefined if the contents of the vector changes afterwards.
Operates like iter, but also passes the index of the character to the given function.
val rangeiter : ('a-> unit)->int ->int ->'at-> unit
rangeiter f m n r applies f to all the elements whose indices k satisfy m <= k < m + n. It is thus equivalent to iter f (sub m n r), but does not create an intermediary vect. rangeiter operates in worst-case O(n + log m) time, which improves on the O(n log m) bound from an explicit loop using get.
map f v returns a vect isomorphic to v where each element of index i equals f (get v i). Therefore, the height of the returned vect is the same as that of the original one. Operates in O(n) time.
exists p [a0; a1; ...; an] checks if at least one element of the vect satisfies the predicate p. That is, it returns (p a0) || (p a1) || ... || (p an).
partition p v returns a pair of vects (v1, v2), where v1 is the vect of all the elements of v that satisfy the predicate p, and v2 is the vect of all the elements of v that do not satisfy p. The order of the elements in the input vect is preserved.