Debian Codegraph @

src/common/bfind.c

raw symbols SCIP
1
2 /*
3 * Copyright 2009 Red Hat Inc., Durham, North Carolina.
4 * All Rights Reserved.
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 *
20 * Authors:
21 * Lukas Kuklinek <[email protected]>
22 */
23
24 #ifdef HAVE_CONFIG_H
25 #include <config.h>
26 #endif
27
28 #include <stddef.h>
29 #ifdef OS_WINDOWS
30 #include <io.h>
31 #else
32 #include <unistd.h>
33 #endif
34 #include <stdint.h>
35 #include <assert.h>
36
37 #include "bfind.h"
38
39 void *oscap_bfind(void *ptr, size_t nmemb, size_t size, void *key, int cmpfn(void *, void *))
40 {
41 size_t w, s;
42 int cmp;
43 uint8_t *p;
44
45 w = nmemb;
46 s = 0;
47 p = (uint8_t *) ptr;
48
49 while (w > 0) {
50 cmp = cmpfn(key, p + (size * (s + w / 2)));
51 if (cmp > 0) {
52 s += w / 2 + 1;
53 w = w - w / 2 - 1;
54 } else if (cmp < 0) {
55 w = w / 2;
56 } else {
57 return ((void *)(p + (size * (s + w / 2))));
58 }
59 }
60
61 return (NULL);
62 }
63
64 int32_t oscap_bfind_i(void *ptr, size_t nmemb, size_t size, void *key, int cmpfn(void *, void *), size_t *save)
65 {
66 size_t w, s;
67 int cmp;
68 uint8_t *p;
69
70 w = nmemb;
71 s = 0;
72 p = (uint8_t *) ptr;
73
74 while (w > 0) {
75 cmp = cmpfn(key, p + (size * (s + w / 2)));
76 if (cmp > 0) {
77 s += w / 2 + 1;
78 w = w - w / 2 - 1;
79 } else if (cmp < 0) {
80 w = w / 2;
81 } else {
82 assert((s + w / 2) <= INT32_MAX);
83 return ((int32_t) (s + w / 2));
84 }
85 }
86
87 if (save != NULL)
88 *save = s;
89
90 return INT32_C(-1);
91 }