From: Jean-Christophe Filliatre <Jean-Christophe.Filliatre@lri.fr>
To: Brian Hurt <brian.hurt@qlogic.com>
Cc: "Yaron M. Minsky" <yminsky@CS.Cornell.EDU>,
Caml List <caml-list@inria.fr>
Subject: Re: [Caml-list] Re: Wanted - General Purpose "Glue Logic" Data-Structures
Date: Thu, 10 Apr 2003 10:12:26 +0200 [thread overview]
Message-ID: <16021.10090.355061.746312@gargle.gargle.HOWL> (raw)
In-Reply-To: <Pine.LNX.4.33.0304091302360.2225-100000@eagle.ancor.com>
In the solution I proposed, you do not deallocate the array block when
you delete the last element; only the size is decreased.
Actually, I didn't use this trick to implement resizeable arrays, but
to implement heaps encoded in (resizeable) arrays---available from the
hump; but I think the same idea applies.
--
Jean-Christophe
Brian Hurt writes:
>
> This doesn't work, or at least doesn't work well. What happens when you
> delete the value being used as the null value? You have to go through and
> reset all the null values to the new null value. Also, you want to keep
> reallocations to a minimum. If you hit the point where you keep adding
> and deleting the last element, you have to keep allocating and
> deallocating the array block.
>
> Brian
>
>
> On Wed, 9 Apr 2003, Jean-Christophe Filliatre wrote:
>
> >
> > Yaron M. Minsky writes:
> > > Resizeable arrays seem like a
> > > pretty essential data structure, and the fact that you can't implement
> > > them nicely without breaking the standard compiler abstractions (due to
> > > the dummy value issue) argues in favor of including it in the
> > > distribution, I would think.
> >
> > There is an easy trick for this dummy value issue.
> >
> > What you want is an interface looking like:
> >
> > ======================================================================
> > type 'a t (* resizeable vectors *)
> > val create : int -> 'a t (* only initial capacity is given *)
> > val add : 'a t -> 'a -> unit
> > ...
> > ======================================================================
> >
> > The idea is to initially store the capacity as a negative size, thus
> > remembering that the data array still needs initialization:
> >
> > ======================================================================
> > type 'a t = { mutable size : int; mutable data : 'a array }
> >
> > let create n =
> > if n <= 0 then invalid_arg "create";
> > { size = -n; data = [||] }
> > ======================================================================
> >
> > Note that being empty is having a size less or equal than zero:
> >
> > ======================================================================
> > let is_empty v = v.size <= 0
> > ======================================================================
> >
> > Then the first time an addition is performed, you allocate the array:
> >
> > ======================================================================
> > let add v x =
> > if v.size < 0 then begin (* first addition: we allocate the array *)
> > v.data <- Array.create (- v.size) x; v.size <- 0
> > end;
> > (* code to insert x, resizing data if needed *)
> > ...
> > ======================================================================
> >
> > Hope this helps,
> >
-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
next prev parent reply other threads:[~2003-04-10 8:23 UTC|newest]
Thread overview: 15+ messages / expand[flat|nested] mbox.gz Atom feed top
2003-03-20 2:33 [Caml-list] " John Gerard Malecki
2003-03-20 16:53 ` Brian Hurt
2003-03-20 17:36 ` Matthew W. Boyd
2003-03-24 6:08 ` Nicolas Cannasse
2003-04-08 0:59 ` [Caml-list] " Wheeler Ruml
2003-04-08 9:12 ` Markus Mottl
2003-04-08 12:03 ` Yaron M. Minsky
2003-04-09 6:51 ` Jean-Christophe Filliatre
2003-04-09 18:12 ` Brian Hurt
2003-04-10 8:12 ` Jean-Christophe Filliatre [this message]
2003-04-10 10:35 ` Markus Mottl
2003-04-10 15:30 ` David Brown
2003-04-10 15:03 ` Brian Hurt
2003-04-08 15:07 ` Brian Hurt
2003-04-08 16:38 ` John Gerard Malecki
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=16021.10090.355061.746312@gargle.gargle.HOWL \
--to=jean-christophe.filliatre@lri.fr \
--cc=brian.hurt@qlogic.com \
--cc=caml-list@inria.fr \
--cc=yminsky@CS.Cornell.EDU \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox