Skip to content

Commit

Permalink
Reorganized class order
Browse files Browse the repository at this point in the history
  • Loading branch information
eprbell committed Jun 23, 2024
1 parent a103e6e commit 9a46fb5
Showing 1 changed file with 39 additions and 39 deletions.
78 changes: 39 additions & 39 deletions src/rp2/abstract_accounting_method.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,6 @@
from rp2.rp2_error import RP2RuntimeError, RP2TypeError


class AbstractAccountingMethodIterator:
def __next__(self) -> InTransaction:
raise NotImplementedError("abstract function")


class AcquiredLotAndAmount(NamedTuple):
acquired_lot: InTransaction
amount: RP2Decimal
Expand All @@ -44,6 +39,45 @@ class AcquiredLotHeapSortKey(NamedTuple):
internal_id_int: int


class AbstractAccountingMethodIterator:
def __next__(self) -> InTransaction:
raise NotImplementedError("abstract function")


class ListAccountingMethodIterator(AbstractAccountingMethodIterator):
def __init__(self, acquired_lot_list: List[InTransaction], from_index: int, to_index: int, order_type: AcquiredLotCandidatesOrder) -> None:
self.__acquired_lot_list = acquired_lot_list
self.__start_index = from_index if order_type == AcquiredLotCandidatesOrder.OLDER_TO_NEWER else to_index
self.__end_index = to_index if order_type == AcquiredLotCandidatesOrder.OLDER_TO_NEWER else from_index
self.__step = 1 if order_type == AcquiredLotCandidatesOrder.OLDER_TO_NEWER else -1
self.__index = self.__start_index
self.__order_type = order_type

def _check_index(self) -> bool:
if self.__order_type == AcquiredLotCandidatesOrder.OLDER_TO_NEWER:
return self.__index <= self.__end_index
return self.__index >= self.__end_index

def __next__(self) -> InTransaction:
result: Optional[InTransaction] = None
while self._check_index():
result = self.__acquired_lot_list[self.__index]
self.__index += self.__step
return result
raise StopIteration(self)


class HeapAccountingMethodIterator(AbstractAccountingMethodIterator):
def __init__(self, acquired_lot_heap: List[Tuple[AcquiredLotHeapSortKey, InTransaction]]) -> None:
self.__acquired_lot_heap = acquired_lot_heap

def __next__(self) -> InTransaction:
while len(self.__acquired_lot_heap) > 0:
_, result = heappop(self.__acquired_lot_heap)
return result
raise StopIteration(self)


class AbstractAcquiredLotCandidates:
def __init__(
self,
Expand Down Expand Up @@ -93,40 +127,6 @@ def __iter__(self) -> AbstractAccountingMethodIterator:
return self._accounting_method._create_accounting_method_iterator(self)


class ListAccountingMethodIterator(AbstractAccountingMethodIterator):
def __init__(self, acquired_lot_list: List[InTransaction], from_index: int, to_index: int, order_type: AcquiredLotCandidatesOrder) -> None:
self.__acquired_lot_list = acquired_lot_list
self.__start_index = from_index if order_type == AcquiredLotCandidatesOrder.OLDER_TO_NEWER else to_index
self.__end_index = to_index if order_type == AcquiredLotCandidatesOrder.OLDER_TO_NEWER else from_index
self.__step = 1 if order_type == AcquiredLotCandidatesOrder.OLDER_TO_NEWER else -1
self.__index = self.__start_index
self.__order_type = order_type

def _check_index(self) -> bool:
if self.__order_type == AcquiredLotCandidatesOrder.OLDER_TO_NEWER:
return self.__index <= self.__end_index
return self.__index >= self.__end_index

def __next__(self) -> InTransaction:
result: Optional[InTransaction] = None
while self._check_index():
result = self.__acquired_lot_list[self.__index]
self.__index += self.__step
return result
raise StopIteration(self)


class HeapAccountingMethodIterator(AbstractAccountingMethodIterator):
def __init__(self, acquired_lot_heap: List[Tuple[AcquiredLotHeapSortKey, InTransaction]]) -> None:
self.__acquired_lot_heap = acquired_lot_heap

def __next__(self) -> InTransaction:
while len(self.__acquired_lot_heap) > 0:
_, result = heappop(self.__acquired_lot_heap)
return result
raise StopIteration(self)


class ListAcquiredLotCandidates(AbstractAcquiredLotCandidates):
pass

Expand Down

0 comments on commit 9a46fb5

Please sign in to comment.