1
// Copyright (c) 2010-2013 AlphaSierraPapa for the SharpDevelop Team
3
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
4
// software and associated documentation files (the "Software"), to deal in the Software
5
// without restriction, including without limitation the rights to use, copy, modify, merge,
6
// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
7
// to whom the Software is furnished to do so, subject to the following conditions:
9
// The above copyright notice and this permission notice shall be included in all copies or
10
// substantial portions of the Software.
12
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
13
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
14
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
15
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
16
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
17
// DEALINGS IN THE SOFTWARE.
20
using System.Collections.Generic;
23
namespace ICSharpCode.NRefactory.Utils
26
/// A dictionary that allows multiple pairs with the same key.
28
public class MultiDictionary<TKey, TValue> : ILookup<TKey, TValue>
30
readonly Dictionary<TKey, List<TValue>> dict;
32
public MultiDictionary()
34
dict = new Dictionary<TKey, List<TValue>>();
37
public MultiDictionary(IEqualityComparer<TKey> comparer)
39
dict = new Dictionary<TKey, List<TValue>>(comparer);
42
public void Add(TKey key, TValue value)
44
List<TValue> valueList;
45
if (!dict.TryGetValue(key, out valueList)) {
46
valueList = new List<TValue>();
47
dict.Add(key, valueList);
52
public bool Remove(TKey key, TValue value)
54
List<TValue> valueList;
55
if (dict.TryGetValue(key, out valueList)) {
56
if (valueList.Remove(value)) {
57
if (valueList.Count == 0)
66
/// Removes all entries with the specified key.
68
/// <returns>Returns true if at least one entry was removed.</returns>
69
public bool RemoveAll(TKey key)
71
return dict.Remove(key);
80
public IReadOnlyList<TValue> this[TKey key] {
82
public IList<TValue> this[TKey key] {
86
if (dict.TryGetValue(key, out list))
89
return EmptyList<TValue>.Instance;
94
/// Returns the number of different keys.
97
get { return dict.Count; }
100
public ICollection<TKey> Keys {
101
get { return dict.Keys; }
104
public IEnumerable<TValue> Values {
105
get { return dict.Values.SelectMany(list => list); }
108
IEnumerable<TValue> ILookup<TKey, TValue>.this[TKey key] {
109
get { return this[key]; }
112
bool ILookup<TKey, TValue>.Contains(TKey key)
114
return dict.ContainsKey(key);
117
public IEnumerator<IGrouping<TKey, TValue>> GetEnumerator()
119
foreach (var pair in dict)
120
yield return new Grouping(pair.Key, pair.Value);
123
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
125
return GetEnumerator();
128
sealed class Grouping : IGrouping<TKey, TValue>
131
readonly List<TValue> values;
133
public Grouping(TKey key, List<TValue> values)
136
this.values = values;
143
public IEnumerator<TValue> GetEnumerator()
145
return values.GetEnumerator();
148
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
150
return values.GetEnumerator();