Repository navigation
Expand file tree
/
Copy pathParallelEx.cs
More file actions
92 lines (74 loc) · 3.17 KB
/
Copy pathParallelEx.cs
File metadata and controls
92 lines (74 loc) · 3.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
using System;
using System.Collections.Generic;
using System.Linq;
namespace DynamicData.PLinq
{
/// <summary>
/// Parallelisation extensions for DynamicData
/// </summary>
internal static class ParallelEx
{
#region Parallelisation
internal static bool ShouldParallelise<TObject, TKey>(this IChangeSet<TObject, TKey> source, ParallelisationOptions option)
{
return (option.Type == ParallelType.Parallelise || option.Type == ParallelType.Ordered)
&& (option.Threshold >= 0 && source.Count >= option.Threshold);
}
internal static ParallelQuery<Change<TObject, TKey>> Parallelise<TObject, TKey>(this IChangeSet<TObject, TKey> source, ParallelisationOptions option)
{
switch (option.Type)
{
case ParallelType.Parallelise:
return source.AsParallel();
case ParallelType.Ordered:
return source.AsParallel().AsOrdered();
default:
throw new ArgumentException("Should not parallelise! Call ShouldParallelise() first");
}
}
internal static bool ShouldParallelise<TObject, TKey>(this IEnumerable<KeyValuePair<TKey,TObject>> source, ParallelisationOptions option)
{
return (option.Type == ParallelType.Parallelise || option.Type == ParallelType.Ordered)
&& (option.Threshold >= 0 && source.Skip(option.Threshold).Any());
}
internal static ParallelQuery<KeyValuePair<TKey,TObject>> Parallelise<TObject, TKey>(this IEnumerable<KeyValuePair<TKey,TObject>> source, ParallelisationOptions option)
{
switch (option.Type)
{
case ParallelType.Parallelise:
return source.AsParallel();
case ParallelType.Ordered:
return source.AsParallel().AsOrdered();
default:
throw new ArgumentException("Should not parallelise! Call ShouldParallelise() first");
}
}
internal static IEnumerable<T> Parallelise<T>(this IEnumerable<T> source, ParallelisationOptions option)
{
switch (option.Type)
{
case ParallelType.Parallelise:
{
var parallelise = source as T[] ?? source.ToArray();
if (option.Threshold >= 0 && parallelise.Length >= option.Threshold)
{
return parallelise.AsParallel();
}
return parallelise;
}
case ParallelType.Ordered:
{
var parallelise = source as T[] ?? source.ToArray();
if (option.Threshold >= 0 && parallelise.Length >= option.Threshold)
{
return parallelise.AsParallel().AsOrdered();
}
return parallelise;
}
default:
return source;
}
}
#endregion
}
}