search a value in an ordered array of indices
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
integer, | intent(in), | DIMENSION(:) | :: | array | ||
integer, | intent(in) | :: | key | |||
integer, | intent(out) | :: | loc | |||
logical, | intent(out) | :: | found | |||
integer, | intent(in), | optional | :: | lb | ||
integer, | intent(in), | optional | :: | ub |
PURE SUBROUTINE ordered_search(array, key, loc, found, lb, ub) !! search a value in an ordered array of indices INTEGER, DIMENSION(:), INTENT(IN) :: array INTEGER, INTENT(IN) :: key INTEGER, INTENT(OUT) :: loc LOGICAL, INTENT(OUT) :: found INTEGER, INTENT(IN), OPTIONAL :: lb, ub INTEGER :: high, low, val found = .FALSE. IF (PRESENT(lb)) THEN low = lb ELSE low = LBOUND(array, 1) END IF IF (PRESENT(ub)) THEN high = ub ELSE high = UBOUND(array, 1) END IF loc = (low + high)/2 DO WHILE (loc .GE. low .AND. loc .LE. high) val = array(loc) IF (val .EQ. key) THEN found = .TRUE. EXIT ELSEIF (val .LT. key) THEN low = loc + 1 ELSE high = loc - 1 END IF loc = (low + high)/2 END DO END SUBROUTINE ordered_search