From: Jerome Vouillon <Jerome.Vouillon@inria.fr>
To: Steve Stevenson <steve@cs.clemson.edu>, caml-list@inria.fr
Subject: Re: Imperative list operations
Date: Wed, 15 Sep 1999 14:35:24 +0200 [thread overview]
Message-ID: <19990915143524.40888@pauillac.inria.fr> (raw)
In-Reply-To: <14302.41638.913957.615588@merlin.cs.clemson.edu>; from Steve Stevenson on Tue, Sep 14, 1999 at 03:36:18PM -0400
On Tue, Sep 14, 1999 at 03:36:18PM -0400, Steve Stevenson wrote:
> I need a double-ended queue implementation. The lists
> can get very long, so I would like to use imperative operations to
> change the links.
There are some very nice and quite efficient purely functional
implementations of double-ended queues. I suggest you to have a look
at Chris Okasaki's work on http://www.cs.columbia.edu/~cdo/papers.html
(in particular, "Simple Confluently Persistent Catenable Lists",
"Catenable Double-Ended Queues" and "Simple and Efficient Purely
Functional Queues and Deques").
> I've tried all the naïve type declarations --- all of which
> don't seem to work. I've tried the age old tricks. What am I not
> understanding? or doing right?
It is hard to guess without knowing what you have done...
You can use the following type definition :
type 'a node =
{ mutable prev : 'a list; mutable next : 'a list; value : 'a }
and 'a list = 'a node option;;
type 'a dequeue = { mutable head : 'a list; mutable tail : 'a list}
A double-ended queue has a pointer to the head of the list and a
pointer to its tail. A list can either be empty (None) or contain a
sequence of nodes. A node holds a pointer to the nodes that precedes
it and a pointer to the nodes that follows it.
Ther is some space overhead in using option types. So, you could also
use a circular list. The type definition would be :
type 'a node =
{ mutable prev : 'a node; mutable next : 'a node; val : 'a }
type 'a dequeue = 'a node option ref
A double-ended queue is either empty (None) or point to the head of
the circular list. Each node has a pointer to the previous node and
the next node in the circular list.
Insertion and removal looks something like that :
let insert_front d v =
match !d with
None ->
let rec node = { prev = node; next = node; value = v } in
d := Some node
| Some n' ->
let n = { prev = n'.prev; next = n'; value = v } in
n'.prev.next <- n; n'.prev <- n;
d := Some n;;
let remove_front d =
match !d with
None ->
raise Not_found
| Some n when n.next == n ->
d := None;
n.value
| Some n ->
n.next.prev <- n.prev; n.prev.next <- n.next;
d := Some n.next;
n.value;;
Regards,
-- Jérôme
next prev parent reply other threads:[~1999-09-17 11:06 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
1999-09-14 19:36 Steve Stevenson
1999-09-15 12:35 ` Jerome Vouillon [this message]
1999-09-15 13:09 ` Steve Stevenson
1999-09-15 13:33 ` John Prevost
1999-09-15 14:35 ` Stefan Monnier
1999-09-17 12:45 ` Markus Mottl
1999-09-16 14:06 ` Christophe Raffalli
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=19990915143524.40888@pauillac.inria.fr \
--to=jerome.vouillon@inria.fr \
--cc=caml-list@inria.fr \
--cc=steve@cs.clemson.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