views:

232

answers:

2

I'm looking for a collection object/strategy that can allow for FIFO and lets me view the items in the collection by simply specifying their position. To clarify:

  1. I would like this data structure to hold say 100 DTO objects, and then when it gets to 101, I can make room by deleting the first item, etc. (FIFO).

  2. I'd like to be able to return the newest x # of these objects when requested.

I tried to use the .Net queue object, however as far as I can tell it does not support #2, although I might be overlooking something there.

+2  A: 

It would not be hard to wrap a List and do a RemoveAt(0) when you want to pop an item out of the queue. That would give you FIFO and let you index in anywhere you want. You should probably wrap it to protect the integrity of the queue (FIFO only).

JP Alioto
I wouldn't wrap a List, because indexing into it is a O(N) operation. It sounds like the OP wanted to index into the queue frequently, so that's a large concern.An array will give you O(1) arbitrary access time.
arke
List-classes in .NET, like List<String> uses an array internally, but removing item #0 is an O(N) operation for those, so the overhead is there just the same, just not in the same place.
Lasse V. Karlsen
Ah, right, I was thinking of LinkedList. In that case, using read and write indices in a wrapper collection class makes it O(1) for all operations that OP cares about.
arke
The question specified 100 objects, not 1,000,000 -- linear time removal is not an issue here.
JP Alioto
Sure, but it also didn't specify how often items are enqueued or dequeued. Maybe it's my game programmer mentality, but I'd rather spend the 5-10 minutes extra to get very fast O(1) all around. Read and write indices are a negligible performance hit, while doing it the "primitive" way incurs a memory allocation and copy on every dequeue. If that's done, say, 20 times a frame, ...
arke
I agree, it's not that much development effort to get the better perf.
JP Alioto
I've implemented it with List. I'm not too worried at just 100 items, but say if i scale it to 10,000 items, to clarify, is removing item #0 an O(1) or O(N) for List? If O(N), which object would be best to use?
alchemical
+1  A: 

I took a quick look at the .NET docs and I couldn't find anything that fulfils it as you need it. Looks like you need to implement it on your own, though that's not too difficult. I recommend going with an array of the appropriate size, and keep around read and write indices for the current position in the array, and use it as a circular array.

Enqueue would be writing the value to readIndex, then setting readIndex to ((readIndex + 1) % queueSize). Dequeue would throw an exception if readIndex == writeIndex, otherwise get the value at writeIndex, then increment writeIndex by ((writeIndex + 1) % queueSize). Peeking into a queue index (from the top of the queue, that is the item that was queued last) would be returning the item at ((queueSize + (readIndex - index)) % queueSize).

arke