punish v6.8.18.0
This commit is contained in:
commit
e786325cda
322 changed files with 554232 additions and 0 deletions
78
Questionable/Questionable.Controller.Steps/TaskQueue.cs
Normal file
78
Questionable/Questionable.Controller.Steps/TaskQueue.cs
Normal file
|
@ -0,0 +1,78 @@
|
|||
using System.Collections.Generic;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Linq;
|
||||
|
||||
namespace Questionable.Controller.Steps;
|
||||
|
||||
internal sealed class TaskQueue
|
||||
{
|
||||
private readonly List<ITask> _completedTasks = new List<ITask>();
|
||||
|
||||
private readonly List<ITask> _tasks = new List<ITask>();
|
||||
|
||||
public ITaskExecutor? CurrentTaskExecutor { get; set; }
|
||||
|
||||
public IEnumerable<ITask> RemainingTasks => _tasks;
|
||||
|
||||
public bool AllTasksComplete
|
||||
{
|
||||
get
|
||||
{
|
||||
if (CurrentTaskExecutor == null)
|
||||
{
|
||||
return _tasks.Count == 0;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public void Enqueue(ITask task)
|
||||
{
|
||||
_tasks.Add(task);
|
||||
}
|
||||
|
||||
public void EnqueueAll(IEnumerable<ITask> tasks)
|
||||
{
|
||||
_tasks.InsertRange(0, tasks);
|
||||
}
|
||||
|
||||
public bool TryDequeue([NotNullWhen(true)] out ITask? task)
|
||||
{
|
||||
task = _tasks.FirstOrDefault();
|
||||
if (task == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
if (task.ShouldRedoOnInterrupt())
|
||||
{
|
||||
_completedTasks.Add(task);
|
||||
}
|
||||
_tasks.RemoveAt(0);
|
||||
return true;
|
||||
}
|
||||
|
||||
public bool TryPeek([NotNullWhen(true)] out ITask? task)
|
||||
{
|
||||
task = _tasks.FirstOrDefault();
|
||||
return task != null;
|
||||
}
|
||||
|
||||
public void Reset()
|
||||
{
|
||||
_tasks.Clear();
|
||||
_completedTasks.Clear();
|
||||
CurrentTaskExecutor = null;
|
||||
}
|
||||
|
||||
public void InterruptWith(List<ITask> interruptionTasks)
|
||||
{
|
||||
List<ITask> list = new List<ITask>();
|
||||
list.AddRange(interruptionTasks);
|
||||
list.AddRange(_completedTasks.Where((ITask x) => x != CurrentTaskExecutor?.CurrentTask).ToList());
|
||||
list.Add(CurrentTaskExecutor?.CurrentTask);
|
||||
list.AddRange(_tasks);
|
||||
List<ITask> source = list;
|
||||
Reset();
|
||||
_tasks.AddRange(source.Where((ITask x) => x != null).Cast<ITask>());
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue