using System.Linq;
using System;
using System.Collections.Generic;
using System.Resources;
namespace Python.Runtime
{
///
/// This class is responsible for efficiently maintaining the bits
/// of information we need to support aliases with 'nice names'.
///
internal static class GenericUtil
{
///
/// Maps namespace -> generic base name -> list of generic type names
///
private static Dictionary>> mapping = new();
public static void Reset()
{
mapping = new Dictionary>>();
}
///
/// Register a generic type that appears in a given namespace.
///
/// A generic type definition (t.IsGenericTypeDefinition must be true)
internal static void Register(Type t)
{
if (null == t.Namespace || null == t.Name)
{
return;
}
if (!mapping.TryGetValue(t.Namespace, out var nsmap))
{
nsmap = new Dictionary>();
mapping[t.Namespace] = nsmap;
}
string basename = GetBasename(t.Name);
if (!nsmap.TryGetValue(basename, out var gnames))
{
gnames = new List();
nsmap[basename] = gnames;
}
gnames.Add(t.Name);
}
///
/// xxx
///
public static List? GetGenericBaseNames(string ns)
{
if (mapping.TryGetValue(ns, out var nsmap))
{
return nsmap.Keys.ToList();
}
return null;
}
///
/// Finds a generic type with the given number of generic parameters and the same name and namespace as .
///
public static Type? GenericForType(Type t, int paramCount)
{
return GenericByName(t.Namespace, t.Name, paramCount);
}
///
/// Finds a generic type in the given namespace with the given name and number of generic parameters.
///
public static Type? GenericByName(string ns, string basename, int paramCount)
{
if (mapping.TryGetValue(ns, out var nsmap))
{
if (nsmap.TryGetValue(GetBasename(basename), out var names))
{
foreach (string name in names)
{
string qname = $"{ns}.{name}";
Type o = AssemblyManager.LookupTypes(qname).FirstOrDefault();
if (o != null && o.GetGenericArguments().Length == paramCount)
{
return o;
}
}
}
}
return null;
}
///
/// xxx
///
public static string? GenericNameForBaseName(string ns, string name)
{
if (mapping.TryGetValue(ns, out var nsmap))
{
nsmap.TryGetValue(name, out var gnames);
if (gnames?.Count > 0)
{
return gnames[0];
}
}
return null;
}
private static string GetBasename(string name)
{
int tick = name.IndexOf("`");
if (tick > -1)
{
return name.Substring(0, tick);
}
else
{
return name;
}
}
}
}