Free cookie consent management tool by TermsFeed Policy Generator

source: branches/CodeEditor/HeuristicLab.ExtLibs/HeuristicLab.NRefactory/5.5.0/NRefactory-5.5.0/TypeSystem/Implementation/SimpleInterningProvider.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: 4.4 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;
21using System.Collections.Generic;
22using System.Collections.ObjectModel;
23using System.Linq;
24using System.Runtime.CompilerServices;
25
26using ICSharpCode.NRefactory.Utils;
27
28namespace ICSharpCode.NRefactory.TypeSystem.Implementation
29{
30  /// <summary>
31  /// Simple interning provider.
32  /// </summary>
33  public sealed class SimpleInterningProvider : InterningProvider
34  {
35    sealed class InterningComparer : IEqualityComparer<ISupportsInterning>
36    {
37      public bool Equals(ISupportsInterning x, ISupportsInterning y)
38      {
39        return x.EqualsForInterning(y);
40      }
41     
42      public int GetHashCode(ISupportsInterning obj)
43      {
44        return obj.GetHashCodeForInterning();
45      }
46    }
47   
48    sealed class ListComparer : IEqualityComparer<IEnumerable>
49    {
50      public bool Equals(IEnumerable a, IEnumerable b)
51      {
52        if (a.GetType() != b.GetType())
53          return false;
54        IEnumerator e1 = a.GetEnumerator();
55        IEnumerator e2 = b.GetEnumerator();
56        while (e1.MoveNext()) {
57          // e1 has more elements than e2; or elements are different
58          if (!e2.MoveNext() || e1.Current != e2.Current)
59            return false;
60        }
61        if (e2.MoveNext()) // e2 has more elements than e1
62          return false;
63        // No need to dispose e1/e2: non-generic IEnumerator doesn't implement IDisposable,
64        // and the underlying enumerator will likely be a List<T>.Enumerator which has an empty Dispose() method.
65        return true;
66      }
67     
68      public int GetHashCode(IEnumerable obj)
69      {
70        int hashCode = obj.GetType().GetHashCode();
71        unchecked {
72          foreach (object o in obj) {
73            hashCode *= 27;
74            hashCode += RuntimeHelpers.GetHashCode(o);
75          }
76        }
77        return hashCode;
78      }
79    }
80   
81    Dictionary<object, object> byValueDict = new Dictionary<object, object>();
82    Dictionary<ISupportsInterning, ISupportsInterning> supportsInternDict = new Dictionary<ISupportsInterning, ISupportsInterning>(new InterningComparer());
83    Dictionary<IEnumerable, IEnumerable> listDict = new Dictionary<IEnumerable, IEnumerable>(new ListComparer());
84   
85    public override ISupportsInterning Intern(ISupportsInterning obj)
86    {
87      if (obj == null)
88        return null;
89     
90      // ensure objects are frozen when we put them into the dictionary
91      // note that Freeze may change the hash code of the object
92      FreezableHelper.Freeze(obj);
93
94      ISupportsInterning output;
95      if (supportsInternDict.TryGetValue(obj, out output)) {
96        return output;
97      } else {
98        supportsInternDict.Add(obj, obj);
99        return obj;
100      }
101    }
102   
103    public override string Intern(string text)
104    {
105      if (text == null)
106        return null;
107     
108      object output;
109      if (byValueDict.TryGetValue(text, out output))
110        return (string)output;
111      else
112        return text;
113    }
114   
115    public override object InternValue(object obj)
116    {
117      if (obj == null)
118        return null;
119     
120      object output;
121      if (byValueDict.TryGetValue(obj, out output))
122        return output;
123      else
124        return obj;
125    }
126   
127    public override IList<T> InternList<T>(IList<T> list)
128    {
129      if (list == null)
130        return null;
131      if (list.Count == 0)
132        return EmptyList<T>.Instance;
133      if (!list.IsReadOnly)
134        list = new ReadOnlyCollection<T>(list);
135      IEnumerable output;
136      if (listDict.TryGetValue(list, out output))
137        list = (IList<T>)output;
138      else
139        listDict.Add(list, list);
140      return list;
141    }
142  }
143}
Note: See TracBrowser for help on using the repository browser.