Skip to content

sparse_set / storage iterators going from back to front #849

Answered by skypjack
KabelitzJ asked this question in Q&A
Discussion options

You must be logged in to vote

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.

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@KabelitzJ
Comment options

Answer selected by KabelitzJ
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
question open question
2 participants