Filter with FIFO
I'd like to filter incoming pakets from a stream within a certai开发者_JAVA百科n 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
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.
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 passed 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.
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?
精彩评论