-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
54f2e40
commit 8f25033
Showing
3 changed files
with
44 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
# Swift | ||
.build*/ | ||
.swiftpm/ | ||
|
||
# VSCode | ||
launch.json | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,37 @@ | ||
#if canImport(Glibc) | ||
import Glibc | ||
#endif | ||
|
||
/// Compute the prefix sum of `seq`. | ||
private func scan<S: Sequence, U>(_ seq: S, _ initial: U, _ combine: (U, S.Element) -> U) -> [U] { | ||
var result: [U] = [] | ||
result.reserveCapacity(seq.underestimatedCount) | ||
var runningResult = initial | ||
for element in seq { | ||
runningResult = combine(runningResult, element) | ||
result.append(runningResult) | ||
/// Compute the prefix sum of `seq`. | ||
private func scan<S: Sequence, U>(_ seq: S, _ initial: U, _ combine: (U, S.Element) -> U) -> [U] { | ||
var result: [U] = [] | ||
result.reserveCapacity(seq.underestimatedCount) | ||
var runningResult = initial | ||
for element in seq { | ||
runningResult = combine(runningResult, element) | ||
result.append(runningResult) | ||
} | ||
return result | ||
} | ||
return result | ||
} | ||
|
||
func withArrayOfIovecs<R>(_ args: [String], _ body: ([iovec]) -> R) -> R { | ||
let argsCounts = Array(args.map { $0.utf8.count + 1 }) | ||
let argsOffsets = [0] + scan(argsCounts, 0, +) | ||
let argsBufferSize = argsOffsets.last! | ||
var argsBuffer: [UInt8] = [] | ||
argsBuffer.reserveCapacity(argsBufferSize) | ||
for arg in args { | ||
argsBuffer.append(contentsOf: arg.utf8) | ||
argsBuffer.append(0) | ||
} | ||
return argsBuffer.withUnsafeMutableBufferPointer { | ||
argsBuffer in | ||
let ptr = UnsafeMutableRawPointer(argsBuffer.baseAddress!).bindMemory( | ||
to: CChar.self, capacity: argsBuffer.count | ||
) | ||
let iovecs: [iovec] = zip(argsCounts, argsOffsets).map { count, offset in | ||
iovec(iov_base: ptr + offset, iov_len: count - 1) | ||
func withArrayOfIovecs<R>(_ args: [String], _ body: ([iovec]) -> R) -> R { | ||
let argsCounts = Array(args.map { $0.utf8.count + 1 }) | ||
let argsOffsets = [0] + scan(argsCounts, 0, +) | ||
let argsBufferSize = argsOffsets.last! | ||
var argsBuffer: [UInt8] = [] | ||
argsBuffer.reserveCapacity(argsBufferSize) | ||
for arg in args { | ||
argsBuffer.append(contentsOf: arg.utf8) | ||
argsBuffer.append(0) | ||
} | ||
return argsBuffer.withUnsafeMutableBufferPointer { | ||
argsBuffer in | ||
let ptr = UnsafeMutableRawPointer(argsBuffer.baseAddress!).bindMemory( | ||
to: CChar.self, capacity: argsBuffer.count | ||
) | ||
let iovecs: [iovec] = zip(argsCounts, argsOffsets).map { count, offset in | ||
iovec(iov_base: ptr + offset, iov_len: count - 1) | ||
} | ||
return body(iovecs) | ||
} | ||
return body(iovecs) | ||
} | ||
} | ||
#endif |