| # kjs Library API Documentation
Overview | Class Hierarchy | Classes | Classes (annotated) | Members | Source Files

# 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  *
    00006  *  This library is free software; you can redistribute it and/or
    00007  *  modify it under the terms of the GNU Lesser General Public
    00008  *  License as published by the Free Software Foundation; either
    00009  *  version 2 of the License, or (at your option) any later version.
    00010  *
    00011  *  This library is distributed in the hope that it will be useful,
    00012  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
    00013  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    00014  *  Lesser General Public License for more details.
    00015  *
    00016  *  You should have received a copy of the GNU Lesser General Public
    00017  *  License along with this library; if not, write to the Free Software
    00018  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    00019  *
    00020  *  $Id: lookup.cpp,v 1.11 2002/06/19 08:21:00 domi Exp $
    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 const HashEntry* Lookup::findEntry( const struct HashTable *table,
    00035                               const UChar *c, unsigned int len )
    00036 {
    00037   if (table->type != 2) {
    00038     fprintf(stderr, "KJS: Unknown hash table version.\n");
    00039     return 0;
    00040   }
    00041   char *ascii = new char[len+1];
    00042   unsigned int i;
    00043   for(i = 0; i < len; i++, c++) {
    00044     if (!c->high())
    00045       ascii[i] = c->low();
    00046     else
    00047       break;
    00048   }
    00049   ascii[i] = '\0';
    00050
    00051   int h = hash(ascii) % table->hashSize;
    00052   const HashEntry *e = &table->entries[h];
    00053
    00054   // empty bucket ?
    00055   if (!e->s) {
    00056     delete [] ascii;
    00057     return 0;
    00058   }
    00059
    00060   do {
    00061     // compare strings
    00062     if (strcmp(ascii, e->s) == 0) {
    00063       delete [] ascii;
    00064       return e;
    00065     }
    00066     // try next bucket
    00067     e = e->next;
    00068   } while (e);
    00069
    00070   delete [] ascii;
    00071   return 0;
    00072 }
    00073
    00074 const HashEntry* Lookup::findEntry( const struct HashTable *table,
    00075                                 const UString &s )
    00076 {
    00077   return findEntry( table, s.data(), s.size() );
    00078 }
    00079
    00080 int Lookup::find(const struct HashTable *table,
    00081                  const UChar *c, unsigned int len)
    00082 {
    00083   const HashEntry *entry = findEntry( table, c, len );
    00084   if (entry)
    00085     return entry->value;
    00086   return -1;
    00087 }
    00088
    00089 int Lookup::find(const struct HashTable *table, const UString &s)
    00090 {
    00091   return find(table, s.data(), s.size());
    00092 }
    00093
    00094 unsigned int Lookup::hash(const UChar *c, unsigned int len)
    00095 {
    00096   unsigned int val = 0;
    00097   // ignoring higher byte
    00098   for (unsigned int i = 0; i < len; i++, c++)
    00099     val += c->low();
    00100
    00101   return val;
    00102 }
    00103
    00104 unsigned int Lookup::hash(const UString &key)
    00105 {
    00106   return hash(key.data(), key.size());
    00107 }
    00108
    00109 unsigned int Lookup::hash(const char *s)
    00110 {
    00111   unsigned int val = 0;
    00112   while (*s)
    00113     val += *s++;
    00114
    00115   return val;
    00116 }

This file is part of the documentation for kdelibs Version 3.1.5.
Documentation copyright © 1996-2002 the KDE developers.
Generated on Wed Jan 28 13:08:28 2004 by doxygen 1.3.4 written by Dimitri van Heesch, © 1997-2001