Lookup structure for handling future events (time based)

前端 未结 4 686
庸人自扰
庸人自扰 2021-02-10 15:31

I am looking for an efficient data structure, that\'ll allow me to cue events ... that is, i will be having an app, where at any time in execution, it is possible, that an event

4条回答
  •  星月不相逢
    2021-02-10 15:55

    I believe you're looking for a Priority Queue with the timestamp of when the event occurs being the priority (well, lower timestamps would be higher priority)

    Just a little elucidation with your use cases:

    ... where i can put in any event in any time in the future...

    You'd insert into the priority queue with insertWithPriority, using the timestamp of when the event occurs. This would be O(lgN)

    ... and where i can get and (by doing so) remove all due events ...

    You'd repeatedly call getTop (gets event with the lowest timestamp) collecting all elements up to the time of interest.

    ... also, a plus would be, if i were able to remove an event from the datastructure (because it was canceled) ... not too important though, since i can simply flag it as cancelled ..

    This would be possible, but would be O(lgN) due to rebalancing.

提交回复
热议问题