Merge pull request #1382 from airspeedswift/remove-indexdistance

Make algorithms generic over IndexDistance
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 6c63cee..3fcd877 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 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..<count - 1 {
+        let c = count
+        guard c > 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 6c63cee..3fcd877 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 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..<count - 1 {
+        let c = count
+        guard c > 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 d7d62d4..c1b5568 100644
--- a/Sources/Basic/OutputByteStream.swift
+++ b/Sources/Basic/OutputByteStream.swift
@@ -116,23 +116,23 @@
 
     /// Write a collection of bytes to the buffer.
     public final func write<C: Collection>(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 @@
                     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 @@
 @discardableResult
 public func <<< <C: Collection>(stream: OutputByteStream, value: C) -> OutputByteStream where
     C.Iterator.Element == UInt8,
-    C.IndexDistance == Int,
     C.SubSequence: Collection {
     stream.write(collection: value)
     return stream