Created
September 26, 2018 21:32
-
-
Save seivan/dcc8942d5b0d45c6287a5864cfabefc4 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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