in reply to Can I speed this up? (repetitively scanning ranges in a large array)
Note that it does not handle the case where there are points not covered by any range.#!/usr/bin/perl use strict; use warnings; use 5.010; my $len = 87688; open my $rfh, '<', 'ranges.txt' or die $!; my (@start, @end); while (<$rfh>) { chomp; my ($start, $end) = split /\s+/; $start--; $end--; if ($start > $end) { my $start2 = $start - $len; push @start, ($start2 < -$end ? -$end : $start2); push @end, $end; $end += $len; } push @start, $start; push @end, $end; } # sort the ranges my @ix = sort { $start[$a] <=> $start[$b] or $end[$b] <=> $end[$a] } 0..$#start; # filter out shadowed ranges my @good; my $last_end = -1; for my $ix (@ix) { if ($end[$ix] > $last_end) { push @good, $ix; $last_end = $end[$ix]; } } # create slopes my @top; my $last_ix = shift @good; for my $ix (@good) { my $x0 = int (($start[$last_ix] + $end[$last_ix] + 1) / 2); my $y0 = $end[$last_ix] - $x0; my $x1 = int (($start[$ix] + $end[$ix]) / 2); my $y01 = $x0 - $start[$ix]; $last_ix = $ix; for my $x ($x0..$x1) { $top[$x] = ($y0 > $y01 ? $y0 : $y01); $y0--; $y01++; } } $#top = $len - 1; say $_ + 1 for @top;
|
|---|