开发者

How can I safely iterate a lua table while keys are being removed

开发者 https://www.devze.com 2023-03-08 21:20 出处:网络
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 part

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:

  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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号