]> Sergey Matveev's repositories - public-inbox.git/blobdiff - lib/PublicInbox/SearchThread.pm
thread: avoid incrementing undefined value
[public-inbox.git] / lib / PublicInbox / SearchThread.pm
index 7e89946df81bd695ab7b14251cf2eae1ae346653..ba31f4322ae851c23a65fdfe6e96c0a42f28b601 100644 (file)
@@ -31,16 +31,10 @@ sub new {
 
 sub thread {
        my $self = shift;
-       $self->_setup();
+       _add_message($self, $_) foreach @{$self->{messages}};
        $self->{rootset} = [
                        grep { !$_->{parent} } values %{$self->{id_table}} ];
-       $self->_finish();
-}
-
-sub _finish {
-       my $self = shift;
        delete $self->{id_table};
-       delete $self->{seen};
 }
 
 sub _get_cont_for_id ($$) {
@@ -48,12 +42,6 @@ sub _get_cont_for_id ($$) {
        $self->{id_table}{$mid} ||= PublicInbox::SearchThread::Msg->new($mid);
 }
 
-sub _setup {
-       my ($self) = @_;
-
-       _add_message($self, $_) foreach @{$self->{messages}};
-}
-
 sub _add_message ($$) {
        my ($self, $smsg) = @_;
 
@@ -93,8 +81,7 @@ sub _add_message ($$) {
 }
 
 sub order {
-       my $self = shift;
-       my $ordersub = shift;
+       my ($self, $ordersub) = @_;
 
        # make a fake root
        my $root = _get_cont_for_id($self, 'fakeroot');
@@ -186,29 +173,13 @@ sub set_children {
        } while ($walk);
 }
 
-sub order_children {
-       my $self = shift;
-       my $ordersub = shift;
-
-       return unless $ordersub;
-
-       my $sub = sub {
-               my $cont = shift;
-               my $children = $cont->children;
-               return if @$children < 2;
-               $cont->set_children( $ordersub->( $children ) );
-       };
-       $self->iterate_down( undef, $sub );
-       undef $sub;
-}
-
 # 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}++;
+               $seen{$cont} = 1;
                $callback->($cont);
 
                if (my $next = $cont->{next}) {
@@ -228,20 +199,17 @@ sub recurse_down {
        }
 }
 
-sub iterate_down {
-       my $self = shift;
-       my ($before, $after) = @_;
+sub order_children {
+       my ($walk, $ordersub) = @_;
 
        my %seen;
-       my $walk = $self;
        my $depth = 0;
        my @visited;
        while ($walk) {
-               push @visited, [ $walk, $depth ];
-               $before->($walk, $depth) if $before;
+               push @visited, $walk;
 
                # spot/break loops
-               $seen{$walk}++;
+               $seen{$walk} = 1;
 
                my $child = $walk->{child};
                if ($child && $seen{$child}) {
@@ -270,8 +238,15 @@ sub iterate_down {
                }
                $walk = $next;
        }
-       return unless $after;
-       while (@visited) { $after->(@{ pop @visited }) }
+       foreach my $cont (@visited) {
+               my $children = $cont->children;
+               next if @$children < 2;
+               $children = $ordersub->($children);
+               $cont = $cont->{child} = shift @$children;
+               do {
+                       $cont = $cont->{next} = shift @$children;
+               } while ($cont);
+       }
 }
 
 1;