hash.c revision 36afc64eb7e4fb92525653fea47b522fa428d12e
/*
Copyright (c) 2003 Timo Sirainen
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
occur, we move on to another hash (10% of the size of primary) which also
contains pointer to next collision, working as linked list. */
/* @UNSAFE: whole file */
#include "lib.h"
#include "hash.h"
#include "primes.h"
#include <ctype.h>
#define HASH_TABLE_MIN_SIZE 109
#define COLLISIONS_MIN_SIZE 37
struct hash_node {
void *key;
void *value;
};
struct collision_node {
struct collision_node *next;
};
struct hash_table {
int frozen;
#ifdef DEBUG
#endif
struct collision_node *collisions;
struct collision_node *free_cnodes;
};
static int foreach_stop;
{
}
static unsigned int direct_hash(const void *p)
{
/* NOTE: may truncate the value, but that doesn't matter. */
return POINTER_CAST_TO(p, unsigned int);
}
{
struct hash_table *table;
return table;
}
{
else {
}
}
struct collision_node *cnode)
{
struct collision_node *next;
}
}
{
size_t i;
for (i = 0; i < table->collisions_size; i++) {
}
}
{
}
}
{
table->nodes_count = 0;
table->removed_count = 0;
#ifdef DEBUG
table->collisions_count = 0;
#endif
}
static struct hash_node *
{
struct collision_node *cnode;
do {
}
return NULL;
}
static struct hash_node *
{
if (table->removed_count == 0)
return NULL;
} else {
return node;
}
}
{
}
{
return FALSE;
return TRUE;
}
static struct hash_node *
int check_existing)
{
unsigned int hash;
/* there may be holes, have to check everything */
return node;
}
/* a) primary hash */
table->nodes_count++;
return node;
}
if (check_existing) {
return node;
}
/* b) collisions list */
do {
break;
if (check_existing) {
return node;
}
/* resized table, try again */
}
struct collision_node, 1);
} else {
}
}
#ifdef DEBUG
#endif
table->nodes_count++;
}
{
}
{
}
unsigned int collision_idx,
unsigned int hash)
{
/* remove deleted nodes from the list */
#ifdef DEBUG
#endif
}
}
do {
/* if root is marked as deleted, replace it with first node
from list */
/* no collisions left - nothing to do */
return;
}
#ifdef DEBUG
#endif
}
/* see if node in primary table was deleted */
if (hash == 0)
#ifdef DEBUG
#endif
}
}
{
struct collision_node *cnode;
size_t i;
for (i = 0; i < table->collisions_size; i++) {
hash_compress(table, i, 0);
}
table->removed_count = 0;
}
{
unsigned int hash;
table->removed_count++;
else if (!hash_resize(table))
}
{
return table->nodes_count;
}
{
struct collision_node *cnode;
size_t i;
if (foreach_stop) {
return;
}
}
}
if (!foreach_stop) {
for (i = 0; i < table->collisions_size; i++) {
do {
context);
if (foreach_stop) {
return;
}
}
}
}
}
void hash_foreach_stop(void)
{
foreach_stop = TRUE;
}
{
}
{
return;
if (table->removed_count > 0) {
if (!hash_resize(table))
}
}
{
float nodes_per_list;
(nodes_per_list < 2.0))
return FALSE;
/* recreate primary table */
/* recreate collisions table */
table->nodes_count = 0;
table->removed_count = 0;
#ifdef DEBUG
table->collisions_count = 0;
#endif
/* move the data */
for (i = 0; i < old_size; i++) {
}
}
for (i = 0; i < old_csize; i++) {
cnode = &old_cnodes[i];
do {
}
}
return TRUE;
}
/* a char* hash function from ASU -- from glib */
unsigned int str_hash(const void *p)
{
const unsigned char *s = p;
unsigned int g, h = 0;
while (*s != '\0') {
h = (h << 4) + *s;
if ((g = h & 0xf0000000UL)) {
h = h ^ (g >> 24);
h = h ^ g;
}
s++;
}
return h;
}
/* a char* hash function from ASU -- from glib */
unsigned int strcase_hash(const void *p)
{
const unsigned char *s = p;
unsigned int g, h = 0;
while (*s != '\0') {
h = (h << 4) + i_toupper(*s);
if ((g = h & 0xf0000000UL)) {
h = h ^ (g >> 24);
h = h ^ g;
}
s++;
}
return h;
}