Skip to content
GitLab
Menu
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
ImportedProjects
SVG
Commits
5dc7e7bc
Commit
5dc7e7bc
authored
Feb 27, 2019
by
Ken in NH
Committed by
mrbean-bremen
Feb 27, 2019
Browse files
Took SvgDocument exclusion out of ApplyRecursive and optimized traversals (#425)
parent
4f18e55a
Changes
1
Hide whitespace changes
Inline
Side-by-side
Source/SvgExtentions.cs
View file @
5dc7e7bc
...
...
@@ -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
);
}
}
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment