Git Product home page Git Product logo

run_length's Introduction

Run Length

A simple run length encoder built in Ruby for encoding bit vector strings and "compressing" files via this method. The run length encoder will reduce the length of a bit vector. If the file is more heavily composed of one value than another (0s or 1s), the encoding will compress fairly successfully. If not, it's a mediocre compression techique. You can check this out using the performance_test function in run_length.rb, which lets you control "distribution" of bit values in a bit vector, and will give handy statistics for how effective the compression was. I have yet to make an interface for this function, but generally the percent compression effectiveness was proportional to the "distribution factor" of 1s and 0s, as I called it. Check out the code for details.

I've also included a command line interface for "compressing" text files. "Compressing" I put in quotes because it actually converts every ASCII character in a file to binary, then converts this binary string to a run lenth encoded string, so in the end the file isn't really any shorter than the original, and in fact is almost certainly going to be longer. It will encode it though, which is cool and handy if you want to keep the contents of a file secret I guess.

The code overall is small and simple, just something I threw together as an experiment one afternoon. Mostly I was curious about how well such a simple encoder could compress bit vectors, encoding ASCII files was an afterthought that I clearly didn't think through all the way.

How the encoder works

The bit vector "0001110011" will be converted to "03322"

The first bit of the encoded string is the first bit of original string, and the next numbers are the number of bits until a switch. So "03322" says:

  • Start with a 0
  • The next 3 bits are 0
  • The next 3 are 1
  • The next 2 are 0
  • The 2 after that are 1

Pretty simple. The code for encoding and decoding is correspondingly tiny (but I bet it could be tinier).

Using the command line interface

The interface for encoding text files is structured as follows:

$ ruby file_compress.rb [compress|decompress] inpath outpath

Example, to compress/encode the file test.txt and put the compressed copy at test_compressed.txt, run:

$ ruby file_compress.rb compress test.txt test_compressed.txt

To decode/decompress test_compressed.txt run:

$ ruby file_compress.rb decompress test_compressed.txt test_decompressed.txt

There's no real error checking on this interface, so be careful about modifying files. The interface is still very simple and limited, but it's enough to quickly fire up and encode some files with.

Again, I say "compressed" as the operation, but in reality the compressed file will in all likelyhood be encoded but bigger in terms of bytes than the original. Still, I thought it was a fun little tool to play around with. I might look into actual compression techniques in the future.

run_length's People

Contributors

akowalz avatar

Stargazers

Adam Hallett avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.