开发者

Data structure for a rolling history of position

I have a application reading in offset values from external data source. These offsets are +- from the centrepoint (nom =0).

I would like to save the last minute of these offsets and display them on a scrolling graph. This graph needs to automatically adjust its min/max values according to the data over the last minute.

So i can see this is pointing toward a FIFO queue.

I am using Delphi 7, however when trying to use the TQueue class i cant see any way of accessing a value in the queue (rather only the top of the queue) using Peek().

Is there a better data structure for my problem?

I need to store 60 floating po开发者_运维技巧int numbers, access all of them to display on the graph and determine the maximum value in the queue at any point in time.


Caveat: I don't know Delphi. But in language agnostic terms, you can just maintain some kind of circular buffer with the appropriate size. Back it with an array and have your write pointer cycle through the array indices. Your reads start from write pointer if the buffer is filled, else 0 if it is not.

For finding min/max, there are several things you can do:

  1. The naive approach: recompute min and max every time. O(n). (Not too bad for n=60.)
  2. Slightly better: only recompute min and max when the min or max is popped. (Of course you need to update it for every push too.) O(n).
  3. Asymptotically good: keep your values in a self-balancing binary search tree. Insert into the tree when you push onto the circular buffer and delete from the tree when you pop from the buffer. O(log n).
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜