Returns the number of suffixes strictly less than the query string. We note that rank(select(i)) equals i for each i between 0 and n -1. @param query the query string @return the number of suffixes strictly less than query
(String query)
| 168 | * @return the number of suffixes strictly less than {@code query} |
| 169 | */ |
| 170 | public int rank(String query) { |
| 171 | int lo = 0, hi = suffixes.length - 1; |
| 172 | while (lo <= hi) { |
| 173 | int mid = lo + (hi - lo) / 2; |
| 174 | int cmp = compare(query, suffixes[mid]); |
| 175 | if (cmp < 0) hi = mid - 1; |
| 176 | else if (cmp > 0) lo = mid + 1; |
| 177 | else return mid; |
| 178 | } |
| 179 | return lo; |
| 180 | } |
| 181 | |
| 182 | // compare query string to suffix |
| 183 | private static int compare(String query, Suffix suffix) { |