using System.Collections.Generic; using System.Linq; using osu.Framework.Lists; namespace osu.Game.Beatmaps.ControlPoints { public class ControlPointInfo { public readonly SortedList TimingPoints = new SortedList(Comparer.Default); public readonly SortedList DifficultyPoints = new SortedList(Comparer.Default); public readonly SortedList SoundPoints = new SortedList(Comparer.Default); public readonly SortedList EffectPoints = new SortedList(Comparer.Default); /// /// Finds the difficulty control point that is active at . /// /// The time to find the difficulty control point at. /// The difficulty control point. public DifficultyControlPoint DifficultyPointAt(double time) => binarySearch(DifficultyPoints, time); /// /// Finds the effect control point that is active at . /// /// The time to find the effect control point at. /// The effect control point. public EffectControlPoint EffectPointAt(double time) => binarySearch(EffectPoints, time); /// /// Finds the sound control point that is active at . /// /// The time to find the sound control point at. /// The sound control point. public SoundControlPoint SoundPointAt(double time) => binarySearch(SoundPoints, time); /// /// Finds the timing control point that is active at . /// /// The time to find the timing control point at. /// The timing control point. public TimingControlPoint TimingPointAt(double time) => binarySearch(TimingPoints, time); /// /// Finds the maximum BPM represented by any timing control point. /// public double BPMMaximum => 60000 / (TimingPoints.OrderBy(c => c.BeatLength).FirstOrDefault() ?? new TimingControlPoint()).BeatLength; /// /// Finds the minimum BPM represented by any timing control point. /// public double BPMMinimum => 60000 / (TimingPoints.OrderByDescending(c => c.BeatLength).FirstOrDefault() ?? new TimingControlPoint()).BeatLength; /// /// Finds the mode BPM (most common BPM) represented by the control points. /// public double BPMMode => 60000 / (TimingPoints.GroupBy(c => c.BeatLength).OrderByDescending(grp => grp.Count()).FirstOrDefault()?.FirstOrDefault() ?? new TimingControlPoint()).BeatLength; private T binarySearch(SortedList list, double time) where T : ControlPoint, new() { if (list.Count == 0) return new T(); if (time < list[0].Time) return new T(); int index = list.BinarySearch(new T() { Time = time }); // Check if we've found an exact match (t == time) if (index >= 0) return list[index]; index = ~index; if (index == list.Count) return list[list.Count - 1]; return list[index - 1]; } } }