Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re: Efficient 7bit compression

by Roy Johnson (Monsignor)
on Mar 14, 2005 at 15:49 UTC ( [id://439318]=note: print w/replies, xml ) Need Help??


in reply to Efficient 7bit compression

It's easy, but does it count as efficient to expand the strings this way?
use strict; use warnings; my $str = 'abcdefghijklmnop'; sub shrink { my $bitstr = unpack('B*', $_[0]); $bitstr =~ s/.(.{7})/$1/g; pack('B*', $bitstr); } sub grow { my $bitstr = unpack('B*', $_[0]); $bitstr =~ s/(.{7})/0$1/g; pack('B*', $bitstr); } my $shrunk = shrink($str); printf "Was: %s, Is: %s\n", length($str), length($shrunk); printf "Restored: <%s>\n", grow $shrunk;

Caution: Contents may have been coded under pressure.

Replies are listed 'Best First'.
Re^2: Efficient 7bit compression
by Limbic~Region (Chancellor) on Mar 14, 2005 at 15:52 UTC
    Roy Johnson,
    This looks similar to my unshown trading memory for time solution. The $bitstr will temporarily be 8 times larger than the original $str. I outlined 2 kinds of efficiency in my post (RAM & runtime). Now if there was a way to have our cake and eat it too.
    Thanks!

    Cheers - L~R

      This has constant overhead by expanding only 8 chars at a time.
      sub shrink { my $packed = ''; while ($_[0] =~ /(.{1,8})/gs) { my $bitstr = unpack('B*', $1); $bitstr =~ s/.(.{7})/$1/g; $packed .= pack('B*', $bitstr); } $packed; } sub grow { my $unpacked = ''; while ($_[0] =~ /(.{1,7})/gs) { my $bitstr = unpack('B*', $1); $bitstr =~ s/(.{7})/0$1/g; $unpacked .= pack('B*', $bitstr); } $unpacked; }

      Caution: Contents may have been coded under pressure.
        Roy Johnson,
        I tried a similar approach only using vec() and starting 1 bit back from the end of the string each time - I got bizarre results. An idea I had that I would have pursued if I could have made it work would be to pre-size the strings and use 4arg substr instead of growing it each time.

        Cheers - L~R

      I'm not sure how this fares in reguard to speed, but it only uses 2-5 scalars, tries to avoid generating intermidiate lists, and modifies their arguments inplace.
      sub shrink { my $dbit = 0; for (my $sbit = 0; $sbit < length($_[0]) * 8; $sbit++) { next if $sbit % 8 == 7; vec($_[0], $dbit++, 1) = vec($_[0], $sbit, 1); } my $dlen = length($_[0]) * 7 / 8; $dlen++ unless $dlen == int($dlen); $dlen = int($dlen); my $extra = length($_[0]) - $dlen; if ($extra > 0) { substr($_[0], $dlen, $extra, ''); } for (my $pbit = $dbit; $pbit < $dlen * 8; $pbit++) { vec($_[0], $pbit, 1) = 0; } } sub grow { my $sbit = int(length($_[0]) * 8 / 7) * 7 - 1; for (my $dbit = int(length($_[0]) * 8 / 7) * 8 - 1; $dbit >= 0; $d +bit--) { vec($_[0], $dbit, 1) = $dbit % 8 == 7 ? 0 : vec($_[0], $sbit-- +, 1); } }
      I have the nagging fealing there is a better way to implement ceil (near the middle of shrink).
      If padding the compressed string with 0 bits is not needed then the second for loop of shrink can be omitted to save (on average) 4 bits of time.
        ...a better way to implement ceil...
        my $dlen = int((7 + length($_[0]) * 7)/8); substr($_[0], $dlen) = ''; vec ($_[0], $dbit++, 1) = 0 while ($dbit < $dlen * 8); }
        This replaces your shrink function from the definition of $dlen on.

        Caution: Contents may have been coded under pressure.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://439318]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (10)
As of 2024-04-18 14:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found