1 /* reordi.f -- translated by f2c (version 19980913).
2 You must link the resulting object file with the libraries:
3 -lf2c -lm (in that order)
4 */
5
6 #include "f2c.h"
7
8 /* $Procedure REORDI ( Reorder an integer array ) */
reordi_(integer * iorder,integer * ndim,integer * array)9 /* Subroutine */ int reordi_(integer *iorder, integer *ndim, integer *array)
10 {
11 /* System generated locals */
12 integer i__1, i__2;
13
14 /* Local variables */
15 integer hold, temp, index, start;
16
17 /* $ Abstract */
18
19 /* Re-order the elements of an integer array according to */
20 /* a given order vector. */
21
22 /* $ Disclaimer */
23
24 /* THIS SOFTWARE AND ANY RELATED MATERIALS WERE CREATED BY THE */
25 /* CALIFORNIA INSTITUTE OF TECHNOLOGY (CALTECH) UNDER A U.S. */
26 /* GOVERNMENT CONTRACT WITH THE NATIONAL AERONAUTICS AND SPACE */
27 /* ADMINISTRATION (NASA). THE SOFTWARE IS TECHNOLOGY AND SOFTWARE */
28 /* PUBLICLY AVAILABLE UNDER U.S. EXPORT LAWS AND IS PROVIDED "AS-IS" */
29 /* TO THE RECIPIENT WITHOUT WARRANTY OF ANY KIND, INCLUDING ANY */
30 /* WARRANTIES OF PERFORMANCE OR MERCHANTABILITY OR FITNESS FOR A */
31 /* PARTICULAR USE OR PURPOSE (AS SET FORTH IN UNITED STATES UCC */
32 /* SECTIONS 2312-2313) OR FOR ANY PURPOSE WHATSOEVER, FOR THE */
33 /* SOFTWARE AND RELATED MATERIALS, HOWEVER USED. */
34
35 /* IN NO EVENT SHALL CALTECH, ITS JET PROPULSION LABORATORY, OR NASA */
36 /* BE LIABLE FOR ANY DAMAGES AND/OR COSTS, INCLUDING, BUT NOT */
37 /* LIMITED TO, INCIDENTAL OR CONSEQUENTIAL DAMAGES OF ANY KIND, */
38 /* INCLUDING ECONOMIC DAMAGE OR INJURY TO PROPERTY AND LOST PROFITS, */
39 /* REGARDLESS OF WHETHER CALTECH, JPL, OR NASA BE ADVISED, HAVE */
40 /* REASON TO KNOW, OR, IN FACT, SHALL KNOW OF THE POSSIBILITY. */
41
42 /* RECIPIENT BEARS ALL RISK RELATING TO QUALITY AND PERFORMANCE OF */
43 /* THE SOFTWARE AND ANY RELATED MATERIALS, AND AGREES TO INDEMNIFY */
44 /* CALTECH AND NASA FOR ALL THIRD-PARTY CLAIMS RESULTING FROM THE */
45 /* ACTIONS OF RECIPIENT IN THE USE OF THE SOFTWARE. */
46
47 /* $ Required_Reading */
48
49 /* None. */
50
51 /* $ Keywords */
52
53 /* ARRAY, SORT */
54
55 /* $ Declarations */
56 /* $ Brief_I/O */
57
58 /* VARIABLE I/O DESCRIPTION */
59 /* -------- --- -------------------------------------------------- */
60 /* IORDER I Order vector to be used to re-order ARRAY. */
61 /* NDIM I Dimension of ARRAY. */
62 /* ARRAY I/O Array to be re-ordered. */
63
64 /* $ Detailed_Input */
65
66 /* IORDER is the order vector to be used to re-order the input */
67 /* array. The first element of IORDER is the index of */
68 /* the first item of the re-ordered array, and so on. */
69
70 /* Note that the order imposed by REORDI is not the */
71 /* same order that would be imposed by a sorting */
72 /* routine. In general, the order vector will have */
73 /* been created (by one of the ORDER routines) for */
74 /* a related array, as illustrated in the example below. */
75
76 /* NDIM is the number of elements in the input array. */
77
78 /* ARRAY on input, is an array containing some number of */
79 /* elements in unspecified order. */
80
81 /* $ Detailed_Output */
82
83 /* ARRAY on output, is the same array, with the elements */
84 /* in re-ordered as specified by IORDER. */
85
86 /* $ Parameters */
87
88 /* None. */
89
90 /* $ Particulars */
91
92 /* REORDI uses a cyclical algorithm to re-order the elements of */
93 /* the array in place. After re-ordering, element IORDER(1) of */
94 /* the input array is the first element of the output array, */
95 /* element IORDER(2) is the input array is the second element of */
96 /* the output array, and so on. */
97
98 /* The order vector used by REORDI is typically created for */
99 /* a related array by one of the ORDER routines, as shown in */
100 /* the example below. */
101
102 /* $ Examples */
103
104 /* In the following example, the ORDER and REORD routines are */
105 /* used to sort four related arrays (containing the names, */
106 /* masses, integer ID codes, and visual magnitudes for a group */
107 /* of satellites). This is representative of the typical use of */
108 /* these routines. */
109
110 /* C */
111 /* C Sort the object arrays by name. */
112 /* C */
113 /* CALL ORDERC ( NAMES, N, IORDER ) */
114
115 /* CALL REORDC ( IORDER, N, NAMES ) */
116 /* CALL REORDD ( IORDER, N, MASSES ) */
117 /* CALL REORDI ( IORDER, N, CODES ) */
118 /* CALL REORDR ( IORDER, N, VMAGS ) */
119
120 /* $ Restrictions */
121
122 /* None. */
123
124 /* $ Exceptions */
125
126 /* Error free. */
127
128 /* $ Files */
129
130 /* None. */
131
132 /* $ Author_and_Institution */
133
134 /* W.L. Taber (JPL) */
135 /* I.M. Underwood (JPL) */
136
137 /* $ Literature_References */
138
139 /* None. */
140
141 /* $ Version */
142
143 /* - SPICELIB Version 1.0.1, 10-MAR-1992 (WLT) */
144
145 /* Comment section for permuted index source lines was added */
146 /* following the header. */
147
148 /* - SPICELIB Version 1.0.0, 31-JAN-1990 (WLT) (IMU) */
149
150 /* -& */
151 /* $ Index_Entries */
152
153 /* reorder an integer array */
154
155 /* -& */
156
157 /* Local variables */
158
159
160 /* If the array doesn't have at least two elements, don't bother. */
161
162 if (*ndim < 2) {
163 return 0;
164 }
165
166 /* START is the position in the order vector that begins the */
167 /* current cycle. When all the switches have been made, START */
168 /* will point to the end of the order vector. */
169
170 start = 1;
171 while(start < *ndim) {
172
173 /* Begin with the element of input vector specified by */
174 /* IORDER(START). Move it to the correct position in the */
175 /* array, after saving the element it replaces to TEMP. */
176 /* HOLD indicates the position of the array element to */
177 /* be moved to its new position. After the element has */
178 /* been moved, HOLD indicates the position of an available */
179 /* space within the array. */
180
181 index = start;
182 temp = array[index - 1];
183 hold = iorder[index - 1];
184
185 /* As each slot in the output array is filled in, the sign */
186 /* of the corresponding element in the order vector is changed */
187 /* from positive to negative. This way, we know which elements */
188 /* have already been ordered when looking for the beginning of */
189 /* the next cycle. */
190
191 /* Keep going until HOLD points to the first array element */
192 /* moved during the current cycle. This ends the cycle. */
193
194 while(hold != start) {
195 array[index - 1] = array[hold - 1];
196 index = hold;
197 hold = iorder[hold - 1];
198 iorder[index - 1] = -iorder[index - 1];
199 }
200
201 /* The last element in the cycle is restored from TEMP. */
202
203 array[index - 1] = temp;
204 iorder[hold - 1] = -iorder[hold - 1];
205
206 /* Begin the next cycle at the next element in the order */
207 /* vector with a positive sign. (That is, the next one */
208 /* that hasn't been moved.) */
209
210 while(iorder[start - 1] < 0 && start < *ndim) {
211 ++start;
212 }
213 }
214
215 /* Restore the original signs of the elements of the order vector, */
216 /* in case the vector is to be used again with another array. */
217
218 i__1 = *ndim;
219 for (index = 1; index <= i__1; ++index) {
220 iorder[index - 1] = (i__2 = iorder[index - 1], abs(i__2));
221 }
222 return 0;
223 } /* reordi_ */
224
225