[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: avoiding recursion in a dump_table function
- From: Fabien <fleutot+lua@...>
- Date: Tue, 10 May 2011 23:57:32 +0200
This dumper handles recursion as well as shared sub-structures, on keys and on values; it produces valid Lua code, and the code is as simple as one could expect when there's no shared sub-structure:
As for the "seen-it table" vs. visiting stack approach questioned by David Favro, the reason to go with the "seen-it" approach are:
- it's very simple in a language where tables support arbitrary keys;
- it's good enough for most actual use cases;
- it allows to detect not only cycles, but also shared sub-trees. You might or might not be interested into handling shared sub-trees in a special way, but you certainly want to be protected against them: it's easy to create structures which contain no cycle, but take an exponential time to walk through, and will exhaust your RAM without being detected by a node stack.