44 lines
1.1 KiB
Python
44 lines
1.1 KiB
Python
import enum
|
|
from typing import Self
|
|
|
|
from xmet.list import nearest
|
|
|
|
class SeriesIntersection(enum.Enum):
|
|
GREATER = -1
|
|
EQUAL = 0
|
|
LESSER = 1
|
|
|
|
@staticmethod
|
|
def cmp(a, b) -> Self:
|
|
if a > b:
|
|
return SeriesIntersection.GREATER
|
|
elif a == b:
|
|
return SeriesIntersection.EQUAL
|
|
elif a < b:
|
|
return SeriesIntersection.LESSER
|
|
|
|
class Series(dict):
|
|
def __init__(self):
|
|
super().__init__()
|
|
|
|
def neighbors(self, series: Self):
|
|
return nearest(sorted(self.keys(), reverse=True),
|
|
sorted(series.keys(), reverse=True))
|
|
|
|
def intersect(self,
|
|
series: Self,
|
|
intersection: SeriesIntersection,
|
|
start: float=None) -> tuple[float]:
|
|
pairs = self.neighbors(series)
|
|
|
|
for pair in pairs:
|
|
v1, v2 = self[pair[0]], series[pair[1]]
|
|
|
|
if start is not None:
|
|
if pair[0] > start:
|
|
continue
|
|
|
|
sign = SeriesIntersection.cmp(v1, v2)
|
|
|
|
if sign is intersection:
|
|
return v1, pair[0]
|