/** * Finds the position of the closest location in the sorted list of locations. * This is a binary search of logarithmic complexity. */ static int findClosestLocation(final double[] locs, final double l) { assert(locs.length > 0); if (locs.length == 1) { return 0; } final int firstGreater = findFirstGreater(locs, l); final int left; final int right; if (firstGreater == -1 || firstGreater == 0) { // All locations are greater, or all locations are smaller. // Closest location must be either smallest or greatest location. left = locs.length - 1; right = 0; } else { left = firstGreater - 1; right = firstGreater; } if (Location.distance(l, locs[left]) <= Location.distance(l, locs[right])) { return left; } return right; }