Algorithm-BinarySearch-Vec
view release on metacpan or search on metacpan
package Algorithm::BinarySearch::Vec;
use Exporter;
use Carp;
use AutoLoader;
use Config qw();
use strict;
use bytes;
our @ISA = qw(Exporter);
our $VERSION = '0.08';
our ($HAVE_XS);
eval {
require XSLoader;
$HAVE_XS = XSLoader::load('Algorithm::BinarySearch::Vec', $VERSION);
} or do {
$HAVE_XS = 0;
};
# Preloaded methods go here.
#require Algorithm::BinarySearch::Vec::XS::Whatever;
# Autoload methods go after =cut, and are processed by the autosplit program.
##======================================================================
## Exports
##======================================================================
no warnings 'portable'; ##-- avoid "Bit vector size > 32 non-portable" errors for native quads
our $HAVE_QUAD = ($Config::Config{use64bitint} ##-- avoid errors with xs U64TYPE but no perl-side 64bit ints (e.g. freebsd w/o -use64bitint perl config option)
&&
($HAVE_XS ? Algorithm::BinarySearch::Vec::XS::HAVE_QUAD() : $Config::Config{d_quad})
);
our $KEY_NOT_FOUND = $HAVE_XS ? Algorithm::BinarySearch::Vec::XS::KEY_NOT_FOUND() : 0xffffffff;
#our $KEY_NOT_FOUND = $HAVE_XS ? Algorithm::BinarySearch::Vec::XS::KEY_NOT_FOUND() : ($HAVE_QUAD ? 0xffffffffffffffff : 0xffffffff);
our (%EXPORT_TAGS, @EXPORT_OK, @EXPORT);
BEGIN {
%EXPORT_TAGS =
(
api => [qw( vbsearch vbsearch_lb vbsearch_ub),
qw(vabsearch vabsearch_lb vabsearch_ub),
qw(vvbsearch vvbsearch_lb vvbsearch_ub),
qw(vunion vintersect vsetdiff),
],
const => [qw($HAVE_QUAD $KEY_NOT_FOUND)],
debug => [qw(vget vset vec2array)],
);
$EXPORT_TAGS{all} = [map {@$_} @EXPORT_TAGS{qw(api const debug)}];
$EXPORT_TAGS{default} = [map {@$_} @EXPORT_TAGS{qw(api const)}];
@EXPORT_OK = @{$EXPORT_TAGS{all}};
@EXPORT = @{$EXPORT_TAGS{default}};
}
##======================================================================
## Debug wrappers
##--------------------------------------------------------------
## $val = vget($vec,$i,$nbits)
sub _vget {
return vec($_[0],$_[1],$_[2]);
}
##--------------------------------------------------------------
## undef = vset($vec,$i,$nbits,$val)
sub _vset {
return vec($_[0],$_[1],$_[2])=$_[3];
}
##======================================================================
## API: Search: element-wise
##--------------------------------------------------------------
## $index = vbsearch($v,$key,$nbits)
## $index = vbsearch($v,$key,$nbits,$ilo,$ihi)
sub _vbsearch {
my ($vr,$key,$nbits,$ilo,$ihi) = (\$_[0],@_[1..$#_]);
$ilo = 0 if (!defined($ilo));
$ihi = 8*length($$vr)/$nbits if (!defined($ihi));
my ($imid);
while ($ilo < $ihi) {
$imid = ($ihi+$ilo) >> 1;
if (vec($$vr,$imid,$nbits) < $key) {
$ilo = $imid + 1;
} else {
$ihi = $imid;
}
}
return ($ilo==$ihi) && vec($$vr,$ilo,$nbits)==$key ? $ilo : $KEY_NOT_FOUND;
( run in 0.420 second using v1.01-cache-2.11-cpan-e1769b4cff6 )