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-rosetta/blob/main/drafts/rosettacode/dutch-national-flag-problem.wiki
# omega-rosetta
aien-dev/omega-rosetta
Brought by drakestapleton
Sign in to star 0 stars 0 watching
About Files History Releases Proposed changes Issues For developers
branch `main` Branches Tags History
`omega-rosetta / drafts / rosettacode / dutch-national-flag-problem.wiki`
141 lines ยท 2629 bytes Raw Blame History
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 (emit and friends) are part of the program.
4
5 om2
6 text nl "\n"
7 text sp " "
8 text minus "-"
9 text tred "red"
10 text twhite "white"
11 text tblue "blue"
12 text tok "in flag order: "
13 text tyes "yes\n"
14 text tno "no\n"
15 global vals long 16
16 global seed ulong 1
17 note emit writes every byte of a to stdout; a short write is retried, an error exits 1
18 fn emit a view uchar * to void
19 o = ulong 0
20 while lt o len a bound 4096
21 k = write 1 a o sub len a o
22 if le k 0
23 exit 1
24 end
25 o = add o ulong k
26 end
27 end
28 note emitu writes n in decimal; 20 digits hold every ulong
29 fn emitu n ulong to void
30 d = new uchar 20 0
31 i = ulong 20
32 v = n
33 while true bound 20
34 i = sub i 1
35 put d i uchar add rem v 10 48
36 v = div v 10
37 if eq v 0
38 break
39 end
40 end
41 emit part d i sub 20 i
42 end
43 note rnd is Knuth's MMIX linear congruential generator, x = x*`6364136223846793005` + `1442695040888963407`
44 note mod 2^64 (ulong arithmetic wraps), starting from seed 0; it returns the top 31 bits, x >> 33
45 fn rnd to ulong
46 x = add mul get seed 0 `6364136223846793005` `1442695040888963407`
47 put seed 0 x
48 ret shr x 33
49 end
50 fn ball c long to void
51 if eq c 0
52 emit tred
53 end
54 if eq c 1
55 emit twhite
56 end
57 if eq c 2
58 emit tblue
59 end
60 end
61 fn show n ulong to void
62 for i ulong n
63 if gt i 0
64 emit sp
65 end
66 ball get vals i
67 end
68 emit nl
69 end
70 fn inorder n ulong to bool
71 for i ulong sub n 1
72 if gt get vals i get vals add i 1
73 ret false
74 end
75 end
76 ret true
77 end
78 fn swap i ulong j ulong to void
79 t = get vals i
80 put vals i get vals j
81 put vals j t
82 end
83 note Dijkstra's three-way partition: [0,lo) red, [lo,mid) white, (hi,n) blue; every pass moves mid up
84 note or hi down, so n passes suffice
85 fn flag n ulong to void
86 lo = ulong 0
87 mid = ulong 0
88 hi = n
89 while lt mid hi bound 16
90 c = get vals mid
91 if eq c 0
92 swap lo mid
93 lo = add lo 1
94 mid = add mid 1
95 else
96 if eq c 1
97 mid = add mid 1
98 else
99 hi = sub hi 1
100 swap mid hi
101 end
102 end
103 end
104 end
105 note draws 12 random balls (0 red, 1 white, 2 blue), drawing again while they happen to be in order;
106 note with this generator the first draw is already out of order, and 100 draws bound it
107 fn main to int
108 n = ulong 12
109 while true bound 100
110 for i ulong n
111 put vals i long rem rnd 3
112 end
113 if not inorder n
114 break
115 end
116 end
117 show n
118 emit tok
119 if inorder n
120 emit tyes
121 else
122 emit tno
123 end
124 flag n
125 show n
126 emit tok
127 if inorder n
128 emit tyes
129 else
130 emit tno
131 end
132 ret 0
133 end
134
135 {{out}}
136
137 blue red white white blue blue blue blue red red white red
138 in flag order: no
139 red red red red white white white blue blue blue blue blue
140 in flag order: yes
141