1
0
Fork 0
Brotli-Builder/BrotliCalc/Helpers/Linq.cs

36 lines
1.1 KiB
C#

using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
namespace BrotliCalc.Helpers{
static class Linq{
public static int MaxThreads { get; set; } = int.MaxValue;
public static IEnumerable<(TA a, TB b)> Cartesian<TA, TB>(this IEnumerable<TA> me, IReadOnlyList<TB> other){
foreach(var a in me){
foreach(var b in other){
yield return (a, b);
}
}
}
public static IEnumerable<(int index, T ele)> WithIndex<T>(this IEnumerable<T> me){
int index = 0;
foreach(var element in me){
yield return (index++, element);
}
}
public static ParallelQuery<T> Parallelize<T>(this IEnumerable<T> me){
var query = Partitioner.Create(me, EnumerablePartitionerOptions.NoBuffering).AsParallel().WithMergeOptions(ParallelMergeOptions.NotBuffered);
if (MaxThreads != int.MaxValue){
query = query.WithDegreeOfParallelism(MaxThreads);
}
return query;
}
}
}