2

Looking for any existing FIFO Queue which also handles overrides based on topic as follows given

 struct QueueItem { string Topic; .... other data }

if the queue has items added in the order

q.Add( new QueueItem() { topic = A, ... } ); //1
q.Add( new QueueItem() { topic = B, ... } ); //2
q.Add( new QueueItem() { topic = C, ... } ); //3
q.Add( new QueueItem() { topic = A, ... } ); //4
q.Add( new QueueItem() { topic = B, ... } ); //5

At retrieval the order should be #4 Topic A - skipping #1 #5 Topic B - skipping #2 #3 Topic C

etc.

NOTE - the order in which the items/Topics are added/processed is important given the rule above

Wondering if this is a known scenario and there might be an existing implementation out there already

Kumar
  • 9,481
  • 10
  • 74
  • 126
  • 2
    It's a little heavier but I've used a dictionary of queues for this purpose before (improving the API for this purpose of course by implementing a custom dictionary). It might be worth it for your case though. – M.Babcock Mar 09 '12 at 02:55
  • I am not sure I understand the logic behind your retrievals? Can you elaborate how you expect the results to work out the way you explain it? – Justin Pihony Mar 09 '12 at 02:55
  • @JustinPihony lots of uses in the fin world and others ! – Kumar Mar 09 '12 at 03:02
  • @M.Babcock the order of items is important so dictionary of queues would not work in this scenario, updating the Q – Kumar Mar 09 '12 at 03:04
  • @Kumar - The order that a specific topic is significant or the order overall? – M.Babcock Mar 09 '12 at 03:05
  • Ahhh, sorry, it is late and I did not even follow why you were picking each element. So, am I correct in that this is a "skip to the next matching if there is one" queue? – Justin Pihony Mar 09 '12 at 03:12
  • @M.Babcock the order in which items are added is significant – Kumar Mar 09 '12 at 12:08
  • @JustinPihony the items are processed in the order they're added with the caveat if another item was added subsequently with the same topic then skip the current item and use the latest instance ! – Kumar Mar 09 '12 at 12:09
  • @Kumar - So if you only care about the most recent entry of a specific topic then is it really a queue at all? In your example, you say that for "Topic A" #4 should be considered skipping #1, will #1 ever be considered or would it effectively be removed? It almost sounds like you really just need a `ConcurrentDictionary` where the key is the topic and the value is your `QueueItem` struct. – M.Babcock Mar 09 '12 at 15:39
  • @M.Babcock The order in which items are added is important so it has queue behavior there! sorry the scenario doesn't fit the known definitions/types – Kumar Mar 10 '12 at 04:33
  • In that case you need to separate the queue from the dictionary (conceptually at least). Override `Queue` which _has a_ `Dictionary` where `T` is your topic and `U` is your `QueueItem`. When the user dequeues from your implementation dequeue from the `Queue` and return the result in the `Dictionary`. When the user enqueue's to your implementation check to see if it exists (delete it if it does) and then queue up the value to be queued. – M.Babcock Mar 10 '12 at 04:47

2 Answers2

1

What you're looking for is a priority queue. Your requirement is slightly different in that it uses letters instead of integers for the priority, but the concept is the same. See answers C# Priority Queue and Priority queue in .Net

Community
  • 1
  • 1
robbymurphy
  • 690
  • 1
  • 5
  • 16
  • looks interesting, will take a deeper look into use of IComparable vs. say a dictionary ! – Kumar Mar 09 '12 at 12:17
0

For a nice thread safe queue implementation check the ConcurrentQueue<T> built in to the framework from v4 onwards.

Having said that, the behaviour you ask for is not strictly a queue, it is more of a list where Add() is overridden to either add or replace an equivalent existing item. So the question remains, do you want a FIFO queue, or an add/replace list?

slugster
  • 47,434
  • 13
  • 92
  • 138
  • It's both or neither or a combination there of ! the order is important so it's a Q, if a new item with the same topic arrives the old one can be discarded, lots of uses in the financial world for sure – Kumar Mar 09 '12 at 03:01