-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathCollectionExtensions.cs
More file actions
64 lines (57 loc) · 1.84 KB
/
Copy pathCollectionExtensions.cs
File metadata and controls
64 lines (57 loc) · 1.84 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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ICSharpCode.NRefactory.CSharp;
namespace System.Collections.Generic
{
static class CollectionExtensions
{
public static bool Parallel = true;
public static Action ParallelPreAction { get; set; }
public static void ForEachParallel<T>(this IEnumerable<T> items, Action<T> action, bool parallel)
{
if (parallel)
{
Action<T> action2 = action;
if (ParallelPreAction != null)
action2 = t => {ParallelPreAction(); action(t);};
items.AsParallel().ForAll(action2);
}
else
items.ForEach(action);
}
public static void ForEachParallel<T>(this IEnumerable<T> items, Action<T> action)
{
items.ForEachParallel(action, Parallel);
}
public static IEnumerable<T> NotNull<T>(this IEnumerable<T> list)
{
if (list == null)
return Enumerable.Empty<T>();
return list;
}
public static R IfNotNull<T, R>(this T obj, Func<T, R> func) where T : class
{
if (obj != null)
return func(obj);
return default(R);
}
public static bool Matches<T>(this IEnumerable<T> list, IEnumerable<T> list2, Func<T, T, bool> match)
{
var x = list.GetEnumerator();
var y = list2.GetEnumerator();
while (true)
{
var x1 = x.MoveNext();
var x2 = y.MoveNext();
if (x1 != x2)
return false;
if (!x1)
return true;
if (!match(x.Current, y.Current))
return false;
}
}
}
}