// Copyright (C) 2024 Intro-Skipper contributors // SPDX-License-Identifier: GPL-3.0-only. using System; using System.Collections.Generic; namespace IntroSkipper.Data; /// /// Time range helpers. /// public static class TimeRangeHelpers { /// /// Finds the longest contiguous time range. /// /// Sorted timestamps to search. /// Maximum distance permitted between contiguous timestamps. /// The longest contiguous time range (if one was found), or null (if none was found). public static TimeRange? FindContiguous(double[] times, double maximumDistance) { if (times.Length == 0) { return null; } Array.Sort(times); var ranges = new List(); var currentRange = new TimeRange(times[0], times[0]); // For all provided timestamps, check if it is contiguous with its neighbor. for (var i = 0; i < times.Length - 1; i++) { var current = times[i]; var next = times[i + 1]; if (next - current <= maximumDistance) { currentRange.End = next; continue; } ranges.Add(new TimeRange(currentRange)); currentRange = new TimeRange(next, next); } // Find and return the longest contiguous range. ranges.Sort(); return (ranges.Count > 0) ? ranges[0] : null; } }