]> git.vpit.fr Git - perl/modules/Scalar-Vec-Util.git/blobdiff - lib/Scalar/Vec/Util.pm
Introduce vrot()
[perl/modules/Scalar-Vec-Util.git] / lib / Scalar / Vec / Util.pm
index b5a8080e7f235a1cde6ca7693f59dd9547d92af0..dd8b1cbcea3b3cdb30da08a3098a114c9701b838 100644 (file)
@@ -146,6 +146,37 @@ sub vshift {
  }
 }
 
+=head2 C<< vrot $v, $start, $length, $bits >>
+
+In the area starting at C<$start> and of length C<$length> in C<$v>, rotates bits C<abs $bits> positions left if C<< $bits > 0 >> and right otherwise.
+Bits outside of the specified area are left untouched.
+Currently allocates an extra buffer of size C<O($bits)>.
+
+=cut
+
+sub vrot {
+ my ($start, $length, $bits) = @_[1 .. 3];
+ return unless $length and $bits;
+ my $left = 1;
+ if ($bits < 0) {
+  $left = 0;
+  $bits = -$bits;
+ }
+ $bits %= $length;
+ return unless $bits;
+ $length -= $bits;
+ my $buf = '';
+ if ($left) {
+  vcopy($_[0], $start + $length, $buf,  0,              $bits);
+  vcopy($_[0], $start,           $_[0], $start + $bits, $length);
+  vcopy($buf,  0,                $_[0], $start,         $bits);
+ } else {
+  vcopy($_[0], $start,           $buf,  0,                $bits);
+  vcopy($_[0], $start + $bits,   $_[0], $start,           $length);
+  vcopy($buf,  0,                $_[0], $start + $length, $bits);
+ }
+}
+
 =head2 C<< veq $v1 => $v1_start, $v2 => $v2_start, $length >>
 
 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.
@@ -166,7 +197,7 @@ sub veq_pp {
 
 =head1 EXPORT
 
-The functions L</vfill>, L</vcopy>, L</vshift> and L</veq> are only exported on request.
+The functions L</vfill>, L</vcopy>, L</vshift>, L</vrot> and L</veq> are only exported on request.
 All of them are exported by the tags C<':funcs'> and C<':all'>.
 
 The constants L</SVU_PP> and L</SVU_SIZE> are also only exported on request.
@@ -178,7 +209,7 @@ use base qw/Exporter/;
 
 our @EXPORT         = ();
 our %EXPORT_TAGS    = (
- 'funcs'  => [ qw/vfill vcopy vshift veq/ ],
+ 'funcs'  => [ qw/vfill vcopy vshift vrot veq/ ],
  'consts' => [ qw/SVU_PP SVU_SIZE/ ]
 );
 our @EXPORT_OK      = map { @$_ } values %EXPORT_TAGS;