Reputation: 7605
I am looking for a data structure that works essentially like this:
The above is my best implementation guess. The properties I actually want are to be able to have a consumer which keeps a sort of bookmark value and can read the new items without missing out, and to be able to clear out items before a certain timestamp. However, the data structure need not know about the timestamp; being able to delete everything before a particular index is fine.
The way I would implement this structure is by keeping an int of where the item count starts and then keep either a list of items directly or a list of list segments. The index being some sort of primitive integer will wrap, but not within several multiples of the realistic timespan of the program's life.
The structure that I know of that gets closest is the particular ring buffer from the LMAX Disruptor. However, that structure also blocks if the consumer gets too far behind which will not suit my purposes. The consumer is batch-oriented and may not get frequent CPU time; the Disruptor works best with dedicated logical tasks continuously working against the ring buffer.
So the questions I'm asking are:
Upvotes: 1
Views: 657
Reputation: 697
Dont know of any preset collections that work exactly like that, but you could create a custom collection object, extending System.Collections.CollectionBase.
public class MyCollection : CollectionBase
{
public MyCollection() { }
This collection base object containse a List property that you can expose any way you like, for instance, add an Add() method
public int Add(Object o) // could by anything
{
return List.Add(o);
}
You could have a GetRange(int start, int length) that returns a new collection, and just as you fill the return collection, remove from the base collection
Upvotes: 1