Re: Size of arrays

From: Pierre Weis (Pierre.Weis@inria.fr)
Date: Sun Jan 24 1999 - 23:31:42 MET


From: Pierre Weis <Pierre.Weis@inria.fr>
Message-Id: <199901242231.XAA12515@pauillac.inria.fr>
Subject: Re: Size of arrays
To: jjgarcia@ind-cr.uclm.es (Juan Jose Garcia Ripoll)
Date: Sun, 24 Jan 1999 23:31:42 +0100 (MET)
In-Reply-To: <36A9D5F0.F9215F45@ind-cr.uclm.es> from "Juan Jose Garcia Ripoll" at Jan 23, 99 03:00:16 pm

> I've noticed that OCaml imposes a very small upper limit in the size of
> arrays: 8Mb or so, I think. Can this limit be modified somehow?
[...]

No there is no way to modify this limit: it is wired into the data
structures representation algorithm. However, this limit is machine
dependant, and very high on a 64bits machine
(18014398509481983).

Anyway, it is not difficult to design an ADT that gives you large
arrays on a 32 bits machine:

(* Module Bvect of big vectors (length up-to max_int).
   Access and assigment 2 times slowlier than regular arrays.
   Iteration is as efficient as Array.iter. *)
(* bvect.mli *)
type 'a bvect;;
val make : int -> 'a -> 'a bvect;;
val length : 'a bvect -> int;;
val item : 'a bvect -> int -> 'a;;
val assign : 'a bvect -> int -> 'a -> unit;;
....

(* bvect.ml *)
type 'a bvect = 'a array * 'a array;;

let make len init =
 if len <= Sys.max_array_length then (Array.make len init, [| |])
 else (Array.make Sys.max_array_length init,
       Array.make (len - Sys.max_array_length) init);;

let length (v1, v2) = Array.length v1 + Array.length v2;;

let item (v1, v2) i =
 if i < Sys.max_array_length then v1.(i)
 else v2.(i - Sys.max_array_length);;

let assign (v1, v2) i item =
 if i < Sys.max_array_length then v1.(i) <- item
 else v2.(i - Sys.max_array_length) <- item;;

let iter f (v1, v2) = Array.iter f v1; Array.iter f v2;;

(* Omitted, sub_vect, map_vect, ..., as in module Array. *)

Hope this helps,

Pierre Weis

INRIA, Projet Cristal, Pierre.Weis@inria.fr, http://cristal.inria.fr/~weis/



This archive was generated by hypermail 2b29 : Sun Jan 02 2000 - 11:58:18 MET