On Thu, 29 May 2003, Brian Hurt wrote:
> I don't have a problem with a pre-expand, but adding elements out of order
> doesn't work (at least the way I think they work). If the dynarray has n
> elements, the elements are numbers 0..n-1 inclusive.
>
> The solution is to use a 'a option Dynarray.t. You're null element then
> becomes None. I dislike exposing the null elements as "valid entries"- to
> my mind, they're just filler. This is especially important if you
> consider an int Dynarray.t - is that 0 a real, important 0, or a null
> element? So I would would fake up a enum that returns n Nones and append
> it to the dynarray to increase the size. Dynarray.set can then change the
> Nones to Some x.
Yeah, exposing phony nulls was one of the philisophical issues I was
wrestling with as I added my expansion hack. At the time I decided that
the ability to do this outweighed the potential to do something silly (and
I promised myself to always look out for null values (the null I'm using
in this case would never be a valid value, so I can ignore them when I run
across them).
And you're right, expanding the array the way I described it won't work
like I wanted, you have to do something along the lines of:
let null = "";;
let j = DynArray.make 2 null;;
DynArray.add j "blah";;
DynArray.add j "flah";;
(* Now I want to add something to slot 9 *)
ignore (Dynarray.append j (DynArray.init 8 8 null (fun x -> null)));;
DynArray.set j 9 "flee";;
Where the length field is artificially pushed out to the end of the array,
so stuff can be added out of order. But this does expose all those
not-really-null nulls in spots 2 - 8. Now that I think about it this
might be best left out. However, if it is added, it should be called
unsafe_expand or something similar so people realize that they're
willfully doing something "incorrect".
Sigh...why does doing things the right way always interfere with doing
what I think I want...
William D. Neumann
---
"Well I could be a genius, if I just put my mind to it.
And I...I could do anything, if only I could get 'round to it.
Oh we were brought up on the space-race, now they expect you to clean toilets.
When you've seen how big the world is, how can you make do with this?
If you want me, I'll be sleeping in - sleeping in throughout these glory days."
-- Jarvis Cocker
-------------------------------------------------------
This SF.net email is sponsored by: eBay
Get office equipment for less on eBay!
http://adfarm.mediaplex.com/ad/ck/711-11697-6916-5
|
Try Searching:
servers, voip, java, networking, microsoft ...
|
|
|
|