/
Multimap.cs
200 lines (180 loc) · 8 KB
/
Multimap.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
// -------------------------------------------------------------------------------------------------
// <copyright file="Multimap.cs" company="Ninject Project Contributors">
// Copyright (c) 2007-2010 Enkari, Ltd. All rights reserved.
// Copyright (c) 2010-2020 Ninject Project Contributors. All rights reserved.
//
// Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL).
// You may not use this file except in compliance with one of the Licenses.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
// or
// http://www.microsoft.com/opensource/licenses.mspx
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// </copyright>
// -------------------------------------------------------------------------------------------------
namespace Ninject.Infrastructure
{
using System;
using System.Collections;
using System.Collections.Generic;
/// <summary>
/// A data structure that contains multiple values for a each key.
/// </summary>
/// <typeparam name="TKey">The type of key.</typeparam>
/// <typeparam name="TValue">The type of value.</typeparam>
public class Multimap<TKey, TValue> : IEnumerable<KeyValuePair<TKey, IList<TValue>>>
{
private readonly Dictionary<TKey, IList<TValue>> items;
/// <summary>
/// Initializes a new instance of the <see cref="Multimap{TKey, TValue}"/> class that is empty,
/// has the default initial capacity, and uses the default <see cref="IEqualityComparer{T}"/>.
/// </summary>
public Multimap()
{
this.items = new Dictionary<TKey, IList<TValue>>();
}
/// <summary>
/// Initializes a new instance of the <see cref="Multimap{TKey, TValue}"/> class that is empty,
/// has the default initial capacity, and uses the specified <see cref="IEqualityComparer{T}"/>.
/// </summary>
/// <param name="comparer">The <see cref="IEqualityComparer{T}"/> implementation to use when comparing keys, or <see langword="null"/> to use the default <see cref="EqualityComparer{T}"/> for the type of the key.</param>
public Multimap(IEqualityComparer<TKey> comparer)
{
this.items = new Dictionary<TKey, IList<TValue>>(comparer);
}
/// <summary>
/// Gets the collection of keys.
/// </summary>
public ICollection<TKey> Keys
{
get { return this.items.Keys; }
}
/// <summary>
/// Gets the collection of collections of values.
/// </summary>
public ICollection<IList<TValue>> Values
{
get { return this.items.Values; }
}
/// <summary>
/// Gets the collection of values stored under the specified key.
/// </summary>
/// <param name="key">The key.</param>
/// <exception cref="ArgumentNullException"><paramref name="key"/> is <see langword="null"/>.</exception>
public IList<TValue> this[TKey key]
{
get
{
if (this.items.TryGetValue(key, out var values))
{
return values;
}
values = new List<TValue>();
this.items[key] = values;
return values;
}
}
/// <summary>
/// Adds the specified value for the specified key.
/// </summary>
/// <param name="key">The key.</param>
/// <param name="value">The value.</param>
/// <exception cref="ArgumentNullException"><paramref name="key"/> is <see langword="null"/>.</exception>
/// <exception cref="ArgumentNullException"><paramref name="value"/> is <see langword="null"/>.</exception>
public void Add(TKey key, TValue value)
{
Ensure.ArgumentNotNull(key, nameof(key));
Ensure.ArgumentNotNull(value, nameof(value));
this[key].Add(value);
}
/// <summary>
/// Removes the specified value for the specified key.
/// </summary>
/// <param name="key">The key.</param>
/// <param name="value">The value.</param>
/// <returns>
/// <see langword="true"/> if such a value existed and was removed; otherwise, <see langword="false"/>.
/// </returns>
/// <exception cref="ArgumentNullException"><paramref name="key"/> is <see langword="null"/>.</exception>
/// <exception cref="ArgumentNullException"><paramref name="value"/> is <see langword="null"/>.</exception>
public bool Remove(TKey key, TValue value)
{
Ensure.ArgumentNotNull(key, nameof(key));
Ensure.ArgumentNotNull(value, nameof(value));
if (this.items.TryGetValue(key, out var values))
{
return values.Remove(value);
}
return false;
}
/// <summary>
/// Removes all values for the specified key.
/// </summary>
/// <param name="key">The key.</param>
/// <returns>
/// <see langword="true"/> if any such values existed; otherwise, <see langword="false"/>.
/// </returns>
/// <exception cref="ArgumentNullException"><paramref name="key"/> is <see langword="null"/>.</exception>
public bool RemoveAll(TKey key)
{
return this.items.Remove(key);
}
/// <summary>
/// Removes all values.
/// </summary>
public void Clear()
{
this.items.Clear();
}
/// <summary>
/// Determines whether the multimap contains any values for the specified key.
/// </summary>
/// <param name="key">The key.</param>
/// <returns>
/// <see langword="true"/> if the multimap has one or more values for the specified key; otherwise, <see langword="false"/>.
/// </returns>
/// <exception cref="ArgumentNullException"><paramref name="key"/> is <see langword="null"/>.</exception>
public bool ContainsKey(TKey key)
{
return this.items.ContainsKey(key);
}
/// <summary>
/// Determines whether the multimap contains the specified value for the specified key.
/// </summary>
/// <param name="key">The key.</param>
/// <param name="value">The value.</param>
/// <returns>
/// <see langword="true"/> if the multimap contains such a value; otherwise, <see langword="false"/>.
/// </returns>
/// <exception cref="ArgumentNullException"><paramref name="key"/> is <see langword="null"/>.</exception>
/// <exception cref="ArgumentNullException"><paramref name="value"/> is <see langword="null"/>.</exception>
public bool ContainsValue(TKey key, TValue value)
{
Ensure.ArgumentNotNull(key, nameof(key));
Ensure.ArgumentNotNull(value, nameof(value));
return this.items.TryGetValue(key, out var values) && values.Contains(value);
}
/// <summary>
/// Returns an enumerator that iterates through a the multimap.
/// </summary>
/// <returns>An <see cref="IEnumerator"/> object that can be used to iterate through the multimap.</returns>
public IEnumerator GetEnumerator()
{
return this.items.GetEnumerator();
}
/// <summary>
/// Returns an enumerator that iterates through a the multimap.
/// </summary>
/// <returns>An <see cref="IEnumerator{T}"/> object that can be used to iterate through the multimap.</returns>
IEnumerator<KeyValuePair<TKey, IList<TValue>>> IEnumerable<KeyValuePair<TKey, IList<TValue>>>.GetEnumerator()
{
return this.items.GetEnumerator();
}
}
}