-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAvailabilityIterator.js
67 lines (63 loc) · 2.1 KB
/
AvailabilityIterator.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
function AvailabilityIterator(aMatrix) {
this.aMatrix = aMatrix;
this.curSlot = undefined;
this.curPers = 0;
this.usedSlots = UTIL.fillArray(null, aMatrix.ys.length, function(index) {
return false;
});
this.usedPeople = UTIL.fillArray(null, aMatrix.ys.length, function(index) {
return false;
});
this.pCountBySlot = UTIL.fillArray(null, aMatrix.ys.length, function(index) {
return aMatrix.countYVals(index);
});
this.sCountByPerson = UTIL.fillArray(null, aMatrix.xs.length, function(index) {
return aMatrix.countXVals(index);
});
this.staffedCountByPerson = UTIL.fillArray(null, aMatrix.xs.length, function(index) {
return 0;
});
}
AvailabilityIterator.prototype.nextTimeslot = function() {
var that = this;
if(this.curSlot != undefined) {
var aPeople = this.aMatrix.getYVals(this.curSlot);
UTIL.forEach(aPeople, function(value) {
that.sCountByPerson[value]--;
});
}
this.curSlot = UTIL.leastIndex(this.pCountBySlot, this.usedSlots);
this.usedSlots[this.curSlot] = true;
this.usedPeople = UTIL.fillArray(null, this.aMatrix.xs.length, function(index) {
return !that.aMatrix.isSet(index, that.curSlot) || that.sCountByPerson[index] == 0;
});
return this.curSlot;
}
AvailabilityIterator.prototype.nextPerson = function() {
var that = this;
var leastStaffedValue = UTIL.leastValue(this.staffedCountByPerson, this.usedPeople);
var leastStaffedPeople = [];
UTIL.forEach(this.staffedCountByPerson, function(value, index) {
if(!that.usedPeople[index] && value === leastStaffedValue)
leastStaffedPeople.push(index);
});
if(leastStaffedPeople.length == 0) {
var index = -1;
}
else {
var least = this.sCountByPerson[leastStaffedPeople[0]] + 1;
var index = -1;
UTIL.forEach(leastStaffedPeople, function(value, i) {
if(least > that.sCountByPerson[value]) {
index = value;
least = that.sCountByPerson[value];
}
});
this.usedPeople[index] = true;
}
this.curPers = index;
return index;
}
AvailabilityIterator.prototype.confirmUse = function(person) {
this.staffedCountByPerson[person]++;
}