|
9 | 9 | //
|
10 | 10 | //===----------------------------------------------------------------------===//
|
11 | 11 |
|
12 |
| -// For log(_:) and exp(_:) |
13 |
| -#if canImport(Glibc) |
| 12 | +// For log(_:) and root(_:_:) |
14 | 13 | @_implementationOnly
|
15 |
| -import Glibc |
16 |
| -#elseif canImport(ucrt) |
17 |
| -@_implementationOnly |
18 |
| -import ucrt |
19 |
| -#elseif canImport(Darwin) |
20 |
| -@_implementationOnly |
21 |
| -import Darwin |
22 |
| -#endif |
| 14 | +import RealModule |
23 | 15 |
|
24 | 16 | //===----------------------------------------------------------------------===//
|
25 | 17 | // randomStableSample(count:)
|
@@ -90,15 +82,15 @@ extension Collection {
|
90 | 82 | // https://dl.acm.org/doi/pdf/10.1145/198429.198435
|
91 | 83 |
|
92 | 84 | fileprivate func nextW<G: RandomNumberGenerator>(
|
93 |
| - k: Double, using rng: inout G |
| 85 | + k: Int, using rng: inout G |
94 | 86 | ) -> Double {
|
95 |
| - exp(log(Double.random(in: 0..<1, using: &rng)) / k) |
| 87 | + Double.root(.random(in: 0..<1, using: &rng), k) |
96 | 88 | }
|
97 | 89 |
|
98 | 90 | fileprivate func nextOffset<G: RandomNumberGenerator>(
|
99 | 91 | w: Double, using rng: inout G
|
100 | 92 | ) -> Int {
|
101 |
| - Int(log(Double.random(in: 0..<1, using: &rng)) / log(1 - w)) |
| 93 | + Int(Double.log(.random(in: 0..<1, using: &rng)) / .log(1 - w)) |
102 | 94 | }
|
103 | 95 |
|
104 | 96 | extension Collection {
|
@@ -129,11 +121,10 @@ extension Collection {
|
129 | 121 | result.append(self[i])
|
130 | 122 | formIndex(after: &i)
|
131 | 123 | }
|
132 |
| - |
133 |
| - let dk = Double(k) |
| 124 | + |
134 | 125 | while i < endIndex {
|
135 | 126 | // Calculate the next value of w.
|
136 |
| - w *= nextW(k: dk, using: &rng) |
| 127 | + w *= nextW(k: k, using: &rng) |
137 | 128 |
|
138 | 129 | // Find index of the next element to swap into the reservoir.
|
139 | 130 | let offset = nextOffset(w: w, using: &rng)
|
@@ -198,10 +189,9 @@ extension Sequence {
|
198 | 189 | result.append(el)
|
199 | 190 | }
|
200 | 191 |
|
201 |
| - let dk = Double(k) |
202 | 192 | while true {
|
203 | 193 | // Calculate the next value of w.
|
204 |
| - w *= nextW(k: dk, using: &rng) |
| 194 | + w *= nextW(k: k, using: &rng) |
205 | 195 |
|
206 | 196 | // Find the offset of the next element to swap into the reservoir.
|
207 | 197 | var offset = nextOffset(w: w, using: &rng) + 1
|
|
0 commit comments