diff --git a/django_ltree/paths.py b/django_ltree/paths.py index ffeef42..bbd9597 100644 --- a/django_ltree/paths.py +++ b/django_ltree/paths.py @@ -32,26 +32,27 @@ def __next__(self): next = __next__ - def guess_the_label_size(self, path_size: int, combination_size: int) -> int: + @staticmethod + def guess_the_label_size(path_size: int, combination_size: int): + if path_size == 0: + return 1 + # The theoritical limit for this at the time of writing is 2_538_557_185_841_324_496 (python 3.12.2) - calculated_path_size = 0 + calculated_path_size = +0 # The theoritical limit for this at the time of writing is 32 (python 3.12.2) label_size = 0 # THIS IS AN VERY IMPORTANT CHECK last = 0 - while True: + while calculated_path_size < path_size: possible_cominations = math.comb(combination_size, label_size) + if last > possible_cominations: raise ValueError("We approached the limit of `math.comb`") last = possible_cominations calculated_path_size += possible_cominations - - if calculated_path_size > path_size and label_size != 0: - break - label_size += 1 return label_size