lookup.cpp00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #include <stdio.h>
00024 #include <string.h>
00025
00026 #include "lookup.h"
00027
00028 #ifdef HAVE_CONFIG_H
00029 #include <config.h>
00030 #endif
00031
00032 using namespace KJS;
00033
00034 static bool keysMatch(const UChar *c, unsigned len, const char *s)
00035 {
00036 for (unsigned i = 0; i != len; i++, c++, s++)
00037 if (c->uc != (unsigned char)*s)
00038 return false;
00039 return *s == 0;
00040 }
00041
00042 const HashEntry* Lookup::findEntry( const struct HashTable *table,
00043 const UChar *c, unsigned int len )
00044 {
00045 #ifndef NDEBUG
00046 if (table->type != 2) {
00047 fprintf(stderr, "KJS: Unknown hash table version.\n");
00048 return 0;
00049 }
00050 #endif
00051
00052 int h = hash(c, len) % table->hashSize;
00053 const HashEntry *e = &table->entries[h];
00054
00055
00056 if (!e->soffset)
00057 return 0;
00058
00059 while(1) {
00060
00061 if (keysMatch(c, len, &table->sbase[e->soffset]))
00062 return e;
00063
00064 if(e->next < 0) break;
00065
00066 e = &table->entries[e->next];
00067 }
00068
00069 return 0;
00070 }
00071
00072 const HashEntry* Lookup::findEntry( const struct HashTable *table,
00073 const Identifier &s )
00074 {
00075 return findEntry( table, s.data(), s.size() );
00076 }
00077
00078 int Lookup::find(const struct HashTable *table,
00079 const UChar *c, unsigned int len)
00080 {
00081 const HashEntry *entry = findEntry( table, c, len );
00082 if (entry)
00083 return entry->value;
00084 return -1;
00085 }
00086
00087 int Lookup::find(const struct HashTable *table, const Identifier &s)
00088 {
00089 return find(table, s.data(), s.size());
00090 }
00091
00092 unsigned int Lookup::hash(const UChar *c, unsigned int len)
00093 {
00094 unsigned int val = 0;
00095
00096 for (unsigned int i = 0; i < len; i++, c++)
00097 val += c->low();
00098
00099 return val;
00100 }
00101
00102 unsigned int Lookup::hash(const Identifier &key)
00103 {
00104 return hash(key.data(), key.size());
00105 }
00106
00107 unsigned int Lookup::hash(const char *s)
00108 {
00109 unsigned int val = 0;
00110 while (*s)
00111 val += *s++;
00112
00113 return val;
00114 }
|