<commit_before>use std::marker::PhantomData;
use par_exec::{Executor, ExecutorJobError, JobExecuteError};

use super::PopulationInit;
use super::super::individual::Individual;
use super::super::super::set::{Set, SetManager};
use super::super::super::set::union;

pub trait Policy {
    type LocalContext: AsMut<Self::PopSM>;
    type Exec: Executor<LC = Self::LocalContext>;
    type Indiv: Individual;
    type PopE: Send + 'static;
    type Pop: Set<T = Self::Indiv, E = Self::PopE> + Send + 'static;
    type PopSME: Send + 'static;
    type PopSM: SetManager<S = Self::Pop, E = Self::PopSME>;
}

pub struct LimitedPopulationInit<P> where P: Policy {
    limit: usize,
    _marker: PhantomData<P>,
}

impl<P> LimitedPopulationInit<P> where P: Policy {
    pub fn new(limit: usize) -> LimitedPopulationInit<P> {
        LimitedPopulationInit {
            limit: limit,
            _marker: PhantomData,
        }
    }
}

pub enum Error<P> where P: Policy {
    NoOutputPopulation,
    Executor(<P::Exec as Executor>::E),
    JobMap(()),
    JobReduce(union::Error<P::PopE, P::PopSME>),
    Several(Vec<Error<P>>),
}

impl<P> PopulationInit for LimitedPopulationInit<P> where P: Policy {
    type Exec = P::Exec;
    type Indiv = P::Indiv;
    type Pop = P::Pop;
    type Err = Error<P>;

    fn init(&self, exec: &mut Self::Exec) -> Result<Self::Pop, Self::Err> {

        fn err_map_rec<P>(err: ExecutorJobError<<P::Exec as Executor>::E, JobExecuteError<(), union::Error<P::PopE, P::PopSME>>>) -> Error<P> where P: Policy {
            match err {
                ExecutorJobError::Executor(e) =>
                    Error::Executor(e),
                ExecutorJobError::Job(JobExecuteError::Job(e)) =>
                    Error::JobMap(e),
                ExecutorJobError::Job(JobExecuteError::Reducer(e)) =>
                    Error::JobReduce(e),
                ExecutorJobError::Several(ee) =>
                    Error::Several(IntoIterator::into_iter(ee).map(err_map_rec).collect()),
            }
        }

        match exec.execute_job(
            self.limit,
            move |_local_context, _input_indices| {
                Err(())
            },
            move |_local_context: &mut <Self::Exec as Executor>::LC, set: &Self::Pop| Some(set.size()),
            move |local_context, pop_a, pop_b| union::union(local_context, pop_a, pop_b))
        {
            Ok(None) => Err(Error::NoOutputPopulation),
            Ok(Some(population)) => Ok(population),
            Err(e) => Err(err_map_rec(e)),
        }
    }
}<commit_msg>simplify error + par_exec changes followup<commit_after>use std::marker::PhantomData;
use par_exec::{Executor, ExecutorJobError, JobExecuteError};

use super::PopulationInit;
use super::super::individual::Individual;
use super::super::super::set::{Set, SetManager};
use super::super::super::set::union;

pub trait Policy {
    type LocalContext: AsMut<Self::PopSM>;
    type Exec: Executor<LC = Self::LocalContext>;
    type Indiv: Individual;
    type PopE: Send + 'static;
    type Pop: Set<T = Self::Indiv, E = Self::PopE> + Send + 'static;
    type PopSME: Send + 'static;
    type PopSM: SetManager<S = Self::Pop, E = Self::PopSME>;
}

pub struct LimitedPopulationInit<P> where P: Policy {
    limit: usize,
    _marker: PhantomData<P>,
}

impl<P> LimitedPopulationInit<P> where P: Policy {
    pub fn new(limit: usize) -> LimitedPopulationInit<P> {
        LimitedPopulationInit {
            limit: limit,
            _marker: PhantomData,
        }
    }
}

pub enum Error<P> where P: Policy {
    NoOutputPopulation,
    Executor(ExecutorJobError<<P::Exec as Executor>::E, JobExecuteError<(), union::Error<P::PopE, P::PopSME>>>),
}

impl<P> PopulationInit for LimitedPopulationInit<P> where P: Policy {
    type Exec = P::Exec;
    type Indiv = P::Indiv;
    type Pop = P::Pop;
    type Err = Error<P>;

    fn init(&self, exec: &mut Self::Exec) -> Result<Self::Pop, Self::Err> {
        match exec.execute_job(
            self.limit,
            move |_local_context, _input_indices| {
                Err(())
            },
            move |local_context, pop_a, pop_b| union::union(local_context, pop_a, pop_b))
        {
            Ok(None) => Err(Error::NoOutputPopulation),
            Ok(Some(population)) => Ok(population),
            Err(e) => Err(Error::Executor(e)),
        }
    }
}
{'fraction_non_alphanumeric': 0.14763869784502523, 'fraction_numerical': 0.0, 'mean_word_length': 2.9484162895927604, 'pattern_counts': {'":': 0, '<': 46, '<?xml version=': 0, '>': 61, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5684974', 'n_tokens_mistral': 1431, 'n_tokens_neox': 1372, 'n_words': 304}