1
1
mirror of https://github.com/qvacua/vimr.git synced 2024-11-23 19:21:53 +03:00

Reformat and add a test

This commit is contained in:
Tae Won Ha 2021-12-23 22:37:11 +01:00
parent 848017f450
commit e4701adc1d
No known key found for this signature in database
GPG Key ID: E40743465B5B8B44
2 changed files with 22 additions and 49 deletions

View File

@ -15,16 +15,11 @@ public extension Array where Element: Hashable {
}
public extension Array {
func data() -> Data {
self.withUnsafeBufferPointer(Data.init)
}
func data() -> Data { self.withUnsafeBufferPointer(Data.init) }
}
public extension RandomAccessCollection where Index == Int {
func parallelMap<T>(
chunkSize: Int = 1,
_ transform: @escaping (Element) -> T
) -> [T] {
func parallelMap<T>(chunkSize: Int = 1, _ transform: @escaping (Element) -> T) -> [T] {
let count = self.count
guard count > chunkSize else { return self.map(transform) }
@ -34,9 +29,7 @@ public extension RandomAccessCollection where Index == Int {
// then we get crashes.
result.withUnsafeMutableBufferPointer { pointer in
if chunkSize == 1 {
DispatchQueue.concurrentPerform(iterations: count) { i in
pointer[i] = transform(self[i])
}
DispatchQueue.concurrentPerform(iterations: count) { i in pointer[i] = transform(self[i]) }
} else {
let chunkCount = Int(ceil(Double(self.count) / Double(chunkSize)))
DispatchQueue.concurrentPerform(iterations: chunkCount) { chunkIndex in
@ -51,45 +44,7 @@ public extension RandomAccessCollection where Index == Int {
return result.map { $0! }
}
func groupedRangesOriginal<T: Equatable>(
with marker: (Index, Element) -> T
) -> [CountableClosedRange<Index>] {
if self.isEmpty { return [] }
if self.count == 1 { return [self.startIndex...self.startIndex] }
var result = [CountableClosedRange<Index>]()
result.reserveCapacity(self.count / 2)
let inclusiveEndIndex = self.endIndex - 1
var lastStartIndex = self.startIndex
var lastEndIndex = self.startIndex
var lastMarker = marker(0, self.first!) // self is not empty!
for i in self.startIndex..<self.endIndex {
let currentMarker = marker(i, self[i])
if lastMarker == currentMarker {
if i == inclusiveEndIndex {
result.append(lastStartIndex...i)
}
} else {
result.append(lastStartIndex...lastEndIndex)
lastMarker = currentMarker
lastStartIndex = i
if i == inclusiveEndIndex {
result.append(i...i)
}
}
lastEndIndex = i
}
return result
}
func groupedRanges<T: Equatable>(
with marker: (Element) -> T
) -> [CountableClosedRange<Index>] {
func groupedRanges<T: Equatable>(with marker: (Element) -> T) -> [ClosedRange<Index>] {
if self.isEmpty { return [] }
if self.count == 1 { return [self.startIndex...self.startIndex] }

View File

@ -129,6 +129,24 @@ class ArraySliceTest: XCTestCase {
]
))
}
func testArraySliceGroup7() {
let grouped = [
Dummy(value: 0, marker: true),
Dummy(value: 0, marker: true),
Dummy(value: 0, marker: false),
Dummy(value: 0, marker: true),
][1...2].groupedRanges { element in element.marker }
expect(grouped).to(equal(
[
1...1,
2...2
]
))
}
}
class SwiftCommonsTest: XCTestCase {