KDevelop API Documentation

lookup.cpp

Go to the documentation of this file.
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  */
00021 
00022 // adapted to kdevelop by Roberto Raggi <roberto@kdevelop.org>
00023 
00024 #include <stdio.h>
00025 #include <string.h>
00026 
00027 #include "lookup.h"
00028 
00029 const HashEntry* Lookup::findEntry( const struct HashTable *table,
00030                               const QChar *c, unsigned int len )
00031 {
00032   if (table->type != 2) {
00033     fprintf(stderr, "KJS: Unknown hash table version.\n");
00034     return 0;
00035   }
00036   char *ascii = new char[len+1];
00037   unsigned int i;
00038   for(i = 0; i < len; i++, c++) {
00039     if (!c->row())
00040       ascii[i] = c->cell();
00041     else
00042       break;
00043   }
00044   ascii[i] = '\0';
00045 
00046   int h = hash(ascii) % table->hashSize;
00047   const HashEntry *e = &table->entries[h];
00048 
00049   // empty bucket ?
00050   if (!e->s) {
00051     delete [] ascii;
00052     return 0;
00053   }
00054 
00055   do {
00056     // compare strings
00057     if (strcmp(ascii, e->s) == 0) {
00058       delete [] ascii;
00059       return e;
00060     }
00061     // try next bucket
00062     e = e->next;
00063   } while (e);
00064 
00065   delete [] ascii;
00066   return 0;
00067 }
00068 
00069 const HashEntry* Lookup::findEntry( const struct HashTable *table,
00070                     const QString &s )
00071 {
00072     return findEntry( table, s.unicode(), s.length() );
00073 }
00074 
00075 int Lookup::find(const struct HashTable *table,
00076          const QChar *c, unsigned int len)
00077 {
00078   const HashEntry *entry = findEntry( table, c, len );
00079   if (entry)
00080     return entry->value;
00081   return -1;
00082 }
00083 
00084 int Lookup::find(const struct HashTable *table, const QString &s)
00085 {
00086   return find(table, s.unicode(), s.length());
00087 }
00088 
00089 unsigned int Lookup::hash(const QChar *c, unsigned int len)
00090 {
00091   unsigned int val = 0;
00092   // ignoring rower byte
00093   for (unsigned int i = 0; i < len; i++, c++)
00094     val += c->cell();
00095 
00096   return val;
00097 }
00098 
00099 unsigned int Lookup::hash(const QString &key)
00100 {
00101     return hash(key.unicode(), key.length());
00102 }
00103 
00104 unsigned int Lookup::hash(const char *s)
00105 {
00106   unsigned int val = 0;
00107   while (*s)
00108     val += *s++;
00109 
00110   return val;
00111 }
KDE Logo
This file is part of the documentation for KDevelop Version 3.1.2.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Wed Mar 23 00:03:51 2005 by doxygen 1.3.9.1 written by Dimitri van Heesch, © 1997-2003