Created
November 4, 2013 14:45
-
-
Save Gerjo/7303512 to your computer and use it in GitHub Desktop.
Javascript template specialisation.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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