-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbs.pl
52 lines (45 loc) · 1.62 KB
/
bs.pl
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
#!/usr/env perl
# Derived from code by Nathan Torkington
# also - Jon Orwant, Jarkko Hietaniemi & John Macdonald
use v5.10;
use strict;
use integer;
my ($word, $file) = @ARGV;
open (FILE, $file) or die "Can't open $file: $!";
my $position = binary_search(\*FILE, $word);
if (defined $position) { say "$word occurs at position $position" }
else { say "$word does not occur in $file" }
sub binary_search {
my ( $file, $word ) = @_;
my ( $high, $low, $mid, $mid2, $line );
$low = 0;
$high = (stat($file))[7]; # Might not be the start of line.
$word =~ s/\W//g;
$word =~ lc($word);
while ($high != $low) {
$mid = ($high+$low)/2;
seek($file, $mid, 0) || die "Couldn't seek: $!\n";
$line = <$file>;
$mid2 = tell($file);
if ($mid2 < $high) {
$mid = $mid2;
$line = <$file>;
} else {
seek($file, $low, 0) || die "Couldn't seek: $!\n";
while (defined($line = <$file>)) {
last if compare($line, $word) >= 0;
$low = tell($file);
}
last;
}
if (compare($line,$word) < 0) { $low = $mid }
else { $high = $mid }
}
return if compare($line, $word);
return $low;
}
sub compare {
my ($word1,$word2) = @_;
$word1 =~ s/\W//g; $word = lc($word1);
return $word1 cmp $word2;
}