Skip to content

Commit 83ae317

Browse files
committed
Make code generic over all IndexDistance
1 parent 08d8dda commit 83ae317

File tree

3 files changed

+26
-23
lines changed

3 files changed

+26
-23
lines changed

Fixtures/DependencyResolution/External/Complex/FisherYates/src/Fisher-Yates_Shuffle.swift

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,20 @@ public extension Collection {
1313
}
1414
}
1515

16-
public extension MutableCollection where Index == Int { //, IndexDistance == Int {
16+
public extension MutableCollection {
17+
/// Shuffles the contents of this collection.
1718
mutating func shuffleInPlace() {
18-
guard count > 1 else { return }
19-
20-
for i in 0..<count - 1 {
19+
let c = count
20+
guard c > 1 else { return }
21+
22+
for (firstUnshuffled, unshuffledCount) in zip(indices, stride(from: c, to: 1, by: -1)) {
2123
#if os(macOS) || os(iOS)
22-
let j = Int(arc4random_uniform(UInt32(count - i))) + i
24+
let d = arc4random_uniform(numericCast(unshuffledCount))
2325
#else
24-
let j = Int(random() % (count - i)) + i
26+
let d = numericCast(random()) % (count - i) + i
2527
#endif
26-
guard i != j else { continue }
27-
swap(&self[i], &self[j])
28+
let i = index(firstUnshuffled, offsetBy: numericCast(d))
29+
swapAt(firstUnshuffled, i)
2830
}
2931
}
3032
}

Fixtures/DependencyResolution/External/IgnoreIndirectTests/FisherYates/src/Fisher-Yates_Shuffle.swift

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,20 @@ public extension Collection {
1313
}
1414
}
1515

16-
public extension MutableCollection where Index == Int { //, IndexDistance == Int {
16+
public extension MutableCollection {
17+
/// Shuffles the contents of this collection.
1718
mutating func shuffleInPlace() {
18-
guard count > 1 else { return }
19-
20-
for i in 0..<count - 1 {
19+
let c = count
20+
guard c > 1 else { return }
21+
22+
for (firstUnshuffled, unshuffledCount) in zip(indices, stride(from: c, to: 1, by: -1)) {
2123
#if os(macOS) || os(iOS)
22-
let j = Int(arc4random_uniform(UInt32(count - i))) + i
24+
let d = arc4random_uniform(numericCast(unshuffledCount))
2325
#else
24-
let j = Int(random() % (count - i)) + i
26+
let d = numericCast(random()) % (count - i) + i
2527
#endif
26-
guard i != j else { continue }
27-
swap(&self[i], &self[j])
28+
let i = index(firstUnshuffled, offsetBy: numericCast(d))
29+
swapAt(firstUnshuffled, i)
2830
}
2931
}
3032
}

Sources/Basic/OutputByteStream.swift

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -116,23 +116,23 @@ public class OutputByteStream: TextOutputStream {
116116

117117
/// Write a collection of bytes to the buffer.
118118
public final func write<C: Collection>(collection bytes: C) where
119-
// C.IndexDistance == Int,
120119
C.Iterator.Element == UInt8,
121120
C.SubSequence: Collection {
122121
queue.sync {
123122
// This is based on LLVM's raw_ostream.
124123
let availableBufferSize = self.availableBufferSize
124+
let byteCount = Int(bytes.count)
125125

126126
// If we have to insert more than the available space in buffer.
127-
if bytes.count > availableBufferSize {
127+
if byteCount > availableBufferSize {
128128
// If buffer is empty, start writing and keep the last chunk in buffer.
129129
if buffer.isEmpty {
130-
let bytesToWrite = bytes.count - (bytes.count % availableBufferSize)
131-
let writeUptoIndex = bytes.index(bytes.startIndex, offsetBy: bytesToWrite)
130+
let bytesToWrite = byteCount - (byteCount % availableBufferSize)
131+
let writeUptoIndex = bytes.index(bytes.startIndex, offsetBy: numericCast(bytesToWrite))
132132
writeImpl(bytes.prefix(upTo: writeUptoIndex))
133133

134134
// If remaining bytes is more than buffer size write everything.
135-
let bytesRemaining = bytes.count - bytesToWrite
135+
let bytesRemaining = byteCount - bytesToWrite
136136
if bytesRemaining > availableBufferSize {
137137
writeImpl(bytes.suffix(from: writeUptoIndex))
138138
return
@@ -142,7 +142,7 @@ public class OutputByteStream: TextOutputStream {
142142
return
143143
}
144144

145-
let writeUptoIndex = bytes.index(bytes.startIndex, offsetBy: availableBufferSize)
145+
let writeUptoIndex = bytes.index(bytes.startIndex, offsetBy: numericCast(availableBufferSize))
146146
// Append whatever we can accommodate.
147147
buffer += bytes.prefix(upTo: writeUptoIndex)
148148

@@ -296,7 +296,6 @@ public func <<< (stream: OutputByteStream, value: ArraySlice<UInt8>) -> OutputBy
296296
@discardableResult
297297
public func <<< <C: Collection>(stream: OutputByteStream, value: C) -> OutputByteStream where
298298
C.Iterator.Element == UInt8,
299-
// C.IndexDistance == Int,
300299
C.SubSequence: Collection {
301300
stream.write(collection: value)
302301
return stream

0 commit comments

Comments
 (0)