If I decide to go with a list (which I most definitely will) I will definitely use this (with credit, of course ) or at least use this as a reference in learning how to make one my self.arras wrote:Here is sorted list class I use for A* pathfinding:Code: Select all
...
And I will definitely look into this as well...vitek wrote:You could just use the Standard C++ Library algorithm sort...
I've thought about this, and I think it would be necessary to update the list every time an object is moved, not just once an iteration. That's why I've decided to use a list; so that I can remove and reinsert a node without having to recopy the whole rest of the array. I want my program to be optimized for large numbers of elements in the array (>1000, at the least).rogerborg wrote:Indeed, but depending on how you actually implement your object logic, you may want to go with your own bubble sort.
If you first move all objects, and then (in a separate pass) do collision / AI, then a single sort (after all the moves) will likely be more efficient.
However, if you want to do movement + AI/collision once per object, then you'll have to keep your list sorted all the time. As you noted, it's just a case of swapping objects up or down until they're back in order again. You could use a list, but an array would be just fine as well.
If I understand correctly (which it is probable that I don't), moving an item in a list is a couple of pointer changes, whereas removing an element from an array and reinserting it somewhere else would require that the whole rest of the array be copied and moved to fit the new element in. Bear in mind, however, that I have no idea if that actually effects the situation; I'm just making an assumption.Saturn wrote:So I still fail to see how a sorted list is superior to a sorted array in this context.