Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
You can look up all the test points in parallel.

Some code showing how to do it for a simplified version of your problem where the ranges cover all the domain (there are no gaps):

#!/usr/bin/perl use strict; use warnings; my @ranges = 0; push @ranges, $ranges[-1] + 1 + int rand 200 for 1..10000; my @tests = map int rand $ranges[-1], 0..1000000; match (\@ranges, \@tests); sub div { my ($border, $tests) = @_; my ($lt, $ge) = ([], []); push @{$_ < $border ? $lt : $ge}, $_ for @$tests; ($lt, $ge); } sub match { my ($ranges, $tests) = @_; if (@$ranges == 1) { if (@$tests) { print "tests in range $ranges->[0]:\n", join(", ", @$tests +), "\n"; } else { print "range $ranges->[0] is empty\n"; } } else { my $pivot = int((@$ranges + 1)/ 2); my ($lt, $ge) = div($ranges->[$pivot], $tests); match([@{$ranges}[0..$pivot-1]], $lt); match([@{$ranges}[$pivot..$#$ranges]], $ge); } }
And a version optimized to not copy data around but to use indexes inside the main arrays:
#!/usr/bin/perl use strict; use warnings; my @ranges = 0; push @ranges, $ranges[-1] + 1 + int rand 200 for 1..1_000_000; my @tests = map int rand $ranges[-1], 0..10_000_000; match(0, scalar @ranges, 0, scalar @tests); sub div { my ($border, $start, $end) = @_; while ($end > $start) { if ($tests[$start] < $border) { $start++; } else { if (--$end > $start) { @tests[$start, $end] = @tests[$end, $start]; } } } $end; } sub match { my ($r_start, $r_end, $t_start, $t_end) = @_; if ($r_end - $r_start <= 1) { if ($t_end - $t_start > 0) { print "tests in range $ranges[$r_start]:\n", join(", ", @t +ests[$t_start..$t_end-1]), "\n"; } else { print "range $ranges[$r_start] is empty\n"; } } else { my $r_pivot = int(($r_start + $r_end)/ 2); my $t_pivot = div($ranges[$r_pivot], $t_start, $t_end); match($r_start, $r_pivot - 1, $t_start, $t_pivot - 1); match($r_pivot, $r_end, $t_pivot, $t_end); } }
This algorithm, for N ranges and M test points where M > N has complexity O(MlogN) and does not require extra memory besides that used to store the ranges and the test points.

On my not so new computer, with the print sentences commented out, it runs in a couple of minutes.

In reply to Re: Range overlap (with a lot of ranges) by salva
in thread Range overlap (with a lot of ranges) by BioLion

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others about the Monastery: (8)
    As of 2019-11-18 16:52 GMT
    Find Nodes?
      Voting Booth?
      Strict and warnings: which comes first?

      Results (90 votes). Check out past polls.