2020-04-29 09:43:49 +08:00
|
|
|
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
|
|
|
|
// See the LICENCE file in the repository root for full licence text.
|
|
|
|
|
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
2024-08-27 15:30:49 +08:00
|
|
|
using System.Diagnostics.CodeAnalysis;
|
2020-04-29 09:43:49 +08:00
|
|
|
using System.Linq;
|
|
|
|
|
|
|
|
namespace osu.Game.Utils
|
|
|
|
{
|
|
|
|
/// <summary>
|
2020-05-02 07:33:33 +08:00
|
|
|
/// Represents a tracking component used for whether a specific time instant falls into any of the provided periods.
|
2020-04-29 09:43:49 +08:00
|
|
|
/// </summary>
|
|
|
|
public class PeriodTracker
|
|
|
|
{
|
2020-05-02 07:33:33 +08:00
|
|
|
private readonly List<Period> periods;
|
2020-04-29 09:43:49 +08:00
|
|
|
private int nearestIndex;
|
|
|
|
|
2020-05-02 07:33:33 +08:00
|
|
|
public PeriodTracker(IEnumerable<Period> periods)
|
2020-04-29 09:43:49 +08:00
|
|
|
{
|
2020-05-02 07:33:33 +08:00
|
|
|
this.periods = periods.OrderBy(period => period.Start).ToList();
|
2020-04-29 09:43:49 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Whether the provided time is in any of the added periods.
|
|
|
|
/// </summary>
|
2020-05-02 07:33:33 +08:00
|
|
|
/// <param name="time">The time value to check.</param>
|
2024-08-27 15:30:49 +08:00
|
|
|
public bool IsInAny(double time) => IsInAny(time, out _);
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Whether the provided time is in any of the added periods.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="time">The time value to check.</param>
|
|
|
|
/// <param name="period">The period which matched.</param>
|
|
|
|
public bool IsInAny(double time, [NotNullWhen(true)] out Period? period)
|
2020-04-29 09:43:49 +08:00
|
|
|
{
|
2024-08-27 15:30:49 +08:00
|
|
|
period = null;
|
|
|
|
|
2020-04-29 09:43:49 +08:00
|
|
|
if (periods.Count == 0)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (time > periods[nearestIndex].End)
|
|
|
|
{
|
|
|
|
while (time > periods[nearestIndex].End && nearestIndex < periods.Count - 1)
|
|
|
|
nearestIndex++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
while (time < periods[nearestIndex].Start && nearestIndex > 0)
|
|
|
|
nearestIndex--;
|
|
|
|
}
|
|
|
|
|
|
|
|
var nearest = periods[nearestIndex];
|
2024-08-27 15:30:49 +08:00
|
|
|
bool isInAny = time >= nearest.Start && time <= nearest.End;
|
|
|
|
|
|
|
|
if (isInAny)
|
|
|
|
{
|
|
|
|
period = nearest;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
2020-04-29 09:43:49 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-02 07:33:33 +08:00
|
|
|
public readonly struct Period
|
2020-04-29 09:43:49 +08:00
|
|
|
{
|
|
|
|
/// <summary>
|
|
|
|
/// The start time of this period.
|
|
|
|
/// </summary>
|
|
|
|
public readonly double Start;
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// The end time of this period.
|
|
|
|
/// </summary>
|
|
|
|
public readonly double End;
|
|
|
|
|
2024-08-27 15:30:49 +08:00
|
|
|
public double Duration => End - Start;
|
|
|
|
|
2020-04-29 09:43:49 +08:00
|
|
|
public Period(double start, double end)
|
|
|
|
{
|
|
|
|
if (start >= end)
|
2020-05-02 07:33:33 +08:00
|
|
|
throw new ArgumentException($"Invalid period provided, {nameof(start)} must be less than {nameof(end)}");
|
2020-04-29 09:43:49 +08:00
|
|
|
|
|
|
|
Start = start;
|
|
|
|
End = end;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|