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
//
// GENERATED FILE
//
use super::*;
use crate::SpiceContext;
use f2rust_std::*;
pub const LBCELL: i32 = -5;
/// Union two DP windows
///
/// Place the union of two double precision windows into a third
/// window.
///
/// # Required Reading
///
/// * [WINDOWS](crate::required_reading::windows)
///
/// # Brief I/O
///
/// ```text
/// VARIABLE I/O DESCRIPTION
/// -------- --- --------------------------------------------------
/// A,
/// B I Input windows.
/// C O Union of A and B.
/// ```
///
/// # Detailed Input
///
/// ```text
/// A,
/// B are SPICE windows, each of which contains zero or more
/// intervals.
/// ```
///
/// # Detailed Output
///
/// ```text
/// C is the output SPICE window, containing the union of A
/// and B --- every point contained in A, or in B, or in
/// both.
///
/// C must be distinct from both A and B.
/// ```
///
/// # Exceptions
///
/// ```text
/// 1) If the union of the two windows results in an excess of
/// elements, the error SPICE(WINDOWEXCESS) is signaled.
///
/// 2) The cardinality of the input windows must be even. Left
/// endpoints of stored intervals must be strictly greater than
/// preceding right endpoints. Right endpoints must be greater
/// than or equal to corresponding left endpoints. Invalid window
/// data are not diagnosed by this routine and may lead to
/// unpredictable results.
/// ```
///
/// # Particulars
///
/// ```text
/// The union of two windows contains every point contained in the
/// first window, or the second window, or both.
/// ```
///
/// # Examples
///
/// ```text
/// Let A contain the intervals
///
/// [ 1, 3 ] [ 7, 11 ] [ 23, 27 ]
///
/// and B contain the intervals
///
/// [ 2, 6 ] [ 8, 10 ] [ 16, 18 ]
///
/// Then the union of A and B contains the intervals
///
/// [ 1, 6 ] [ 7, 11 ] [ 16, 18 ] [ 23, 27 ]
/// ```
///
/// # 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.2.0, 24-AUG-2021 (JDR) (NJB)
///
/// Added IMPLICIT NONE statement.
///
/// Edited the header to comply with NAIF standard. Fixed I/O type
/// of argument C in $Brief_I/O table.
///
/// Added entry #2 in $Exceptions section. Removed unnecessary
/// entries in $Revisions section.
///
/// - SPICELIB Version 1.1.0, 08-FEB-1999 (WLT)
///
/// The variable END was not initialized in the previous
/// edition. It is now initialized to be the minimum of
/// A(1) and B(1).
///
/// - 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) (HAN)
/// ```
///
/// # Revisions
///
/// ```text
/// - Beta Version 1.1.0, 27-FEB-1989 (HAN)
///
/// Due to the calling sequence and functionality changes
/// in the routine EXCESS, the method of signaling an
/// excess of elements needed to be changed.
/// ```
pub fn wnunid(ctx: &mut SpiceContext, a: &[f64], b: &[f64], c: &mut [f64]) -> crate::Result<()> {
WNUNID(a, b, c, ctx.raw_context())?;
ctx.handle_errors()?;
Ok(())
}
//$Procedure WNUNID ( Union two DP windows )
pub fn WNUNID(A: &[f64], B: &[f64], C: &mut [f64], ctx: &mut Context) -> f2rust_std::Result<()> {
let A = DummyArray::new(A, LBCELL..);
let B = DummyArray::new(B, LBCELL..);
let mut C = DummyArrayMut::new(C, LBCELL..);
let mut ACARD: i32 = 0;
let mut BCARD: i32 = 0;
let mut CSIZE: i32 = 0;
let mut AP: i32 = 0;
let mut BP: i32 = 0;
let mut CP: i32 = 0;
let mut END: f64 = 0.0;
let mut USE = [b' '; 1];
let mut OVER: i32 = 0;
//
// SPICELIB functions
//
//
// Local variables
//
//
// Standard SPICE error handling.
//
if RETURN(ctx) {
return Ok(());
} else {
CHKIN(b"WNUNID", ctx)?;
}
//
// Find the cardinality of the input windows, and the allowed size
// of the output window.
//
ACARD = CARDD(A.as_slice(), ctx)?;
BCARD = CARDD(B.as_slice(), ctx)?;
CSIZE = SIZED(C.as_slice(), ctx)?;
//
// Begin with the input pointers at the first elements of the
// input windows. The initial cardinality of the output window
// is zero. And there is no overflow so far.
//
// (Note that AP and BP point to the LEFT endpoints of intervals
// in A and B, while CP points to the RIGHT endpoint of the latest
// interval in C.)
//
AP = 1;
BP = 1;
CP = 0;
END = intrinsics::DMIN1(&[A[1], B[1]]);
OVER = 0;
//
// When the ends of both input windows are reached, we're done.
//
while ((AP < ACARD) || (BP < BCARD)) {
//
// If the end of one window has been reached, copy (or merge)
// the next interval from the other window.
//
if (AP > ACARD) {
fstr::assign(&mut USE, b"B");
} else if (BP > BCARD) {
fstr::assign(&mut USE, b"A");
//
// Otherwise, let's see what we can do with the earlier of
// the next intervals from A and B.
//
} else if (A[AP] < B[BP]) {
fstr::assign(&mut USE, b"A");
} else if (B[BP] <= A[AP]) {
fstr::assign(&mut USE, b"B");
}
//
// If there is still space in the output window, fill it
// as necessary. Otherwise, stop filling the array, but continue
// to count the number of elements in excess of the size of the
// output window.
//
// The general idea is this: if the next interval overlaps the
// latest output interval, merge the two (extending the output
// interval to the right). Otherwise, insert the next interval
// intact.
//
if fstr::eq(&USE, b"A") {
if (CP < CSIZE) {
if ((A[AP] <= END) && (CP > 0)) {
C[CP] = intrinsics::DMAX1(&[C[CP], A[(AP + 1)]]);
} else {
CP = (CP + 2);
C[(CP - 1)] = A[AP];
C[CP] = A[(AP + 1)];
}
END = C[CP];
} else {
if (A[AP] <= END) {
END = intrinsics::DMAX1(&[END, A[(AP + 1)]]);
} else {
OVER = (OVER + 2);
END = A[(AP + 1)];
}
}
AP = (AP + 2);
//
// This is the same as the last clause, with B replacing A.
//
} else if fstr::eq(&USE, b"B") {
if (CP < CSIZE) {
if ((B[BP] <= END) && (CP > 0)) {
C[CP] = intrinsics::DMAX1(&[C[CP], B[(BP + 1)]]);
} else {
CP = (CP + 2);
C[(CP - 1)] = B[BP];
C[CP] = B[(BP + 1)];
}
END = C[CP];
} else {
if (B[BP] <= END) {
END = intrinsics::DMAX1(&[END, B[(BP + 1)]]);
} else {
OVER = (OVER + 2);
END = B[(BP + 1)];
}
}
BP = (BP + 2);
}
}
//
// Set the cardinality of the output window.
//
SCARDD(CP, C.as_slice_mut(), ctx)?;
//
// If there is an excess of elements, signal an error and check out
// as usual.
//
if (OVER > 0) {
EXCESS(OVER, b"window", ctx)?;
SIGERR(b"SPICE(WINDOWEXCESS)", ctx)?;
}
CHKOUT(b"WNUNID", ctx)?;
Ok(())
}