-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathAdminShellCollections.cs
More file actions
144 lines (121 loc) · 3.78 KB
/
Copy pathAdminShellCollections.cs
File metadata and controls
144 lines (121 loc) · 3.78 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
/*
Copyright (c) 2018-2023 Festo SE & Co. KG <https://www.festo.com/net/de_de/Forms/web/contact_international>
Author: Michael Hoffmeister
This source code is licensed under the Apache License 2.0 (see LICENSE.txt).
This source code may use other Open Source software components (see LICENSE.txt).
*/
using System.Collections.Generic;
using System.Linq;
namespace AdminShellNS
{
/// <summary>
/// Just add some convenience methods to <c>Dictionary</c>
/// Note: Not an extension class in order to not interfere with really
/// commonly used standard class.
/// </summary>
public class ConvenientDictionary<K, V> : Dictionary<K, V>
{
public V GetValueOrDefault(K key)
{
if (key != null && this.ContainsKey(key))
return this[key];
return default(V);
}
}
public class MultiValueDictionary<K, V>
{
private Dictionary<K, List<V>> dict = new Dictionary<K, List<V>>();
public void Add(K key, V value)
{
if (key == null)
return;
if (dict.TryGetValue(key, out var list))
list.Add(value);
else
dict.Add(key, new List<V> { value });
}
public void AddIfValueIsNew(K key, V value)
{
if (key == null)
return;
if (dict.TryGetValue(key, out var list))
{
if (!list.Contains(value))
list.Add(value);
}
else
dict.Add(key, new List<V> { value });
}
public void Remove(K key)
{
if (dict.ContainsKey(key))
dict.Remove(key);
}
public bool ContainsKey(K key) => dict.ContainsKey(key);
public List<V> this[K key] => dict[key];
public IEnumerable<List<V>> ValueLists
{
get
{
return dict.Values;
}
}
public IEnumerable<V> Values
{
get
{
foreach (var vl in dict.Values)
foreach (var v in vl)
yield return v;
}
}
public IEnumerable<K> Keys
{
get
{
return dict.Keys;
}
}
public void Clear() => dict.Clear();
public IEnumerable<V> All(K key)
{
if (!dict.ContainsKey(key))
yield break;
foreach (var x in dict[key])
yield return x;
}
}
public class DoubleSidedDict<T1, T2>
{
private Dictionary<T1, T2> _forward = new Dictionary<T1, T2>();
private Dictionary<T2, T1> _backward = new Dictionary<T2, T1>();
public void AddPair(T1 item1, T2 item2)
{
_forward.Add(item1, item2);
_backward.Add(item2, item1);
}
public bool Contains1(T1 key1) => _forward.ContainsKey(key1);
public bool Contains2(T2 key2) => _backward.ContainsKey(key2);
public T2 Get2(T1 key1) => _forward[key1];
public T1 Get1(T2 key2) => _backward[key2];
public T2 Get2OrDefault(T1 key1)
=> (key1 != null && _forward.ContainsKey(key1)) ? _forward[key1] : default(T2);
public T1 Get1OrDefault(T2 key2)
=> (key2 != null && _backward.ContainsKey(key2)) ? _backward[key2] : default(T1);
public void Clear() { _forward.Clear(); _backward.Clear(); }
}
public class IntValueDictionary<K> : Dictionary<K, int>
{
public void IncKey(K key)
{
if (!this.ContainsKey(key))
this.Add(key, 1);
else
{
var i = this[key];
this.Remove(key);
this.Add(key, i + 1);
}
}
}
}