-
Notifications
You must be signed in to change notification settings - Fork 2
/
Overlap_binary_search.pm
executable file
·69 lines (42 loc) · 1.49 KB
/
Overlap_binary_search.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#!/usr/local/bin/perl
package main;
our $SEE;
package Overlap_binary_search;
use strict;
# Provide a list of elements in the form:
# @ordered_element_list = ( [lend, rend], [lend, rend], ...)
# where lend <= rend and elements are ordered by their midpoint.
sub new {
my ($packagename, @ordered_element_list) = @_;
my $self = { ordered_list => [@ordered_element_list] };
bless ($self, $packagename);
return ($self);
}
## Performs a binary search on the ordered element list to identify an element overlapping the
## given set of lend,rend coordinates.
# returns the index of the overlapping element within the list, or -1 if none found.
sub find_overlapping_element {
my ($self, $lend, $rend) = @_;
my $midpt = ($lend+$rend)/2;
my $ordered_element_list_aref = $self->{ordered_list};
my $last_index = $#$ordered_element_list_aref;
my $leftBound = 0;
my $rightBound = $last_index;
while ($leftBound <= $rightBound) {
my $currIndex = int( ($leftBound + $rightBound) /2 );
my $currElement = $ordered_element_list_aref->[$currIndex];
my ($currLend, $currRend) = @$currElement;
my $curr_mid = ($currLend + $currRend) /2;
print "comparing ($lend,$rend) to ($currLend,$currRend)\n" if $SEE;
if ($lend <= $currRend && $rend >= $currLend) { #overlap
return ($currIndex);
}
if ($midpt < $curr_mid ) {
$rightBound = $currIndex - 1;
} else {
$leftBound = $currIndex + 1;
}
}
return (-1); #not found
}
1; #EOM