Skip to content

Commit

Permalink
Complete type annotations of ported 'BigInt' class
Browse files Browse the repository at this point in the history
  • Loading branch information
lycantropos committed Apr 5, 2023
1 parent 79eb7e9 commit e17e9c6
Showing 1 changed file with 12 additions and 10 deletions.
22 changes: 12 additions & 10 deletions voronoi/big_int.py
@@ -1,3 +1,5 @@
from __future__ import annotations

import ctypes
from math import (copysign,
inf,
Expand All @@ -24,10 +26,10 @@ def __init__(self, sign: int, digits: List[int]) -> None:

__repr__ = generate_repr(__init__)

def __abs__(self) -> 'BigInt':
def __abs__(self) -> BigInt:
return BigInt(abs(self.sign), self.digits)

def __add__(self, other: 'BigInt') -> 'BigInt':
def __add__(self, other: BigInt) -> BigInt:
result = BigInt(0, [])
result._add(self, other)
return result
Expand All @@ -50,21 +52,21 @@ def __float__(self) -> float:
except OverflowError:
return copysign(inf, mantissa)

def __mul__(self, other: 'BigInt') -> 'BigInt':
def __mul__(self, other: BigInt) -> BigInt:
result = BigInt(0, [])
result._multiply(self, other)
return result

def __neg__(self) -> 'BigInt':
def __neg__(self) -> BigInt:
return BigInt(-self.sign, self.digits[:])

def __sub__(self, other: 'BigInt') -> 'BigInt':
def __sub__(self, other: BigInt) -> BigInt:
result = BigInt(0, [])
result._subtract(self, other)
return result

@classmethod
def from_int32(cls, value: int) -> 'BigInt':
def from_int32(cls, value: int) -> BigInt:
if value > 0:
sign, digits = 1, [_to_uint32(value)]
elif value < 0:
Expand All @@ -74,7 +76,7 @@ def from_int32(cls, value: int) -> 'BigInt':
return cls(sign, digits)

@classmethod
def from_int64(cls, value: int) -> 'BigInt':
def from_int64(cls, value: int) -> BigInt:
if value > 0:
sign, digits = 1, [_to_uint32(value)]
value >>= 32
Expand Down Expand Up @@ -110,7 +112,7 @@ def frexp(self) -> Tuple[float, int]:
mantissa = -mantissa
return mantissa, exponent

def _add(self, left: 'BigInt', right: 'BigInt') -> None:
def _add(self, left: BigInt, right: BigInt) -> None:
if not left.sign:
self.sign, self.digits = right.sign, right.digits[:]
return
Expand Down Expand Up @@ -145,7 +147,7 @@ def _add_digits(self,
if cursor and len(self.digits) < MAX_DIGITS_COUNT:
self.digits.append(cursor)

def _multiply(self, left: 'BigInt', right: 'BigInt') -> None:
def _multiply(self, left: BigInt, right: BigInt) -> None:
if not left.sign or not right.sign:
return
self._multiply_digits(left.digits, right.digits)
Expand Down Expand Up @@ -174,7 +176,7 @@ def _multiply_digits(self,
if current_digit and len(self.digits) < MAX_DIGITS_COUNT:
self.digits.append(_to_uint32(current_digit))

def _subtract(self, left: 'BigInt', right: 'BigInt') -> None:
def _subtract(self, left: BigInt, right: BigInt) -> None:
if not left.sign:
self.sign, self.digits = -right.sign, right.digits[:]
return
Expand Down

0 comments on commit e17e9c6

Please sign in to comment.