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

« back to all changes in this revision

Viewing changes to contrib/ICSharpCode.NRefactory/TypeSystem/Implementation/SimpleInterningProvider.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) 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;
21
 
using System.Collections.Generic;
22
 
using System.Collections.ObjectModel;
23
 
using System.Linq;
24
 
using System.Runtime.CompilerServices;
25
 
 
26
 
using ICSharpCode.NRefactory.Utils;
27
 
 
28
 
namespace ICSharpCode.NRefactory.TypeSystem.Implementation
29
 
{
30
 
        /// <summary>
31
 
        /// Simple interning provider.
32
 
        /// </summary>
33
 
        public sealed class SimpleInterningProvider : IInterningProvider
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
 
                int stackDepth = 0;
86
 
                
87
 
                public T Intern<T>(T obj) where T : class
88
 
                {
89
 
                        if (obj == null)
90
 
                                return null;
91
 
                        ISupportsInterning s = obj as ISupportsInterning;
92
 
                        if (s != null) {
93
 
                                ISupportsInterning output;
94
 
                                //if (supportsInternDict.TryGetValue(s, out output)) {
95
 
                                //      obj = (T)output;
96
 
                                //} else {
97
 
                                        s.PrepareForInterning(this);
98
 
                                        if (supportsInternDict.TryGetValue(s, out output))
99
 
                                                obj = (T)output;
100
 
                                        else
101
 
                                                supportsInternDict.Add(s, s);
102
 
                                //}
103
 
                        } else if (Type.GetTypeCode(obj.GetType()) >= TypeCode.Boolean) {
104
 
                                // Intern primitive types (and strings) by value
105
 
                                object output;
106
 
                                if (byValueDict.TryGetValue(obj, out output))
107
 
                                        obj = (T)output;
108
 
                                else
109
 
                                        byValueDict.Add(obj, obj);
110
 
                        }
111
 
                        stackDepth--;
112
 
                        return obj;
113
 
                }
114
 
                
115
 
                public IList<T> InternList<T>(IList<T> list) where T : class
116
 
                {
117
 
                        if (list == null)
118
 
                                return null;
119
 
                        for (int i = 0; i < list.Count; i++) {
120
 
                                T oldItem = list[i];
121
 
                                T newItem = Intern(oldItem);
122
 
                                if (oldItem != newItem) {
123
 
                                        if (list.IsReadOnly) {
124
 
                                                T[] array = new T[list.Count];
125
 
                                                list.CopyTo(array, 0);
126
 
                                                list = array;
127
 
                                        }
128
 
                                        list[i] = newItem;
129
 
                                }
130
 
                        }
131
 
                        if (!list.IsReadOnly)
132
 
                                list = new ReadOnlyCollection<T>(list);
133
 
                        IEnumerable output;
134
 
                        if (listDict.TryGetValue(list, out output))
135
 
                                list = (IList<T>)output;
136
 
                        else
137
 
                                listDict.Add(list, list);
138
 
                        return list;
139
 
                }
140
 
        }
141
 
}