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
//
// GENERATED FILE
//
use super::*;
use crate::SpiceContext;
use f2rust_std::*;
/// Cycle the elements of an integer array, in place
///
/// Cycle the elements of an integer array forward or backward
/// in place.
///
/// # Brief I/O
///
/// ```text
/// VARIABLE I/O DESCRIPTION
/// -------- --- --------------------------------------------------
/// NELT I Number of elements.
/// DIR I Direction to cycle: 'F' or 'B'.
/// NCYCLE I Number of times to cycle.
/// ARRAY I-O Array to be cycled/cycled array.
/// ```
///
/// # Detailed Input
///
/// ```text
/// NELT is the number of elements in the input array.
///
/// DIR is the direction in which the elements in the
/// array are to be cycled.
///
/// 'F' or 'f' to cycle forward.
/// 'B' or 'b' to cycle backward.
///
/// NCYCLE is the number of times the elements in the array
/// are to be cycled.
///
/// ARRAY is the array to be cycled.
/// ```
///
/// # Detailed Output
///
/// ```text
/// ARRAY is the input array after it has been cycled.
/// ```
///
/// # Exceptions
///
/// ```text
/// 1) If the value of DIR is not recognized, the error
/// SPICE(INVALIDDIRECTION) is signaled.
///
/// 2) If NELT is less than 1, the output array is not modified.
///
/// 3) If NCYCLE is negative, the array is cycled NCYCLE times in
/// the opposite direction of DIR.
/// ```
///
/// # Particulars
///
/// ```text
/// This routine cycles an integer array in place. To cycle
/// an array and store the result in a new array, use CYCLAI.
///
/// An array is cycled when its contents are shifted forward or
/// backward by one place. An element pushed off one end of the
/// array is brought around to the other end of the array instead
/// of disappearing.
/// ```
///
/// # Examples
///
/// ```text
/// Let the integer array A contain the following elements.
///
/// A(1) = 1
/// A(2) = 2
/// A(3) = 3
/// A(4) = 4
///
/// Cycling A forward once yields the array
///
/// A(1) = 4
/// A(2) = 1
/// A(3) = 2
/// A(4) = 3
///
/// Cycling A backward once yields the array
///
/// A(1) = 2
/// A(2) = 3
/// A(3) = 4
/// A(4) = 1
///
/// Cycling by any multiple of the number of elements in the array
/// yields the same array.
/// ```
///
/// # Author and Institution
///
/// ```text
/// N.J. Bachman (JPL)
/// J. Diaz del Rio (ODC Space)
/// H.A. Neilan (JPL)
/// W.L. Taber (JPL)
/// I.M. Underwood (JPL)
/// ```
///
/// # Version
///
/// ```text
/// - SPICELIB Version 1.1.0, 12-AUG-2021 (JDR)
///
/// Added IMPLICIT NONE statement.
///
/// Edited the header to comply with NAIF standard.
///
/// - SPICELIB Version 1.0.0, 09-SEP-2005 (NJB) (HAN) (WLT) (IMU)
/// ```
pub fn cyaiip(
ctx: &mut SpiceContext,
nelt: i32,
dir: char,
ncycle: i32,
array: &mut [i32],
) -> crate::Result<()> {
CYAIIP(
nelt,
&[u8::try_from(dir).unwrap()],
ncycle,
array,
ctx.raw_context(),
)?;
ctx.handle_errors()?;
Ok(())
}
//$Procedure CYAIIP ( Cycle the elements of an integer array, in place )
pub fn CYAIIP(
NELT: i32,
DIR: &[u8],
NCYCLE: i32,
ARRAY: &mut [i32],
ctx: &mut Context,
) -> f2rust_std::Result<()> {
let DIR = &DIR[..1];
let mut ARRAY = DummyArrayMut::new(ARRAY, 1..);
let mut LAST: i32 = 0;
let mut TEMP: i32 = 0;
let mut G: i32 = 0;
let mut K: i32 = 0;
let mut L: i32 = 0;
let mut M: i32 = 0;
//
// SPICELIB functions
//
//
// Local variables
//
//
// Standard SPICE error handling.
//
if RETURN(ctx) {
return Ok(());
} else {
CHKIN(b"CYAIIP", ctx)?;
}
//
// Don't even screw around if there are no elements in the array.
//
if (NELT < 1) {
CHKOUT(b"CYAIIP", ctx)?;
return Ok(());
}
//
// A backward cycle is the same as a forward cycle by the opposite
// of NCYCLE. Moreover a cycle by K is the same as a cycle by
// K + m*N for any integer m. Thus we compute the value of the
// minimum forward right cycle that is equivalent to the inputs.
//
if (fstr::eq(DIR, b"B") || fstr::eq(DIR, b"b")) {
K = intrinsics::MOD(-NCYCLE, NELT);
} else if (fstr::eq(DIR, b"F") || fstr::eq(DIR, b"F")) {
K = intrinsics::MOD(NCYCLE, NELT);
} else {
SETMSG(b"Cycling direction was *.", ctx);
ERRCH(b"*", DIR, ctx);
SIGERR(b"SPICE(INVALIDDIRECTION)", ctx)?;
CHKOUT(b"CYAIIP", ctx)?;
return Ok(());
}
if (K < 0) {
K = (K + NELT);
} else if (K == 0) {
CHKOUT(b"CYAIIP", ctx)?;
return Ok(());
}
//
// The algorithm used to cycle arrays is identical to the one used
// to cycle character strings in CYCLEC. We won't repeat the (rather
// lengthy) description here.
//
G = GCD(K, NELT);
M = (NELT / G);
for I in 1..=G {
L = I;
LAST = ARRAY[L];
for J in 1..=M {
L = (L + K);
if (L > NELT) {
L = (L - NELT);
}
TEMP = ARRAY[L];
ARRAY[L] = LAST;
LAST = TEMP;
}
}
CHKOUT(b"CYAIIP", ctx)?;
Ok(())
}