Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 555743
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T11:50:47+00:00 2026-05-13T11:50:47+00:00

I have a function, binary_range_search , that is called like so: my $brs_iterator =

  • 0

I have a function, binary_range_search, that is called like so:

my $brs_iterator = binary_range_search(
    target => $range,                   # eg. [1, 200]
    search => $ranges                   # eg. [ {start => 1,   end => 1000},
);                                      #       {start => 500, end => 1500} ]

brs_iterator->() will iterate over all @$ranges on which $range overlaps.

I would like to extend binary_range_search to be able to call it with multiple ranges as its target, eg:

target => $target_ranges # eg. [ [1, 200], [50, 300], ... ]
search => $search_ranges # as above

So, when the search on $range->[0] is exhausted, it should move on to $range->[1], and so on. Here is the function in question, in its original form:

sub binary_range_search {
    my %options = @_;
    my $range    = $options{target}  || return;
    my $ranges   = $options{search}  || return;

    my ( $low, $high ) = ( 0, @{$ranges} - 1 );

    while ( $low <= $high ) {

        my $try = int( ( $low + $high ) / 2 );

        $low  = $try + 1, next if $ranges->[$try]{end}   < $range->[0];
        $high = $try - 1, next if $ranges->[$try]{start} > $range->[1];

        my ( $down, $up ) = ($try) x 2;

        my %seen = ();

        my $brs_iterator = sub {

            if (    $ranges->[ $up + 1 ]{end}       >= $range->[0]
                    and $ranges->[ $up + 1 ]{start} <= $range->[1]
                    and !exists $seen{ $up + 1 } )
            {
                $seen{ $up + 1 } = undef;
                return $ranges->[ ++$up ];
            }
            elsif ( $ranges->[ $down - 1 ]{end}       >= $range->[0]
                    and $ranges->[ $down + 1 ]{start} <= $range->[1]
                    and !exists $seen{ $down - 1 }
                    and $down > 0 )
            {
                $seen{ $down - 1 } = undef;
                return $ranges->[ --$down ];
            }
            elsif ( !exists $seen{$try} ) {
                $seen{$try} = undef;
              return $ranges->[$try];
            }
            else {
                return;
            }

        };
        return $brs_iterator;
    }
    return sub { };
}

It’s a standard binary search strategy, until it finds an overlapping range. It then moves on the right, exhausts it, moves on the left, exhausts it, and finally gives up. Ideally, it should then maybe shift the next target range, and redo the search, I suppose (perhaps via recursion?). My problem is, I am not sure how to make that work with the iterator construction.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T11:50:47+00:00Added an answer on May 13, 2026 at 11:50 am

    I just wrapped your iterator generation in a for loop, and built up an array of iterator functions.

    Depending on context, I either return a master iterator or a list of iterator functions. I wasn’t sure what you wanted.

    use strict;
    use warnings;
    
    
    my $t = [ [1,200], [400,900] ];
    my @r = (
        { start =>   1, end =>  100 },
        { start =>   2, end =>  500 },
        { start => 204, end =>  500 },
        { start => 208, end =>  500 },
        { start => 215, end => 1000 },
        { start => 150, end => 1000 },
        { start => 500, end => 1100 },
    );
    
    # Get a master iterator that will process each iterator in turn.
    my $brs_iterator = binary_range_search(
        targets => $t,  
        search => \@r,
    );
    
    # Get an array of iterators
    my @brs_iterator = binary_range_search(
        targets => $t,  
        search => \@r,
    );
    
    
    
    sub binary_range_search {
        my %options = @_;
        my $targets = $options{targets}  || return;
        my $ranges  = $options{search}  || return;
    
    
        my @iterators;
    
        TARGET:
        for my $target ( @$targets ) {
    
            my ( $low, $high ) = ( 0, $#{$ranges} );
    
            RANGE_CHECK:
            while ( $low <= $high ) {
    
                my $try = int( ( $low + $high ) / 2 );
    
                # Remove non-overlapping ranges
                $low  = $try + 1, next RANGE_CHECK 
                    if $ranges->[$try]{end}   < $target->[0];
    
                $high = $try - 1, next RANGE_CHECK 
                    if $ranges->[$try]{start} > $target->[1];
    
                my ( $down, $up ) = ($try) x 2;
    
                my %seen = ();
    
                my $brs_iterator = sub {
    
                    if (    exists $ranges->[$up + 1]
                            and $ranges->[ $up + 1 ]{end}   >= $target->[0]
                            and $ranges->[ $up + 1 ]{start} <= $target->[1]
                            and !exists $seen{ $up + 1 } )
                    {
                        $seen{ $up + 1 } = undef;
                        return $ranges->[ ++$up ];
                    }
                    elsif ( $ranges->[ $down - 1 ]{end}       >= $target->[0]
                            and $ranges->[ $down + 1 ]{start} <= $target->[1]
                            and !exists $seen{ $down - 1 }
                            and $down > 0 )
                    {
                        $seen{ $down - 1 } = undef;
                        return $ranges->[ --$down ];
                    }
                    elsif ( !exists $seen{$try} ) {
                        $seen{$try} = undef;
                      return $ranges->[$try];
                    }
                    else {
                        return;
                    }
    
                };
                push @iterators, $brs_iterator;
                next TARGET;
            }
    
        }
    
        # In scalar context return master iterator that iterates over the list of range iterators.
        # In list context returns a list of range iterators.
        return wantarray 
             ? @iterators 
             : sub { 
                 while( @iterators ) {
                     if( my $range = $iterators[0]() ) {
                         return $range;
                     }
                     shift @iterators;
                 }
                 return;
            }; 
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a binary search function that will search for a word in an
I have function Start() that is fired on ready. When I click on .ExampleClick
I have a function that copies binary file public static void copyFile(String Src, String
I have function like this: function ypg_delete_img($id, $img) { $q = $this->ypg_get_one($id); $imgs =
I have function getCartItems in cart.js and I want to call that function in
I have data that always looks something like this: alt text http://michaelfogleman.com/static/images/chart.png I need
I have a function that gives me a number as result: Now to make
I have a function that I am calling that runs all the way up
I have this function which I would like to parallelize using openmp: for(i=len-1;i>=0;i--){ if(bin[i]==49)
I have Clojure function that takes a sequence of numbers chops it into the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.