2021-08-26 01:28:26 +02:00
|
|
|
import collections
|
|
|
|
from typing import Iterable, Tuple
|
|
|
|
|
|
|
|
|
|
|
|
class KoboldStoryRegister(collections.OrderedDict):
|
|
|
|
'''
|
|
|
|
Complexity-optimized class for keeping track of story chunks
|
|
|
|
'''
|
|
|
|
|
|
|
|
def __init__(self, sequence: Iterable[Tuple[int, str]] = ()):
|
|
|
|
super().__init__(sequence)
|
|
|
|
self.__next_id: int = len(sequence)
|
|
|
|
|
|
|
|
def append(self, v: str) -> None:
|
|
|
|
self[self.__next_id] = v
|
|
|
|
self.increment_id()
|
|
|
|
|
|
|
|
def pop(self) -> str:
|
|
|
|
return self.popitem()[1]
|
|
|
|
|
|
|
|
def get_first_key(self) -> int:
|
2022-03-05 16:31:28 +01:00
|
|
|
if len(self) == 0:
|
|
|
|
return -1
|
|
|
|
else:
|
|
|
|
return next(iter(self))
|
2021-08-26 01:28:26 +02:00
|
|
|
|
|
|
|
def get_last_key(self) -> int:
|
2022-03-05 16:31:28 +01:00
|
|
|
if len(self) == 0:
|
|
|
|
return -1
|
|
|
|
else:
|
|
|
|
return next(reversed(self))
|
2021-08-26 01:28:26 +02:00
|
|
|
|
|
|
|
def __getitem__(self, k: int) -> str:
|
|
|
|
return super().__getitem__(k)
|
|
|
|
|
|
|
|
def __setitem__(self, k: int, v: str) -> None:
|
|
|
|
return super().__setitem__(k, v)
|
|
|
|
|
|
|
|
def increment_id(self) -> None:
|
|
|
|
self.__next_id += 1
|
|
|
|
|
|
|
|
def get_next_id(self) -> int:
|
|
|
|
return self.__next_id
|
|
|
|
|
|
|
|
def set_next_id(self, x: int) -> None:
|
|
|
|
self.__next_id = x
|