Author Topic: Run-Length Encoding for Strings  (Read 9190 times)

0 Members and 2 Guests are viewing this topic.

Offline meishe91

  • Super Ninja
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2946
  • Rating: +115/-11
    • View Profile
    • DeviantArt
Run-Length Encoding for Strings
« on: August 25, 2010, 04:47:33 am »
So before I do anything more on my recent project I told myself I would work on making the moving engine, map display, and that stuff as good as I could before I continued on. So I decided to mess with RLE compression and came up with a program to compress a string. Just decided that I would share it with you guys for learning purposes or if someone wants to use it or something. (Compressed mine by about 800 bytes.)

Code: (RLE Compression) [Select]
"_?Str2
Str1+Ans?Str1
{0,1?L1
DelVar BDelVar A"sub(Str1,B+1,1?u
Repeat u="_
Repeat u?sub(Str1,B+A+1,1
A+1?A
End
{0,A?L2
LinReg(ax+b) Y1
Equ?String(Y1,Str3
Str2+sub(Str3,1,length(Str3)-3)+u?Str2
B+A?B
DelVar AEnd
DelVar Str3DelVar L1DelVar L2DelVar Y1DelVar usub(Str1,1,length(Str1)-1?Str1
sub(Str2,2,length(Str2)-1?Str2

I know there are probably optimizations that could be made to that I just haven't gone over it really to look for any. I kinda just made it so it'd work and then posted it. If someone wants to optimize, go for it :)

Note:
The only thing that needs to be modified in this one is that the space that gets placed at the end needs to just be any token that IS NOT used in the string you are compressing. So if you string being compressed uses spaces then just replace the space with, for example, a question mark. After that you need to replace the space in the fifth row (where your checking to see if the string part equals something) with that same token as in the first row. Hopefully that makes sense.

As a side note to this I realized this didn't work all that well for maps that were all numbers so I made another program that converts those numbers into some other token of your choice. This one will need some adjusting depending on what your string contains but it is fairly easy. If anyone needs it explained I will.

Code: (Different Tokens) [Select]
"_"+Str1?Str2
For(A,2,1+length(Str2
sub(Str2,1,A-1)+sub("SATRWHM",expr(sub(Str2,A,1))+1,1)+sub(Str2,A+1,length(Str2)-A?Str2
End
sub(Str2,2,length(Str2)-1?Str2

Like I said that one needs to be modified depending on your string and such. The one that is posted is specific to how I wanted my string to be outputted and what my original string was.

So, hopefully someone finds this useful or something. Any questions just ask :)
« Last Edit: August 26, 2010, 06:20:01 pm by meishe91 »
Spoiler For Spoiler:



For the 51st time, that is not my card! (Magic Joke)

Offline DJ Omnimaga

  • Clacualters are teh gr33t
  • CoT Emeritus
  • LV15 Omnimagician (Next: --)
  • *
  • Posts: 55942
  • Rating: +3154/-232
  • CodeWalrus founder & retired Omnimaga founder
    • View Profile
    • Dream of Omnimaga Music
Re: Run-Length Encoding for Strings
« Reply #1 on: August 25, 2010, 02:08:56 pm »
Hmm interesting. There isn't a lot of compression stuff documented in BASIC. I wonder how fast is yours?
Now active at https://discord.gg/cuZcfcF (CodeWalrus server)

Offline meishe91

  • Super Ninja
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2946
  • Rating: +115/-11
    • View Profile
    • DeviantArt
Re: Run-Length Encoding for Strings
« Reply #2 on: August 25, 2010, 02:16:29 pm »
Well it runs my 1886 byte string in a little under four minutes and takes off 800 bytes but it'll vary from string to string. So it isn't terribly fast but I don't think it's that slow either, but I don't know really. I'm sure there is a better method or optimizations that could be made.
« Last Edit: August 25, 2010, 02:17:16 pm by meishe91 »
Spoiler For Spoiler:



For the 51st time, that is not my card! (Magic Joke)

Offline ztrumpet

  • The Rarely Active One
  • CoT Emeritus
  • LV13 Extreme Addict (Next: 9001)
  • *
  • Posts: 5712
  • Rating: +364/-4
  • If you see this, send me a PM. Just for fun.
    • View Profile
Re: Run-Length Encoding for Strings
« Reply #3 on: August 25, 2010, 02:20:44 pm »
How long does it take to decompress them? :)

Offline meishe91

  • Super Ninja
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2946
  • Rating: +115/-11
    • View Profile
    • DeviantArt
Re: Run-Length Encoding for Strings
« Reply #4 on: August 25, 2010, 02:22:04 pm »
I don't know...haven't made a decompressor yet :P
Spoiler For Spoiler:



For the 51st time, that is not my card! (Magic Joke)

Offline ztrumpet

  • The Rarely Active One
  • CoT Emeritus
  • LV13 Extreme Addict (Next: 9001)
  • *
  • Posts: 5712
  • Rating: +364/-4
  • If you see this, send me a PM. Just for fun.
    • View Profile
Re: Run-Length Encoding for Strings
« Reply #5 on: August 25, 2010, 02:22:37 pm »
lol. :P  Good luck on the decompressor.  I hope it's fast. ;D

Offline meishe91

  • Super Ninja
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2946
  • Rating: +115/-11
    • View Profile
    • DeviantArt
Re: Run-Length Encoding for Strings
« Reply #6 on: August 25, 2010, 02:24:27 pm »
Haha thanks. I'll post it once I make it. I'll start on it once I get back from Qdoba :P
Spoiler For Spoiler:



For the 51st time, that is not my card! (Magic Joke)

Offline DJ Omnimaga

  • Clacualters are teh gr33t
  • CoT Emeritus
  • LV15 Omnimagician (Next: --)
  • *
  • Posts: 55942
  • Rating: +3154/-232
  • CodeWalrus founder & retired Omnimaga founder
    • View Profile
    • Dream of Omnimaga Music
Re: Run-Length Encoding for Strings
« Reply #7 on: August 25, 2010, 04:29:33 pm »
I can't wait to see it in action ^^

Now active at https://discord.gg/cuZcfcF (CodeWalrus server)

Offline meishe91

  • Super Ninja
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2946
  • Rating: +115/-11
    • View Profile
    • DeviantArt
Re: Run-Length Encoding for Strings
« Reply #8 on: August 25, 2010, 05:18:31 pm »
In a game or just in general? Because just in general it's not all that exciting to wait for a string to compress :P
Spoiler For Spoiler:



For the 51st time, that is not my card! (Magic Joke)

Offline nemo

  • LV9 Veteran (Next: 1337)
  • *********
  • Posts: 1203
  • Rating: +95/-11
    • View Profile
Re: Run-Length Encoding for Strings
« Reply #9 on: August 25, 2010, 06:24:11 pm »
looks nice (:

just thought i'd comment that the Equ>String() command is entirely useless.
Code: [Select]
Equâ–ºString(Y1,Str4
is one byte larger than
Code: [Select]
Y1->Str1

and they're the exact same speed. they actually use the same routines. equ>string is completely useless.


Offline meishe91

  • Super Ninja
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2946
  • Rating: +115/-11
    • View Profile
    • DeviantArt
Re: Run-Length Encoding for Strings
« Reply #10 on: August 25, 2010, 06:26:45 pm »
When I try to do that though I get a ERR:DATA TYPE ???

By the way, I was just telling Z that I knew you'd pop in sometime and find something :P Also, thanks :)
« Last Edit: August 25, 2010, 06:28:58 pm by meishe91 »
Spoiler For Spoiler:



For the 51st time, that is not my card! (Magic Joke)

Offline Builderboy

  • Physics Guru
  • CoT Emeritus
  • LV13 Extreme Addict (Next: 9001)
  • *
  • Posts: 5673
  • Rating: +613/-9
  • Would you kindly?
    • View Profile
Re: Run-Length Encoding for Strings
« Reply #11 on: August 25, 2010, 06:30:26 pm »
I believe its the String>Equ( command that is the useless one.  But for some reason it doesn't go both ways.  You need Equ>String() to transfer an equation to a string, but you dont need String>Equ() to transfer a String to an equation o.O

Offline meishe91

  • Super Ninja
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2946
  • Rating: +115/-11
    • View Profile
    • DeviantArt
Re: Run-Length Encoding for Strings
« Reply #12 on: August 25, 2010, 06:32:22 pm »
I believe its the String>Equ( command that is the useless one.  But for some reason it doesn't go both ways.  You need Equ>String() to transfer an equation to a string, but you dont need String>Equ() to transfer a String to an equation o.O

Ya, Builder, I think you're right. That is yet another thing that TI failed upon :P Thanks for clearing that up :)
Spoiler For Spoiler:



For the 51st time, that is not my card! (Magic Joke)

Offline nemo

  • LV9 Veteran (Next: 1337)
  • *********
  • Posts: 1203
  • Rating: +95/-11
    • View Profile
Re: Run-Length Encoding for Strings
« Reply #13 on: August 25, 2010, 06:33:17 pm »
oops. my bad, i thought there was only one of those commands.


Offline meishe91

  • Super Ninja
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2946
  • Rating: +115/-11
    • View Profile
    • DeviantArt
Re: Run-Length Encoding for Strings
« Reply #14 on: August 25, 2010, 06:35:06 pm »
Nah, there is both of those, for what ever reason that we will never know. Thanks though :)
Spoiler For Spoiler:



For the 51st time, that is not my card! (Magic Joke)