1

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

Fred Foo
  • 355,277
  • 75
  • 744
  • 836
Jonathan S.
  • 1,540
  • 1
  • 15
  • 26

1 Answers1

1

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.

Community
  • 1
  • 1
Fred Foo
  • 355,277
  • 75
  • 744
  • 836