check.bend

bend-osdev-algo@0.1.0.0 · 58 lines · 6 definitions

1import Base
2import bend-osdev-word64@0.1.0.0/word64.bend as W
3import bend-osdev-algo@0.1.0.0/algo.bend as A
4import bend-osdev-algo@0.1.0.0/bitmap.bend as M
5import bend-osdev-algo@0.1.0.0/bsearch.bend as S
6
7def show_maybe(m: Maybe<&2, Nat>) -> String:
8  match m:
9    case None{}:
10      "none"
11    case Some{v}:
12      Nat.show(v)
13
14def fill(xs: List<&2, U32>, +i: U32, a: Array<U32>) -> Array<U32>:
15  match xs:
16    case Nil{}:
17      a
18    case Con{x, t}:
19      fill(t, U32.inc(i), Array.set(U32, a, i, x))
20
21def show_val(r: Array<U32> & U32) -> String:
22  (a, x) = r
23  U32.show(x)
24
25def show_opt(r: Array<U32> & Maybe<&2, U32>) -> String:
26  (a, m) = r
27  match m:
28    case None{}:
29      "none"
30    case Some{v}:
31      U32.show(v)
32
33def sorted() -> List<&2, U32>:
34  [2, 3, 5, 7, 11, 13, 17, 19]
35
36def main() -> IO(Unit):
37  do IO<Unit>:
38    IO.print(Nat.show(A.bits32.popcount(3735928559)))
39    IO.print(Nat.show(A.bits32.ffs(40)) ++ " " ++ Nat.show(A.bits32.fls(40)))
40    IO.print(Nat.show(A.bits32.ctz(40)) ++ " " ++ Nat.show(A.bits32.clz(40)))
41    IO.print(show_maybe(A.bits32.first_set(0)) ++ " " ++ show_maybe(A.bits32.last_set(2147483648)))
42    IO.print(Nat.show(A.bits64.ctz(W.W64.from_parts(16, 0))) ++ " " ++ Nat.show(A.bits64.clz(W.W64.from_parts(16, 0))))
43    IO.print(Nat.show(A.bits64.popcount(W.W64.max())))
44    IO.print(Nat.show(M.bitmap.next_set([0, 8], 64n, 3n)) ++ " " ++ Nat.show(M.bitmap.next_clear([4294967295, 1], 64n, 0n)))
45    IO.print(Nat.show(M.bitmap.first_set([0, 0], 64n)))
46    IO.print(Nat.show(S.bsearch.lower_bound(sorted(), 12)) ++ " " ++ show_maybe(S.bsearch.find(sorted(), 13)))
47    IO.print(show_val(S.bsearch.array_lower_bound(fill(sorted(), 0, [0 : U32*8n]), 8, 12)) ++ " " ++ show_opt(S.bsearch.array_find(fill(sorted(), 0, [0 : U32*8n]), 8, 4)))
48
49#|24
50#|4 6
51#|3 26
52#|none 31
53#|36 27
54#|64
55#|35 33
56#|64
57#|5 5
58#|5 none