Created
January 23, 2015 22:10
-
-
Save fge/d305c943680ff8b0d290 to your computer and use it in GitHub Desktop.
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
| private int binarySearch(final int index) | |
| { | |
| return doBinarySearch(0, nrLines - 1, index); | |
| } | |
| private int doBinarySearch(final int low, final int high, final int index) | |
| { | |
| // Guaranteed to always succeed at this point | |
| if (low == high) | |
| return low; | |
| final int middle = (low + high) / 2; | |
| final Range<Integer> range = lines.get(middle); | |
| if (range.contains(index)) | |
| return middle; | |
| return index < range.lowerEndpoint() | |
| ? doBinarySearch(low, middle, index) | |
| : doBinarySearch(middle, high, index); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment