forked from swiftlang/swift-corelibs-foundation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgressFraction.swift
276 lines (233 loc) · 11.2 KB
/
ProgressFraction.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2016 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See http://swift.org/LICENSE.txt for license information
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
// Implementation note: This file is included in both the framework and the test bundle, in order for us to be able to test it directly. Once @testable support works for Linux we may be able to use it from the framework instead.
internal struct _ProgressFraction : Equatable, CustomDebugStringConvertible {
var completed : Int64
var total : Int64
private(set) var overflowed : Bool
init() {
completed = 0
total = 0
overflowed = false
}
init(double: Double, overflow: Bool = false) {
if double == 0 {
self.completed = 0
self.total = 1
} else if double == 1 {
self.completed = 1
self.total = 1
}
(self.completed, self.total) = _ProgressFraction._fromDouble(double)
self.overflowed = overflow
}
init(completed: Int64, total: Int64) {
self.completed = completed
self.total = total
self.overflowed = false
}
// ----
internal mutating func simplify() {
if self.total == 0 {
return
}
(self.completed, self.total) = _ProgressFraction._simplify(completed, total)
}
internal func simplified() -> _ProgressFraction {
let simplified = _ProgressFraction._simplify(completed, total)
return _ProgressFraction(completed: simplified.0, total: simplified.1)
}
static private func _math(lhs: _ProgressFraction, rhs: _ProgressFraction, whichOperator: (_ lhs : Double, _ rhs : Double) -> Double, whichOverflow : (_ lhs: Int64, _ rhs: Int64) -> (Int64, overflow: Bool)) -> _ProgressFraction {
// Mathematically, it is nonsense to add or subtract something with a denominator of 0. However, for the purposes of implementing Progress' fractions, we just assume that a zero-denominator fraction is "weightless" and return the other value. We still need to check for the case where they are both nonsense though.
precondition(!(lhs.total == 0 && rhs.total == 0), "Attempt to add or subtract invalid fraction")
guard lhs.total != 0 else {
return rhs
}
guard rhs.total != 0 else {
return lhs
}
guard !lhs.overflowed && !rhs.overflowed else {
// If either has overflowed already, we preserve that
return _ProgressFraction(double: whichOperator(lhs.fractionCompleted, rhs.fractionCompleted), overflow: true)
}
if let lcm = _leastCommonMultiple(lhs.total, rhs.total) {
let result = whichOverflow(lhs.completed * (lcm / lhs.total), rhs.completed * (lcm / rhs.total))
if result.overflow {
return _ProgressFraction(double: whichOperator(lhs.fractionCompleted, rhs.fractionCompleted), overflow: true)
} else {
return _ProgressFraction(completed: result.0, total: lcm)
}
} else {
// Overflow - simplify and then try again
let lhsSimplified = lhs.simplified()
let rhsSimplified = rhs.simplified()
if let lcm = _leastCommonMultiple(lhsSimplified.total, rhsSimplified.total) {
let result = whichOverflow(lhsSimplified.completed * (lcm / lhsSimplified.total), rhsSimplified.completed * (lcm / rhsSimplified.total))
if result.overflow {
// Use original lhs/rhs here
return _ProgressFraction(double: whichOperator(lhs.fractionCompleted, rhs.fractionCompleted), overflow: true)
} else {
return _ProgressFraction(completed: result.0, total: lcm)
}
} else {
// Still overflow
return _ProgressFraction(double: whichOperator(lhs.fractionCompleted, rhs.fractionCompleted), overflow: true)
}
}
}
static internal func +(lhs: _ProgressFraction, rhs: _ProgressFraction) -> _ProgressFraction {
return _math(lhs: lhs, rhs: rhs, whichOperator: +, whichOverflow: { $0.addingReportingOverflow($1) })
}
static internal func -(lhs: _ProgressFraction, rhs: _ProgressFraction) -> _ProgressFraction {
return _math(lhs: lhs, rhs: rhs, whichOperator: -, whichOverflow: { $0.subtractingReportingOverflow($1) })
}
static internal func *(lhs: _ProgressFraction, rhs: _ProgressFraction) -> _ProgressFraction {
guard !lhs.overflowed && !rhs.overflowed else {
// If either has overflowed already, we preserve that
return _ProgressFraction(double: rhs.fractionCompleted * rhs.fractionCompleted, overflow: true)
}
let newCompleted = lhs.completed.multipliedReportingOverflow(by: rhs.completed)
let newTotal = lhs.total.multipliedReportingOverflow(by: rhs.total)
if newCompleted.overflow || newTotal.overflow {
// Try simplifying, then do it again
let lhsSimplified = lhs.simplified()
let rhsSimplified = rhs.simplified()
let newCompletedSimplified = lhsSimplified.completed.multipliedReportingOverflow(by: rhsSimplified.completed)
let newTotalSimplified = lhsSimplified.total.multipliedReportingOverflow(by: rhsSimplified.total)
if newCompletedSimplified.overflow || newTotalSimplified.overflow {
// Still overflow
return _ProgressFraction(double: lhs.fractionCompleted * rhs.fractionCompleted, overflow: true)
} else {
return _ProgressFraction(completed: newCompletedSimplified.0, total: newTotalSimplified.0)
}
} else {
return _ProgressFraction(completed: newCompleted.0, total: newTotal.0)
}
}
static internal func /(lhs: _ProgressFraction, rhs: Int64) -> _ProgressFraction {
guard !lhs.overflowed else {
// If lhs has overflowed, we preserve that
return _ProgressFraction(double: lhs.fractionCompleted / Double(rhs), overflow: true)
}
let newTotal = lhs.total.multipliedReportingOverflow(by: rhs)
if newTotal.overflow {
let simplified = lhs.simplified()
let newTotalSimplified = simplified.total.multipliedReportingOverflow(by: rhs)
if newTotalSimplified.overflow {
// Still overflow
return _ProgressFraction(double: lhs.fractionCompleted / Double(rhs), overflow: true)
} else {
return _ProgressFraction(completed: lhs.completed, total: newTotalSimplified.0)
}
} else {
return _ProgressFraction(completed: lhs.completed, total: newTotal.0)
}
}
static internal func ==(lhs: _ProgressFraction, rhs: _ProgressFraction) -> Bool {
if lhs.isNaN || rhs.isNaN {
// NaN fractions are never equal
return false
} else if lhs.completed == rhs.completed && lhs.total == rhs.total {
return true
} else if lhs.total == rhs.total {
// Direct comparison of numerator
return lhs.completed == rhs.completed
} else if lhs.completed == 0 && rhs.completed == 0 {
return true
} else if lhs.completed == lhs.total && rhs.completed == rhs.total {
// Both finished (1)
return true
} else if (lhs.completed == 0 && rhs.completed != 0) || (lhs.completed != 0 && rhs.completed == 0) {
// One 0, one not 0
return false
} else {
// Cross-multiply
let left = lhs.completed.multipliedReportingOverflow(by: rhs.total)
let right = lhs.total.multipliedReportingOverflow(by: rhs.completed)
if !left.overflow && !right.overflow {
if left.0 == right.0 {
return true
}
} else {
// Try simplifying then cross multiply again
let lhsSimplified = lhs.simplified()
let rhsSimplified = rhs.simplified()
let leftSimplified = lhsSimplified.completed.multipliedReportingOverflow(by: rhsSimplified.total)
let rightSimplified = lhsSimplified.total.multipliedReportingOverflow(by: rhsSimplified.completed)
if !leftSimplified.overflow && !rightSimplified.overflow {
if leftSimplified.0 == rightSimplified.0 {
return true
}
} else {
// Ok... fallback to doubles. This doesn't use an epsilon
return lhs.fractionCompleted == rhs.fractionCompleted
}
}
}
return false
}
// ----
internal var isIndeterminate : Bool {
return completed < 0 || total < 0 || (completed == 0 && total == 0)
}
internal var isFinished : Bool {
return ((completed >= total) && completed > 0 && total > 0) || (completed > 0 && total == 0)
}
internal var fractionCompleted : Double {
if isIndeterminate {
// Return something predictable
return 0.0
} else if total == 0 {
// When there is nothing to do, you're always done
return 1.0
} else {
return Double(completed) / Double(total)
}
}
internal var isNaN : Bool {
return total == 0
}
internal var debugDescription : String {
return "\(completed) / \(total) (\(fractionCompleted))"
}
// ----
private static func _fromDouble(_ d : Double) -> (Int64, Int64) {
// This simplistic algorithm could someday be replaced with something better.
// Basically - how many 1/Nths is this double?
// And we choose to use 131072 for N
let denominator : Int64 = 131072
let numerator = Int64(d / (1.0 / Double(denominator)))
return (numerator, denominator)
}
private static func _greatestCommonDivisor(_ inA : Int64, _ inB : Int64) -> Int64 {
// This is Euclid's algorithm. There are faster ones, like Knuth, but this is the simplest one for now.
var a = inA
var b = inB
repeat {
let tmp = b
b = a % b
a = tmp
} while (b != 0)
return a
}
private static func _leastCommonMultiple(_ a : Int64, _ b : Int64) -> Int64? {
// This division always results in an integer value because gcd(a,b) is a divisor of a.
// lcm(a,b) == (|a|/gcd(a,b))*b == (|b|/gcd(a,b))*a
let result = (a / _greatestCommonDivisor(a, b)).multipliedReportingOverflow(by: b)
if result.overflow {
return nil
} else {
return result.0
}
}
private static func _simplify(_ n : Int64, _ d : Int64) -> (Int64, Int64) {
let gcd = _greatestCommonDivisor(n, d)
return (n / gcd, d / gcd)
}
}