8
0
mirror of https://github.com/FirebirdSQL/firebird.git synced 2025-01-30 09:23:09 +01:00
firebird-mirror/src/jrd/sym.cpp

165 lines
3.5 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>
#include "../jrd/jrd.h"
#include "../jrd/val.h"
#include "../jrd/err_proto.h"
#include "../jrd/sym_proto.h"
#include "../jrd/thd_proto.h"
2003-12-31 06:36:12 +01:00
static SSHORT hash_func(const SCHAR*);
2001-05-23 15:26:42 +02:00
void SYM_insert(SYM symbol)
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
2003-12-31 06:36:12 +01:00
const int h = hash_func(symbol->sym_string);
2001-05-23 15:26:42 +02:00
2003-12-31 06:36:12 +01:00
for (sym* old = dbb->dbb_hash_table[h]; old; old = old->sym_collision) {
2001-05-23 15:26:42 +02:00
if (!strcmp(symbol->sym_string, old->sym_string)) {
symbol->sym_homonym = old->sym_homonym;
old->sym_homonym = symbol;
return;
}
2003-12-31 06:36:12 +01:00
}
2001-05-23 15:26:42 +02:00
symbol->sym_collision = dbb->dbb_hash_table[h];
dbb->dbb_hash_table[h] = symbol;
}
2003-12-31 06:36:12 +01:00
SYM SYM_lookup(const TEXT* 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
2003-12-31 06:36:12 +01:00
for (sym* 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
{
if (!strcmp(string, symbol->sym_string))
return symbol;
}
2001-05-23 15:26:42 +02:00
return NULL;
}
void SYM_remove(SYM symbol)
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
2003-12-31 06:36:12 +01:00
const int h = hash_func(symbol->sym_string);
2001-05-23 15:26:42 +02:00
2003-12-31 06:36:12 +01:00
for (sym** next = &dbb->dbb_hash_table[h]; *next;
2003-09-13 14:03:11 +02:00
next = &(*next)->sym_collision)
{
2003-12-31 06:36:12 +01:00
if (symbol == *next) {
sym* homonym = symbol->sym_homonym;
if (homonym) {
2001-05-23 15:26:42 +02:00
homonym->sym_collision = symbol->sym_collision;
*next = homonym;
return;
}
else {
*next = symbol->sym_collision;
return;
}
2003-12-31 06:36:12 +01:00
}
else {
for (sym** ptr = &(*next)->sym_homonym; *ptr;
2003-09-13 14:03:11 +02:00
ptr = &(*ptr)->sym_homonym)
{
if (symbol == *ptr) {
2001-05-23 15:26:42 +02:00
*ptr = symbol->sym_homonym;
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 */
}
2003-12-31 06:36:12 +01:00
static SSHORT hash_func(const SCHAR* string)
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
2003-12-31 06:36:12 +01:00
SCHAR c;
while ( (c = *string++) ) {
2001-05-23 15:26:42 +02:00
value = (value << 1) + UPPER7(c);
2003-12-31 06:36:12 +01:00
}
2001-05-23 15:26:42 +02:00
return ((value >= 0) ? value : -value) % HASH_SIZE;
}