1 | // -*- c-basic-offset: 2 -*-
|
---|
2 | /*
|
---|
3 | * This file is part of the KDE libraries
|
---|
4 | * Copyright (C) 1999-2000 Harri Porten ([email protected])
|
---|
5 | *
|
---|
6 | * This library is free software; you can redistribute it and/or
|
---|
7 | * modify it under the terms of the GNU Lesser General Public
|
---|
8 | * License as published by the Free Software Foundation; either
|
---|
9 | * version 2 of the License, or (at your option) any later version.
|
---|
10 | *
|
---|
11 | * This library is distributed in the hope that it will be useful,
|
---|
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
14 | * Lesser General Public License for more details.
|
---|
15 | *
|
---|
16 | * You should have received a copy of the GNU Lesser General Public
|
---|
17 | * License along with this library; if not, write to the Free Software
|
---|
18 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
---|
19 | *
|
---|
20 | */
|
---|
21 |
|
---|
22 | #include <stdio.h>
|
---|
23 | #include <string.h>
|
---|
24 |
|
---|
25 | #include "lookup.h"
|
---|
26 |
|
---|
27 | #ifdef HAVE_CONFIG_H
|
---|
28 | #include <config.h>
|
---|
29 | #endif
|
---|
30 |
|
---|
31 | using namespace KJS;
|
---|
32 |
|
---|
33 | static bool keysMatch(const UChar *c, unsigned len, const char *s)
|
---|
34 | {
|
---|
35 | for (unsigned i = 0; i != len; i++, c++, s++)
|
---|
36 | if (c->unicode() != (unsigned char)*s)
|
---|
37 | return false;
|
---|
38 | return *s == 0;
|
---|
39 | }
|
---|
40 |
|
---|
41 | const HashEntry* Lookup::findEntry( const struct HashTable *table,
|
---|
42 | const UChar *c, unsigned int len )
|
---|
43 | {
|
---|
44 | #ifndef NDEBUG
|
---|
45 | if (table->type != 2) {
|
---|
46 | fprintf(stderr, "KJS: Unknown hash table version.\n");
|
---|
47 | return 0;
|
---|
48 | }
|
---|
49 | #endif
|
---|
50 |
|
---|
51 | int h = hash(c, len) % table->hashSize;
|
---|
52 | const HashEntry *e = &table->entries[h];
|
---|
53 |
|
---|
54 | // empty bucket ?
|
---|
55 | if (!e->s)
|
---|
56 | return 0;
|
---|
57 |
|
---|
58 | do {
|
---|
59 | // compare strings
|
---|
60 | if (keysMatch(c, len, e->s))
|
---|
61 | return e;
|
---|
62 | // try next bucket
|
---|
63 | e = e->next;
|
---|
64 | } while (e);
|
---|
65 |
|
---|
66 | return 0;
|
---|
67 | }
|
---|
68 |
|
---|
69 | const HashEntry* Lookup::findEntry( const struct HashTable *table,
|
---|
70 | const Identifier &s )
|
---|
71 | {
|
---|
72 | return findEntry( table, s.data(), s.size() );
|
---|
73 | }
|
---|
74 |
|
---|
75 | int Lookup::find(const struct HashTable *table,
|
---|
76 | const UChar *c, unsigned int len)
|
---|
77 | {
|
---|
78 | const HashEntry *entry = findEntry( table, c, len );
|
---|
79 | if (entry)
|
---|
80 | return entry->value;
|
---|
81 | return -1;
|
---|
82 | }
|
---|
83 |
|
---|
84 | int Lookup::find(const struct HashTable *table, const Identifier &s)
|
---|
85 | {
|
---|
86 | return find(table, s.data(), s.size());
|
---|
87 | }
|
---|
88 |
|
---|
89 | unsigned int Lookup::hash(const UChar *c, unsigned int len)
|
---|
90 | {
|
---|
91 | unsigned int val = 0;
|
---|
92 | // ignoring higher byte
|
---|
93 | for (unsigned int i = 0; i < len; i++, c++)
|
---|
94 | val += c->low();
|
---|
95 |
|
---|
96 | return val;
|
---|
97 | }
|
---|
98 |
|
---|
99 | unsigned int Lookup::hash(const Identifier &key)
|
---|
100 | {
|
---|
101 | return hash(key.data(), key.size());
|
---|
102 | }
|
---|
103 |
|
---|
104 | unsigned int Lookup::hash(const char *s)
|
---|
105 | {
|
---|
106 | unsigned int val = 0;
|
---|
107 | while (*s)
|
---|
108 | val += *s++;
|
---|
109 |
|
---|
110 | return val;
|
---|
111 | }
|
---|