Upcast Dictionary Improvements

I wanted to improve the predictability and the efficiency of the latest
UpcastDictionary code. I switched to a breadth-first search of
sub-types, filtering out duplicates.
This commit is contained in:
Travis Parks 2014-06-26 14:45:21 -04:00
parent 55ff1ef5de
commit f73814e3d9
2 changed files with 36 additions and 13 deletions

View File

@ -14,6 +14,6 @@ using System.Runtime.InteropServices;
[assembly: CLSCompliant(true)]
[assembly: ComVisible(false)]
[assembly: Guid("e5a4263d-d450-4d85-a4d5-44c0a2822668")]
[assembly: AssemblyVersion("0.2.6.0")]
[assembly: AssemblyFileVersion("0.2.6.0")]
[assembly: AssemblyVersion("0.2.7.0")]
[assembly: AssemblyFileVersion("0.2.7.0")]
[assembly: InternalsVisibleTo("mustache-sharp.test")]

View File

@ -24,24 +24,47 @@ namespace Mustache
return getDictionary(types, source);
}
private static IEnumerable<Type> getTypes(Type type)
private static IEnumerable<Type> getTypes(Type sourceType)
{
HashSet<Type> types = new HashSet<Type>();
getTypes(types, type);
return types;
Queue<Type> pending = new Queue<Type>();
HashSet<Type> visited = new HashSet<Type>();
pending.Enqueue(sourceType);
foreach (Type type in getTypes(pending, visited))
{
yield return type;
}
}
private static void getTypes(HashSet<Type> types, Type type)
private static IEnumerable<Type> getTypes(Queue<Type> pending, HashSet<Type> visited)
{
if (type == null)
if (pending.Count == 0)
{
return;
yield break;
}
types.Add(type);
getTypes(types, type.BaseType);
foreach (Type interfaceType in type.GetInterfaces())
Type sourceType = pending.Dequeue();
visited.Add(sourceType);
yield return sourceType;
if (sourceType.BaseType != null)
{
getTypes(types, interfaceType);
if (!visited.Contains(sourceType.BaseType))
{
pending.Enqueue(sourceType.BaseType);
}
}
foreach (Type interfaceType in sourceType.GetInterfaces())
{
if (!visited.Contains(interfaceType))
{
pending.Enqueue(interfaceType);
}
}
foreach (Type type in getTypes(pending, visited))
{
yield return type;
}
}