Run-length encoding: Difference between revisions
Content added Content deleted
mNo edit summary |
mNo edit summary |
||
Line 2,835: | Line 2,835: | ||
This gives RLE encoding for strings and RLE decoding for strings and arrays, e.g., for [[Conway's_Game_of_Life|Conway's Game of Life]] |
This gives RLE encoding for strings and RLE decoding for strings and arrays, e.g., for [[Conway's_Game_of_Life|Conway's Game of Life]] |
||
<syntaxhighlight lang=FutureBasic> |
<syntaxhighlight lang=FutureBasic> |
||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
Dynamic a(1, 1) as Short // Self-fulfilling array |
|||
local fn encode( string as CFStringRef) as CFStringRef |
local fn encode( string as CFStringRef) as CFStringRef |
||
CFStringRef ch, s, t |
CFStringRef ch, s, t |
||
Short i, rl |
Short i, rl |
||
s = @"" |
s = @"" // Initalize the output string |
||
for i = 0 to len( string ) - 1 |
for i = 0 to len( string ) - 1 // Encode string char by char |
||
ch = mid( string, i, 1) |
ch = mid( string, i, 1) // Read character at index |
||
rl = 1 |
rl = 1 // Start run-length counter |
||
while fn StringIsEqual( mid( string, i + rl, 1), ch ) |
while fn StringIsEqual( mid( string, i + rl, 1), ch ) |
||
rl ++ |
rl ++ // Same char, so increase counter |
||
wend |
wend |
||
if rl == 1 then t = @"" else t = fn StringWithFormat( @"%d", rl ) // |
if rl == 1 then t = @"" else t = fn StringWithFormat( @"%d", rl ) // Counter as string |
||
t = fn |
t = fn StringByAppendingString( t, ch ) // Add character |
||
s = fn StringByAppendingString( s, t ) // Add to encoded string |
|||
i += rl - 1 |
|||
s = fn StringByAppendingString( s, t ) // Add to output string |
|||
i += rl - 1 // Move counter |
|||
next |
next |
||
print s |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
print s |
|||
end fn |
|||
Line 2,921: | Line 2,924: | ||
001 |
001 |
||
111 |
111 |
||
</pre> |
</pre> |