Debian Codegraph @

terps/git/search.c

raw symbols SCIP
1 // $Id: search.c,v 1.1 2003/10/18 20:06:41 iain Exp $
2
3 // search.c: Glulxe code for built-in search opcodes
4 // Designed by Andrew Plotkin <[email protected]>
5 // http://www.eblong.com/zarf/glulx/index.html
6
7 #include "glk.h"
8 #include "git.h"
9 #include "opcodes.h"
10
11 #ifndef TRUE
12 #define TRUE 1
13 #endif
14 #ifndef FALSE
15 #define FALSE 0
16 #endif
17
18 #define serop_KeyIndirect (0x01)
19 #define serop_ZeroKeyTerminates (0x02)
20 #define serop_ReturnIndex (0x04)
21 /* ### KeyZeroBounded? variants? */
22 /* ### LowerBoundKey? */
23
24 /* In general, these search functions look through a bunch of structures
25 in memory, searching for one whose key (a fixed-size sequence of bytes
26 within the structure) matches a given key. The result can indicate a
27 particular structure within the bunch, or it can be NULL ("not found".)
28
29 Any or all of these options can be applied:
30
31 KeyIndirect: If this is true, the key argument is taken to be the
32 start of an array of bytes in memory (whose length is keysize).
33 If it is false, the key argument contains the key itself. In
34 this case, keysize *must* be 1, 2, or 4. The key is stored in the
35 lower bytes of the key argument, big-endian. (The upper bytes are
36 ignored.)
37
38 ZeroKeyTerminates: If this is true, when the search reaches a struct
39 whose key is all zeroes, the search terminates (and returns NULL).
40 If the searched-for key happens to also be zeroes, the key-match
41 (returning the struct) takes precedence over the zero-match (returning
42 NULL.)
43
44 ReturnIndex: If this is false, the return value is the memory address
45 of the matching struct, or 0 to indicate NULL. If true, the return value
46 is the array index of the matching struct, or -1 to indicate NULL.
47 */
48
49 static void fetchkey(unsigned char *keybuf, glui32 key, glui32 keysize,
50 glui32 options);
51
52 /* linear_search():
53 An array of data structures is stored in memory, beginning at start,
54 each structure being structsize bytes. Within each struct, there is
55 a key value keysize bytes long, starting at position keyoffset (from
56 the start of the structure.) Search through these in order. If one
57 is found whose key matches, return it. If numstructs are searched
58 with no result, return NULL.
59
60 numstructs may be -1 (0xFFFFFFFF) to indicate no upper limit to the
61 number of structures to search. The search will continue until a match
62 is found, or (if ZeroKeyTerminates is set) a zero key.
63
64 The KeyIndirect, ZeroKeyTerminates, and ReturnIndex options may be
65 used.
66 */
67 glui32 git_linear_search(glui32 key, glui32 keysize,
68 glui32 start, glui32 structsize, glui32 numstructs,
69 glui32 keyoffset, glui32 options)
70 {
71 unsigned char keybuf[4];
72 glui32 count;
73 int ix;
74 int retindex = ((options & serop_ReturnIndex) != 0);
75 int zeroterm = ((options & serop_ZeroKeyTerminates) != 0);
76
77 fetchkey(keybuf, key, keysize, options);
78
79 for (count=0; count<numstructs; count++, start+=structsize) {
80 int match = TRUE;
81 if (keysize <= 4) {
82 for (ix=0; match && ix<keysize; ix++) {
83 if (memRead8(start + keyoffset + ix) != keybuf[ix])
84 match = FALSE;
85 }
86 }
87 else {
88 for (ix=0; match && ix<keysize; ix++) {
89 if (memRead8(start + keyoffset + ix) != memRead8(key + ix))
90 match = FALSE;
91 }
92 }
93
94 if (match) {
95 if (retindex)
96 return count;
97 else
98 return start;
99 }
100
101 if (zeroterm) {
102 match = TRUE;
103 for (ix=0; match && ix<keysize; ix++) {
104 if (memRead8(start + keyoffset + ix) != 0)
105 match = FALSE;
106 }
107 if (match) {
108 break;
109 }
110 }
111 }
112
113 if (retindex)
114 return -1;
115 else
116 return 0;
117 }
118
119 /* binary_search():
120 An array of data structures is in memory, as above. However, the
121 structs must be stored in forward order of their keys (taking each key
122 to be a multibyte unsigned integer.) There can be no duplicate keys.
123 numstructs must indicate the exact length of the array; it cannot
124 be -1.
125
126 The KeyIndirect and ReturnIndex options may be used.
127 */
128
129 static glui32 binary_search_generic(glui32 key, glui32 keysize,
130 glui32 start, glui32 structsize, glui32 numstructs,
131 glui32 keyoffset, glui32 options)
132 {
133 unsigned char keybuf[4];
134 unsigned char byte, byte2;
135 glui32 top, bot, val, addr;
136 int ix;
137 int retindex = ((options & serop_ReturnIndex) != 0);
138
139 fetchkey(keybuf, key, keysize, options);
140
141 bot = 0;
142 top = numstructs;
143 while (bot < top) {
144 int cmp = 0;
145 val = (top+bot) / 2;
146 addr = start + val * structsize;
147
148 if (keysize <= 4) {
149 for (ix=0; (!cmp) && ix<keysize; ix++) {
150 byte = memRead8(addr + keyoffset + ix);
151 byte2 = keybuf[ix];
152 if (byte < byte2)
153 cmp = -1;
154 else if (byte > byte2)
155 cmp = 1;
156 }
157 }
158 else {
159 for (ix=0; (!cmp) && ix<keysize; ix++) {
160 byte = memRead8(addr + keyoffset + ix);
161 byte2 = memRead8(key + ix);
162 if (byte < byte2)
163 cmp = -1;
164 else if (byte > byte2)
165 cmp = 1;
166 }
167 }
168
169 if (!cmp) {
170 if (retindex)
171 return val;
172 else
173 return addr;
174 }
175
176 if (cmp < 0) {
177 bot = val+1;
178 }
179 else {
180 top = val;
181 }
182 }
183
184 if (retindex)
185 return -1;
186 else
187 return 0;
188 }
189
190 static glui32 binary_search_16bit(git_uint16 key, glui32 start,
191 glui32 structsize, glui32 numstructs,
192 glui32 keyoffset, glui32 options)
193 {
194 glui32 addr, top, bot, val;
195 git_uint16 m;
196 int retindex = ((options & serop_ReturnIndex) != 0);
197
198 bot = 0;
199 top = numstructs;
200 while (bot < top) {
201 val = (top+bot) / 2;
202 addr = start + val * structsize;
203
204 m = memRead16(addr + keyoffset);
205 if (m == key) {
206 if (retindex)
207 return val;
208 else
209 return addr;
210 } else if (m < key) {
211 bot = val + 1;
212 } else {
213 top = val;
214 }
215 }
216
217 if (retindex)
218 return -1;
219 else
220 return 0;
221 }
222
223 static glui32 binary_search_32bit(git_uint32 key, glui32 start,
224 glui32 structsize, glui32 numstructs,
225 glui32 keyoffset, glui32 options)
226 {
227 glui32 addr, top, bot, val, m;
228 int retindex = ((options & serop_ReturnIndex) != 0);
229
230 bot = 0;
231 top = numstructs;
232 while (bot < top) {
233 val = (top+bot) / 2;
234 addr = start + val * structsize;
235
236 m = memRead32(addr + keyoffset);
237 if (m == key) {
238 if (retindex)
239 return val;
240 else
241 return addr;
242 } else if (m < key) {
243 bot = val + 1;
244 } else {
245 top = val;
246 }
247 }
248
249 if (retindex)
250 return -1;
251 else
252 return 0;
253 }
254
255 glui32 git_binary_search(glui32 key, glui32 keysize,
256 glui32 start, glui32 structsize, glui32 numstructs,
257 glui32 keyoffset, glui32 options)
258 {
259 if (keysize == 2) {
260 git_uint16 key16;
261 if ((options & serop_KeyIndirect) != 0) {
262 key16 = memRead16(key);
263 } else {
264 key16 = key;
265 }
266 return binary_search_16bit(key16, start, structsize, numstructs, keyoffset, options);
267 }
268 if (keysize == 4) {
269 git_uint32 key32;
270 if ((options & serop_KeyIndirect) != 0) {
271 key32 = memRead32(key);
272 } else {
273 key32 = key;
274 }
275 return binary_search_32bit(key32, start, structsize, numstructs, keyoffset, options);
276 }
277 return binary_search_generic(key, keysize, start, structsize, numstructs, keyoffset, options);
278 }
279
280 /* linked_search():
281 The structures may be anywhere in memory, in any order. They are
282 linked by a four-byte address field, which is found in each struct
283 at position nextoffset. If this field contains zero, it indicates
284 the end of the linked list.
285
286 The KeyIndirect and ZeroKeyTerminates options may be used.
287 */
288 glui32 git_linked_search(glui32 key, glui32 keysize,
289 glui32 start, glui32 keyoffset, glui32 nextoffset, glui32 options)
290 {
291 unsigned char keybuf[4];
292 int ix;
293 glui32 val;
294 int zeroterm = ((options & serop_ZeroKeyTerminates) != 0);
295
296 fetchkey(keybuf, key, keysize, options);
297
298 while (start != 0) {
299 int match = TRUE;
300 if (keysize <= 4) {
301 for (ix=0; match && ix<keysize; ix++) {
302 if (memRead8(start + keyoffset + ix) != keybuf[ix])
303 match = FALSE;
304 }
305 }
306 else {
307 for (ix=0; match && ix<keysize; ix++) {
308 if (memRead8(start + keyoffset + ix) != memRead8(key + ix))
309 match = FALSE;
310 }
311 }
312
313 if (match) {
314 return start;
315 }
316
317 if (zeroterm) {
318 match = TRUE;
319 for (ix=0; match && ix<keysize; ix++) {
320 if (memRead8(start + keyoffset + ix) != 0)
321 match = FALSE;
322 }
323 if (match) {
324 break;
325 }
326 }
327
328 val = start + nextoffset;
329 start = memRead32(val);
330 }
331
332 return 0;
333 }
334
335 /* fetchkey():
336 This massages the key into a form that's easier to handle. When it
337 returns, the key will be stored in keybuf if keysize <= 4; otherwise,
338 it will be in memory.
339 */
340 static void fetchkey(unsigned char *keybuf, glui32 key, glui32 keysize,
341 glui32 options)
342 {
343 int ix;
344
345 if (options & serop_KeyIndirect) {
346 if (keysize <= 4) {
347 for (ix=0; ix<keysize; ix++)
348 keybuf[ix] = memRead8(key+ix);
349 }
350 }
351 else {
352 switch (keysize) {
353 case 4:
354 write32(keybuf, key);
355 break;
356 case 2:
357 write16(keybuf, key);
358 break;
359 case 1:
360 write8(keybuf, key);
361 break;
362 default:
363 fatalError("Direct search key must hold one, two, or four bytes.");
364 }
365 }
366 }