Greatly improve performance due to ScriptIterator changes
All checks were successful
Build / Build (push) Successful in 1m43s
All checks were successful
Build / Build (push) Successful in 1m43s
This commit is contained in:
parent
6eba332096
commit
cccffc4954
@ -8,6 +8,7 @@ namespace PkmnLib.Dynamic.ScriptHandling;
|
|||||||
public class ScriptIterator : IEnumerable<ScriptContainer>
|
public class ScriptIterator : IEnumerable<ScriptContainer>
|
||||||
{
|
{
|
||||||
private readonly IReadOnlyList<IEnumerable<ScriptContainer>> _scripts;
|
private readonly IReadOnlyList<IEnumerable<ScriptContainer>> _scripts;
|
||||||
|
private int _currentIndex = 0;
|
||||||
|
|
||||||
/// <inheritdoc cref="ScriptIterator"/>
|
/// <inheritdoc cref="ScriptIterator"/>
|
||||||
public ScriptIterator(IReadOnlyList<IEnumerable<ScriptContainer>> scripts)
|
public ScriptIterator(IReadOnlyList<IEnumerable<ScriptContainer>> scripts)
|
||||||
@ -15,11 +16,15 @@ public class ScriptIterator : IEnumerable<ScriptContainer>
|
|||||||
_scripts = scripts;
|
_scripts = scripts;
|
||||||
}
|
}
|
||||||
|
|
||||||
private IEnumerable<ScriptContainer> GetAsEnumerable()
|
/// <inheritdoc />
|
||||||
|
public IEnumerator<ScriptContainer> GetEnumerator()
|
||||||
{
|
{
|
||||||
foreach (var enumerable in _scripts)
|
while (_currentIndex < _scripts.Count)
|
||||||
{
|
{
|
||||||
if (enumerable is IScriptSet set)
|
var enumerable = _scripts[_currentIndex++];
|
||||||
|
switch (enumerable)
|
||||||
|
{
|
||||||
|
case IScriptSet set:
|
||||||
{
|
{
|
||||||
// We can't assume that the set remains the same size, so we need to iterate through it
|
// We can't assume that the set remains the same size, so we need to iterate through it
|
||||||
// with a for loop.
|
// with a for loop.
|
||||||
@ -31,22 +36,20 @@ public class ScriptIterator : IEnumerable<ScriptContainer>
|
|||||||
continue;
|
continue;
|
||||||
yield return script;
|
yield return script;
|
||||||
}
|
}
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
else
|
case ScriptContainer { IsEmpty: true }:
|
||||||
{
|
|
||||||
foreach (var script in enumerable)
|
|
||||||
{
|
|
||||||
// We can ignore empty scripts.
|
|
||||||
if (script.IsEmpty)
|
|
||||||
continue;
|
continue;
|
||||||
yield return script;
|
case ScriptContainer container:
|
||||||
|
yield return container;
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
throw new InvalidOperationException(
|
||||||
|
$"Unexpected script type: {enumerable.GetType().Name}. Expected IScriptSet or ScriptContainer.");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
_currentIndex = 0;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
/// <inheritdoc />
|
|
||||||
public IEnumerator<ScriptContainer> GetEnumerator() => GetAsEnumerable().GetEnumerator();
|
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
|
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
|
||||||
|
Loading…
x
Reference in New Issue
Block a user