X-Git-Url: http://git.vpit.fr/?a=blobdiff_plain;f=lib%2FScalar%2FVec%2FUtil.pm;h=b7ab6ab774e36fc5452c33a85c98e2dca08e3eb4;hb=dec1755eec42f54c4d57813ed03393c0f765a699;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..b7ab6ab 100644 --- a/lib/Scalar/Vec/Util.pm +++ b/lib/Scalar/Vec/Util.pm @@ -77,12 +77,21 @@ sub _alldef { return 1; } -sub vfill_pp { +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 >> @@ -94,7 +103,7 @@ Doesn't need to allocate any extra memory. =cut -sub vcopy_pp { +sub vcopy_pp ($$$$$) { my ($fs, $ts, $l) = @_[1, 3, 4]; croak "Invalid argument" unless _alldef @_; return unless $l; @@ -115,7 +124,7 @@ Doesn't need to allocate any extra memory. =cut -sub vshift { +sub vshift ($$$$;$) { my ($start, $length, $bits, $insert) = @_[1 .. 4]; return unless $bits; my $left = 1; @@ -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); } } @@ -141,7 +184,7 @@ If needed, C<$length> is decreased to fit inside C<$v1> and C<$v2> boundaries. =cut -sub veq_pp { +sub veq_pp ($$$$$) { my ($s1, $s2, $l) = @_[1, 3, 4]; croak "Invalid argument" unless _alldef @_; my $i = 0; @@ -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;