Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 iterate 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?

like image 860
Eric Avatar asked May 29 '11 11:05

Eric


2 Answers

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.)

like image 174
lhf Avatar answered Sep 20 '22 00:09

lhf


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:

  1. Make an empty table for table additions.
  2. Start iterating over your main table.
  3. As you find entries you want to modify, modify them in-place. (This is permitted.)
  4. As you find entries you want to delete, delete them in-place. (This is permitted.)
  5. As you find entries you want to add, add them into the other table that started empty.
  6. When you finish your iteration, merge the addition table into the main table.
  7. 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.

like image 40
JUST MY correct OPINION Avatar answered Sep 21 '22 00:09

JUST MY correct OPINION