+# non-recursive version of recurse_down to avoid stack depth warnings
+sub recurse_down {
+ my ($self, $callback) = @_;
+ my %seen;
+ my @q = ($self);
+ while (my $cont = shift @q) {
+ $seen{$cont}++;
+ $callback->($cont);
+
+ if (my $next = $cont->next) {
+ if ($seen{$next}) {
+ $cont->next(undef);
+ } else {
+ push @q, $next;
+ }
+ }
+ if (my $child = $cont->child) {
+ if ($seen{$child}) {
+ $cont->child(undef);
+ } else {
+ push @q, $child;
+ }
+ }
+ }
+}
+