Actual source code: numsrt.c

      1: #define PETSCMAT_DLL
      2: /* numsrt.f -- translated by f2c (version of 25 March 1992  12:58:56). */

     4:  #include petsc.h
     5:  #include src/mat/color/color.h

      9: PetscErrorCode MINPACKnumsrt(PetscInt *n,PetscInt *nmax,PetscInt *num,PetscInt *mode,PetscInt *idex,PetscInt *last,PetscInt *next)
     10: {
     11:     /* System generated locals */
     12:     PetscInt i__1, i__2;

     14:     /* Local variables */
     15:     PetscInt jinc, i, j, k, l, jl, ju;

     17: /*     Given a sequence of integers, this subroutine groups */
     18: /*     together those indices with the same sequence value */
     19: /*     and, optionally, sorts the sequence into either */
     20: /*     ascending or descending order. */
     21: /*     The sequence of integers is defined by the array num, */
     22: /*     and it is assumed that the integers are each from the set */
     23: /*     0,1,...,nmax. On output the indices k such that num(k) = l */
     24: /*     for any l = 0,1,...,nmax can be obtained from the arrays */
     25: /*     last and next as follows. */
     26: /*           k = last(l) */
     27: /*           while (k .ne. 0) k = next(k) */
     28: /*     Optionally, the subroutine produces an array index so that */
     29: /*     the sequence num(index(i)), i = 1,2,...,n is sorted. */
     30: /*     The subroutine statement is */
     31: /*       subroutine numsrt(n,nmax,num,mode,index,last,next) */
     32: /*     where */
     33: /*       n is a positive integer input variable. */
     34: /*       nmax is a positive integer input variable. */
     35: /*       num is an input array of length n which contains the */
     36: /*         sequence of integers to be grouped and sorted. It */
     37: /*         is assumed that the integers are each from the set */
     38: /*         0,1,...,nmax. */
     39: /*       mode is an integer input variable. The sequence num is */
     40: /*         sorted in ascending order if mode is positive and in */
     41: /*         descending order if mode is negative. If mode is 0, */
     42: /*         no sorting is done. */
     43: /*       index is an integer output array of length n set so */
     44: /*         that the sequence */
     45: /*               num(index(i)), i = 1,2,...,n */
     46: /*         is sorted according to the setting of mode. If mode */
     47: /*         is 0, index is not referenced. */
     48: /*       last is an integer output array of length nmax + 1. The */
     49: /*         index of num for the last occurrence of l is last(l) */
     50: /*         for any l = 0,1,...,nmax unless last(l) = 0. In */
     51: /*         this case l does not appear in num. */
     52: /*       next is an integer output array of length n. If */
     53: /*         num(k) = l, then the index of num for the previous */
     54: /*         occurrence of l is next(k) for any l = 0,1,...,nmax */
     55: /*         unless next(k) = 0. In this case there is no previous */
     56: /*         occurrence of l in num. */
     57: /*     Argonne National Laboratory. MINPACK Project. July 1983. */
     58: /*     Thomas F. Coleman, Burton S. Garbow, Jorge J. More' */

     60:     /* Parameter adjustments */
     62:     --next;
     63:     --idex;
     64:     --num;

     66:     i__1 = *nmax;
     67:     for (i = 0; i <= i__1; ++i) {
     68:         last[i] = 0;
     69:     }
     70:     i__1 = *n;
     71:     for (k = 1; k <= i__1; ++k) {
     72:         l = num[k];
     73:         next[k] = last[l];
     74:         last[l] = k;
     75:     }
     76:     if (!*mode) {
     77:         return(0);
     78:     }

     80: /*     Store the pointers to the sorted array in index. */

     82:     i = 1;
     83:     if (*mode > 0) {
     84:         jl = 0;
     85:         ju = *nmax;
     86:         jinc = 1;
     87:     } else {
     88:         jl = *nmax;
     89:         ju = 0;
     90:         jinc = -1;
     91:     }
     92:     i__1 = ju;
     93:     i__2 = jinc;
     94:     for (j = jl; i__2 < 0 ? j >= i__1 : j <= i__1; j += i__2) {
     95:         k = last[j];
     96: L30:
     97:         if (!k) {
     98:             goto L40;
     99:         }
    100:         idex[i] = k;
    101:         ++i;
    102:         k = next[k];
    103:         goto L30;
    104: L40:
    105:         ;
    106:     }
    107:     return(0);
    108: }