+ $s->{'LastUpdate'} = $place_counter++;
+ return $s if defined $s->{'Row'};
+
+ my $pri = $state_to_pri{$host_state{$s->{'Host'}}};
+ my ($best, $besti);
+ my $bestpri = -1;
+ for my $i (0..$nrows-1) {
+ my $r = $by_row[$i];
+ if (!defined $r) {
+ $besti = $i;
+ $best = undef;
+ last;
+ }
+ my $rpri = $state_to_pri{$host_state{$r->{'Host'}}};
+ next if $rpri > $pri;
+
+ if ($rpri < $bestpri ||
+ $rpri == $bestpri && $r->{'LastUpdate'} < $best->{'LastUpdate'}) {
+ # Trick: $best must be defined, as otherwise $bestpri == -1
+ $best = $r;
+ $besti = $i;
+ $bestpri = $rpri;
+ }
+ }
+
+ if ($best) {
+ delete $best->{'Row'};
+ }
+ $s->{'Row'} = $besti;
+ $by_row[$besti] = $s;
+ return $s;