I'm implementing a simple pooling system in my box2d based game to spawn/despawn/pre-pool all objects. The objects in question are all circles that are created at a set radius. e.g. When I pre-pool I create 10 x 2m, 5 x 5m, 5 x 10m circles.
Right now i'm using a simple linked list to store pooled objects, so when I need to get an object of radius x, it might take till the end of the list to either find the body with the correct radius, or not find it at all. Which is obviously cumbersome and would only become worse as I pool more objects.
I was thinking of using a hash table, so I could use the radius as a key to hash and quickly access the proper object, but my worry is that the radius values vary greatly by value and number of objects using them. e.g. i might have 50 hash at 2m and 5 hash at 100m and this would cause a create deal of wasted space right?
I'm inexperienced in using data structures and I would like to learn more, so what type of data structure would be best to efficiently handle a system like this and why?
Thank you!
A hash table is a perfect solution for this type of problem. What you want is the diameter to be the key and a vector of circles as the value. When a new item with a new key is added to the table, a new vector should be constructed to hold the value as well as any additional values. If a new item is to be added to the table, but it does not have a unique key, get the existing vector for that key and add the new item to the vector.
This does not waste space. Every radius value links to exactly one vector containing items with that radius. In regards to "e.g. i might have 50 hash at 2m and 5 hash at 100m and this would cause a create deal of wasted space right?" I do not understand why this would be a problem. If you are interested in this topic, look into hashing with collisions resolved by chaining.