forked from github/server
Speeding up the hashing in findtoken even more.
This commit is contained in:
parent
0fe3e9ad4b
commit
6c6c1174d8
2 changed files with 14 additions and 28 deletions
|
@ -36,21 +36,6 @@ typedef struct tref {
|
||||||
#define LEAF 1 /* leaf node for a word. always matches */
|
#define LEAF 1 /* leaf node for a word. always matches */
|
||||||
#define SHARED 2 /* at least two words share the node */
|
#define SHARED 2 /* at least two words share the node */
|
||||||
|
|
||||||
#if NODEHASHSIZE == 7
|
|
||||||
/* lookup table, making c % 7 faster for chars. is this sick or what? */
|
|
||||||
static int divc7[256] = {
|
|
||||||
0,1,2,3,4,5,6,0,1,2,3,4,5,6,0,1,2,3,4,5,6,0,1,2,3,4,5,6,
|
|
||||||
0,1,2,3,4,5,6,0,1,2,3,4,5,6,0,1,2,3,4,5,6,0,1,2,3,4,5,6,
|
|
||||||
0,1,2,3,4,5,6,0,1,2,3,4,5,6,0,1,2,3,4,5,6,0,1,2,3,4,5,6,
|
|
||||||
0,1,2,3,4,5,6,0,1,2,3,4,5,6,0,1,2,3,4,5,6,0,1,2,3,4,5,6,
|
|
||||||
0,1,2,3,4,5,6,0,1,2,3,4,5,6,0,1,2,3,4,5,6,0,1,2,3,4,5,6,
|
|
||||||
0,1,2,3,4,5,6,0,1,2,3,4,5,6,0,1,2,3,4,5,6,0,1,2,3,4,5,6,
|
|
||||||
0,1,2,3,4,5,6,0,1,2,3,4,5,6,0,1,2,3,4,5,6,0,1,2,3,4,5,6,
|
|
||||||
0,1,2,3,4,5,6,0,1,2,3,4,5,6,0,1,2,3,4,5,6,0,1,2,3,4,5,6,
|
|
||||||
0,1,2,3,4,5,6,0,1,2,3,4,5,6,0,1,2,3,4,5,6,0,1,2,3,4,5,6,
|
|
||||||
0,1,2,3 };
|
|
||||||
#endif
|
|
||||||
|
|
||||||
void
|
void
|
||||||
addtoken(tnode * root, const char* str, variant id)
|
addtoken(tnode * root, const char* str, variant id)
|
||||||
{
|
{
|
||||||
|
@ -73,10 +58,10 @@ addtoken(tnode * root, const char* str, variant id)
|
||||||
} else {
|
} else {
|
||||||
tref * next;
|
tref * next;
|
||||||
int index, i = 0;
|
int index, i = 0;
|
||||||
char c = *str;
|
register char c = *str;
|
||||||
if (c<'a' || c>'z') c = (char)tolower((unsigned char)c);
|
if (c<'a' || c>'z') c = (char)tolower((unsigned char)c);
|
||||||
#if NODEHASHSIZE == 7
|
#if NODEHASHSIZE == 8
|
||||||
index = divc7[(unsigned char)c];
|
index = c & 7;
|
||||||
#else
|
#else
|
||||||
index = ((unsigned char)c) % NODEHASHSIZE;
|
index = ((unsigned char)c) % NODEHASHSIZE;
|
||||||
#endif
|
#endif
|
||||||
|
@ -95,7 +80,11 @@ addtoken(tnode * root, const char* str, variant id)
|
||||||
root->next[index] = ref;
|
root->next[index] = ref;
|
||||||
|
|
||||||
if (u!=c) {
|
if (u!=c) {
|
||||||
index = ((unsigned char)u) % NODEHASHSIZE;
|
#if NODEHASHSIZE == 8
|
||||||
|
index = u & 7;
|
||||||
|
#else
|
||||||
|
index = ((unsigned char)u) % NODEHASHSIZE;
|
||||||
|
#endif
|
||||||
ref = malloc(sizeof(tref));
|
ref = malloc(sizeof(tref));
|
||||||
ref->c = u;
|
ref->c = u;
|
||||||
ref->node = node;
|
ref->node = node;
|
||||||
|
@ -123,18 +112,15 @@ addtoken(tnode * root, const char* str, variant id)
|
||||||
int
|
int
|
||||||
findtoken(const tnode * tk, const char * str, variant* result)
|
findtoken(const tnode * tk, const char * str, variant* result)
|
||||||
{
|
{
|
||||||
if (!str) return E_TOK_NOMATCH;
|
if (!str || *str==0) return E_TOK_NOMATCH;
|
||||||
if (*str == 0) return E_TOK_NOMATCH;
|
|
||||||
|
|
||||||
while (*str) {
|
do {
|
||||||
int index;
|
int index;
|
||||||
const tref * ref;
|
const tref * ref;
|
||||||
char c = *str;
|
char c = *str;
|
||||||
|
|
||||||
/* if (c<'a' || c>'z') c = (char)tolower((unsigned char)c); */
|
#if NODEHASHSIZE == 8
|
||||||
|
index = c & 7;
|
||||||
#if NODEHASHSIZE == 7
|
|
||||||
index = divc7[(unsigned char)c];
|
|
||||||
#else
|
#else
|
||||||
index = ((unsigned char)c) % NODEHASHSIZE;
|
index = ((unsigned char)c) % NODEHASHSIZE;
|
||||||
#endif
|
#endif
|
||||||
|
@ -143,7 +129,7 @@ findtoken(const tnode * tk, const char * str, variant* result)
|
||||||
++str;
|
++str;
|
||||||
if (!ref) return E_TOK_NOMATCH;
|
if (!ref) return E_TOK_NOMATCH;
|
||||||
tk = ref->node;
|
tk = ref->node;
|
||||||
}
|
} while (*str);
|
||||||
if (tk) {
|
if (tk) {
|
||||||
*result = tk->id;
|
*result = tk->id;
|
||||||
return E_TOK_SUCCESS;
|
return E_TOK_SUCCESS;
|
||||||
|
|
|
@ -23,7 +23,7 @@ extern "C" {
|
||||||
|
|
||||||
#define E_TOK_NOMATCH (-1)
|
#define E_TOK_NOMATCH (-1)
|
||||||
#define E_TOK_SUCCESS 0
|
#define E_TOK_SUCCESS 0
|
||||||
#define NODEHASHSIZE 7
|
#define NODEHASHSIZE 8
|
||||||
struct tref;
|
struct tref;
|
||||||
|
|
||||||
typedef struct tnode {
|
typedef struct tnode {
|
||||||
|
|
Loading…
Reference in a new issue