caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Christophe Raffalli <christophe.raffalli@univ-savoie.fr>
To: caml-list@inria.fr
Subject: Re: [Caml-list] Fwd: interval trees
Date: Fri, 17 Feb 2012 09:11:48 +0100	[thread overview]
Message-ID: <4F3E0BC4.7030702@univ-savoie.fr> (raw)
In-Reply-To: <4F3DA68F.1030402@riken.jp>

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1


Hello,

> 
> Thanks! That's the technique I was looking for
> (Continuation Passing Style), as I may have to use
> this on some other algorithms in the future.
> 
>> let interval_tree intervals =
>>    let rec interval_tree intervals k =
>>     match intervals with
>>       | [] ->  k Empty
>>       | _ ->
>>           let x_mid = median intervals in
>>           let left, mid, right = partition intervals x_mid in
>>           let left_list = L.sort leftmost_bound_first mid in
>>           let right_list = L.sort rightmost_bound_first mid in
>>           interval_tree left (fun left_tree ->
>>             interval_tree right (fun right_tree ->
>>               k (Node(x_mid, left_list, right_list, left_tree,
>> right_tree))))
>>    in interval_tree intervals (fun t ->  t)

There is still one non tail-rec call, and this will basically rebuild
the stack in the heap, with more GC work. So unless you know your trees
are non balanced and left branch are deeper, this code is probably worst.

In the old days (I think now OCaml is clever), It could save some stack
space (and this could retain some pointer), to do the last call by
another function :

let rec interval_tree intervals =
   match intervals with
       [] ->  Empty
     | _ ->
         let x_mid = median intervals in
         let left, mid, right = partition intervals x_mid in
         let left_list = L.sort leftmost_bound_first mid in
         let right_list = L.sort rightmost_bound_first mid in
         last_call x_mid  left_list right_list left right

and last_call x_mid  left_list right_list left right =
         Node (x_mid,
               left_list, right_list,
               interval_tree left, interval_tree right)

Here it makes sure mid and intervals are not stored in the stack frame
... But it may be done anyway now, I am not sure.

Someone knowns the current rules to know what's retain on the stack in
today OCaml ?

Cheers,
Christophe

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.11 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iEYEARECAAYFAk8+C8MACgkQi9jr/RgYAS4nHwCfYX9mY8nHAkz65QQXDerPwfHd
tvgAoIjvVxlDONYRjwpSZS0/5LhhCo44
=OnkD
-----END PGP SIGNATURE-----

  reply	other threads:[~2012-02-17  8:13 UTC|newest]

Thread overview: 20+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-02-10  1:07 Francois Berenger
2012-02-10 18:29 ` Richard W.M. Jones
2012-02-11 17:38   ` Goswin von Brederlow
2012-02-11 17:49     ` Eliot Handelman
2012-02-13  9:13       ` Sebastien Ferre
2012-02-15  1:28         ` Francois Berenger
2012-02-15 15:21           ` Goswin von Brederlow
2012-02-15 17:22             ` Edgar Friendly
2012-02-16  2:48               ` Francois Berenger
2012-02-16  2:32             ` Francois Berenger
2012-02-16  2:42               ` Francois Berenger
2012-02-16  8:34                 ` Goswin von Brederlow
2012-02-16 12:20                   ` John Carr
2012-02-16 10:21                 ` Gabriel Scherer
2012-02-17  0:59                   ` Francois Berenger
2012-02-17  8:11                     ` Christophe Raffalli [this message]
2012-02-11 20:49     ` Edgar Friendly
2012-02-11 23:54       ` Philippe Veber
2012-02-11 10:54 ` Philippe Veber
2012-02-11 17:33   ` Goswin von Brederlow

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=4F3E0BC4.7030702@univ-savoie.fr \
    --to=christophe.raffalli@univ-savoie.fr \
    --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).