From 0290cbd93cd50ef0d349465f9e13ce30bc948453 Mon Sep 17 00:00:00 2001 From: Ben Cohen Date: Fri, 27 Oct 2017 13:15:55 -0700 Subject: [PATCH] Make code generic over all IndexDistance --- .../FisherYates/src/Fisher-Yates_Shuffle.swift | 18 ++++++++++-------- .../FisherYates/src/Fisher-Yates_Shuffle.swift | 18 ++++++++++-------- Sources/Basic/OutputByteStream.swift | 13 ++++++------- 3 files changed, 26 insertions(+), 23 deletions(-) diff --git a/Fixtures/DependencyResolution/External/Complex/FisherYates/src/Fisher-Yates_Shuffle.swift b/Fixtures/DependencyResolution/External/Complex/FisherYates/src/Fisher-Yates_Shuffle.swift index 6c63ceeb03d..3fcd87774c6 100644 --- a/Fixtures/DependencyResolution/External/Complex/FisherYates/src/Fisher-Yates_Shuffle.swift +++ b/Fixtures/DependencyResolution/External/Complex/FisherYates/src/Fisher-Yates_Shuffle.swift @@ -13,18 +13,20 @@ public extension Collection { } } -public extension MutableCollection where Index == Int, IndexDistance == Int { +public extension MutableCollection { + /// Shuffles the contents of this collection. mutating func shuffleInPlace() { - guard count > 1 else { return } - - for i in 0.. 1 else { return } + + for (firstUnshuffled, unshuffledCount) in zip(indices, stride(from: c, to: 1, by: -1)) { #if os(macOS) || os(iOS) - let j = Int(arc4random_uniform(UInt32(count - i))) + i + let d = arc4random_uniform(numericCast(unshuffledCount)) #else - let j = Int(random() % (count - i)) + i + let d = numericCast(random()) % unshuffledCount #endif - guard i != j else { continue } - swap(&self[i], &self[j]) + let i = index(firstUnshuffled, offsetBy: numericCast(d)) + swapAt(firstUnshuffled, i) } } } diff --git a/Fixtures/DependencyResolution/External/IgnoreIndirectTests/FisherYates/src/Fisher-Yates_Shuffle.swift b/Fixtures/DependencyResolution/External/IgnoreIndirectTests/FisherYates/src/Fisher-Yates_Shuffle.swift index 6c63ceeb03d..3fcd87774c6 100644 --- a/Fixtures/DependencyResolution/External/IgnoreIndirectTests/FisherYates/src/Fisher-Yates_Shuffle.swift +++ b/Fixtures/DependencyResolution/External/IgnoreIndirectTests/FisherYates/src/Fisher-Yates_Shuffle.swift @@ -13,18 +13,20 @@ public extension Collection { } } -public extension MutableCollection where Index == Int, IndexDistance == Int { +public extension MutableCollection { + /// Shuffles the contents of this collection. mutating func shuffleInPlace() { - guard count > 1 else { return } - - for i in 0.. 1 else { return } + + for (firstUnshuffled, unshuffledCount) in zip(indices, stride(from: c, to: 1, by: -1)) { #if os(macOS) || os(iOS) - let j = Int(arc4random_uniform(UInt32(count - i))) + i + let d = arc4random_uniform(numericCast(unshuffledCount)) #else - let j = Int(random() % (count - i)) + i + let d = numericCast(random()) % unshuffledCount #endif - guard i != j else { continue } - swap(&self[i], &self[j]) + let i = index(firstUnshuffled, offsetBy: numericCast(d)) + swapAt(firstUnshuffled, i) } } } diff --git a/Sources/Basic/OutputByteStream.swift b/Sources/Basic/OutputByteStream.swift index d7d62d40f37..c1b55683203 100644 --- a/Sources/Basic/OutputByteStream.swift +++ b/Sources/Basic/OutputByteStream.swift @@ -116,23 +116,23 @@ public class OutputByteStream: TextOutputStream { /// Write a collection of bytes to the buffer. public final func write(collection bytes: C) where - C.IndexDistance == Int, C.Iterator.Element == UInt8, C.SubSequence: Collection { queue.sync { // This is based on LLVM's raw_ostream. let availableBufferSize = self.availableBufferSize + let byteCount = Int(bytes.count) // If we have to insert more than the available space in buffer. - if bytes.count > availableBufferSize { + if byteCount > availableBufferSize { // If buffer is empty, start writing and keep the last chunk in buffer. if buffer.isEmpty { - let bytesToWrite = bytes.count - (bytes.count % availableBufferSize) - let writeUptoIndex = bytes.index(bytes.startIndex, offsetBy: bytesToWrite) + let bytesToWrite = byteCount - (byteCount % availableBufferSize) + let writeUptoIndex = bytes.index(bytes.startIndex, offsetBy: numericCast(bytesToWrite)) writeImpl(bytes.prefix(upTo: writeUptoIndex)) // If remaining bytes is more than buffer size write everything. - let bytesRemaining = bytes.count - bytesToWrite + let bytesRemaining = byteCount - bytesToWrite if bytesRemaining > availableBufferSize { writeImpl(bytes.suffix(from: writeUptoIndex)) return @@ -142,7 +142,7 @@ public class OutputByteStream: TextOutputStream { return } - let writeUptoIndex = bytes.index(bytes.startIndex, offsetBy: availableBufferSize) + let writeUptoIndex = bytes.index(bytes.startIndex, offsetBy: numericCast(availableBufferSize)) // Append whatever we can accommodate. buffer += bytes.prefix(upTo: writeUptoIndex) @@ -296,7 +296,6 @@ public func <<< (stream: OutputByteStream, value: ArraySlice) -> OutputBy @discardableResult public func <<< (stream: OutputByteStream, value: C) -> OutputByteStream where C.Iterator.Element == UInt8, - C.IndexDistance == Int, C.SubSequence: Collection { stream.write(collection: value) return stream