aboutsummaryrefslogtreecommitdiff
path: root/gen/prop/lib.py
diff options
context:
space:
mode:
authorThomas Voss <mail@thomasvoss.com> 2024-04-28 09:11:55 +0200
committerThomas Voss <mail@thomasvoss.com> 2024-04-28 09:11:55 +0200
commit5b4c1d02ce9108be74e60e9993254a71c5fa9318 (patch)
tree770772eebb7e3c6f80adfbd9e5e2b0a57a38fd08 /gen/prop/lib.py
parent4ea2dd117e656f950c41f9954bd593c313e34ee2 (diff)
Move out functions into lib.py
Diffstat (limited to 'gen/prop/lib.py')
-rw-r--r--gen/prop/lib.py47
1 files changed, 47 insertions, 0 deletions
diff --git a/gen/prop/lib.py b/gen/prop/lib.py
new file mode 100644
index 0000000..b4c7e1e
--- /dev/null
+++ b/gen/prop/lib.py
@@ -0,0 +1,47 @@
+import functools
+from typing import Generator
+
+def chunks[T](xs: list[T], n: int) -> Generator[list[T], None, None]:
+ for i in range(0, len(xs), n):
+ yield xs[i:i + n]
+
+def powers_of_2() -> Generator[int, None, None]:
+ i = 0
+ while True:
+ yield 2 ** i
+ i += 1
+
+def columns(n: int) -> int:
+ xs = list(set(functools.reduce(list.__add__, (
+ [i, n // i] for i in range(1, int(n ** 0.5) + 1) if n % i == 0)
+ )))
+ for x in sorted(xs, reverse=True):
+ y = 5
+ y += x * 5
+ y += x - 1
+ if y <= 80:
+ return x
+
+ raise ValueError
+
+def isize(x: int) -> int:
+ if x < 256:
+ return 1
+ if x < 65535:
+ return 2
+ if x < 4294967295:
+ return 3
+ if x < 18446744073709551615:
+ return 4
+ raise ValueError
+
+def typename(x: int) -> str:
+ if x < 256:
+ return "uint8_t"
+ if x < 65535:
+ return "uint16_t"
+ if x < 4294967295:
+ return "uint32_t"
+ if x < 18446744073709551615:
+ return "uint64_t"
+ raise ValueError