/* * GNU AFFERO GENERAL PUBLIC LICENSE * Version 3, 19 November 2007 * Copyright (C) 2007 Free Software Foundation, Inc. * Everyone is permitted to copy and distribute verbatim copies * of this license document, but changing it is not allowed. */ using System; using System.Collections.Concurrent; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; namespace UVtools.Core.Extensions; public static class ParallelExtensions { //public static readonly ParallelOptions ParallelSingleThread = new() { MaxDegreeOfParallelism = 1 }; public static void ForAllInApproximateOrder(this ParallelQuery source, Action action) { Partitioner.Create(source) .AsParallel() .AsOrdered() .ForAll(action); } public static void ForEachInApproximateOrder(this ParallelQuery source, Action action) { source = Partitioner.Create(source) .AsParallel() .AsOrdered(); Parallel.ForEach(source, action); } public static IEnumerable OrderedParallel(this IEnumerable list, Func action) { var unorderedResult = new ConcurrentBag<(long, T1)>(); Parallel.ForEach(list, (o, state, i) => { unorderedResult.Add((i, action.Invoke(o))); }); var ordered = unorderedResult.OrderBy(o => o.Item1); return ordered.Select(o => o.Item2); } }