<commit_before>/*
    #-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=#
      This file is part of the Smart Developer Hub Project:
        http://www.smartdeveloperhub.org/
      Center for Open Middleware
            http://www.centeropenmiddleware.com/
    #-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=#
      Copyright (C) 2015 Center for Open Middleware.
    #-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=#
      Licensed under the Apache License, Version 2.0 (the "License");
      you may not use this file except in compliance with the License.
      You may obtain a copy of the License at
                http://www.apache.org/licenses/LICENSE-2.0
      Unless required by applicable law or agreed to in writing, software
      distributed under the License is distributed on an "AS IS" BASIS,
      WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
      See the License for the specific language governing permissions and
     limitations under the License.
    #-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=#
*/

"use strict";

function WorkerPool (jobsPerWorker) {
    this.jobsPerWorker = jobsPerWorker || 1;
    this.busy = [];
    this.idle = [];
}

WorkerPool.prototype = {
    constructor: WorkerPool,

    add: function(worker) {
        this.idle.push({
            worker: worker,
            jobs: 0
        });
    },

    allBusy: function() {
        return this.idle.length === 0;
    },

    allIdle: function() {
        return this.busy.length === 0;
    },

    getIdleAndAddJob: function() {
        var worker = this.idle.pop();
        if(worker != null) {
            if(++worker.jobs < this.jobsPerWorker) { //Not all concurrent jobs reached so add it to the end of idle queue
                this.idle.push(worker);
            } else { // All concurrent jobs reached so add it to the busy queue
                this.busy.push(worker);
            }
            return worker.worker;
        }

        return null; //Null in case there are no idle workers

    },

    setIdle: function(worker) {

        var index = indexOfWorkerInList(worker, this.idle);

        if(index >= 0) { //It is in the idle queue
            this.idle[index]['jobs']--;

        } else { //Try to find it in the busy queue

            index = indexOfWorkerInList(worker, this.busy);

            if(index != -1) {
                this.busy[index]['jobs']--;
                this.idle.push(this.busy.splice(index, 1)[0]);
            } else {
                throw new Error("Worker could not be found!");
            }
        }

    },

    isBusy: function(worker) {
        return indexOfWorkerInList(worker, this.busy) !== -1;
    }
};

var indexOfWorkerInList = function(worker, list) {
    for(var i = 0; i < list.length; i++) {
        if(list[i].worker === worker) {
            return i;
        }
    }
    return -1;
};

module.exports = WorkerPool;<commit_msg>Make the worker a queue so that the worker retrieved is the one that has been more time idle.<commit_after>/*
    #-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=#
      This file is part of the Smart Developer Hub Project:
        http://www.smartdeveloperhub.org/
      Center for Open Middleware
            http://www.centeropenmiddleware.com/
    #-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=#
      Copyright (C) 2015 Center for Open Middleware.
    #-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=#
      Licensed under the Apache License, Version 2.0 (the "License");
      you may not use this file except in compliance with the License.
      You may obtain a copy of the License at
                http://www.apache.org/licenses/LICENSE-2.0
      Unless required by applicable law or agreed to in writing, software
      distributed under the License is distributed on an "AS IS" BASIS,
      WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
      See the License for the specific language governing permissions and
     limitations under the License.
    #-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=#
*/

"use strict";

function WorkerPool (jobsPerWorker) {
    this.jobsPerWorker = jobsPerWorker || 1;
    this.busy = [];
    this.idle = [];
}

WorkerPool.prototype = {
    constructor: WorkerPool,

    add: function(worker) {
        this.idle.push({
            worker: worker,
            jobs: 0
        });
    },

    allBusy: function() {
        return this.idle.length === 0;
    },

    allIdle: function() {
        return this.busy.length === 0;
    },

    getIdleAndAddJob: function() {
        var worker = this.idle.shift();
        if(worker != null) {
            if(++worker.jobs < this.jobsPerWorker) { //Not all concurrent jobs reached so add it to the end of idle queue
                this.idle.push(worker);
            } else { // All concurrent jobs reached so add it to the busy queue
                this.busy.push(worker);
            }
            return worker.worker;
        }

        return null; //Null in case there are no idle workers

    },

    setIdle: function(worker) {

        var index = indexOfWorkerInList(worker, this.idle);

        if(index >= 0) { //It is in the idle queue
            this.idle[index]['jobs']--;

        } else { //Try to find it in the busy queue

            index = indexOfWorkerInList(worker, this.busy);

            if(index != -1) {
                this.busy[index]['jobs']--;
                this.idle.push(this.busy.splice(index, 1)[0]);
            } else {
                throw new Error("Worker could not be found!");
            }
        }

    },

    isBusy: function(worker) {
        return indexOfWorkerInList(worker, this.busy) !== -1;
    }
};

var indexOfWorkerInList = function(worker, list) {
    for(var i = 0; i < list.length; i++) {
        if(list[i].worker === worker) {
            return i;
        }
    }
    return -1;
};

module.exports = WorkerPool;
{'fraction_non_alphanumeric': 0.19332247557003257, 'fraction_numerical': 0.006188925081433224, 'mean_word_length': 1.801551094890511, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 6, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1399958', 'n_tokens_mistral': 1916, 'n_tokens_neox': 1699, 'n_words': 507}