How can I safely iterate a lua table while keys are being removed
In my main coroutine, I am removing or adding entries from a table depending on user operations. In the background, I'd like to iter开发者_如何学Cate over the entries in the table. I don't mind particularly if I miss an insertion on one iteration, providing I can catch it before the next.
Is it safe to iterate over it with pairs
? Or should I use next
instead?
You can safely remove entries while traversing a table but you cannot create new entries, that is, new keys. You can modify the values of existing entries, though. (Removing an entry being a special case of that rule.)
You can't get there from here. At least not directly....
As lhf said you can modify or remove entries while traversing a table but you cannot add them. The results are ... undefined. (Read: branch into hyperspace or equivalent for all practical purposes.)
If you insist on being able to add entries you're going to have to clone your table and use one copy for iteration and the other for keeping track of your insertions and deletions. If this itself doesn't match your requirements, you'll have to instead do something like this:
- Make an empty table for table additions.
- Start iterating over your main table.
- As you find entries you want to modify, modify them in-place. (This is permitted.)
- As you find entries you want to delete, delete them in-place. (This is permitted.)
- As you find entries you want to add, add them into the other table that started empty.
- When you finish your iteration, merge the addition table into the main table.
- Lather. Rinse. Repeat.
There are other similar patterns with slightly different rules you can employ. For example between steps 5 & 6 you might want to insert a recursive call to your table walking code for the added table entries before merging, etc. You may also have to keep track of possible deletions in both the main table and in your additions table if that is a possible interaction.
精彩评论