-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathn_array_bsearch_ex.c
110 lines (82 loc) · 2.1 KB
/
n_array_bsearch_ex.c
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include "n_array_int.h"
static __inline__
int bsearch_voidp_arr(void *const *arr, size_t arr_size, const void *data,
t_fn_cmp cmpf)
{
register size_t l, r, i;
int cmp_res;
l = 0;
r = arr_size;
while (l < r) {
i = (l + r) / 2;
if ((cmp_res = cmpf(arr[i], data)) == 0) {
return i;
} else if (cmp_res > 0) {
r = i;
} else if (cmp_res < 0) {
l = i + 1;
}
}
return -1;
}
int n_array_bsearch_idx_ex(const tn_array *arr, const void *data,
t_fn_cmp cmpf)
{
int idx;
void **base;
if (cmpf == NULL) {
if (arr->cmp_fn == NULL) {
trurl_die("n_array_bsearch_ex: compare function is NULL\n");
return -1;
}
if (arr->flags & TN_ARRAY_AUTOSORTED)
n_array_sort_ex((tn_array*)arr, NULL);
cmpf = arr->cmp_fn;
}
if(arr->items == 0)
return -1;
base = &arr->data[arr->start_index];
if (arr->items > 1) {
idx = bsearch_voidp_arr(base, arr->items, data, cmpf);
if (idx > 0) {
while (idx) {
if (cmpf(base[idx - 1], data) != 0)
break;
idx--;
}
}
} else {
idx = -1;
if (cmpf(base[0], data) == 0)
idx = 0;
}
return idx;
}
void *n_array_bsearch_ex(const tn_array *arr, const void *data, t_fn_cmp cmpf)
{
int idx;
void **base;
if (cmpf == NULL) {
if (arr->cmp_fn == NULL) {
trurl_die("n_array_bsearch_ex: compare function is NULL\n");
return NULL;
}
if (arr->flags & TN_ARRAY_AUTOSORTED)
n_array_sort_ex((tn_array*)arr, NULL);
cmpf = arr->cmp_fn;
}
if(arr->items == 0)
return NULL;
base = &arr->data[arr->start_index];
if (arr->items > 1) {
idx = bsearch_voidp_arr(base, arr->items, data, cmpf);
} else {
idx = -1;
if (cmpf(base[0], data) == 0)
idx = 0;
}
return idx < 0 ? NULL : base[idx];
}