Skip to content

Make algorithms generic over IndexDistance #1382

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 28, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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..<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)
Copy link
Contributor

@aciidgh aciidgh Oct 28, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. I can do that one in a separate PR right?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, its a separate repo anyway.

}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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..<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)
}
}
}
Expand Down
13 changes: 6 additions & 7 deletions Sources/Basic/OutputByteStream.swift
Original file line number Diff line number Diff line change
Expand Up @@ -116,23 +116,23 @@ public class OutputByteStream: TextOutputStream {

/// 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
Expand All @@ -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)

Expand Down Expand Up @@ -296,7 +296,6 @@ public func <<< (stream: OutputByteStream, value: ArraySlice<UInt8>) -> OutputBy
@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
Expand Down