Module Ctypes.CArray
Operations on C arrays.
type 'a t
= 'a carray
val get : 'a t -> int -> 'a
get a n
returns then
th element of the zero-indexed arraya
. The semantics for non-scalar types are non-copying, as for(!@)
.If you rebind the
CArray
module toArray
then you can also use the syntaxa.(n)
instead ofArray.get a n
.Raise
Invalid_argument "index out of bounds"
ifn
is outside of the range0
to(CArray.length a - 1)
.
val set : 'a t -> int -> 'a -> unit
set a n v
overwrites then
th element of the zero-indexed arraya
withv
.If you rebind the
CArray
module toArray
then you can also use thea.(n) <- v
syntax instead ofArray.set a n v
.Raise
Invalid_argument "index out of bounds"
ifn
is outside of the range0
to(CArray.length a - 1)
.
val unsafe_get : 'a t -> int -> 'a
unsafe_get a n
behaves likeget a n
except that the check thatn
between0
and(CArray.length a - 1)
is not performed.
val unsafe_set : 'a t -> int -> 'a -> unit
unsafe_set a n v
behaves likeset a n v
except that the check thatn
between0
and(CArray.length a - 1)
is not performed.
val of_string : string -> char t
of_string s
builds an array of the same length ass
, and writes the elements ofs
to the corresponding elements of the array.
val of_list : 'a typ -> 'a list -> 'a t
of_list t l
builds an array of typet
of the same length asl
, and writes the elements ofl
to the corresponding elements of the array.
val to_list : 'a t -> 'a list
to_list a
builds a list of the same length asa
such that each element of the list is the result of reading the corresponding element ofa
.
val iter : ('a -> unit) -> 'a t -> unit
iter f a
is analogous toArray.iter f a
: it appliesf
in turn to all the elements ofa
.
val map : 'b typ -> ('a -> 'b) -> 'a t -> 'b t
map t f a
is analogous toArray.map f a
: it creates a new array with element typet
whose elements are obtained by applyingf
to the elements ofa
.
val mapi : 'b typ -> (int -> 'a -> 'b) -> 'a t -> 'b t
mapi
behaves likeArray
.mapi, except that it also passes the index of each element as the first argument tof
and the element itself as the second argument.
val fold_left : ('a -> 'b -> 'a) -> 'a -> 'b t -> 'a
CArray.fold_left (@) x a
computes(((x @ a.(0)) @ a.(1)) ...) @ a.(n-1)
wheren
is the length of the arraya
.
val fold_right : ('b -> 'a -> 'a) -> 'b t -> 'a -> 'a
CArray.fold_right f a x
computesa.(0) @ (a.(1) @ ( ... (a.(n-1) @ x) ...))
wheren
is the length of the arraya
.
val length : 'a t -> int
Return the number of elements of the given array.
val from_ptr : 'a ptr -> int -> 'a t
from_ptr p n
creates ann
-length array reference to the memory at addressp
.
val make : ?finalise:('a t -> unit) -> 'a typ -> ?initial:'a -> int -> 'a t
make t n
creates ann
-length array of typet
. If the optional argument?initial
is supplied, it indicates a value that should be used to initialise every element of the array. The argument?finalise
, if present, will be called just before the memory is freed.