Eliminate recursive calls in UpcastDictionary

I realized I had tail-end recursion, which could be easily eliminated.
Because of yield return, I think this greatly simplified the code.
This commit is contained in:
Travis Parks 2014-06-26 15:21:01 -04:00
parent f73814e3d9
commit 8135db68df
2 changed files with 16 additions and 30 deletions

View File

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

View File

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