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>
|
||||
{
|
||||
private readonly IReadOnlyList<IEnumerable<ScriptContainer>> _scripts;
|
||||
private int _currentIndex = 0;
|
||||
|
||||
/// <inheritdoc cref="ScriptIterator"/>
|
||||
public ScriptIterator(IReadOnlyList<IEnumerable<ScriptContainer>> scripts)
|
||||
@ -15,11 +16,15 @@ public class ScriptIterator : IEnumerable<ScriptContainer>
|
||||
_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
|
||||
// with a for loop.
|
||||
@ -31,22 +36,20 @@ public class ScriptIterator : IEnumerable<ScriptContainer>
|
||||
continue;
|
||||
yield return script;
|
||||
}
|
||||
break;
|
||||
}
|
||||
else
|
||||
{
|
||||
foreach (var script in enumerable)
|
||||
{
|
||||
// We can ignore empty scripts.
|
||||
if (script.IsEmpty)
|
||||
case ScriptContainer { IsEmpty: true }:
|
||||
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 />
|
||||
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
|
||||
|
Loading…
x
Reference in New Issue
Block a user