From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on dcvr.yhbt.net X-Spam-Level: X-Spam-ASN: AS12876 163.172.0.0/16 X-Spam-Status: No, score=-2.2 required=3.0 tests=AWL,BAYES_00, RCVD_IN_MSPIKE_BL,RCVD_IN_MSPIKE_ZBI,RCVD_IN_XBL,SPF_FAIL,SPF_HELO_FAIL, TO_EQ_FM_DOM_SPF_FAIL shortcircuit=no autolearn=no autolearn_force=no version=3.4.0 Received: from 80x24.org (torrelay5.tomhek.net [163.172.38.173]) by dcvr.yhbt.net (Postfix) with ESMTP id 5D4921F855 for ; Fri, 5 Aug 2016 00:24:20 +0000 (UTC) From: Eric Wong To: spew@80x24.org Subject: [PATCH] thread: avoid recursion Date: Fri, 5 Aug 2016 00:24:18 +0000 Message-Id: <20160805002418.7274-1-e@80x24.org> List-Id: (bug reported to CPAN RT, awaiting bug #) --- lib/PublicInbox/Thread.pm | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/lib/PublicInbox/Thread.pm b/lib/PublicInbox/Thread.pm index 44a565a..95627de 100644 --- a/lib/PublicInbox/Thread.pm +++ b/lib/PublicInbox/Thread.pm @@ -42,6 +42,31 @@ sub topmost { $_[0]->SUPER::topmost || PublicInbox::Thread::CPANRTBug106498->new; } +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; + } + } + } +} + # ref: # - http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=795913 # - https://rt.cpan.org/Ticket/Display.html?id=106498 -- EW