sortposofval_label Function

public function sortposofval_label(me, val, nextifnotfound, lower, upper) result(pos)

return the sorted position of a value in the given dynamic array

if the value was not found, - return 0 if nextifnotfound = .false. - return position at the end if nextifnotfound = .true.

binary search on sorted list

Arguments

Type IntentOptional Attributes Name
type(dyn_labelarray_type), intent(in) :: me
character(len=*), intent(in) :: val
logical, intent(in), optional :: nextifnotfound

flag to indicate, if the next entry in the list should be returned, if the searched one is not found.

integer, intent(in), optional :: lower
integer, intent(in), optional :: upper

Return Value integer


Called by

proc~~sortposofval_label~~CalledByGraph proc~sortposofval_label sortposofval_label interface~sortedposofval~5 sortedposofval interface~sortedposofval~5->proc~sortposofval_label proc~append_da_int append_da_int proc~append_da_int->interface~sortedposofval~5 proc~append_da_label append_da_label proc~append_da_label->interface~sortedposofval~5 proc~append_da_long append_da_long proc~append_da_long->interface~sortedposofval~5 proc~append_da_real append_da_real proc~append_da_real->interface~sortedposofval~5 proc~posofval_int posofval_int proc~posofval_int->interface~sortedposofval~5 proc~posofval_label posofval_label proc~posofval_label->interface~sortedposofval~5 proc~posofval_long posofval_long proc~posofval_long->interface~sortedposofval~5 proc~posofval_real posofval_real proc~posofval_real->interface~sortedposofval~5 interface~append~26 append interface~append~26->proc~append_da_long interface~append~27 append interface~append~27->proc~append_da_int interface~append~28 append interface~append~28->proc~append_da_real interface~append~29 append interface~append~29->proc~append_da_label interface~positionofval~2 positionofval interface~positionofval~2->proc~posofval_long interface~positionofval~3 positionofval interface~positionofval~3->proc~posofval_int interface~positionofval~4 positionofval interface~positionofval~4->proc~posofval_real interface~positionofval~5 positionofval interface~positionofval~5->proc~posofval_label

Source Code

  function sortposofval_label(me, val, nextifnotfound, lower, upper) result(pos)
    !------------------------------------------------------------------------
    type(dyn_labelarray_type), intent(in) :: me !< dynamic array
    character(len=*), intent(in) :: val !< value to look for
    !> flag to indicate, if the next entry in the list should be returned,
    !! if the searched one is not found.
    logical, intent(in), optional :: nextifnotfound
    integer, intent(in), optional :: lower !< lower search limit
    integer, intent(in), optional :: upper !< upper search limit
    integer :: pos !< position of val in the sorted list, 0 if not found
    !------------------------------------------------------------------------
    logical :: retnext
    integer :: lb, ub
    integer :: mid
    character(len=labellen) :: lb_val, ub_val
    character(len=labellen) :: mid_val
    !------------------------------------------------------------------------

    retnext = .false.
    if (present(nextifnotfound)) retnext = nextifnotfound

    lb = 1
    ub = me%nvals

    if( present( lower ) ) lb = lower
    if( present( upper ) ) ub = upper

    pos = 0
    if (retnext) pos = lb

    !> binary search on sorted list
    do while(ub >= lb)
      lb_val = me%val(me%sorted(lb))

      if (val < lb_val) then
        if (retnext) pos = lb
        exit
      end if

      ub_val = me%val(me%sorted(ub))

      if (val > ub_val) then
        if (retnext) pos = ub+1
        exit
      end if

      ! safe guard against integer limit overflow
      mid = lb + (ub-lb) / 2
      mid_val = me%val(me%sorted(mid))
      if (val == mid_val) then
        pos = mid
        exit
      end if
      if (val > mid_val) then
        lb = mid + 1
      else
        ub = mid - 1
      end if
    end do
  end function sortposofval_label