in reply to Re^5: Efficient 7bit compression
in thread Efficient 7bit compression

Roy Johnson,
Are you talking about shrinking in place, so you don't have the overhead of having the compressed and uncompressed strings in memory at the same time?

No - but neat. What I was talking about was with regards to $unpacked = ''. Instead, pre-size it and use the 4 arg substr to fill it up instead of growing it each time. As I said before, I didn't get my vec() approach to work but I would have also been able to do this in the shrink() sub as well.

Cheers - L~R

Replies are listed 'Best First'.
Re^7: Efficient 7bit compression
by Roy Johnson (Monsignor) on Mar 14, 2005 at 18:15 UTC
    I've been playing with it, and I see the problem. vec is horrible, because it reverses everything. When you pack them together by sevens and take them apart by eights, it's a mess to try to get everything reversed.

    Here's a version with pre-sized return strings, using vec (but there's no need for substr). No packing, so no reversed-bits madness to deal with.

    sub shrink { my $shrunk = "\0" x (length($_[0]) * 7/8); my $bit = 0; for my $orig_bit (0..(8*length($_[0])-1)) { next if $orig_bit % 8 == 7; vec($shrunk, $bit++, 1) = vec($_[0], $orig_bit, 1); } $shrunk; } sub grow { my $grown = "\0" x (length($_[0]) * 8/7); my $orig_bit = 0; for my $bit (0..(8*length($_[0])-1)) { vec($grown, $orig_bit++, 1) = vec($_[0], $bit, 1); vec($grown, $orig_bit++, 1) = 0 if $orig_bit % 8 == 7; } $grown; }

    Caution: Contents may have been coded under pressure.