package batteries

  1. Overview
  2. Docs
Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source

Module BatVectSource

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
Sourcetype 'a t

The type of a polymorphic vect.

Sourceexception Out_of_bounds

Raised when an operation violates the bounds of the vect.

Sourceval max_length : int

Maximum length of the vect.

Creation and conversions

Sourceval empty : 'a t

The empty vect.

Sourceval singleton : 'a -> 'a t

Returns a vect of length 1 holding only the given element.

Sourceval of_array : 'a array -> 'a t

of_array s returns a vect corresponding to the array s. Operates in O(n) time.

Sourceval to_array : 'a t -> 'a array

to_array r returns an array corresponding to the vect r.

Sourceval to_list : 'a t -> 'a list

Returns a list with the elements contained in the vect.

Sourceval of_list : 'a list -> 'a t
Sourceval make : int -> 'a -> 'a t

make i c returns a vect of length i whose elements are all equal to c; it is similar to Array.make

Sourceval init : int -> (int -> 'a) -> 'a t

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

Sourceval is_empty : 'a t -> bool

Returns whether the vect is empty or not.

Sourceval height : 'a t -> int

Returns the height (depth) of the vect.

Sourceval length : 'a t -> int

Returns the length of the vect (O(1)).

Operations

Sourceval balance : 'a t -> 'a t

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.

Sourceval concat : 'a t -> 'a t -> 'a t

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.

Sourceval append : 'a -> 'a t -> 'a t

append c r returns a new vect with the c element at the end in amortized O(1) time.

Sourceval prepend : 'a -> 'a t -> 'a t

prepend c r returns a new vect with the c character at the beginning in amortized O(1) time.

Sourceval get : 'a t -> int -> 'a

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.

Sourceval at : 'a t -> int -> 'a

as get

Sourceval set : 'a t -> int -> 'a -> 'a t

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.

Sourceval modify : 'a t -> int -> ('a -> 'a) -> 'a t

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.

Sourceval destructive_set : 'a t -> int -> 'a -> unit

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.

Sourceval sub : 'a t -> int -> int -> 'a t

sub m n r returns a sub-vect of r containing all the elements whose indexes range from m to m + n - 1 (included).

  • raises Out_of_bounds

    in the same cases as Array.sub. Operates in worst-case O(log size) time.

Sourceval insert : int -> 'a t -> 'a t -> 'a t

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.

Sourceval remove : int -> int -> 'a t -> 'a t

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

Sourceval enum : 'a t -> 'a BatEnum.t

Returns an enumeration of the elements of the vector. Behavior of the enumeration is undefined if the contents of the vector changes afterwards.

Sourceval of_enum : 'a BatEnum.t -> 'a t

Build a vector from an enumeration.

Sourceval backwards : 'a t -> 'a BatEnum.t

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.

Sourceval of_backwards : 'a BatEnum.t -> 'a t

Build a vector from an enumeration, from last to first.

Iteration and higher-order functions

Sourceval iter : ('a -> unit) -> 'a t -> unit

iter f r applies f to all the elements in the r vect, in order.

Sourceval iteri : (int -> 'a -> unit) -> 'a t -> unit

Operates like iter, but also passes the index of the character to the given function.

Sourceval rangeiter : ('a -> unit) -> int -> int -> 'a t -> 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.

Sourceval fold_left : ('b -> 'a -> 'b) -> 'b -> 'a t -> 'b

fold_left f a r computes f (... (f (f a r0) r1)...) rN-1 where rn = Vect.get n r and N = length r.

Sourceval fold : ('b -> 'a -> 'b) -> 'b -> 'a t -> 'b

An alias for fold_left

Sourceval reduce : ('a -> 'a -> 'a) -> 'a t -> 'a

as fold_left, but no initial value - just applies reducing function to elements from left to right.

Sourceval fold_right : ('a -> 'b -> 'b) -> 'a t -> 'b -> 'b

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.

Sourceval foldi : (int -> 'b -> 'a -> 'b) -> 'b -> 'a t -> 'b

As fold, but with the position of each value passed to the folding function

Sourceval map : ('a -> 'b) -> 'a t -> 'b t

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.

Sourceval mapi : (int -> 'a -> 'b) -> 'a t -> 'b t

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

Sourceval for_all : ('a -> bool) -> 'a t -> bool

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).

Sourceval exists : ('a -> bool) -> 'a t -> bool

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).

Sourceval find : ('a -> bool) -> 'a t -> 'a

find p v returns the first element of vect v that satisfies the predicate p.

  • raises Not_found

    if there is no value that satisfies p in the vect v.

Sourceval find_opt : ('a -> bool) -> 'a t -> 'a option

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.

  • since 2.7.0
Sourceval mem : 'a -> 'a t -> bool

mem a v is true if and only if a is equal to an element of v.

Sourceval memq : 'a -> 'a t -> bool

Same as Vect.mem but uses physical equality instead of structural equality to compare vect elements.

Sourceval findi : ('a -> bool) -> 'a t -> int

findi p v returns the index of the first element of vect v that satisfies the predicate p.

  • raises Not_found

    if there is no value that satisfies p in the vect v.

Sourceval filter : ('a -> bool) -> 'a t -> 'a t

filter f v returns a vect with the elements a from v such that f a returns true. Operates in O(n) time.

Sourceval filter_map : ('a -> 'b option) -> 'a t -> 'b t

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.

Sourceval find_all : ('a -> bool) -> 'a t -> 'a t

find_all is another name for Vect.filter.

Sourceval partition : ('a -> bool) -> 'a t -> 'a t * 'a t

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

Sourceval first : 'a t -> 'a
Sourceval last : 'a t -> 'a

These return the first and last values in the vector

Sourceval shift : 'a t -> 'a * 'a t

Return the first element of a vector and its last n-1 elements.

Sourceval pop : 'a t -> 'a * 'a t

Return the last element of a vector and its first n-1 elements.

Boilerplate code

Sourceval print : ?first:string -> ?last:string -> ?sep:string -> ('a BatInnerIO.output -> 'b -> unit) -> 'a BatInnerIO.output -> 'b t -> unit
Sourceval compare : 'a BatOrd.comp -> 'a t BatOrd.comp
Sourceval equal : 'a BatOrd.eq -> 'a t BatOrd.eq
Sourceval ord : 'a BatOrd.ord -> 'a t BatOrd.ord

Override modules

Sourcemodule Labels : sig ... end

Operations on BatVect with labels.

Functorial interface

Sourcemodule type RANDOMACCESS = sig ... end
Sourcemodule Make (R : RANDOMACCESS) (PARAM : sig ... end) : sig ... end
OCaml

Innovation. Community. Security.