2 Copyright © 1995-2012, The AROS Development Team. All rights reserved.
5 C99 function bsearch().
8 /*****************************************************************************
21 int (* comparefunction
)(const void *, const void *))
24 Search in a sorted array for an entry key.
27 key - Look for this key.
28 base - This is the address of the first element in the array
29 to be searched. Note that the array *must* be sorted.
30 count - The number of elements in the array
31 size - The size of one element
32 comparefunction - The function which is called when two elements
33 must be compared. The function gets the addresses of two
34 elements of the array and must return 0 is both are equal,
35 < 0 if the first element is less than the second and > 0
39 A pointer to the element which equals key in the array or NULL if
40 no such element could be found.
52 ******************************************************************************/
54 char * base2
= (char *)base
;
60 /* Any elements to search ? */
65 /* Find the middle element between a and b */
68 /* Look if key is equal to this element */
69 if ((d
= (*comparefunction
)(key
, &base2
[size
* c
])) == 0)
70 return &base2
[size
* c
];
73 If the middle element equals the lower seach bounds, then
74 there are no more elements in the array which could be
75 searched (c wouldn't change anymore).
81 The middle element is not equal to the key. Is it smaller
82 or larger than the key ? If it's smaller, then c is our
83 new lower bounds, otherwise c is our new upper bounds.