Added optional binary search
This commit is contained in:
parent
419c5f3c17
commit
946e11a0fc
1 changed files with 62 additions and 25 deletions
|
@ -28,7 +28,7 @@
|
|||
*
|
||||
* This file is part of the Contiki operating system.
|
||||
*
|
||||
* @(#)$Id: symtab.c,v 1.3 2006/12/18 15:27:18 fros4943 Exp $
|
||||
* @(#)$Id: symtab.c,v 1.4 2006/12/19 09:31:37 adamdunkels Exp $
|
||||
*/
|
||||
|
||||
#include "symtab.h"
|
||||
|
@ -37,8 +37,44 @@
|
|||
|
||||
#include <string.h>
|
||||
|
||||
#define SYMTAB_CONF_BINARY_SEARCH 0
|
||||
|
||||
/*---------------------------------------------------------------------------*/
|
||||
void *
|
||||
#if SYMTAB_CONF_BINARY_SEARCH
|
||||
const char *
|
||||
symtab_lookup(const char *name)
|
||||
{
|
||||
int start, middle, end;
|
||||
int r;
|
||||
|
||||
start = 0;
|
||||
end = sizeof(symbols) / sizeof(struct symbols);
|
||||
|
||||
do {
|
||||
/* Check middle, divide */
|
||||
middle = (end + start) / 2;
|
||||
|
||||
if(symbols[middle].name == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
r = strcmp(name, symbols[middle].name);
|
||||
if(r == 0) {
|
||||
return symbols[middle].value;
|
||||
}
|
||||
if(end == middle || start == middle) {
|
||||
return NULL;
|
||||
}
|
||||
if(r < 0) {
|
||||
end = middle;
|
||||
}
|
||||
if(r > 0) {
|
||||
start = middle;
|
||||
}
|
||||
} while(1);
|
||||
|
||||
}
|
||||
#else /* SYMTAB_CONF_BINARY_SEARCH */
|
||||
const char *
|
||||
symtab_lookup(const char *name)
|
||||
{
|
||||
const struct symbols *s;
|
||||
|
@ -49,4 +85,5 @@ symtab_lookup(const char *name)
|
|||
}
|
||||
return 0;
|
||||
}
|
||||
#endif /* SYMTAB_CONF_BINARY_SEARCH */
|
||||
/*---------------------------------------------------------------------------*/
|
||||
|
|
Loading…
Reference in a new issue