-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathevent_queue.ml
27 lines (23 loc) · 888 Bytes
/
event_queue.ml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
type time = float
type 'a t = Empty | Node of time * 'a * 'a t * 'a t
let empty = Empty
let rec schedule queue time evt =
match queue with
| Empty -> Node (time, evt, Empty, Empty)
| Node (time', evt', left, right) ->
if time <= time' then Node (time, evt, schedule right time' evt', left)
else Node (time', evt', schedule right time evt, left)
let rec remove_top = function
| Empty -> raise Not_found
| Node (_time, _evt, left, Empty) -> left
| Node (_time, _evt, Empty, right) -> right
| Node
( _time
, _evt
, (Node (ltime, levt, _, _) as left)
, (Node (rtime, revt, _, _) as right) ) ->
if ltime <= rtime then Node (ltime, levt, remove_top left, right)
else Node (rtime, revt, left, remove_top right)
let next = function
| Empty -> raise Not_found
| Node (time, evt, _, _) as queue -> (time, evt, remove_top queue)