multiArray.mli 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. (*
  2. * MultiArray - Resizeable Ocaml big arrays
  3. * Copyright (C) 201 Nicolas Cannasse
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2.1 of the License, or (at your option) any later version,
  9. * with the special exception on linking described in file LICENSE.
  10. *
  11. * This library is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with this library; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  19. *)
  20. (** Dynamic Big arrays.
  21. A dynamic array is equivalent to a OCaml array that will resize itself
  22. when elements are added or removed. MultiArray is different from DynArray
  23. since it allows more than 4 Millions elements on 32 bits systems.
  24. A MultiArray of size <= Sys.max_array_length will use a single indirection
  25. internal representation. If the size exceeds Sys.max_array_length, e.g. by
  26. adding an additional element, the internal representation is promoted to use
  27. double indirection. This allows for bigger arrays, but it also slower.
  28. *)
  29. type 'a t
  30. exception Invalid_arg of int * string * string
  31. (** When an operation on an array fails, [Invalid_arg] is raised. The
  32. integer is the value that made the operation fail, the first string
  33. contains the function name that has been called and the second string
  34. contains the parameter name that made the operation fail.
  35. *)
  36. (** {6 MultiArray creation} *)
  37. val create : unit -> 'a t
  38. (** [create()] returns a new empty dynamic array. *)
  39. val make : int -> 'a -> 'a t
  40. (** [make count value] returns an array with some memory already allocated and
  41. [count] elements initialized to [value]. *)
  42. val init : int -> (int -> 'a) -> 'a t
  43. (** [init n f] returns an array of [n] elements filled with values
  44. returned by [f 0 , f 1, ... f (n-1)]. *)
  45. (** {6 MultiArray manipulation functions} *)
  46. val empty : 'a t -> bool
  47. (** Return true if the number of elements in the array is 0. *)
  48. val length : 'a t -> int
  49. (** Return the number of elements in the array. *)
  50. val get : 'a t -> int -> 'a
  51. (** [get darr idx] gets the element in [darr] at index [idx]. If [darr] has
  52. [len] elements in it, then the valid indexes range from [0] to [len-1]. *)
  53. val set : 'a t -> int -> 'a -> unit
  54. (** [set darr idx v] sets the element of [darr] at index [idx] to value
  55. [v]. The previous value is overwritten. *)
  56. val add : 'a t -> 'a -> unit
  57. (** [add darr v] appends [v] onto [darr]. [v] becomes the new
  58. last element of [darr]. If required, the size of the internal representation
  59. is doubled. If this would exceed Sys.max_array_length, the internal
  60. representation is automatically changed to double indirection and the
  61. current contents are copied over. *)
  62. val clear : 'a t -> unit
  63. (** remove all elements from the array and resize it to 0. *)
  64. (** {6 MultiArray copy and conversion} *)
  65. val of_array : 'a array -> 'a t
  66. (** [of_array arr] returns an array with the elements of [arr] in it
  67. in order. *)
  68. val of_list : 'a list -> 'a t
  69. (** [of_list lst] returns a dynamic array with the elements of [lst] in
  70. it in order. *)
  71. (** {6 MultiArray functional support} *)
  72. val iter : ('a -> unit) -> 'a t -> unit
  73. (** [iter f darr] calls the function [f] on every element of [darr]. It
  74. is equivalent to [for i = 0 to length darr - 1 do f (get darr i) done;] *)
  75. val iteri : (int -> 'a -> unit) -> 'a t -> unit
  76. (** [iter f darr] calls the function [f] on every element of [darr]. It
  77. is equivalent to [for i = 0 to length darr - 1 do f i (get darr i) done;]
  78. *)
  79. val map : ('a -> 'b) -> 'a t -> 'b t
  80. (** [map f darr] applies the function [f] to every element of [darr]
  81. and creates a dynamic array from the results - similar to [List.map] or
  82. [Array.map]. *)
  83. val mapi : (int -> 'a -> 'b) -> 'a t -> 'b t
  84. (** [mapi f darr] applies the function [f] to every element of [darr]
  85. and creates a dynamic array from the results - similar to [List.mapi] or
  86. [Array.mapi]. *)
  87. val fold_left : ('b -> 'a -> 'b) -> 'b -> 'a t -> 'b
  88. (** [fold_left f x darr] computes
  89. [f ( ... ( f ( f (get darr 0) x) (get darr 1) ) ... ) (get darr n-1)],
  90. similar to [Array.fold_left] or [List.fold_left]. *)