From: Brian Hurt <bhurt@spnz.org>
To: tmp123@menta.net
Cc: caml-list@yquem.inria.fr
Subject: Re: [Caml-list] Sorted list
Date: Sat, 4 Aug 2007 08:15:25 -0400 (EDT) [thread overview]
Message-ID: <Pine.LNX.4.64.0708040754370.20887@localhost> (raw)
In-Reply-To: <46B4485B.7040406@menta.net>
On Sat, 4 Aug 2007, tmp123@menta.net wrote:
> Hello,
>
> In order to implement a timers subsystem, I need a module with the following
> values:
>
> *) add_timer : time -> ( unit -> unit ) -> timerid,
> for public usage, where "add_timer t f" means execute "f" at time "t". The
> result is an identifier who allows cancelation of the timer.
> *) cancel_timer: timerid -> unit
> for public usage, cancel a previously added timer
> *) peek_minimum_timer : unit -> ( time, unit -> unit )
> for internal usage, get the timer with minimum time. Returns the time and the
> related function.
> *) remove_minimum_timer : ?? -> unit
> for internal usage, remove the timer with the minimum time. It will be called
> after execute a timer.
>
> Thus, in a generic sense, what I need is a "sorted list", with easy insertion
> of elements, and peek/pop of the head (minimum).
>
> Of the standard modules, the most similar seems "set", because allows
> insertion and has the funcion "min_elt". However, the problem is, if two
> timers have the same time, addition of the second one removes the first.
>
> Please, has someone any sugestion?
What you want is a priority queue.
Here's a simple one: a leftist priority queue. The core operation is
join, which takes two priority queues and joins them together:
module type Req = sig
type t
val compare : t -> t -> int
end
module Make(Ord: Req) = struct
type elem = Ord.t;;
type t =
| Node of int * t * Ord.t * t
| Empty
;;
let height = function
| Empty -> 0
| Node(h, _, _, _) -> h
;;
let join p q =
match p, q with
| Empty, Empty -> Empty
| Empty, _ -> q
| _, Empty -> p
| Node(_, pl, px, pr), Node(_, ql, qx, qr) ->
let x, l, m, r =
if (Ord.compare px qx) <= 0 then
px, pl, pr, q
else
qx, p ql, qr
in
(* pick the tallest of l, m, and r to be the new left
* node. The new right node is the join of the remaining
* two nodes.
*)
let left, right =
if (height l) >= (height m) then
if (height l) >= (height r) then
l, (join m r)
else
r, (join l m)
else
if (height m) >= (height r) then
m, (join l r)
else
r, (join l m)
in
let h = 1 + (max (height left) (height right)) in
Node(h, left, x, right)
;;
let empty = Empty;;
let singelton x = Node(1, Empty, x, Empty);;
let is_empty = function
| Empty -> true
| Node(_, _, _, _) -> false
;;
let get_head = function
| Empty -> invalid_arg "head on empty priority queue!"
| Node(_, _, x, _) -> x
;;
let remove_head = function
| Empty -> invalid_arg "remove_head on empty priority queue!"
| Node(_, l, _, r) -> join l r
;;
let add q x =
let p = singleton x in
join q p
;;
let rec filter f = function
| Empty -> Empty
| Node(_, l, x, r) ->
if f x then
join (join (filter l) (singleton x)) (filter r)
else
join (filter l) (filter r)
;;
The one problem with this is that filter is O(N log N). I'd probably just
add a bit of mutable data to the timer structure, a boolean flag to say
wether it's been deleted our not. Then, when you remove the head node,
you keep removing the head node while it's been deleted (i.e. a deleted
node doesn't get removed until it times out). If the number of deleted
nodes reaches some sufficiently high percentage, you can then afford to
filter the whole table and delete all of them at once.
Brian
next prev parent reply other threads:[~2007-08-04 12:04 UTC|newest]
Thread overview: 40+ messages / expand[flat|nested] mbox.gz Atom feed top
2007-08-04 9:35 tmp123
2007-08-04 10:00 ` [Caml-list] " Jon Harrop
2007-08-04 10:29 ` Philippe Wang
2007-08-04 11:22 ` skaller
2007-08-04 12:23 ` Philippe Wang
2007-08-04 13:39 ` skaller
2007-08-04 14:01 ` Philippe Wang
2007-08-04 14:45 ` skaller
2007-08-04 14:27 ` tmp123
2007-08-04 20:33 ` Christophe TROESTLER
2007-08-04 14:37 ` tmp123
2007-08-04 15:09 ` Brian Hurt
2007-08-04 15:42 ` skaller
2007-08-04 16:21 ` Richard Jones
2007-08-04 17:17 ` Brian Hurt
2007-08-04 18:24 ` skaller
2007-08-04 17:54 ` skaller
2007-08-04 19:16 ` Richard Jones
2007-08-05 16:22 ` David Allsopp
2007-08-05 16:41 ` Xavier Leroy
2007-08-05 17:01 ` David Allsopp
2007-08-04 17:35 ` Julien Moutinho
2007-08-04 18:04 ` skaller
2007-08-05 1:47 ` Jon Harrop
2007-08-05 11:44 ` Erik de Castro Lopo
2007-08-05 12:03 ` Jacques GARRIGUE
2007-08-05 12:31 ` Erik de Castro Lopo
2007-08-05 13:22 ` Richard Jones
2007-08-05 20:47 ` Erik de Castro Lopo
2007-08-05 13:17 ` Richard Jones
2007-08-05 16:26 ` Xavier Leroy
2007-08-05 23:47 ` skaller
2007-08-04 15:36 ` skaller
2007-08-04 15:17 ` tmp123
2007-08-12 12:05 ` Andrej Bauer
2007-08-04 12:15 ` Brian Hurt [this message]
2007-08-04 12:36 ` Brian Hurt
2007-08-04 13:49 ` skaller
2007-08-04 12:16 ` Daniel Bünzli
2007-08-04 12:58 ` Oliver Bandel
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=Pine.LNX.4.64.0708040754370.20887@localhost \
--to=bhurt@spnz.org \
--cc=caml-list@yquem.inria.fr \
--cc=tmp123@menta.net \
/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