Repository navigation
Expand file tree
/
Copy pathEnumerableEx.cs
More file actions
139 lines (127 loc) · 5.61 KB
/
Copy pathEnumerableEx.cs
File metadata and controls
139 lines (127 loc) · 5.61 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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
using System;
using System.Collections.Generic;
using System.Linq;
using DynamicData.Annotations;
namespace DynamicData.Kernel
{
/// <summary>
/// Enumerable extensions
/// </summary>
public static class EnumerableEx
{
/// <summary>
/// Casts the enumerable to an array if it is already an array. Otherwise call ToArray
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="source">The source.</param>
/// <returns></returns>
public static T[] AsArray<T>([NotNull] this IEnumerable<T> source)
{
if (source == null) throw new ArgumentNullException(nameof(source));
return source as T[] ?? source.ToArray();
}
/// <summary>
/// Casts the enumerable to an array if it is already an array. Otherwise call ToArray
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="source">The source.</param>
/// <returns></returns>
public static List<T> AsList<T>([NotNull] this IEnumerable<T> source)
{
if (source == null) throw new ArgumentNullException(nameof(source));
return source as List<T> ?? source.ToList();
}
/// <summary>
/// Returns any duplicated values from the source
/// </summary>
/// <typeparam name="T"></typeparam>
/// <typeparam name="TValue">The type of the value.</typeparam>
/// <param name="source">The source.</param>
/// <param name="valueSelector">The value selector.</param>
/// <returns></returns>
/// <exception cref="System.ArgumentNullException">
/// </exception>
public static IEnumerable<T> Duplicates<T, TValue>([NotNull] this IEnumerable<T> source,
[NotNull] Func<T, TValue> valueSelector)
{
if (source == null) throw new ArgumentNullException(nameof(source));
if (valueSelector == null) throw new ArgumentNullException(nameof(valueSelector));
return source.GroupBy(valueSelector)
.Where(group => group.Count() > 1)
.SelectMany(t => t);
}
/// <summary>
/// Finds the index of many items as specified in the secondary enumerable.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="source">The source.</param>
/// <param name="itemsToFind">The items to find in the source enumerable</param>
/// <returns>
/// A result as specified by the result selector
/// </returns>
/// <exception cref="System.ArgumentNullException"></exception>
public static IEnumerable<ItemWithIndex<T>> IndexOfMany<T>(this IEnumerable<T> source, IEnumerable<T> itemsToFind)
{
return source.IndexOfMany(itemsToFind, (t, idx) => new ItemWithIndex<T>(t, idx));
}
/// <summary>
/// Finds the index of many items as specified in the secondary enumerable.
/// </summary>
/// <typeparam name="TObject">The type of the object.</typeparam>
/// <typeparam name="TResult">The type of the result.</typeparam>
/// <param name="source">The source.</param>
/// <param name="itemsToFind">The items to find.</param>
/// <param name="resultSelector">The result selector</param>
/// <returns>A result as specified by the result selector</returns>
/// <exception cref="System.ArgumentNullException">
/// </exception>
public static IEnumerable<TResult> IndexOfMany<TObject, TResult>([NotNull] this IEnumerable<TObject> source, [NotNull] IEnumerable<TObject> itemsToFind, [NotNull] Func<TObject, int, TResult> resultSelector)
{
if (source == null) throw new ArgumentNullException(nameof(source));
if (itemsToFind == null) throw new ArgumentNullException(nameof(itemsToFind));
if (resultSelector == null) throw new ArgumentNullException(nameof(resultSelector));
var indexed = source.Select((element, index) => new { Element = element, Index = index });
return itemsToFind
.Join(indexed, left => left, right => right.Element, (left, right) => right)
.Select(x => resultSelector(x.Element, x.Index));
}
/// <summary>
/// Returns an object with it's current index.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="source">The source.</param>
/// <returns></returns>
internal static IEnumerable<ItemWithIndex<T>> WithIndex<T>(this IEnumerable<T> source)
{
return source.Select((item, index) => new ItemWithIndex<T>(item, index));
}
internal static void ForEach<T>(this IEnumerable<T> source, Action<T> action)
{
foreach (var item in source)
{
action(item);
}
}
internal static void ForEach<TObject>(this IEnumerable<TObject> source, Action<TObject, int> action)
{
int i = 0;
foreach (var item in source)
{
action(item, i);
i++;
}
}
internal static IEnumerable<T> EmptyIfNull<T>(this IEnumerable<T> source)
{
return source ?? Enumerable.Empty<T>();
}
internal static HashSet<T> ToHashSet<T>(this IEnumerable<T> source)
{
return new HashSet<T>(source);
}
internal static IEnumerable<T> EnumerateOne<T>(this T source)
{
yield return source;
}
}
}