Actual source code: numsrt.c

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

      3: #include <../src/mat/graphops/color/impls/minpack/color.h>

      5: PetscErrorCode MINPACKnumsrt(PetscInt *n, PetscInt *nmax, PetscInt *num, PetscInt *mode, PetscInt *idex, PetscInt *last, PetscInt *next)
      6: {
      7:   /* System generated locals */
      8:   PetscInt i__1, i__2;

     10:   /* Local variables */
     11:   PetscInt jinc, i, j, k, l, jl, ju;

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

     56:   /* Parameter adjustments */
     57:   PetscFunctionBegin;
     58:   --next;
     59:   --idex;
     60:   --num;

     62:   i__1 = *nmax;
     63:   for (i = 0; i <= i__1; ++i) last[i] = 0;

     65:   i__1 = *n;
     66:   for (k = 1; k <= i__1; ++k) {
     67:     l       = num[k];
     68:     next[k] = last[l];
     69:     last[l] = k;
     70:   }
     71:   if (!*mode) PetscFunctionReturn(PETSC_SUCCESS);

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

     75:   i = 1;
     76:   if (*mode > 0) {
     77:     jl   = 0;
     78:     ju   = *nmax;
     79:     jinc = 1;
     80:   } else {
     81:     jl   = *nmax;
     82:     ju   = 0;
     83:     jinc = -1;
     84:   }
     85:   i__1 = ju;
     86:   i__2 = jinc;
     87:   for (j = jl; i__2 < 0 ? j >= i__1 : j <= i__1; j += i__2) {
     88:     k = last[j];
     89:   L30:
     90:     if (!k) goto L40;
     91:     idex[i] = k;
     92:     ++i;
     93:     k = next[k];
     94:     goto L30;
     95:   L40:;
     96:   }
     97:   PetscFunctionReturn(PETSC_SUCCESS);
     98: }