1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
//
// GENERATED FILE
//
use super::*;
use crate::SpiceContext;
use f2rust_std::*;
/// Reorder a character array
///
/// Reorder the elements of an array of character strings according to
/// a given order vector.
///
/// # Brief I/O
///
/// ```text
/// VARIABLE I/O DESCRIPTION
/// -------- --- --------------------------------------------------
/// IORDER I Order vector to be used to re-order ARRAY.
/// NDIM I Dimension of ARRAY.
/// ARRAY I-O Array to be re-ordered.
/// ```
///
/// # Detailed Input
///
/// ```text
/// IORDER is the order vector to be used to re-order the input
/// array. The first element of IORDER is the index of
/// the first item of the re-ordered array, and so on.
///
/// Note that the order imposed by REORDC is not the
/// same order that would be imposed by a sorting
/// routine. In general, the order vector will have
/// been created (by one of the ORDER routines) for
/// a related array, as illustrated in the example below.
///
/// NDIM is the number of elements in the input array.
///
/// ARRAY on input, is an array containing some number of
/// elements in unspecified order.
/// ```
///
/// # Detailed Output
///
/// ```text
/// ARRAY on output, is the same array, with the elements
/// re-ordered as specified by IORDER.
/// ```
///
/// # Exceptions
///
/// ```text
/// Error free.
///
/// 1) If NDIM < 2, this routine executes a no-op. This case is
/// not an error.
/// ```
///
/// # Particulars
///
/// ```text
/// REORDC uses a cyclical algorithm to re-order the elements of
/// the array in place. After re-ordering, element IORDER(1) of
/// the input array is the first element of the output array,
/// element IORDER(2) is the input array is the second element of
/// the output array, and so on.
///
/// The order vector used by REORDC is typically created for
/// a related array by one of the ORDER routines, as shown in
/// the example below.
/// ```
///
/// # Examples
///
/// ```text
/// The numerical results shown for this example may differ across
/// platforms. The results depend on the SPICE kernels used as
/// input, the compiler and supporting libraries, and the machine
/// specific arithmetic implementation.
///
/// 1) Sort four related arrays (containing the names, masses,
/// integer ID codes, and flags indicating whether they have
/// a ring system, for a group of planets).
///
///
/// Example code begins here.
///
///
/// PROGRAM REORDC_EX1
/// IMPLICIT NONE
///
/// C
/// C Local constants.
/// C
/// INTEGER NDIM
/// PARAMETER ( NDIM = 8 )
///
/// INTEGER STRLEN
/// PARAMETER ( STRLEN = 7 )
///
/// C
/// C Local variables.
/// C
/// CHARACTER*(STRLEN) NAMES ( NDIM )
///
/// DOUBLE PRECISION MASSES ( NDIM )
///
/// INTEGER CODES ( NDIM )
/// INTEGER I
/// INTEGER IORDER ( NDIM )
///
/// LOGICAL RINGS ( NDIM )
///
/// C
/// C Set the arrays containing the names, masses (given as
/// C ratios to of Solar GM to barycenter GM), integer ID
/// C codes, and flags indicating whether they have a ring
/// C system.
/// C
/// DATA NAMES /
/// . 'MERCURY', 'VENUS', 'EARTH', 'MARS',
/// . 'JUPITER', 'SATURN', 'URANUS', 'NEPTUNE' /
///
/// DATA MASSES /
/// . 22032.080D0, 324858.599D0,
/// . 398600.436D0, 42828.314D0,
/// . 126712767.881D0, 37940626.068D0,
/// . 5794559.128D0, 6836534.065D0 /
///
/// DATA CODES / 199, 299, 399, 499,
/// . 599, 699, 799, 899 /
///
/// DATA RINGS /
/// . .FALSE., .FALSE., .FALSE., .FALSE.,
/// . .TRUE., .TRUE., .TRUE., .TRUE. /
///
/// C
/// C Sort the object arrays by name.
/// C
/// CALL ORDERC ( NAMES, NDIM, IORDER )
///
/// CALL REORDC ( IORDER, NDIM, NAMES )
/// CALL REORDD ( IORDER, NDIM, MASSES )
/// CALL REORDI ( IORDER, NDIM, CODES )
/// CALL REORDL ( IORDER, NDIM, RINGS )
///
/// C
/// C Output the resulting table.
/// C
/// WRITE(*,'(A)') ' Planet Mass(GMS/GM) ID Code Rings?'
/// WRITE(*,'(A)') '------- ------------- ------- ------'
///
/// DO I = 1, NDIM
///
/// WRITE(*,'(A,F15.3,I9,L5)') NAMES(I), MASSES(I),
/// . CODES(I), RINGS(I)
///
/// END DO
///
/// END
///
///
/// When this program was executed on a Mac/Intel/gfortran/64-bit
/// platform, the output was:
///
///
/// Planet Mass(GMS/GM) ID Code Rings?
/// ------- ------------- ------- ------
/// EARTH 398600.436 399 F
/// JUPITER 126712767.881 599 T
/// MARS 42828.314 499 F
/// MERCURY 22032.080 199 F
/// NEPTUNE 6836534.065 899 T
/// SATURN 37940626.068 699 T
/// URANUS 5794559.128 799 T
/// VENUS 324858.599 299 F
/// ```
///
/// # Author and Institution
///
/// ```text
/// J. Diaz del Rio (ODC Space)
/// W.L. Taber (JPL)
/// I.M. Underwood (JPL)
/// ```
///
/// # Version
///
/// ```text
/// - SPICELIB Version 1.1.0, 27-AUG-2021 (JDR)
///
/// Added IMPLICIT NONE statement.
///
/// Edited the header to comply with NAIF standard.
/// Added complete code example.
///
/// Added entry #1 in $Exceptions section.
///
/// - SPICELIB Version 1.0.1, 10-MAR-1992 (WLT)
///
/// Comment section for permuted index source lines was added
/// following the header.
///
/// - SPICELIB Version 1.0.0, 31-JAN-1990 (WLT) (IMU)
/// ```
pub fn reordc(iorder: &mut [i32], ndim: i32, array: CharArrayMut) {
REORDC(iorder, ndim, array);
}
//$Procedure REORDC ( Reorder a character array )
pub fn REORDC(IORDER: &mut [i32], NDIM: i32, ARRAY: CharArrayMut) {
let mut IORDER = DummyArrayMut::new(IORDER, 1..);
let mut ARRAY = DummyCharArrayMut::new(ARRAY, None, 1..);
let mut START: i32 = 0;
let mut INDEX: i32 = 0;
let mut HOLD: i32 = 0;
let mut TEMP = [b' '; 1];
//
// Local variables
//
//
// If the array doesn't have at least two elements, don't bother.
//
if (NDIM < 2) {
return;
}
//
// What follows is pretty much the same as for the other REORD
// routines. The character routine is somewhat special in that
// the use of a temporary variable would cause strings longer
// than the variable to be truncated. Rather than just declare
// a giant character string, the entire algorithm will be repeated
// for each character in each string. That is, the first characters
// will be ordered, then the second characters, and so on. This
// looks messy as hell, but the same number of operations are
// involved (more or less).
//
for C in 1..=intrinsics::LEN(&ARRAY[1]) {
//
// START is the position in the order vector that begins the
// current cycle. When all the switches have been made, START
// will point to the end of the order vector.
//
START = 1;
while (START < NDIM) {
//
// Begin with the element of input vector specified by
// IORDER(START). Move it to the correct position in the
// array, after saving the element it replaces to TEMP.
// HOLD indicates the position of the array element to
// be moved to its new position. After the element has
// been moved, HOLD indicates the position of an available
// space within the array.
//
INDEX = START;
fstr::assign(&mut TEMP, fstr::substr(ARRAY.get(INDEX), C..=C));
HOLD = IORDER[INDEX];
//
// As each slot in the output array is filled in, the sign
// of the corresponding element in the order vector is changed
// from positive to negative. This way, we know which elements
// have already been ordered when looking for the beginning of
// the next cycle.
//
// Keep going until HOLD points to the first array element
// moved during the current cycle. This ends the cycle.
//
while (HOLD != START) {
let val = fstr::substr(ARRAY.get(HOLD), C..=C).to_vec();
fstr::assign(fstr::substr_mut(ARRAY.get_mut(INDEX), C..=C), &val);
INDEX = HOLD;
HOLD = IORDER[HOLD];
IORDER[INDEX] = -IORDER[INDEX];
}
//
// The last element in the cycle is restored from TEMP.
//
fstr::assign(fstr::substr_mut(ARRAY.get_mut(INDEX), C..=C), &TEMP);
IORDER[HOLD] = -IORDER[HOLD];
//
// Begin the next cycle at the next element in the order
// vector with a positive sign. (That is, the next one
// that hasn't been moved.)
//
while ((IORDER[START] < 0) && (START < NDIM)) {
START = (START + 1);
}
}
//
// Restore the original signs of the elements of the order
// vector, for the next go around.
//
{
let m1__: i32 = 1;
let m2__: i32 = NDIM;
let m3__: i32 = 1;
INDEX = m1__;
for _ in 0..((m2__ - m1__ + m3__) / m3__) as i32 {
IORDER[INDEX] = i32::abs(IORDER[INDEX]);
INDEX += m3__;
}
}
}
}