Skip to content
Skip to link menu
Trinity API Reference
Trinity API Reference
kjs
kjs
kjs
lookup.cpp
1
/*
2
* This file is part of the KDE libraries
3
* Copyright (C) 1999-2000 Harri Porten (porten@kde.org)
4
* Copyright (C) 2003 Apple Computer, Inc.
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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->uc != (
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->
soffset
)
56
return
0;
57
58
while
(1) {
59
// compare strings
60
if
(keysMatch(c, len, &table->
sbase
[e->
soffset
]))
61
return
e;
62
// try next bucket
63
if
(e->
next
< 0)
break
;
64
65
e = &table->
entries
[e->
next
];
66
}
67
68
return
0;
69
}
70
71
const
HashEntry
*
Lookup::findEntry
(
const
struct
HashTable
*table,
72
const
Identifier
&s )
73
{
74
return
findEntry
( table, s.
data
(), s.
size
() );
75
}
76
77
int
Lookup::find
(
const
struct
HashTable
*table,
78
const
UChar
*c,
unsigned
int
len)
79
{
80
const
HashEntry
*entry =
findEntry
( table, c, len );
81
if
(entry)
82
return
entry->
value
;
83
return
-1;
84
}
85
86
int
Lookup::find
(
const
struct
HashTable
*table,
const
Identifier
&s)
87
{
88
return
find
(table, s.
data
(), s.
size
());
89
}
90
91
unsigned
int
Lookup::hash
(
const
UChar
*c,
unsigned
int
len)
92
{
93
unsigned
int
val = 0;
94
// ignoring higher byte
95
for
(
unsigned
int
i = 0; i < len; i++, c++)
96
val += c->
low
();
97
98
return
val;
99
}
100
101
unsigned
int
Lookup::hash
(
const
Identifier
&key)
102
{
103
return
hash
(key.
data
(), key.
size
());
104
}
105
106
unsigned
int
Lookup::hash
(
const
char
*s)
107
{
108
unsigned
int
val = 0;
109
while
(*s)
110
val += *s++;
111
112
return
val;
113
}
kjs
Skip menu "kjs"
Main Page
Class Hierarchy
Alphabetical List
Class List
File List
Class Members
Related Pages
kjs
Skip menu "kjs"
arts
dcop
dnssd
interfaces
kspeech
interface
library
tdetexteditor
kate
kded
kdoctools
kimgio
kjs
libtdemid
libtdescreensaver
tdecore
tdeabc
tdecmshell
tdecore
tdefx
tdehtml
tdeinit
tdeio
bookmarks
httpfilter
kpasswdserver
kssl
tdeioslave
http
tdefile
tdeio
tdeioexec
tdemdi
tdemdi
tdenewstuff
tdeparts
tdeprint
tderandr
tderesources
tdespell2
tdesu
tdeui
tdeunittest
tdeutils
tdewallet