Skip to content

Instantly share code, notes, and snippets.

@mparke
Created June 25, 2013 01:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mparke/5855304 to your computer and use it in GitHub Desktop.
Save mparke/5855304 to your computer and use it in GitHub Desktop.
Matrix shift and unshift prototype
function shift(){
var yLen = mx.length,
row;
if(yLen > 0){
row = mx[0];
if(row.length > 0){
return row.shift();
}else{
mx.shift();
return this.shift();
}
}
}
function unshiftRows(yIndex, row, val){
var nextIndex = ++yIndex;
row.unshift(val);
while(nextIndex < this.mx.length){
if(row.length > this.x){
val = row.pop();
row = this.mx[nextIndex];
row.unshift(val);
}else{
this.mx.push([val]);
}
++nextIndex;
}
}
function unshift(val){
var yLen = mx.length,
row, xLen;
if(yLen > 0){
// get the last row
row = mx[0];
row.unshift(val);
xLen = row.length;
if(xLen > this.x){
// need to push the next row
if(2 <= mx.length){
unshiftRows(1, mx[1], row.pop());
}else{
mx.push([row.pop()]);
}
}
}else{
mx.unshift([val]);
}
}
@mparke
Copy link
Author

mparke commented Jun 25, 2013

still pondering the usage of an unshift method and the associated overhead with re-organizing the matrix

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment