Compare commits

...

1 Commits

Author SHA1 Message Date
Paul Taykalo 38c40c90d3 Inline some frequently used extensions 2022-03-10 18:07:03 +02:00
3 changed files with 11 additions and 3 deletions

View File

@ -2,13 +2,14 @@ import Dispatch
import Foundation
extension Array where Element: NSTextCheckingResult {
@inlinable
func ranges() -> [NSRange] {
return map { $0.range }
}
}
extension Array where Element: Equatable {
var unique: [Element] {
@inlinable var unique: [Element] {
var uniqueValues = [Element]()
for item in self where !uniqueValues.contains(item) {
uniqueValues.append(item)
@ -40,10 +41,12 @@ extension Array {
return nil
}
@inlinable
func group<U: Hashable>(by transform: (Element) -> U) -> [U: [Element]] {
return Dictionary(grouping: self, by: { transform($0) })
}
@inlinable
func partitioned(by belongsInSecondPartition: (Element) throws -> Bool) rethrows ->
(first: ArraySlice<Element>, second: ArraySlice<Element>) {
var copy = self
@ -51,14 +54,17 @@ extension Array {
return (copy[0..<pivot], copy[pivot..<count])
}
@inlinable
func parallelFlatMap<T>(transform: (Element) -> [T]) -> [T] {
return parallelMap(transform: transform).flatMap { $0 }
}
@inlinable
func parallelCompactMap<T>(transform: (Element) -> T?) -> [T] {
return parallelMap(transform: transform).compactMap { $0 }
}
@inlinable
func parallelMap<T>(transform: (Element) -> T) -> [T] {
var result = ContiguousArray<T?>(repeating: nil, count: count)
return result.withUnsafeMutableBufferPointer { buffer in
@ -71,7 +77,7 @@ extension Array {
}
extension Collection {
var isNotEmpty: Bool {
@inlinable var isNotEmpty: Bool {
return !isEmpty
}
}

View File

@ -33,7 +33,7 @@ public struct SwiftLintSyntaxToken {
}
extension Array where Element == SwiftLintSyntaxToken {
var kinds: [SyntaxKind] {
@inlinable var kinds: [SyntaxKind] {
return compactMap { $0.kind }
}
}

View File

@ -1,10 +1,12 @@
import Dispatch
extension Array {
@inlinable
func parallelCompactMap<T>(transform: (Element) -> T?) -> [T] {
return parallelMap(transform: transform).compactMap { $0 }
}
@inlinable
func parallelMap<T>(transform: (Element) -> T) -> [T] {
return [T](unsafeUninitializedCapacity: count) { buffer, initializedCount in
let baseAddress = buffer.baseAddress!