Actual source code: numsrt.c

petsc-3.3-p7 2013-05-11
  1: /* numsrt.f -- translated by f2c (version of 25 March 1992  12:58:56). */

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

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

 12:     /* Local variables */
 13:     PetscInt jinc, i, j, k, l, jl, ju;

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

 58:     /* Parameter adjustments */
 60:     --next;
 61:     --idex;
 62:     --num;

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

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

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