package { /* Note: As a way of storing the highest bit while avoiding some issues when the highest bit gets especially high, instead a mask is stored which can mask any bit being used So, (anyPossibleIndex & maxBits) === anyPossibleIndex maxMask never decreases in this example either. */ public class ArraySearchExample { private var maxMask:uint = 0; private var arr:Array = []; public function ArraySearchExample() { push(128); push(128.01); push(127.99); push(1); push(Number.MAX_VALUE); push(1000); push(Number.MIN_VALUE); push(Number.POSITIVE_INFINITY); push(Number.NEGATIVE_INFINITY); trace(arr.join("\r")); } public function push(entry:Number):void { var cBit:uint = (maxMask >>> 1) + 1; var pos:uint = 0; do { // Test the current bit if ((pos | cBit) <= arr.length && entry >= arr[(pos | cBit)-1]) { pos |= cBit; } } while (cBit >>>= 1); arr.splice(pos,0,entry); // Check to see if maxMask should be higher while ((arr.length & ~maxMask) !== 0) { maxMask |= (maxMask + 1); break; } } public function pop():Number { return arr.pop() as Number; } } }