aboutsummaryrefslogtreecommitdiff
path: root/extensions-builtin/hypertile/hypertile.py
diff options
context:
space:
mode:
authorKohaku-Blueleaf <59680068+KohakuBlueleaf@users.noreply.github.com>2023-12-02 17:00:09 +0800
committerKohaku-Blueleaf <59680068+KohakuBlueleaf@users.noreply.github.com>2023-12-02 17:00:09 +0800
commit110485d5bb511ab01ac3d890f1deca0502f4c7db (patch)
tree4335df77c422985e28b30c67ea846ca71e5ef1d7 /extensions-builtin/hypertile/hypertile.py
parent3d341ebc7dcb44df3b4c013b3805c08d8a35e24a (diff)
parent0bb6e00ba3c6775b17b3449c454c6efabb1bdad1 (diff)
Merge branch 'dev' into test-fp8
Diffstat (limited to 'extensions-builtin/hypertile/hypertile.py')
-rw-r--r--extensions-builtin/hypertile/hypertile.py24
1 files changed, 15 insertions, 9 deletions
diff --git a/extensions-builtin/hypertile/hypertile.py b/extensions-builtin/hypertile/hypertile.py
index feb02fd2..0f40e2d3 100644
--- a/extensions-builtin/hypertile/hypertile.py
+++ b/extensions-builtin/hypertile/hypertile.py
@@ -6,7 +6,6 @@ Original author: @tfernd Github: https://github.com/tfernd/HyperTile
from __future__ import annotations
-import functools
from dataclasses import dataclass
from typing import Callable
@@ -189,20 +188,27 @@ DEPTH_LAYERS_XL = {
RNG_INSTANCE = random.Random()
-
-def random_divisor(value: int, min_value: int, /, max_options: int = 1) -> int:
+@cache
+def get_divisors(value: int, min_value: int, /, max_options: int = 1) -> list[int]:
"""
- Returns a random divisor of value that
+ Returns divisors of value that
x * min_value <= value
- if max_options is 1, the behavior is deterministic
+ in big -> small order, amount of divisors is limited by max_options
"""
+ max_options = max(1, max_options) # at least 1 option should be returned
min_value = min(min_value, value)
-
- # All big divisors of value (inclusive)
divisors = [i for i in range(min_value, value + 1) if value % i == 0] # divisors in small -> big order
-
ns = [value // i for i in divisors[:max_options]] # has at least 1 element # big -> small order
+ return ns
+
+def random_divisor(value: int, min_value: int, /, max_options: int = 1) -> int:
+ """
+ Returns a random divisor of value that
+ x * min_value <= value
+ if max_options is 1, the behavior is deterministic
+ """
+ ns = get_divisors(value, min_value, max_options=max_options) # get cached divisors
idx = RNG_INSTANCE.randint(0, len(ns) - 1)
return ns[idx]
@@ -212,7 +218,7 @@ def set_hypertile_seed(seed: int) -> None:
RNG_INSTANCE.seed(seed)
-@functools.cache
+@cache
def largest_tile_size_available(width: int, height: int) -> int:
"""
Calculates the largest tile size available for a given width and height