use std::cmp::Ordering;
use super::thread_manager::ThreadManager;
pub trait PrioritizeThread {
fn prioritize<'env, 'scope,Input,Output,F>(&self, thread_manager:&mut ThreadManager<'env, 'scope,Input,Output,F>)
-> Vec<(usize, usize)>
where Input: Send + Sync + 'scope,
Output: Send + Sync + 'scope,
F: Fn(Input) -> Output + Send + Sync + 'scope,
'env: 'scope;
}
#[allow(dead_code)]
pub enum ThreadPrioritization {
Remaining,
RateOfChange,
ChangeFromLastPoll
}
impl PrioritizeThread for ThreadPrioritization {
fn prioritize<'env, 'scope,Input,Output,F>(&self, thread_manager:&mut ThreadManager<'env, 'scope,Input,Output,F>)
-> Vec<(usize, usize)>
where Input: Send + Sync + 'scope,
Output: Send + Sync + 'scope,
F: Fn(Input) -> Output + Send + Sync + 'scope,
'env: 'scope
{
let mut vec_ranking = thread_manager.threads_as_mutable()
.iter_mut().filter_map(|thread|
{
thread.poll_progress().map(|stats|
{
(thread.pos(),stats.0, stats.1, stats.2)
}
)
}).collect::<Vec<(usize, usize, usize, f64)>>();
match self {
ThreadPrioritization::ChangeFromLastPoll => {
vec_ranking.sort_by(|a,b|{
if a.2 > b.2 {
Ordering::Greater
} else {
Ordering::Less
}
});
}
ThreadPrioritization::RateOfChange => {
vec_ranking.sort_by(|a,b|{
if a.3 > b.3 {
Ordering::Greater
} else {
Ordering::Less
}
});
}
ThreadPrioritization::Remaining => {
vec_ranking.sort_by(|a,b|{
if a.1 < b.1 {
Ordering::Greater
} else {
Ordering::Less
}
});
}
}
vec_ranking.into_iter().map(|val|(val.0,val.1))
.collect::<Vec<_>>()
}
}