Free cookie consent management tool by TermsFeed Policy Generator

source: branches/CodeEditor/HeuristicLab.ExtLibs/HeuristicLab.NRefactory/5.5.0/NRefactory-5.5.0/Utils/MultiDictionary.cs @ 11700

Last change on this file since 11700 was 11700, checked in by jkarder, 9 years ago

#2077: created branch and added first version

File size: 3.9 KB
Line 
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
19using System;
20using System.Collections.Generic;
21using System.Linq;
22
23namespace 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}
Note: See TracBrowser for help on using the repository browser.