PandA-2024.02
bsearch.c
Go to the documentation of this file.
1 /*
2  * bsearch.c
3  * Original Author: G. Haley
4  * Rewritten by: G. Noer
5  *
6  * Searches an array of nmemb members, the initial member of which is pointed
7  * to by base, for a member that matches the object pointed to by key. The
8  * contents of the array shall be in ascending order according to a comparison
9  * function pointed to by compar. The function shall return an integer less
10  * than, equal to or greater than zero if the first argument is considered to be
11  * respectively less than, equal to or greater than the second. Returns a
12  * pointer to the matching member of the array, or a null pointer if no match
13  * is found.
14  */
15 
16 /*
17 FUNCTION
18 <<bsearch>>---binary search
19 
20 INDEX
21  bsearch
22 
23 ANSI_SYNOPSIS
24  #include <stdlib.h>
25  void *bsearch(const void *<[key]>, const void *<[base]>,
26  size_t <[nmemb]>, size_t <[size]>,
27  int (*<[compar]>)(const void *, const void *));
28 
29 TRAD_SYNOPSIS
30  #include <stdlib.h>
31  char *bsearch(<[key]>, <[base]>, <[nmemb]>, <[size]>, <[compar]>)
32  char *<[key]>;
33  char *<[base]>;
34  size_t <[nmemb]>, <[size]>;
35  int (*<[compar]>)();
36 
37 DESCRIPTION
38 <<bsearch>> searches an array beginning at <[base]> for any element
39 that matches <[key]>, using binary search. <[nmemb]> is the element
40 count of the array; <[size]> is the size of each element.
41 
42 The array must be sorted in ascending order with respect to the
43 comparison function <[compar]> (which you supply as the last argument of
44 <<bsearch>>).
45 
46 You must define the comparison function <<(*<[compar]>)>> to have two
47 arguments; its result must be negative if the first argument is
48 less than the second, zero if the two arguments match, and
49 positive if the first argument is greater than the second (where
50 ``less than'' and ``greater than'' refer to whatever arbitrary
51 ordering is appropriate).
52 
53 RETURNS
54 Returns a pointer to an element of <[array]> that matches <[key]>. If
55 more than one matching element is available, the result may point to
56 any of them.
57 
58 PORTABILITY
59 <<bsearch>> is ANSI.
60 
61 No supporting OS subroutines are required.
62 */
63 typedef unsigned int size_t;
64 
65 #define _AND ,
66 #define _DEFUN(name, arglist, args) name(args)
67 #define _PARAMS(paramlist) paramlist
68 #define _PTR void *
69 #define _EXFNPTR(name, proto) (* name) proto
70 #define _CONST const
71 #define NULL 0
72 
73 _PTR
74 _DEFUN (bsearch, (key, base, nmemb, size, compar),
77  size_t nmemb _AND
78  size_t size _AND
79  int _EXFNPTR(compar, (const _PTR, const _PTR)))
80 {
81  _PTR current;
82  size_t lower = 0;
83  size_t upper = nmemb;
84  size_t index;
85  int result;
86 
87  if (nmemb == 0 || size == 0)
88  return NULL;
89 
90  while (lower < upper)
91  {
92  index = (lower + upper) / 2;
93  current = (_PTR) (((char *) base) + (index * size));
94 
95  result = compar (key, current);
96 
97  if (result < 0)
98  upper = index;
99  else if (result > 0)
100  lower = index + 1;
101  else
102  return current;
103  }
104 
105  return NULL;
106 }
107 
#define NULL
Definition: bsearch.c:71
#define _CONST
Definition: bsearch.c:70
#define _EXFNPTR(name, proto)
Definition: bsearch.c:69
char base
This version is stamped on May 10, 2016.
Definition: nussinov.c:24
void * bsearch(const void *__key, const void *__base, size_t __nmemb, size_t __size, int(*__compar)(const void *, const void *))
Definition: bsearch.c:23
int key[32]
Definition: aes.h:67
#define _AND
Definition: bsearch.c:65
#define _PTR
Definition: bsearch.c:68
#define index(x, y)
Definition: Keccak.c:74
#define _DEFUN(name, arglist, args)
Definition: bsearch.c:66
int result[SIZE]
Definition: adpcm.c:800
int compar(const void *v_lhs, const void *v_rhs)
Definition: generate.c:16
unsigned int size_t
Definition: bsearch.c:19

Generated on Mon Feb 12 2024 13:02:49 for PandA-2024.02 by doxygen 1.8.13