Skip to content

Commit

Permalink
Merge branch 'abstract'
Browse files Browse the repository at this point in the history
  • Loading branch information
targos committed Aug 4, 2016
2 parents 42c35e1 + 833c1eb commit 2581302
Show file tree
Hide file tree
Showing 21 changed files with 1,984 additions and 1,464 deletions.
1,472 changes: 1,472 additions & 0 deletions src/abstractMatrix.js

Large diffs are not rendered by default.

1,484 changes: 22 additions & 1,462 deletions src/matrix.js

Large diffs are not rendered by default.

5 changes: 5 additions & 0 deletions src/symbol-species.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
'use strict';

if (!Symbol.species) {
Symbol.species = Symbol.for('@@species');
}
110 changes: 110 additions & 0 deletions src/util.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
'use strict';

/**
* @private
* Check that a row index is not out of bounds
* @param {Matrix} matrix
* @param {number} index
* @param {boolean} [outer]
*/
exports.checkRowIndex = function checkRowIndex(matrix, index, outer) {
var max = outer ? matrix.rows : matrix.rows - 1;
if (index < 0 || index > max) {
throw new RangeError('Row index out of range');
}
};

/**
* @private
* Check that a column index is not out of bounds
* @param {Matrix} matrix
* @param {number} index
* @param {boolean} [outer]
*/
exports.checkColumnIndex = function checkColumnIndex(matrix, index, outer) {
var max = outer ? matrix.columns : matrix.columns - 1;
if (index < 0 || index > max) {
throw new RangeError('Column index out of range');
}
};

/**
* @private
* Check that the provided vector is an array with the right length
* @param {Matrix} matrix
* @param {Array|Matrix} vector
* @returns {Array}
* @throws {RangeError}
*/
exports.checkRowVector = function checkRowVector(matrix, vector) {
if (vector.to1DArray) {
vector = vector.to1DArray();
}
if (vector.length !== matrix.columns) {
throw new RangeError('vector size must be the same as the number of columns');
}
return vector;
};

/**
* @private
* Check that the provided vector is an array with the right length
* @param {Matrix} matrix
* @param {Array|Matrix} vector
* @returns {Array}
* @throws {RangeError}
*/
exports.checkColumnVector = function checkColumnVector(matrix, vector) {
if (vector.to1DArray) {
vector = vector.to1DArray();
}
if (vector.length !== matrix.rows) {
throw new RangeError('vector size must be the same as the number of rows');
}
return vector;
};

exports.checkIndices = function checkIndices(matrix, rowIndices, columnIndices) {
var rowOut = rowIndices.some(r => {
return r < 0 || r >= matrix.rows;

});

var columnOut = columnIndices.some(c => {
return c < 0 || c >= matrix.columns;
});

if (rowOut || columnOut) {
throw new RangeError('Indices are out of range')
}

if (typeof rowIndices !== 'object' || typeof columnIndices !== 'object') {
throw new TypeError('Unexpected type for row/column indices');
}
if (!Array.isArray(rowIndices)) rowIndices = Array.from(rowIndices);
if (!Array.isArray(columnIndices)) rowIndices = Array.from(columnIndices);

return {
row: rowIndices,
column: columnIndices
};
};

exports.checkRange = function checkRange(matrix, startRow, endRow, startColumn, endColumn) {
if (arguments.length !== 5) throw new TypeError('Invalid argument type');
var notAllNumbers = Array.from(arguments).slice(1).some(function (arg) {
return typeof arg !== 'number';
});
if (notAllNumbers) throw new TypeError('Invalid argument type');
if (startRow > endRow || startColumn > endColumn || startRow < 0 || startRow >= matrix.rows || endRow < 0 || endRow >= matrix.rows || startColumn < 0 || startColumn >= matrix.columns || endColumn < 0 || endColumn >= matrix.columns) {
throw new RangeError('Submatrix indices are out of range');
}
};

exports.getRange = function getRange(from, to) {
var arr = new Array(to - from + 1);
for (var i = 0; i < arr.length; i++) {
arr[i] = from + i;
}
return arr;
};
22 changes: 22 additions & 0 deletions src/views/base.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
'use strict';

var abstractMatrix = require('../abstractMatrix');
var Matrix;

class BaseView extends abstractMatrix() {
constructor(matrix, rows, columns) {
super();
this.matrix = matrix;
this.rows = rows;
this.columns = columns;
}

static get [Symbol.species]() {
if (!Matrix) {
Matrix = require('../matrix');
}
return Matrix;
}
}

module.exports = BaseView;
21 changes: 21 additions & 0 deletions src/views/column.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
'use strict';

var BaseView = require('./base');

class MatrixColumnView extends BaseView {
constructor(matrix, column) {
super(matrix, matrix.rows, 1);
this.column = column;
}

set(rowIndex, columnIndex, value) {
this.matrix.set(rowIndex, this.column, value);
return this;
}

get(rowIndex, columnIndex) {
return this.matrix.get(rowIndex, this.column);
}
}

module.exports = MatrixColumnView;
20 changes: 20 additions & 0 deletions src/views/flipColumn.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
'use strict';

var BaseView = require('./base');

class MatrixFlipColumnView extends BaseView {
constructor(matrix) {
super(matrix, matrix.rows, matrix.columns);
}

set(rowIndex, columnIndex, value) {
this.matrix.set(rowIndex, this.columns - columnIndex - 1, value);
return this;
}

get(rowIndex, columnIndex) {
return this.matrix.get(rowIndex, this.columns - columnIndex - 1);
}
}

module.exports = MatrixFlipColumnView;
20 changes: 20 additions & 0 deletions src/views/flipRow.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
'use strict';

var BaseView = require('./base');

class MatrixFlipRowView extends BaseView {
constructor(matrix) {
super(matrix, matrix.rows, matrix.columns);
}

set(rowIndex, columnIndex, value) {
this.matrix.set(this.rows - rowIndex - 1, columnIndex, value);
return this;
}

get(rowIndex, columnIndex) {
return this.matrix.get(this.rows - rowIndex - 1, columnIndex);
}
}

module.exports = MatrixFlipRowView;
21 changes: 21 additions & 0 deletions src/views/row.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
'use strict';

var BaseView = require('./base');

class MatrixRowView extends BaseView {
constructor(matrix, row) {
super(matrix, 1, matrix.columns);
this.row = row;
}

set(rowIndex, columnIndex, value) {
this.matrix.set(this.row, columnIndex, value);
return this;
}

get(rowIndex, columnIndex) {
return this.matrix.get(this.row, columnIndex);
}
}

module.exports = MatrixRowView;
24 changes: 24 additions & 0 deletions src/views/selection.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
'use strict';

var BaseView = require('./base');
var util = require('../util');

class MatrixSelectionView extends BaseView {
constructor(matrix, rowIndices, columnIndices) {
var indices = util.checkIndices(matrix, rowIndices, columnIndices);
super(matrix, indices.row.length, indices.column.length);
this.rowIndices = indices.row;
this.columnIndices = indices.column;
}

set(rowIndex, columnIndex, value) {
this.matrix.set(this.rowIndices[rowIndex], this.columnIndices[columnIndex] , value);
return this;
}

get(rowIndex, columnIndex) {
return this.matrix.get(this.rowIndices[rowIndex], this.columnIndices[columnIndex]);
}
}

module.exports = MatrixSelectionView;
24 changes: 24 additions & 0 deletions src/views/sub.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
'use strict';

var BaseView = require('./base');
var util = require('../util');

class MatrixSubView extends BaseView {
constructor(matrix, startRow, endRow, startColumn, endColumn) {
util.checkRange(matrix, startRow, endRow, startColumn, endColumn);
super(matrix, endRow - startRow + 1, endColumn - startColumn + 1);
this.startRow = startRow;
this.startColumn = startColumn;
}

set(rowIndex, columnIndex, value) {
this.matrix.set(this.startRow + rowIndex, this.startColumn + columnIndex , value);
return this;
}

get(rowIndex, columnIndex) {
return this.matrix.get(this.startRow + rowIndex, this.startColumn + columnIndex);
}
}

module.exports = MatrixSubView;
20 changes: 20 additions & 0 deletions src/views/transpose.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
'use strict';

var BaseView = require('./base');

class MatrixTransposeView extends BaseView {
constructor(matrix) {
super(matrix, matrix.columns, matrix.rows);
}

set(rowIndex, columnIndex, value) {
this.matrix.set(columnIndex, rowIndex, value);
return this;
}

get(rowIndex, columnIndex) {
return this.matrix.get(columnIndex, rowIndex);
}
}

module.exports = MatrixTransposeView;
Loading

0 comments on commit 2581302

Please sign in to comment.