Created
June 25, 2013 01:50
-
-
Save mparke/5855304 to your computer and use it in GitHub Desktop.
Matrix shift and unshift prototype
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 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]); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
still pondering the usage of an unshift method and the associated overhead with re-organizing the matrix