C
C SUBROUTINE 'IDXRNK'
C ===================
C
C INVERTS A PERMUTATION, CONVERTING A RANK VECTOR TO AN INDEX VECTOR
C OR VICE VERSA. THE INVERSION IS DONE IN PLACE.
C
      SUBROUTINE IDXRNK (IR,N)
C
C PARAMETERS
C
C      IR (I/O)  INTEGER ARRAY HOLDING A PERMUTATION OF THE POSITIVE
C                INTEGERS FROM 1 TO N
C      N  (I)    THE NUMBER OF ELEMENTS IN THE ARRAY (VECTOR)
C
C SPECIFICATIION STATEMENTS
C -------------------------
C
      INTEGER IR(*)
C
C INVERT THE PERMUTATION
C ----------------------
C
      IPOS = 1
      INEXT = IR(1)
      K = 0
 20   IF (IR(INEXT).LT.0) THEN
 30      K = K + 1
         IF (K.GT.N) THEN
            DO 40 I = 1 , N
               IR(I) = -IR(I)
 40         CONTINUE
            RETURN
         ELSE
            IF (IR(K).LT.0) GO TO 30
            IPOS = K
            INEXT = IR(K)
         END IF
      END IF
      JNEXT = IR(INEXT)
      IR(INEXT) = -IPOS
      IPOS = INEXT
      INEXT = JNEXT
      GO TO 20
      END
