]> 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 05de9ec5b84d270fefc34b8b709a4e1d2c9ca292..fe70406b0cbad163f2b52f23d9bda019367e55d4 100644 (file)
@@ -33,7 +33,7 @@ sub thread {
        my $self = shift;
        _add_message($self, $_) foreach @{$self->{messages}};
        my $id_table = delete $self->{id_table};
-       $self->{rootset} = [ grep { !$_->{parent} } values %$id_table ];
+       $self->{rootset} = [ grep { !delete $_->{parent} } values %$id_table ];
 }
 
 sub _get_cont_for_id ($$) {
@@ -49,27 +49,27 @@ sub _add_message ($$) {
        $this->{smsg} = $smsg;
 
        # B. For each element in the message's References field:
+       defined(my $refs = $smsg->{references}) or return;
+
        my $prev;
-       if (defined(my $refs = $smsg->{references})) {
-               foreach my $ref ($refs =~ m/<([^>]+)>/g) {
-                       # Find a Container object for the given Message-ID
-                       my $cont = _get_cont_for_id($self, $ref);
-
-                       # Link the References field's Containers together in
-                       # the order implied by the References header
-                       #
-                       # * If they are already linked don't change the
-                       #   existing links
-                       # * Do not add a link if adding that link would
-                       #   introduce a loop...
-                       if ($prev &&
-                               !$cont->{parent} &&  # already linked
-                               !$cont->has_descendent($prev) # would loop
-                          ) {
-                               $prev->add_child($cont);
-                       }
-                       $prev = $cont;
+       foreach my $ref ($refs =~ m/<([^>]+)>/g) {
+               # Find a Container object for the given Message-ID
+               my $cont = _get_cont_for_id($self, $ref);
+
+               # Link the References field's Containers together in
+               # the order implied by the References header
+               #
+               # * If they are already linked don't change the
+               #   existing links
+               # * Do not add a link if adding that link would
+               #   introduce a loop...
+               if ($prev &&
+                       !$cont->{parent} &&  # already linked
+                       !$cont->has_descendent($prev) # would loop
+                  ) {
+                       $prev->add_child($cont);
                }
+               $prev = $cont;
        }
 
        # C. Set the parent of this message to be the last element in
@@ -90,7 +90,6 @@ package PublicInbox::SearchThread::Msg;
 use strict;
 use warnings;
 use Carp qw(croak);
-use Scalar::Util qw(weaken);
 
 sub new {
        bless {
@@ -99,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};
        }
 
-       weaken($child->{parent} = $self);
+       $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;
 }