Algorithm-BitVector
view release on metacpan or search on metacpan
lib/Algorithm/BitVector.pm view on Meta::CPAN
$self->{bitlist} = undef;
return $self;
}
## Set the bit at the designated position to the value shown
sub set_bit {
my $self = shift;
my $posn = shift;
my $val = shift;
croak "incorrect value for a bit" unless $val =~ /\d/ && ($val == 0 or $val == 1);
die "index range error" if ($posn >= $self->{size}) or ($posn < - $self->{size});
$posn = $self->{size} + $posn if $posn < 0;
my $block_index = int($posn / 16);
my $shift = $posn & 0xF;
my $cv = $self->{_vector}->[$block_index];
if ( ( ( $cv >> $shift ) & 1 ) != $val) {
$self->{_vector}->[$block_index] = $cv ^ (1 << $shift);
}
}
## Get the bit from the designated position. This method can also return a slice of a
## bitvector. HOWEVER, NOTE THAT THE SLICE IS RETURNED AS A LIST OF THE BITS IN THE
## INDEX RANGE YOU SPECIFIED. You can either easily convert the list of bits returned
## into a bitvector in your own code or, starting with Version 1.26, you can call the
## get_slice() method.
sub get_bit {
my $self = shift;
my $pos = shift;
unless (ref($pos) eq "ARRAY") {
die "index range error" if ($pos >= $self->{size}) or ($pos < - $self->{size});
$pos = $self->{size} + $pos if $pos < 0;
return ( $self->{_vector}->[int($pos/16)] >> ($pos&15) ) & 1;
}
# my @slice = map $self->get_bit($_), (@$pos)[0..@$pos-1];
my @slice = map $self->get_bit($_), (@$pos)[0..@$pos-2];
return \@slice;
}
## Get the slice of bits from the bitvector corresponding to the index range specified
## by the argument. The slice is returned as an instance of Algorithm::BitVector
lib/Algorithm/BitVector.pm view on Meta::CPAN
## Read blocksize bits from a disk file and return a BitVector object containing
## the bits. If the file contains fewer bits than blocksize, construct the
## BitVector object from however many bits there are in the file. If the file
## contains zero bits, return a BitVector object of size attribute set to 0.
sub read_bits_from_file {
my $self = shift;
die "Abort: The read_bits_from_file() method invoked on an object that is " .
"not of type Algorithm::BitVector"
unless UNIVERSAL::isa( $self, 'Algorithm::BitVector');
my $blocksize = shift;
my $error_str = "You need to first construct a BitVector object with a filename as argument";
die "$error_str" unless $self->{filename};
die "block size must be a multiple of 8" if $blocksize % 8;
my $bitstr = _readblock( $blocksize, $self );
if (length $bitstr == 0) {
return Algorithm::BitVector->new( size => 0 );
print "file has no bits\n";
} else {
return Algorithm::BitVector->new( bitstring => $bitstr );
}
}
lib/Algorithm/BitVector.pm view on Meta::CPAN
was significantly upgraded in this version.
Version 1.22 removes the Perl version restriction from the module and the
C<Makefile.PL> files and the C<PREREQ_PM> restrictions from the C<Makefile.PL> file.
Version 1.21 fixes a bug in the code for the Miller-Rabin primality test function
C<test_for_primality()>. This version also places a hard limit on the size of the
integers that are allowed to be tested for primality.
Version 1.2 fixes an important bug in creating bitvectors from the contents of a disk
file. This version also includes corrections for some of the documentation errors
discovered.
Version 1.1 incorporates additional type checking on the operands for the overloaded
operators. Also fixed some minor documentation formatting issues.
=head1 DESCRIPTION
My main motivation for creating this module was to provide the students at Purdue and
elsewhere with a Perl class whose API is the same as that of my Python based
C<BitVector> module that appears to have become popular for prototyping algorithms
lib/Algorithm/BitVector.pm view on Meta::CPAN
=head1 THANKS
The bug in the primality test function, whose fix resulted in Version 1.21, was
reported by Dana Jacobsen in a bug report filed at C<rt.cpan.org>. Thanks Dana!
The restriction on the Perl version was removed on Slaven Rezic's recommendation. He
says the module runs fine with Perl 5.8.9. Thanks Slaven!
Austin Nobis reported a documentation error in Version 1.24 which was fixed in Version
1.25. Thanks Austin!
=head1 AUTHOR
The author, Avinash Kak, recently finished a 17-years long "Objects Trilogy Project"
with the publication of the book "Designing with Objects" by John-Wiley. If
interested, visit his web page at Purdue to find out what this project was all
about. You might like "Designing with Objects" especially if you enjoyed reading
Harry Potter as a kid (or even as an adult, for that matter).
( run in 0.692 second using v1.01-cache-2.11-cpan-65fba6d93b7 )