forked from bestswifter/MySampleCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathextension.swift
More file actions
38 lines (31 loc) · 1.32 KB
/
extension.swift
File metadata and controls
38 lines (31 loc) · 1.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
//
// extension.swift
// 利息
//
// Created by 张星宇 on 16/1/16.
// Copyright © 2016年 张星宇. All rights reserved.
//
import Foundation
extension Array {
func ktMap<T>(transform: (Element) -> T) -> [T] {
guard !self.isEmpty else { return [] } // 如果数组为空就直接返回空数组
var result: [(Int, [T])] = []
let group = dispatch_group_create()
let syncQueue = dispatch_queue_create("com.gcd.kt", DISPATCH_QUEUE_SERIAL)
let subArrayLength: Int = max(1, self.count / NSProcessInfo.processInfo().activeProcessorCount / 2)
for var step = 0; step * subArrayLength < self.count; step++ {
var stepResult: [T] = []
let localStep = step
dispatch_group_async(group, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)) {
for i in (localStep * subArrayLength)..<min(((localStep + 1) * subArrayLength), self.count) {
stepResult += [transform(self[i])]
}
dispatch_group_async(group, syncQueue) {
result += [(localStep, stepResult)]
}
}
}
dispatch_group_wait(group, DISPATCH_TIME_FOREVER)
return result.sort { $0.0 < $1.0 }.flatMap { $0.1 }
}
}