From 40d0f65e6999ee55982f7b0e9f3c0e0c3281abe1 Mon Sep 17 00:00:00 2001 From: KX Date: Tue, 23 Apr 2024 12:48:47 +0200 Subject: [PATCH] vicky: made get_next_tasks more concise --- vicky/src/lib/vicky/scheduler.rs | 53 +++++++++++++------------------- 1 file changed, 22 insertions(+), 31 deletions(-) diff --git a/vicky/src/lib/vicky/scheduler.rs b/vicky/src/lib/vicky/scheduler.rs index 28c5135..f7738d5 100644 --- a/vicky/src/lib/vicky/scheduler.rs +++ b/vicky/src/lib/vicky/scheduler.rs @@ -118,40 +118,31 @@ impl Scheduler { Ok(s) } - pub fn get_next_task(self) -> Option { - for task in self.tasks { - if task.status != TaskStatus::NEW { - continue; - } - - if !task - .features - .iter() - .all(|feat| self.machine_features.contains(feat)) - { - continue; - } - - let mut has_conflicts = false; + fn is_unconstrained(&self, task: &Task) -> bool { + task.locks.iter().all(|lock| { + self.constraints + .get_lock_sum(lock) + .map_or(true, |ls| ls.can_add_lock(lock)) + }) + } - for lock in &task.locks { - let lock_sum = self.constraints.get_lock_sum(lock); - match lock_sum { - Some(ls) => { - if !ls.can_add_lock(lock) { - has_conflicts = true; - } - } - None => continue, - } - } + fn supports_all_features(&self, task: &Task) -> bool { + task.features + .iter() + .all(|feat| self.machine_features.contains(feat)) + } - if !has_conflicts { - return Some(task); - } - } + fn should_pick_task(&self, task: &Task) -> bool { + task.status == TaskStatus::NEW + && self.supports_all_features(task) + && self.is_unconstrained(task) + } - None + pub fn get_next_task(mut self) -> Option { + self.tasks + .iter() + .position(|task| self.should_pick_task(task)) + .map(|idx| self.tasks.remove(idx)) } }