Search code examples
c++listalgorithmvectorhandle

Fast algorithm to allocate and release integer-like handles in C++


I need a class that can allocate a unique integer (handle) and assign it to something object. And then release it in order to be used later. This algorithm should be fast. What data structure should I use? vector is fast to access data but long to erase or add(but it also important), list is long to access data. Maybe you know some good implementations? Thanks in advance!


Solution

  • I would simply use 64-bit integers. For allocation, I'd use a simple counter. To deallocate...well, I'd simply ignore the request to free a handle.

    You can ignore deallocation, because with 64-bit handles, you can quite literally allocate handles for decades at a time without even coming close to running out of fresh handles to allocate.