-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathDictionaryExtension.cs
131 lines (117 loc) · 3.39 KB
/
DictionaryExtension.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
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
namespace Bingo.Utils
{
static class DictionaryExtension
{
public static TValue GetOrDefault<TKey, TValue>(this Dictionary<TKey, TValue> dict, TKey key, TValue defaultValue = default(TValue))
{
if(dict == null)
return defaultValue;
if(key == null)
return defaultValue;
TValue value;
return dict.TryGetValue(key, out value) ? value : defaultValue;
}
public static TValue GetOrDefault<TKey, TValue>(this ConcurrentDictionary<TKey, TValue> dict, TKey key, TValue defaultValue = default(TValue))
{
if(dict == null)
return defaultValue;
if(key == null)
return defaultValue;
TValue value;
return dict.TryGetValue(key, out value) ? value : defaultValue;
}
public static string GetOrSelf(this Dictionary<string, string> dict, string val)
{
if(dict == null || val == null)
return val;
string mapped;
return dict.TryGetValue(val, out mapped) ? mapped : val;
}
public static void AddToList<TKey, TValue>(this Dictionary<TKey, List<TValue>> dict, TKey key, TValue addValue)
{
List<TValue> list;
if (!dict.TryGetValue(key, out list))
dict[key] = new List<TValue> { addValue };
else
list.Add(addValue);
}
public static void RemoveFromList<TKey, TValue>(this Dictionary<TKey, List<TValue>> dict, TKey key, TValue removeValue)
{
List<TValue> list;
if(!dict.TryGetValue(key, out list))
return;
list.Remove(removeValue);
if (list.Count == 0)
dict.Remove(key);
}
public static void AddToList<TKey, TValue>(this ConcurrentDictionary<TKey, List<TValue>> dict, TKey key, TValue addValue)
{
dict.AddOrUpdate(key, inn => new List<TValue> { addValue }, (inn, list) =>
{
lock (list)
{
list.Add(addValue);
return list;
}
});
}
public static bool AddToSet<TKey, TValue>(this ConcurrentDictionary<TKey, HashSet<TValue>> dict, TKey key, TValue addValue)
{
var isNew = true;
dict.AddOrUpdate(key, inn => new HashSet<TValue> { addValue }, (inn, list) =>
{
lock (list)
{
isNew = list.Add(addValue);
return list;
}
});
return isNew;
}
public static List<TValue> GetClonedSet<TKey, TValue>(this ConcurrentDictionary<TKey, HashSet<TValue>> dict, TKey key)
{
var set = dict.GetOrDefault(key);
lock(set)
{
return set.ToList();
}
}
public static bool RemoveFromSet<TKey, TValue>(this ConcurrentDictionary<TKey, HashSet<TValue>> dict, TKey key, TValue removeValue)
{
var success = true;
dict.AddOrUpdate(key, inn => new HashSet<TValue>(), (inn, list) =>
{
lock (list)
{
success = list.Remove(removeValue);
return list;
}
});
return success;
}
public static void AddToUniqList<TKey, TValue>(this ConcurrentDictionary<TKey, List<TValue>> dict, TKey key, TValue addValue)
{
dict.AddOrUpdate(key, inn => new List<TValue> { addValue }, (inn, list) =>
{
lock (list)
{
if (!list.Contains(addValue))
list.Add(addValue);
return list;
}
});
}
public static bool IsInList<TKey, TValue>(this ConcurrentDictionary<TKey, List<TValue>> dict, TKey key, TValue value)
{
return dict.GetOrDefault(key)?.Contains(value) ?? false;
}
public static bool TryRemove<TKey, TValue>(this ConcurrentDictionary<TKey, TValue> dict, TKey key)
{
TValue temp;
return dict.TryRemove(key, out temp);
}
}
}