| 258 | |
| 259 | template <typename Iterator, typename Functor> |
| 260 | auto AThreadPool::parallel(Iterator begin, Iterator end, Functor&& functor) { |
| 261 | using ResultType = decltype(std::declval<Functor>()(std::declval<Iterator>(), std::declval<Iterator>())); |
| 262 | AFutureSet<ResultType> futureSet; |
| 263 | |
| 264 | size_t itemCount = end - begin; |
| 265 | size_t affinity = (glm::min) (AThreadPool::global().getTotalWorkerCount(), itemCount); |
| 266 | if (affinity == 0) |
| 267 | return futureSet; |
| 268 | size_t itemsPerThread = itemCount / affinity; |
| 269 | |
| 270 | for (size_t threadIndex = 0; threadIndex < affinity; ++threadIndex) { |
| 271 | auto forThreadBegin = begin; |
| 272 | begin += itemsPerThread; |
| 273 | auto forThreadEnd = threadIndex + 1 == affinity ? end : begin; |
| 274 | futureSet.push_back( |
| 275 | *this * [functor = std::forward<Functor>(functor), forThreadBegin, forThreadEnd]() -> decltype(auto) { |
| 276 | return functor(forThreadBegin, forThreadEnd); |
| 277 | }); |
| 278 | } |
| 279 | |
| 280 | return futureSet; |
| 281 | } |
| 282 | |
| 283 | #include <AUI/Reflect/AReflect.h> |