numsrt Subroutine

private subroutine numsrt(n, Nmax, Num, Mode, Index, Last, Next)

Given a sequence of integers, this subroutine groups together those indices with the same sequence value and, optionally, sorts the sequence into either ascending or descending order.

The sequence of integers is defined by the array num, and it is assumed that the integers are each from the set 0,1,...,nmax. on output the indices k such that num(k) = l for any l = 0,1,...,nmax can be obtained from the arrays last and next as follows.

  k = last(l)
  while (k /= 0) k = next(k)

Optionally, the subroutine produces an array index so that the sequence num(index(i)), i = 1,2,...,n is sorted.

Arguments

Type IntentOptional Attributes Name
integer, intent(in) :: n

a positive integer input variable.

integer :: Nmax

a positive integer input variable.

integer, dimension(n) :: Num

an input array of length n which contains the sequence of integers to be grouped and sorted. it is assumed that the integers are each from the set 0,1,...,nmax.

integer :: Mode

an integer input variable. the sequence num is sorted in ascending order if mode is positive and in descending order if mode is negative. if mode is 0, no sorting is done.

integer, dimension(n) :: Index

an integer output array of length n set so that the sequence num(index(i)), i = 1,2,...,n is sorted according to the setting of mode. if mode is 0, index is not referenced.

integer, dimension(0:Nmax) :: Last

an integer output array of length nmax + 1. the index of num for the last occurrence of l is last(l) for any l = 0,1,...,nmax unless last(l) = 0. in this case l does not appear in num.

integer, dimension(n) :: Next

an integer output array of length n. if num(k) = l, then the index of num for the previous occurrence of l is next(k) for any l = 0,1,...,nmax unless next(k) = 0. in this case there is no previous occurrence of l in num.


Called by

proc~~numsrt~~CalledByGraph proc~numsrt dsm_module::numsrt proc~dsm dsm_module::dsm proc~dsm->proc~numsrt proc~ido dsm_module::ido proc~dsm->proc~ido proc~ido->proc~numsrt proc~dsm_wrapper numerical_differentiation_module::sparsity_pattern%dsm_wrapper proc~dsm_wrapper->proc~dsm proc~compute_sparsity_random numerical_differentiation_module::compute_sparsity_random proc~compute_sparsity_random->proc~dsm_wrapper proc~compute_sparsity_random_2 numerical_differentiation_module::compute_sparsity_random_2 proc~compute_sparsity_random_2->proc~dsm_wrapper proc~set_sparsity_pattern numerical_differentiation_module::numdiff_type%set_sparsity_pattern proc~set_sparsity_pattern->proc~dsm_wrapper

Source Code

    subroutine numsrt(n,Nmax,Num,Mode,Index,Last,Next)

    implicit none

    integer,intent(in)        :: n      !! a positive integer input variable.
    integer                   :: Nmax   !! a positive integer input variable.
    integer                   :: Mode   !! an integer input variable. the sequence `num` is
                                        !! sorted in ascending order if `mode` is positive and in
                                        !! descending order if `mode` is negative. if `mode` is 0,
                                        !! no sorting is done.
    integer,dimension(n)      :: Num    !! an input array of length `n` which contains the
                                        !! sequence of integers to be grouped and sorted. it
                                        !! is assumed that the integers are each from the set
                                        !! `0,1,...,nmax`.
    integer,dimension(n)      :: Index  !! an integer output array of length `n` set so
                                        !! that the sequence
                                        !! `num(index(i)), i = 1,2,...,n`
                                        !! is sorted according to the setting of mode.
                                        !! if `mode` is 0, `index` is not referenced.
    integer,dimension(0:Nmax) :: Last   !! an integer output array of length `nmax + 1`. the
                                        !! index of `num` for the last occurrence of `l` is `last(l)`
                                        !! for any `l = 0,1,...,nmax` unless `last(l) = 0`. in
                                        !! this case `l` does not appear in `num`.
    integer,dimension(n)      :: Next   !! an integer output array of length `n`. if
                                        !! `num(k) = l`, then the index of `num` for the previous
                                        !! occurrence of `l` is `next(k)` for any `l = 0,1,...,nmax`
                                        !! unless `next(k) = 0`. in this case there is no previous
                                        !! occurrence of `l` in `num`.

    integer :: i , j , jinc , jl , ju , k , l

    ! determine the arrays next and last.

    do i = 0 , Nmax
        Last(i) = 0
    enddo

    do k = 1 , n
        l = Num(k)
        Next(k) = Last(l)
        Last(l) = k
    enddo

    if ( Mode/=0 ) then

        ! store the pointers to the sorted array in index.
        i = 1
        if ( Mode>0 ) then
            jl = 0
            ju = Nmax
            jinc = 1
        else
            jl = Nmax
            ju = 0
            jinc = -1
        endif
        do j = jl , ju , jinc
            k = Last(j)
            do
                if (k==0) exit
                Index(i) = k
                i = i + 1
                k = Next(k)
            enddo
        enddo

    end if

    end subroutine numsrt