Actual source code: sortip.c

  1: /*$Id: sortip.c,v 1.37 2001/08/07 21:29:06 bsmith Exp $*/
  2: /*
  3:    This file contains routines for sorting integers and doubles with a permutation array.

  5:    The word "register"  in this code is used to identify data that is not
  6:    aliased.  For some compilers, this can cause the compiler to fail to
  7:    place inner-loop variables into registers.
  8:  */
 9:  #include petsc.h
 10:  #include petscsys.h

 12: #define SWAP(a,b,t) {t=a;a=b;b=t;}

 14: static int PetscSortIntWithPermutation_Private(const int v[],int vdx[],int right)
 15: {
 16:   int ierr,tmp,i,vl,last;

 19:   if (right <= 1) {
 20:     if (right == 1) {
 21:       if (v[vdx[0]] > v[vdx[1]]) SWAP(vdx[0],vdx[1],tmp);
 22:     }
 23:     return(0);
 24:   }
 25:   SWAP(vdx[0],vdx[right/2],tmp);
 26:   vl   = v[vdx[0]];
 27:   last = 0;
 28:   for (i=1; i<=right; i++) {
 29:     if (v[vdx[i]] < vl) {last++; SWAP(vdx[last],vdx[i],tmp);}
 30:   }
 31:   SWAP(vdx[0],vdx[last],tmp);
 32:   PetscSortIntWithPermutation_Private(v,vdx,last-1);
 33:   PetscSortIntWithPermutation_Private(v,vdx+last+1,right-(last+1));
 34:   return(0);
 35: }

 37: /*@
 38:    PetscSortIntWithPermutation - Computes the permutation of values that gives 
 39:    a sorted sequence.

 41:    Not Collective

 43:    Input Parameters:
 44: +  n  - number of values to sort
 45: .  i  - values to sort
 46: -  idx - permutation array.  Must be initialized to 0:n-1 on input.

 48:    Level: intermediate

 50:    Notes: 
 51:    i is unchanged on output.

 53:    Concepts: sorting^ints with permutation

 55: .seealso: PetscSortInt(), PetscSortRealWithPermutation()
 56:  @*/
 57: int PetscSortIntWithPermutation(int n,const int i[],int idx[])
 58: {
 59:   int ierr,j,k,tmp,ik;

 62:   if (n<8) {
 63:     for (k=0; k<n; k++) {
 64:       ik = i[idx[k]];
 65:       for (j=k+1; j<n; j++) {
 66:         if (ik > i[idx[j]]) {
 67:           SWAP(idx[k],idx[j],tmp);
 68:           ik = i[idx[k]];
 69:         }
 70:       }
 71:     }
 72:   } else {
 73:     PetscSortIntWithPermutation_Private(i,idx,n-1);
 74:   }
 75:   return(0);
 76: }

 78: /* ---------------------------------------------------------------------- */

 80: static int PetscSortRealWithPermutation_Private(const PetscReal v[],int vdx[],int right)
 81: {
 82:   PetscReal vl;
 83:   int       ierr,tmp,i,last;

 86:   if (right <= 1) {
 87:     if (right == 1) {
 88:       if (v[vdx[0]] > v[vdx[1]]) SWAP(vdx[0],vdx[1],tmp);
 89:     }
 90:     return(0);
 91:   }
 92:   SWAP(vdx[0],vdx[right/2],tmp);
 93:   vl   = v[vdx[0]];
 94:   last = 0;
 95:   for (i=1; i<=right; i++) {
 96:     if (v[vdx[i]] < vl) {last++; SWAP(vdx[last],vdx[i],tmp);}
 97:   }
 98:   SWAP(vdx[0],vdx[last],tmp);
 99:   PetscSortRealWithPermutation_Private(v,vdx,last-1);
100:   PetscSortRealWithPermutation_Private(v,vdx+last+1,right-(last+1));
101:   return(0);
102: }

104: /*@
105:    PetscSortRealWithPermutation - Computes the permutation of values that gives 
106:    a sorted sequence.

108:    Not Collective

110:    Input Parameters:
111: +  n  - number of values to sort
112: .  i  - values to sort
113: -  idx - permutation array.  Must be initialized to 0:n-1 on input.

115:    Level: intermediate

117:    Notes: 
118:    i is unchanged on output.

120:    Concepts: sorting^doubles with permutation

122: .seealso: PetscSortReal(), PetscSortIntWithPermutation()
123:  @*/
124: int PetscSortRealWithPermutation(int n,const PetscReal i[],int idx[])
125: {
126:   int       j,k,tmp,ierr;
127:   PetscReal ik;

130:   if (n<8) {
131:     for (k=0; k<n; k++) {
132:       ik = i[idx[k]];
133:       for (j=k+1; j<n; j++) {
134:         if (ik > i[idx[j]]) {
135:           SWAP(idx[k],idx[j],tmp);
136:           ik = i[idx[k]];
137:         }
138:       }
139:     }
140:   } else {
141:     PetscSortRealWithPermutation_Private(i,idx,n-1);
142:   }
143:   return(0);
144: }