X-Git-Url: http://git.vpit.fr/?a=blobdiff_plain;f=lib%2FScalar%2FVec%2FUtil.pm;h=dd8b1cbcea3b3cdb30da08a3098a114c9701b838;hb=856caa5792565f48b11a5dc86856519b649eaffb;hp=c80d6424c1c4dc920c4b9729053a7fd271b00a81;hpb=0062dbd2da53db62346382e729b84097131c61ee;p=perl%2Fmodules%2FScalar-Vec-Util.git diff --git a/lib/Scalar/Vec/Util.pm b/lib/Scalar/Vec/Util.pm index c80d642..dd8b1cb 100644 --- a/lib/Scalar/Vec/Util.pm +++ b/lib/Scalar/Vec/Util.pm @@ -81,8 +81,17 @@ sub vfill_pp { (undef, my $s, my $l, my $x) = @_; croak "Invalid argument" unless _alldef @_; return unless $l; - $x = 1 if $x; - vec($_[0], $_, 1) = $x for $s .. $s + $l - 1; + $x = ~0 if $x; + my $SIZE = 32; + my $t = int($s / $SIZE) + 1; + my $u = int(($s + $l) / $SIZE); + if ($SIZE * $t < $s + $l and $t <= $u) { + vec($_[0], $_, 1) = $x for $s .. $SIZE * $t - 1; + vec($_[0], $_, $SIZE) = $x for $t .. $u - 1; + vec($_[0], $_, 1) = $x for $SIZE * $u .. $s + $l - 1; + } else { + vec($_[0], $_, 1) = $x for $s .. $s + $l - 1; + } } =head2 C<< vcopy $from => $from_start, $to => $to_start, $length >> @@ -123,14 +132,48 @@ sub vshift { $left = 0; $bits = -$bits; } - $bits = $length if $bits > $length; + if ($bits < $length) { + $length -= $bits; + if ($left) { + vcopy($_[0], $start, $_[0], $start + $bits, $length); + vfill($_[0], $start, $bits, $insert) if defined $insert; + } else { + vcopy($_[0], $start + $bits, $_[0], $start, $length); + vfill($_[0], $start + $length, $bits, $insert) if defined $insert; + } + } else { + vfill($_[0], $start, $length, $insert) if defined $insert; + } +} + +=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 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. + +=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, $_[0], $start + $bits, $length); - vfill($_[0], $start, $bits, $insert) if defined $insert; + vcopy($_[0], $start + $length, $buf, 0, $bits); + vcopy($_[0], $start, $_[0], $start + $bits, $length); + vcopy($buf, 0, $_[0], $start, $bits); } else { - vcopy($_[0], $start + $bits, $_[0], $start, $length); - vfill($_[0], $start + $length, $bits, $insert) if defined $insert; + vcopy($_[0], $start, $buf, 0, $bits); + vcopy($_[0], $start + $bits, $_[0], $start, $length); + vcopy($buf, 0, $_[0], $start + $length, $bits); } } @@ -154,7 +197,7 @@ sub veq_pp { =head1 EXPORT -The functions L, L, L and L are only exported on request. +The functions L, L, L, L and L are only exported on request. All of them are exported by the tags C<':funcs'> and C<':all'>. The constants L and L are also only exported on request. @@ -166,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;