1 /* diffi.f -- translated by f2c (version 19980913).
2    You must link the resulting object file with the libraries:
3 	-lf2c -lm   (in that order)
4 */
5 
6 #include "f2c.h"
7 
8 /* $Procedure      DIFFI ( Difference of two integer sets ) */
diffi_(integer * a,integer * b,integer * c__)9 /* Subroutine */ int diffi_(integer *a, integer *b, integer *c__)
10 {
11     integer over, acard, bcard, ccard;
12     extern integer cardi_(integer *);
13     extern /* Subroutine */ int chkin_(char *, ftnlen);
14     integer csize;
15     extern integer sizei_(integer *);
16     extern /* Subroutine */ int scardi_(integer *, integer *);
17     integer apoint, bpoint;
18     extern /* Subroutine */ int excess_(integer *, char *, ftnlen), sigerr_(
19 	    char *, ftnlen), chkout_(char *, ftnlen);
20     extern logical return_(void);
21 
22 /* $ Abstract */
23 
24 /*      Take the difference of two integer sets to form a third set. */
25 
26 /* $ Disclaimer */
27 
28 /*     THIS SOFTWARE AND ANY RELATED MATERIALS WERE CREATED BY THE */
29 /*     CALIFORNIA INSTITUTE OF TECHNOLOGY (CALTECH) UNDER A U.S. */
30 /*     GOVERNMENT CONTRACT WITH THE NATIONAL AERONAUTICS AND SPACE */
31 /*     ADMINISTRATION (NASA). THE SOFTWARE IS TECHNOLOGY AND SOFTWARE */
32 /*     PUBLICLY AVAILABLE UNDER U.S. EXPORT LAWS AND IS PROVIDED "AS-IS" */
33 /*     TO THE RECIPIENT WITHOUT WARRANTY OF ANY KIND, INCLUDING ANY */
34 /*     WARRANTIES OF PERFORMANCE OR MERCHANTABILITY OR FITNESS FOR A */
35 /*     PARTICULAR USE OR PURPOSE (AS SET FORTH IN UNITED STATES UCC */
36 /*     SECTIONS 2312-2313) OR FOR ANY PURPOSE WHATSOEVER, FOR THE */
37 /*     SOFTWARE AND RELATED MATERIALS, HOWEVER USED. */
38 
39 /*     IN NO EVENT SHALL CALTECH, ITS JET PROPULSION LABORATORY, OR NASA */
40 /*     BE LIABLE FOR ANY DAMAGES AND/OR COSTS, INCLUDING, BUT NOT */
41 /*     LIMITED TO, INCIDENTAL OR CONSEQUENTIAL DAMAGES OF ANY KIND, */
42 /*     INCLUDING ECONOMIC DAMAGE OR INJURY TO PROPERTY AND LOST PROFITS, */
43 /*     REGARDLESS OF WHETHER CALTECH, JPL, OR NASA BE ADVISED, HAVE */
44 /*     REASON TO KNOW, OR, IN FACT, SHALL KNOW OF THE POSSIBILITY. */
45 
46 /*     RECIPIENT BEARS ALL RISK RELATING TO QUALITY AND PERFORMANCE OF */
47 /*     THE SOFTWARE AND ANY RELATED MATERIALS, AND AGREES TO INDEMNIFY */
48 /*     CALTECH AND NASA FOR ALL THIRD-PARTY CLAIMS RESULTING FROM THE */
49 /*     ACTIONS OF RECIPIENT IN THE USE OF THE SOFTWARE. */
50 
51 /* $ Required_Reading */
52 
53 /*     None. */
54 
55 /* $ Keywords */
56 
57 /*      CELLS, SETS */
58 
59 /* $ Declarations */
60 /* $ Brief_I/O */
61 
62 /*      VARIABLE  I/O  DESCRIPTION */
63 /*      --------  ---  -------------------------------------------------- */
64 /*      A          I   First input set. */
65 /*      B          I   Second input set. */
66 /*      C          O   Difference of A and B. */
67 
68 /* $ Detailed_Input */
69 
70 
71 /*      A           is a set. */
72 
73 
74 /*      B           is a set, distinct from A. */
75 
76 /* $ Detailed_Output */
77 
78 /*      C           is a set, distinct from sets A and B, which */
79 /*                  contains the difference of A and B (that is, */
80 /*                  all of the elements which are in A, but NOT */
81 /*                  in B). */
82 
83 /*                  If the size (maximum cardinality) of C is smaller */
84 /*                  than the cardinality of the difference of A and B, */
85 /*                  then only as many items as will fit in C are */
86 /*                  included, and an error is returned. */
87 
88 /* $ Parameters */
89 
90 /*      None. */
91 
92 /* $ Particulars */
93 
94 /*      None. */
95 
96 /* $ Examples */
97 
98 /*      The DIFFERENCE of two sets contains every element which is */
99 /*      in the first set, but NOT in the second. */
100 
101 /*            {a,b}      difference  {c,d}     =  {a,b} */
102 /*            {a,b,c}                {b,c,d}      {a} */
103 /*            {a,b,c,d}              {}           {a,b,c,d} */
104 /*            {}                     {a,b,c,d}    {} */
105 /*            {}                     {}           {} */
106 
107 /*      The following call */
108 
109 /*            CALL DIFFC  ( PLANETS, ASTEROIDS, RESULT ) */
110 
111 /*      places the difference of the character sets PLANETS and */
112 /*      ASTEROIDS into the character set RESULT. */
113 
114 /*      The output set must be distinct from both of the input sets. */
115 /*      For example, the following calls are invalid. */
116 
117 /*            CALL DIFFI  ( CURRENT,     NEW, CURRENT ) */
118 /*            CALL DIFFI  (     NEW, CURRENT, CURRENT ) */
119 
120 /*      In each of the examples above, whether or not the subroutine */
121 /*      signals an error, the results will almost certainly be wrong. */
122 /*      Nearly the same effect can be achieved, however, by placing the */
123 /*      result into a temporary set, which is immediately copied back */
124 /*      into one of the input sets, as shown below. */
125 
126 /*            CALL DIFFI  ( CURRENT, NEW,  TEMP ) */
127 /*            CALL COPYI  ( TEMP,    NEW        ) */
128 
129 /* $ Restrictions */
130 
131 /*      None. */
132 
133 /* $ Exceptions */
134 
135 /*     1) If the difference of the two sets causes an excess of */
136 /*        elements, the error SPICE(SETEXCESS) is signalled. */
137 
138 /* $ Files */
139 
140 /*      None. */
141 
142 /* $ Literature_References */
143 
144 /*      None. */
145 
146 /* $ Author_and_Institution */
147 
148 /*      N.J. Bachman    (JPL) */
149 /*      C.A. Curzon     (JPL) */
150 /*      W.L. Taber      (JPL) */
151 /*      I.M. Underwood  (JPL) */
152 
153 /* $ Version */
154 
155 /* -    SPICELIB Version 1.0.1, 10-MAR-1992 (WLT) */
156 
157 /*        Comment section for permuted index source lines was added */
158 /*        following the header. */
159 
160 /* -    SPICELIB Version 1.0.0, 31-JAN-1990 (CAC) (WLT) (IMU) */
161 
162 /* -& */
163 /* $ Index_Entries */
164 
165 /*     difference of two integer sets */
166 
167 /* -& */
168 /* $ Revisions */
169 
170 /* -    Beta Version 1.1.0, 06-JAN-1989 (NJB) */
171 
172 /*        Calling protocol of EXCESS changed.  Call to SETMSG removed. */
173 
174 /*        Also, in the overflow case, the number of excess elements was */
175 /*        computed incorrectly; this has been fixed.  The problem was */
176 /*        that OVER was incremented in all cases of the overflow IF */
177 /*        block, rather than only in the cases where the cardinality of */
178 /*        the output cell would have been incremented if there were */
179 /*        enough room. */
180 
181 /* -& */
182 
183 /*     SPICELIB functions */
184 
185 
186 /*     Local variables */
187 
188 
189 /*     Set up the error processing. */
190 
191     if (return_()) {
192 	return 0;
193     }
194     chkin_("DIFFI", (ftnlen)5);
195 
196 /*     Find the cardinality of the input sets, and the allowed size */
197 /*     of the output set. */
198 
199     acard = cardi_(a);
200     bcard = cardi_(b);
201     csize = sizei_(c__);
202 
203 /*     Begin with the input pointers at the first elements of the */
204 /*     input sets. The cardinality of the output set is zero. */
205 /*     And there is no overflow so far. */
206 
207     apoint = 1;
208     bpoint = 1;
209     ccard = 0;
210     over = 0;
211 
212 /*     When the end of the first input set is reached, we're done. */
213 
214     while(apoint <= acard) {
215 
216 /*        If there is still space in the output set, fill it */
217 /*        as necessary. */
218 
219 	if (ccard < csize) {
220 	    if (bpoint > bcard) {
221 		++ccard;
222 		c__[ccard + 5] = a[apoint + 5];
223 		++apoint;
224 	    } else if (a[apoint + 5] == b[bpoint + 5]) {
225 		++apoint;
226 		++bpoint;
227 	    } else if (a[apoint + 5] < b[bpoint + 5]) {
228 		++ccard;
229 		c__[ccard + 5] = a[apoint + 5];
230 		++apoint;
231 	    } else if (b[bpoint + 5] < a[apoint + 5]) {
232 		++bpoint;
233 	    }
234 
235 /*        Otherwise, stop folling the array, but continue to count the */
236 /*        number of elements in excess of the size of the output set. */
237 
238 	} else {
239 	    if (bpoint > bcard) {
240 		++over;
241 		++apoint;
242 	    } else if (a[apoint + 5] == b[bpoint + 5]) {
243 		++apoint;
244 		++bpoint;
245 	    } else if (a[apoint + 5] < b[bpoint + 5]) {
246 		++over;
247 		++apoint;
248 	    } else if (b[bpoint + 5] < a[apoint + 5]) {
249 		++bpoint;
250 	    }
251 	}
252     }
253 
254 /*     Set the cardinality of the output set. */
255 
256     scardi_(&ccard, c__);
257 
258 /*     Report any excess. */
259 
260     if (over > 0) {
261 	excess_(&over, "set", (ftnlen)3);
262 	sigerr_("SPICE(SETEXCESS)", (ftnlen)16);
263     }
264     chkout_("DIFFI", (ftnlen)5);
265     return 0;
266 } /* diffi_ */
267 
268