-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathInt.swift
241 lines (237 loc) · 7.19 KB
/
Int.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
// Copyright 2022 Cii
//
// This file is part of Shikishi.
//
// Shikishi is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Shikishi is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Shikishi. If not, see <http://www.gnu.org/licenses/>.
extension Int {
static func ** (lhs: Int, rhs: Int) -> Int {
if lhs == -1 {
return rhs % 2 == 0 ? 1 : -1
} else if lhs == 0 {
return 0
} else if lhs == 1 {
return 1
} else {
return (0..<rhs).reduce(1) { v, _ in v * lhs }
}
}
}
extension Int {
static func gcd(_ m: Int, _ n: Int) -> Int {
n == 0 ? m : gcd(n, m % n)
}
static func lcd(_ m: Int, _ n: Int) -> Int {
let (v, overflow) = m.multipliedReportingOverflow(by: n)
if overflow {
return .max
}
return v / gcd(m, n)
}
static func lcd(_ vs: [Int]) -> Int {
if vs.count == 1 {
return vs[0]
}
var n = lcd(vs[0], vs[1])
for i in 2..<vs.count {
n = lcd(n, vs[i])
}
return n
}
func interval(scale: Int) -> Int {
if scale == 0 {
return self
} else {
let t = (self / scale) * scale
return self - t > scale / 2 ?
t + scale : t
}
}
var factorial: Int {
guard self >= 0 else { fatalError() }
if self < 2 {
return 1
} else {
return (2...self).reduce(1) { $0 * $1 }
}
}
init(_ x: Rational) {
self = x.integralPart
}
enum OverResult {
case int(Int), double(Double)
init(_ v: Double) {
if let v = Int(exactly: v) {
self = .int(v)
} else {
self = .double(v)
}
}
}
static func overAdd(_ lhs: Int, _ rhs: Int) -> OverResult {
let (v, o) = lhs.addingReportingOverflow(rhs)
if o {
return lhs < 0 && rhs < 0 ?
.double(Double(Int.min) + Double(v)) :
.double(Double(Int.max) + Double(v))
} else {
return .int(v)
}
}
static func overDiff(_ lhs: Int, _ rhs: Int) -> OverResult {
overAdd(lhs, -rhs)
}
static func overMulti(_ lhs: Int, _ rhs: Int) -> OverResult {
OverResult(Double(lhs) * Double(rhs))
}
static func overDiv(_ lhs: Int, _ rhs: Int) -> OverResult {
OverResult(Double(lhs) / Double(rhs))
}
static func overMod(_ lhs: Int, _ rhs: Int) -> OverResult {
OverResult(Double(lhs).truncatingRemainder(dividingBy: Double(rhs)))
}
static func overPow(_ lhs: Int, _ rhs: Int) -> OverResult {
OverResult(Double(lhs) ** rhs)
}
var overFactorial: OverResult {
guard self >= 0 else { return .double(.nan) }
if self < 2 {
return .int(1)
} else {
var i = 1
for j in 2...self {
switch Int.overMulti(i, j) {
case .int(let ni): i = ni
case .double: return .double(.factorial(Double(self)))
}
}
return .int(i)
}
}
var overGamma: OverResult {
guard self >= 1 else { return .double(.nan) }
if self < 1 {
return .int(1)
} else {
var i = 1
for j in 2..<self {
switch Int.overMulti(i, j) {
case .int(let ni): i = ni
case .double: return .double(.gamma(Double(self)))
}
}
return .int(i)
}
}
static func binomFromStack(_ n: Int, _ k: Int) -> Int {
let k = Swift.min(k, n - k)
if k == 0 {
return 1
}
return binom(n - 1, k - 1) * n / k
}
static func binom(_ n: Int, _ k: Int) -> Int {
var n = n, k = k
var stack = Stack<(Int, Int)>()
while true {
k = Swift.min(k, n - k)
if k == 0 { break }
stack.push((n, k))
n = n - 1
k = k - 1
}
var y = 1
while let v = stack.pop() { y *= v.0 / v.1 }
return y
}
static func binomDouble(_ n: Int, _ k: Int) -> Double {
var n = Double(n), k = Double(k)
var stack = Stack<(Double, Double)>()
while true {
k = Swift.min(k, n - k)
if k == 0 { break }
stack.push((n, k))
n = n - 1
k = k - 1
}
var y = 1.0
while let v = stack.pop() { y *= v.0 / v.1 }
return y
}
static func overBinom(_ on: Int, _ ok: Int) -> OverResult {
var n = on, k = ok
var stack = Stack<(Int, Int)>()
while true {
guard case .int(let nk) = overDiff(n, k) else {
return .double(binomDouble(on, ok))
}
k = Swift.min(k, nk)
if k == 0 { break }
stack.push((n, k))
guard case .int(let nn) = overDiff(n, 1) else {
return .double(binomDouble(on, ok))
}
guard case .int(let kk) = overDiff(k, 1) else {
return .double(binomDouble(on, ok))
}
n = nn
k = kk
}
var y = 1
while let v = stack.pop() {
guard case .int(let yv0) = overMulti(y, v.0) else {
return .double(binomDouble(on, ok))
}
guard case .int(let yv0v1) = overDiv(yv0, v.1) else {
return .double(binomDouble(on, ok))
}
y = yv0v1
}
return .int(y)
}
}
extension Int: Interpolatable {
static func linear(_ f0: Int, _ f1: Int,
t: Double) -> Int {
Int(Double.linear(Double(f0), Double(f1), t: t))
}
static func firstSpline(_ f1: Int,
_ f2: Int, _ f3: Int, t: Double) -> Int {
Int(Double.firstSpline(Double(f1), Double(f2), Double(f3), t: t))
}
static func spline(_ f0: Int, _ f1: Int,
_ f2: Int, _ f3: Int, t: Double) -> Int {
Int(Double.spline(Double(f0), Double(f1), Double(f2), Double(f3), t: t))
}
static func lastSpline(_ f0: Int, _ f1: Int,
_ f2: Int, t: Double) -> Int {
Int(Double.lastSpline(Double(f0), Double(f1), Double(f2), t: t))
}
}
extension Int {
init(_ o: Bool) {
self = o ? 1 : 0
}
}
struct IndexValue<Value> {
var value: Value
var index: Int
}
extension IndexValue: Equatable where Value: Equatable {}
extension IndexValue: Hashable where Value: Hashable {}
extension IndexValue: CustomStringConvertible {
var description: String {
"(\(value) at: \(index))"
}
}
extension IndexValue: Codable where Value: Codable {}