Buffering Timestamped States and Wraparound

Started by
2 comments, last by sufficientreason 12 years, 2 months ago
Hi everyone,

I'm trying to create a buffer of game states for interpolation, each with a unique incrementing timestep marker. I want the buffer to always be able to retrieve the state with the lowest timestamp. The states are not guaranteed to be inserted in order in the buffer (they're being transmitted via UDP), so this raises an issue with wraparound.

I'm using C# uints, but let's say for a moment that I'm just using bytes with a range of 0-255. Beginning at timestep 253, the order of output states would be tagged 253, 254, 255, 0, 1, 2, 3. When we mix up the order, the buffer could receive the list [0, 254, 3, 2, 253, 255, 1]. Using normal comparison, a sorted list would arrange these as [0, 1, 2, 3, 253, 254, 255], but I need [253, 254, 255, 0, 1, 2, 3]. We can assume that the buffer will never have more than, say, 100 entries in it (or, more accurately, more than half the maximum value for the datatype of the timestamp).

Ideally I'd like to just write a special IComparer<uint> in C# and use a SortedList<uint, State>. I need fast insertion, and the buffer always has to be able to quickly retrieve the lowest-timestep state. I know this has to be a solved problem, but I can't find it anywhere. Any help? Thanks!
Advertisement
Loop through and see if both 0 and 255 exists, and if so, add 255 if value is below 127 while comparing? If you can store last frames highest value, you might be able to skip the loop step though.
Why go through the headache and processing to save a few bytes? Just use DateTime.Ticks or some other non-wraparound sized timestamp...

Though timestamps aren't exactly super reliable across machines regardless... I'm not sure what you're using them for.
Yeah, you're right. That's just a simpler way to do it. I thought I would tag states by the "tick" they were generated on, but that didn't even turn out to be accurate enough. Nevermind. Thank you though,

This topic is closed to new replies.

Advertisement