1
0
mirror of https://github.com/chylex/Brotli-Builder.git synced 2024-12-22 07:42:47 +01:00
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;
}
}
}