-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathHelpers.cs
70 lines (62 loc) · 2.28 KB
/
Helpers.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
using System.Collections.Generic;
namespace GitImporter
{
public static class Helpers
{
public static C AddToCollection<K, C, V>(this Dictionary<K, C> dict, K key, V toAdd) where C : ICollection<V>, new()
{
C collection;
if (!dict.TryGetValue(key, out collection))
{
collection = new C();
dict.Add(key, collection);
}
collection.Add(toAdd);
return collection;
}
public static bool RemoveFromCollection<K, C, V>(this Dictionary<K, C> dict, K key, V toRemove) where C : ICollection<V>, new()
{
C collection;
if (!dict.TryGetValue(key, out collection))
return false;
bool removed = collection.Remove(toRemove);
if (collection.Count == 0)
dict.Remove(key);
return removed;
}
public static C AddToCollection<K, C, V>(this List<KeyValuePair<K, C>> dict, K key, V toAdd) where C : ICollection<V>, new()
{
C collection = default(C);
foreach (var pair in dict)
if (pair.Key.Equals(key))
{
collection = pair.Value;
break;
}
if (Equals(collection, default(C)))
{
collection = new C();
dict.Add(new KeyValuePair<K, C>(key, collection));
}
collection.Add(toAdd);
return collection;
}
public static bool RemoveFromCollection<K, C, V>(this List<KeyValuePair<K, C>> dict, K key, V toRemove) where C : ICollection<V>, new()
{
C collection = default(C);
int index;
for (index = 0; index < dict.Count; index++)
if (dict[index].Key.Equals(key))
{
collection = dict[index].Value;
break;
}
if (Equals(collection, default(C)))
return false;
bool removed = collection.Remove(toRemove);
if (collection.Count == 0)
dict.RemoveAt(index);
return removed;
}
}
}