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/anagrams/anagrams.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 / anagrams / anagrams.om` 121 lines ยท 2595 bytes Raw Blame History 1 note Rosetta Code task "Anagrams": https://rosettacode.org/wiki/Anagrams 2 note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 3 om2 4 text nl "\n" 5 text sp " " 6 note the word list (Rosetta Code reads unixdict.txt; this is a fixed sample of it), newline-terminated 7 text dict "abel\nable\nbale\nbela\nelba\nalger\nglare\nlager\nlarge\nregal\nangel\nangle\ngalen\nglean\nlange\ncaret\ncarte\ncater\ncrate\ntrace\nevil\nlevi\nlive\nveil\nvile\nelan\nlane\nlean\nlena\nneal\n" 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 key writes the letters of a sorted (insertion sort) into k; words are at most 16 letters 20 fn key a view uchar * k edit uchar 16 to void 21 for i ulong 16 22 put k i 0 23 end 24 for i ulong len a 25 c = get a i 26 j = i 27 while gt j 0 bound 16 28 if le get k sub j 1 c 29 break 30 end 31 put k j get k sub j 1 32 j = sub j 1 33 end 34 put k j c 35 end 36 end 37 fn same x view uchar 16 y view uchar 16 to bool 38 for i ulong 16 39 if ne get x i get y i 40 ret false 41 end 42 end 43 ret true 44 end 45 note index records each word's (start, length) and returns how many words there are 46 fn index st edit ulong 64 ln edit ulong 64 to ulong 47 k = ulong 0 48 s = ulong 0 49 for i ulong len dict 50 if eq get dict i 10 51 put st k s 52 put ln k sub i s 53 k = add k 1 54 s = add i 1 55 end 56 end 57 ret k 58 end 59 note count: how many words share word w's key 60 fn count st view ulong 64 ln view ulong 64 n ulong w ulong to ulong 61 a = new uchar 16 0 62 b = new uchar 16 0 63 key part dict get st w get ln w a 64 c = ulong 0 65 for v ulong n 66 key part dict get st v get ln v b 67 if same a b 68 c = add c 1 69 end 70 end 71 ret c 72 end 73 note first: no earlier word has word w's key 74 fn first st view ulong 64 ln view ulong 64 w ulong to bool 75 a = new uchar 16 0 76 b = new uchar 16 0 77 key part dict get st w get ln w a 78 for v ulong w 79 key part dict get st v get ln v b 80 if same a b 81 ret false 82 end 83 end 84 ret true 85 end 86 note print every group of the largest size, groups by first appearance, words in list order 87 fn show st view ulong 64 ln view ulong 64 n ulong best ulong to void 88 a = new uchar 16 0 89 b = new uchar 16 0 90 for w ulong n 91 if and first st ln w eq count st ln n w best 92 key part dict get st w get ln w a 93 f = true 94 for v ulong n 95 key part dict get st v get ln v b 96 if same a b 97 if not f 98 emit sp 99 end 100 emit part dict get st v get ln v 101 f = false 102 end 103 end 104 emit nl 105 end 106 end 107 end 108 fn main to int 109 st = new ulong 64 0 110 ln = new ulong 64 0 111 n = index st ln 112 best = ulong 0 113 for w ulong n 114 c = count st ln n w 115 if gt c best 116 best = c 117 end 118 end 119 show st ln n best 120 ret 0 121 end