src/algorithms/smith/index.js
fe944750
 import {
b681b265
   findIndex, map, propEq, reduce, filter, reverse
fe944750
 } from 'rambda';
 import type { JobId } from '../../types';
b681b265
 import { max, remove } from 'ramda';
 import { fillCorrupted } from '../moore';
fe944750
 
 type JobTime = {
   jobId: JobId,
   t: number,
   d: number,
8c7841d1
   name: string
fe944750
 };
 
 const findMaxTJob = (jobTimes: JobTime[]) =>
   reduce(
     (maxTJob: JobTime, jobTime: JobTime) => maxTJob.t >= jobTime.t ? maxTJob : jobTime,
     { t: -Infinity },
     jobTimes
   );
 
 const updateCTime = (jobTimes: JobTime[]) => {
   let prev = 0;
 
   const cj = map((jobTime: JobTime) => {
     prev += jobTime.t;
 
     return { ...jobTime, c: prev };
   }, jobTimes);
 
   return cj;
 };
 
 export const createSchedule = (jobs: JobTime[]) => {
b681b265
   jobs = fillCorrupted(jobs);
 
fe944750
   let fi = reduce((sum, job: JobTime) => sum + job.t, 0, jobs);
   let jobsD = jobs;
   let R = [];
 
   while (fi > 0) {
     const G = filter((jobTime: JobTime) => jobTime.d >= fi, jobsD);
 
     if (!G.length) return null;
 
     const maxTJob = findMaxTJob(G);
     const maxTJobIndex = findIndex(propEq('jobId', maxTJob.jobId), jobsD);
 
     jobsD = remove(maxTJobIndex, 1, jobsD);
     fi -= maxTJob.t;
 
     R.push(maxTJob);
   }
 
   const withC = updateCTime(reverse(R));
 
   return withC;
 };
 
 export default (jobs: JobTime[]) => {
   const schedule = createSchedule(jobs);
 
b681b265
   if (!schedule) return null;
 
fe944750
   const normalizedSchedule = map((jobTime: JobTime) => ({
     processor: 1,
     startTime: jobTime.c - jobTime.t,
     endTime: jobTime.c,
8c7841d1
     delayed: max(0, jobTime.c - jobTime.d),
     name: jobTime.name
fe944750
   }), schedule);
 
   return [normalizedSchedule];
 };