* Switch from linear search to binary ditto.

* Symbol names is "const char *" but symbol values are "void *" (not
  "const char *").
This commit is contained in:
bg- 2007-01-12 13:55:04 +00:00
parent c42988b78b
commit f8d7943735
3 changed files with 21 additions and 27 deletions

View File

@ -28,14 +28,14 @@
*
* This file is part of the Contiki operating system.
*
* @(#)$Id: symbols-def.h,v 1.2 2006/12/20 14:36:28 bg- Exp $
* @(#)$Id: symbols-def.h,v 1.3 2007/01/12 13:55:04 bg- Exp $
*/
#ifndef __SYMBOLS_DEF_H__
#define __SYMBOLS_DEF_H__
struct symbols {
const char *name;
const char *value;
void *value;
};
extern const int symbols_nelts;

View File

@ -28,7 +28,7 @@
*
* This file is part of the Contiki operating system.
*
* @(#)$Id: symtab.c,v 1.5 2006/12/20 14:41:28 bg- Exp $
* @(#)$Id: symtab.c,v 1.6 2007/01/12 13:55:05 bg- Exp $
*/
#include "symtab.h"
@ -37,44 +37,38 @@
#include <string.h>
#define SYMTAB_CONF_BINARY_SEARCH 0
/* Binary search is twice as large but still small. */
#ifndef SYMTAB_CONF_BINARY_SEARCH
#define SYMTAB_CONF_BINARY_SEARCH 1
#endif
/*---------------------------------------------------------------------------*/
#if SYMTAB_CONF_BINARY_SEARCH
const char *
void *
symtab_lookup(const char *name)
{
int start, middle, end;
int r;
start = 0;
end = symbols_nelts;
end = symbols_nelts - 1; /* Last entry is { 0, 0 }. */
do {
while(start <= end) {
/* Check middle, divide */
middle = (end + start) / 2;
if(symbols[middle].name == NULL) {
return NULL;
}
middle = (start + end) / 2;
r = strcmp(name, symbols[middle].name);
if(r == 0) {
if(r < 0) {
end = middle - 1;
} else if(r > 0) {
start = middle + 1;
} else {
return symbols[middle].value;
}
if(end == middle || start == middle) {
return NULL;
}
if(r < 0) {
end = middle;
}
if(r > 0) {
start = middle;
}
} while(1);
}
return NULL;
}
#else /* SYMTAB_CONF_BINARY_SEARCH */
const char *
void *
symtab_lookup(const char *name)
{
const struct symbols *s;

View File

@ -28,11 +28,11 @@
*
* This file is part of the Contiki operating system.
*
* @(#)$Id: symtab.h,v 1.3 2006/12/19 09:30:57 adamdunkels Exp $
* @(#)$Id: symtab.h,v 1.4 2007/01/12 13:55:05 bg- Exp $
*/
#ifndef __SYMTAB_H__
#define __SYMTAB_H__
const char *symtab_lookup(const char *name);
void *symtab_lookup(const char *name);
#endif /* __SYMTAB_H__ */