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/tasks/ordered-partitions/ordered-partitions.om # 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 / tasks / ordered-partitions / ordered-partitions.om` 115 lines ยท 2089 bytes Raw Blame History 1 note Rosetta Code task "Ordered partitions": https://rosettacode.org/wiki/Ordered_partitions 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 tlp "(" 8 text trp ")" 9 text tlb "{" 10 text trb "}" 11 text tcomma ", " 12 text tpart "partitions" 13 global size ulong 4 14 global owner ulong 8 15 global nb ulong 1 16 global nel 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 owner[i] is the block holding element i+1 (nb = unassigned); blocks are filled in order, each with 44 note its elements chosen as a lexicographic combination of what is left, so the output is in order 45 fn show to void 46 emit tlp 47 for b ulong get nb 0 48 if gt b 0 49 emit tcomma 50 end 51 emit tlb 52 first = true 53 for i ulong get nel 0 54 if eq get owner i b 55 if not first 56 emit tcomma 57 end 58 emitu add i 1 59 first = false 60 end 61 end 62 emit trb 63 end 64 emit trp 65 emit nl 66 end 67 note fill starts block b, or prints the partition once every block is full 68 fn fill b ulong to void 69 if eq b get nb 0 70 show 71 ret 72 end 73 choose b get size b 0 74 end 75 note choose gives block b `need` more elements, each larger than the last one chosen (at index >= from) 76 fn choose b ulong need ulong from ulong to void 77 if eq need 0 78 fill add b 1 79 ret 80 end 81 n = get nel 0 82 for x ulong sub n from 83 i = add from x 84 if eq get owner i get nb 0 85 put owner i b 86 choose b sub need 1 add i 1 87 put owner i get nb 0 88 end 89 end 90 end 91 fn run s0 ulong s1 ulong s2 ulong to void 92 put nb 0 3 93 put size 0 s0 94 put size 1 s1 95 put size 2 s2 96 put nel 0 add add s0 s1 s2 97 for i ulong get nel 0 98 put owner i 3 99 end 100 emit tpart 101 emit tlp 102 emitu s0 103 emit tcomma 104 emitu s1 105 emit tcomma 106 emitu s2 107 emit trp 108 emit nl 109 fill 0 110 end 111 fn main to int 112 run 2 0 2 113 run 1 2 1 114 ret 0 115 end