Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
template size_t handle_map::erase(Id_T handle)
{
if (!isValid(handle)) { return 0; }
m_fragmented = 1;
Id_T innerId = m_sparseIds[handle.index];
uint32_t innerIndex = innerId.index;
// push this slot to the back of the freelist
innerId.free = 1;
++innerId.generation;
// increment generation so remaining outer ids go stale
innerId.index = 0xFFFFFFFF;
// max numeric value represents the end of the freelist
m_sparseIds[handle.index] = innerId;
// write outer id changes back to the array
if (freeListEmpty())
{
// if the freelist was empty, it now starts (and ends) at this index
m_freeListFront = handle.index;
m_freeListBack = m_freeListFront;
}
else
{
m_sparseIds[m_freeListBack].index = handle.index;
// previous back of the freelist points to new back
m_freeListBack = handle.index;
// new freelist back is stored
}
// remove the component by swapping with the last element, then pop_back
if (innerIndex != m_items.size() - 1)
{
std::swap(m_items.at(innerIndex), m_items.back());
std::swap(m_meta.at(innerIndex), m_meta.back());
// fix the ComponentId index of the swapped component
m_sparseIds[m_meta.at(innerIndex).denseToSparse].index = innerIndex;
}
m_items.pop_back();
m_meta.pop_back();
return 1;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.