Created
February 5, 2014 21:13
-
-
Save skeggse/8833227 to your computer and use it in GitHub Desktop.
Binary Search Generator Thingy
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* locate(low, high) { | |
var mid; | |
if (high === Infinity || high === undefined) { | |
mid = 1; | |
for (;;) { | |
if ((yield mid) > 0) { | |
mid <<= 1; | |
} else { | |
high = mid << 1; | |
break; | |
} | |
} | |
} | |
while (low < high) { | |
mid = (low + high) >>> 1; | |
(yield mid) > 0 ? (low = mid + 1) : (high = mid); | |
} | |
return low; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment