diff options
author | Holden Rohrer <hr@hrhr.dev> | 2020-02-05 23:24:09 +0000 |
---|---|---|
committer | Holden Rohrer <hr@hrhr.dev> | 2020-02-05 19:51:39 -0500 |
commit | e618b9737fe1b4eebbfeec2eaabd5bb923179c83 (patch) | |
tree | 48f00968ca373579ba9e844909ccd9032348b22a | |
parent | 242f1ab38530a1271d072964334e33b85a4ab279 (diff) |
schedule.js prevents off-by-one error by popping minimum amount in step 3
-rw-r--r-- | tools/schedule.js | 10 |
1 files changed, 5 insertions, 5 deletions
diff --git a/tools/schedule.js b/tools/schedule.js index caad554..dfbe854 100644 --- a/tools/schedule.js +++ b/tools/schedule.js @@ -28,8 +28,8 @@ exports.Queue = function(delayms, maxExport, call){ this.size = 0; this.enqueue = function(job){ - if (! job[0]) return; if (! (job instanceof exports.Job)) job = new exports.Job(job); + if (! job.data[0]) return; let prio = job.prio; if (!jobs[prio]){ jobs[prio] = []; @@ -94,15 +94,15 @@ exports.Queue = function(delayms, maxExport, call){ // Step 3: Then, pop job.normweight*num//1 elems from remaining, without num decrease or normweight recalc. But keep job.wacc = job.normweight*num%1 let efflen = num - dequeued.length; for (job of jobq){ - job.wacc += job.wt*efflen/weightsum; + let topop = job.wt*efflen/weightsum; + job.wacc += topop%1; + topop = topop-topop%1; let data = job.data; - let topop = job.wacc-job.wacc%1; - job.wacc -= topop dequeued.push(...data.splice(data.length-topop)); } // Step 4: Shallow copy job array, and sort by job.wacc. - for (job of jobq.splice().sort((el, ne) => el.wacc-ne.wacc)){ + for (job of jobq.slice().sort((el, ne) => el.wacc-ne.wacc)){ // Step 5: Iterate through array (high->low), and subtract 1 until the length of output is num. if (dequeued.length == num) break; job.wacc--; |