Jonathan S.
Jonathan S.

Reputation: 1550

most efficient data storage object for queuing a max number of elements

I need a storage type that I can set a max number of elements for and whenever I add something to the tail, the head is truncated as necessary with low overhead. I can of course do this manually if I have to. Example

max = 1000

fill it with integers 1-1000 : [1,2,...,999,1000]

add numbers 1000 - 1500 : [500,501,....,1499,1500]

It has to be as cheap an operation as possible since I will be running multiple threads at this time, one doing audio recording. I don't care about keeping the head elements as they are popped off, I would like to get rid of them in a bulk operation.

I checked out the queue types in the SDK, not sure which could suit these needs, possibly a linked queue of some kind.

Thanks for any help

Upvotes: 1

Views: 214

Answers (1)

Fred Foo
Fred Foo

Reputation: 363587

Use a ring buffer, also known as a circular queue; these can be implemented as arrays, so they're particularly cheap. See this question for an implementation in Java.

Upvotes: 1

Related Questions