caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Max Kirillov <max630@mail.ru>
To: caml-list@inria.fr
Subject: Re: [Caml-list] Doubly-linked list
Date: Wed, 14 Aug 2002 00:16:25 +0700	[thread overview]
Message-ID: <20020814001625.A4141@max.home> (raw)
In-Reply-To: <200208131611.MAA00208@dewberry.cc.columbia.edu>; from oleg_inconnu@myrealbox.com on Tue, Aug 13, 2002 at 12:12:45PM -0400

[-- Attachment #1: Type: text/plain, Size: 477 bytes --]

On Tue, Aug 13, 2002 at 12:12:45PM -0400, Oleg wrote:
> type 'a dlist = {mutable prev : 'a dlist option; 
>                      mutable next : 'a dlist option; 
>                      info : 'a}


> Maybe I'll write it in the next few days. What kind of bothers me is the need 
> to have iterators (a la STL) as helper types.

Unless you need thread-safe behavior, that could be just
"'a dlist ref". I tried to do some fast things. See
attachment.  That's just an idea.

Max.

[-- Attachment #2: dlist.ml --]
[-- Type: text/plain, Size: 1047 bytes --]


type 'a t = {mutable prev : 'a t option;
            mutable next : 'a t option;
            info : 'a}

type 'a dlist = {mutable front: 'a t;
		mutable back: 'a t}

exception Bound

next = function {next=Some obj1} -> obj1 | _ -> raise Bound
prev = function {prev=Some obj1} -> obj1 | _ -> raise Bound

takeNext objR = objR:=(next !objR)
takePrev objR = objR:=(prev !objR)

add_before v obj =
	let newObj = {prev=None;next=obj;info=v} in
	(match obj
	    with {prev=Some obj1} -> (newObj.prev<-obj1;
				    obj1.next<-newObj)
		| {prev=None} -> ());
	obj.prev=newObj
	    
add_after v obj =
	let newObj = {next=None;prev=obj;info=v} in
	(match obj
	    with {next=Some obj1} -> (newObj.next<-obj1;
				    obj1.prev<-newObj)
		| {next=None} -> ());
	obj.next=newObj

(* Hmm... I'm not clear whether front is after back or before it.
    Depends on moving direction:) Let's suppose before *)

push_back v ({back=obj} as data) = add_after v obj; data.back <- next obj
push_front v ({front=obj} as data) = add_before v obj; data.front <- prev obj

  reply	other threads:[~2002-08-13 17:26 UTC|newest]

Thread overview: 23+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2002-08-13  8:00 Oleg
2002-08-13  8:54 ` Markus Mottl
2002-08-13 15:52   ` Oleg
2002-08-13 11:00 ` Diego Olivier Fernandez Pons
2002-08-13 14:30   ` Oleg
2002-08-13 15:11     ` Anton Moscal
2002-08-13 16:12       ` Oleg
2002-08-13 17:16         ` Max Kirillov [this message]
2002-08-14  0:49           ` Max Kirillov
2002-08-13 18:23         ` Anton E. Moscal
2002-08-13 16:16   ` Brian Rogoff
2002-08-14  8:13     ` Diego Olivier Fernandez Pons
2002-08-14 15:43       ` Brian Rogoff
2002-08-19 10:38         ` Diego Olivier Fernandez Pons
2002-08-19 15:58           ` Polymorphic recursion 9Was Re: [Caml-list] Doubly-linked list) Brian Rogoff
2002-08-21  8:04             ` Diego Olivier Fernandez Pons
2002-08-21 15:48               ` Brian Rogoff
2002-08-23  8:14                 ` Diego Olivier Fernandez Pons
2002-08-23 21:57               ` John Max Skaller
2002-08-27 13:00                 ` Diego Olivier Fernandez Pons
2002-08-28 14:50                   ` John Max Skaller
2002-08-28 17:27                     ` [Caml-list] FELIX (was: Polymorphic recursion) Oleg
2002-08-19 23:17 ` [Caml-list] Doubly-linked list james woodyatt

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=20020814001625.A4141@max.home \
    --to=max630@mail.ru \
    --cc=caml-list@inria.fr \
    /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;
as well as URLs for NNTP newsgroup(s).