-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMatrix.js
More file actions
134 lines (124 loc) · 4 KB
/
Matrix.js
File metadata and controls
134 lines (124 loc) · 4 KB
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
var Staffing = Staffing || {};
/**
* Creates a new instance of Matrix
*
* @param {Number} xcount The width of the array
* @param {Number} ycount The height of the array
* @param {Array} [initialValues] An Array of length xcount containing Arrays of
* length ycount which contains values upon which the array should be
* initialized. If this parameter is provided, emptyVal must also be provided.
* @param [emptyVal] A value to compare each value to using ==. If the
* comparison returns true, this value will not be stored in the matrix.
* @classDescription The Matrix class represents a zero-indexed 2d matrix.
*/
Staffing.Matrix = function Matrix(xcount, ycount, initialValues, emptyVal) {
var args = arguments;
this.xs = UTIL.fillArray(null, xcount, function(index) {
var ret = new UTIL.Dictionary();
if(args.length > 2) {
for(var y = 0; y < ycount; y++) {
if(initialValues[index][y] != emptyVal)
ret.store(y, initialValues[index][y]);
}
}
return ret;
});
this.ys = UTIL.fillArray(null, ycount, function(index) {
var ret = new UTIL.Dictionary();
if(args.length > 2) {
for(var x = 0; x < xcount; x++) {
if(initialValues[x][index] != emptyVal)
ret.store(x, initialValues[x][index]);
}
}
return ret;
});
};
/**
* @property {Error} An exception meaning the element accessed was out of the
* matrix bounds
*/
Staffing.Matrix.prototype.OutOfBoundsException = new Error("Out of matrix bounds!");
/**
* Sets a value in the matrix
*
* @param {Number} x The x coordinate of the parameter to set
* @param {Number} y The y coordinate of the parameter to set
* @param {Object, String, Number, Boolean} The value to set at the specified
* location
* @exception {Matrix.prototype.OutofBoundsException} An exception meaning the
* element accessed was outside of the bounds of the matrix.
*/
Staffing.Matrix.prototype.setValue = function(x, y, value) {
if(x < this.xs.length && y < this.ys.length) {
this.xs[x].store(y, value);
this.ys[y].store(x, value);
}
else
throw Matrix.prototype.OutOfBoundsException;
};
/**
* Removes/unsets a value in the matrix
*
* @param {Number} x The x coordinate of the parameter to remove
* @param {Number} y The y coordinate of the parameter to remove
* @exception {Matrix.prototype.OutofBoundsException} An exception meaning the
* element accessed was outside of the bounds of the matrix.
*/
Staffing.Matrix.prototype.removeValue = function(x, y) {
if(x < this.xs.length && y < this.ys.length) {
this.xs[x].remove(y);
this.ys[y].remove(x);
}
else
throw Matrix.prototype.OutOfBoundsException
};
/**
* Checks to see if a property is set in this matrix
*
* @param {Number} x The x coordinate of the parameter to check
* @param {Number} y The y coordinate of the parameter to check
* @return {Boolean} True if set, false otherwise
*/
Staffing.Matrix.prototype.isSet = function(x, y) {
return this.xs[x].contains(y);
};
/**
* Counts the values in an X row
* @param {Number} x The x number of the row to check
* @return {Number} The number of set values
*/
Staffing.Matrix.prototype.countXVals = function(x) {
return this.xs[x].count;
};
/**
* Counts the values in an y column
* @param {Number} y The y number of the column to check
* @return {Number} The number of set values
*/
Staffing.Matrix.prototype.countYVals = function(y) {
return this.ys[y].count
}
Staffing.Matrix.prototype.getXVals = function(x) {
var ret = [];
UTIL.forEachIn(this.xs[x].values, function(key, value) {
ret.push(key);
});
return ret;
}
Staffing.Matrix.prototype.getYVals = function(y) {
var ret = [];
UTIL.forEachIn(this.ys[y].values, function(key, value) {
ret.push(key);
});
return ret;
}
Staffing.Matrix.prototype.get2DArray = function() {
var that = this;
var ret = UTIL.fillArray(null, that.xs.length, function(xIndex) {
return UTIL.fillArray(null, that.ys.length, function(yIndex) {
return that.xs[xIndex].lookup(yIndex);
});
});
return ret;
}