Str's UTF-8 and toInt, Map's remove and next, Sort, Math, Fmt.real Str: encode and decode, FFFDH for what isn't a char (surrogates, overlong, cut short); toInt counts below zero for the smallest I64. Map: remove by moving later entries back, no tombstones; next walks the slots. Sort: a heapsort. Math after fdlibm, coefficients its own; sqrt by sqrtsd; sin and cos reduce past 1.6E6 by Payne-Hanek with a table of 2/pi. Within 1 ulp of glibc on 30,000 args, edges alike. Fmt.real: whole and fraction apart, an exponent from 1E15. Map and Sort, generic, are tested in test/run; the others in their main. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
| @@ -39,7 +39,7 @@ | ||
| 39 | 39 | breaks here: `spawn` changes. |
| 40 | 40 | - [x] 4. `Sys`, the rest: `clock`, `now`, `sleep`, `random`; |
| 41 | 41 | `connectTcp`, `connectUdp`, `listen`, `listenTcp`, `accept`. |
| 42 | -- [ ] 5. `Str.encode`, `Str.decode`, `Str.toInt`; `Map.remove` and | |
| 42 | +- [x] 5. `Str.encode`, `Str.decode`, `Str.toInt`; `Map.remove` and | |
| 43 | 43 | `Map.next`; `Sort`; `Math`; `Fmt.real`. Each module's `main` |
| 44 | 44 | tests it. |
| 45 | 45 | - [ ] 5a. Asked for rloc, and every program after it: `Opt`, a |
| @@ -127,12 +127,13 @@ | ||
| 127 | 127 | |
| 128 | 128 | `Rt` (ending the program, failed checks, signals), `Sys` (system calls), `Out` |
| 129 | 129 | (buffered output), `C`, `Lock`, `Thread` (threads without C), `Mem`, |
| 130 | -`Heap`, `Arena`, `Str`, `Buf`, `Fmt`, `List(T)` and `Map(K, V; hash, | |
| 131 | -eq)`. `tools/Doc.raz` prints any of them: | |
| 130 | +`Heap`, `Arena`, `Str`, `Buf`, `Fmt`, `Math`, `List(T)`, `Map(K, V; | |
| 131 | +hash, eq)` and `Sort(T; less)`. `tools/Doc.raz` prints any of them: | |
| 132 | 132 | |
| 133 | 133 | $ ./raz -o Doc tools/Doc.raz && ./Doc lib/Str.raz |
| 134 | 134 | |
| 135 | -Each library module's `main` is its test, which `make check` runs. | |
| 135 | +Each library module's `main` is its test, which `make check` runs; the | |
| 136 | +generic ones, which can't run alone, are tested in `test/run`. | |
| 136 | 137 | |
| 137 | 138 | ## Editors |
| 138 | 139 |
| @@ -1,5 +1,5 @@ | ||
| 1 | 1 | (* Formatted output. Each {} in the format takes the next argument: |
| 2 | - Fmt.print("{} of {}\n", [Fmt.int(n), Fmt.int(m)]); | |
| 2 | + Fmt.print("{} of {}\n", [Fmt.int(n), Fmt.real(x, 2)]); | |
| 3 | 3 | {{ and }} stand for { and }. print writes to standard output, |
| 4 | 4 | write appends to a Buf. *) |
| 5 | 5 | module Fmt; |
| @@ -13,6 +13,7 @@ | ||
| 13 | 13 | str(s: []U8); |
| 14 | 14 | char(c: U8); |
| 15 | 15 | bool(b: BOOL); |
| 16 | + real(r: F64; places: I64); | |
| 16 | 17 | end; |
| 17 | 18 | |
| 18 | 19 | (* The digits of u in base, at the end of the array: it and where they start. *) |
| @@ -35,6 +36,42 @@ | ||
| 35 | 36 | if b = nil then Out.str(s); else Buf.str(b, s); end; |
| 36 | 37 | end; |
| 37 | 38 | |
| 39 | +(* x with places digits after the point, at most 9, rounded to the | |
| 40 | + nearest; from 1E15 on, with the exponent that makes it less than 10, | |
| 41 | + as 1.50e17. The whole part and the fraction are taken apart, so that | |
| 42 | + each fits an I64. *) | |
| 43 | +proc putReal(b: ^Buf.Buf; x: F64; places: I64); | |
| 44 | +const zeros = "00000000"; | |
| 45 | +var d: [24]U8; at, scale, i, whole, frac, e: I64; | |
| 46 | +begin | |
| 47 | + if x # x then put(b, "nan"); return; end; | |
| 48 | + if x < 0.0 then put(b, "-"); x := -x; end; | |
| 49 | + if x > 1.7976931348623157E308 then put(b, "inf"); return; end; | |
| 50 | + if places < 0 then places := 0; elsif places > 9 then places := 9; end; | |
| 51 | + scale := 1; | |
| 52 | + for i := 1 to places do scale := scale * 10; end; | |
| 53 | + if x >= 1.0E15 then | |
| 54 | + while x >= 10.0 do x := x / 10.0; INC(e); end; | |
| 55 | + end; | |
| 56 | + whole := I64(x); | |
| 57 | + frac := I64((x - F64(whole)) * F64(scale) + 0.5); | |
| 58 | + if frac = scale then INC(whole); frac := 0; end; | |
| 59 | + if (e > 0) and (whole = 10) then whole := 1; INC(e); end; | |
| 60 | + d, at := digits(U64(whole), 10); | |
| 61 | + put(b, d[at..24]); | |
| 62 | + if places > 0 then | |
| 63 | + put(b, "."); | |
| 64 | + d, at := digits(U64(frac), 10); | |
| 65 | + put(b, zeros[0..places - (24 - at)]); | |
| 66 | + put(b, d[at..24]); | |
| 67 | + end; | |
| 68 | + if e > 0 then | |
| 69 | + d, at := digits(U64(e), 10); | |
| 70 | + put(b, "e"); | |
| 71 | + put(b, d[at..24]); | |
| 72 | + end; | |
| 73 | +end; | |
| 74 | + | |
| 38 | 75 | proc arg(b: ^Buf.Buf; a: Arg); |
| 39 | 76 | var d: [24]U8; at: I64; one: [1]U8; |
| 40 | 77 | begin |
| @@ -52,6 +89,7 @@ | ||
| 52 | 89 | | str(s): put(b, s); |
| 53 | 90 | | char(c): one[0] := c; put(b, one); |
| 54 | 91 | | bool(t): if t then put(b, "true"); else put(b, "false"); end; |
| 92 | + | real(r, places): putReal(b, r, places); | |
| 55 | 93 | end; |
| 56 | 94 | end; |
| 57 | 95 | |
| @@ -89,7 +127,7 @@ | ||
| 89 | 127 | |
| 90 | 128 | (* The module's test: formats into a buffer over an arena. *) |
| 91 | 129 | proc main*; |
| 92 | -var ar: Arena.Arena; b: Buf.Buf; | |
| 130 | +var ar: Arena.Arena; b: Buf.Buf; zero: F64; | |
| 93 | 131 | begin |
| 94 | 132 | if not Arena.init(ADR(ar), 1 shl 16) then Rt.stop("Fmt: no arena"); end; |
| 95 | 133 | Buf.init(ADR(b), ADR(ar.base)); |
| @@ -99,6 +137,14 @@ | ||
| 99 | 137 | Buf.reset(ADR(b)); |
| 100 | 138 | write(ADR(b), "{} and {}", [int(1)]); |
| 101 | 139 | if Buf.bytes(ADR(b)) # "1 and " then Rt.stop("Fmt: too few arguments"); end; |
| 140 | + Buf.reset(ADR(b)); | |
| 141 | + write(ADR(b), "{} {} {} {} {} {} {} {}", [real(3.14159, 2), real(-2.5, 0), real(0.999, 2), real(0.05, 1), | |
| 142 | + real(1.0, 12), real(-0.001, 2), real(123.0, 0), real(999999999999999.9, 0)]); | |
| 143 | + if Buf.bytes(ADR(b)) # "3.14 -3 1.00 0.1 1.000000000 -0.00 123 1000000000000000" then Rt.stop("Fmt.real"); end; | |
| 144 | + Buf.reset(ADR(b)); | |
| 145 | + write(ADR(b), "{} {} {} {} {} {}", [real(1.5E17, 2), real(-9.999E20, 2), real(1.0E15, 0), real(1.7976931348623157E308, 3), | |
| 146 | + real(1.0 / zero, 2), real(zero / zero, 2)]); | |
| 147 | + if Buf.bytes(ADR(b)) # "1.50e17 -1.00e21 1e15 1.798e308 inf nan" then Rt.stop("Fmt.real: an exponent"); end; | |
| 102 | 148 | Arena.done(ADR(ar)); |
| 103 | 149 | end; |
| 104 | 150 |
| @@ -68,6 +68,30 @@ | ||
| 68 | 68 | return true; |
| 69 | 69 | end; |
| 70 | 70 | |
| 71 | +(* k maps to nothing; false if it didn't map to anything. The entries | |
| 72 | + after it that were pushed on past its slot move back, so that a | |
| 73 | + search for each still finds it before an empty slot. *) | |
| 74 | +proc remove*(m: ^Map; k: K): BOOL; | |
| 75 | +var i, j, home: I64; | |
| 76 | +begin | |
| 77 | + if m^.cap = 0 then return false; end; | |
| 78 | + i := slot(m, k); | |
| 79 | + if not m^.slots[i].used then return false; end; | |
| 80 | + j := (i + 1) & (m^.cap - 1); | |
| 81 | + while m^.slots[j].used do | |
| 82 | + (* the entry at j may fill i if i lies between its home and j *) | |
| 83 | + home := I64(hash(m^.slots[j].key) & U64(m^.cap - 1)); | |
| 84 | + if (j - home) & (m^.cap - 1) >= (j - i) & (m^.cap - 1) then | |
| 85 | + m^.slots[i] := m^.slots[j]; | |
| 86 | + i := j; | |
| 87 | + end; | |
| 88 | + j := (j + 1) & (m^.cap - 1); | |
| 89 | + end; | |
| 90 | + m^.slots[i].used := false; | |
| 91 | + DEC(m^.len); | |
| 92 | + return true; | |
| 93 | +end; | |
| 94 | + | |
| 71 | 95 | (* What k maps to, and whether it maps to anything. *) |
| 72 | 96 | proc get*(m: ^Map; k: K): (V, BOOL); |
| 73 | 97 | var i: I64; none: V; |
| @@ -76,6 +100,18 @@ | ||
| 76 | 100 | i := slot(m, k); |
| 77 | 101 | if not m^.slots[i].used then return none, false; end; |
| 78 | 102 | return m^.slots[i].val, true; |
| 103 | +end; | |
| 104 | + | |
| 105 | +(* The entry after i, walking from 0: the number to pass for the one | |
| 106 | + after it, or -1 if there are no more, then its key and value. *) | |
| 107 | +proc next*(m: ^Map; i: I64): (I64, K, V); | |
| 108 | +var k: K; v: V; | |
| 109 | +begin | |
| 110 | + while i < m^.cap do | |
| 111 | + if m^.slots[i].used then return i + 1, m^.slots[i].key, m^.slots[i].val; end; | |
| 112 | + INC(i); | |
| 113 | + end; | |
| 114 | + return -1, k, v; | |
| 79 | 115 | end; |
| 80 | 116 | |
| 81 | 117 | end Map. |
| @@ -0,0 +1,348 @@ | ||
| 1 | +(* Mathematical functions of F64, as C's math.h has them, to within the | |
| 2 | + last bit or so. They follow fdlibm, Sun's library that most C ones | |
| 3 | + come from: each brings its argument into a small range, where a | |
| 4 | + polynomial is accurate, and puts the range back on the result. The | |
| 5 | + polynomials' coefficients are fdlibm's. | |
| 6 | + | |
| 7 | + Left out: tan, the inverse sines and powers, until a program needs | |
| 8 | + them. *) | |
| 9 | +module Math; | |
| 10 | +import Rt; | |
| 11 | + | |
| 12 | +const | |
| 13 | + pi* = 3.141592653589793; | |
| 14 | + ln2hi = 6.93147180369123816490E-01; (* ln 2 in two parts, whose sum is more exact than one F64 *) | |
| 15 | + ln2lo = 1.90821492927058770002E-10; | |
| 16 | + | |
| 17 | +(* The bits of x, and the F64 with bits u. *) | |
| 18 | +proc bits(x: F64): U64; | |
| 19 | +var p: ^U64; | |
| 20 | +begin | |
| 21 | + p := ^U64(ADR(x)); | |
| 22 | + return p^; | |
| 23 | +end; | |
| 24 | + | |
| 25 | +proc fromBits(u: U64): F64; | |
| 26 | +var p: ^F64; | |
| 27 | +begin | |
| 28 | + p := ^F64(ADR(u)); | |
| 29 | + return p^; | |
| 30 | +end; | |
| 31 | + | |
| 32 | +(* Whether x has its sign bit set, -0.0 included. *) | |
| 33 | +proc negative(x: F64): BOOL; | |
| 34 | +begin | |
| 35 | + return bits(x) shr 63 = 1; | |
| 36 | +end; | |
| 37 | + | |
| 38 | +proc sqrt*(x: F64): F64; | |
| 39 | +code 0F2H, 0FH, 51H, 0C0H, (* sqrtsd xmm0, xmm0 *) | |
| 40 | + 0C3H; (* ret *) | |
| 41 | + | |
| 42 | +(* The largest whole number not above x. One of 2^52 or more is whole | |
| 43 | + already, and so are an infinity and NaN. *) | |
| 44 | +proc floor*(x: F64): F64; | |
| 45 | +var t: F64; | |
| 46 | +begin | |
| 47 | + if not (x > -4503599627370496.0) or not (x < 4503599627370496.0) or (x = 0.0) then return x; end; | |
| 48 | + t := F64(I64(x)); | |
| 49 | + if t > x then t := t - 1.0; end; | |
| 50 | + return t; | |
| 51 | +end; | |
| 52 | + | |
| 53 | +(* x times 2^k, for k from -1075 to 1024: 2^k is made from its bits, | |
| 54 | + in two steps where k is past what an F64's exponent holds. *) | |
| 55 | +proc scale(x: F64; k: I64): F64; | |
| 56 | +begin | |
| 57 | + if k > 1023 then x := x * fromBits(U64(2046) shl 52); DEC(k, 1023); end; | |
| 58 | + if k < -1022 then x := x * fromBits(U64(1 + 53) shl 52); INC(k, 1022 - 53); end; | |
| 59 | + return x * fromBits(U64(k + 1023) shl 52); | |
| 60 | +end; | |
| 61 | + | |
| 62 | +(* e^x = 2^k e^r, with r = x - k ln 2 at most ln 2 / 2 across. *) | |
| 63 | +proc exp*(x: F64): F64; | |
| 64 | +var k: I64; hi, lo, r, rr, c: F64; | |
| 65 | +begin | |
| 66 | + if x # x then return x; end; | |
| 67 | + if x > 709.782712893383973096 then return x * fromBits(U64(2046) shl 52); end; (* too big: inf *) | |
| 68 | + if x < -745.13321910194110842 then return 0.0; end; | |
| 69 | + if x < 0.0 then k := I64(x * 1.44269504088896338700 - 0.5); | |
| 70 | + else k := I64(x * 1.44269504088896338700 + 0.5); | |
| 71 | + end; | |
| 72 | + hi := x - F64(k) * ln2hi; | |
| 73 | + lo := F64(k) * ln2lo; | |
| 74 | + r := hi - lo; | |
| 75 | + rr := r * r; | |
| 76 | + c := r - rr * (1.66666666666666019037E-01 + rr * (-2.77777777770155933842E-03 | |
| 77 | + + rr * (6.61375632143793436117E-05 + rr * (-1.65339022054652515390E-06 | |
| 78 | + + rr * 4.13813679705723846039E-08)))); | |
| 79 | + return scale(1.0 + (r * c / (2.0 - c) - lo + hi), k); | |
| 80 | +end; | |
| 81 | + | |
| 82 | +(* ln x = k ln 2 + ln m, with m from sqrt(2)/2 to sqrt(2): ln m is | |
| 83 | + found through s = f / (2 + f), f = m - 1, whose series converges | |
| 84 | + fast. *) | |
| 85 | +proc ln*(x: F64): F64; | |
| 86 | +var u: U64; k: I64; f, hfsq, s, z, w, r: F64; | |
| 87 | +begin | |
| 88 | + if x # x then return x; end; | |
| 89 | + if x < 0.0 then return (x - x) / 0.0; end; (* NaN *) | |
| 90 | + if x = 0.0 then return -1.0 / 0.0; end; (* -inf *) | |
| 91 | + if x > 1.7976931348623157E308 then return x; end; | |
| 92 | + if x < 2.2250738585072014E-308 then (* subnormal: made normal *) | |
| 93 | + x := x * 18014398509481984.0; (* 2^54 *) | |
| 94 | + k := -54; | |
| 95 | + end; | |
| 96 | + (* the exponent, counted from m's lower end, and the bits of m *) | |
| 97 | + u := bits(x) + (3FF0000000000000H - 3FE6A09E00000000H); | |
| 98 | + INC(k, I64(u shr 52) - 3FFH); | |
| 99 | + f := fromBits((u & 0FFFFFFFFFFFFFH) + 3FE6A09E00000000H) - 1.0; | |
| 100 | + hfsq := 0.5 * f * f; | |
| 101 | + s := f / (2.0 + f); | |
| 102 | + z := s * s; | |
| 103 | + w := z * z; | |
| 104 | + r := w * (3.999999999940941908E-01 + w * (2.222219843214978396E-01 + w * 1.531383769920937332E-01)) | |
| 105 | + + z * (6.666666666666735130E-01 + w * (2.857142874366239149E-01 | |
| 106 | + + w * (1.818357216161805012E-01 + w * 1.479819860511658591E-01))); | |
| 107 | + return s * (hfsq + r) + F64(k) * ln2lo - hfsq + f + F64(k) * ln2hi; | |
| 108 | +end; | |
| 109 | + | |
| 110 | +(* a times b: the low 64 bits of the product, and the high. *) | |
| 111 | +proc mul(a, b: U64): (U64, U64); | |
| 112 | +code 48H, 89H, 0F8H, (* mov rax, rdi *) | |
| 113 | + 48H, 0F7H, 0E6H, (* mul rsi *) | |
| 114 | + 0C3H; (* ret *) | |
| 115 | + | |
| 116 | +(* 64 bits of t from bit s on, the first bit of t[0] being 0. *) | |
| 117 | +proc window(t: []U64; s: I64): U64; | |
| 118 | +var q, r: I64; | |
| 119 | +begin | |
| 120 | + q := s div 64; | |
| 121 | + r := s mod 64; | |
| 122 | + if r = 0 then return t[q]; end; | |
| 123 | + return t[q] shl r | t[q + 1] shr (64 - r); | |
| 124 | +end; | |
| 125 | + | |
| 126 | +(* reduce's work, for an x too big for it, by Payne and Hanek's way. | |
| 127 | + x is m 2^e, m a whole number of 53 bits, so x 2/pi is m times the | |
| 128 | + bits of 2/pi moved by e: those that land at 4 or above add whole | |
| 129 | + turns, which change no sine, and are left out; the 192 after them | |
| 130 | + give the two bits of n mod 4 and the fraction past n, exact far | |
| 131 | + beyond an F64. *) | |
| 132 | +proc reduceLarge(x: F64): (I64, F64, F64); | |
| 133 | +var | |
| 134 | + t: [21]U64; (* 2/pi's bits after its point, behind a word of zeros *) | |
| 135 | + m, w0, w1, w2, p0, p1, p2, lo: U64; e, s, n, k: I64; y: F64; neg, up: BOOL; | |
| 136 | +begin | |
| 137 | + t := [0, 0A2F9836E4E441529H, 0FC2757D1F534DDC0H, 0DB6295993C439041H, 0FE5163ABDEBBC561H, | |
| 138 | + 0B7246E3A424DD2E0H, 06492EEA09D1921CH, 0FE1DEB1CB129A73EH, 0E88235F52EBB4484H, | |
| 139 | + 0E99C7026B45F7E41H, 03991D639835339F4H, 09C845F8BBDF9283BH, 01FF897FFDE05980FH, | |
| 140 | + 0EF2F118B5A0A6D1FH, 06D367ECF27CB09B7H, 04F463F669E5FEA2DH, 07527BAC7EBE5F17BH, | |
| 141 | + 03D0739F78A5292EAH, 06BFB5FB11F8D5D08H, 056033046FC7B6BABH, 0F0CFBC209AF4361DH]; | |
| 142 | + neg := x < 0.0; | |
| 143 | + if neg then x := -x; end; | |
| 144 | + m := bits(x) & 0FFFFFFFFFFFFFH | 10000000000000H; | |
| 145 | + e := I64(bits(x) shr 52) - 1075; | |
| 146 | + (* W, the 192 bits of 2/pi from the one worth 2^(1 - e), so that | |
| 147 | + x 2/pi is m W 2^-190, of which the bits from 2^192 up are left out *) | |
| 148 | + s := e + 62; | |
| 149 | + w0 := window(t, s); | |
| 150 | + w1 := window(t, s + 64); | |
| 151 | + w2 := window(t, s + 128); | |
| 152 | + p0, p1 := mul(m, w2); | |
| 153 | + lo, p2 := mul(m, w1); | |
| 154 | + INC(p1, lo); | |
| 155 | + INC(p2, m * w0); | |
| 156 | + if p1 < lo then INC(p2); end; | |
| 157 | + (* n is the two bits from 2^190, the fraction those below; from a | |
| 158 | + half up, n is the next and the fraction 1 less, taken as 1 less | |
| 159 | + the fraction, negated: its bits complemented, one in 2^190 short *) | |
| 160 | + n := I64(p2 shr 62); | |
| 161 | + p2 := p2 & 3FFFFFFFFFFFFFFFH; | |
| 162 | + up := p2 shr 61 = 1; | |
| 163 | + if up then | |
| 164 | + INC(n); | |
| 165 | + p0 := ~p0; p1 := ~p1; p2 := ~p2 & 3FFFFFFFFFFFFFFFH; | |
| 166 | + end; | |
| 167 | + (* the fraction as an F64, from the 64 bits that start at its first one *) | |
| 168 | + k := 0; | |
| 169 | + while (p2 shr 61 = 0) and (k < 128) do | |
| 170 | + p2 := p2 shl 1 | p1 shr 63; | |
| 171 | + p1 := p1 shl 1 | p0 shr 63; | |
| 172 | + p0 := p0 shl 1; | |
| 173 | + INC(k); | |
| 174 | + end; | |
| 175 | + y := scale(F64(p2 shl 2 | p1 shr 62) * (pi / 2.0), -64 - k); | |
| 176 | + if up then y := -y; end; | |
| 177 | + if neg then return -n, -y, 0.0; end; | |
| 178 | + return n, y, 0.0; | |
| 179 | +end; | |
| 180 | + | |
| 181 | +(* x less the whole number n of pi/2 nearest it, as y + yy, more exact | |
| 182 | + than one F64; and n. pi/2 is taken in three parts of 33 bits, each | |
| 183 | + with what is left of it after, so that n times each is exact while | |
| 184 | + n is below 2^20, about x = 1.6E6. *) | |
| 185 | +proc reduce(x: F64): (I64, F64, F64); | |
| 186 | +var n: I64; fn, r, t, w, y: F64; | |
| 187 | +begin | |
| 188 | + if (x > 1.6E6) or (x < -1.6E6) then return reduceLarge(x); end; | |
| 189 | + if x < 0.0 then n := I64(x * 6.36619772367581382433E-01 - 0.5); | |
| 190 | + else n := I64(x * 6.36619772367581382433E-01 + 0.5); | |
| 191 | + end; | |
| 192 | + fn := F64(n); | |
| 193 | + r := x - fn * 1.57079632673412561417E+00; | |
| 194 | + t := r; | |
| 195 | + w := fn * 6.07710050630396597660E-11; | |
| 196 | + r := t - w; | |
| 197 | + w := fn * 2.02226624879595063154E-21 - ((t - r) - w); | |
| 198 | + t := r; | |
| 199 | + w := fn * 2.02226624871116645580E-21; | |
| 200 | + r := t - w; | |
| 201 | + w := fn * 8.47842766036889956997E-32 - ((t - r) - w); | |
| 202 | + y := r - w; | |
| 203 | + return n, y, (r - y) - w; | |
| 204 | +end; | |
| 205 | + | |
| 206 | +(* sin and cos of x + y, x at most pi/4 and y far below it. *) | |
| 207 | +proc kernelSin(x, y: F64): F64; | |
| 208 | +var z, v, r: F64; | |
| 209 | +begin | |
| 210 | + z := x * x; | |
| 211 | + v := z * x; | |
| 212 | + r := 8.33333333332248946124E-03 + z * (-1.98412698298579493134E-04 + z * 2.75573137070700676789E-06) | |
| 213 | + + z * z * z * (-2.50507602534068634195E-08 + z * 1.58969099521155010221E-10); | |
| 214 | + return x - ((z * (0.5 * y - v * r) - y) + v * 1.66666666666666324348E-01); | |
| 215 | +end; | |
| 216 | + | |
| 217 | +proc kernelCos(x, y: F64): F64; | |
| 218 | +var z, r, hz, w: F64; | |
| 219 | +begin | |
| 220 | + z := x * x; | |
| 221 | + r := z * (4.16666666666666019037E-02 + z * (-1.38888888888741095749E-03 + z * 2.48015872894767294178E-05)) | |
| 222 | + + z * z * z * z * (-2.75573143513906633035E-07 + z * (2.08757232129817482790E-09 | |
| 223 | + - z * 1.13596475577881948265E-11)); | |
| 224 | + hz := 0.5 * z; | |
| 225 | + w := 1.0 - hz; | |
| 226 | + return w + (((1.0 - w) - hz) + (z * r - x * y)); | |
| 227 | +end; | |
| 228 | + | |
| 229 | +proc sin*(x: F64): F64; | |
| 230 | +var n: I64; y, yy: F64; | |
| 231 | +begin | |
| 232 | + if (x = 0.0) or (x # x) then return x; end; | |
| 233 | + if (x - x) # 0.0 then return x - x; end; (* an infinity: NaN *) | |
| 234 | + n, y, yy := reduce(x); | |
| 235 | + n := n & 3; | |
| 236 | + if n = 0 then return kernelSin(y, yy); | |
| 237 | + elsif n = 1 then return kernelCos(y, yy); | |
| 238 | + elsif n = 2 then return -kernelSin(y, yy); | |
| 239 | + end; | |
| 240 | + return -kernelCos(y, yy); | |
| 241 | +end; | |
| 242 | + | |
| 243 | +proc cos*(x: F64): F64; | |
| 244 | +var n: I64; y, yy: F64; | |
| 245 | +begin | |
| 246 | + if (x - x) # 0.0 then return x - x; end; (* an infinity or NaN: NaN *) | |
| 247 | + n, y, yy := reduce(x); | |
| 248 | + n := n & 3; | |
| 249 | + if n = 0 then return kernelCos(y, yy); | |
| 250 | + elsif n = 1 then return -kernelSin(y, yy); | |
| 251 | + elsif n = 2 then return -kernelCos(y, yy); | |
| 252 | + end; | |
| 253 | + return kernelSin(y, yy); | |
| 254 | +end; | |
| 255 | + | |
| 256 | +(* The arc tangent of x, at least 0: atan(x) = atan(c) + atan(t), t | |
| 257 | + small, for c one of 1/2, 1, 3/2 and infinity, as x is nearest; the | |
| 258 | + atan(c) in two parts as pi is. *) | |
| 259 | +proc atan(x: F64): F64; | |
| 260 | +var hi, lo, z, w, s1, s2: F64; | |
| 261 | +begin | |
| 262 | + if x < 0.4375 then | |
| 263 | + hi := 0.0; lo := 0.0; | |
| 264 | + elsif x < 0.6875 then | |
| 265 | + hi := 4.63647609000806093515E-01; lo := 2.26987774529616870924E-17; | |
| 266 | + x := (2.0 * x - 1.0) / (2.0 + x); | |
| 267 | + elsif x < 1.1875 then | |
| 268 | + hi := 7.85398163397448278999E-01; lo := 3.06161699786838301793E-17; | |
| 269 | + x := (x - 1.0) / (x + 1.0); | |
| 270 | + elsif x < 2.4375 then | |
| 271 | + hi := 9.82793723247329054082E-01; lo := 1.39033110312309984516E-17; | |
| 272 | + x := (x - 1.5) / (1.0 + 1.5 * x); | |
| 273 | + else | |
| 274 | + hi := 1.57079632679489655800E+00; lo := 6.12323399573676603587E-17; | |
| 275 | + x := -1.0 / x; | |
| 276 | + end; | |
| 277 | + z := x * x; | |
| 278 | + w := z * z; | |
| 279 | + s1 := z * (3.33333333333329318027E-01 + w * (1.42857142725034663711E-01 + w * (9.09088713343650656196E-02 | |
| 280 | + + w * (6.66107313738753120669E-02 + w * (4.97687799461593236017E-02 + w * 1.62858201153657823623E-02))))); | |
| 281 | + s2 := w * (-1.99999999998764832476E-01 + w * (-1.11111104054623557880E-01 + w * (-7.69187620504482999495E-02 | |
| 282 | + + w * (-5.83357013379057348645E-02 - w * 3.65315727442169155270E-02)))); | |
| 283 | + return hi - ((x * (s1 + s2) - lo) - x); | |
| 284 | +end; | |
| 285 | + | |
| 286 | +(* The angle of the point (x, y) from the x axis, from -pi to pi. *) | |
| 287 | +proc atan2*(y, x: F64): F64; | |
| 288 | +var a: F64; (* the angle from the x axis on x's side, at least 0 *) | |
| 289 | +begin | |
| 290 | + if (x # x) or (y # y) then return x + y; end; | |
| 291 | + if y = 0.0 then | |
| 292 | + if not negative(x) then return y; end; | |
| 293 | + a := pi; | |
| 294 | + else | |
| 295 | + if ((x - x) # 0.0) and ((y - y) # 0.0) then a := pi / 4.0; (* both infinite *) | |
| 296 | + else | |
| 297 | + a := y / x; | |
| 298 | + if a < 0.0 then a := -a; end; | |
| 299 | + a := atan(a); | |
| 300 | + end; | |
| 301 | + (* pi less a, with what pi lacks of its true value *) | |
| 302 | + if negative(x) then a := pi - (a - 1.2246467991473531772E-16); end; | |
| 303 | + end; | |
| 304 | + if negative(y) then return -a; end; | |
| 305 | + return a; | |
| 306 | +end; | |
| 307 | + | |
| 308 | +(* The module's test: values C's library gives, to within two units in | |
| 309 | + the last place, and the edges. *) | |
| 310 | +proc near(got, want: F64): BOOL; | |
| 311 | +var d: F64; | |
| 312 | +begin | |
| 313 | + d := got - want; | |
| 314 | + if d < 0.0 then d := -d; end; | |
| 315 | + if want < 0.0 then want := -want; end; | |
| 316 | + return d <= want * 4.5E-16; | |
| 317 | +end; | |
| 318 | + | |
| 319 | +proc main*; | |
| 320 | +var inf, nan, hard: F64; | |
| 321 | +begin | |
| 322 | + inf := 1.0 / 0.0; | |
| 323 | + nan := inf - inf; | |
| 324 | + hard := scale(6381956970095103.0, 797); (* of all F64s, the nearest a multiple of pi/2 *) | |
| 325 | + if (sqrt(2.0) # 1.4142135623730951) or (sqrt(0.0) # 0.0) or (sqrt(-1.0) = sqrt(-1.0)) then Rt.stop("Math.sqrt"); end; | |
| 326 | + if (floor(2.5) # 2.0) or (floor(-2.5) # -3.0) or (floor(-2.0) # -2.0) or (floor(1.0E300) # 1.0E300) | |
| 327 | + or not negative(floor(-0.0)) or (floor(nan) = floor(nan)) then Rt.stop("Math.floor"); end; | |
| 328 | + if not near(exp(1.0), 2.718281828459045) or not near(exp(-3.5), 0.0301973834223185) | |
| 329 | + or not near(exp(700.0), 1.0142320547350045E304) or not near(exp(-740.0), 4.2E-322) | |
| 330 | + or (exp(0.0) # 1.0) or (exp(710.0) # inf) or (exp(-746.0) # 0.0) or (exp(-inf) # 0.0) then Rt.stop("Math.exp"); end; | |
| 331 | + if not near(ln(10.0), 2.302585092994046) or not near(ln(0.3), -1.2039728043259361) | |
| 332 | + or not near(ln(1.0E-310), -713.8013788281542) or (ln(1.0) # 0.0) or (ln(0.0) # -inf) | |
| 333 | + or (ln(inf) # inf) or (ln(-1.0) = ln(-1.0)) then Rt.stop("Math.ln"); end; | |
| 334 | + if not near(sin(1.0), 0.8414709848078965) or not near(sin(-100.0), 0.5063656411097588) | |
| 335 | + or not near(sin(pi), 1.2246467991473532E-16) or not near(sin(1.0E6), -0.34999350217129294) | |
| 336 | + or not near(sin(1.0E22), -0.8522008497671888) or not negative(sin(-0.0)) | |
| 337 | + or (sin(inf) = sin(inf)) then Rt.stop("Math.sin"); end; | |
| 338 | + if not near(cos(1.0), 0.5403023058681398) or not near(cos(-100.0), 0.8623188722876839) | |
| 339 | + or not near(cos(pi / 2.0), 6.123233995736766E-17) or not near(cos(1.0E6), 0.9367521275331447) | |
| 340 | + or not near(cos(1.0E300), -0.5753861119575491) or not near(cos(hard), -4.687165924254628E-19) | |
| 341 | + or (cos(0.0) # 1.0) or (cos(nan) = cos(nan)) then Rt.stop("Math.cos"); end; | |
| 342 | + if not near(atan2(1.0, 2.0), 0.4636476090008061) or not near(atan2(-3.0, -4.0), -2.498091544796509) | |
| 343 | + or not near(atan2(5.0, 0.1), 1.550798992821746) or not near(atan2(1.0, -1.0E-20), 1.5707963267948966) | |
| 344 | + or (atan2(0.0, -1.0) # pi) or not negative(atan2(-0.0, 1.0)) or (atan2(-inf, inf) # -pi / 4.0) | |
| 345 | + or not near(atan2(inf, -inf), 2.356194490192345) or (atan2(1.0, 0.0) # pi / 2.0) then Rt.stop("Math.atan2"); end; | |
| 346 | +end; | |
| 347 | + | |
| 348 | +end Math. |
| @@ -0,0 +1,35 @@ | ||
| 1 | +(* Sorting a slice in place, in the order less gives: | |
| 2 | + import ByAge := Sort(Person, younger); | |
| 3 | + It is a heapsort: no memory of its own, and n log n steps at most, | |
| 4 | + whatever the order it starts in. Two elements equal by less may | |
| 5 | + change places. *) | |
| 6 | +module Sort(T; less: proc(a, b: T): BOOL); | |
| 7 | + | |
| 8 | +(* The element at i sinks through s[0..n], a heap with the largest at | |
| 9 | + its root, to where it is no smaller than its children, 2i+1 and | |
| 10 | + 2i+2. *) | |
| 11 | +proc sink(s: []T; i, n: I64); | |
| 12 | +var c: I64; t: T; | |
| 13 | +begin | |
| 14 | + c := 2 * i + 1; | |
| 15 | + while c < n do | |
| 16 | + if (c + 1 < n) and less(s[c], s[c + 1]) then INC(c); end; | |
| 17 | + if not less(s[i], s[c]) then return; end; | |
| 18 | + t := s[i]; s[i] := s[c]; s[c] := t; | |
| 19 | + i := c; | |
| 20 | + c := 2 * i + 1; | |
| 21 | + end; | |
| 22 | +end; | |
| 23 | + | |
| 24 | +(* s made a heap, then its largest taken, in turn, to the end. *) | |
| 25 | +proc sort*(s: []T); | |
| 26 | +var i: I64; t: T; | |
| 27 | +begin | |
| 28 | + for i := LEN(s) div 2 - 1 to 0 by -1 do sink(s, i, LEN(s)); end; | |
| 29 | + for i := LEN(s) - 1 to 1 by -1 do | |
| 30 | + t := s[0]; s[0] := s[i]; s[i] := t; | |
| 31 | + sink(s, 0, i); | |
| 32 | + end; | |
| 33 | +end; | |
| 34 | + | |
| 35 | +end Sort. |
| @@ -1,7 +1,10 @@ | ||
| 1 | 1 | (* Operations on strings, which are []U8. = and # compare them; this |
| 2 | - module orders, searches and hashes them. *) | |
| 2 | + module orders, searches and hashes them, reads and writes their | |
| 3 | + UTF-8, and reads a number from one. *) | |
| 3 | 4 | module Str; |
| 4 | 5 | import Rt; |
| 6 | + | |
| 7 | +const least = -9223372036854775807 - 1; (* the smallest I64 *) | |
| 5 | 8 | |
| 6 | 9 | (* -1, 0 or 1 as a sorts before, with or after b, byte by byte. *) |
| 7 | 10 | proc cmp*(a, b: []U8): I64; |
| @@ -51,6 +54,154 @@ | ||
| 51 | 54 | return (LEN(s) >= LEN(p)) and (s[0..LEN(p)] = p); |
| 52 | 55 | end; |
| 53 | 56 | |
| 57 | +(* c in UTF-8: its bytes, and how many. A number that isn't a | |
| 58 | + character, a surrogate or one past 10FFFFH, becomes FFFDH, the | |
| 59 | + character that stands for a wrong one. *) | |
| 60 | +proc encode*(c: I64): ([4]U8, I64); | |
| 61 | +var b: [4]U8; | |
| 62 | +begin | |
| 63 | + if (c < 0) or (c > 10FFFFH) or (c >= 0D800H) and (c <= 0DFFFH) then c := 0FFFDH; end; | |
| 64 | + if c < 80H then | |
| 65 | + b[0] := U8(c); | |
| 66 | + return b, 1; | |
| 67 | + elsif c < 800H then | |
| 68 | + b[0] := U8(0C0H + (c shr 6)); | |
| 69 | + b[1] := U8(80H + (c & 3FH)); | |
| 70 | + return b, 2; | |
| 71 | + elsif c < 10000H then | |
| 72 | + b[0] := U8(0E0H + (c shr 12)); | |
| 73 | + b[1] := U8(80H + (c shr 6 & 3FH)); | |
| 74 | + b[2] := U8(80H + (c & 3FH)); | |
| 75 | + return b, 3; | |
| 76 | + end; | |
| 77 | + b[0] := U8(0F0H + (c shr 18)); | |
| 78 | + b[1] := U8(80H + (c shr 12 & 3FH)); | |
| 79 | + b[2] := U8(80H + (c shr 6 & 3FH)); | |
| 80 | + b[3] := U8(80H + (c & 3FH)); | |
| 81 | + return b, 4; | |
| 82 | +end; | |
| 83 | + | |
| 84 | +(* The character s starts with, and its length in bytes: FFFDH and 1 | |
| 85 | + if its first byte doesn't start one rightly, being a continuation, | |
| 86 | + or followed by too few continuations, or spelling a surrogate or a | |
| 87 | + character in more bytes than it needs; 0 and 0 if s is empty. *) | |
| 88 | +proc decode*(s: []U8): (I64, I64); | |
| 89 | +var c, n, least, i: I64; | |
| 90 | +begin | |
| 91 | + if LEN(s) = 0 then return 0, 0; end; | |
| 92 | + c := I64(s[0]); | |
| 93 | + if c < 80H then return c, 1; | |
| 94 | + elsif c < 0C0H then return 0FFFDH, 1; | |
| 95 | + elsif c < 0E0H then n := 2; c := c & 1FH; least := 80H; | |
| 96 | + elsif c < 0F0H then n := 3; c := c & 0FH; least := 800H; | |
| 97 | + elsif c < 0F8H then n := 4; c := c & 7H; least := 10000H; | |
| 98 | + else return 0FFFDH, 1; | |
| 99 | + end; | |
| 100 | + if LEN(s) < n then return 0FFFDH, 1; end; | |
| 101 | + for i := 1 to n - 1 do | |
| 102 | + if s[i] & 0C0H # 80H then return 0FFFDH, 1; end; | |
| 103 | + c := c shl 6 + I64(s[i] & 3FH); | |
| 104 | + end; | |
| 105 | + if (c < least) or (c > 10FFFFH) or (c >= 0D800H) and (c <= 0DFFFH) then return 0FFFDH, 1; end; | |
| 106 | + return c, n; | |
| 107 | +end; | |
| 108 | + | |
| 109 | +(* s as a decimal number, with a - or + before it if any, and nothing | |
| 110 | + else; false if it isn't one, or an I64 can't hold it. *) | |
| 111 | +proc toInt*(s: []U8): (I64, BOOL); | |
| 112 | +var n, i, d: I64; neg: BOOL; | |
| 113 | +begin | |
| 114 | + neg := (LEN(s) > 0) and (s[0] = '-'); | |
| 115 | + if (LEN(s) > 0) and ((s[0] = '-') or (s[0] = '+')) then i := 1; end; | |
| 116 | + if i = LEN(s) then return 0, false; end; | |
| 117 | + (* the digits are taken off below zero, since the smallest I64 has no positive *) | |
| 118 | + while i < LEN(s) do | |
| 119 | + if (s[i] < '0') or (s[i] > '9') then return 0, false; end; | |
| 120 | + d := I64(s[i] - '0'); | |
| 121 | + if n < (least + d) div 10 then return 0, false; end; | |
| 122 | + n := n * 10 - d; | |
| 123 | + INC(i); | |
| 124 | + end; | |
| 125 | + if not neg then | |
| 126 | + if n = least then return 0, false; end; | |
| 127 | + n := -n; | |
| 128 | + end; | |
| 129 | + return n, true; | |
| 130 | +end; | |
| 131 | + | |
| 132 | +proc testUtf8; | |
| 133 | +var b: [4]U8; n, c, i: I64; | |
| 134 | +begin | |
| 135 | + b, n := encode('a'); | |
| 136 | + if (n # 1) or (b[0] # 'a') then Rt.stop("Str.encode: a"); end; | |
| 137 | + b, n := encode(0E9H); | |
| 138 | + if b[0..n] # "é" then Rt.stop("Str.encode: é"); end; | |
| 139 | + b, n := encode(20ACH); | |
| 140 | + if b[0..n] # "€" then Rt.stop("Str.encode: €"); end; | |
| 141 | + b, n := encode(1F600H); | |
| 142 | + if b[0..n] # "😀" then Rt.stop("Str.encode: 😀"); end; | |
| 143 | + b, n := encode(0D800H); | |
| 144 | + if b[0..n] # "\u{FFFD}" then Rt.stop("Str.encode: a surrogate"); end; | |
| 145 | + b, n := encode(110000H); | |
| 146 | + if n # 3 then Rt.stop("Str.encode: past 10FFFFH"); end; | |
| 147 | + (* every character back through decode *) | |
| 148 | + for i := 0 to 10FFFFH do | |
| 149 | + if (i < 0D800H) or (i > 0DFFFH) then | |
| 150 | + b, n := encode(i); | |
| 151 | + c, n := decode(b[0..n]); | |
| 152 | + if c # i then Rt.stop("Str.decode: not what encode wrote"); end; | |
| 153 | + end; | |
| 154 | + end; | |
| 155 | + c, n := decode(""); | |
| 156 | + if (c # 0) or (n # 0) then Rt.stop("Str.decode: empty"); end; | |
| 157 | + c, n := decode("é!"); | |
| 158 | + if (c # 0E9H) or (n # 2) then Rt.stop("Str.decode: é"); end; | |
| 159 | + b := [80H, 'a', 0, 0]; | |
| 160 | + c, n := decode(b[0..2]); | |
| 161 | + if (c # 0FFFDH) or (n # 1) then Rt.stop("Str.decode: a continuation"); end; | |
| 162 | + b := [0E2H, 82H, 'a', 0]; | |
| 163 | + c, n := decode(b[0..3]); | |
| 164 | + if (c # 0FFFDH) or (n # 1) then Rt.stop("Str.decode: too few continuations"); end; | |
| 165 | + c, n := decode(b[0..2]); | |
| 166 | + if (c # 0FFFDH) or (n # 1) then Rt.stop("Str.decode: cut short"); end; | |
| 167 | + b := [0C0H, 0AFH, 0, 0]; | |
| 168 | + c, n := decode(b[0..2]); | |
| 169 | + if (c # 0FFFDH) or (n # 1) then Rt.stop("Str.decode: overlong"); end; | |
| 170 | + b := [0EDH, 0A0H, 80H, 0]; | |
| 171 | + c, n := decode(b[0..3]); | |
| 172 | + if (c # 0FFFDH) or (n # 1) then Rt.stop("Str.decode: a surrogate"); end; | |
| 173 | + b := [0F4H, 90H, 80H, 80H]; | |
| 174 | + c, n := decode(b); | |
| 175 | + if (c # 0FFFDH) or (n # 1) then Rt.stop("Str.decode: past 10FFFFH"); end; | |
| 176 | +end; | |
| 177 | + | |
| 178 | +proc testToInt; | |
| 179 | +var n: I64; ok: BOOL; | |
| 180 | +begin | |
| 181 | + n, ok := toInt("42"); | |
| 182 | + if not ok or (n # 42) then Rt.stop("Str.toInt: 42"); end; | |
| 183 | + n, ok := toInt("-17"); | |
| 184 | + if not ok or (n # -17) then Rt.stop("Str.toInt: -17"); end; | |
| 185 | + n, ok := toInt("+0"); | |
| 186 | + if not ok or (n # 0) then Rt.stop("Str.toInt: +0"); end; | |
| 187 | + n, ok := toInt("9223372036854775807"); | |
| 188 | + if not ok or (n # 9223372036854775807) then Rt.stop("Str.toInt: the largest"); end; | |
| 189 | + n, ok := toInt("-9223372036854775808"); | |
| 190 | + if not ok or (n # least) then Rt.stop("Str.toInt: the smallest"); end; | |
| 191 | + n, ok := toInt("9223372036854775808"); | |
| 192 | + if ok then Rt.stop("Str.toInt: past the largest"); end; | |
| 193 | + n, ok := toInt("-9223372036854775809"); | |
| 194 | + if ok then Rt.stop("Str.toInt: past the smallest"); end; | |
| 195 | + n, ok := toInt(""); | |
| 196 | + if ok then Rt.stop("Str.toInt: empty"); end; | |
| 197 | + n, ok := toInt("-"); | |
| 198 | + if ok then Rt.stop("Str.toInt: a sign alone"); end; | |
| 199 | + n, ok := toInt("12a"); | |
| 200 | + if ok then Rt.stop("Str.toInt: a letter"); end; | |
| 201 | + n, ok := toInt(" 1"); | |
| 202 | + if ok then Rt.stop("Str.toInt: a space"); end; | |
| 203 | +end; | |
| 204 | + | |
| 54 | 205 | (* The module's test: stops the program if something is wrong. *) |
| 55 | 206 | proc main*; |
| 56 | 207 | begin |
| @@ -61,6 +212,8 @@ | ||
| 61 | 212 | if (find("hello", "ll") # 2) or (find("hello", "x") # -1) or (find("", "") # 0) |
| 62 | 213 | or (find("ab", "abc") # -1) then Rt.stop("Str.find"); end; |
| 63 | 214 | if not hasPrefix("hello", "he") or hasPrefix("he", "hello") then Rt.stop("Str.hasPrefix"); end; |
| 215 | + testUtf8; | |
| 216 | + testToInt; | |
| 64 | 217 | end; |
| 65 | 218 | |
| 66 | 219 | end Str. |
| @@ -1,6 +1,7 @@ | ||
| 1 | 1 | module Str; |
| 2 | 2 | Operations on strings, which are []U8. = and # compare them; this |
| 3 | - module orders, searches and hashes them. | |
| 3 | + module orders, searches and hashes them, reads and writes their | |
| 4 | + UTF-8, and reads a number from one. | |
| 4 | 5 | |
| 5 | 6 | proc cmp*(a, b: []U8): I64; |
| 6 | 7 | -1, 0 or 1 as a sorts before, with or after b, byte by byte. |
| @@ -14,6 +15,21 @@ | ||
| 14 | 15 | Where sub first starts in s, or -1. |
| 15 | 16 | |
| 16 | 17 | proc hasPrefix*(s, p: []U8): BOOL; |
| 18 | + | |
| 19 | +proc encode*(c: I64): ([4]U8, I64); | |
| 20 | + c in UTF-8: its bytes, and how many. A number that isn't a | |
| 21 | + character, a surrogate or one past 10FFFFH, becomes FFFDH, the | |
| 22 | + character that stands for a wrong one. | |
| 23 | + | |
| 24 | +proc decode*(s: []U8): (I64, I64); | |
| 25 | + The character s starts with, and its length in bytes: FFFDH and 1 | |
| 26 | + if its first byte doesn't start one rightly, being a continuation, | |
| 27 | + or followed by too few continuations, or spelling a surrogate or a | |
| 28 | + character in more bytes than it needs; 0 and 0 if s is empty. | |
| 29 | + | |
| 30 | +proc toInt*(s: []U8): (I64, BOOL); | |
| 31 | + s as a decimal number, with a - or + before it if any, and nothing | |
| 32 | + else; false if it isn't one, or an I64 can't hold it. | |
| 17 | 33 | |
| 18 | 34 | proc main*; |
| 19 | 35 | The module's test: stops the program if something is wrong. |
| @@ -0,0 +1,2 @@ | ||
| 1 | +341 entries, 341 of them, summing 6658308 | |
| 2 | +walked 341, summing 6658308 |
| @@ -0,0 +1,56 @@ | ||
| 1 | +(* Map's remove and next, against an array that holds the same: keys | |
| 2 | + hashed into runs of eight, so that removing moves entries back, | |
| 3 | + around the end of the slots too. *) | |
| 4 | +module MapRemove; | |
| 5 | +import Fmt, Heap, Rt, Ints := Map(I64, I64, near, same); | |
| 6 | + | |
| 7 | +const N = 500; | |
| 8 | + | |
| 9 | +proc near(k: I64): U64; | |
| 10 | +begin | |
| 11 | + return U64(k div 8 * 977); | |
| 12 | +end; | |
| 13 | + | |
| 14 | +proc same(a, b: I64): BOOL; | |
| 15 | +begin | |
| 16 | + return a = b; | |
| 17 | +end; | |
| 18 | + | |
| 19 | +proc main*; | |
| 20 | +var | |
| 21 | + m: Ints.Map; want: [N]I64; i, k, v, n, sum, seed: I64; | |
| 22 | + found, _ok: BOOL; | |
| 23 | +begin | |
| 24 | + Ints.init(ADR(m), Heap.heap); | |
| 25 | + seed := 1; | |
| 26 | + for i := 1 to 20000 do | |
| 27 | + seed := (seed * 1103515245 + 12345) & 7FFFFFFFH; | |
| 28 | + k := seed mod N; | |
| 29 | + if seed div N mod 3 = 0 then | |
| 30 | + if Ints.remove(ADR(m), k) # (want[k] # 0) then Rt.stop("remove: wrong answer"); end; | |
| 31 | + want[k] := 0; | |
| 32 | + else | |
| 33 | + _ok := Ints.put(ADR(m), k, i); | |
| 34 | + want[k] := i; | |
| 35 | + end; | |
| 36 | + end; | |
| 37 | + for k := 0 to N - 1 do | |
| 38 | + v, found := Ints.get(ADR(m), k); | |
| 39 | + if (found # (want[k] # 0)) or (v # want[k]) then Rt.stop("get: not what was put"); end; | |
| 40 | + if found then INC(n); INC(sum, v); end; | |
| 41 | + end; | |
| 42 | + if Ints.remove(ADR(m), N) then Rt.stop("remove: a key never put"); end; | |
| 43 | + Fmt.print("{} entries, {} of them, summing {}\n", [Fmt.int(m.len), Fmt.int(n), Fmt.int(sum)]); | |
| 44 | + (* the walk sees each entry once *) | |
| 45 | + n := 0; sum := 0; | |
| 46 | + i, k, v := Ints.next(ADR(m), 0); | |
| 47 | + while i >= 0 do | |
| 48 | + if want[k] # v then Rt.stop("next: not what was put"); end; | |
| 49 | + want[k] := 0; | |
| 50 | + INC(n); INC(sum, v); | |
| 51 | + i, k, v := Ints.next(ADR(m), i); | |
| 52 | + end; | |
| 53 | + Fmt.print("walked {}, summing {}\n", [Fmt.int(n), Fmt.int(sum)]); | |
| 54 | +end; | |
| 55 | + | |
| 56 | +end MapRemove. |
| @@ -0,0 +1,4 @@ | ||
| 1 | +Bo 7 | |
| 2 | +Di 23 | |
| 3 | +Ana 41 | |
| 4 | +Cy 90 |
| @@ -0,0 +1,51 @@ | ||
| 1 | +(* Sort: slices in many orders and of every length up to 40 come out | |
| 2 | + in order, holding what they held; and a sort by a record's field. *) | |
| 3 | +module Sorted; | |
| 4 | +import Fmt, Rt, Ints := Sort(I64, below), ByAge := Sort(Person, younger); | |
| 5 | + | |
| 6 | +type Person = record name: []U8; age: I64; end; | |
| 7 | + | |
| 8 | +proc below(a, b: I64): BOOL; | |
| 9 | +begin | |
| 10 | + return a < b; | |
| 11 | +end; | |
| 12 | + | |
| 13 | +proc younger(a, b: Person): BOOL; | |
| 14 | +begin | |
| 15 | + return a.age < b.age; | |
| 16 | +end; | |
| 17 | + | |
| 18 | +proc main*; | |
| 19 | +var | |
| 20 | + a: [40]I64; count: [10]I64; n, i, kind, seed: I64; | |
| 21 | + people: [4]Person; | |
| 22 | +begin | |
| 23 | + seed := 7; | |
| 24 | + for n := 0 to 40 do | |
| 25 | + for kind := 0 to 3 do | |
| 26 | + for i := 0 to 9 do count[i] := 0; end; | |
| 27 | + for i := 0 to n - 1 do | |
| 28 | + seed := (seed * 1103515245 + 12345) & 7FFFFFFFH; | |
| 29 | + if kind = 0 then a[i] := seed mod 10; (* at random, with repeats *) | |
| 30 | + elsif kind = 1 then a[i] := i mod 10; (* rising *) | |
| 31 | + elsif kind = 2 then a[i] := 9 - i mod 10; (* falling *) | |
| 32 | + else a[i] := 5; (* all equal *) | |
| 33 | + end; | |
| 34 | + INC(count[a[i]]); | |
| 35 | + end; | |
| 36 | + Ints.sort(a[0..n]); | |
| 37 | + for i := 0 to n - 1 do | |
| 38 | + if (i > 0) and (a[i - 1] > a[i]) then Rt.stop("out of order"); end; | |
| 39 | + DEC(count[a[i]]); | |
| 40 | + end; | |
| 41 | + for i := 0 to 9 do | |
| 42 | + if count[i] # 0 then Rt.stop("not what it held"); end; | |
| 43 | + end; | |
| 44 | + end; | |
| 45 | + end; | |
| 46 | + people := [{name: "Ana", age: 41}, {name: "Bo", age: 7}, {name: "Cy", age: 90}, {name: "Di", age: 23}]; | |
| 47 | + ByAge.sort(people); | |
| 48 | + for i := 0 to 3 do Fmt.print("{} {}\n", [Fmt.str(people[i].name), Fmt.int(people[i].age)]); end; | |
| 49 | +end; | |
| 50 | + | |
| 51 | +end Sorted. |