James Bond
James Bond

Reputation: 7913

how to design a fixed TIME length circular buffer?

boost::circular_buffer cann provide a fixed length buffer, e.g., at size of 5.

Imaging that I have realtime data stream coming in with timestamp. I want to keep a buffer of all the elemens within the last 5 minutes.

Naively, i can build a wrapper of std::list, everytime a new data point D coming in, I push_back(D), and then do a while loop to pop_front() all the data point older than 5 minutes.

The problem with such a design is that, I have to construct a new instance for every point, this seems to be a waste of time (this is a very heavily used object)

does anyone here have a more elegant solution?

thanks!

Upvotes: 1

Views: 1074

Answers (1)

edA-qa mort-ora-y
edA-qa mort-ora-y

Reputation: 31861

A list or a deque are both suitable for ring buffers. If your objects are trivially copyable and small you can just used the deque and probably not worry about the memory instances. If you have larger data you can use the list and a custom object pool (so that old unusued objects will be reused for future additions).

If you don't like the std collection object pool semantics (crappy prior to C++11, I'm not sure now), then you can simply store pointers in the deque and manage your own memory.

Upvotes: 1

Related Questions