Last change
on this file since 2156 was 2156, checked in by sandervl, 26 years ago |
Added bsearch from EMX and corrected it's calling convention
|
File size:
777 bytes
|
Line | |
---|
1 | /* $Id: bsearch.c,v 1.1 1999-12-20 11:09:41 sandervl Exp $ */
|
---|
2 | /* bsearch.c (emx+gcc) -- Copyright (c) 1990-1995 by Eberhard Mattes */
|
---|
3 |
|
---|
4 | #include <odin.h>
|
---|
5 | #include <builtin.h>
|
---|
6 |
|
---|
7 | void *CDECL bsearch (const void *key, const void *base, size_t num, size_t width,
|
---|
8 | int (* CDECL compare)(const void *key, const void *element))
|
---|
9 | {
|
---|
10 | int left, right, median, sign;
|
---|
11 | const void *element;
|
---|
12 |
|
---|
13 | if (width == 0)
|
---|
14 | return 0;
|
---|
15 | left = 1; right = num;
|
---|
16 | while (left <= right)
|
---|
17 | {
|
---|
18 | median = (left + right) / 2;
|
---|
19 | element = (void *)((char *)base + (median-1)*width);
|
---|
20 | sign = compare (key, element);
|
---|
21 | if (sign == 0)
|
---|
22 | return (void *)element;
|
---|
23 | if (sign > 0)
|
---|
24 | left = median + 1;
|
---|
25 | else
|
---|
26 | right = median - 1;
|
---|
27 | }
|
---|
28 | return 0;
|
---|
29 | }
|
---|
Note:
See
TracBrowser
for help on using the repository browser.