You can save the βfreeβ list of unused identifiers as a separate list inside your main World object.
When an object is destroyed by World (its identifier is not used), you can click this identifier on the head of the free list.
When you create a new object, you can do the following:
If the free list is non-empty: pop the head item and take that ID. Else increment a global ID counter and assign it current value.
While you can still use identifiers (if you simultaneously had more objects than the maximum value of your counter), this strategy will allow you to process identifiers and do everything with O(1) execution complexity.
EDIT: according to @Matthieu's comments below, the std::deque container can also be used to support a βfreeβ list. This container also supports push_front, pop_front with O(1) complexity.
Hope this helps.
source share