1
/* Copyright (C) 2005 PrimeBase Technologies GmbH
5
* This program is free software; you can redistribute it and/or modify
6
* it under the terms of the GNU General Public License as published by
7
* the Free Software Foundation; either version 2 of the License, or
8
* (at your option) any later version.
10
* This program is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
* GNU General Public License for more details.
15
* You should have received a copy of the GNU General Public License
16
* along with this program; if not, write to the Free Software
17
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19
* 2004-01-03 Paul McCullagh
24
#include "xt_config.h"
28
#include "bsearch_xt.h"
29
#include "pthread_xt.h"
30
#include "thread_xt.h"
33
* Binary search a array of 'count' items, with byte size 'size'. This
34
* function returns a pointer to the element and the 'index'
35
* of the element if found.
37
* If not found the index of the insert point of the item
38
* is returned (0 <= index <= count).
40
* The comparison routine 'compar' may throw an exception.
41
* In this case the error details will be stored in 'thread'.
43
void *xt_bsearch(XTThreadPtr thread, const void *key, register const void *base, size_t count, size_t size, size_t *idx, const void *thunk, XTCompareFunc compar)
46
register size_t guess;
51
guess = (i + count - 1) >> 1;
52
r = (compar)(thread, thunk, key, ((char *) base) + guess * size);
55
return ((char *) base) + guess * size;