2014-05-06 19:41:08 +02:00
|
|
|
#import "EventWindow.h"
|
|
|
|
|
|
|
|
@implementation EventWindow
|
|
|
|
|
|
|
|
+(EventWindow*) eventWindowWithWindowDuration:(NSTimeInterval)windowDuration {
|
|
|
|
require(windowDuration >= 0);
|
|
|
|
|
|
|
|
EventWindow* w = [EventWindow new];
|
|
|
|
w->windowDuration = windowDuration;
|
|
|
|
w->events = [PriorityQueue priorityQueueAscendingWithComparator:^NSComparisonResult(id obj1, id obj2) {
|
|
|
|
return [(NSNumber*)obj1 compare:(NSNumber*)obj2];
|
|
|
|
}];
|
|
|
|
w->lastWindowEnding = -INFINITY;
|
|
|
|
return w;
|
|
|
|
}
|
|
|
|
|
|
|
|
-(void) addEventAtTime:(NSTimeInterval)eventTime {
|
2014-08-13 02:02:29 +02:00
|
|
|
[events enqueue:@(eventTime)];
|
2014-05-06 19:41:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
-(NSUInteger) countAfterRemovingEventsBeforeWindowEndingAt:(NSTimeInterval)endOfWindowTime {
|
|
|
|
// because values are removed, going backwards will give misleading results.
|
|
|
|
// checking for this case so callers don't get silent bad results
|
|
|
|
// includes a small leeway in case of non-monotonic time source or extended precision lose
|
|
|
|
requireState(endOfWindowTime >= lastWindowEnding-0.03);
|
|
|
|
lastWindowEnding = endOfWindowTime;
|
|
|
|
|
|
|
|
NSTimeInterval startOfWindowTime = endOfWindowTime - windowDuration;
|
2014-11-19 05:17:06 +01:00
|
|
|
while(events.count > 0 && [events.peek doubleValue] < startOfWindowTime) {
|
2014-05-06 19:41:08 +02:00
|
|
|
[events dequeue];
|
|
|
|
}
|
2014-08-14 03:13:24 +02:00
|
|
|
return events.count;
|
2014-05-06 19:41:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@end
|