Run-length encoding: Difference between revisions
Content added Content deleted
mNo edit summary |
mNo edit summary |
||
Line 2,842: | Line 2,842: | ||
CFStringRef ch, s, t |
CFStringRef ch, s, t |
||
Short i, rl |
Short i, rl |
||
s = @"" // Initalize the output string |
s = @"" // Initalize the output string |
||
for i = 0 to len( string ) - 1 // Encode string char by char |
for i = 0 to len( string ) - 1 // Encode string char by char |
||
ch = mid( string, i, 1) // Read character at index |
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 ) // Counter as string |
if rl == 1 then t = @"" else t = fn StringWithFormat( @"%d", rl ) // Counter as string, don't encode 1's |
||
t = fn StringByAppendingString( t, ch ) // Add character |
t = fn StringByAppendingString( t, ch ) // Add character |
||
s = fn StringByAppendingString( s, t ) // Add to encoded string |
s = fn StringByAppendingString( s, t ) // Add to already encoded string |
||
i += rl - 1 // Move index |
|||
i += rl - 1 |
|||
next |
next |
||
print s |
print s |
||
Line 2,859: | Line 2,859: | ||
local fn decode( string as CFStringRef ) |
local fn decode( string as CFStringRef ) |
||
CFStringRef ch, s, t // character, |
CFStringRef ch, s, t // character, outputstring, temporary string |
||
Short i, rl // index, run length |
Short i, rl // index, run length |
||
s = @"" // Initalize the output string |
s = @"" // Initalize the output string |
||
for i = 0 to len( string ) - 1 // Decode input string char by char |
for i = 0 to len( string ) - 1 // Decode input string char by char |
||
ch = mid( string, i, 1 ) // Read character at index |
ch = mid( string, i, 1 ) // Read character at index |
||
if intval( ch ) == 0 // Not a digit |
if intval( ch ) == 0 // Not a digit |
||
rl = |
rl = |
||
else |
else |
||
rl = intval( mid( string, i ) ) // Read run-length |
rl = intval( mid( string, i ) ) // Read run-length |
||
i += fix( log10( rl ) + 1 ) // Move index past digits |
i += fix( log10( rl ) + 1 ) // Move index past digits |
||
ch = mid( string, i, 1 ) // Read character after run length |
ch = mid( string, i, 1 ) // Read character after run length |
||
end if |
end if |
||
t = fn StringByPaddingToLength( ch, rl, ch, 0 ) // Assemble string |
t = fn StringByPaddingToLength( ch, rl, ch, 0 ) // Assemble string |
||
s = fn StringByAppendingString( s, t ) // Add to decoded string |
s = fn StringByAppendingString( s, t ) // Add to decoded string |
||
next |
next |
||
print s |
print s |
||
end fn |
end fn |
||
local fn decode2D( string as CFStringRef ) // For Game of Life |
local fn decode2D( string as CFStringRef ) // For Game of Life |
||
CFStringRef ch |
CFStringRef ch |
||
Short i, j, rl, f // Decoded char |
Short i, j, rl, f // Decoded char |
||
Short v = 0, w = 0, x = 0, y = 0 // Temp width, max width, array coordinates |
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 ! |
for i = 0 to len( string ) - 2 // Final char is always ! |
||
ch = mid( string, i, 1 ) |
ch = mid( string, i, 1 ) |
||
if intval( ch ) == 0 |
if intval( ch ) == 0 |
||
Line 2,891: | Line 2,891: | ||
ch = mid( string, i, 1 ) |
ch = mid( string, i, 1 ) |
||
end if |
end if |
||
select ch // Decode character as: |
select ch // Decode character as: |
||
case @"$" : f = -1 // - new line |
case @"$" : f = -1 // - new line |
||
case @"b" : f = 0 // - dead |
case @"b" : f = 0 // - dead |
||
case @"o" : f = 1 // - live |
case @"o" : f = 1 // - live |
||
case else : // Ignore |
case else : // Ignore |
||
end select |
end select |
||
for j = 1 to rl // Fill array with run of chars |
for j = 1 to rl // Fill array with run of chars |
||
if f = -1 |
if f = -1 |
||
x = 0 : y ++ : v = 0 // New line |
x = 0 : y ++ : v = 0 // New line |
||
Line 2,904: | Line 2,904: | ||
x ++ : v ++ : if v > w then w = v |
x ++ : v ++ : if v > w then w = v |
||
end if |
end if |
||
next |
next |
||
next |
next |
||
for j = 0 to y : for i = 0 to w - 1 |
for j = 0 to y : for i = 0 to w - 1 |
||
print a(i, j); |
print a(i, j); |