1*bc328a43SAugustin Cavalier /*-
2*bc328a43SAugustin Cavalier * SPDX-License-Identifier: BSD-3-Clause
3*bc328a43SAugustin Cavalier *
45af32e75SAxel Dörfler * Copyright (c) 1990, 1993
55af32e75SAxel Dörfler * The Regents of the University of California. All rights reserved.
65af32e75SAxel Dörfler *
75af32e75SAxel Dörfler * Redistribution and use in source and binary forms, with or without
85af32e75SAxel Dörfler * modification, are permitted provided that the following conditions
95af32e75SAxel Dörfler * are met:
105af32e75SAxel Dörfler * 1. Redistributions of source code must retain the above copyright
115af32e75SAxel Dörfler * notice, this list of conditions and the following disclaimer.
125af32e75SAxel Dörfler * 2. Redistributions in binary form must reproduce the above copyright
135af32e75SAxel Dörfler * notice, this list of conditions and the following disclaimer in the
145af32e75SAxel Dörfler * documentation and/or other materials provided with the distribution.
15*bc328a43SAugustin Cavalier * 3. Neither the name of the University nor the names of its contributors
165af32e75SAxel Dörfler * may be used to endorse or promote products derived from this software
175af32e75SAxel Dörfler * without specific prior written permission.
185af32e75SAxel Dörfler *
195af32e75SAxel Dörfler * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
205af32e75SAxel Dörfler * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
215af32e75SAxel Dörfler * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
225af32e75SAxel Dörfler * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
235af32e75SAxel Dörfler * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
245af32e75SAxel Dörfler * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
255af32e75SAxel Dörfler * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
265af32e75SAxel Dörfler * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
275af32e75SAxel Dörfler * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
285af32e75SAxel Dörfler * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
295af32e75SAxel Dörfler * SUCH DAMAGE.
305af32e75SAxel Dörfler */
315af32e75SAxel Dörfler
325af32e75SAxel Dörfler #if defined(LIBC_SCCS) && !defined(lint)
335af32e75SAxel Dörfler static char sccsid[] = "@(#)bsearch.c 8.1 (Berkeley) 6/4/93";
345af32e75SAxel Dörfler #endif /* LIBC_SCCS and not lint */
35*bc328a43SAugustin Cavalier #include <stddef.h>
365af32e75SAxel Dörfler #include <stdlib.h>
375af32e75SAxel Dörfler
38*bc328a43SAugustin Cavalier #define COMPAR(x,y) compar(x, y)
39*bc328a43SAugustin Cavalier
405af32e75SAxel Dörfler /*
415af32e75SAxel Dörfler * Perform a binary search.
425af32e75SAxel Dörfler *
435af32e75SAxel Dörfler * The code below is a bit sneaky. After a comparison fails, we
445af32e75SAxel Dörfler * divide the work in half by moving either left or right. If lim
455af32e75SAxel Dörfler * is odd, moving left simply involves halving lim: e.g., when lim
465af32e75SAxel Dörfler * is 5 we look at item 2, so we change lim to 2 so that we will
475af32e75SAxel Dörfler * look at items 0 & 1. If lim is even, the same applies. If lim
48*bc328a43SAugustin Cavalier * is odd, moving right again involves halving lim, this time moving
495af32e75SAxel Dörfler * the base up one item past p: e.g., when lim is 5 we change base
505af32e75SAxel Dörfler * to item 3 and make lim 2 so that we will look at items 3 and 4.
515af32e75SAxel Dörfler * If lim is even, however, we have to shrink it by one before
525af32e75SAxel Dörfler * halving: e.g., when lim is 4, we still looked at item 2, so we
535af32e75SAxel Dörfler * have to make lim 3, then halve, obtaining 1, so that we will only
545af32e75SAxel Dörfler * look at item 3.
555af32e75SAxel Dörfler */
565af32e75SAxel Dörfler void *
bsearch(const void * key,const void * base0,size_t nmemb,size_t size,int (* compar)(const void *,const void *))57*bc328a43SAugustin Cavalier bsearch(const void *key, const void *base0, size_t nmemb, size_t size,
58*bc328a43SAugustin Cavalier int (*compar)(const void *, const void *))
595af32e75SAxel Dörfler {
60*bc328a43SAugustin Cavalier const char *base = base0;
615af32e75SAxel Dörfler size_t lim;
625af32e75SAxel Dörfler int cmp;
63*bc328a43SAugustin Cavalier const void *p;
645af32e75SAxel Dörfler
655af32e75SAxel Dörfler for (lim = nmemb; lim != 0; lim >>= 1) {
665af32e75SAxel Dörfler p = base + (lim >> 1) * size;
67*bc328a43SAugustin Cavalier cmp = COMPAR(key, p);
68*bc328a43SAugustin Cavalier if (cmp == 0)
69*bc328a43SAugustin Cavalier return ((void *)p);
705af32e75SAxel Dörfler if (cmp > 0) { /* key > p: move right */
715af32e75SAxel Dörfler base = (char *)p + size;
725af32e75SAxel Dörfler lim--;
735af32e75SAxel Dörfler } /* else move left */
745af32e75SAxel Dörfler }
75*bc328a43SAugustin Cavalier return (NULL);
765af32e75SAxel Dörfler }
77