
#ifndef PLS_PARALLEL_FOR_IMPL_H
#define PLS_PARALLEL_FOR_IMPL_H

#include "pls/internal/scheduling/task.h"
#include "pls/internal/scheduling/scheduler.h"

#include "pls/internal/helpers/unique_id.h"

namespace pls {
namespace algorithm {

template<typename RandomIt, typename Function>
void parallel_for(RandomIt first, RandomIt last, const Function &function) {
  using namespace ::pls::internal::scheduling;
  constexpr long min_elements = 4;

  long num_elements = std::distance(first, last);
  if (num_elements <= min_elements) {
    // calculate last elements in loop to avoid overhead
    for (auto current = first; current != last; current++) {
      function(*current);
    }
  } else {
    // Cut in half recursively
    long middle_index = num_elements / 2;

    auto body = [=] { parallel_for(first + middle_index, last, function); };
    lambda_task_by_reference<decltype(body)> second_half_task(body);
    scheduler::spawn_child(second_half_task);

    parallel_for(first, first + middle_index, function);
    scheduler::wait_for_all();
  }
}

}
}

#endif //PLS_INVOKE_PARALLEL_IMPL_H
