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

« back to all changes in this revision

Viewing changes to external/nrefactory/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) 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;
 
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 : 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
}