-
Notifications
You must be signed in to change notification settings - Fork 23
/
Curve.swift
70 lines (61 loc) · 2.06 KB
/
Curve.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift Collections open source project
//
// Copyright (c) 2021 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
//
//===----------------------------------------------------------------------===//
public struct Curve<Point> {
public internal(set) var points: [Point]
public init(_ points: [Point] = []) {
self.points = points
}
}
extension Curve: CustomStringConvertible {
public var description: String {
"Curve<\(Point.self)> with \(points.count) points"
}
}
extension Curve: Decodable where Point: Decodable {
public init(from decoder: Decoder) throws {
let container = try decoder.singleValueContainer()
self.points = try container.decode([Point].self)
}
}
extension Curve: Encodable where Point: Encodable {
public func encode(to encoder: Encoder) throws {
var container = encoder.singleValueContainer()
try container.encode(points)
}
}
extension Curve {
public func map<T>(_ transform: (Point) -> T) -> Curve<T> {
return Curve<T>(points.map(transform))
}
}
extension Curve where Point == Measurement {
var sizeRange: ClosedRange<Size>? {
guard !points.isEmpty else { return nil }
let min = points.min(by: { $0.size < $1.size })
let max = points.max(by: { $0.size < $1.size })
return min!.size ... max!.size
}
var timeRange: ClosedRange<Time>? {
guard !points.isEmpty else { return nil }
let min = points.min(by: { $0.time < $1.time })
let max = points.max(by: { $0.time < $1.time })
return min!.time ... max!.time
}
}
extension BenchmarkResults {
public func curve(id: TaskID, statistic: Sample.Statistic) -> Curve<Measurement> {
let points: [Measurement] = self[id: id].compactMap { (size, sample) in
guard let time = sample[statistic] else { return nil }
return Measurement(size: size, time: time)
}
return Curve(points)
}
}