caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Goswin von Brederlow <goswin-v-b@web.de>
To: Gerd Stolpmann <info@gerd-stolpmann.de>
Cc: Goswin von Brederlow <goswin-v-b@web.de>,
	Gabriel Scherer <gabriel.scherer@gmail.com>,
	Edgar Friendly <thelema314@gmail.com>,
	caml-list@inria.fr
Subject: Re: [Caml-list] Can one implement greedy/inline data structures in ocaml?
Date: Fri, 09 Mar 2012 08:29:26 +0100	[thread overview]
Message-ID: <87vcmez0yx.fsf@frosties.localnet> (raw)
In-Reply-To: <1331213502.2826.277.camel@thinkpad> (Gerd Stolpmann's message of "Thu, 08 Mar 2012 14:31:42 +0100")

Gerd Stolpmann <info@gerd-stolpmann.de> writes:

> Am Donnerstag, den 08.03.2012, 06:11 +0100 schrieb Goswin von Brederlow:
>> Gabriel Scherer <gabriel.scherer@gmail.com> writes:
>> 
>> >> So then you need mutable option types or mutables that you initialize
>> >> with dummy values and then overwrite with the real thing once all
>> >> members of a cycle are created. Or some other trickery to make it
>> >> work. Overall cycles are generally not good.
>> >
>> > I believe the trick you need (either passing a dummy value of your
>> > type, or Obj.magic) is less ugly that your Camlp4 solution for inline
>> > access.
>> > If I really needed absolute performance, I'd use the inline version
>> > just like in C, with mutable fields, but without preprocessor sugar.
>> > Preprocessing could avoid a bit of duplication (corresponding to
>> > manual inlining) on the structure-definition side, but wouldn't
>> > simplify the structure-using code much.
>> 
>> How?
>> 
>> type task = {
>>      mutable all_next : task;
>>      mutable all_prev : task;
>>      mutable state_next : task;
>>      mutable state_prev : tast;
>>      ...
>> }
>> 
>> Now how do yout write DList.insert, DList.remove, DList.iter, ...?
>> 
>> I'm looking for some nice tricks using closures, functors, first class
>> modules or something to make it look pretty and easy to use.
>
> There is a solution with functors. However, I don't dare to predict how
> fast it is (functors add additional indirection):
>
> module type DLINKABLE = sig
>   type t
>   val next : t -> t option
>   val prev : t -> t option
>   val set_next : t -> t option -> unit
>   val set_prev : t -> t option -> unit
> end
>
> module type DHEAD = sig
>   type dlist
>   type t
>   val first : dlist -> t option
>   val last : dlist -> t option
>   val set_first : dlist -> t option -> unit
>   val set_last : dlist -> t option -> unit
> end
>
> module DLIST(H : DHEAD)(L : DLINKABLE with type t = H.t) = struct
>   (* Here define operations over lists, e.g. *)
>
>   let remove (dl:H.dlist) (x:D.t) =
>     ( match D.prev x with
>         | None ->
>             H.set_first dl (D.next x)
>         | Some p ->
>             D.set_next (D.next x)
>     );
>     ( match D.next x with
>         | None ->
>             H.set_last dl (D.prev x)
>         | Some n ->
>             D.set_prev (D.prev x)
>     )
> end
>
> For a concrete application you would only have to implement DLINKABLE
> and DHEAD, and then apply DLIST on these implementations. If elements
> are members of several lists, do this for each list separately. I guess
> you could define macros for generating DLINKABLE and DHEAD (even the
> macros built into camlp4 could be good enough for this).
>
> Gerd

That is a start. But you used an open ended doubly linked list. This
makes it simpler because you can have an empty list and initialize items
to not be linked to anything before inserting them. That avoids the
cycles.

If you have a closed (cyclic or ring) doubly linked list the problem
becomes how to create the first element that must link to itself. Can
you come up with something without option type or Obj.magic? Something
that doesn't duplicate the initialization of the DList for each
instance.

MfG
        Goswin

      reply	other threads:[~2012-03-09  7:29 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-03-07 14:41 Goswin von Brederlow
2012-03-07 15:00 ` Edgar Friendly
2012-03-07 16:32   ` Goswin von Brederlow
2012-03-07 16:58     ` Gabriel Scherer
2012-03-08  5:11       ` Goswin von Brederlow
2012-03-08  8:51         ` Gabriel Scherer
2012-03-09  7:50           ` Goswin von Brederlow
2012-03-09  9:21             ` Gabriel Scherer
2012-03-09 13:29               ` Markus Weißmann
2012-03-10 12:37                 ` Goswin von Brederlow
2012-03-09 15:43               ` Hezekiah M. Carty
2012-03-10 12:49               ` Goswin von Brederlow
2012-03-08 13:31         ` Gerd Stolpmann
2012-03-09  7:29           ` Goswin von Brederlow [this message]

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=87vcmez0yx.fsf@frosties.localnet \
    --to=goswin-v-b@web.de \
    --cc=caml-list@inria.fr \
    --cc=gabriel.scherer@gmail.com \
    --cc=info@gerd-stolpmann.de \
    --cc=thelema314@gmail.com \
    /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).