| 1 | # | ||
| 2 | # $Id: Binary.pm,v 1.5 1998/12/15 20:55:08 rantapaa Exp $ | ||
| 3 | # | ||
| 4 | # Seach::Binary | ||
| 5 | |||
| 6 | package Search::Binary; | ||
| 7 | |||
| 8 | =head1 NAME | ||
| 9 | |||
| 10 | Search::Binary -- generic binary search | ||
| 11 | |||
| 12 | =head1 SYNOPSIS | ||
| 13 | |||
| 14 | use Seach::Binary; | ||
| 15 | $pos = binary_search($min, $max, $val, $read, $handle, [$size]); | ||
| 16 | |||
| 17 | =head1 DESCRIPTION | ||
| 18 | |||
| 19 | C<binary_search> implements a generic binary search algorithm returning the | ||
| 20 | I<position> of the first I<record> whose I<index value> is greater than or | ||
| 21 | equal to C<$val>. The search routine does not define any of the terms | ||
| 22 | I<position>, I<record> or I<index value>, but leaves their interpretation | ||
| 23 | and implementation to the user supplied function C<&$read()>. The only | ||
| 24 | restriction is that positions must be integer scalars. | ||
| 25 | |||
| 26 | During the search the read function will be called with three arguments: | ||
| 27 | the input parameters C<$handle> and C<$val>, and a position. If the position | ||
| 28 | is not C<undef>, the read function should read the first whole record starting | ||
| 29 | at or after the position; otherwise, the read function should read the record | ||
| 30 | immediately following the last record it read. The search algorithm will | ||
| 31 | guarantee that the first call to the read function will not be with a position | ||
| 32 | of C<undef>. The read function needs to return a two element array consisting | ||
| 33 | of the result of comparing C<$val> with the index value of the read record and | ||
| 34 | the position of the read record. The comparison value must be positive if | ||
| 35 | C<$val> is strictly greater than the index value of the read record, C<0> | ||
| 36 | if equal, and negative if strictly less. Furthermore, the returned position | ||
| 37 | value must be greater than or equal to the position the read function was | ||
| 38 | called with. | ||
| 39 | |||
| 40 | The input parameters C<$min> and C<$max> are positions and represents the | ||
| 41 | extent of the search. Only records which begin at positions within this range | ||
| 42 | (inclusive) will be searched. Moreover, C<$min> must be the starting position | ||
| 43 | of a record. If present C<$size> is a difference between positions and | ||
| 44 | determines when the algorithms switches to a sequential search. C<$val> is | ||
| 45 | an index value. The value of C<$handle> is of no consequence to the binary | ||
| 46 | search algorithm; it is merely passed as a convenience to the read function. | ||
| 47 | |||
| 48 | =head1 COPYRIGHT | ||
| 49 | |||
| 50 | Copyright 1998, Erik Rantapaa | ||
| 51 | |||
| 52 | This library is free software; you can redistribute it and/or | ||
| 53 | modify it under the same terms as Perl itself. | ||
| 54 | |||
| 55 | =cut | ||
| 56 | |||
| 57 | # use strict; | ||
| 58 | require Exporter; | ||
| 59 | @ISA = qw(Exporter); | ||
| 60 | @EXPORT = qw(binary_search); | ||
| 61 | |||
| 62 | $VERSION = "0.95"; | ||
| 63 | |||
| 64 | sub binary_search { | ||
| 65 | my $posmin = shift; | ||
| 66 | my $posmax = shift; | ||
| 67 | my $target = shift; | ||
| 68 | my $readfn = shift; | ||
| 69 | my $handle = shift; | ||
| 70 | my $smallblock = shift || 512; | ||
| 71 | |||
| 72 | my ($x, $compare, $mid, $lastmid); | ||
| 73 | my ($seeks, $reads); | ||
| 74 | |||
| 75 | # assert $posmin <= $posmax | ||
| 76 | |||
| 77 | $seeks = $reads = 0; | ||
| 78 | $lastmid = int(($posmin + $posmax)/2)-1; | ||
| 79 | while ($posmax - $posmin > $smallblock) { | ||
| 80 | |||
| 81 | # assert: $posmin is the beginning of a record | ||
| 82 | # and $target >= index value for that record | ||
| 83 | |||
| 84 | $seeks++; | ||
| 85 | $x = int(($posmin + $posmax)/2); | ||
| 86 | ($compare, $mid) = &$readfn($handle, $target, $x); | ||
| 87 | |||
| 88 | unless (defined($compare)) { | ||
| 89 | $posmax = $mid; | ||
| 90 | next; | ||
| 91 | } | ||
| 92 | last if ($mid == $lastmid); | ||
| 93 | if ($compare > 0) { | ||
| 94 | $posmin = $mid; | ||
| 95 | } else { | ||
| 96 | $posmax = $mid; | ||
| 97 | } | ||
| 98 | $lastmid = $mid; | ||
| 99 | } | ||
| 100 | |||
| 101 | # Switch to sequential search. | ||
| 102 | |||
| 103 | $x = $posmin; | ||
| 104 | while ($posmin <= $posmax) { | ||
| 105 | |||
| 106 | # same loop invarient as above applies here | ||
| 107 | |||
| 108 | $reads++; | ||
| 109 | ($compare, $posmin) = &$readfn($handle, $target, $x); | ||
| 110 | last unless (defined($compare) && $compare > 0); | ||
| 111 | $x = undef; | ||
| 112 | } | ||
| 113 | wantarray ? ($posmin, $seeks, $reads) : $posmin; | ||
| 114 | } | ||
| 115 | |||
| 116 | 1; |