Mapper
bs_sindx.c
Go to the documentation of this file.
1 /*****************************************************************************\
2 * bs_sindx *
3 *******************************************************************************
4 * Bs_sindx() performs a binary search on a sorted array of sindx structures. *
5 * The sorted array of num structures is pointed to by sdx. Bs_sindx() tries *
6 * to match id with a sdx.sid member in the array of sindx structures. It *
7 * returns the index of the element in the sdx array whose sdx.sid member *
8 * matches id. If no match is found, NOT_USED is returned. *
9 * Wayne Martin - CNRFC 09/21/94 *
10 \*****************************************************************************/
11 
12 #include <stdio.h>
13 
14 #include "database.h"
15 #include "dbsearch.h"
16 
17 #define NO 0x00
18 #define YES 0x01
19 
20 unsigned long int bs_sindx(struct sindx *sdx, unsigned long int id,
21  unsigned long int num) {
22 
23  unsigned char found; /* Search flag; found = NO or YES */
24  unsigned long int lower, upper; /* Lower and upper bounds of current array */
25  unsigned long int i; /* Current array position to be tested */
26  unsigned long int match; /* Return value */
27 
28  lower = 0x00000000;
29  upper = num - 0x00000001;
30  match = NOT_USED;
31  found = NO;
32 
33  /* return NOT_USED immediately if no ID's in list to be searched */
34  if(num < 1L) {
35  return(match);
36  }
37 
38  while(found == NO) {
39 
40  /* Find midpoint of remaining sdx array */
41  i = (upper + lower) / 2L;
42 
43  /* Found sdx.sid match for id */
44  if(sdx[i].sid == id) {
45  match = i;
46  found = YES;
47  }
48  else {
49  /* Take upper half of sdx array */
50  if(id > sdx[i].sid) {
51  lower = i + 1L;
52  }
53  /* Take lower half of sdx array */
54  else {
55  upper = i - 1L;
56  }
57  }
58  /* id not in sdx array */
59  if(upper < lower) {
60  found = YES;
61  }
62  if(upper == NOT_USED) {
63  found = YES;
64  }
65  }
66  return(match);
67 }
unsigned long int bs_sindx(struct sindx *sdx, unsigned long int id, unsigned long int num)
Definition: bs_sindx.c:20
#define YES
Definition: bs_sindx.c:18
#define NO
Definition: bs_sindx.c:17
static int i
#define NOT_USED
Definition: database.h:110
struct sindx * sdx
Definition: mapper.c:122