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>
local fn decode( string as CFStringRef ) as CFStringRef
CFStringRef ch, s, t // character, output string, temporary string
Short i, rl // index, run-length
s = @"" // Initialize the output string
for i = 0 to len( string ) - 1 // Decode input string char by char
ch = mid( string, i, 1 ) // Read character at index
if intval( ch ) == 0 // Not a digit
rl = 1
else
rl = intval( mid( string, i ) ) // Read run-length counter
i += fix( log10( rl ) + 1 ) // Move index past digits
ch = mid( string, i, 1 ) // Read character after run-length
end if
t = fn StringByPaddingToLength( ch, rl, ch, 0 ) // Assemble string of chars
s = fn StringByAppendingString( s, t ) // Add to output string
next // character
end fn = s


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 // Start run-length counter
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 ++ // Same char, so increase counter
rl ++ // Same char, so increase counter
wend
wend
if rl == 1 then t = @"" else t = fn StringWithFormat( @"%d", rl ) // Ignore count of 1
if rl == 1 then t = @"" else t = fn StringWithFormat( @"%d", rl ) // Counter as string
t = fn StringWithFormat( @"%d", rl ) // Counter as string
t = fn StringByAppendingString( t, ch ) // Add character
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
end fn = s
print s
end fn


local fn decode( string as CFStringRef )
CFStringRef ch, s, t // character, ouputstring, temporary string
Short i, rl // index, run length
s = @"" // Initalize the output string
for i = 0 to len( string ) - 1 // Decode input string char by char
ch = mid( string, i, 1 ) // Read character at index
if intval( ch ) == 0 // Not a digit
rl = 1
else
rl = intval( mid( string, i ) ) // Read run-length counter
i += fix( log10( rl ) + 1 ) // Move index past digits
ch = mid( string, i, 1 ) // Read character after run length
end if
t = fn StringByPaddingToLength( ch, rl, ch, 0 ) // Assemble string
s = fn StringByAppendingString( s, t ) // Add to decoded string
next // character
print s
end fn




Line 2,921: Line 2,924:
001
001
111
111




</pre>
</pre>