Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/omega-mv/blob/main/si/rosetta/tasks/dutch-national-flag-problem/dutch-national-flag-problem.om # omega-mv Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance. Brought by drakestapleton Sign in to star 1 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `omega-mv / si / rosetta / tasks / dutch-national-flag-problem / dutch-national-flag-problem.om` 131 lines ยท 2368 bytes Raw Blame History 1 note Rosetta Code task "Dutch national flag problem": https://rosettacode.org/wiki/Dutch_national_flag_problem 2 note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 3 om2 4 text nl "\n" 5 text sp " " 6 text minus "-" 7 text tred "red" 8 text twhite "white" 9 text tblue "blue" 10 text tok "in flag order: " 11 text tyes "yes\n" 12 text tno "no\n" 13 global vals long 16 14 global seed ulong 1 15 note emit writes every byte of a to stdout; a short write is retried, an error exits 1 16 fn emit a view uchar * to void 17 o = ulong 0 18 while lt o len a bound 4096 19 k = write 1 a o sub len a o 20 if le k 0 21 exit 1 22 end 23 o = add o ulong k 24 end 25 end 26 note emitu writes n in decimal; 20 digits hold every ulong 27 fn emitu n ulong to void 28 d = new uchar 20 0 29 i = ulong 20 30 v = n 31 while true bound 20 32 i = sub i 1 33 put d i uchar add rem v 10 48 34 v = div v 10 35 if eq v 0 36 break 37 end 38 end 39 emit part d i sub 20 i 40 end 41 note rnd is Knuth's MMIX linear congruential generator, x = x*`6364136223846793005` + `1442695040888963407` 42 note mod 2^64 (ulong arithmetic wraps), starting from seed 0; it returns the top 31 bits, x >> 33 43 fn rnd to ulong 44 x = add mul get seed 0 `6364136223846793005` `1442695040888963407` 45 put seed 0 x 46 ret shr x 33 47 end 48 fn ball c long to void 49 if eq c 0 50 emit tred 51 end 52 if eq c 1 53 emit twhite 54 end 55 if eq c 2 56 emit tblue 57 end 58 end 59 fn show n ulong to void 60 for i ulong n 61 if gt i 0 62 emit sp 63 end 64 ball get vals i 65 end 66 emit nl 67 end 68 fn inorder n ulong to bool 69 for i ulong sub n 1 70 if gt get vals i get vals add i 1 71 ret false 72 end 73 end 74 ret true 75 end 76 fn swap i ulong j ulong to void 77 t = get vals i 78 put vals i get vals j 79 put vals j t 80 end 81 note Dijkstra's three-way partition: [0,lo) red, [lo,mid) white, (hi,n) blue; every pass moves mid up 82 note or hi down, so n passes suffice 83 fn flag n ulong to void 84 lo = ulong 0 85 mid = ulong 0 86 hi = n 87 while lt mid hi bound 16 88 c = get vals mid 89 if eq c 0 90 swap lo mid 91 lo = add lo 1 92 mid = add mid 1 93 else 94 if eq c 1 95 mid = add mid 1 96 else 97 hi = sub hi 1 98 swap mid hi 99 end 100 end 101 end 102 end 103 note draws 12 random balls (0 red, 1 white, 2 blue), drawing again while they happen to be in order; 104 note with this generator the first draw is already out of order, and 100 draws bound it 105 fn main to int 106 n = ulong 12 107 while true bound 100 108 for i ulong n 109 put vals i long rem rnd 3 110 end 111 if not inorder n 112 break 113 end 114 end 115 show n 116 emit tok 117 if inorder n 118 emit tyes 119 else 120 emit tno 121 end 122 flag n 123 show n 124 emit tok 125 if inorder n 126 emit tyes 127 else 128 emit tno 129 end 130 ret 0 131 end