michael@0: # Uses a binary search algorithm to locate a value in the specified array. michael@0: window.binary_search = (items, value) -> michael@0: michael@0: start = 0 michael@0: stop = items.length - 1 michael@0: pivot = Math.floor (start + stop) / 2 michael@0: michael@0: while items[pivot] isnt value and start < stop michael@0: michael@0: # Adjust the search area. michael@0: stop = pivot - 1 if value < items[pivot] michael@0: start = pivot + 1 if value > items[pivot] michael@0: michael@0: # Recalculate the pivot. michael@0: pivot = Math.floor (stop + start) / 2 michael@0: michael@0: # Make sure we've found the correct value. michael@0: if items[pivot] is value then pivot else -1