From 8135db68df63fd6b6ef0b71614d49aa91ca50235 Mon Sep 17 00:00:00 2001 From: Travis Parks Date: Thu, 26 Jun 2014 15:21:01 -0400 Subject: [PATCH] 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. --- mustache-sharp/Properties/AssemblyInfo.cs | 4 +-- mustache-sharp/UpcastDictionary.cs | 42 ++++++++--------------- 2 files changed, 16 insertions(+), 30 deletions(-) diff --git a/mustache-sharp/Properties/AssemblyInfo.cs b/mustache-sharp/Properties/AssemblyInfo.cs index 8e80552..68d40aa 100644 --- a/mustache-sharp/Properties/AssemblyInfo.cs +++ b/mustache-sharp/Properties/AssemblyInfo.cs @@ -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.7.0")] -[assembly: AssemblyFileVersion("0.2.7.0")] +[assembly: AssemblyVersion("0.2.7.1")] +[assembly: AssemblyFileVersion("0.2.7.1")] [assembly: InternalsVisibleTo("mustache-sharp.test")] \ No newline at end of file diff --git a/mustache-sharp/UpcastDictionary.cs b/mustache-sharp/UpcastDictionary.cs index 2015dda..e32ba21 100644 --- a/mustache-sharp/UpcastDictionary.cs +++ b/mustache-sharp/UpcastDictionary.cs @@ -29,43 +29,29 @@ namespace Mustache Queue pending = new Queue(); HashSet visited = new HashSet(); pending.Enqueue(sourceType); - foreach (Type type in getTypes(pending, visited)) + + while (pending.Count != 0) { + Type type = pending.Dequeue(); + visited.Add(type); yield return type; - } - } - private static IEnumerable getTypes(Queue pending, HashSet visited) - { - 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)) + if (type.BaseType != null) { - pending.Enqueue(sourceType.BaseType); + if (!visited.Contains(type.BaseType)) + { + pending.Enqueue(type.BaseType); + } } - } - foreach (Type interfaceType in sourceType.GetInterfaces()) - { - if (!visited.Contains(interfaceType)) + foreach (Type interfaceType in type.GetInterfaces()) { - pending.Enqueue(interfaceType); + if (!visited.Contains(interfaceType)) + { + pending.Enqueue(interfaceType); + } } } - - foreach (Type type in getTypes(pending, visited)) - { - yield return type; - } } private static IDictionary getDictionary(IEnumerable types, object source)