Array.prototype.bSearch = function (key, options) {
this.sortDESCENDING = (options & Array.DESCENDING) ? true : false;
this.sortNUMERIC = (options & Array.NUMERIC) ? true : false;
this.sortCASEINSENSITIVE = (options & Array.CASEINSENSITIVE) ? true : false;
this.padRole = String.fromCharCode(31); var iL = 0, iU = this.length-1, iM = 0; var iterations = 0;
while (iU-iL > 1) {
iM = (iU+iL) >> 1; iterations++;
this.getRow2Test(key, iM); if ( (this.sKey >= this.tKey) != this.sortDESCENDING)
iL=iM;
else
iU=iM;
}
if ( iterations > Math.round(Math.LOG2E*Math.log(this.length)) ) {
trace( "Warning: search took too long! \n Details: " + iterations + " iterations of outer search loop were used, \n compared to theoretical maximum of log2(array size): " + Math.round(Math.LOG2E*Math.log(this.length)) );
trace( " Perhaps this array has not been sorted correctly, or different options should be pased to this function." );
}
this.getRow2Test(key, (this.sortDESCENDING ? iU: iL)) if ( this.sKey == this.tKey )
return (this.sortDESCENDING ? iU: iL);
this.getRow2Test(key, 0)
if (this.sKey == this.tKey)
return 0;
this.getRow2Test(key, this.length-1)
if (this.sKey == this.tKey)
return this.length-1;
return -1; };
Array.prototype.getRow2Test = function (key, row) {
this.sKey=""; this.tKey=""; for (var role in key) {
var iPadRole = Math.max( String(key[role]).length, String(this[row][role]).length ) + 1;
if ( typeof key[role] == "number" && this.sortNUMERIC) {
for (var p = String(key[role]).length; p<iPadRole; p++)
this.sKey += this.padRole;
for (var p = String(this[row][role]).length; p<iPadRole; p++)
this.tKey += this.padRole;
this.sKey += String(key[role])
this.tKey += String(this[row][role])
}
else {
this.sKey += String(key[role])
this.tKey += String(this[row][role])
for (var p = String(key[role]).length; p<iPadRole; p++)
this.sKey += this.padRole;
for (var p = String(this[row][role]).length; p<iPadRole; p++)
this.tKey += this.padRole;
}
}
if (this.sortCASEINSENSITIVE) {
this.sKey = this.sKey.toUpperCase();
this.tKey = this.tKey.toUpperCase();
}
};
var playList = new Array();
playList.push( {slide: 2, clip: "_level0.instance3", cue: 4000} );
playList.push( {slide: 3, clip: "_level0.instance1", cue: 5000} );
playList.push( {slide: 1, clip: "_level0.instance1", cue: 1000} );
playList.push( {slide: 10, clip: "_level0.instance2", cue: 2000} );
playList.sortOn( ["clip", "slide"], Array.DESCENDING | Array.NUMERIC | Array.CASEINSENSITIVE)
var index = playList.bSearch( {clip: "_level0.instance1", slide: 1}, Array.DESCENDING | Array.NUMERIC | Array.CASEINSENSITIVE )
if (index)
trace("clip found, with cue time: " + playList[index].cue); else
trace("Oops, clip not found");