Tie::VecArray - An array interface to a bit vector.
require Tie::VecArray; $vector = ''; vec($vector, 0, 32) = 33488897; # Tie the vector to an array as 8 bits. $obj = tie @array, 'Tie::VecArray', 8, $vector; @array[0..3] = qw(1 255 256 -1); # SURPRISE! Its 1, 255, 0, 255! print @array[0..3]; # Look at the same vector as a 32 bit vector. $obj->bits(32); # Back to 33488897 print $array;
This module implements an array interface to a bit vector.
$vec_obj = tie(@array, 'Tie::VecArray', $bits); $vec_obj = tie(@array, 'Tie::VecArray', $bits, $vec);
Creates a new @array tied to a bit vector. $bits is the number of bits which will be passed to
vec() to interpret the vector.
If $vec is given that will be used as the bit vector, otherwise the vector will start out empty.
$bits = $vec_obj->bits; $vec_obj->bits($bits);
Get/set the bit size we'll use to interpret the vector.
When setting the bit size the length of the array might be ambiguous. (For instance, going from a one bit vector with five entries to a two bit vector... do you have two or three entries?) The length of the array will always round up. This can cause odd things to happen. Consider:
$vec_obj = tie @vec, 'Tie::VecArray', 1; # A one bit vector with 5 entries. @vec[0..4] = (1) x 5; # prints a size of 5, as expected. print scalar @vec; # Switch to two bit interpretation. $vec_obj->bits(2); # This returns 3 since it will round up. print scalar @vec; # Switch back to one bit. $vec_obj->bits(1); # Whoops, 6! print scalar @vec;
Due to the sluggishness of perl's tie interface, this module is about 8 times slower than using direct calls with
vec(). Suck. If you care alot about speed, don't use this module. If you care about easy bit vector access or low memory usage, use this module.
$vec_array[$i][$j] = $num; isn't going to work. A future class will cover this possibility.
Michael G Schwern <email@example.com>