Professional Web Applications Themes

Compression (besides Huffman) and Ruby - Ruby

Hi! I don't want to start a lenghty thread about wether that makes sense or not. My question is only if such a thing is known to exist. I did find Huffman but besides that rei, nada, rien, zero, nichts. Anything else (note that the point is *plain* Ruby, no extension). Otherwise I will try to implement such an algorithm on my own. Josef 'Jupp' SCHUGT -- http://oss.erdfunkstelle.de/ruby/ - German comp.lang.ruby-FAQ http://rubyforge.org/users/jupp/ - Ruby projects at Rubyforge -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- Germany 2004: To boldly spy where no GESTAPO / STASI has spied before...

  1. #1

    Default Compression (besides Huffman) and Ruby

    Hi!

    I don't want to start a lenghty thread about wether that makes sense
    or not. My question is only if such a thing is known to exist. I did
    find Huffman but besides that rei, nada, rien, zero, nichts. Anything
    else (note that the point is *plain* Ruby, no extension). Otherwise I
    will try to implement such an algorithm on my own.

    Josef 'Jupp' SCHUGT
    --
    http://oss.erdfunkstelle.de/ruby/ - German comp.lang.ruby-FAQ
    http://rubyforge.org/users/jupp/ - Ruby projects at Rubyforge
    -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
    Germany 2004: To boldly spy where no GESTAPO / STASI has spied before


    Josef Guest

  2. #2

    Default Re: Compression (besides Huffman) and Ruby

    --Boundary-02=_AL2+/FjRAtxYqwa
    Content-Type: text/plain;
    cht="utf-8"
    Content-Transfer-Encoding: quoted-printable
    Content-Description: signed data
    Content-Disposition: inline

    On Tuesday 06 January 2004 6:06 pm, Josef 'Jupp' SCHUGT wrote: 

    Well, here's a really bad RLE encoder/decoder. ;)

    #!/usr/bin/ruby

    $last=3Dnil
    $count=3D0

    def compress
    while gets
    $_.each_byte { |c|
    if c =3D=3D $last
    $count +=3D 1
    else
    puts "#{$count}:#{$last}";
    $last =3D c
    $count =3D 1
    end
    }
    end
    end

    def decompress
    while gets
    ~ /(\d+):(\d+)/
    $1.to_i.times { s =3D ' '; s[0] =3D $2.to_i; print s }
    end
    end

    if ARGV.shift =3D=3D '-d'
    decompress
    else
    compress
    end

    Usually I just do:

    require 'zlib'

    Since it comes with ruby. =3D)

    =2D-=20
    Wesley J. Landaker - net
    OpenPGP FP: 4135 2A3B 4726 ACC5 9094 0097 F0A9 8A4C 4CD6 E3D2


    --Boundary-02=_AL2+/FjRAtxYqwa
    Content-Type: application/pgp-signature
    Content-Description: signature

    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v1.2.4 (GNU/Linux)

    iD8DBQA/+2LA8KmKTEzW49IRAkvKAJ9O7L6KUfvNQCq3KpwLCOyDMO2wpg CeKIE5
    XOeeqH1C0dMPlNDY2yNTVic=
    =trBq
    -----END PGP SIGNATURE-----

    --Boundary-02=_AL2+/FjRAtxYqwa--

    Wesley Guest

  3. #3

    Default Re: Compression (besides Huffman) and Ruby

    Josef 'Jupp' SCHUGT wrote:
     
    I've played with the idea of implementing zlib in pure Ruby...
    unfortunately, I've never got beyond that. I've got about 30% of a
    working tar implementation, but that's not really compression. I'd be
    very interested in a pure ruby zlib or bzip2 implementation, if you
    decide to write one. ;)

    - Jamis

    --
    Jamis Buck
    byu.edu

    ruby -h | ruby -e 'a=[];readlines.join.scan(/-(.)\[e|Kk(\S*)|le.l(..)e|#!(\S*)/) {|r| a << r.compact.first };puts "\n>#{a.join(%q/ /)}<\n\n"'



    Jamis Guest

  4. #4

    Default Re: Compression (besides Huffman) and Ruby

    On Wed, Jan 07, 2004 at 11:37:24AM +0900, Jamis Buck wrote: 

    I've got the bare bones of a Ruby implementation of adaptive arithmetic
    encoding, using the Rational module. But it's r-e-a-l-l-y s-l-o-w. :)

    -Mark
    Mark Guest

  5. #5

    Default Re: Compression (besides Huffman) and Ruby

    Mark J. Reed wrote:
     

    Heh. :) Well, the point of doing it in Ruby isn't to win races, I
    guess. It's more to make these things portable. I'm sure a zlib or
    bzip2 algorithm implemented in Ruby would be dog slow, too, but the
    convenience provided by having them available directly, on any platform,
    has to be taken into account.

    Is your code in the RAA, perchance? I'd be interested in taking a peek.

    --
    Jamis Buck
    byu.edu

    ruby -h | ruby -e 'a=[];readlines.join.scan(/-(.)\[e|Kk(\S*)|le.l(..)e|#!(\S*)/) {|r| a << r.compact.first };puts "\n>#{a.join(%q/ /)}<\n\n"'


    Jamis Guest

  6. #6

    Default Re: Compression (besides Huffman) and Ruby


    "Jamis Buck" <byu.edu> schrieb im Newsbeitrag
    news:byu.edu... 
    >
    > Heh. :) Well, the point of doing it in Ruby isn't to win races, I
    > guess. It's more to make these things portable. I'm sure a zlib or
    > bzip2 algorithm implemented in Ruby would be dog slow, too, but the
    > convenience provided by having them available directly, on any platform,
    > has to be taken into account.[/ref]

    But wouldn't it bettern then to implement it in C (or use an existing
    implementation)? I mean, C *is* portable.

    Cheers

    robert

    Robert Guest

  7. #7

    Default Re: Compression (besides Huffman) and Ruby

    On Wed, Jan 07, 2004 at 01:19:07PM +0900, Jamis Buck wrote: 

    This doesn't make any sense. When it's possible to have a ansi c compliant
    program (the ruby interpreter) which can be compiled on/for a (unix environment
    on a) foreign platform, it'll also be possible to compile zlib or whatsoever
    there. Probably it's more likely for such libs to be portable than ruby
    itself.

    I dunno where you people think ruby is running on (aside of your mind :).

    Regards,

    -Martin


    Martin Guest

  8. #8

    Default Re: Compression (besides Huffman) and Ruby

    Robert Klemme wrote:
     

    Very true. Generally, C *is* portable, and in general, such things
    should be (and are) written in C. However, C is *not* portable when a
    module is not available as a pre-compiled binary for a particular
    platform, and the user does not have access to a C compiler (or is not a
    programmer and is not confident compiling their own module). In such
    cases, the value of simply dropping the module into the Ruby path is
    quite high.

    Please don't misunderstand me. I am not saying that every C module ever
    written for Ruby should have been written *in* Ruby. That would be
    ludicrous. I'm simply saying that for some things (compression
    algorithms, for instance), there is some value in having them available
    *both* as a C modules, *and* as pure Ruby modules.

    Consider the case of self-extracting Ruby modules. You simply
    distribute the pure-ruby version of the compression library with your
    own code, and then anyone can extract your module, without having to go
    through some (potentially elaborate) installation scheme. Before I
    finally broke down and installed both wxWindows and FOX, I would avoid
    programs that used them because I've always found it to be a pain to
    install those systems (partly because, in addition to the systems
    themselves, you then had to install bindings for your language of
    choice). (Until I started using Gentoo, anyway, but that's beside the
    point.)

    Anyway, I hope that has clarified my position. I'm not trying to start
    a flame war on the merits of C modules versus pure Ruby modules (or even
    wxWindows and FOX--those were just examples from my own personal
    experience), so please don't be too vehement with any opposing opinions. :)

    --
    Jamis Buck
    byu.edu

    ruby -h | ruby -e 'a=[];readlines.join.scan(/-(.)\[e|Kk(\S*)|le.l(..)e|#!(\S*)/) {|r| a << r.compact.first };puts "\n>#{a.join(%q/ /)}<\n\n"'


    Jamis Guest

  9. #9

    Default Re: Compression (besides Huffman) and Ruby


    "Jamis Buck" <byu.edu> schrieb im Newsbeitrag
    news:byu.edu... 
    >
    > Very true. Generally, C *is* portable, and in general, such things
    > should be (and are) written in C. However, C is *not* portable when a
    > module is not available as a pre-compiled binary for a particular
    > platform, and the user does not have access to a C compiler (or is not a
    > programmer and is not confident compiling their own module). In such
    > cases, the value of simply dropping the module into the Ruby path is
    > quite high.[/ref]

    Another reasonable conclusion is to include a ruby module for such a basic
    thing as (de)compression into the std distribution. At least the cygwin
    distribution includes Zlib:

    irb(main):001:0> require 'zlib'
    => true
    irb(main):002:0> ObjectSpace.each_object( Class ) {|cl| p cl if
    /zip|zlib/i =~ cl.name }
    Zlib::GzipReader
    Zlib::GzipWriter
    Zlib::GzipFile::LengthError
    Zlib::GzipFile::CRCError
    Zlib::GzipFile::NoFooter
    Zlib::GzipFile::Error
    Zlib::GzipFile
    Zlib::Inflate
    Zlib::Deflate
    Zlib::ZStream
    Zlib::VersionError
    Zlib::BufError
    Zlib::MemError
    Zlib::StreamError
    Zlib::DataError
    Zlib::NeedDict
    Zlib::StreamEnd
    Zlib::Error
    => 358
    irb(main):003:0>

    Does that help? :-)
     
    :)

    No offense taken.

    Kind regards

    robert

    Robert Guest

  10. #10

    Default Re: Compression (besides Huffman) and Ruby

    Jamis Buck <byu.edu> writes:
     

    A good point. That's what some Smalltalks do, but in an even more useful
    way. Primitives are sometimes implemented in C (or at least in the VM
    itself, rather than in pure Smalltalk). If the primitive fails or does not
    exist, then alternate Smalltalk code can be run.

    For example, this is from Squeak's Object class:

    instVarAt: index
    "Primitive. Answer a fixed variable in an object. The numbering of the
    variables corresponds to the named instance variables. Fail if the
    index is not an Integer or is not the index of a fixed variable. Essential.
    See Object doentation whatIsAPrimitive."

    <primitive: 73>
    "Access beyond fixed variables."
    ^self basicAt: index - self class instSize

    The Squeak Object class has a method called "whatIsAPrimitive" whose entire
    purpose is to act as doentation. Here is a snippet from the comment:

    When the Smalltalk interpreter begins to execute a method which
    specifies a primitive response, it tries to perform the primitive
    action and to return a result. If the routine in the interpreter for
    this primitive is successful, it will return a value and the
    expressions in the method will not be evaluated. If the primitive
    routine is not successful, the primitive 'fails', and the Smalltalk
    expressions in the method are executed instead. These expressions are
    evaluated as though the primitive routine had not been called.

    Jim
    --
    Jim Menard, com, http://www.io.com/~jimm/
    "T-shirts are the computer industry's only persistent objects."
    -- Seen in R. L. Peskin's .sig
    Jim Guest

  11. #11

    Default Re: Compression (besides Huffman) and Ruby

    On Linux, at least, zlib appears to be compiled into "standard" Ruby. For
    the PragProg distribution for Windows, zlib is also present.

    -austin
    --
    austin ziegler * ca * Toronto, ON, Canada
    software designer * pragmatic programmer * 2004.01.07
    * 10.31.03




    Austin Guest

  12. #12

    Default Re: Compression (besides Huffman) and Ruby

    Austin Ziegler wrote:
     

    Thanks. It is true, that with 1.8.x and zlib, there shouldn't be a real
    necessity for implementing the zlib algorithm. I wasn't aware that it
    came standard with 1.8.

    However, (and I'm speaking more broadly than of just compression
    algorithms now) in general it is not possible to expect specific
    functionality to be distributed standard with the interpreter. If we
    wait for that, we'll wind up with a multi-megabyte runtime that becomes
    much more expensive (bandwidth-wise) to distribute. For such things, it
    *may* be desirable to have a pure-ruby implementation of the
    functionality that was desired (be it compression, gui toolkits, etc.,
    etc.).

    My two cents. I'll shut up now.

    --
    Jamis Buck
    byu.edu

    ruby -h | ruby -e 'a=[];readlines.join.scan(/-(.)\[e|Kk(\S*)|le.l(..)e|#!(\S*)/) {|r| a << r.compact.first };puts "\n>#{a.join(%q/ /)}<\n\n"'



    Jamis Guest

  13. #13

    Default Re: Compression (besides Huffman) and Ruby

    Hi!

    * Wesley J Landaker: 

    Wasn't aware that 1.8 has zlib binding. watashi wa baka desu :->

    Josef 'Jupp' SCHUGT
    --
    http://oss.erdfunkstelle.de/ruby/ - German comp.lang.ruby-FAQ
    http://rubyforge.org/users/jupp/ - Ruby projects at Rubyforge
    -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
    Germany 2004: To boldly spy where no GESTAPO / STASI has spied before


    Josef Guest

  14. #14

    Default Re: Compression (besides Huffman) and Ruby

    Josef 'Jupp' SCHUGT wrote: 
    >
    >
    > Wasn't aware that 1.8 has zlib binding. watashi wa baka desu :->[/ref]

    ^^^^^^^^^^^^^^^^^^^^
    Not if you know how to say that ;)



    Joel Guest

Similar Threads

  1. Algorithm::Huffman
    By Susan in forum PERL Modules
    Replies: 0
    Last Post: December 12th, 01:44 AM
  2. Replies: 0
    Last Post: November 1st, 05:21 PM
  3. Replies: 1
    Last Post: October 29th, 07:52 PM
  4. [ANN] ruby-freedb, ruby-serialport, ruby-mp3info moved to Rubyforge
    By guillaume.pierronnet@ratp.fr in forum Ruby
    Replies: 0
    Last Post: August 31st, 11:57 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139