import Foundation /// History remains bounded; a rolling plot can draw one outgoing vertex beyond its left edge. nonisolated struct PanelCurveFrame: Equatable { let dates: [Date] let series: [[Double?]] let intervals: [TimeInterval] let boundaryDates: Set /// Only adjacent samples from the same timeline can slide into view. func follows(_ old: Self, maximumGap: TimeInterval) -> Bool { guard old.dates.count >= 2, let before = old.dates.last, let end = dates.last, let begin = old.dates.first, begin < before, end > before, !boundaryDates.contains(end), end.timeIntervalSince(before) <= PanelCurveGeometry.allowedGap(at: dates.count - 1, intervals: intervals, fallback: maximumGap) else { return false } return dates.dropLast().elementsEqual(old.dates) || (dates.count == old.dates.count && dates.dropLast().elementsEqual(old.dates.dropFirst())) } func includingOutgoing(from old: Self) -> Self { guard dates.first != old.dates.first, let outgoing = old.dates.first else { return self } return Self(dates: [outgoing] + dates, series: series.enumerated().map { index, values in [old.series.indices.contains(index) ? old.series[index].first ?? nil : nil] + values }, intervals: [old.intervals.first ?? 2] + intervals, boundaryDates: boundaryDates) } struct Domain { let begin: TimeInterval let end: TimeInterval func position(_ date: Date) -> Double { end > begin ? (date.timeIntervalSinceReferenceDate - begin) / (end - begin) : 1 } } static func domain(begin: Date, end: Date) -> Domain { Domain(begin: begin.timeIntervalSinceReferenceDate, end: end.timeIntervalSinceReferenceDate) } }