Binary search of sorted list for the smallest ix such that list(ix) >= val. On failure, returns size(list)+1
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| real(kind=dp), | intent(in) | :: | list(:) |
Sorted list |
||
| real(kind=dp), | intent(in) | :: | val |
Value to search for |
pure function find_index_bsearch(list, val) result(ix) real(dp), intent(in) :: list(:) !< Sorted list real(dp), intent(in) :: val !< Value to search for integer :: ix, i_min, i_max, i_middle i_min = 1 i_max = size(list) do while (i_min < i_max) ! This safely performs: i_middle = (i_max + i_min) / 2 i_middle = i_min + ishft(i_max - i_min, -1) if (list(i_middle) >= val) then i_max = i_middle else i_min = i_middle + 1 end if end do ix = i_min if (val > list(ix)) ix = size(list) + 1 end function find_index_bsearch