-
Ive recently looked into the implementation off entt and dircovererd that the custom iterators for the sparse_set and the storage iterate from the back to the fron. What is the reasoning behind that? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
That's a trick to make it possible to add and remove elements during iterations. |
Beta Was this translation helpful? Give feedback.
That's a trick to make it possible to add and remove elements during iterations.
For perf reasons, EnTT does a swap-and-pop when you delete a component. Imagine now to iterate from 0 to N. If you store aside the size, you can still add new elements during iterations. However, if you remove an element, it gets super tricky and error prone.
What happens when you iterate backwards? Adding isn't a problem since the new elements go to the end while you move towards the head. Similarly, if you remove the current element, you swap-and-pop with the last one which is an already visited object. Also in this case, no errors nor risks.