lookup.cpp

00001 // -*- c-basic-offset: 2 -*-
00002 /*
00003  *  This file is part of the KDE libraries
00004  *  Copyright (C) 1999-2000 Harri Porten (porten@kde.org)
00005  *  Copyright (C) 2003 Apple Computer, Inc.
00006  *
00007  *  This library is free software; you can redistribute it and/or
00008  *  modify it under the terms of the GNU Lesser General Public
00009  *  License as published by the Free Software Foundation; either
00010  *  version 2 of the License, or (at your option) any later version.
00011  *
00012  *  This library is distributed in the hope that it will be useful,
00013  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  *  Lesser General Public License for more details.
00016  *
00017  *  You should have received a copy of the GNU Lesser General Public
00018  *  License along with this library; if not, write to the Free Software
00019  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
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   // empty bucket ?
00056   if (!e->soffset)
00057     return 0;
00058 
00059   while(1) {
00060     // compare strings
00061     if (keysMatch(c, len, &table->sbase[e->soffset]))
00062       return e;
00063     // try next bucket
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   // ignoring higher byte
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 }
KDE Home | KDE Accessibility Home | Description of Access Keys