tags:

views:

62

answers:

3
+1  Q: 

Filter with FIFO

I'd like to filter incoming pakets from a stream within a certain amount of time.

e.g.

filter all "A" within 5 seconds

10:00:00 "A" <- show
10:00:01 "A" <- don't show
10:00:02 "B" <- show
10:00:03 "A" <- don't show
10:00:06 "A" <- show

I thought about implementing this with a sort of FIFO.

what do you think might be the best solution?

I'm using c#, .net 3.5

A: 

I'm not sure that I've understood your problem correctly, but couldn't you just store the time for the first A that you encounter and for each incoming A after that you compare the time with that time? If less than 5 seconds has passed, don't show it, if more then reset the time and show A?

villintehaspam
+3  A: 

I don't see the benefit of FIFO. Take a dictionary where the string ("A" etc.) are the keys and store the ignore time (e.g. DateTime.Now.AddSeconds(5), or using ticks if you're afraid of problems due to changes in the computer time). Whenever you get a new item, check if you have it in the dictionary. If yes, compare the expiration time; ignore the item if it has not yet been reached. In all other cases, keep the item and just store the new expiration in the dictionary.

Lucero
D'oh, you beat me to it. :-)
Scott Smith
Sorry about that... I'll wait a few seconds longer the next time! ;)
Lucero
good idea but let's imagine my app is running for 2 weeks and every 2 seconds I get a new item. at what point can I cleanup the dict?
Kai
+1  A: 

So, you want to see the first "A" or "B" packet, but no repeats until a certain amount of time has elapsed?

You could create a hashtable that maps the type (e.g. "A") to a DateTime or timer-tick value.

For every packet you receive, look for its type in the hashtable.

If not found, add it along with a time (say) five seconds from now. Then output the packet.

If you do find it in the hashtable, check to see if the associated time has past already.

If so, calculate a new time (five seconds from now), replacing the old time, and output the packet.

If not, ignore (filter out) the packet.

Scott Smith
wow, alright. can you maybe show me that in a few lines of "pseudo" code?
Kai