]> Sergey Matveev's repositories - public-inbox.git/blobdiff - lib/PublicInbox/SearchThread.pm
wwwstream: improve documentation and variable naming
[public-inbox.git] / lib / PublicInbox / SearchThread.pm
index c6bd999c050038c52bf16589566a09c7499572ef..fe70406b0cbad163f2b52f23d9bda019367e55d4 100644 (file)
@@ -98,33 +98,37 @@ sub new {
        }, $_[0];
 }
 
+sub topmost {
+       my ($self) = @_;
+       my @q = ($self);
+       while (my $cont = shift @q) {
+               return $cont if $cont->{smsg};
+               push @q, values %{$cont->{children}};
+       }
+       undef;
+}
+
 sub add_child {
        my ($self, $child) = @_;
        croak "Cowardly refusing to become my own parent: $self"
          if $self == $child;
 
        my $cid = $child->{id};
-       $self->{children}->{$cid} = $child;
 
        # reparenting:
        if (defined(my $parent = $child->{parent})) {
                delete $parent->{children}->{$cid};
        }
 
+       $self->{children}->{$cid} = $child;
        $child->{parent} = $self;
 }
 
 sub has_descendent {
-       my ($cur, $child) = @_;
-       my %seen;
-       my @q = ($cur->{parent} || $cur);
-
-       while (defined($cur = shift @q)) {
-               return 1 if $cur == $child;
-
-               if (!$seen{$cur}++) {
-                       push @q, values %{$cur->{children}};
-               }
+       my ($self, $child) = @_;
+       while ($child) {
+               return 1 if $self == $child;
+               $child = $child->{parent};
        }
        0;
 }