<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><br class=""><div><blockquote type="cite" class=""><div class="">On 30 Oct 2016, at 09:15, Karl <<a href="mailto:raziel.im+swift-users@gmail.com" class="">raziel.im+swift-users@gmail.com</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class="">I had the need for a concurrent map recently. I had a part of a program which needed to read chunks of data and concurrently process them and assemble the results in an array. This isn’t necessarily as obvious as it sounds, because of arrays being value types. I came up with the following snippet which I’d like to check for correctness; it could also be helpful to others.<div class=""><br class=""></div><div class="">Perhaps this is something Dispatch should provide out-of-the-box?</div><div class=""><br class=""></div><div class="">- Karl</div></div></div></blockquote><br class=""></div><div>Ah one second, I was refactoring this and forgot to test it. Here’s the actual code:</div><div><br class=""></div><blockquote style="margin: 0 0 0 40px; border: none; padding: 0px;" class=""><blockquote style="margin: 0 0 0 40px; border: none; padding: 0px;" class=""><div>extension DispatchQueue {</div></blockquote><blockquote style="margin: 0 0 0 40px; border: none; padding: 0px;" class=""><div><br class=""></div></blockquote><blockquote style="margin: 0 0 0 40px; border: none; padding: 0px;" class=""><div> static func concurrentMap<T>(iterations: Int, execute block: (Int) -> T) -> [T] {</div></blockquote><blockquote style="margin: 0 0 0 40px; border: none; padding: 0px;" class=""><div><br class=""></div></blockquote><blockquote style="margin: 0 0 0 40px; border: none; padding: 0px;" class=""><div> let __result = UnsafeMutableRawBufferPointer.allocate(count: iterations * MemoryLayout<T>.stride)</div></blockquote><blockquote style="margin: 0 0 0 40px; border: none; padding: 0px;" class=""><div> defer { __result.deallocate() }</div></blockquote><blockquote style="margin: 0 0 0 40px; border: none; padding: 0px;" class=""><div> let _result = __result.baseAddress?.assumingMemoryBound(to: T.self)</div></blockquote><blockquote style="margin: 0 0 0 40px; border: none; padding: 0px;" class=""><div> let result = UnsafeMutableBufferPointer<T>(start: _result, count: iterations)</div></blockquote><blockquote style="margin: 0 0 0 40px; border: none; padding: 0px;" class=""><div> concurrentPerform(iterations: iterations) { idx in</div></blockquote><blockquote style="margin: 0 0 0 40px; border: none; padding: 0px;" class=""><div> result[idx] = block(idx)</div></blockquote><blockquote style="margin: 0 0 0 40px; border: none; padding: 0px;" class=""><div> }</div></blockquote><blockquote style="margin: 0 0 0 40px; border: none; padding: 0px;" class=""><div> return Array(result)</div></blockquote><blockquote style="margin: 0 0 0 40px; border: none; padding: 0px;" class=""><div> }</div></blockquote><blockquote style="margin: 0 0 0 40px; border: none; padding: 0px;" class=""><div>}</div></blockquote><blockquote style="margin: 0 0 0 40px; border: none; padding: 0px;" class=""><div><br class=""></div></blockquote><blockquote style="margin: 0 0 0 40px; border: none; padding: 0px;" class=""><div>extension Array {</div></blockquote><blockquote style="margin: 0 0 0 40px; border: none; padding: 0px;" class=""><div> func concurrentMap<T>(execute block: (Element)->T) -> [T] {</div></blockquote><blockquote style="margin: 0 0 0 40px; border: none; padding: 0px;" class=""><div> return DispatchQueue.concurrentMap(iterations: count) { block(self[$0]) }</div></blockquote><blockquote style="margin: 0 0 0 40px; border: none; padding: 0px;" class=""><div> }</div></blockquote><blockquote style="margin: 0 0 0 40px; border: none; padding: 0px;" class=""><div>}</div></blockquote></blockquote><div><br class=""><br class=""></div><div>Unfortunately I don’t think there’s a way to get an array to take over a +1 UnsafeMutableBufferPointer without copying.</div><br class=""><div class="">- Karl</div></body></html>