Files
UVtools/UVtools.Core/Extensions/ParallelExtensions.cs
T
Tiago Conceição 8ef55e2e05 v2.23.5
- (Fix) Odd crash when detecing for resin trap issues which was duplicating contours
2021-10-07 04:39:30 +01:00

50 lines
1.6 KiB
C#

/*
* GNU AFFERO GENERAL PUBLIC LICENSE
* Version 3, 19 November 2007
* Copyright (C) 2007 Free Software Foundation, Inc. <https://fsf.org/>
* 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<TSource>(this ParallelQuery<TSource> source, Action<TSource> action)
{
Partitioner.Create(source)
.AsParallel()
.AsOrdered()
.ForAll(action);
}
public static void ForEachInApproximateOrder<TSource>(this ParallelQuery<TSource> source, Action<TSource> action)
{
source = Partitioner.Create(source)
.AsParallel()
.AsOrdered();
Parallel.ForEach(source, action);
}
public static IEnumerable<T1> OrderedParallel<T, T1>(this IEnumerable<T> list, Func<T, T1> 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);
}
}
}