Skip to content

Instantly share code, notes, and snippets.

@Gerjo
Created November 4, 2013 14:45
Show Gist options
  • Save Gerjo/7303512 to your computer and use it in GitHub Desktop.
Save Gerjo/7303512 to your computer and use it in GitHub Desktop.
Javascript template specialisation.
function GenMatrix(rows, cols) {
function Matrix() {
// Whatever prefered way of storing you may have.
this.nums = new Array(rows * cols);
}
if(rows == 2 && cols == 2) {
Matrix.prototype.inverse = function() {
// optimize matrix inverse code for 2x2
}
} else {
Matrix.prototype.inverse = function() {
// The generic inverse logic.
}
}
return Matrix;
}
var M33 = GenMatrix(3, 3);
var M22 = GenMatrix(2, 2);
var m33 = new M33();
m33.inverse(); // calls generic inverse method.
var m22 = new M22();
m22.inverse(); // calls specialized inverse for 2x2 matrices.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment