hash.c revision 021bbf3ac22e1b6872242576c775affeb1e1d83f
/*
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.
*/
/* @UNSAFE: whole file */
#include "lib.h"
#include "hash.h"
#include "primes.h"
#include <ctype.h>
#define HASH_TABLE_MIN_SIZE 109
struct hash_node {
void *key;
void *value;
};
struct hash_table {
int frozen;
struct hash_node *free_nodes;
};
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 *
{
struct hash_table *table;
return table;
}
{
else {
}
}
{
}
}
{
size_t i;
}
}
{
}
}
{
if (free_nodes) {
}
table->nodes_count = 0;
table->removed_count = 0;
}
static struct hash_node *
{
do {
return node;
}
return NULL;
}
{
}
{
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 node */
table->nodes_count++;
return node;
}
if (check_existing) {
return node;
}
}
/* b) collisions list */
break;
if (check_existing) {
return node;
}
}
}
/* resized table, try again */
}
else {
}
}
table->nodes_count++;
return node;
}
{
}
{
}
{
/* remove deleted nodes from the list */
} else {
}
}
/* update root */
}
}
{
size_t i;
table->removed_count = 0;
}
{
unsigned int hash;
i_panic("key not found from hash");
table->nodes_count--;
table->removed_count++;
else if (!hash_resize(table))
}
{
return table->nodes_count;
}
void *context)
{
size_t i;
do {
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 */
table->nodes_count = 0;
table->removed_count = 0;
/* move the data */
for (i = 0; i < old_size; i++) {
}
}
}
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;
}