2007-12-30 17:41:49 +01:00
|
|
|
/***************************************************************************
|
|
|
|
|
2011-09-08 22:23:15 +02:00
|
|
|
gb_hash_temp.h
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2011-09-08 22:23:15 +02:00
|
|
|
(c) 2000-2011 Benoît Minisini <gambas@users.sourceforge.net>
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2011-09-08 22:23:15 +02:00
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation; either version 2, or (at your option)
|
|
|
|
any later version.
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2011-09-08 22:23:15 +02:00
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2011-09-08 22:23:15 +02:00
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
2011-06-03 02:51:09 +02:00
|
|
|
MA 02110-1301, USA.
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
***************************************************************************/
|
|
|
|
|
|
|
|
#define __GB_HASH_C
|
|
|
|
|
|
|
|
#include <ctype.h>
|
|
|
|
|
|
|
|
#include "gb_common.h"
|
|
|
|
#include "gb_common_case.h"
|
2008-01-27 15:00:04 +01:00
|
|
|
#include "gb_common_string.h"
|
2007-12-30 17:41:49 +01:00
|
|
|
#include "gb_alloc.h"
|
|
|
|
#include "gb_hash.h"
|
|
|
|
|
|
|
|
|
|
|
|
#define NODE_value(_node) ((void *)((char *)_node) + sizeof(HASH_NODE))
|
|
|
|
//#define NODE_length(_table, _node) (*((ushort *)(((char *)_node) + sizeof(HASH_NODE) + (_table)->s_value)))
|
|
|
|
#define NODE_key(_table, _node) ((HASH_KEY *)(((char *)_node) + sizeof(HASH_NODE) + (_table)->s_value))
|
|
|
|
|
|
|
|
static void hash_table_resize(HASH_TABLE *hash_table);
|
|
|
|
static HASH_NODE **hash_table_lookup_node(HASH_TABLE *hash_table, const char *key, int len);
|
|
|
|
static HASH_NODE *hash_node_new(HASH_TABLE *hash_table, const char *key, int len);
|
|
|
|
static void hash_node_destroy(HASH_NODE *hash_node);
|
|
|
|
static void hash_nodes_destroy(HASH_NODE *hash_node);
|
|
|
|
|
|
|
|
|
|
|
|
static const int primes[] =
|
|
|
|
{
|
2011-09-08 22:23:15 +02:00
|
|
|
11, 19, 37, 73, 109, 163, 251, 367, 557, 823, 1237, 1861, 2777, 4177, 6247, 9371,
|
|
|
|
14057, 21089, 31627, 47431, 71143, 106721, 160073, 240101, 360163, 540217, 810343,
|
|
|
|
1215497, 1823231, 2734867, 4102283, 6153409, 9230113, 13845163
|
2007-12-30 17:41:49 +01:00
|
|
|
};
|
|
|
|
|
2010-06-06 22:43:13 +02:00
|
|
|
//static const uint seed[] = { 0x9A177BA5, 0x9A177BA4, 0x9A177BA7, 0x9A177BA6, 0x9A177BA1, 0x9A177BA0, 0x9A177BA3, 0x9A177BA2, 0x9A177BAD };
|
|
|
|
|
2007-12-30 17:41:49 +01:00
|
|
|
static const int nprimes = sizeof (primes) / sizeof (primes[0]);
|
|
|
|
|
|
|
|
static int spaced_primes_closest(int num)
|
|
|
|
{
|
2011-09-08 22:23:15 +02:00
|
|
|
int i;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2011-09-08 22:23:15 +02:00
|
|
|
for (i = 0; i < nprimes; i++)
|
|
|
|
if (primes[i] > num)
|
|
|
|
return primes[i];
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2011-09-08 22:23:15 +02:00
|
|
|
return primes[nprimes - 1];
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-06-06 22:43:13 +02:00
|
|
|
// Fast hashing functions. Sometimes Microsoft Research produces useful things.
|
|
|
|
|
2007-12-30 17:41:49 +01:00
|
|
|
static uint key_hash_binary(const char *key, int len)
|
|
|
|
{
|
2010-06-06 22:43:13 +02:00
|
|
|
static const void *jump[] = { &&__LEN_0, &&__LEN_1, &&__LEN_2, &&__LEN_3, &&__LEN_4, &&__LEN_5, &&__LEN_6, &&__LEN_7, &&__LEN_8 };
|
2011-09-08 22:23:15 +02:00
|
|
|
uint hash = 0x9A177BA5 ^ len;
|
2008-01-27 15:00:04 +01:00
|
|
|
|
2010-06-06 22:43:13 +02:00
|
|
|
if (len > 8)
|
|
|
|
len = 8;
|
|
|
|
|
2008-01-27 15:00:04 +01:00
|
|
|
goto *jump[len];
|
|
|
|
|
2010-06-06 22:43:13 +02:00
|
|
|
__LEN_8:
|
|
|
|
hash = hash * 101 + key[7];
|
|
|
|
__LEN_7:
|
|
|
|
hash = hash * 101 + key[6];
|
|
|
|
__LEN_6:
|
|
|
|
hash = hash * 101 + key[5];
|
|
|
|
__LEN_5:
|
|
|
|
hash = hash * 101 + key[4];
|
|
|
|
__LEN_4:
|
|
|
|
hash = hash * 101 + key[3];
|
2008-01-27 15:00:04 +01:00
|
|
|
__LEN_3:
|
2010-06-06 22:43:13 +02:00
|
|
|
hash = hash * 101 + key[2];
|
2008-01-27 15:00:04 +01:00
|
|
|
__LEN_2:
|
2010-06-06 22:43:13 +02:00
|
|
|
hash = hash * 101 + key[1];
|
2008-01-27 15:00:04 +01:00
|
|
|
__LEN_1:
|
2010-06-06 22:43:13 +02:00
|
|
|
hash = hash * 101 + key[0];
|
2008-01-27 15:00:04 +01:00
|
|
|
__LEN_0:
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-06-06 22:43:13 +02:00
|
|
|
return hash;
|
|
|
|
}
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
static uint key_hash_text(const char *key, int len)
|
|
|
|
{
|
2010-06-06 22:43:13 +02:00
|
|
|
static const void *jump[] = { &&__LEN_0, &&__LEN_1, &&__LEN_2, &&__LEN_3, &&__LEN_4, &&__LEN_5, &&__LEN_6, &&__LEN_7, &&__LEN_8 };
|
2011-09-08 22:23:15 +02:00
|
|
|
uint hash = 0x9A177BA5 ^ len;
|
2008-01-27 15:00:04 +01:00
|
|
|
|
2010-06-06 22:43:13 +02:00
|
|
|
if (len > 8)
|
|
|
|
len = 8;
|
|
|
|
|
2008-01-27 15:00:04 +01:00
|
|
|
goto *jump[len];
|
|
|
|
|
2010-06-06 22:43:13 +02:00
|
|
|
__LEN_8:
|
|
|
|
hash = hash * 101 + (key[7] & ~0x20);
|
|
|
|
__LEN_7:
|
|
|
|
hash = hash * 101 + (key[6] & ~0x20);
|
|
|
|
__LEN_6:
|
|
|
|
hash = hash * 101 + (key[5] & ~0x20);
|
|
|
|
__LEN_5:
|
|
|
|
hash = hash * 101 + (key[4] & ~0x20);
|
|
|
|
__LEN_4:
|
|
|
|
hash = hash * 101 + (key[3] & ~0x20);
|
2008-01-27 15:00:04 +01:00
|
|
|
__LEN_3:
|
2010-06-06 22:43:13 +02:00
|
|
|
hash = hash * 101 + (key[2] & ~0x20);
|
2008-01-27 15:00:04 +01:00
|
|
|
__LEN_2:
|
2010-06-06 22:43:13 +02:00
|
|
|
hash = hash * 101 + (key[1] & ~0x20);
|
2008-01-27 15:00:04 +01:00
|
|
|
__LEN_1:
|
2010-06-06 22:43:13 +02:00
|
|
|
hash = hash * 101 + (key[0] & ~0x20);
|
2008-01-27 15:00:04 +01:00
|
|
|
__LEN_0:
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2011-09-08 22:23:15 +02:00
|
|
|
return hash;
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-06-06 22:43:13 +02:00
|
|
|
#define get_hash_func(_hash) ((_hash)->mode ? key_hash_text : key_hash_binary)
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
|
2009-07-08 21:57:50 +02:00
|
|
|
void HASH_TABLE_create(HASH_TABLE **hash, size_t s_value, HASH_FLAG mode)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2011-09-08 22:23:15 +02:00
|
|
|
HASH_TABLE *hash_table;
|
|
|
|
/*int i;*/
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2011-09-08 22:23:15 +02:00
|
|
|
ALLOC_ZERO(&hash_table, sizeof(HASH_TABLE), "HASH_TABLE_create");
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2011-09-08 22:23:15 +02:00
|
|
|
hash_table->size = HASH_TABLE_MIN_SIZE;
|
|
|
|
hash_table->s_value = s_value;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2011-09-08 22:23:15 +02:00
|
|
|
ALLOC_ZERO(&hash_table->nodes, sizeof(HASH_NODE *) * hash_table->size, "HASH_TABLE_create");
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2011-09-08 22:23:15 +02:00
|
|
|
if (mode == HF_IGNORE_CASE)
|
|
|
|
hash_table->mode = mode;
|
|
|
|
else
|
|
|
|
hash_table->mode = HF_NORMAL;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2011-09-08 22:23:15 +02:00
|
|
|
*hash = hash_table;
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-07-08 21:57:50 +02:00
|
|
|
void HASH_TABLE_delete(HASH_TABLE **hash)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2011-09-08 22:23:15 +02:00
|
|
|
int i;
|
|
|
|
HASH_TABLE *hash_table = *hash;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2011-09-08 22:23:15 +02:00
|
|
|
if (hash_table == NULL)
|
|
|
|
return;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2011-09-08 22:23:15 +02:00
|
|
|
for (i = 0; i < hash_table->size; i++)
|
|
|
|
hash_nodes_destroy(hash_table->nodes[i]);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2011-09-08 22:23:15 +02:00
|
|
|
FREE(&hash_table->nodes, "HASH_TABLE_delete");
|
|
|
|
FREE(hash, "HASH_TABLE_delete");
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-07-08 21:57:50 +02:00
|
|
|
int HASH_TABLE_size(HASH_TABLE *hash_table)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2011-09-08 22:23:15 +02:00
|
|
|
return hash_table->nnodes;
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static HASH_NODE **hash_table_lookup_node(HASH_TABLE *hash_table, const char *key, int len)
|
|
|
|
{
|
2011-09-08 22:23:15 +02:00
|
|
|
HASH_NODE **node;
|
|
|
|
HASH_KEY *node_key;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-06-06 22:43:13 +02:00
|
|
|
if (hash_table->mode)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2011-09-08 22:23:15 +02:00
|
|
|
node = &hash_table->nodes[key_hash_text(key, len) % hash_table->size];
|
|
|
|
|
|
|
|
while (*node)
|
|
|
|
{
|
|
|
|
node_key = NODE_key(hash_table, *node);
|
|
|
|
if (UNLIKELY(STRING_equal_ignore_case(key, len, node_key->key, node_key->len)))
|
|
|
|
break;
|
|
|
|
node = &(*node)->next;
|
|
|
|
}
|
|
|
|
}
|
2007-12-30 17:41:49 +01:00
|
|
|
else
|
|
|
|
{
|
|
|
|
node = &hash_table->nodes[key_hash_binary(key, len) % hash_table->size];
|
|
|
|
|
2011-09-08 22:23:15 +02:00
|
|
|
while (*node)
|
|
|
|
{
|
|
|
|
node_key = NODE_key(hash_table, *node);
|
|
|
|
if (UNLIKELY(STRING_equal(key, len, node_key->key, node_key->len)))
|
|
|
|
break;
|
|
|
|
node = &(*node)->next;
|
|
|
|
}
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
2011-09-08 22:23:15 +02:00
|
|
|
return node;
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-07-08 21:57:50 +02:00
|
|
|
void *HASH_TABLE_lookup(HASH_TABLE *hash_table, const char *key, int len)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2011-09-08 22:23:15 +02:00
|
|
|
HASH_NODE *node;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-06-09 01:08:04 +02:00
|
|
|
if (len == 0)
|
|
|
|
return NULL;
|
|
|
|
|
2011-09-08 22:23:15 +02:00
|
|
|
node = *hash_table_lookup_node(hash_table, key, len);
|
|
|
|
hash_table->last = node;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2011-09-08 22:23:15 +02:00
|
|
|
return node ? NODE_value(node) : NULL;
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-07-08 21:57:50 +02:00
|
|
|
void *HASH_TABLE_insert(HASH_TABLE *hash_table, const char *key, int len)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2011-09-08 22:23:15 +02:00
|
|
|
HASH_NODE **node;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2011-09-08 22:23:15 +02:00
|
|
|
node = hash_table_lookup_node(hash_table, key, len);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2011-09-08 22:23:15 +02:00
|
|
|
if (UNLIKELY(*node != NULL))
|
|
|
|
return NODE_value(*node);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2011-09-08 22:23:15 +02:00
|
|
|
/*hash_table_resize(hash_table);*/
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2011-09-08 22:23:15 +02:00
|
|
|
*node = hash_node_new(hash_table, key, len);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2011-09-08 22:23:15 +02:00
|
|
|
hash_table->nnodes++;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2011-09-08 22:23:15 +02:00
|
|
|
/*if (!hash_table->frozen)*/
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2011-09-08 22:23:15 +02:00
|
|
|
return NODE_value(*node);
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-07-08 21:57:50 +02:00
|
|
|
void HASH_TABLE_remove(HASH_TABLE *hash_table, const char *key, int len)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2011-09-08 22:23:15 +02:00
|
|
|
HASH_NODE **node, *dest;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2011-09-08 22:23:15 +02:00
|
|
|
node = hash_table_lookup_node(hash_table, key, len);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2011-09-08 22:23:15 +02:00
|
|
|
if (LIKELY(*node != NULL))
|
|
|
|
{
|
|
|
|
dest = *node;
|
|
|
|
(*node) = dest->next;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2011-09-08 22:23:15 +02:00
|
|
|
#ifdef KEEP_ORDER
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2011-09-08 22:23:15 +02:00
|
|
|
if (dest->sprev)
|
|
|
|
dest->sprev->snext = dest->snext;
|
|
|
|
else
|
|
|
|
hash_table->sfirst = dest->snext;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2011-09-08 22:23:15 +02:00
|
|
|
if (dest->snext)
|
|
|
|
dest->snext->sprev = dest->sprev;
|
|
|
|
else
|
|
|
|
hash_table->slast = dest->sprev;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2011-09-08 22:23:15 +02:00
|
|
|
#endif
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2011-09-08 22:23:15 +02:00
|
|
|
hash_node_destroy(dest);
|
|
|
|
hash_table->nnodes--;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2011-09-08 22:23:15 +02:00
|
|
|
hash_table->last = NULL;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2011-09-08 22:23:15 +02:00
|
|
|
/*if (!hash_table->frozen)*/
|
|
|
|
hash_table_resize(hash_table);
|
|
|
|
}
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-07-08 21:57:50 +02:00
|
|
|
void *HASH_TABLE_next(HASH_TABLE *hash_table, HASH_ENUM *iter)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2011-09-08 22:23:15 +02:00
|
|
|
#ifdef KEEP_ORDER
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2011-09-08 22:23:15 +02:00
|
|
|
if (iter->node == NULL)
|
|
|
|
iter->node = hash_table->sfirst;
|
|
|
|
else
|
|
|
|
iter->node = iter->next;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2011-09-08 22:23:15 +02:00
|
|
|
hash_table->last = iter->node;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2011-09-08 22:23:15 +02:00
|
|
|
if (iter->node)
|
|
|
|
{
|
|
|
|
iter->next = iter->node->snext;
|
|
|
|
return NODE_value(iter->node);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return NULL;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2011-09-08 22:23:15 +02:00
|
|
|
#else
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2011-09-08 22:23:15 +02:00
|
|
|
HASH_NODE *enum_node = iter->node;
|
|
|
|
int enum_i = iter->index;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2011-09-08 22:23:15 +02:00
|
|
|
if (enum_node)
|
|
|
|
enum_node = enum_node->next;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2011-09-08 22:23:15 +02:00
|
|
|
while (enum_node == NULL)
|
|
|
|
{
|
|
|
|
enum_i++;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2011-09-08 22:23:15 +02:00
|
|
|
if (enum_i >= hash_table->size)
|
|
|
|
break;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2011-09-08 22:23:15 +02:00
|
|
|
enum_node = hash_table->nodes[enum_i];
|
|
|
|
};
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2011-09-08 22:23:15 +02:00
|
|
|
iter->node = enum_node;
|
|
|
|
iter->index = enum_i;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2011-09-08 22:23:15 +02:00
|
|
|
hash_table->last = enum_node;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2011-09-08 22:23:15 +02:00
|
|
|
if (enum_node)
|
|
|
|
return NODE_value(enum_node);
|
|
|
|
else
|
|
|
|
return NULL;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2011-09-08 22:23:15 +02:00
|
|
|
#endif
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void hash_table_resize(HASH_TABLE *hash_table)
|
|
|
|
{
|
2011-09-08 22:23:15 +02:00
|
|
|
HASH_NODE **new_nodes;
|
|
|
|
HASH_NODE *node;
|
|
|
|
HASH_NODE *next;
|
|
|
|
HASH_KEY *node_key;
|
|
|
|
double nodes_per_list;
|
|
|
|
int hash_val;
|
|
|
|
int new_size;
|
|
|
|
int i;
|
|
|
|
HASH_FUNC hash_func = get_hash_func(hash_table);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
|
2011-09-08 22:23:15 +02:00
|
|
|
nodes_per_list = hash_table->nnodes / hash_table->size;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2011-09-08 22:23:15 +02:00
|
|
|
if ((nodes_per_list > 0.3 || hash_table->size <= HASH_TABLE_MIN_SIZE) &&
|
|
|
|
(nodes_per_list < 3.0 || hash_table->size >= HASH_TABLE_MAX_SIZE))
|
|
|
|
return;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2011-09-08 22:23:15 +02:00
|
|
|
new_size = MinMax(spaced_primes_closest(hash_table->nnodes), HASH_TABLE_MIN_SIZE, HASH_TABLE_MAX_SIZE);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2011-09-08 22:23:15 +02:00
|
|
|
ALLOC_ZERO(&new_nodes, new_size * sizeof(HASH_NODE *), "hash_table_resize");
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2011-09-08 22:23:15 +02:00
|
|
|
for (i = 0; i < hash_table->size; i++)
|
|
|
|
for (node = hash_table->nodes[i]; node; node = next)
|
|
|
|
{
|
|
|
|
next = node->next;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2011-09-08 22:23:15 +02:00
|
|
|
node_key = NODE_key(hash_table, node);
|
|
|
|
hash_val = (*hash_func)(node_key->key, node_key->len) % new_size;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2011-09-08 22:23:15 +02:00
|
|
|
node->next = new_nodes[hash_val];
|
|
|
|
new_nodes[hash_val] = node;
|
|
|
|
}
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2011-09-08 22:23:15 +02:00
|
|
|
FREE(&hash_table->nodes, "hash_table_resize");
|
|
|
|
hash_table->nodes = new_nodes;
|
|
|
|
hash_table->size = new_size;
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static HASH_NODE *hash_node_new(HASH_TABLE *hash_table, const char *key, int len)
|
|
|
|
{
|
2011-09-08 22:23:15 +02:00
|
|
|
HASH_NODE *hash_node;
|
|
|
|
int size;
|
|
|
|
HASH_KEY *node_key;
|
|
|
|
|
|
|
|
size = sizeof(HASH_NODE) + hash_table->s_value + len + sizeof(ushort);
|
|
|
|
ALLOC_ZERO(&hash_node, size, "hash_node_new");
|
|
|
|
|
|
|
|
node_key = NODE_key(hash_table, hash_node);
|
|
|
|
memcpy(node_key->key, key, len);
|
|
|
|
node_key->len = len;
|
|
|
|
|
|
|
|
#ifdef KEEP_ORDER
|
|
|
|
|
|
|
|
if (UNLIKELY(!hash_table->sfirst))
|
|
|
|
{
|
|
|
|
hash_table->sfirst = hash_node;
|
|
|
|
hash_table->slast = hash_node;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
hash_node->sprev = hash_table->slast;
|
|
|
|
hash_node->sprev->snext = hash_node;
|
|
|
|
hash_table->slast = hash_node;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return hash_node;
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void hash_node_destroy(HASH_NODE *hash_node)
|
|
|
|
{
|
2011-09-08 22:23:15 +02:00
|
|
|
FREE(&hash_node, "hash_node_destroy");
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void hash_nodes_destroy(HASH_NODE *hash_node)
|
|
|
|
{
|
2011-09-08 22:23:15 +02:00
|
|
|
HASH_NODE *node = hash_node;
|
|
|
|
HASH_NODE *next;
|
|
|
|
|
|
|
|
for(;;)
|
|
|
|
{
|
|
|
|
if (UNLIKELY(node == NULL))
|
|
|
|
return;
|
|
|
|
|
|
|
|
next = node->next;
|
|
|
|
FREE(&node, "hash_nodes_destroy");
|
|
|
|
node = next;
|
|
|
|
}
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-07-08 21:57:50 +02:00
|
|
|
void HASH_TABLE_get_key(HASH_TABLE *hash_table, HASH_NODE *node, char **key, int *len)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2011-09-08 22:23:15 +02:00
|
|
|
HASH_KEY *node_key;
|
|
|
|
|
|
|
|
if (LIKELY(node != NULL))
|
|
|
|
{
|
|
|
|
node_key = NODE_key(hash_table, node);
|
|
|
|
*key = node_key->key;
|
|
|
|
*len = node_key->len;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
*len = 0;
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-07-08 21:57:50 +02:00
|
|
|
bool HASH_TABLE_get_last_key(HASH_TABLE *hash_table, char **key, int *len)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2011-09-08 22:23:15 +02:00
|
|
|
if (hash_table->last == NULL)
|
|
|
|
return TRUE;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2011-09-08 22:23:15 +02:00
|
|
|
HASH_TABLE_get_key(hash_table, hash_table->last, key, len);
|
|
|
|
return FALSE;
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|