/// <inheritdoc/> public override DateTime?GetTimeOfNearestMessage(DateTime time, NearestMessageType snappingBehavior) { int index = IndexHelper.GetIndexForTime(time, this.data.Count, (idx) => this.data[idx].OriginatingTime, snappingBehavior); return((index >= 0) ? this.data[index].OriginatingTime : null); }
/// <summary> /// Gets the time of the nearest message to a specified time, on a specified stream. /// </summary> /// <param name="streamSource">The stream source specifying the stream of interest.</param> /// <param name="time">The time to find the nearest message to.</param> /// <param name="nearestMessageType">The type of nearest message to find.</param> /// <returns>The time of the nearest message, if one is found or null otherwise.</returns> internal DateTime?GetTimeOfNearestMessage(StreamSource streamSource, DateTime time, NearestMessageType nearestMessageType) => this.GetStreamProviderOrDefault(streamSource.StreamName) .GetTimeOfNearestMessage(time, nearestMessageType);
/// <inheritdoc/> public abstract DateTime?GetTimeOfNearestMessage(DateTime time, NearestMessageType snappingBehavior);
/// <summary> /// Gets the index id corresponding to a specified time for /// a collection of indices that are ordered by time. /// </summary> /// <param name="dateTime">The time to find the index for.</param> /// <param name="count">The number of items in the index collection.</param> /// <param name="timeAtIndex">A function that returns the time for a given index id.</param> /// <param name="snappingBehavior">Timeline snapping behaviors.</param> /// <returns>The index id closest to the specified time, using the specified interpolation style.</returns> public static int GetIndexForTime(DateTime dateTime, int count, Func <int, DateTime> timeAtIndex, NearestMessageType snappingBehavior = NearestMessageType.Nearest) { // If there's only one point in the index, then return its index if (count == 1) { return(0); } // Perform a binary search // If no exact match, lo and hi indicate ticks that // are right before and right after the time we're looking for. SearchResult result = SearchIndex(dateTime, count, timeAtIndex); if (result.ExactMatchFound) { return(result.ExactIndex); } return(snappingBehavior switch { NearestMessageType.Previous => result.LowIndex, NearestMessageType.Next => result.HighIndex, // o/w return the index of whichever point is closest to the current time _ => (timeAtIndex(result.HighIndex) - dateTime) < (dateTime - timeAtIndex(result.LowIndex)) ? result.HighIndex : result.LowIndex, });