| 1 |
/*
|
| 2 |
* PROGRAM: JRD access method
|
| 3 |
* MODULE: hsh.cpp
|
| 4 |
* DESCRIPTION: Hash table and symbol manager
|
| 5 |
*
|
| 6 |
* The contents of this file are subject to the Interbase Public
|
| 7 |
* License Version 1.0 (the "License"); you may not use this file
|
| 8 |
* except in compliance with the License. You may obtain a copy
|
| 9 |
* of the License at http://www.Inprise.com/IPL.html
|
| 10 |
*
|
| 11 |
* Software distributed under the License is distributed on an
|
| 12 |
* "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express
|
| 13 |
* or implied. See the License for the specific language governing
|
| 14 |
* rights and limitations under the License.
|
| 15 |
*
|
| 16 |
* The Original Code was created by Inprise Corporation
|
| 17 |
* and its predecessors. Portions created by Inprise Corporation are
|
| 18 |
* Copyright (C) Inprise Corporation.
|
| 19 |
*
|
| 20 |
* All Rights Reserved.
|
| 21 |
* Contributor(s): ______________________________________.
|
| 22 |
*/
|
| 23 |
|
| 24 |
#include "firebird.h"
|
| 25 |
#include <string.h>
|
| 26 |
#include "../jrd/common.h"
|
| 27 |
#include "../jrd/jrd.h"
|
| 28 |
#include "../jrd/val.h"
|
| 29 |
#include "../jrd/err_proto.h"
|
| 30 |
#include "../jrd/sym.h"
|
| 31 |
#include "../jrd/thd.h"
|
| 32 |
|
| 33 |
|
| 34 |
using namespace Jrd;
|
| 35 |
|
| 36 |
namespace {
|
| 37 |
SSHORT hash_func(const Firebird::MetaName&);
|
| 38 |
}
|
| 39 |
|
| 40 |
void Symbol::insert()
|
| 41 |
{
|
| 42 |
/**************************************
|
| 43 |
*
|
| 44 |
* S Y M _ i n s e r t
|
| 45 |
*
|
| 46 |
**************************************
|
| 47 |
*
|
| 48 |
* Functional description
|
| 49 |
* Insert a symbol into the hash table.
|
| 50 |
*
|
| 51 |
**************************************/
|
| 52 |
Database* dbb = GET_DBB();
|
| 53 |
|
| 54 |
const int h = hash_func(sym_string);
|
| 55 |
|
| 56 |
for (Symbol* old = dbb->dbb_hash_table[h]; old; old = old->sym_collision)
|
| 57 |
{
|
| 58 |
if (sym_string == old->sym_string)
|
| 59 |
{
|
| 60 |
sym_homonym = old->sym_homonym;
|
| 61 |
old->sym_homonym = this;
|
| 62 |
return;
|
| 63 |
}
|
| 64 |
}
|
| 65 |
|
| 66 |
sym_collision = dbb->dbb_hash_table[h];
|
| 67 |
dbb->dbb_hash_table[h] = this;
|
| 68 |
}
|
| 69 |
|
| 70 |
|
| 71 |
Symbol* Symbol::lookup(const Firebird::MetaName& string)
|
| 72 |
{
|
| 73 |
/**************************************
|
| 74 |
*
|
| 75 |
* S Y M _ l o o k u p
|
| 76 |
*
|
| 77 |
**************************************
|
| 78 |
*
|
| 79 |
* Functional description
|
| 80 |
* Perform a string lookup against hash table.
|
| 81 |
*
|
| 82 |
**************************************/
|
| 83 |
Database* dbb = GET_DBB();
|
| 84 |
|
| 85 |
for (Symbol* symbol = dbb->dbb_hash_table[hash_func(string)]; symbol;
|
| 86 |
symbol = symbol->sym_collision)
|
| 87 |
{
|
| 88 |
if (string == symbol->sym_string)
|
| 89 |
return symbol;
|
| 90 |
}
|
| 91 |
|
| 92 |
return NULL;
|
| 93 |
}
|
| 94 |
|
| 95 |
|
| 96 |
void Symbol::remove()
|
| 97 |
{
|
| 98 |
/**************************************
|
| 99 |
*
|
| 100 |
* S Y M _ r e m o v e
|
| 101 |
*
|
| 102 |
**************************************
|
| 103 |
*
|
| 104 |
* Functional description
|
| 105 |
* Remove a symbol from the hash table.
|
| 106 |
*
|
| 107 |
**************************************/
|
| 108 |
Database* dbb = GET_DBB();
|
| 109 |
|
| 110 |
const int h = hash_func(sym_string);
|
| 111 |
|
| 112 |
for (Symbol** next = &dbb->dbb_hash_table[h]; *next;
|
| 113 |
next = &(*next)->sym_collision)
|
| 114 |
{
|
| 115 |
if (this == *next) {
|
| 116 |
Symbol* homonym = sym_homonym;
|
| 117 |
if (homonym) {
|
| 118 |
homonym->sym_collision = sym_collision;
|
| 119 |
*next = homonym;
|
| 120 |
return;
|
| 121 |
}
|
| 122 |
else {
|
| 123 |
*next = sym_collision;
|
| 124 |
return;
|
| 125 |
}
|
| 126 |
}
|
| 127 |
else {
|
| 128 |
for (Symbol** ptr = &(*next)->sym_homonym; *ptr;
|
| 129 |
ptr = &(*ptr)->sym_homonym)
|
| 130 |
{
|
| 131 |
if (this == *ptr) {
|
| 132 |
*ptr = sym_homonym;
|
| 133 |
return;
|
| 134 |
}
|
| 135 |
}
|
| 136 |
}
|
| 137 |
}
|
| 138 |
|
| 139 |
BUGCHECK(164); /* msg 164 failed to remove symbol from hash table */
|
| 140 |
}
|
| 141 |
|
| 142 |
namespace {
|
| 143 |
|
| 144 |
SSHORT hash_func(const Firebird::MetaName& str)
|
| 145 |
{
|
| 146 |
/**************************************
|
| 147 |
*
|
| 148 |
* h a s h
|
| 149 |
*
|
| 150 |
**************************************
|
| 151 |
*
|
| 152 |
* Functional description
|
| 153 |
* Returns the hash function of a string.
|
| 154 |
*
|
| 155 |
**************************************/
|
| 156 |
|
| 157 |
/* It is OK to not Internationalize this function as it is for
|
| 158 |
internal optimization of symbol lookup */
|
| 159 |
|
| 160 |
int value = 0;
|
| 161 |
|
| 162 |
for (const char *s = str.c_str(); *s; s++)
|
| 163 |
{
|
| 164 |
value = (value << 1) + UPPER7(*s);
|
| 165 |
}
|
| 166 |
|
| 167 |
return ((value >= 0) ? value : -value) % HASH_SIZE;
|
| 168 |
}
|
| 169 |
|
| 170 |
} //noname namespace
|