1 package Scalar::Vec::Util;
10 Scalar::Vec::Util - Utility routines for vec strings.
23 XSLoader::load(__PACKAGE__, $VERSION);
36 use Scalar::Vec::Util qw/vfill vcopy veq/;
39 vfill $s, 0, 100, 1; # Fill with 100 bits 1 starting at 0.
41 vcopy $s, 20, $t, 10, 30; # Copy 30 bits from $s, starting at 20,
42 # to $t, starting at 10.
43 vcopy $t, 10, $t, 20, 30; # Overalapping areas DWIM.
44 if (veq $t, 10, $t, 20, 30) { ... } # Yes, they are equal now.
48 A set of utilities to manipulate bits in vec strings. Highly optimized XS routines are used when available, but straightforward pure perl replacements are also provided for platforms without a C compiler.
50 This module doesn't reimplement bit vectors. It can be used on the very same scalars that C<vec> builds, or actually on any Perl string (C<SVt_PV>).
56 True when pure perl fallbacks are used instead of XS functions.
60 Size in bits of the unit used for moves. The higher this value is, the faster the XS functions are. It's usually C<CHAR_BIT * $Config{alignbytes}>, except on non-little-endian architectures where it currently falls back to C<CHAR_BIT> (e.g. SPARC).
64 =head2 C<vfill $vec, $start, $length, $bit>
66 Starting at C<$start> in C<$vec>, fills C<$length> bits with C<$bit>. Grows C<$vec> if necessary.
71 for (@_) { return 0 unless defined }
76 (undef, my $s, my $l, my $x) = @_;
77 croak "Invalid argument" unless _alldef @_;
80 vec($_[0], $_, 1) = $x for $s .. $s + $l - 1;
83 =head2 C<< vcopy $from => $from_start, $to => $to_start, $length >>
85 Copies C<$length> bits starting at C<$from_start> in C<$from> to C<$to_start> in C<$to>. If C<$from_start + $length> is too long for C<$from>, zeros are copied past C<$length>. Grows C<$to> if necessary.
90 my ($fs, $ts, $l) = @_[1, 3, 4];
91 croak "Invalid argument" unless _alldef @_;
95 vec($_[2], $_ + $step, 1) = vec($_[0], $_, 1) for $fs .. $fs + $l - 1;
96 } else { # There's a risk of overwriting if $_[0] and $_[2] are the same SV.
97 vec($_[2], $_ + $step, 1) = vec($_[0], $_, 1) for reverse $fs .. $fs + $l - 1;
101 =head2 C<< veq $v1 => $v1_start, $v2 => $v2_start, $length >>
103 Returns true if the C<$length> bits starting at C<$v1_start> in C<$v1> and C<$v2_start> in C<$v2> are equal, and false otherwise. If needed, C<$length> is decreased to fit inside C<$v1> and C<$v2> boundaries.
108 my ($s1, $s2, $l) = @_[1, 3, 4];
109 croak "Invalid argument" unless _alldef @_;
112 return 0 if vec($_[0], $s1 + $i, 1) != vec($_[2], $s2 + $i, 1);
120 The functions L</vfill>, L</vcopy> and L</veq> are only exported on request. All of them are exported by the tags C<':funcs'> and C<':all'>.
122 The constants L</SVU_PP> and L</SVU_SIZE> are also only exported on request. They are all exported by the tags C<':consts'> and C<':all'>.
126 use base qw/Exporter/;
130 'funcs' => [ qw/vfill vcopy veq/ ],
131 'consts' => [ qw/SVU_PP SVU_SIZE/ ]
133 our @EXPORT_OK = map { @$_ } values %EXPORT_TAGS;
134 $EXPORT_TAGS{'all'} = [ @EXPORT_OK ];
138 Please report architectures where we can't use the alignment as the move unit. I'll add exceptions for them.
142 L<Carp>, L<Exporter> (core modules since perl 5), L<XSLoader> (since perl 5.006).
146 L<Bit::Vector> gives a complete reimplementation of bit vectors.
150 Vincent Pit, C<< <perl at profvince.com> >>, L<http://www.profvince.com>.
152 You can contact me by mail or on #perl @ FreeNode (vincent or Prof_Vince).
156 Please report any bugs or feature requests to C<bug-scalar-vec-util at rt.cpan.org>, or through the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Scalar-Vec-Util>. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.
160 You can find documentation for this module with the perldoc command.
162 perldoc Scalar::Vec::Util
164 Tests code coverage report is available at L<http://www.profvince.com/perl/cover/Scalar-Vec-Util>.
166 =head1 COPYRIGHT & LICENSE
168 Copyright 2008 Vincent Pit, all rights reserved.
170 This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
174 1; # End of Scalar::Vec::Util