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