Skip to content

Commit

Permalink
Make data optional during construction
Browse files Browse the repository at this point in the history
  • Loading branch information
ethanresnick committed Dec 18, 2012
1 parent 37b8bd4 commit e19eb49
Showing 1 changed file with 27 additions and 7 deletions.
34 changes: 27 additions & 7 deletions lib/types/matrix.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,36 @@
var matrix = require('../numbers/matrix');

var Matrix = function (data) {
if (!data) {
throw new Error('must provide valid data');
if (data) {
this.data = data;

//cache row and colCount so .length
//isn't constantly recalculatd.
this.colCount = this.data[0].length;
this.rowCount = this.data.length;
}
}

this.data = data;
/**
* Fill a matrix with an array of arrays.
*
* @param {Array} Array of array of numbers.
* @return {Matrix} Updated/Filled matrix.
*/
Matrix.prototype.fill = function(aoa) {
for (var i = 0, rowCount = aoa.length, colCount = aoa[0].length; i < rowCount; i++) {
if (aoa[i].length != colCount) {
throw new IllegalArgumentException("All rows must have the same length.");
}
for (var j = 0; j < colCount; j++) {
this.data[i][j] = aoa[i][j];
}
}

//cache row and colCount so .length
//isn't constantly recalculatd.
this.colCount = this.data[0].length;
this.rowCount = this.data.length;
this.colCount = colCount;
this.rowCount = rowCount;

return this;
}

/**
Expand Down

0 comments on commit e19eb49

Please sign in to comment.