Run-length encoding: Difference between revisions

m
no edit summary
mNo edit summary
Line 2,833:
 
=={{header|FutureBasic}}==
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>
local fn decode( string as CFStringRef ) as CFStringRef
Line 2,873 ⟶ 2,874:
 
 
local fn decode2D( string as CFStringRef ) // For Game of Life
print fn encode( @"WWWWWWWWWWWWBWWWWWWWWWWWWBBBWWWWWWWWWWWWWWWWWWWWWWWWBWWWWWWWWWWWWWW" )
CFStringRef ch
print fn encode( fn decode( @"12WB12W3B24W1B14W" ) )
Short i, j, rl, f // Decoded char
Short v = 0, w = 0, x = 0, y = 0 // Temp width, max width, array coordinates
for i = 0 to len( string ) - 2 // Final char is !
ch = mid( string, i, 1 )
if intval( ch ) == 0 // Not o, b, $
rl = 1
else
rl = intval( mid( string, i ) )
i += fix( log10( rl ) + 1 )
ch = mid( string, i, 1 )
end if
select ch // Decode character
case @"$" : f = -1 // new line
case @"b" : f = 0 // dead
case @"o" : f = 1 // live
case else : // Ignore
end select
for j = 1 to rl // Fill array with run of chars
if f = -1
x = 0 : y ++ : v = 0 // New line
else
a(x, y) = f : x ++ : v ++ : if v > w then w = v
end if
next // run
next // character
for j = 0 to y : for i = 0 to w - 1
print a(i, j);
next : print : next
end fn
 
fn decode( @"12W1B12W3B24W1B14W" ) // Assignment
print fn encode( @"WWWWWWWWWWWWBWWWWWWWWWWWWBBBWWWWWWWWWWWWWWWWWWWWWWWWBWWWWWWWWWWWWWW" )
fn decode2D( @"bo$2bo$3o!" ) // Glider
 
handleevents // Join Mac event loop
Line 2,882 ⟶ 2,916:
<pre>
 
WWWWWWWWWWWWBWWWWWWWWWWWWBBBWWWWWWWWWWWWWWWWWWWWWWWWBWWWWWWWWWWWWWW
12WB12W3B24WB14W
011
12WB12W3B24WB14W
001
111
 
 
 
</pre>
60

edits