sovereign-intelligence
Front door to Drake Stapleton's sovereign stack.
Brought by drakestapleton
sovereign-intelligence / tests / omc / examples / count_word.om
| 1 | note count_word: OM-2 `count A off n v`, a ulong, is how many of the n uchar elements of A from off |
| 2 | note equal v. Checked here against a byte-by-byte loop: every off 0..47 with lengths 0..40, the 16-byte |
| 3 | note chunk edges and the 4080-byte block edges, four values, over mixed bytes and over a run where every |
| 4 | note byte equals v; then in a local array, a text, a sized view, an unsized view, a part and an edit |
| 5 | note parameter, and with the destination also an input. Prints the number of cases and their counts. |
| 6 | om2 |
| 7 | global g uchar 70000 |
| 8 | global lens ulong 64 |
| 9 | global nlen ulong 1 |
| 10 | global cases ulong 1 |
| 11 | global total ulong 1 |
| 12 | global ob uchar 64 |
| 13 | text tok "count_word ok cases " |
| 14 | text tsum " sum " |
| 15 | text tbad "count_word MISMATCH\n" |
| 16 | text tnl "\n" |
| 17 | text tt "a\nbb\n\nccc\n" |
| 18 | |
| 19 | note naive: the same count over g, one byte at a time |
| 20 | fn naive o ulong n ulong v uchar to ulong |
| 21 | c = ulong 0 |
| 22 | for i ulong n bound 70000 |
| 23 | if eq get g add o i v |
| 24 | c = add c 1 |
| 25 | end |
| 26 | end |
| 27 | ret c |
| 28 | end |
| 29 | |
| 30 | note bad: report a mismatch and stop with exit 1 |
| 31 | fn bad to void |
| 32 | k = write 1 tbad 0 len tbad |
| 33 | exit 1 |
| 34 | end |
| 35 | |
| 36 | note one: compare count with naive for g[o, o + n) and value v |
| 37 | fn one o ulong n ulong v uchar to void |
| 38 | a = count g o n v |
| 39 | b = naive o n v |
| 40 | if ne a b |
| 41 | bad |
| 42 | end |
| 43 | put cases 0 add get cases 0 1 |
| 44 | put total 0 add get total 0 a |
| 45 | end |
| 46 | |
| 47 | note addlen: append n to the length list |
| 48 | fn addlen n ulong to void |
| 49 | k = get nlen 0 |
| 50 | put lens k n |
| 51 | put nlen 0 add k 1 |
| 52 | end |
| 53 | |
| 54 | note sweep: every off in [base, base + 48) with every listed length and four values |
| 55 | fn sweep base ulong to void |
| 56 | for j ulong 48 |
| 57 | o = add base j |
| 58 | for q ulong get nlen 0 bound 64 |
| 59 | n = get lens q |
| 60 | one o n 10 |
| 61 | one o n 0 |
| 62 | one o n 255 |
| 63 | one o n 37 |
| 64 | end |
| 65 | end |
| 66 | end |
| 67 | |
| 68 | note emu: print v in decimal |
| 69 | fn emu v ulong to void |
| 70 | d = new uchar 20 0 |
| 71 | x = v |
| 72 | k = ulong 0 |
| 73 | going = true |
| 74 | while going bound 21 |
| 75 | put d k uchar add rem x 10 48 |
| 76 | k = add k 1 |
| 77 | x = div x 10 |
| 78 | if eq x 0 |
| 79 | going = false |
| 80 | end |
| 81 | end |
| 82 | p = ulong 0 |
| 83 | while gt k 0 bound 21 |
| 84 | k = sub k 1 |
| 85 | put ob p get d k |
| 86 | p = add p 1 |
| 87 | end |
| 88 | w = write 1 ob 0 p |
| 89 | end |
| 90 | |
| 91 | note views: count through a sized view, an unsized view and an edit parameter |
| 92 | fn cs a view uchar 16 o ulong n ulong v uchar to ulong |
| 93 | ret count a o n v |
| 94 | end |
| 95 | |
| 96 | fn cv a view uchar * o ulong n ulong v uchar to ulong |
| 97 | ret count a o n v |
| 98 | end |
| 99 | |
| 100 | fn ce a edit uchar * o ulong n ulong v uchar to ulong |
| 101 | put a 0 v |
| 102 | ret count a o n v |
| 103 | end |
| 104 | |
| 105 | note forms: the other kinds of array, and a destination that is also an input |
| 106 | fn forms to void |
| 107 | loc = new uchar 16 7 |
| 108 | put loc 3 10 |
| 109 | put loc 9 10 |
| 110 | put loc 15 10 |
| 111 | if ne count loc 0 16 10 3 |
| 112 | bad |
| 113 | end |
| 114 | if ne count loc 4 11 10 1 |
| 115 | bad |
| 116 | end |
| 117 | if ne count loc 0 16 7 13 |
| 118 | bad |
| 119 | end |
| 120 | if ne count tt 0 len tt 10 4 |
| 121 | bad |
| 122 | end |
| 123 | if ne count tt 1 3 98 2 |
| 124 | bad |
| 125 | end |
| 126 | if ne cs loc 0 16 10 3 |
| 127 | bad |
| 128 | end |
| 129 | if ne cs loc 10 6 10 1 |
| 130 | bad |
| 131 | end |
| 132 | if ne cv loc 0 16 10 3 |
| 133 | bad |
| 134 | end |
| 135 | if ne cv part loc 3 7 0 7 10 2 |
| 136 | bad |
| 137 | end |
| 138 | if ne cv part loc 4 0 0 0 10 0 |
| 139 | bad |
| 140 | end |
| 141 | if ne ce loc 0 16 10 4 |
| 142 | bad |
| 143 | end |
| 144 | o = ulong 2 |
| 145 | o = count loc o 14 10 |
| 146 | if ne o 3 |
| 147 | bad |
| 148 | end |
| 149 | n = ulong 16 |
| 150 | n = count loc 0 n 10 |
| 151 | if ne n 4 |
| 152 | bad |
| 153 | end |
| 154 | end |
| 155 | |
| 156 | fn main to int |
| 157 | x = uint 12345 |
| 158 | for i ulong 30000 |
| 159 | x = add mul x 1103515245 12345 |
| 160 | b = uchar and shr x 16 255 |
| 161 | if lt b 64 |
| 162 | b = uchar 10 |
| 163 | end |
| 164 | put g i b |
| 165 | end |
| 166 | for i ulong 40000 |
| 167 | put g add 30000 i 10 |
| 168 | end |
| 169 | for i ulong 41 |
| 170 | addlen i |
| 171 | end |
| 172 | addlen 63 |
| 173 | addlen 64 |
| 174 | addlen 65 |
| 175 | addlen 255 |
| 176 | addlen 256 |
| 177 | addlen 257 |
| 178 | addlen 1000 |
| 179 | addlen 4079 |
| 180 | addlen 4080 |
| 181 | addlen 4081 |
| 182 | addlen 4095 |
| 183 | addlen 4096 |
| 184 | addlen 4097 |
| 185 | addlen 8160 |
| 186 | addlen 8161 |
| 187 | addlen 12000 |
| 188 | sweep 0 |
| 189 | sweep 30000 |
| 190 | one 0 70000 10 |
| 191 | one 0 70000 37 |
| 192 | one 69999 1 10 |
| 193 | one 70000 0 10 |
| 194 | forms |
| 195 | k = write 1 tok 0 len tok |
| 196 | emu get cases 0 |
| 197 | k = write 1 tsum 0 len tsum |
| 198 | emu get total 0 |
| 199 | k = write 1 tnl 0 len tnl |
| 200 | ret 0 |
| 201 | end |