Module CCVector

Growable, mutable vector

type ro = [
| `RO
]
type rw = [
| `RW
]
type ('a, 'mut) t

The type of a vector of elements of type 'a, with a mutability flat 'mut.

type 'a vector = ('arw) t

Type synonym: a 'a vector is mutable.

type 'a ro_vector = ('aro) t

Alias for immutable vectors.

since
0.15
type 'a sequence = ('a -> unit) -> unit
type 'a klist = unit -> [ `Nil | `Cons of 'a * 'a klist ]
type 'a gen = unit -> 'a option
type 'a equal = 'a -> 'a -> bool
type 'a ord = 'a -> 'a -> int
type 'a printer = Stdlib.Format.formatter -> 'a -> unit
val freeze : ('a_) t -> ('aro) t

Make an immutable vector (no copy! Don't use the old version).

val freeze_copy : ('a_) t -> ('aro) t

Copy the vector into an immutable version.

val create : unit -> ('arw) t

Create a new, empty vector.

val create_with : ?⁠capacity:int -> 'a -> ('arw) t

Create a new vector, using the given value as a filler.

parameter capacity

the size of the underlying array. caution: the value will likely not be GC'd before the vector is.

val return : 'a -> ('a'mut) t

Singleton vector.

since
0.14
val make : int -> 'a -> ('a'mut) t

make n x makes a vector of size n, filled with x.

val init : int -> (int -> 'a) -> ('a'mut) t

Init the vector with the given function and size.

val clear : ('arw) t -> unit

Clear the content of the vector.

val ensure_with : init:'a -> ('arw) t -> int -> unit

Hint to the vector that it should have at least the given capacity.

parameter init

if capacity v = 0, used as a filler element for the underlying array (see create_with).

since
0.14
val ensure : ('arw) t -> int -> unit

Hint to the vector that it should have at least the given capacity. Just a hint, will not be enforced if the vector is empty and init is not provided.

val is_empty : ('a_) t -> bool

Is the vector empty?

val push : ('arw) t -> 'a -> unit

Add an element at the end of the vector.

val append : ('arw) t -> ('a_) t -> unit

append a b adds all elements of b to a.

val append_array : ('arw) t -> 'a array -> unit

Like append, with an array.

val append_seq : ('arw) t -> 'a sequence -> unit

Append content of sequence.

val append_list : ('arw) t -> 'a list -> unit

Append content of list.

since
0.14
val append_gen : ('arw) t -> 'a gen -> unit

Append content of generator.

since
0.20
val equal : 'a equal -> ('a_) t equal
val compare : 'a ord -> ('a_) t ord

Total ordering on vectors. Lexicographic comparison.

exception Empty

Raised on empty stack.

val pop : ('arw) t -> 'a option

Remove last element, or None.

val pop_exn : ('arw) t -> 'a

Remove last element, or raise a Failure if empty.

raises Empty

on an empty vector.

val top : ('a_) t -> 'a option

Top element, if present.

since
0.6
val top_exn : ('a_) t -> 'a

Top element, if present.

raises Empty

on an empty vector.

since
0.6
val copy : ('a_) t -> ('a'mut) t

Shallow copy (may give an immutable or mutable vector).

val shrink : ('arw) t -> int -> unit

Shrink to the given size (remove elements above this size). Does nothing if the parameter is bigger than the current size.

val member : eq:('a -> 'a -> bool) -> 'a -> ('a_) t -> bool

Is the element a member of the vector?

val sort : ('a -> 'a -> int) -> ('a_) t -> ('a'mut) t

Sort the vector, returning a copy of it that is sorted w.r.t the given ordering. The vector itself is unchanged.

val sort' : ('a -> 'a -> int) -> ('arw) t -> unit

Sort the vector in place (modifying it).

val uniq_sort : ('a -> 'a -> int) -> ('arw) t -> unit

Sort the array and remove duplicates, in place (e.g. modifying the vector itself).

val iter : ('a -> unit) -> ('a_) t -> unit

Iterate on the vector's content.

val iteri : (int -> 'a -> unit) -> ('a_) t -> unit

Iterate on the vector, with indexes.

val map : ('a -> 'b) -> ('a_) t -> ('b'mut) t

Map elements of the vector, yielding a new vector.

val map_in_place : ('a -> 'a) -> ('a_) t -> unit

Map elements of the vector in place

since
2.3
val filter : ('a -> bool) -> ('a_) t -> ('a'mut) t

Filter elements from the vector. filter p v leaves v unchanged but returns a new vector that only contains elements of v satisfying p.

val filter' : ('a -> bool) -> ('arw) t -> unit

Filter elements in place.

val fold : ('b -> 'a -> 'b) -> 'b -> ('a_) t -> 'b

Fold on elements of the vector

val exists : ('a -> bool) -> ('a_) t -> bool

Existential test (is there an element that satisfies the predicate?).

val for_all : ('a -> bool) -> ('a_) t -> bool

Universal test (do all the elements satisfy the predicate?).

val find : ('a -> bool) -> ('a_) t -> 'a option

Find an element that satisfies the predicate.

val find_exn : ('a -> bool) -> ('a_) t -> 'a

Find an element that satisfies the predicate, or

raises Not_found

if no element does.

val find_map : ('a -> 'b option) -> ('a_) t -> 'b option

find_map f v returns the first Some y = f x for x in v, or None if f x = None for each x in v.

since
0.14
val filter_map : ('a -> 'b option) -> ('a_) t -> ('b'mut) t

Map elements with a function, possibly filtering some of them out.

val filter_map_in_place : ('a -> 'a option) -> ('a_) t -> unit

Filter-map elements of the vector in place

since
2.3
val flat_map : ('a -> ('b_) t) -> ('a_) t -> ('b'mut) t

Map each element to a sub-vector.

val flat_map_seq : ('a -> 'b sequence) -> ('a_) t -> ('b'mut) t

Like flat_map, but using sequence for intermediate collections.

since
0.14
val flat_map_list : ('a -> 'b list) -> ('a_) t -> ('b'mut) t

Like flat_map, but using list for intermediate collections.

since
0.14
val (>>=) : ('a_) t -> ('a -> ('b_) t) -> ('b'mut) t

Infix version of flat_map.

val (>|=) : ('a_) t -> ('a -> 'b) -> ('b'mut) t

Infix version of map.

val get : ('a_) t -> int -> 'a

Access element by its index, or

raises Invalid_argument

if bad index.

val set : ('arw) t -> int -> 'a -> unit

Modify element at given index, or

raises Invalid_argument

if bad index.

val remove : ('arw) t -> int -> unit

Remove the n-th element of the vector. Does NOT preserve the order of the elements (might swap with the last element).

val rev : ('a_) t -> ('a'mut) t

Reverse the vector.

val rev_in_place : ('arw) t -> unit

Reverse the vector in place.

since
0.14
val rev_iter : ('a -> unit) -> ('a_) t -> unit

rev_iter f a is the same as iter f (rev a), only more efficient.

since
0.14
val size : ('a_) t -> int

Number of elements in the vector.

val length : (__) t -> int

Synonym for size.

val capacity : (__) t -> int

Number of elements the vector can contain without being resized.

val unsafe_get_array : ('arw) t -> 'a array

Access the underlying shared array (do not modify!). unsafe_get_array v is longer than size v, but elements at higher index than size v are undefined (do not access!).

val (--) : int -> int -> (int, 'mut) t

Range of integers, either ascending or descending (both included, therefore the result is never empty). Example: 1 -- 10 returns the vector [1;2;3;4;5;6;7;8;9;10].

val (--^) : int -> int -> (int, 'mut) t

Range of integers, either ascending or descending, but excluding right. Example: 1 --^ 10 returns the vector [1;2;3;4;5;6;7;8;9].

since
0.17
val of_array : 'a array -> ('a'mut) t

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

val of_list : 'a list -> ('a'mut) t
val to_array : ('a_) t -> 'a array

to_array v returns an array corresponding to the vector v.

val to_list : ('a_) t -> 'a list

Return a list with the elements contained in the vector.

val of_seq : ?⁠init:('arw) t -> 'a sequence -> ('arw) t
val to_seq : ('a_) t -> 'a sequence

Return a sequence with the elements contained in the vector.

val to_seq_rev : ('a_) t -> 'a sequence

to_seq_rev v returns the sequence of elements of v in reverse order, that is, the last elements of v are iterated on first.

since
0.14
val slice : ('arw) t -> 'a array * int * int

Vector as an array slice. By doing it we expose the internal array, so be careful!.

val slice_seq : ('a_) t -> int -> int -> 'a sequence

slice_seq v start len is the sequence of elements from v.(start) to v.(start+len-1).

val fill_empty_slots_with : ('a_) t -> 'a -> unit

fill_empty_slots_with v x puts x in the slots of v's underlying array that are not used (ie in the last capacity v - length v slots). This is useful if you removed some elements from the vector and want to be sure they can be GC'd by erasing them from the vector.

since
2.4
val of_klist : ?⁠init:('arw) t -> 'a klist -> ('arw) t
val to_klist : ('a_) t -> 'a klist
val of_gen : ?⁠init:('arw) t -> 'a gen -> ('arw) t
val to_gen : ('a_) t -> 'a gen
val pp : ?⁠start:string -> ?⁠stop:string -> ?⁠sep:string -> 'a printer -> ('a_) t printer