umbrello API Documentation

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  */
00021 
00022 // adapted to kdevelop by Roberto Raggi <roberto@kdevelop.org>
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   // empty bucket ?
00052   if (!e->s) {
00053     delete [] ascii;
00054     return 0;
00055   }
00056 
00057   do {
00058     // compare strings
00059     if (strcmp(ascii, e->s) == 0) {
00060       delete [] ascii;
00061       return e;
00062     }
00063     // try next bucket
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   // ignoring rower byte
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 }
KDE Logo
This file is part of the documentation for umbrello Version 3.1.0.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Tue Jun 26 08:07:58 2007 by doxygen 1.4.1 written by Dimitri van Heesch, © 1997-2003