- Home /
C# - Is it possible to sync a Queue to a List, or is there a better option?
I have a bit of a interesting problem in that I'm using a Queue to manage a endless runner pool. However, I just realized that the items in the queue can't be accessed and selected for quick modification.
Temporarily, I've also added a List, but without being constantly updated, that list does not stay synced to the positions of the object - for instance, 5 in the List could be at the very back of the track, while at the start it would reference the 5th track piece on screen.
My major goal is to do things like mark the last track piece that a player actively touched before they died, or modify a track in a event system. Having a list also helps keep track of spawning positions.
What option would be best for me to use here?
It's not clear to me exactly what you're trying to do with the Queue. What is the content of the objects that are in the Queue? I'd guess that you only want/need a single collection type (List, Queue, ??) ins$$anonymous$$d of trying to sync multiple collections, though it's hard to tell without more info.
Can you provide more details?
I'm attempting to view the data in the queue as it exists in an array or list so that I can access the individual pieces as they currently are in queue order. However, I did discover that Queue has "ToArray()". I am sort of wondering if iterating that each time the list is modified would be too expensive or not.
$$anonymous$$y problem is that right now, outside the queue each piece exists independently without a reference to it in the code.
Answer by FWCorey · Apr 23, 2016 at 02:56 AM
Your best bet if you are truly concerned about performance is to try several methods, profiling each until you find the one that works best for your particular use case. If your queue has a known maximum size you might want to use an array with pointers and create a circular queue that way. That would be the fastest, but it's expensive to expand the maximum size and if you need access to more than basic functionality can be a little advanced to implement as a generic IEnumerable class.
Thankfully the queue is limited to a hundred items. It should never expand beyond that, the elements inside just get recycled and re-positioned.
Then a circular Array queue will work best for you. Just create a class with an array of your items and a few int pointers
int _topOfQueue index pointer for the top of the queue int _bottomOfQueue index pointer for the bottom of the queue int Count {get; private set;} property for the count of items
an Enqueue method that increments _topOfQueue with wraparound sets a new item to the array element at index _topOfQueue and increments Count
a Dequeue method to check that Count is greater than 0 and throw an error or return the item at array index _bottomOfQueue and then increment _bottomOfQueue and decrease Count
a Peek method to check that Count is greater than 0 and throw an error or return the item at array index _bottomOfQueue. Dequeue can call this before updating the pointers but despite the code duplication, it would shorten the call stack when debugging to keep them separate.
a Clear method to simply set _topOfQueue, _bottomOfQueue and Count to zero
If there is ANY chance the queue size would change you can add a check in Enqueue to make sure _topOfQueue never increments to the same value as _bottomOfQueue with a Count greater than 0 and resize and reorder the array if it does. $$anonymous$$aybe with a Debug.Warning message so you can change the initial size to prevent it happening again
Your answer
Follow this Question
Related Questions
A node in a childnode? 1 Answer
Queueing methods and calling them at specific times 1 Answer
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
object assigns but does not show when added to list, or any other var 0 Answers