aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHolden Rohrer <hr@hrhr.dev>2020-01-17 00:44:11 -0500
committerHolden Rohrer <hr@hrhr.dev>2020-01-17 00:44:11 -0500
commit32615549686567094ce80d89ad33695acfafa3ef (patch)
tree451ebcb28dd27f874e1f070e41dd5a4e97385d89
parent7e32677b7d61d84cf9b67133f9a12d37523120ba (diff)
reorg comments in tools/schedule.js
-rw-r--r--tools/schedule.js24
1 files changed, 14 insertions, 10 deletions
diff --git a/tools/schedule.js b/tools/schedule.js
index 399b8e4..83d7853 100644
--- a/tools/schedule.js
+++ b/tools/schedule.js
@@ -27,7 +27,8 @@ function Queue(maxExport, call, delay){
let prio = job.prio;
if (!jobs[prio]) jobs[prio] = [];
jobs[prio].splice(0, Math.abs(bs(jobs[prio], job, (el, ne) => el.maxr - ne.maxr)), job);
- prios.splice(0, Math.abs(bs(prios, prio, (el, ne) => el-ne)), prio)
+ prios.splice(0, Math.abs(bs(prios, prio, (el, ne) => el-ne)), prio); // prios is meant to be sorted least to most, and each job layer is too (by "maximum number of rounds").
+ // These were sorted like this so that getNumOrAll could use [0] or [.length-1] or .pop instead of having to re-sort lists repetitively.
if (open) dequeue();
}
@@ -41,6 +42,7 @@ function Queue(maxExport, call, delay){
}
function dequeue(){
+ // Wraps getNumOrAll, managing open/disab, and concatenating possibly multiple layers
if (disab){
open = true;
return;
@@ -60,22 +62,18 @@ function Queue(maxExport, call, delay){
}
function getNumOrAll(prio, num){
- /*
-
- Step 1: (Pre-)sort by job.data.length/job.weight.
- Step 2: Start at lowest, and pop all until job.data.length>job.normweight*num (decreasing num as popping and recalc job.normweight). Delete the job.
- 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
- Step 4: Shallow copy job array, and sort by job.wacc.
- Step 5: Iterate through array (high->low), and subtract 1 until the length of output is num.
- Step 6: If empty, remove prio && jobs[prio]; return.
- */
+ // Step 1: (Pre-)sort by job.data.length/job.weight.
let jobq = jobs[prio];
let dequeued = [];
+
+ // Step 2: Start at lowest, and pop all until job.data.length>job.normweight*num (decreasing num as popping and recalc job.normweight). Delete the job.
let weightsum = Math.sum(jobq.map(job => job.wt)).reduce((acc, cur)=>acc+cur);
while (job[0].data.length<(job[0].wacc+job[0].wt*num/weightsum)){
weightsum -= job.wt;
dequeued.push(...jobq.shift().data);
}
+
+ // 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
for (job of jobq){
job.wacc += job.wt*num/weightsum;
let data = job.data;
@@ -83,15 +81,21 @@ function Queue(maxExport, call, delay){
job.wacc -= topop
dequeued.push(...data.splice(-topop));
}
+
+ // Step 4: Shallow copy job array, and sort by job.wacc.
for (job of jobq.splice().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--;
dequeue.push(job.pop());
}
+
+ // Step 6: If empty, remove prio && jobs[prio]; return.
if (jobq.length == 0){
delete jobs[prio];
prios.splice(bs(prios, prio, (el, ne)=>el-ne), 1);
}
+
return dequeued;
}
}