Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions Sources/LoopAlgorithm/SampleValue.swift
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,13 @@ public extension Sequence where Element: TimelineValue {
public extension RandomAccessCollection where Element: TimelineValue, Index == Int {
func filterDateRange(_ startDate: Date?, _ endDate: Date?) -> [Element] {
guard !isEmpty else { return [] }
// This binary-search filter is only correct when the elements are sorted
// ascending by startDate. Catch contract violations in debug builds; the
// check is compiled out of release builds, so there is no runtime cost.
assert(
zip(self, dropFirst()).allSatisfy { $0.startDate <= $1.startDate },
"filterDateRange requires elements sorted ascending by startDate"
)
// Lower bound: first index where element.endDate >= startDate
var lo = startIndex
if let startDate {
Expand Down
Loading