Skip to content

Commit 8e754b6

Browse files
committed
Inlined GenericUtil.GenericsByName into GenericByName.
Removed unused GenericUtil.GenericsForType. Other code quality improvements.
1 parent 0fe042c commit 8e754b6

File tree

1 file changed

+36
-62
lines changed

1 file changed

+36
-62
lines changed

src/runtime/genericutil.cs

Lines changed: 36 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,13 @@ namespace Python.Runtime
99
/// This class is responsible for efficiently maintaining the bits
1010
/// of information we need to support aliases with 'nice names'.
1111
/// </summary>
12-
internal class GenericUtil
12+
internal static class GenericUtil
1313
{
14+
/// <summary>
15+
/// Maps namespace -> generic base name -> list of generic type names
16+
/// </summary>
1417
private static Dictionary<string, Dictionary<string, List<string>>> mapping;
1518

16-
private GenericUtil()
17-
{
18-
}
19-
2019
public static void Reset()
2120
{
2221
mapping = new Dictionary<string, Dictionary<string, List<string>>>();
@@ -25,29 +24,23 @@ public static void Reset()
2524
/// <summary>
2625
/// Register a generic type that appears in a given namespace.
2726
/// </summary>
27+
/// <param name="t">A generic type definition (<c>t.IsGenericTypeDefinition</c> must be true)</param>
2828
internal static void Register(Type t)
2929
{
3030
if (null == t.Namespace || null == t.Name)
3131
{
3232
return;
3333
}
3434

35-
Dictionary<string, List<string>> nsmap = null;
36-
mapping.TryGetValue(t.Namespace, out nsmap);
37-
if (nsmap == null)
35+
Dictionary<string, List<string>> nsmap;
36+
if (!mapping.TryGetValue(t.Namespace, out nsmap))
3837
{
3938
nsmap = new Dictionary<string, List<string>>();
4039
mapping[t.Namespace] = nsmap;
4140
}
42-
string basename = t.Name;
43-
int tick = basename.IndexOf("`");
44-
if (tick > -1)
45-
{
46-
basename = basename.Substring(0, tick);
47-
}
48-
List<string> gnames = null;
49-
nsmap.TryGetValue(basename, out gnames);
50-
if (gnames == null)
41+
string basename = GetBasename(t.Name);
42+
List<string> gnames;
43+
if (!nsmap.TryGetValue(basename, out gnames))
5144
{
5245
gnames = new List<string>();
5346
nsmap[basename] = gnames;
@@ -60,9 +53,8 @@ internal static void Register(Type t)
6053
/// </summary>
6154
public static List<string> GetGenericBaseNames(string ns)
6255
{
63-
Dictionary<string, List<string>> nsmap = null;
64-
mapping.TryGetValue(ns, out nsmap);
65-
if (nsmap == null)
56+
Dictionary<string, List<string>> nsmap;
57+
if (!mapping.TryGetValue(ns, out nsmap))
6658
{
6759
return null;
6860
}
@@ -85,81 +77,63 @@ public static Type GenericForType(Type t, int paramCount)
8577
/// <summary>
8678
/// Finds a generic type in the given namespace with the given name and number of generic parameters.
8779
/// </summary>
88-
public static Type GenericByName(string ns, string name, int paramCount)
89-
{
90-
var types = GenericsByName(ns, name);
91-
if (types != null)
92-
{
93-
foreach (Type t in types)
94-
{
95-
if (t.GetGenericArguments().Length == paramCount)
96-
{
97-
return t;
98-
}
99-
}
100-
}
101-
return null;
102-
}
103-
104-
public static List<Type> GenericsForType(Type t)
80+
public static Type GenericByName(string ns, string basename, int paramCount)
10581
{
106-
return GenericsByName(t.Namespace, t.Name);
107-
}
108-
109-
public static List<Type> GenericsByName(string ns, string basename)
110-
{
111-
Dictionary<string, List<string>> nsmap = null;
112-
mapping.TryGetValue(ns, out nsmap);
113-
if (nsmap == null)
82+
Dictionary<string, List<string>> nsmap;
83+
if (!mapping.TryGetValue(ns, out nsmap))
11484
{
11585
return null;
11686
}
11787

118-
int tick = basename.IndexOf("`");
119-
if (tick > -1)
120-
{
121-
basename = basename.Substring(0, tick);
122-
}
123-
124-
List<string> names = null;
125-
nsmap.TryGetValue(basename, out names);
126-
if (names == null)
88+
List<string> names;
89+
if (!nsmap.TryGetValue(GetBasename(basename), out names))
12790
{
12891
return null;
12992
}
13093

131-
var result = new List<Type>();
13294
foreach (string name in names)
13395
{
13496
string qname = ns + "." + name;
13597
Type o = AssemblyManager.LookupTypes(qname).FirstOrDefault();
136-
if (o != null)
98+
if (o != null && o.GetGenericArguments().Length == paramCount)
13799
{
138-
result.Add(o);
100+
return o;
139101
}
140102
}
141103

142-
return result;
104+
return null;
143105
}
144106

145107
/// <summary>
146108
/// xxx
147109
/// </summary>
148110
public static string GenericNameForBaseName(string ns, string name)
149111
{
150-
Dictionary<string, List<string>> nsmap = null;
151-
mapping.TryGetValue(ns, out nsmap);
152-
if (nsmap == null)
112+
Dictionary<string, List<string>> nsmap;
113+
if (!mapping.TryGetValue(ns, out nsmap))
153114
{
154115
return null;
155116
}
156-
List<string> gnames = null;
117+
List<string> gnames;
157118
nsmap.TryGetValue(name, out gnames);
158119
if (gnames?.Count > 0)
159120
{
160121
return gnames[0];
161122
}
162123
return null;
163124
}
125+
126+
private static string GetBasename(string name)
127+
{
128+
int tick = name.IndexOf("`");
129+
if (tick > -1)
130+
{
131+
return name.Substring(0, tick);
132+
}
133+
else
134+
{
135+
return name;
136+
}
137+
}
164138
}
165139
}

0 commit comments

Comments
 (0)