Algorithm-BitVector
view release on metacpan or search on metacpan
Examples/BitVectorDemo.pl view on Meta::CPAN
#!/usr/bin/perl -w
#use lib '../blib/lib', '../blib/arch';
## BitVectorDemo.pl
use strict;
use Algorithm::BitVector 1.26;
# Construct an EMPTY bitvector (a bitvector of size 0):
print "\nConstructing an EMPTY bitvector (a bitvector of size 0):\n";
my $bv1 = Algorithm::BitVector->new( size => 0 );
print "$bv1\n"; # no output
# Construct a bitvector of size 2:
print "\nConstructing a bitvector of size 2:\n";
my $bv2 = Algorithm::BitVector->new( size => 2 );
print "$bv2\n"; # 00
# Joining two bitvectors:
print "\nConcatenating two previously constructed bitvectors:\n";
my $result = $bv1 + $bv2;
print "$result\n"; # 00
# The following works because Perl implicitly first stringyfies each
# argument before invoking the `.' operator
$result = $bv1 . $bv2;
print "$result\n"; # 00
# Construct a bitvector with a list of bits:
print "\nConstructing a bitvector from a list of bits:\n";
my $bv = Algorithm::BitVector->new( bitlist => [1, 1, 0, 1] );
print "$bv\n"; # 1101
# Construct a bitvector from an integer
$bv = Algorithm::BitVector->new( intVal => 5678 );
print "\nbitvector constructed from integer 5678:\n";
print "$bv\n"; # 1011000101110
print "\nbitvector constructed from integer 0:\n";
$bv = Algorithm::BitVector->new( intVal => 0 );
print "$bv\n"; # 0
print "\nbitvector constructed from integer 2:\n";
$bv = Algorithm::BitVector->new( intVal => 2 );
print "$bv\n"; # 10
print "\nbitvector constructed from integer 3:\n";
$bv = Algorithm::BitVector->new( intVal => 3 );
print "$bv\n"; # 11
print "\nbitvector constructed from integer 123456:\n";
$bv = Algorithm::BitVector->new( intVal => 123456 );
print "$bv\n"; # 11110001001000000
print "\nInt value of the previous bitvector as computed by int_value():\n";
print int($bv) . "\n"; # 123456
# Construct a bitvector from a very large integer:
use Math::BigInt;
my $x = Math::BigInt->new('12345678901234567890123456789012345678901234567890');
$bv = Algorithm::BitVector->new( intVal => $x );
print "\nHere is a bitvector constructed from a very large integer:\n";
print "$bv\n";
printf "The integer value of the above bitvector shown as a string is: %s\n", $bv->int_value();
print "Size of the bitvector: " . $bv->length() . "\n";
# Construct a bitvector of a specified length from a large integer:
$bv =Algorithm::BitVector->new(intVal => Math::BigInt->new("89485765"), size => 32);
print "\nHere is a bitvector of a specified size constructed from a large integer:\n";
my $len= $bv->length();
print "$bv\n";
print "size of bitvec: $len\n";
printf "The integer value of the above bitvector shown as a string is: %s\n", $bv->int_value();
print "Size of the bitvector: " . $bv->length() . "\n";
# Construct a bitvector directly from a bit string:
$bv = Algorithm::BitVector->new( bitstring => '00110011' );
print "\nBitvector constructed directly from a bit string:\n";
print "$bv\n"; # 00110011
$bv = Algorithm::BitVector->new( bitstring => '');
print "\nBitvector constructed directly from an empty bit string:\n";
print "$bv\n"; # nothing
print "\nInteger value of the previous bitvector:\n";
print int($bv) . "\n"; # 0
# Construct a bitvector directly from an ASCII text string:
print "\nConstructing a bitvector from the textstring 'hello':\n";
my $bv3 = Algorithm::BitVector->new( textstring => "hello" );
print "$bv3\n"; # 0110100001100101011011000110110001101111
#my $mytext = $bv3->get_text_from_bitvector();
my $mytext = $bv3->get_bitvector_in_ascii();
print "Text recovered from the previous bitvector:\n";
print "$mytext\n"; # hello
print "\nConstructing a bitvector from the textstring 'hello\\njello':\n";
$bv3 = Algorithm::BitVector->new( textstring => "hello\njello" );
print "$bv3\n";
# 0110100001100101011011000110110001101111000010100110101001100101011011000110110001101111
#$mytext = $bv3->get_text_from_bitvector();
$mytext = $bv3->get_bitvector_in_ascii();
print "Text recovered from the previous bitvector:\n";
print "$mytext\n"; # hello
# jello
# Construct a bitvector directly from a hex string:
print "\nConstructing a bitvector from the hexstring '68656c6c6f':\n";
my $bv4 = Algorithm::BitVector->new( hexstring => "68656c6c6f" );
print "$bv4\n"; # 0110100001100101011011000110110001101111
my $myhexstring = $bv4->get_bitvector_in_hex();
print "Hex string recovered from the previous bitvector: \n";
print "$myhexstring\n"; # 68656c6c6f
# Test get_bit() based array-like indexing for a bitvector:
$bv = Algorithm::BitVector->new( bitstring => '10111' );
print "\nPrints out bits individually from bitstring 10111:\n";
print $bv->get_bit(0), $bv->get_bit(1), $bv->get_bit(2), $bv->get_bit(3), $bv->get_bit(4), "\n"; # 10111
print "\nSame as above but using negative array indexing (shown in backward order):\n";
print $bv->get_bit(-1), $bv->get_bit(-2), $bv->get_bit(-3), $bv->get_bit(-4), $bv->get_bit(-5), "\n";
# Test setting bit values with positive and negative accessors:
$bv = Algorithm::BitVector->new( bitstring => '1111' );
print "\nBitstring for 1111:\n";
print "$bv\n"; # 1111
print "\nReset individual bits of above vector:\n";
$bv->set_bit(0,0); $bv->set_bit(1,0); $bv->set_bit(2,0); $bv->set_bit(3,0);
print "$bv\n"; # 0000
print "\nDo the same as above with negative indices:\n";
$bv->set_bit(-1,1); $bv->set_bit(-2,1); $bv->set_bit(-4,1);
print "$bv\n"; # 1011
# Check spaceship overloading for numeric comparison operators:
print "\nCheck equality and inequality ops:\n";
$bv1 = Algorithm::BitVector->new( bitstring => '00110011' );
$bv2 = Algorithm::BitVector->new( bitlist => [0,0,1,1,0,0,1,1] );
$bv1 == $bv2 ? print "1\n" : print "0\n"; # 1
$bv1 != $bv2 ? print "1\n" : print "0\n"; # 0
$bv1 < $bv2 ? print "1\n" : print "0\n"; # 0
$bv1 <= $bv2 ? print "1\n" : print "0\n"; # 1
$bv3 = Algorithm::BitVector->new( intVal => 5678 );
print $bv3->int_value(), "\n"; # 5678
print "$bv3\n"; # 1011000101110
$bv1 == $bv3 ? print "1\n" : print "0\n"; # 0
$bv3 > $bv1 ? print "1\n" : print "0\n"; # 1
$bv3 >= $bv1 ? print "1\n" : print "0\n"; # 1
# Check overloading of bitwise logical operators:
print "\nExperiments with bitwise logical operations:\n";
$bv = $bv1 | $bv2;
print "$bv\n"; # 00110011
$bv = $bv1 | $bv3;
print "$bv\n"; # 1011000111111
$bv = $bv1 & $bv2;
print "$bv\n"; # 00110011
$bv = $bv1 + $bv2;
print "$bv\n"; # 0011001100110011
$bv4 = Algorithm::BitVector->new( size => 3 );
print "$bv4\n"; # 000
my $bv5 = $bv + $bv4;
print "$bv5\n"; # 0011001100110011000
my $bv6 = ~$bv5;
print "$bv6\n"; # 1100110011001100111
my $bv7 = $bv5 & $bv6;
print "$bv7\n"; # 0000000000000000000
$bv7 = $bv5 | $bv6;
print "$bv7\n"; # 1111111111111111111
# Experiment with logical operations on bitvectors of different sizes:
print "\nTry logical operations on bitvectors of different sizes:\n";
$bv = Algorithm::BitVector->new(intVal=>6) ^ Algorithm::BitVector->new(intVal=>13);
print "$bv\n"; # 1011
$bv = Algorithm::BitVector->new(intVal=>6) & Algorithm::BitVector->new(intVal=>13);
print "$bv\n"; # 0100
$bv = Algorithm::BitVector->new(intVal=>6) | Algorithm::BitVector->new(intVal=>13);
print "$bv\n"; # 1111
$bv = Algorithm::BitVector->new(intVal=>1) ^ Algorithm::BitVector->new(intVal=>13);
print "$bv\n"; # 1100
$bv = Algorithm::BitVector->new(intVal=>1) & Algorithm::BitVector->new(intVal=>13);
print "$bv\n"; # 0001
$bv = Algorithm::BitVector->new(intVal=>1) | Algorithm::BitVector->new(intVal=>13);
print "$bv\n"; # 1101
# Experiments with set_bit() and length():\n";
print "\nExperiments with set_bit() and length():\n";
$bv7->set_bit(7,0);
print "$bv7\n"; # 1111111011111111111
print length($bv7) . "\n"; # 19
my $bv8 = ($bv5 & $bv6) ^ $bv7;
print "$bv8\n"; # 1111111011111111111
# Constructing a bitvector from the contents of a disk file:
print "\nConstruct a bitvector from what is in the file testinput1.txt:\n";
$bv = Algorithm::BitVector->new( filename => 'testinput.txt' );
#print "$bv\n"; # nothing to show
$bv1 = $bv->read_bits_from_file(64);
print "\nPrint out the first 64 bits read from the file:\n";
print "$bv1\n";
# 0100000100100000011010000111010101101110011001110111001001111001
print "\nRead the next 64 bits from the same file:\n";
$bv2 = $bv->read_bits_from_file(64);
print "$bv2\n";
# 0010000001100010011100100110111101110111011011100010000001100110
print "\nTake xor of the previous two bitvectors:\n";
$bv3 = $bv1 ^ $bv2;
print "$bv3\n";
# 0110000101000010000110100001101000011001000010010101001000011111
# Dividing an even-sized bitvector into two bitvectors:
print "\nExperiment with dividing an even-sized vector into two:\n";
($bv4,$bv5) = $bv3->divide_into_two();
print "$bv4\n"; # 01100001010000100001101000011010
print "$bv5\n"; # 00011001000010010101001000011111
# Permute a bitvector:
print "\nWe will use this bitvector for experiments with permute()\n";
$bv1 = Algorithm::BitVector->new( bitlist => [1, 0, 0, 1, 1, 0, 1] );
print "$bv1\n"; # 1001101
$bv2 = $bv1->permute( [6, 2, 0, 1] );
print "Permuted and contracted form of the previous bitvector:\n";
print "$bv2\n"; # 1010
# Write an internally generated bitvector to a disk file:
print "\nExperiment with writing an internally generated bitvector out to a disk file:\n";
$bv1 = Algorithm::BitVector->new( bitstring => '00001010' );
open my $FILEOUT, ">test.txt";
$bv1->write_to_file( $FILEOUT );
close $FILEOUT;
$bv2 = Algorithm::BitVector->new( filename => 'test.txt' );
$bv3 = $bv2->read_bits_from_file( 32 );
print "\nDisplay bitvectors written out to file and read back from the file and their respective lengths:\n";
print "$bv1 $bv3\n"; # 00001010 00001010
print length($bv1) . " " . length($bv3) . "\n"; # 8 8
# Experiment with reading a file from beginning to end and constructing 64-bit bit
# vectors as you go along:
print "\nExperiments with reading a file from the beginning to end:\n";
$bv = Algorithm::BitVector->new( filename => 'testinput.txt' );
print "Here are all the bits read from the file:\n";
while ($bv->{more_to_read}) {
my $bv_read = $bv->read_bits_from_file( 64 );
print "$bv_read\n";
}
print "\n";
# 0100000100100000011010000111010101101110011001110111001001111001
# 0010000001100010011100100110111101110111011011100010000001100110
# 0110111101111000001000000110101001110101011011010111000001100101
# 0110010000100000011011110111011001100101011100100010000001100001
# 0010000001101100011000010111101001111001001000000111011101101001
# 0110110001100100001000000110010001101111011001110010111000001010
# 0000101001100001011000010110000101100001011000010110000101100001
# 0110000101100001011000010110000101100001000010100000101001101010
# 0110101001101010011010100110101001101010011010100110101001101010
# 0110101001101010011010100110101001101010011010100110101001101010
# 0000101000001010
print "\nExperiment with closing a file object and start extracting bitvectors from the file from the beginning again:\n";
$bv->close_file_handle();
$bv = Algorithm::BitVector->new( filename => 'testinput.txt' );
$bv1 = $bv->read_bits_from_file(64);
print "Here are all the first 64 bits read from the file again after the file object was closed and opened again:\n";
print "$bv1\n"; # 0100000100100000011010000111010101101110011001110111001001111001
open $FILEOUT, ">testinput5.txt";
$bv1->write_to_file( $FILEOUT );
close $FILEOUT;
# Experiments in 64-bit permutation and unpermutation:
print "\nExperiment in 64-bit permutation and unpermutation of the previous 64-bit bitvector:\n";
print "The permutation array was generated separately by the Fisher-Yates shuffle algorithm:\n";
$bv2 = $bv1->permute( [22, 47, 33, 36, 18, 6, 32, 29, 54, 62, 4,
9, 42, 39, 45, 59, 8, 50, 35, 20, 25, 49,
15, 61, 55, 60, 0, 14, 38, 40, 23, 17, 41,
10, 57, 12, 30, 3, 52, 11, 26, 43, 21, 13,
58, 37, 48, 28, 1, 63, 2, 31, 53, 56, 44, 24,
51, 19, 7, 5, 34, 27, 16, 46] );
print "Permuted bitvector:\n";
print "$bv2\n"; # 0111100110001011010111000100100111100000100011001101000010101101
$bv3 = $bv2->unpermute( [22, 47, 33, 36, 18, 6, 32, 29, 54, 62, 4,
9, 42, 39, 45, 59, 8, 50, 35, 20, 25, 49,
15, 61, 55, 60, 0, 14, 38, 40, 23, 17, 41,
10, 57, 12, 30, 3, 52, 11, 26, 43, 21, 13,
58, 37, 48, 28, 1, 63, 2, 31, 53, 56, 44, 24,
51, 19, 7, 5, 34, 27, 16, 46] );
print "Result obtained by unpurmuting the permuted bitvector:\n";
print "$bv3\n"; # 0100000100100000011010000111010101101110011001110111001001111001
# Experiments with circular shifts to the left and to the right:
print "\nTry circular shifts to the left and to the right for the following bitvector:\n";
print "$bv3\n"; # 0100000100100000011010000111010101101110011001110111001001111001
print "Circular shift to the left by 7 positions:\n";
$bv3 = $bv3 << 7;
print "$bv3\n"; # 1001000000110100001110101011011100110011101110010011110010100000
print "\nCircular shift to the right by 7 positions:\n";
$bv3 = $bv3 >> 7;
print "$bv3\n"; # 0100000100100000011010000111010101101110011001110111001001111001
print "Test length on the above bitvector: ";
print length($bv3) . "\n"; # 64
print "\nExperiments with chained invocations of circular shifts:\n";
$bv = Algorithm::BitVector->new( bitlist => [1, 1, 1, 0, 0, 1] );
print "$bv\n"; # 111001
$bv = $bv >> 1;
print "$bv\n"; # 111100
$bv = $bv >> 1 >> 1;
print "$bv\n"; # 001111
$bv = Algorithm::BitVector->new( bitlist => [1, 1, 1, 0, 0, 1] );
print "$bv\n"; # 111001
$bv = $bv << 1;
print "$bv\n"; # 110011
$bv = $bv << 1 << 1;
print "$bv\n"; # 001111
# Experiment with extracting a slice (in the form of a list of bits) from a bitvector:
print "\nTest forming a [5..21] list-based slice of the above bitvector:\n";
# The following call will return bits indexed from 5 through 20 from the $bv3 bitvector:
my $arr = $bv3->get_bit( [5..21] );
print "@$arr\n"; # 0 0 1 0 0 1 0 0 0 0 0 0 1 1 0 1
# Experiment with extracting a slice in the form of a BitVector from a larger BitVector:
print "\nTest get_slice() method that returns a BitVector:\n";
my $bv9 = Algorithm::BitVector->new( intVal => 63437, size => 16 );
print "BitVector for testing get_slice(): $bv9\n"; # 1111011111001101
my $slice_bv = $bv9->get_slice( [5..11] );
print "slice BitVector for index values 5 through 10: $slice_bv\n"; # 111110
# Experiment with setting a slice in a previously constructed BitVector for a given index range:
print "\nTest set_slice() method for setting a slice in a BitVector:\n";
print "BitVector for testing set_slice(): $bv9\n"; # 1111011111001101
my $values_bv = Algorithm::BitVector->new( bitlist => [1,1,1,1] );
$bv9->set_slice( [4..8], $values_bv );
print "BitVector after set_slice(): $bv9\n"; # 1111111111001101
# Experiment with the overloading of the iterator `<>':
print "\nTest the iterator:\n";
while (<$bv4>) {
print "$_ ";
} # 0 1 1 0 0 0 0 1 0 1 0 0 0 0 1 0 0 0 0 1 1 0 1 0 0 0 0 1 1 0 1 0
print "\n";
# Experiments with padding a bitvector with zeros:
print "\nDemonstrate padding a bitvector from left:\n";
$bv = Algorithm::BitVector->new( bitstring => '101010' );
$bv->pad_from_left( 4 );
print "$bv\n"; # 000010101
print "\nDemonstrate padding a bitvector from right:\n";
$bv->pad_from_right( 4 );
print "$bv\n"; # 00001010100000
# Experiments with the size modifier when int value is used for constructing a bitvector:
print "\nTest the size modifier when a bitvector is initialized with the intVal method:\n";
$bv = Algorithm::BitVector->new( intVal => 45, size => 16 );
print "$bv\n"; # 0000000000101101
$bv = Algorithm::BitVector->new( intVal => 0, size => 8 );
print "$bv\n"; # 00000000
$bv = Algorithm::BitVector->new( intVal => 1, size => 8 );
print "$bv\n"; # 00000001
( run in 0.402 second using v1.01-cache-2.11-cpan-119454b85a5 )