1 | (** This module provides simple extensible arrays. *) |
---|
2 | |
---|
3 | (** The type of extensible arrays with elements in ['a]. *) |
---|
4 | type 'a t |
---|
5 | |
---|
6 | (** [make n a] creates an extensible array with initial size [n] |
---|
7 | and default element [a]. The optional argument [buff] sets the |
---|
8 | initial buffer size (16 by default, set to 1 if [buff < 1]). *) |
---|
9 | val make : ?buff:int -> int -> 'a -> 'a t |
---|
10 | |
---|
11 | (** [detault v] returns the default element of v, which fills unspecified |
---|
12 | cells when extending the array. *) |
---|
13 | val default : 'a t -> 'a |
---|
14 | |
---|
15 | (** [get v n] gets the element in [v] at [n]. Raises |
---|
16 | [Invalid_argument "out of bounds"] if [n < 0] or [n >= length v]. *) |
---|
17 | val get : 'a t -> int -> 'a |
---|
18 | |
---|
19 | (** [set v n x] sets the element in [v] at [n] to [x]. Raises |
---|
20 | [Invalid_argument "out of bounds"] if [n < 0], but fills the |
---|
21 | array as needed with [default v] if [n >= length v]. The final length |
---|
22 | is thus [max (length v) (n + 1)]. *) |
---|
23 | val set : 'a t -> int -> 'a -> unit |
---|
24 | |
---|
25 | (** [length v] returns the current length of the array. *) |
---|
26 | val length : 'a t -> int |
---|
27 | |
---|
28 | (** [chop v n] erases the last [n] elements of [v], or all the elements of [v] |
---|
29 | if [n >= length v]. The final length is thus [max 0 (length v - n)]. |
---|
30 | Implementation note: this function does not free any memory. *) |
---|
31 | val chop : 'a t -> int -> unit |
---|
32 | |
---|
33 | (** [extend v n] adds [n] default elements at the end of the array. It is |
---|
34 | equivalent to [append v (default v); ... ; append v (default v)] [n] |
---|
35 | times. *) |
---|
36 | val extend : 'a t -> int -> unit |
---|
37 | |
---|
38 | (** [ensure v n] will ensure [n >= 0] is inside the bounds of the array [v]. |
---|
39 | To this end it will add [default v] elements at the end of [v] if necessary. |
---|
40 | It does nothing if [n < 0]. *) |
---|
41 | val ensure : 'a t -> int -> unit |
---|
42 | |
---|
43 | (** [append v a] appends [a] at the end of [v], with the length of [v] being |
---|
44 | thus raised by [1]. It is equivalent to [set v (length v) a]. *) |
---|
45 | val append : 'a t -> 'a -> unit |
---|
46 | |
---|
47 | (** [reclaim v] will shrink the memory imprint of [v] roughly to a multiple |
---|
48 | of [packet] (16 by default), without changinb [v]'s content. The effect |
---|
49 | depends on garbage collection. *) |
---|
50 | val reclaim : ?packet:int -> 'a t -> unit |
---|
51 | |
---|
52 | (** [iter f v] iterates [f] over the elements of [v]. *) |
---|
53 | val iter : ('a -> unit) -> 'a t -> unit |
---|
54 | (** [fold_left f x v] computes [f (... (f (f x (get v 0)) (get v 1)) ...) |
---|
55 | (get v (n-1))], where [n] is the length of [v]. *) |
---|
56 | val fold_left : ('a -> 'b -> 'a) -> 'a -> 'b t -> 'a |
---|
57 | |
---|
58 | (** [fold_right f v x] computes [f (get v 0) (... (f (get v (n-1)) x)...)], |
---|
59 | where [n] is the length of [v]. *) |
---|
60 | val fold_right : ('a -> 'b -> 'b) -> 'a t -> 'b -> 'b |
---|