Created
August 12, 2015 17:20
-
-
Save animecyc/0289f0464bfae5844cec to your computer and use it in GitHub Desktop.
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
var fs = require('fs'); | |
function MatrixSearch(file, search) { | |
var repo = fs.readFileSync(file, { | |
encoding: 'utf8' | |
}).trim(); | |
this.matrixSize = search.length | |
this.matrices = this.chunk(repo.split('\n').map(function(line) { | |
return this.chunk(line.split(''), this.matrixSize); | |
}, this), this.matrixSize).map(function(chunk) { | |
return this.matrix(chunk, this.matrixSize); | |
}, this); | |
} | |
MatrixSearch.prototype.matrix = function(rows, size) { | |
if (rows.length != size) { | |
throw new Error('Not enough row data'); | |
} | |
var matrix = new Array(size); | |
for (var i = 0; i < matrix.length; i++) { | |
var matrixRow = new Array(size); | |
for (var j = 0; j < matrixRow.length; j++) { | |
matrixRow[j] = rows[i][j]; | |
} | |
matrix[i] = matrixRow; | |
} | |
return matrix; | |
}; | |
MatrixSearch.prototype.chunk = function(arr, size) { | |
var chunks = new Array(arr.length); | |
for (var i = 0; i < arr.length; i++) { | |
chunks[i] = arr.slice(i, i + size); | |
if (chunks[i].length !== size) { | |
delete chunks[i]; | |
} | |
} | |
return chunks.filter(function(chunk) { | |
return chunk; | |
}); | |
}; | |
new MatrixSearch('table.txt', '123'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment