(define-values (struct:history
history-constructor
history?
history-field-ref
set-history-field!)
(make-struct-type 'history #f 7 0))
(define history-time-dependant?
(make-struct-field-accessor history-field-ref 0 'time-dependant?))
(define set-history-time-dependant?!
(make-struct-field-mutator set-history-field! 0 'time-dependant?))
(define history-initial-time
(make-struct-field-accessor history-field-ref 1 'initial-time))
(define set-history-initial-time!
(make-struct-field-mutator set-history-field! 1 'initial-time))
(define history-n
(make-struct-field-accessor history-field-ref 2 'n))
(define set-history-n!
(make-struct-field-mutator set-history-field! 2 'n))
(define history-values
(make-struct-field-accessor history-field-ref 3 'values))
(define set-history-values!
(make-struct-field-mutator set-history-field! 3 'values))
(define history-last-value-cell
(make-struct-field-accessor history-field-ref 4 'last-value-cell))
(define set-history-last-value-cell!
(make-struct-field-mutator set-history-field! 4 'last-value-cell))
(define history-durations
(make-struct-field-accessor history-field-ref 5 'durations))
(define set-history-durations!
(make-struct-field-mutator set-history-field! 5 'durations))
(define history-last-duration-cell
(make-struct-field-accessor history-field-ref 6 'last-duration-cell))
(define set-history-last-duration-cell!
(make-struct-field-mutator set-history-field! 6 'last-duration-cell))
(define (make-history time-dependant? time)
(history-constructor time-dependant? time 0 '() #f '() #f))
(define (history-accumulate! history value duration)
(if (= (history-n history) 0)
(let ((new-value-cell (mcons value '()))
(new-duration-cell
(mcons duration '())))
(set-history-n! history 1)
(set-history-values! history new-value-cell)
(set-history-last-value-cell! history new-value-cell)
(set-history-durations! history new-duration-cell)
(set-history-last-duration-cell! history new-duration-cell))
(let ((last-value-cell (history-last-value-cell history))
(last-duration-cell (history-last-duration-cell history)))
(if (or (= value (mcar last-value-cell))
(= 0.0 (mcar last-duration-cell)))
(begin
(set-mcar! last-value-cell value)
(set-mcar! last-duration-cell
(+ duration (mcar last-duration-cell))))
(let ((new-value-cell (mcons value '()))
(new-duration-cell (mcons duration '())))
(set-history-n! history (+ (history-n history) 1))
(set-mcdr! last-value-cell new-value-cell)
(set-history-last-value-cell! history new-value-cell)
(set-mcdr! last-duration-cell new-duration-cell)
(set-history-last-duration-cell! history new-duration-cell))))))
(define (history-tally! history value)
(if (= (history-n history) 0)
(let ((new-value-cell (mcons value '())))
(set-history-n! history 1)
(set-history-values! history new-value-cell)
(set-history-last-value-cell! history new-value-cell))
(let ((last-value-cell (history-last-value-cell history))
(new-value-cell (mcons value '())))
(set-history-n! history (+ (history-n history) 1))
(set-mcdr! last-value-cell new-value-cell)
(set-history-last-value-cell! history new-value-cell))))