From: Eric C. C. <ec...@cm...> - 2003-06-20 22:02:26
|
On Fri, Jun 20, 2003 at 03:58:38PM -0500, Brian Hurt wrote: > Unless I'm missing something (which is possible), there are three possible > implementations of Dynarray that I can see working: > > 1) Have the internal array be a 'a option array, and not a 'a array as it > is currently. [...] > 2) Use the null value kludge. [...] > 3) Write in C, leaving floats unboxed. [...] I'm probably missing some essential context, but what is wrong with the simple approach of using a mutable 'a array that gets replaced with a bigger one when you try to set an element beyond its current length? Something like the following: type 'a t = { mutable data : 'a array; mutable length : int } let make n v = { data = Array.make n v; length = n } let empty () = { data = [||]; length = 0 } let length a = a.length let get a i = if i < a.length then a.data.(i) else raise (Invalid_argument "Dynarray.get") let set a i v = if i >= a.length then begin if i >= Array.length a.data then begin let n = Array.length a.data in let newlen = max (i+1) (2*n) in let newdata = Array.make newlen v in Array.blit a.data 0 newdata 0 n; a.data <- newdata end; a.length <- i + 1 end; a.data.(i) <- v -- Eric C. Cooper e c c @ c m u . e d u |