-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIDictionaryExtension.cs
394 lines (317 loc) · 11 KB
/
IDictionaryExtension.cs
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
using System;
using System.Collections;
using System.Collections.Generic;
namespace Aya.Extension
{
public static partial class IDictionaryExtension
{
internal static Random Rand = new Random();
#region Null / Empty
public static bool IsNullOrEmpty<TKey, TValue>(this IDictionary<TKey, TValue> dictionary)
{
var result = dictionary == null || dictionary.Count == 0;
return result;
}
public static bool IsEmpty<TKey, TValue>(this IDictionary<TKey, TValue> dictionary)
{
if (dictionary == null)
{
throw new NullReferenceException();
}
var result = dictionary.Count == 0;
return result;
}
public static bool IsNullOrEmpty(this IDictionary dictionary)
{
var result = dictionary == null || dictionary.Count == 0;
return result;
}
public static bool IsEmpty(this IDictionary dictionary)
{
if (dictionary == null)
{
throw new NullReferenceException();
}
var result = dictionary.Count == 0;
return result;
}
#endregion
#region Add T
public static IDictionary<TKey, TValue> Add<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, KeyValuePair<TKey, TValue> pair)
{
dictionary.Add(pair.Key, pair.Value);
return dictionary;
}
public static bool TryAdd<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, KeyValuePair<TKey, TValue> pair)
{
if (dictionary.ContainsKey(pair.Key)) return false;
dictionary.Add(pair.Key, pair.Value);
return true;
}
public static bool TryAdd<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TKey key, TValue value)
{
if (dictionary.ContainsKey(key)) return false;
dictionary.Add(key, value);
return true;
}
public static IDictionary<TKey, TValue> AddOrReplace<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TKey key, TValue value)
{
dictionary[key] = value;
return dictionary;
}
public static IDictionary<TKey, TValue> AddRange<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, IEnumerable<KeyValuePair<TKey, TValue>> values,
bool replaceExisted)
{
foreach (var item in values)
{
if (!dictionary.ContainsKey(item.Key) || replaceExisted)
{
dictionary[item.Key] = item.Value;
}
}
return dictionary;
}
#endregion
#region Add
public static bool TryAdd(this IDictionary dictionary, object key, object value)
{
if (dictionary.Contains(key)) return false;
dictionary.Add(key, value);
return true;
}
public static IDictionary AddOrReplace(this IDictionary dictionary, object key, object value)
{
dictionary[key] = value;
return dictionary;
}
public static IDictionary AddRange(this IDictionary dictionary, IDictionary values, bool replaceExisted)
{
foreach (var key in values.Keys)
{
var value = values[key];
if (!dictionary.Contains(key) || replaceExisted)
{
dictionary[key] = value;
}
}
return dictionary;
}
#endregion
#region Merge T
public static IDictionary<TKey, TValue> Merge<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, params IDictionary<TKey, TValue>[] dictionaries)
{
foreach (var dic in dictionaries)
{
dictionary.AddRange(dic, true);
}
return dictionary;
}
#endregion
#region Get T
public static TValue GetValue<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TKey key, TValue defaultValue = default(TValue))
{
if (key == null) return defaultValue;
return dictionary.TryGetValue(key, out var ret) ? ret : defaultValue;
}
public static TValue GetOrAdd<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TKey key, TValue defaultValue = default(TValue))
{
if (dictionary.TryGetValue(key, out var ret))
{
return ret;
}
dictionary.Add(key, defaultValue);
return defaultValue;
}
public static TValue Random<TKey, TValue>(this IDictionary<TKey, TValue> dictionary)
{
if (dictionary.Count < 1) return default(TValue);
int i = 0, index = Rand.Next(0, dictionary.Count);
var result = default(TValue);
foreach (var value in dictionary.Values)
{
if (i >= index)
{
result = value;
break;
}
i++;
}
return result;
}
#endregion
#region Get
public static object GetValue(this IDictionary dictionary, object key, object defaultValue = default(object))
{
if (key == null) return defaultValue;
var ret = dictionary[key];
return ret;
}
public static object GetOrAdd(this IDictionary dictionary, object key, object defaultValue = default(object))
{
var ret = dictionary[key];
if (ret != null)
{
return ret;
}
dictionary.Add(key, defaultValue);
return defaultValue;
}
public static object Random(this IDictionary dictionary)
{
if (dictionary.Count < 1) return default(object);
int i = 0, index = Rand.Next(0, dictionary.Count);
var result = default(object);
foreach (var value in dictionary.Values)
{
if (i >= index)
{
result = value;
break;
}
i++;
}
return result;
}
#endregion
#region Foreach T
public static IDictionary<TKey, TValue> ForEach<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, Action<TKey, TValue> action)
{
if (action == null) return dictionary;
foreach (var key in dictionary.Keys)
{
var value = dictionary[key];
action(key, value);
}
return dictionary;
}
public static IDictionary<TKey, TValue> ForEach<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, Func<TKey, TValue, bool> selector,
Action<TKey, TValue> action)
{
if (selector == null || action == null) return dictionary;
foreach (var item in dictionary)
{
if (!selector(item.Key, item.Value)) continue;
action(item.Key, item.Value);
}
return dictionary;
}
#endregion
#region Foreach
public static IDictionary ForEach(this IDictionary dictionary, Action<object> action)
{
if (action == null) return dictionary;
foreach (var item in dictionary)
{
action(item);
}
return dictionary;
}
public static IDictionary ForEach(this IDictionary dictionary, Func<object, bool> selector, Action<object> action)
{
if (selector == null || action == null) return dictionary;
foreach (var item in dictionary)
{
if (!selector(item)) continue;
action(item);
}
return dictionary;
}
#endregion
#region Find T
public static KeyValuePair<TKey, TValue>? Find<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, Func<TKey, TValue, bool> selector)
{
if (selector == null) return null;
foreach (var pair in dictionary)
{
if (selector(pair.Key, pair.Value))
{
return pair;
}
}
return null;
}
public static IDictionary<TKey, TValue> FindAll<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, Func<TKey, TValue, bool> selector)
{
if (selector == null) return null;
var ret = new Dictionary<TKey, TValue>();
foreach (var pair in dictionary)
{
if (selector(pair.Key, pair.Value))
{
ret.Add(pair);
}
}
return ret;
}
#endregion
#region Find
public static object Find(this IDictionary dictionary, Func<object, bool> selector)
{
if (selector == null) return null;
foreach (var value in dictionary)
{
if (selector(value))
{
return value;
}
}
return null;
}
public static IDictionary FindAll(this IDictionary dictionary, Func<object, object, bool> selector)
{
if (selector == null) return null;
var ret = new Hashtable();
foreach (var key in dictionary.Keys)
{
var value = dictionary[key];
if (selector(key, value))
{
ret.Add(key, value);
}
}
return ret;
}
#endregion
#region Remove T
public static bool RemoveValue<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TValue value)
{
var match = false;
var key = default(TKey);
foreach (var kv in dictionary)
{
if (!kv.Value.Equals(value)) continue;
key = kv.Key;
match = true;
break;
}
if (match)
{
dictionary.Remove(key);
return true;
}
return false;
}
#endregion
#region Remove
public static bool RemoveValue<TValue>(this IDictionary dictionary, TValue value)
{
var match = false;
object key = null;
foreach (var k in dictionary.Keys)
{
var v = dictionary[k];
if (!v.Equals(value)) continue;
key = k;
match = true;
break;
}
if (match)
{
dictionary.Remove(key);
return true;
}
return false;
}
#endregion
}
}