Skip to content

Commit

Permalink
feat: add flipColumn and flipRow views
Browse files Browse the repository at this point in the history
  • Loading branch information
targos committed Aug 4, 2016
1 parent f0f8375 commit 55ee4a6
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/abstractMatrix.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ var util = require('./util');
var MatrixTransposeView = require('./views/transpose');
var MatrixRowView = require('./views/row');
var MatrixColumnView = require('./views/column');
var MatrixFlipRowView = require('./views/flipRow');
var MatrixFlipColumnView = require('./views/flipColumn');

function abstractMatrix(superCtor) {
if (superCtor === undefined) superCtor = Object;
Expand Down Expand Up @@ -1192,6 +1194,14 @@ function abstractMatrix(superCtor) {
util.checkColumnIndex(this, column);
return new MatrixColumnView(this, column);
}

flipRowView() {
return new MatrixFlipRowView(this);
}

flipColumnView() {
return new MatrixFlipColumnView(this);
}
}

Matrix.prototype.klass = 'Matrix';
Expand Down
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;
19 changes: 19 additions & 0 deletions test/views/flipColumn.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
'use strict';

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

describe('Flip column view', function () {
it('should set and get values', function () {
var m = Matrix.ones(5, 8);
var view = m.flipColumnView();

m.set(0, 3, 5);
view.get(0, 4).should.equal(5);

view.set(0, 0, 10);
m.get(0, 7).should.equal(10);

view.rows.should.equal(5);
view.columns.should.equal(8);
});
});
19 changes: 19 additions & 0 deletions test/views/flipRow.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
'use strict';

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

describe('Flip row view', function () {
it('should set and get values', function () {
var m = Matrix.ones(5, 8);
var view = m.flipRowView();

m.set(0, 3, 5);
view.get(4, 3).should.equal(5);

view.set(0, 0, 10);
m.get(4, 0).should.equal(10);

view.rows.should.equal(5);
view.columns.should.equal(8);
});
});

0 comments on commit 55ee4a6

Please sign in to comment.