Commit 5dc7e7bc authored by Ken in NH's avatar Ken in NH Committed by mrbean-bremen
Browse files

Took SvgDocument exclusion out of ApplyRecursive and optimized traversals (#425)

parent 4f18e55a
......@@ -78,28 +78,52 @@ namespace Svg
public static void ApplyRecursive(this SvgElement elem, Action<SvgElement> action)
{
action(elem);
if (!(elem is SvgDocument)) //don't apply action to subtree of documents
foreach (var e in elem
.Traverse(e => e.Children))
{
foreach (var element in elem.Children)
{
element.ApplyRecursive(action);
}
action(e);
}
}
public static void ApplyRecursiveDepthFirst(this SvgElement elem, Action<SvgElement> action)
{
if (!(elem is SvgDocument)) //don't apply action to subtree of documents
foreach (var e in elem
.TraverseDepthFirst(e => e.Children))
{
foreach (var element in elem.Children)
{
element.ApplyRecursiveDepthFirst(action);
}
action(e);
}
}
public static IEnumerable<T> Traverse<T>(this IEnumerable<T> items, Func<T, IEnumerable<T>> childrenSelector)
{
if (childrenSelector == null) throw new ArgumentNullException(nameof(childrenSelector));
var itemQueue = new Queue<T>(items);
while (itemQueue.Count > 0)
{
var current = itemQueue.Dequeue();
yield return current;
foreach (var child in childrenSelector(current) ?? Enumerable.Empty<T>()) itemQueue.Enqueue(child);
}
}
action(elem);
public static IEnumerable<T> Traverse<T>(this T root, Func<T, IEnumerable<T>> childrenSelector)
=> Enumerable.Repeat(root, 1).Traverse(childrenSelector);
public static IEnumerable<T> TraverseDepthFirst<T>(this IEnumerable<T> items, Func<T, IEnumerable<T>> childrenSelector)
{
if (childrenSelector == null) throw new ArgumentNullException(nameof(childrenSelector));
var itemStack = new Stack<T>(items ?? Enumerable.Empty<T>());
while (itemStack.Count > 0)
{
var current = itemStack.Pop();
yield return current;
foreach (var child in childrenSelector(current) ?? Enumerable.Empty<T>()) itemStack.Push(child);
}
}
public static IEnumerable<T> TraverseDepthFirst<T>(this T root, Func<T, IEnumerable<T>> childrenSelector)
=> Enumerable.Repeat(root, 1).TraverseDepthFirst(childrenSelector);
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment