I need a class where I can store objects in a stack and when adding one, remove items, which are older than a predefined treshold.
I want to store data each 30 milli seconds in that array (with a timestamp).
When adding a new data object I would like to remove all objects which exceed the treshold from their timestamp and the one which was recently added or will be added.
I created a class with an array of class DataQueueObject
(this class has a variable timestamp).
Now, when pushing a new item, first it checks if there are items in it, then starting from the last, removing it, if it exceeds the maximumQueueTime which is my interval variable
private (set) var objects : [DataQueueObject] = [DataQueueObject]();
private let maximumQueueTime : Double;
func push(item : DataQueueObjects) {
while(objects.count > 0) {
let da : DataQueueObjects = objects.last!;
if((item.timestamp - da.timestamp) > maximumQueueTime) {
objects.removeLast()
}
else {
break;
}
}
objects.append(item);
}
My problem now is that sometimes objects.removeLast
result in a
fatal error:
Array replace: subRange extends past the end