forked from ServiceStack/ServiceStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEnumerableExtensions.cs
More file actions
240 lines (203 loc) · 7 KB
/
EnumerableExtensions.cs
File metadata and controls
240 lines (203 loc) · 7 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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
using System;
using System.Collections;
using System.Collections.Generic;
namespace ServiceStack
{
public static class EnumerableExtensions
{
public static bool IsEmpty<T>(this ICollection<T> collection)
{
return collection == null || collection.Count == 0;
}
public static bool IsEmpty<T>(this T[] collection)
{
return collection == null || collection.Length == 0;
}
public static HashSet<T> ToHashSet<T>(this IEnumerable<T> items)
{
return new HashSet<T>(items);
}
public static void Each<T>(this IEnumerable<T> values, Action<T> action)
{
if (values == null) return;
foreach (var value in values)
{
action(value);
}
}
public static void Each<T>(this IEnumerable<T> values, Action<int, T> action)
{
if (values == null) return;
var i = 0;
foreach (var value in values)
{
action(i++, value);
}
}
public static List<To> Map<To, From>(this IEnumerable<From> items, Func<From, To> converter)
{
if (items == null)
return new List<To>();
var list = new List<To>();
foreach (var item in items)
{
list.Add(converter(item));
}
return list;
}
public static List<To> Map<To>(this System.Collections.IEnumerable items, Func<object, To> converter)
{
if (items == null)
return new List<To>();
var list = new List<To>();
foreach (var item in items)
{
list.Add(converter(item));
}
return list;
}
public static List<object> ToObjects<T>(this IEnumerable<T> items)
{
var to = new List<object>();
foreach (var item in items)
{
to.Add(item);
}
return to;
}
public static string FirstNonDefaultOrEmpty(this IEnumerable<string> values)
{
foreach (var value in values)
{
if (!string.IsNullOrEmpty(value)) return value;
}
return null;
}
public static T FirstNonDefault<T>(this IEnumerable<T> values)
{
foreach (var value in values)
{
if (!Equals(value, default(T))) return value;
}
return default(T);
}
public static bool EquivalentTo(this byte[] bytes, byte[] other)
{
var compare = 0;
for (var i = 0; i < other.Length; i++)
compare |= other[i] ^ bytes[i];
return compare == 0;
}
public static bool EquivalentTo<T>(this T[] array, T[] otherArray, Func<T, T, bool> comparer = null)
{
if (array == null || otherArray == null)
return array == otherArray;
if (array.Length != otherArray.Length)
return false;
if (comparer == null)
comparer = (v1, v2) => v1.Equals(v2);
for (var i = 0; i < array.Length; i++)
{
if (!comparer(array[i], otherArray[i]))
return false;
}
return true;
}
public static bool EquivalentTo<T>(this IEnumerable<T> thisList, IEnumerable<T> otherList, Func<T, T, bool> comparer = null)
{
if (comparer == null)
comparer = (v1, v2) => v1.Equals(v2);
if (thisList == null || otherList == null)
return thisList == otherList;
var otherEnum = otherList.GetEnumerator();
foreach (var item in thisList)
{
if (!otherEnum.MoveNext()) return false;
var thisIsDefault = Equals(item, default(T));
var otherIsDefault = Equals(otherEnum.Current, default(T));
if (thisIsDefault || otherIsDefault)
{
return thisIsDefault && otherIsDefault;
}
if (!comparer(item, otherEnum.Current)) return false;
}
var hasNoMoreLeftAsWell = !otherEnum.MoveNext();
return hasNoMoreLeftAsWell;
}
public static bool EquivalentTo<K, V>(this IDictionary<K, V> a, IDictionary<K, V> b, Func<V,V,bool> comparer = null)
{
if (comparer == null)
comparer = (v1, v2) => v1.Equals(v2);
if (a == null || b == null)
return a == b;
if (a.Count != b.Count)
return false;
foreach (var entry in a)
{
V value;
if (!b.TryGetValue(entry.Key, out value))
return false;
if (entry.Value == null || value == null)
{
if (entry.Value == null && value == null)
continue;
return false;
}
if (!comparer(entry.Value, value))
return false;
}
return true;
}
public static IEnumerable<T[]> BatchesOf<T>(this IEnumerable<T> sequence, int batchSize)
{
var batch = new List<T>(batchSize);
foreach (var item in sequence)
{
batch.Add(item);
if (batch.Count >= batchSize)
{
yield return batch.ToArray();
batch.Clear();
}
}
if (batch.Count > 0)
{
yield return batch.ToArray();
batch.Clear();
}
}
public static Dictionary<TKey, T> ToSafeDictionary<T, TKey>(this IEnumerable<T> list, Func<T, TKey> expr)
{
var map = new Dictionary<TKey, T>();
if (list != null)
{
foreach (var item in list)
{
map[expr(item)] = item;
}
}
return map;
}
public static Dictionary<TKey, TValue> ToDictionary<T, TKey, TValue>(this IEnumerable<T> list, Func<T, KeyValuePair<TKey, TValue>> map)
{
var to = new Dictionary<TKey, TValue>();
foreach (var item in list)
{
var entry = map(item);
to[entry.Key] = entry.Value;
}
return to;
}
/// <summary>
/// Return T[0] when enumerable is null, safe to use in enumerations like foreach
/// </summary>
public static IEnumerable<T> Safe<T>(this IEnumerable<T> enumerable)
{
return enumerable ?? TypeConstants<T>.EmptyArray;
}
public static IEnumerable Safe(this IEnumerable enumerable)
{
return enumerable ?? TypeConstants.EmptyObjectArray;
}
}
}