The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

  Tie::VecArray - An array interface to a bit vector.

SYNOPSIS

  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[0];

DESCRIPTION

This module implements an array interface to a bit vector.

Method

tie
  $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
  $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;

CAVEATS

Its slow

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.

No multidimentional arrays

$vec_array[$i][$j] = $num; isn't going to work. A future class will cover this possibility.

AUTHOR

Michael G Schwern <schwern@pobox.com>

SEE ALSO

"vec" in perlfunc, Tie::Array

1 POD Error

The following errors were encountered while parsing the POD:

Around line 204:

You forgot a '=back' before '=head1'