~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to plugin/pbxt/src/bsearch_xt.cc

  • Committer: Olaf van der Spek
  • Date: 2011-02-12 18:24:24 UTC
  • mto: (2167.1.2 build) (2172.1.4 build)
  • mto: This revision was merged to the branch mainline in revision 2168.
  • Revision ID: olafvdspek@gmail.com-20110212182424-kgnm9osi7qo97at2
casts

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Copyright (C) 2005 PrimeBase Technologies GmbH
 
2
 *
 
3
 * PrimeBase XT
 
4
 *
 
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.
 
9
 *
 
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.
 
14
 *
 
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
 
18
 *
 
19
 * 2004-01-03   Paul McCullagh
 
20
 *
 
21
 * H&G2JCtL
 
22
 */
 
23
 
 
24
#include "xt_config.h"
 
25
 
 
26
#include <stdio.h>
 
27
 
 
28
#include "bsearch_xt.h"
 
29
#include "pthread_xt.h"
 
30
#include "thread_xt.h"
 
31
 
 
32
/**
 
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.
 
36
 *
 
37
 * If not found the index of the insert point of the item
 
38
 * is returned (0 <= index <= count).
 
39
 *
 
40
 * The comparison routine 'compar' may throw an exception.
 
41
 * In this case the error details will be stored in 'thread'.
 
42
 */
 
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)
 
44
{
 
45
        register size_t         i;
 
46
        register size_t         guess;
 
47
        register int            r;
 
48
 
 
49
        i = 0;
 
50
        while (i < count) {
 
51
                guess = (i + count - 1) >> 1;
 
52
                r = (compar)(thread, thunk, key, ((char *) base) + guess * size);
 
53
                if (r == 0) {
 
54
                        *idx = guess;
 
55
                        return ((char *) base) + guess * size;
 
56
                }
 
57
                if (r < 0)
 
58
                        count = guess;
 
59
                else
 
60
                        i = guess + 1;
 
61
        }
 
62
 
 
63
        *idx = i;
 
64
        return NULL;
 
65
}
 
66