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
//
// GENERATED FILE
//
use super::*;
use crate::SpiceContext;
use f2rust_std::*;
pub const LBCELL: i32 = -5;
/// Insert an item into a character set
///
/// Insert an item into a character set.
///
/// # Required Reading
///
/// * [SETS](crate::required_reading::sets)
///
/// # Brief I/O
///
/// ```text
/// VARIABLE I/O DESCRIPTION
/// -------- --- --------------------------------------------------
/// ITEM I Item to be inserted.
/// A I-O Insertion set.
/// ```
///
/// # Detailed Input
///
/// ```text
/// ITEM is an item which is to be inserted into the specified
/// set. ITEM may or may not already be an element of the
/// set.
///
/// If ITEM is longer than the declared maximum length of the
/// set's elements, the string will be truncated on the right
/// when it is inserted. Trailing blanks in ITEM are not
/// significant.
///
/// A is a SPICE set.
///
/// On input, A may or may not contain the input item as an
/// element.
/// ```
///
/// # Detailed Output
///
/// ```text
/// A on output, contains the union of the input set and the
/// singleton set containing the input item, unless there was
/// not sufficient room in the set for the item to be
/// included, in which case the set is not changed and an
/// error is signaled.
/// ```
///
/// # Exceptions
///
/// ```text
/// 1) If the insertion of the item into the set causes an excess
/// of elements, the error SPICE(SETEXCESS) is signaled.
///
/// 2) If the item to be inserted has greater length than the string
/// length of the elements of the set, the item will be truncated
/// on the right when it is inserted. The insertion point of the
/// element will be determined by the comparison of the truncated
/// item to members of the set. If, after truncation, the item to
/// be inserted matches an element already present in the set, no
/// insertion occurs.
/// ```
///
/// # 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) Create a set with all the original planets of the Solar
/// System and then remove Pluto from that set.
///
///
/// Example code begins here.
///
///
/// PROGRAM INSRTC_EX1
/// IMPLICIT NONE
///
/// C
/// C SPICELIB functions.
/// C
/// INTEGER CARDC
///
/// C
/// C Local constants.
/// C
/// INTEGER LBCELL
/// PARAMETER ( LBCELL = -5 )
///
/// INTEGER PNAMSZ
/// PARAMETER ( PNAMSZ = 7 )
///
/// INTEGER SETDIM
/// PARAMETER ( SETDIM = 9 )
///
/// C
/// C Local variables.
/// C
/// CHARACTER*(PNAMSZ) LIST ( SETDIM )
/// CHARACTER*(PNAMSZ) PLNETS ( LBCELL:SETDIM )
///
/// INTEGER I
///
/// C
/// C Create the original planets list.
/// C
/// DATA LIST /
/// . 'MERCURY', 'VENUS', 'EARTH',
/// . 'MARS', 'JUPITER', 'SATURN',
/// . 'URANUS', 'NEPTUNE', 'PLUTO' /
///
/// C
/// C Initialize the empty set.
/// C
/// CALL VALIDC ( SETDIM, 0, PLNETS )
///
/// C
/// C Insert the list of planets into the set. If the item is
/// C an element of the set, the set is not changed.
/// C
/// DO I = 1, SETDIM
///
/// CALL INSRTC ( LIST(I), PLNETS )
///
/// END DO
///
/// C
/// C Remove the Pluto from the set. If the Pluto is not an
/// C element of the set, the set is not changed.
/// C
/// CALL REMOVC ( 'PLUTO', PLNETS )
///
/// C
/// C Output the contents of PLNETS.
/// C
/// WRITE(*,*) 'Planets of the Solar System:'
///
/// DO I = 1, CARDC ( PLNETS )
///
/// WRITE(*,*) ' ', PLNETS(I)
///
/// END DO
///
/// END
///
///
/// When this program was executed on a Mac/Intel/gfortran/64-bit
/// platform, the output was:
///
///
/// Planets of the Solar System:
/// EARTH
/// JUPITER
/// MARS
/// MERCURY
/// NEPTUNE
/// SATURN
/// URANUS
/// VENUS
/// ```
///
/// # Author and Institution
///
/// ```text
/// N.J. Bachman (JPL)
/// C.A. Curzon (JPL)
/// J. Diaz del Rio (ODC Space)
/// W.L. Taber (JPL)
/// I.M. Underwood (JPL)
/// ```
///
/// # Version
///
/// ```text
/// - SPICELIB Version 2.1.0, 24-AUG-2021 (JDR)
///
/// Added IMPLICIT NONE statement.
///
/// Edited the header to comply with NAIF standard. Added complete
/// code example.
///
/// Improved the argument ITEM description in $Detailed_Input.
///
/// - SPICELIB Version 2.0.0, 01-NOV-2005 (NJB)
///
/// Bug fix: when the item to be inserted would, after
/// truncation to the set's string length, match an item
/// already in the set, no insertion is performed. Previously
/// the truncated string was inserted, corrupting the set.
///
/// Long error message was updated to include size of
/// set into which insertion was attempted.
///
/// - 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 (CAC) (WLT) (IMU)
/// ```
///
/// # Revisions
///
/// ```text
/// - Beta Version 1.1.0, 06-JAN-1989 (NJB)
///
/// Calling protocol of EXCESS changed. Call to SETMSG removed.
/// ```
pub fn insrtc(ctx: &mut SpiceContext, item: &str, a: CharArrayMut) -> crate::Result<()> {
INSRTC(item.as_bytes(), a, ctx.raw_context())?;
ctx.handle_errors()?;
Ok(())
}
//$Procedure INSRTC ( Insert an item into a character set )
pub fn INSRTC(ITEM: &[u8], A: CharArrayMut, ctx: &mut Context) -> f2rust_std::Result<()> {
let mut A = DummyCharArrayMut::new(A, None, LBCELL..);
let mut CARD: i32 = 0;
let mut LAST: i32 = 0;
let mut SIZE: i32 = 0;
let mut SLEN: i32 = 0;
let mut IN: bool = false;
//
// SPICELIB functions
//
//
// Local variables
//
//
// Set up the error processing.
//
if RETURN(ctx) {
return Ok(());
}
CHKIN(b"INSRTC", ctx)?;
//
// What are the size and cardinality of the set?
//
SIZE = SIZEC(A.as_arg(), ctx)?;
CARD = CARDC(A.as_arg(), ctx)?;
//
// When we insert an item into the set, any trailing characters
// that don't fit are truncated. So in deciding where to insert
// the item, we ignore any characters that won't remain after
// insertion.
//
// We're going to consider only the initial substring of ITEM
// whose length doesn't exceed the string length of the set's
// members.
//
SLEN = intrinsics::MIN0(&[intrinsics::LEN(ITEM), intrinsics::LEN(&A[1])]);
//
// Find the last element of the set which would come before the
// input item. This will be the item itself, if it is already an
// element of the set.
//
LAST = LSTLEC(fstr::substr(ITEM, 1..=SLEN), CARD, A.subarray(1));
//
// Is the item already in the set? If not, it needs to be inserted.
//
if (LAST > 0) {
IN = fstr::eq(A.get(LAST), fstr::substr(ITEM, 1..=SLEN));
} else {
IN = false;
}
if !IN {
//
// If there is room in the set for the new element, then move
// the succeeding elements back to make room. And update the
// cardinality for future reference.
//
if (CARD < SIZE) {
for I in intrinsics::range(CARD, (LAST + 1), -1) {
let val = A.get(I).to_vec();
fstr::assign(A.get_mut((I + 1)), &val);
}
fstr::assign(A.get_mut((LAST + 1)), fstr::substr(ITEM, 1..=SLEN));
SCARDC((CARD + 1), A.as_arg_mut(), ctx)?;
} else {
SETMSG(b"An element could not be inserted into the set due to lack of space; set size is #.", ctx);
ERRINT(b"#", SIZE, ctx);
SIGERR(b"SPICE(SETEXCESS)", ctx)?;
}
}
CHKOUT(b"INSRTC", ctx)?;
Ok(())
}