• 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::HashTable::sbase
const char *const sbase
pointer to the string table.
Definition: lookup.h:95
KJS::HashEntry::value
short int value
value is the result value (usually an enum value)
Definition: lookup.h:44
KJS::HashTable::type
int type
type is a version number.
Definition: lookup.h:75
KJS::Identifier::data
const UChar * data() const
returns a UChar pointer to the string of the identifier with a size defined by size().
Definition: identifier.h:62
KJS::Lookup::hash
static unsigned int hash(const Identifier &key)
Calculate the hash value for a given key.
Definition: lookup.cpp:101
KJS::HashEntry
An entry in a hash table.
Definition: lookup.h:36
KJS::HashTable::hashSize
int hashSize
the maximum value for the hash.
Definition: lookup.h:90
KJS::HashTable
A hash table Usually the hashtable is generated by the create_hash_table script, from a ...
Definition: lookup.h:71
KJS::HashEntry::soffset
unsigned short soffset
s is the offset to the string key (e.g.
Definition: lookup.h:40
KJS::HashEntry::next
short next
next is the index to the next entry for the same hash value
Definition: lookup.h:57
KJS::UChar::low
unsigned char low() const
Definition: ustring.h:77
KJS::UChar
Unicode character.
Definition: ustring.h:51
KJS
Definition: array_instance.h:27
KJS::Lookup::findEntry
static const HashEntry * findEntry(const struct HashTable *table, const Identifier &s)
Find an entry in the table, and return the entry This variant gives access to the other attributes of...
Definition: lookup.cpp:71
KJS::Identifier::size
int size() const
The size of the UChar string returned.
Definition: identifier.h:66
KJS::Lookup::find
static int find(const struct HashTable *table, const Identifier &s)
Find an entry in the table, and return its value (i.e.
Definition: lookup.cpp:86
KJS::HashTable::entries
const HashEntry *const entries
pointer to the array of entries Mind that some entries in the array are null (0,0,0,0).
Definition: lookup.h:86
KJS::Identifier
Represents an Identifier for a Javascript object.
Definition: identifier.h:32

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
Generated for kjs by doxygen 1.8.8
This website is maintained by Timothy Pearson.