1 /* bschoc.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 BSCHOC ( Binary search with order vector, character ) */
bschoc_(char * value,integer * ndim,char * array,integer * order,ftnlen value_len,ftnlen array_len)9 integer bschoc_(char *value, integer *ndim, char *array, integer *order,
10 ftnlen value_len, ftnlen array_len)
11 {
12 /* System generated locals */
13 integer ret_val;
14
15 /* Builtin functions */
16 integer s_cmp(char *, char *, ftnlen, ftnlen);
17 logical l_lt(char *, char *, ftnlen, ftnlen);
18
19 /* Local variables */
20 integer left, i__, right;
21
22 /* $ Abstract */
23
24 /* Do a binary search for a given value within a character array, */
25 /* accompanied by an order vector. Return the index of the */
26 /* matching array entry, or zero if the key value is not found. */
27
28 /* $ Disclaimer */
29
30 /* THIS SOFTWARE AND ANY RELATED MATERIALS WERE CREATED BY THE */
31 /* CALIFORNIA INSTITUTE OF TECHNOLOGY (CALTECH) UNDER A U.S. */
32 /* GOVERNMENT CONTRACT WITH THE NATIONAL AERONAUTICS AND SPACE */
33 /* ADMINISTRATION (NASA). THE SOFTWARE IS TECHNOLOGY AND SOFTWARE */
34 /* PUBLICLY AVAILABLE UNDER U.S. EXPORT LAWS AND IS PROVIDED "AS-IS" */
35 /* TO THE RECIPIENT WITHOUT WARRANTY OF ANY KIND, INCLUDING ANY */
36 /* WARRANTIES OF PERFORMANCE OR MERCHANTABILITY OR FITNESS FOR A */
37 /* PARTICULAR USE OR PURPOSE (AS SET FORTH IN UNITED STATES UCC */
38 /* SECTIONS 2312-2313) OR FOR ANY PURPOSE WHATSOEVER, FOR THE */
39 /* SOFTWARE AND RELATED MATERIALS, HOWEVER USED. */
40
41 /* IN NO EVENT SHALL CALTECH, ITS JET PROPULSION LABORATORY, OR NASA */
42 /* BE LIABLE FOR ANY DAMAGES AND/OR COSTS, INCLUDING, BUT NOT */
43 /* LIMITED TO, INCIDENTAL OR CONSEQUENTIAL DAMAGES OF ANY KIND, */
44 /* INCLUDING ECONOMIC DAMAGE OR INJURY TO PROPERTY AND LOST PROFITS, */
45 /* REGARDLESS OF WHETHER CALTECH, JPL, OR NASA BE ADVISED, HAVE */
46 /* REASON TO KNOW, OR, IN FACT, SHALL KNOW OF THE POSSIBILITY. */
47
48 /* RECIPIENT BEARS ALL RISK RELATING TO QUALITY AND PERFORMANCE OF */
49 /* THE SOFTWARE AND ANY RELATED MATERIALS, AND AGREES TO INDEMNIFY */
50 /* CALTECH AND NASA FOR ALL THIRD-PARTY CLAIMS RESULTING FROM THE */
51 /* ACTIONS OF RECIPIENT IN THE USE OF THE SOFTWARE. */
52
53 /* $ Required_Reading */
54
55 /* None. */
56
57 /* $ Keywords */
58
59 /* ARRAY, SEARCH */
60
61 /* $ Declarations */
62 /* $ Brief_I/O */
63
64 /* VARIABLE I/O DESCRIPTION */
65 /* -------- --- -------------------------------------------------- */
66 /* VALUE I Value to find in ARRAY. */
67 /* NDIM I Dimension of ARRAY. */
68 /* ARRAY I Array to be searched. */
69 /* ORDER I Order vector. */
70 /* BSCHOC O Index of VALUE in ARRAY. (Zero if not found.) */
71
72 /* $ Detailed_Input */
73
74 /* VALUE is the value to be found in the input array. */
75
76 /* NDIM is the number of elements in the input array. */
77
78 /* ARRAY is the array to be searched. */
79
80
81 /* ORDER is an order array that can be used to access */
82 /* the elements of ARRAY in order (according to the */
83 /* ASCII collating sequence). */
84
85 /* $ Detailed_Output */
86
87 /* BSCHOC is the index of the input value in the input array. */
88 /* If ARRAY does not contain VALUE, BSCHOC is zero. */
89
90 /* If ARRAY contains more than one occurrence of VALUE, */
91 /* BSCHOC may point to any of the occurrences. */
92
93 /* $ Parameters */
94
95 /* None. */
96
97 /* $ Files */
98
99 /* None. */
100
101 /* $ Exceptions */
102
103 /* Error free. */
104
105 /* If NDIM < 1 the value of the function is zero. */
106
107 /* $ Particulars */
108
109 /* A binary search is implemented on the input array, whose order */
110 /* is given by an associated order vector. If an element of the */
111 /* array is found to match the input value, the index of that */
112 /* element is returned. If no matching element is found, zero is */
113 /* returned. */
114
115 /* $ Examples */
116
117 /* Let ARRAY and ORDER contain the following elements: */
118
119 /* ARRAY ORDER */
120 /* ----------- ----- */
121 /* 'FEYNMAN' 2 */
122 /* 'BOHR' 3 */
123 /* 'EINSTEIN' 1 */
124 /* 'NEWTON' 5 */
125 /* 'GALILEO' 4 */
126
127 /* Then */
128
129 /* BSCHOC ( 'NEWTON', 5, ARRAY, ORDER ) = 4 */
130 /* BSCHOC ( 'EINSTEIN', 5, ARRAY, ORDER ) = 3 */
131 /* BSCHOC ( 'GALILEO', 5, ARRAY, ORDER ) = 5 */
132 /* BSCHOC ( 'Galileo', 5, ARRAY, ORDER ) = 0 */
133 /* BSCHOC ( 'BETHE', 5, ARRAY, ORDER ) = 0 */
134
135 /* That is */
136
137 /* ARRAY(4) = 'NEWTON' */
138 /* ARRAY(3) = 'EINSTEIN' */
139 /* ARRAY(5) = 'GALILEO' */
140
141 /* (Compare with BSCHOC_2.) */
142
143 /* $ Restrictions */
144
145 /* ORDER is assumed to give the order of the elements of ARRAY */
146 /* in increasing order according to the ASCII collating sequence. */
147 /* If this condition is not met, the results of BSCHOC are */
148 /* unpredictable. */
149
150 /* $ Author_and_Institution */
151
152 /* I. M. Underwood */
153 /* W. L. Taber */
154
155 /* $ Literature_References */
156
157 /* None. */
158
159 /* $ Version */
160
161 /* - SPICELIB Version 1.0.0, 18-SEP-1995 (IMU) (WLT) */
162
163 /* -& */
164 /* $ Index_Entries */
165
166 /* binary search for a string using an order vector */
167
168 /* -& */
169
170 /* Local variables */
171
172
173 /* Set the initial bounds for the search area. */
174
175 left = 1;
176 right = *ndim;
177 while(left <= right) {
178
179 /* Check the middle element. */
180
181 i__ = (left + right) / 2;
182
183 /* If the middle element matches, return its location. */
184
185 if (s_cmp(value, array + (order[i__ - 1] - 1) * array_len, value_len,
186 array_len) == 0) {
187 ret_val = order[i__ - 1];
188 return ret_val;
189
190 /* Otherwise narrow the search area. */
191
192 } else if (l_lt(value, array + (order[i__ - 1] - 1) * array_len,
193 value_len, array_len)) {
194 right = i__ - 1;
195 } else {
196 left = i__ + 1;
197 }
198 }
199
200 /* If the search area is empty, return zero. */
201
202 ret_val = 0;
203 return ret_val;
204 } /* bschoc_ */
205
206