/
bsearch.h
58 lines (47 loc) · 1.28 KB
/
bsearch.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
/* bsearch.h -- binary searching arrays */
#ifndef UTILS_BSEARCH_H
#define UTILS_BSEARCH_H
#ifdef __cplusplus
extern "C"
{
#endif
#include <stddef.h>
/* Binary search an array 'nelems' long for 'want'.
* Each array element is 'stride' bytes wide. */
/* Example:
*
* To search an array, ordered by key, composed of the following structures:
*
* struct foo
* {
* int key;
* int value;
* }
* foo_t;
*
* foo_t foos[100];
*
* index = bsearch_int(&foos[0].key, 100, sizeof(foo_t), 42);
*
* 'index' is the index of the element, or < 0 if not found.
*/
int bsearch_short(const short *array,
unsigned int nelems,
size_t stride,
short want);
int bsearch_ushort(const unsigned short *array,
unsigned int nelems,
size_t stride,
unsigned short want);
int bsearch_int(const int *array,
unsigned int nelems,
size_t stride,
int want);
int bsearch_uint(const unsigned int *array,
unsigned int nelems,
size_t stride,
unsigned int want);
#ifdef __cplusplus
}
#endif
#endif /* UTILS_BSEARCH_H */