package batteries
Install
Dune Dependency
Authors
Maintainers
Sources
md5=ea26b5c72e6731e59d856626049cca4d
sha512=55975b62c26f6db77433a3ac31f97af609fc6789bb62ac38b267249c78fd44ff37fe81901f1cf560857b9493a6046dd37b0d1c0234c66bd59e52843aac3ce6cb
doc/batteries.unthreaded/BatVect/index.html
Module BatVect
Source
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.
The type of a polymorphic vect.
Raised when an operation violates the bounds of the vect.
Maximum length of the vect.
Creation and conversions
of_array s
returns a vect corresponding to the array s
. Operates in O(n)
time.
make i c
returns a vect of length i
whose elements are all equal to c
; it is similar to Array.make
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
.
Properties
Operations
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 explicitly.
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.
append c r
returns a new vect with the c
element at the end in amortized O(1)
time.
prepend c r
returns a new vect with the c
character at the beginning in amortized O(1)
time.
get v n
returns the (n+1)th element from the vect v
; i.e. get v 0
returns the first element. Operates in worst-case O(log size)
time.
set v n c
returns a copy of the v
vect where the (n+1)th element (see also get
) has been set to c
. Operates in worst-case O(log size)
time.
modify v n f
is equivalent to set v n (f (get v n))
, but more efficient. Operates in worst-case O(log size)
time.
destructive_set v n c
sets the element of index n
in the v
vect to c
. This operation is destructive, and will also affect vects sharing the modified leaf with v
. Use with caution.
sub m n r
returns a sub-vect of r
containing all the elements whose indexes range from m
to m + n - 1
(included).
insert n r u
returns a copy of the u
vect where r
has been inserted between the elements with index n - 1
and n
in the original vect; after insertion, the first element of r
(if any) is at index n
. 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.
Conversion
Returns an enumeration of the elements of the vector. Behavior of the enumeration is undefined if the contents of the vector changes afterwards.
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.
Build a vector from an enumeration, from last to first.
Iteration and higher-order functions
iter f r
applies f
to all the elements in the r
vect, in order.
Operates like iter, but also passes the index of the character to the given function.
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
.
fold_left f a r
computes f (... (f (f a r0) r1)...) rN-1
where rn = Vect.get n r
and N = length r
.
as fold_left
, but no initial value - just applies reducing function to elements from left to right.
fold_right f r a
computes f (r0 ... (f rN-2 (f rN-1 a)) ...))
where rn = Vect.get n r
and N = length r
.
As fold
, but with the position of each value passed to the folding function
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.
Same as map
, but the function is applied to the index of the element as first argument, and the element itself as second argument.
Predicates
for_all p [a0; a1; ...; an]
checks if all elements of the vect satisfy the predicate p
. That is, it returns (p a0) && (p a1) && ... && (p an)
.
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)
.
find p v
returns the first element of vect v
that satisfies the predicate p
.
find_opt p v
returns Some a
, where a
is the first element of vect v
that satisfies the predicate p
, or None
if no such element exists.
Same as Vect.mem
but uses physical equality instead of structural equality to compare vect elements.
findi p v
returns the index of the first element of vect v
that satisfies the predicate p
.
filter f v
returns a vect with the elements a
from v
such that f a
returns true
. Operates in O(n)
time.
filter_map f v
returns a vect consisting of all elements b
such that f a
returns Some b
, where a
is an element of v
.
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.
Convenience Functions
Boilerplate code
val print :
?first:string ->
?last:string ->
?sep:string ->
('a BatInnerIO.output -> 'b -> unit) ->
'a BatInnerIO.output ->
'b t ->
unit