omega-rosetta
aien-dev/omega-rosetta
Brought by drakestapleton
omega-rosetta / drafts / rosettacode / run-length-encoding.wiki
| 1 | =={{header|Omegese}}== |
| 2 | {{works with|omc|sha256 2c824805, aien-dev/omega-mv e5b32567, Linux AArch64}} |
| 3 | Omega has no imports, so the small output helpers (<code>emit</code> and friends) are part of the program. |
| 4 | <syntaxhighlight lang="text"> |
| 5 | om2 |
| 6 | text nl "\n" |
| 7 | text msg "WWWWWWWWWWWWBWWWWWWWWWWWWBBBWWWWWWWWWWWWWWWWWWWWWWWWBWWWWWWWWWWWWWW" |
| 8 | note emit writes every byte of a to stdout; a short write is retried, an error exits 1 |
| 9 | fn emit a view uchar * to void |
| 10 | o = ulong 0 |
| 11 | while lt o len a bound 4096 |
| 12 | k = write 1 a o sub len a o |
| 13 | if le k 0 |
| 14 | exit 1 |
| 15 | end |
| 16 | o = add o ulong k |
| 17 | end |
| 18 | end |
| 19 | note putnum writes n in decimal into r at p and returns the offset after it |
| 20 | fn putnum r edit uchar * p ulong n ulong to ulong |
| 21 | d = new uchar 20 0 |
| 22 | i = ulong 20 |
| 23 | v = n |
| 24 | while true bound 20 |
| 25 | i = sub i 1 |
| 26 | put d i uchar add rem v 10 48 |
| 27 | v = div v 10 |
| 28 | if eq v 0 |
| 29 | break |
| 30 | end |
| 31 | end |
| 32 | q = p |
| 33 | for j ulong sub 20 i |
| 34 | put r q get d add i j |
| 35 | q = add q 1 |
| 36 | end |
| 37 | ret q |
| 38 | end |
| 39 | note encode writes count then byte for every run of equal bytes; returns the length |
| 40 | fn encode a view uchar * r edit uchar * to ulong |
| 41 | p = ulong 0 |
| 42 | i = ulong 0 |
| 43 | while lt i len a bound 4096 |
| 44 | c = get a i |
| 45 | j = i |
| 46 | while lt j len a bound 4096 |
| 47 | if ne get a j c |
| 48 | break |
| 49 | end |
| 50 | j = add j 1 |
| 51 | end |
| 52 | p = putnum r p sub j i |
| 53 | put r p c |
| 54 | p = add p 1 |
| 55 | i = j |
| 56 | end |
| 57 | ret p |
| 58 | end |
| 59 | note decode reads a decimal count then one byte, and repeats the byte; returns the length |
| 60 | fn decode a view uchar * r edit uchar * to ulong |
| 61 | p = ulong 0 |
| 62 | v = ulong 0 |
| 63 | for i ulong len a |
| 64 | c = get a i |
| 65 | if and ge c 48 le c 57 |
| 66 | v = add mul v 10 ulong sub c 48 |
| 67 | else |
| 68 | for k ulong v |
| 69 | put r p c |
| 70 | p = add p 1 |
| 71 | end |
| 72 | v = 0 |
| 73 | end |
| 74 | end |
| 75 | ret p |
| 76 | end |
| 77 | fn main to int |
| 78 | e = new uchar 256 0 |
| 79 | d = new uchar 256 0 |
| 80 | n = encode msg e |
| 81 | emit part e 0 n |
| 82 | emit nl |
| 83 | m = decode part e 0 n d |
| 84 | emit part d 0 m |
| 85 | emit nl |
| 86 | ret 0 |
| 87 | end |
| 88 | </syntaxhighlight> |
| 89 | {{out}} |
| 90 | <pre> |
| 91 | 12W1B12W3B24W1B14W |
| 92 | WWWWWWWWWWWWBWWWWWWWWWWWWBBBWWWWWWWWWWWWWWWWWWWWWWWWBWWWWWWWWWWWWWW |
| 93 | </pre> |