session-ios/Signal/src/profiling/EventWindow.m

37 lines
1.2 KiB
Mathematica
Raw Normal View History

2014-05-06 19:41:08 +02:00
#import "EventWindow.h"
#import "Util.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 {
[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-08-14 03:13:24 +02: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