using System;
using System.Collections.Generic;
namespace ConfusedPolarBear.Plugin.IntroSkipper;
#pragma warning disable CA1036 // Override methods on comparable types
///
/// Range of contiguous time.
///
public class TimeRange : IComparable
{
///
/// Initializes a new instance of the class.
///
public TimeRange()
{
Start = 0;
End = 0;
}
///
/// Initializes a new instance of the class.
///
/// Time range start.
/// Time range end.
public TimeRange(double start, double end)
{
Start = start;
End = end;
}
///
/// Initializes a new instance of the class.
///
/// Original TimeRange.
public TimeRange(TimeRange original)
{
Start = original.Start;
End = original.End;
}
///
/// Gets or sets the time range start (in seconds).
///
public double Start { get; set; }
///
/// Gets or sets the time range end (in seconds).
///
public double End { get; set; }
///
/// Gets the duration of this time range (in seconds).
///
public double Duration => End - Start;
///
/// Compare TimeRange durations.
///
/// Object to compare with.
/// int.
public int CompareTo(object? obj)
{
if (!(obj is TimeRange tr))
{
throw new ArgumentException("obj must be a TimeRange");
}
return tr.Duration.CompareTo(Duration);
}
///
/// Tests if this TimeRange object intersects the provided TimeRange.
///
/// Second TimeRange object to test.
/// true if tr intersects the current TimeRange, false otherwise.
public bool Intersects(TimeRange tr)
{
return
(Start < tr.Start && tr.Start < End) ||
(Start < tr.End && tr.End < End);
}
}
#pragma warning restore CA1036
///
/// 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;
}
}