~ubuntu-branches/ubuntu/trusty/monodevelop/trusty-proposed

« back to all changes in this revision

Viewing changes to external/nrefactory/ICSharpCode.NRefactory/Utils/MultiDictionary.cs

  • Committer: Package Import Robot
  • Author(s): Jo Shields
  • Date: 2013-05-12 09:46:03 UTC
  • mto: This revision was merged to the branch mainline in revision 29.
  • Revision ID: package-import@ubuntu.com-20130512094603-mad323bzcxvmcam0
Tags: upstream-4.0.5+dfsg
Import upstream version 4.0.5+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// Copyright (c) 2010-2013 AlphaSierraPapa for the SharpDevelop Team
 
2
// 
 
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:
 
8
// 
 
9
// The above copyright notice and this permission notice shall be included in all copies or
 
10
// substantial portions of the Software.
 
11
// 
 
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.
 
18
 
 
19
using System;
 
20
using System.Collections.Generic;
 
21
using System.Linq;
 
22
 
 
23
namespace ICSharpCode.NRefactory.Utils
 
24
{
 
25
        /// <summary>
 
26
        /// A dictionary that allows multiple pairs with the same key.
 
27
        /// </summary>
 
28
        public class MultiDictionary<TKey, TValue> : ILookup<TKey, TValue>
 
29
        {
 
30
                readonly Dictionary<TKey, List<TValue>> dict;
 
31
                
 
32
                public MultiDictionary()
 
33
                {
 
34
                        dict = new Dictionary<TKey, List<TValue>>();
 
35
                }
 
36
                
 
37
                public MultiDictionary(IEqualityComparer<TKey> comparer)
 
38
                {
 
39
                        dict = new Dictionary<TKey, List<TValue>>(comparer);
 
40
                }
 
41
                
 
42
                public void Add(TKey key, TValue value)
 
43
                {
 
44
                        List<TValue> valueList;
 
45
                        if (!dict.TryGetValue(key, out valueList)) {
 
46
                                valueList = new List<TValue>();
 
47
                                dict.Add(key, valueList);
 
48
                        }
 
49
                        valueList.Add(value);
 
50
                }
 
51
 
 
52
                public bool Remove(TKey key, TValue value)
 
53
                {
 
54
                        List<TValue> valueList;
 
55
                        if (dict.TryGetValue(key, out valueList)) {
 
56
                                if (valueList.Remove(value)) {
 
57
                                        if (valueList.Count == 0)
 
58
                                                dict.Remove(key);
 
59
                                        return true;
 
60
                                }
 
61
                        }
 
62
                        return false;
 
63
                }
 
64
                
 
65
                /// <summary>
 
66
                /// Removes all entries with the specified key.
 
67
                /// </summary>
 
68
                /// <returns>Returns true if at least one entry was removed.</returns>
 
69
                public bool RemoveAll(TKey key)
 
70
                {
 
71
                        return dict.Remove(key);
 
72
                }
 
73
                
 
74
                public void Clear()
 
75
                {
 
76
                        dict.Clear();
 
77
                }
 
78
                
 
79
                #if NET_4_5
 
80
                public IReadOnlyList<TValue> this[TKey key] {
 
81
                #else
 
82
                public IList<TValue> this[TKey key] {
 
83
                #endif
 
84
                        get {
 
85
                                List<TValue> list;
 
86
                                if (dict.TryGetValue(key, out list))
 
87
                                        return list;
 
88
                                else
 
89
                                        return EmptyList<TValue>.Instance;
 
90
                        }
 
91
                }
 
92
                
 
93
                /// <summary>
 
94
                /// Returns the number of different keys.
 
95
                /// </summary>
 
96
                public int Count {
 
97
                        get { return dict.Count; }
 
98
                }
 
99
                
 
100
                public ICollection<TKey> Keys {
 
101
                        get { return dict.Keys; }
 
102
                }
 
103
                
 
104
                public IEnumerable<TValue> Values {
 
105
                        get { return dict.Values.SelectMany(list => list); }
 
106
                }
 
107
                
 
108
                IEnumerable<TValue> ILookup<TKey, TValue>.this[TKey key] {
 
109
                        get { return this[key]; }
 
110
                }
 
111
                
 
112
                bool ILookup<TKey, TValue>.Contains(TKey key)
 
113
                {
 
114
                        return dict.ContainsKey(key);
 
115
                }
 
116
                
 
117
                public IEnumerator<IGrouping<TKey, TValue>> GetEnumerator()
 
118
                {
 
119
                        foreach (var pair in dict)
 
120
                                yield return new Grouping(pair.Key, pair.Value);
 
121
                }
 
122
                
 
123
                System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
 
124
                {
 
125
                        return GetEnumerator();
 
126
                }
 
127
                
 
128
                sealed class Grouping : IGrouping<TKey, TValue>
 
129
                {
 
130
                        readonly TKey key;
 
131
                        readonly List<TValue> values;
 
132
                        
 
133
                        public Grouping(TKey key, List<TValue> values)
 
134
                        {
 
135
                                this.key = key;
 
136
                                this.values = values;
 
137
                        }
 
138
                        
 
139
                        public TKey Key {
 
140
                                get { return key; }
 
141
                        }
 
142
                        
 
143
                        public IEnumerator<TValue> GetEnumerator()
 
144
                        {
 
145
                                return values.GetEnumerator();
 
146
                        }
 
147
                        
 
148
                        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
 
149
                        {
 
150
                                return values.GetEnumerator();
 
151
                        }
 
152
                }
 
153
        }
 
154
}