<commit_before>/**
 * Created by max on 2017/8/23.
 */

let resultTableMap;
let peopleInfoList;

exports.handleResultTable = async function ( resultTable, peopleInfo, peopleAvailableToShiftMap, colsCount, rowsCount, peopleCountInShift, limitOfEachOneShiftsCount, isUserSetShiftLimit ) {
  resultTableMap = resultTable;
  peopleInfoList = peopleInfo;

  for (var i=0; i<peopleInfoList.length; i++) {
    peopleInfoList[i].priorityMap = await makeEmptyMap( colsCount, rowsCount );
    peopleInfoList[i].continuousWeightMap = await makeEmptyMap(colsCount, rowsCount);
  }

  for (var i=0; i<colsCount; i++) {
    for (var j=0; j<rowsCount; j++) {
      let idList = peopleAvailableToShiftMap[i][j];
      peopleInfoList = await handlePersonPriority( idList, peopleInfoList, i, j );
      peopleInfoList = await handleContinuousPeopleInfoList( idList, peopleInfoList, colsCount, rowsCount, i, j );
      peopleInfoList = await handleLastCountPeopleInfoList( idList, peopleInfoList );
      resultTableMap[i][j] = await handlePriorityRanking( idList, i, j, peopleCountInShift, limitOfEachOneShiftsCount, isUserSetShiftLimit );
      peopleAvailableToShiftMap[i][j] = peopleAvailableToShiftMap[i][j].filter(val => !resultTableMap[i][j].includes(val));
    }
  }

  return Promise.resolve({
    resultTableMap,
    peopleInfoList,
    peopleAvailableToShiftMap
  });
};

var handlePersonPriority = async function ( idList, peopleInfoList, currentCol, currentRow ) {
  let priority = 0;

  for(var k=0; k<idList.length; k++) {
    for(var l=0; l<peopleInfoList.length; l++) {
      const personInfo = peopleInfoList[l];
      if ( idList[k] === personInfo.id ) {

        if ( personInfo.shiftsMap[currentCol][currentRow] === 1 ) {
          try {
            if ( resultTableMap[currentCol][currentRow-1].includes( personInfo.id ) ) {
              priority++;
            }
          } catch (e) {}
          try {
            if ( personInfo.shiftsMap[currentCol][currentRow+1] === 1 ) {
              priority++;
            }
          } catch (e) {}
        }
        peopleInfoList[l].priorityMap[currentCol][currentRow] = priority;
      }
    }
  }

  return Promise.resolve(peopleInfoList);
};

let handlePriorityRanking = async function ( idList, currentCol, currentRow, peopleCountInShift, limitOfEachOneShiftsCount, isUserSetShiftLimit ) {
  let peopleList = [];

  for(var k=0; k<idList.length; k++) {
    for(var l=0; l<peopleInfoList.length; l++) {
      const personInfo = peopleInfoList[l];
      if ( idList[k] === personInfo.id ) {
        const person = {
          id: personInfo.id,
          priorityRank: parseInt(personInfo.lastCountWeight) + parseInt(personInfo.continuousWeightMap[currentCol][currentRow])
        };
        peopleList.push(person);
      }
    }
  }

  peopleList = peopleList.sort(function (a, b) {
    return a.priorityRank < b.priorityRank ? 1 : -1;
  });

  let resultList = resultTableMap[currentCol][currentRow];
  for(var k=0; k<peopleList.length; k++) {
    for (var l=0; l<peopleInfoList.length; l++) {
      const personInfo = peopleInfoList[l];
      if ( peopleList[k].id === personInfo.id && resultList.length < peopleCountInShift && personInfo.usedCount < limitOfEachOneShiftsCount && personInfo.lastCount > 0 ) {
        if ( isUserSetShiftLimit ) {
          resultList.push( peopleList[k].id );
          personInfo.usedCount++;
          personInfo.lastCount--;
        } else {
          resultList.push( peopleList[k].id );
          personInfo.usedCount++;
          personInfo.lastCount--;
        }
      }
    }
  }

  if ( !isUserSetShiftLimit ) {
    for(var k=0; k<peopleList.length; k++) {
      for (var l=0; l<peopleInfoList.length; l++) {
        const personInfo = peopleInfoList[l];
        if ( peopleList[k].id === personInfo.id && resultList.length < peopleCountInShift && personInfo.lastCount > 0 ) {
          resultList.push( peopleList[k].id );
          personInfo.usedCount++;
          personInfo.lastCount--;
        }
      }
    }
  }

  return Promise.resolve(resultList);

};

var handleLastCountPeopleInfoList = async function ( idList, peopleInfoList ) {
  let peopleList = [];

  for(var i=0; i<idList.length; i++) {
    for(var j=0; j<peopleInfoList.length; j++) {
      const personInfo = peopleInfoList[j];
      if ( idList[i] === personInfo.id ) {
        const last = {
          id: personInfo.id,
          lastCount: personInfo.lastCount
        };
        peopleList.push(last)
      }
    }
  }

  peopleList = peopleList.sort(function (a, b) {
    return a.lastCount < b.lastCount ? 1 : -1;
  });

  let weight = 0;
  for(var i=0; i<peopleList.length; i++) {
    if ( i === 0 ) {
      peopleList[i].lastCountWeight = 0;
    } else if ( peopleList[i].lastCount === peopleList[i-1].lastCount ) {
      peopleList[i].lastCountWeight = weight;
    } else {
      peopleList[i].lastCountWeight = ++weight;
    }
  }

  for(var i=0; i<peopleList.length; i++) {
    for(var j=0; j<peopleInfoList.length; j++) {
      const personInfo = peopleInfoList[j];
      if ( peopleList[i].id === personInfo.id ) {
        peopleInfoList[j].lastCountWeight = peopleList[i].lastCountWeight;
      }
    }
  }

  return Promise.resolve(peopleInfoList);
};

var handleContinuousPeopleInfoList = async function ( idList, peopleInfoList, colsCount, rowsCount, currentCol, currentRow ) {
  let peopleList = [];

  for(var k=0; k<idList.length; k++) {
    for(var l=0; l<peopleInfoList.length; l++) {
      const personInfo = peopleInfoList[l];
      if ( idList[k] === personInfo.id ) {
        const continuous = {
          id: personInfo.id,
          continuousWeight: personInfo.priorityMap[currentCol][currentRow]
        };
        peopleList.push(continuous)
      }
    }
  }<commit_msg>HandleResultTable: Fix algorithm.<commit_after>/**
 * Created by max on 2017/8/23.
 */

let resultTableMap;
let peopleInfoList;

exports.handleResultTable = async function ( resultTable, peopleInfo, peopleAvailableToShiftMap, colsCount, rowsCount, peopleCountInShift, limitOfEachOneShiftsCount, isUserSetShiftLimit ) {
  resultTableMap = resultTable;
  peopleInfoList = peopleInfo;

  for (var i=0; i<peopleInfoList.length; i++) {
    peopleInfoList[i].priorityMap = await makeEmptyMap( colsCount, rowsCount );
    peopleInfoList[i].continuousWeightMap = await makeEmptyMap(colsCount, rowsCount);
  }

  for (var i=0; i<colsCount; i++) {
    for (var j=0; j<rowsCount; j++) {
      let idList = peopleAvailableToShiftMap[i][j];
      peopleInfoList = await handlePersonPriority( idList, peopleInfoList, i, j );
      peopleInfoList = await handleContinuousPeopleInfoList( idList, peopleInfoList, colsCount, rowsCount, i, j );
      peopleInfoList = await handleGapCountPeopleInfoList( idList, peopleInfoList, limitOfEachOneShiftsCount );
      resultTableMap[i][j] = await handlePriorityRanking( idList, i, j, peopleCountInShift, limitOfEachOneShiftsCount, isUserSetShiftLimit );
      peopleAvailableToShiftMap[i][j] = peopleAvailableToShiftMap[i][j].filter(val => !resultTableMap[i][j].includes(val));
    }
  }

  return Promise.resolve({
    resultTableMap,
    peopleInfoList,
    peopleAvailableToShiftMap
  });
};

var handlePersonPriority = async function ( idList, peopleInfoList, currentCol, currentRow ) {
  let priority = 0;

  for(var k=0; k<idList.length; k++) {
    for(var l=0; l<peopleInfoList.length; l++) {
      const personInfo = peopleInfoList[l];
      if ( idList[k] === personInfo.id ) {

        if ( personInfo.shiftsMap[currentCol][currentRow] === 1 ) {
          try {
            if ( resultTableMap[currentCol][currentRow-1].includes( personInfo.id ) ) {
              priority++;
            }
          } catch (e) {}
          try {
            if ( personInfo.shiftsMap[currentCol][currentRow+1] === 1 ) {
              priority++;
            }
          } catch (e) {}
        }
        peopleInfoList[l].priorityMap[currentCol][currentRow] = priority;
      }
    }
  }

  return Promise.resolve(peopleInfoList);
};

let handlePriorityRanking = async function ( idList, currentCol, currentRow, peopleCountInShift, limitOfEachOneShiftsCount, isUserSetShiftLimit ) {
  let peopleList = [];

  for(var k=0; k<idList.length; k++) {
    for(var l=0; l<peopleInfoList.length; l++) {
      const personInfo = peopleInfoList[l];
      if ( idList[k] === personInfo.id ) {
        const person = {
          id: personInfo.id,
          priorityRank: parseInt(personInfo.gapCountWeight) + parseInt(personInfo.continuousWeightMap[currentCol][currentRow])
        };
        peopleList.push(person);
      }
    }
  }

  peopleList = peopleList.sort(function (a, b) {
    return a.priorityRank < b.priorityRank ? 1 : -1;
  });

  let resultList = resultTableMap[currentCol][currentRow];
  for(var k=0; k<peopleList.length; k++) {
    for (var l=0; l<peopleInfoList.length; l++) {
      const personInfo = peopleInfoList[l];
      if ( peopleList[k].id === personInfo.id && resultList.length < peopleCountInShift && personInfo.usedCount < limitOfEachOneShiftsCount && personInfo.lastCount > 0 ) {
        if ( isUserSetShiftLimit ) {
          resultList.push( peopleList[k].id );
          personInfo.usedCount++;
          personInfo.lastCount--;
        } else {
          resultList.push( peopleList[k].id );
          personInfo.usedCount++;
          personInfo.lastCount--;
        }
      }
    }
  }

  if ( !isUserSetShiftLimit ) {
    for(var k=0; k<peopleList.length; k++) {
      for (var l=0; l<peopleInfoList.length; l++) {
        const personInfo = peopleInfoList[l];
        if ( peopleList[k].id === personInfo.id && resultList.length < peopleCountInShift && personInfo.lastCount > 0 ) {
          resultList.push( peopleList[k].id );
          personInfo.usedCount++;
          personInfo.lastCount--;
        }
      }
    }
  }

  return Promise.resolve(resultList);

};

var handleGapCountPeopleInfoList = async function ( idList, peopleInfoList, limitOfEachOneShiftsCount ) {
  let peopleList = [];

  for(var i=0; i<idList.length; i++) {
    for(var j=0; j<peopleInfoList.length; j++) {
      const personInfo = peopleInfoList[j];
      if ( idList[i] === personInfo.id ) {
        const last = {
          id: personInfo.id,
          gapCount: limitOfEachOneShiftsCount - personInfo.usedCount
        };
        peopleList.push(last)
      }
    }
  }

  peopleList = peopleList.sort(function (a, b) {
    return a.gapCount > b.gapCount ? 1 : -1;
  });

  let weight = 0;
  for(var i=0; i<peopleList.length; i++) {
    if ( i === 0 ) {
      peopleList[i].gapCountWeight = 0;
    } else if ( peopleList[i].gapCount === peopleList[i-1].gapCount ) {
      peopleList[i].gapCountWeight = weight;
    } else {
      peopleList[i].gapCountWeight = ++weight;
    }
  }

  for(var i=0; i<peopleList.length; i++) {
    for(var j=0; j<peopleInfoList.length; j++) {
      const personInfo = peopleInfoList[j];
      if ( peopleList[i].id === personInfo.id ) {
        peopleInfoList[j].gapCountWeight = peopleList[i].gapCountWeight;
      }
    }
  }

  return Promise.resolve(peopleInfoList);
};

var handleContinuousPeopleInfoList = async function ( idList, peopleInfoList, colsCount, rowsCount, currentCol, currentRow ) {
  let peopleList = [];

  for(var k=0; k<idList.length; k++) {
    for(var l=0; l<peopleInfoList.length; l++) {
      const personInfo = peopleInfoList[l];
      if ( idList[k] === personInfo.id ) {
        const continuous = {
          id: personInfo.id,
          continuousWeight: personInfo.priorityMap[currentCol][currentRow]
        };
        peopleList.push(continuous)
      }
    }
  }
{'fraction_non_alphanumeric': 0.13102500214795085, 'fraction_numerical': 0.006873442735630209, 'mean_word_length': 2.9794871794871796, 'pattern_counts': {'":': 0, '<': 48, '<?xml version=': 0, '>': 10, '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': '7035620', 'n_tokens_mistral': 3720, 'n_tokens_neox': 3640, 'n_words': 707}