From: Brian Hurt <bhurt@janestcapital.com>
To: Christophe Raffalli <christophe.raffalli@univ-savoie.fr>
Cc: caml-list@inria.fr
Subject: Re: [Caml-list] filter on map
Date: Thu, 07 Jun 2007 09:16:18 -0400 [thread overview]
Message-ID: <46680522.3070501@janestcapital.com> (raw)
In-Reply-To: <4667C188.1060408@univ-savoie.fr>
Christophe Raffalli wrote:
> Dear list members
>
> I just write this piece of code because I wanted filter on map:
>
> module Map_Label = struct
> module M = Map.Make(Label)
> include M
>
> let filter f m =
> M.fold (fun k v m ->
> if f k then M.add k v m else m) m M.empty
> end
>
> But I am now wondering: this is O(n ln n), isn't there an O(n)
> implementation (or just a faster
> implementation). This code insert the keys in increasing order which
> is the worst case for balancing ? Just using a "random_fold" should
> make things better ...
>
> What do you think ?
>
> Cheers,
Take a look at this paper:
http://citeseer.ist.psu.edu/236207.html
It's about red-black trees but it's easy to generalize to all balanced
binary trees.
Since you're generating the elements are already in order, you can build
the resulting tree in O(N).
Does this help?
Brian
prev parent reply other threads:[~2007-06-07 13:16 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2007-06-07 8:27 Christophe Raffalli
2007-06-07 9:11 ` [Caml-list] " Jon Harrop
2007-06-07 13:16 ` Brian Hurt [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=46680522.3070501@janestcapital.com \
--to=bhurt@janestcapital.com \
--cc=caml-list@inria.fr \
--cc=christophe.raffalli@univ-savoie.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