8
0
mirror of https://github.com/FirebirdSQL/firebird.git synced 2025-01-29 06:43:03 +01:00
firebird-mirror/src/jrd/sym.cpp

171 lines
3.6 KiB
C++
Raw Normal View History

2001-05-23 15:26:42 +02:00
/*
* PROGRAM: JRD access method
* MODULE: hsh.cpp
2001-05-23 15:26:42 +02:00
* DESCRIPTION: Hash table and symbol manager
*
* The contents of this file are subject to the Interbase Public
* License Version 1.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy
* of the License at http://www.Inprise.com/IPL.html
*
* Software distributed under the License is distributed on an
* "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express
* or implied. See the License for the specific language governing
* rights and limitations under the License.
*
* The Original Code was created by Inprise Corporation
* and its predecessors. Portions created by Inprise Corporation are
* Copyright (C) Inprise Corporation.
*
* All Rights Reserved.
* Contributor(s): ______________________________________.
*/
#include "firebird.h"
2001-05-23 15:26:42 +02:00
#include <string.h>
2004-03-22 12:38:23 +01:00
#include "../jrd/common.h"
2001-05-23 15:26:42 +02:00
#include "../jrd/jrd.h"
#include "../jrd/val.h"
#include "../jrd/err_proto.h"
2004-03-31 20:02:50 +02:00
#include "../jrd/sym.h"
2001-05-23 15:26:42 +02:00
#include "../jrd/thd_proto.h"
using namespace Jrd;
2001-05-23 15:26:42 +02:00
2004-03-31 20:02:50 +02:00
namespace {
SSHORT hash_func(const Firebird::string&);
}
2001-05-23 15:26:42 +02:00
2004-03-31 20:02:50 +02:00
void Symbol::insert()
2001-05-23 15:26:42 +02:00
{
/**************************************
*
* S Y M _ i n s e r t
*
**************************************
*
* Functional description
* Insert a symbol into the hash table.
*
**************************************/
2004-03-07 08:58:55 +01:00
Database* dbb = GET_DBB;
2001-05-23 15:26:42 +02:00
2004-03-31 20:02:50 +02:00
const int h = hash_func(sym_string);
2001-05-23 15:26:42 +02:00
2004-03-31 20:02:50 +02:00
for (Symbol* old = dbb->dbb_hash_table[h]; old; old = old->sym_collision)
{
if (sym_string == old->sym_string)
{
sym_homonym = old->sym_homonym;
old->sym_homonym = this;
2001-05-23 15:26:42 +02:00
return;
}
2003-12-31 06:36:12 +01:00
}
2001-05-23 15:26:42 +02:00
2004-03-31 20:02:50 +02:00
sym_collision = dbb->dbb_hash_table[h];
dbb->dbb_hash_table[h] = this;
2001-05-23 15:26:42 +02:00
}
2004-03-31 20:02:50 +02:00
Symbol* Symbol::lookup(const Firebird::string& string)
2001-05-23 15:26:42 +02:00
{
/**************************************
*
* S Y M _ l o o k u p
*
**************************************
*
* Functional description
* Perform a string lookup against hash table.
*
**************************************/
2004-03-07 08:58:55 +01:00
Database* dbb = GET_DBB;
2001-05-23 15:26:42 +02:00
for (Symbol* symbol = dbb->dbb_hash_table[hash_func(string)]; symbol;
2001-05-23 15:26:42 +02:00
symbol = symbol->sym_collision)
2003-12-31 06:36:12 +01:00
{
2004-03-31 20:02:50 +02:00
if (string == symbol->sym_string)
2003-12-31 06:36:12 +01:00
return symbol;
}
2001-05-23 15:26:42 +02:00
return NULL;
}
2004-03-31 20:02:50 +02:00
void Symbol::remove()
2001-05-23 15:26:42 +02:00
{
/**************************************
*
* S Y M _ r e m o v e
*
**************************************
*
* Functional description
* Remove a symbol from the hash table.
*
**************************************/
2004-03-07 08:58:55 +01:00
Database* dbb = GET_DBB;
2001-05-23 15:26:42 +02:00
2004-03-31 20:02:50 +02:00
const int h = hash_func(sym_string);
2001-05-23 15:26:42 +02:00
for (Symbol** next = &dbb->dbb_hash_table[h]; *next;
2003-09-13 14:03:11 +02:00
next = &(*next)->sym_collision)
{
2004-03-31 20:02:50 +02:00
if (this == *next) {
Symbol* homonym = sym_homonym;
2003-12-31 06:36:12 +01:00
if (homonym) {
2004-03-31 20:02:50 +02:00
homonym->sym_collision = sym_collision;
2001-05-23 15:26:42 +02:00
*next = homonym;
return;
}
else {
2004-03-31 20:02:50 +02:00
*next = sym_collision;
2001-05-23 15:26:42 +02:00
return;
}
2003-12-31 06:36:12 +01:00
}
else {
for (Symbol** ptr = &(*next)->sym_homonym; *ptr;
2003-09-13 14:03:11 +02:00
ptr = &(*ptr)->sym_homonym)
{
2004-03-31 20:02:50 +02:00
if (this == *ptr) {
*ptr = sym_homonym;
2001-05-23 15:26:42 +02:00
return;
}
2003-09-13 14:03:11 +02:00
}
2003-12-31 06:36:12 +01:00
}
2003-09-13 14:03:11 +02:00
}
2001-05-23 15:26:42 +02:00
BUGCHECK(164); /* msg 164 failed to remove symbol from hash table */
}
2004-03-31 20:02:50 +02:00
namespace {
2001-05-23 15:26:42 +02:00
2004-03-31 20:02:50 +02:00
SSHORT hash_func(const Firebird::string& str)
2001-05-23 15:26:42 +02:00
{
/**************************************
*
* h a s h
*
**************************************
*
* Functional description
* Returns the hash function of a string.
*
**************************************/
/* It is OK to not Internationalize this function as it is for
internal optimization of symbol lookup */
2003-12-31 06:36:12 +01:00
int value = 0;
2001-05-23 15:26:42 +02:00
2004-03-31 20:02:50 +02:00
for (const char *s = str.c_str(); *s; s++)
{
value = (value << 1) + UPPER7(*s);
2003-12-31 06:36:12 +01:00
}
2001-05-23 15:26:42 +02:00
return ((value >= 0) ? value : -value) % HASH_SIZE;
}
2004-03-31 20:02:50 +02:00
} //noname namespace