(define-values (struct:event
event-constructor
event?
event-field-ref
set-event-field!)
(make-struct-type 'event #f 7 0))
(define event-time
(make-struct-field-accessor event-field-ref 0 'time))
(define set-event-time!
(make-struct-field-mutator set-event-field! 0 'time))
(define event-priority
(make-struct-field-accessor event-field-ref 1 'priority))
(define set-event-priority!
(make-struct-field-mutator set-event-field! 1 'priority))
(define event-process
(make-struct-field-accessor event-field-ref 2 'process))
(define set-event-process!
(make-struct-field-mutator set-event-field! 2 'process))
(define event-function
(make-struct-field-accessor event-field-ref 3 'function))
(define set-event-function!
(make-struct-field-mutator set-event-field! 3 'function))
(define event-arguments
(make-struct-field-accessor event-field-ref 4 'arguments))
(define set-event-arguments!
(make-struct-field-mutator set-event-field! 4 'arguments))
(define event-event-list
(make-struct-field-accessor event-field-ref 5 'event-list))
(define set-event-event-list!
(make-struct-field-mutator set-event-field! 5 'event-list))
(define event-linked-event-list
(make-struct-field-accessor event-field-ref 6 'linked-event-list))
(define set-event-linked-event-list!
(make-struct-field-mutator set-event-field! 6 'linked-event-list))
(define (make-event time priority process function arguments)
(event-constructor time priority process function arguments #f #f))
(define-values (struct:event-list
event-list-constructor
event-list?
event-list-field-ref
set-event-list-field!)
(make-struct-type 'event-list #f 1 0))
(define event-list-events
(make-struct-field-accessor event-list-field-ref 0 'events))
(define set-event-list-events!
(make-struct-field-mutator set-event-list-field! 0 'events))
(define (make-event-list)
(event-list-constructor '()))
(define (event-list-empty? event-list)
(eq? (event-list-events event-list) '()))
(define (event-list-add! event-list event)
(let ((events (event-list-events event-list))
(previous #f))
(let loop ()
(when (and (not (null? events))
(or (> (event-time event)
(event-time (mcar events)))
(and (= (event-time event)
(event-time (mcar events)))
(<= (event-priority event)
(event-priority (mcar events))))))
(set! previous events)
(set! events (mcdr events))
(loop)))
(set-event-event-list! event event-list)
(if previous
(set-mcdr! previous (mcons event events))
(set-event-list-events! event-list (mcons event events)))))
(define event-list-remove!
(case-lambda
((event-list event)
(let loop ((previous #f)
(events (event-list-events event-list)))
(when (not (null? events))
(if (eq? event (mcar events))
(begin
(if previous
(set-mcdr! previous (mcdr events))
(set-event-list-events!
event-list (mcdr events)))
(set-event-event-list! event #f))
(loop events (mcdr events))))))
((event)
(when (event-event-list event)
(event-list-remove! (event-event-list event) event)))))
(define (event-list-pop! event-list)
(let* ((events (event-list-events event-list))
(event (mcar events)))
(set-event-list-events! event-list (mcdr events))
(set-event-event-list! event #f)
event))