use jam_types::{core_count, CoreIndex, VecSet};
use std::iter::Cycle;

/// Determines which core to use for the next work package.
pub struct CoreScheduler {
	cores: Cycle<<VecSet<CoreIndex> as IntoIterator>::IntoIter>,
}

impl CoreScheduler {
	pub fn new(cores: VecSet<CoreIndex>) -> Self {
		assert!(cores.iter().all(|core| *core <= core_count()) && !cores.is_empty());
		Self { cores: cores.into_iter().cycle() }
	}

	/// Returns the core that builder should use for the next work package.
	pub fn next_core(&mut self) -> CoreIndex {
		// TODO @ivan Select next core based on authorizers in the pool.
		self.cores.next().expect("Cyclic iterator")
	}
}
