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
//
// GENERATED FILE
//
use super::*;
use crate::SpiceContext;
use f2rust_std::*;
pub const LBCELL: i32 = -5;
/// Union two character sets
///
/// Compute the union of two character sets to form a third set.
///
/// # Required Reading
///
/// * [SETS](crate::required_reading::sets)
///
/// # Brief I/O
///
/// ```text
/// VARIABLE I/O DESCRIPTION
/// -------- --- --------------------------------------------------
/// A I First input set.
/// B I Second input set.
/// C O Union of A and B.
/// ```
///
/// # Detailed Input
///
/// ```text
/// A is a set.
///
///
/// B is a set, distinct from A.
/// ```
///
/// # Detailed Output
///
/// ```text
/// C is a set, distinct from sets A and B, which
/// contains the union of A and B (that is, all of
/// the elements which are in A or B or both).
///
/// If the size (maximum cardinality) of C is smaller
/// than the cardinality of the union of A and B,
/// then only as many items as will fit in C are
/// included, and an error is signaled.
/// ```
///
/// # Exceptions
///
/// ```text
/// 1) If the union of the two sets causes an excess of elements, the
/// error SPICE(SETEXCESS) is signaled.
///
/// 2) If length of the elements of the output set is < the
/// maximum of the lengths of the elements of the input
/// sets, the error SPICE(ELEMENTSTOOSHORT) is signaled.
/// ```
///
/// # Examples
///
/// ```text
/// The UNION of two sets contains every element which is
/// in the first set, or in the second set, or in both sets.
///
/// {a,b} union {c,d} = {a,b,c,d}
/// {a,b,c} {b,c,d} {a,b,c,d}
/// {a,b,c,d} {} {a,b,c,d}
/// {} {a,b,c,d} {a,b,c,d}
/// {} {} {}
///
/// The following call
///
/// CALL UNIONC ( PLANETS, ASTEROIDS, RESULT )
///
/// places the union of the character sets PLANETS and
/// ASTEROIDS into the character set RESULT.
///
/// The output set must be distinct from both of the input sets.
/// For example, the following calls are invalid.
///
/// CALL UNIONI ( CURRENT, NEW, CURRENT )
/// CALL UNIONI ( NEW, CURRENT, CURRENT )
///
/// In each of the examples above, whether or not the subroutine
/// signals an error, the results will almost certainly be wrong.
/// Nearly the same effect can be achieved, however, by placing the
/// result into a temporary set, which is immediately copied back
/// into one of the input sets, as shown below.
///
/// CALL UNIONI ( CURRENT, NEW, TEMP )
/// CALL COPYI ( TEMP, NEW )
/// ```
///
/// # 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 1.2.0, 26-OCT-2021 (JDR)
///
/// Added IMPLICIT NONE statement.
///
/// Edited the header to comply with NAIF standard.
///
/// - SPICELIB Version 1.1.0, 18-JUN-1999 (WLT)
///
/// Made CHKOUT calls consistent with CHKIN.
///
/// - 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) (NJB)
/// ```
///
/// # Revisions
///
/// ```text
/// - Beta Version 2.0.0, 05-JAN-1989 (NJB)
///
/// Error signaled if output set elements are not long enough.
/// Length must be at least max of lengths of input elements.
/// Also, calling protocol for EXCESS has been changed.
/// ```
pub fn unionc(
ctx: &mut SpiceContext,
a: CharArray,
b: CharArray,
c: CharArrayMut,
) -> crate::Result<()> {
UNIONC(a, b, c, ctx.raw_context())?;
ctx.handle_errors()?;
Ok(())
}
//$Procedure UNIONC ( Union two character sets )
pub fn UNIONC(
A: CharArray,
B: CharArray,
C: CharArrayMut,
ctx: &mut Context,
) -> f2rust_std::Result<()> {
let A = DummyCharArray::new(A, None, LBCELL..);
let B = DummyCharArray::new(B, None, LBCELL..);
let mut C = DummyCharArrayMut::new(C, None, LBCELL..);
let mut APOINT: i32 = 0;
let mut BPOINT: i32 = 0;
let mut CSIZE: i32 = 0;
let mut ACARD: i32 = 0;
let mut BCARD: i32 = 0;
let mut CCARD: i32 = 0;
let mut OVER: i32 = 0;
//
// SPICELIB functions
//
//
// Local variables
//
//
// Set up the error processing.
//
if RETURN(ctx) {
return Ok(());
}
CHKIN(b"UNIONC", ctx)?;
//
// Make sure output set elements are long enough.
//
if (intrinsics::LEN(&C[LBCELL])
< intrinsics::MAX0(&[intrinsics::LEN(&A[LBCELL]), intrinsics::LEN(&B[LBCELL])]))
{
SETMSG(
b"Length of output cell is #. Length required to contain result is #.",
ctx,
);
ERRINT(b"#", intrinsics::LEN(&C[LBCELL]), ctx);
ERRINT(
b"#",
intrinsics::MAX0(&[intrinsics::LEN(&A[LBCELL]), intrinsics::LEN(&B[LBCELL])]),
ctx,
);
SIGERR(b"SPICE(ELEMENTSTOOSHORT)", ctx)?;
CHKOUT(b"UNIONC", ctx)?;
return Ok(());
}
//
// Find the cardinality of the input sets, and the allowed size
// of the output set.
//
ACARD = CARDC(A.as_arg(), ctx)?;
BCARD = CARDC(B.as_arg(), ctx)?;
CSIZE = SIZEC(C.as_arg(), ctx)?;
//
// Begin with the input pointers at the first elements of the
// input sets. The cardinality of the output set is zero.
// And there is no overflow so far.
//
APOINT = 1;
BPOINT = 1;
CCARD = 0;
OVER = 0;
//
// When the ends of both input sets are reached, we're done.
//
while ((APOINT <= ACARD) || (BPOINT <= BCARD)) {
//
// If there is still space in the output set, fill it
// as necessary.
//
if (CCARD < CSIZE) {
if (APOINT > ACARD) {
CCARD = (CCARD + 1);
fstr::assign(C.get_mut(CCARD), B.get(BPOINT));
BPOINT = (BPOINT + 1);
} else if (BPOINT > BCARD) {
CCARD = (CCARD + 1);
fstr::assign(C.get_mut(CCARD), A.get(APOINT));
APOINT = (APOINT + 1);
} else if fstr::eq(A.get(APOINT), B.get(BPOINT)) {
CCARD = (CCARD + 1);
fstr::assign(C.get_mut(CCARD), A.get(APOINT));
APOINT = (APOINT + 1);
BPOINT = (BPOINT + 1);
} else if fstr::lt(&A[APOINT], &B[BPOINT]) {
CCARD = (CCARD + 1);
fstr::assign(C.get_mut(CCARD), A.get(APOINT));
APOINT = (APOINT + 1);
} else if fstr::gt(&A[APOINT], &B[BPOINT]) {
CCARD = (CCARD + 1);
fstr::assign(C.get_mut(CCARD), B.get(BPOINT));
BPOINT = (BPOINT + 1);
}
//
// Otherwise, stop filling the array, but continue to count the
// number of elements in excess of the size of the output set.
//
} else {
if (APOINT > ACARD) {
OVER = (OVER + 1);
BPOINT = (BPOINT + 1);
} else if (BPOINT > BCARD) {
OVER = (OVER + 1);
APOINT = (APOINT + 1);
} else if fstr::eq(A.get(APOINT), B.get(BPOINT)) {
OVER = (OVER + 1);
APOINT = (APOINT + 1);
BPOINT = (BPOINT + 1);
} else if fstr::lt(&A[APOINT], &B[BPOINT]) {
OVER = (OVER + 1);
APOINT = (APOINT + 1);
} else if fstr::gt(&A[APOINT], &B[BPOINT]) {
OVER = (OVER + 1);
BPOINT = (BPOINT + 1);
}
}
}
//
// Set the cardinality of the output set.
//
SCARDC(CCARD, C.as_arg_mut(), ctx)?;
//
// Report any excess.
//
if (OVER > 0) {
EXCESS(OVER, b"set", ctx)?;
SIGERR(b"SPICE(SETEXCESS)", ctx)?;
}
CHKOUT(b"UNIONC", ctx)?;
Ok(())
}