lookup.cpp
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #include "lookup.h"
00025
00026 #include <kdebug.h>
00027
00028 #include <stdio.h>
00029 #include <string.h>
00030
00031 const HashEntry* Lookup::findEntry( const struct HashTable *table,
00032 const QChar *c, unsigned int len )
00033 {
00034 if (table->type != 2) {
00035 kdDebug() << "KJS: Unknown hash table version" << endl;
00036 return 0;
00037 }
00038 char *ascii = new char[len+1];
00039 unsigned int i;
00040 for(i = 0; i < len; i++, c++) {
00041 if (!c->row())
00042 ascii[i] = c->cell();
00043 else
00044 break;
00045 }
00046 ascii[i] = '\0';
00047
00048 int h = hash(ascii) % table->hashSize;
00049 const HashEntry *e = &table->entries[h];
00050
00051
00052 if (!e->s) {
00053 delete [] ascii;
00054 return 0;
00055 }
00056
00057 do {
00058
00059 if (strcmp(ascii, e->s) == 0) {
00060 delete [] ascii;
00061 return e;
00062 }
00063
00064 e = e->next;
00065 } while (e);
00066
00067 delete [] ascii;
00068 return 0;
00069 }
00070
00071 const HashEntry* Lookup::findEntry( const struct HashTable *table,
00072 const QString &s )
00073 {
00074 return findEntry( table, s.unicode(), s.length() );
00075 }
00076
00077 int Lookup::find(const struct HashTable *table,
00078 const QChar *c, unsigned int len)
00079 {
00080 const HashEntry *entry = findEntry( table, c, len );
00081 if (entry)
00082 return entry->value;
00083 return -1;
00084 }
00085
00086 int Lookup::find(const struct HashTable *table, const QString &s)
00087 {
00088 return find(table, s.unicode(), s.length());
00089 }
00090
00091 unsigned int Lookup::hash(const QChar *c, unsigned int len)
00092 {
00093 unsigned int val = 0;
00094
00095 for (unsigned int i = 0; i < len; i++, c++)
00096 val += c->cell();
00097
00098 return val;
00099 }
00100
00101 unsigned int Lookup::hash(const QString &key)
00102 {
00103 return hash(key.unicode(), key.length());
00104 }
00105
00106 unsigned int Lookup::hash(const char *s)
00107 {
00108 unsigned int val = 0;
00109 while (*s)
00110 val += *s++;
00111
00112 return val;
00113 }
This file is part of the documentation for umbrello Version 3.1.0.