2006-08-20 16:25:41 +04:00
|
|
|
/*
|
|
|
|
* Copyright 2006 Rob Kendrick <rjek@rjek.com>
|
2006-10-13 19:09:30 +04:00
|
|
|
* Copyright 2006 Richard Wilson <info@tinct.net>
|
2007-08-08 20:16:03 +04:00
|
|
|
*
|
|
|
|
* This file is part of NetSurf, http://www.netsurf-browser.org/
|
|
|
|
*
|
|
|
|
* NetSurf 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; version 2 of the License.
|
|
|
|
*
|
|
|
|
* NetSurf 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.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2006-08-20 16:25:41 +04:00
|
|
|
*/
|
|
|
|
|
2006-08-20 20:02:22 +04:00
|
|
|
/** \file
|
|
|
|
* Write-Once hash table for string to string mappings */
|
2006-08-20 16:25:41 +04:00
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2006-08-20 17:46:30 +04:00
|
|
|
#include <stdbool.h>
|
2006-08-20 16:25:41 +04:00
|
|
|
#ifdef TEST_RIG
|
|
|
|
#include <assert.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#endif
|
2007-05-31 02:39:54 +04:00
|
|
|
#include "utils/hashtable.h"
|
|
|
|
#include "utils/log.h"
|
2006-08-20 16:25:41 +04:00
|
|
|
|
2006-10-13 19:09:30 +04:00
|
|
|
|
|
|
|
struct hash_entry {
|
2006-10-20 16:13:28 +04:00
|
|
|
char *pairing; /**< block containing '<key>\0<value>\0' */
|
|
|
|
unsigned int key_length; /**< length of key */
|
|
|
|
struct hash_entry *next; /**< next entry */
|
2006-10-13 19:09:30 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
struct hash_table {
|
|
|
|
unsigned int nchains;
|
|
|
|
struct hash_entry **chain;
|
|
|
|
};
|
|
|
|
|
2008-07-27 02:29:15 +04:00
|
|
|
/**
|
|
|
|
* Hash a string, returning a 32bit value. The hash algorithm used is
|
|
|
|
* Fowler Noll Vo - a very fast and simple hash, ideal for short strings.
|
|
|
|
* See http://en.wikipedia.org/wiki/Fowler_Noll_Vo_hash for more details.
|
|
|
|
*
|
|
|
|
* \param datum The string to hash.
|
|
|
|
* \param len Pointer to unsigned integer to record datum's length in.
|
|
|
|
* \return The calculated hash value for the datum.
|
|
|
|
*/
|
|
|
|
|
|
|
|
static inline unsigned int hash_string_fnv(const char *datum, unsigned int *len)
|
|
|
|
{
|
|
|
|
unsigned int z = 0x01000193;
|
|
|
|
const char *start = datum;
|
|
|
|
*len = 0;
|
|
|
|
|
|
|
|
if (datum == NULL)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
while (*datum) {
|
|
|
|
z *= 0x01000193;
|
|
|
|
z ^= *datum++;
|
|
|
|
}
|
|
|
|
*len = datum - start;
|
|
|
|
|
|
|
|
return z;
|
|
|
|
}
|
|
|
|
|
2006-10-13 19:09:30 +04:00
|
|
|
|
2006-08-21 20:51:39 +04:00
|
|
|
/**
|
|
|
|
* Create a new hash table, and return a context for it. The memory consumption
|
2006-10-20 16:13:28 +04:00
|
|
|
* of a hash table is approximately 8 + (nchains * 12) bytes if it is empty.
|
2006-08-21 20:51:39 +04:00
|
|
|
*
|
|
|
|
* \param chains Number of chains/buckets this hash table will have. This
|
2006-10-20 16:13:28 +04:00
|
|
|
* should be a prime number, and ideally a prime number just
|
|
|
|
* over a power of two, for best performance and distribution.
|
2006-08-21 20:51:39 +04:00
|
|
|
* \return struct hash_table containing the context of this hash table or NULL
|
2006-10-20 16:13:28 +04:00
|
|
|
* if there is insufficent memory to create it and its chains.
|
2006-08-21 20:51:39 +04:00
|
|
|
*/
|
2006-10-12 18:00:40 +04:00
|
|
|
|
2006-08-20 16:25:41 +04:00
|
|
|
struct hash_table *hash_create(unsigned int chains)
|
|
|
|
{
|
2006-08-20 20:02:22 +04:00
|
|
|
struct hash_table *r = malloc(sizeof(struct hash_table));
|
2006-08-20 16:25:41 +04:00
|
|
|
|
|
|
|
if (r == NULL) {
|
|
|
|
LOG(("Not enough memory for hash table."));
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
r->nchains = chains;
|
2006-08-20 20:02:22 +04:00
|
|
|
r->chain = calloc(chains, sizeof(struct hash_entry));
|
2006-08-20 16:25:41 +04:00
|
|
|
|
|
|
|
if (r->chain == NULL) {
|
|
|
|
LOG(("Not enough memory for %d hash table chains.", chains));
|
|
|
|
free(r);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2006-08-21 20:51:39 +04:00
|
|
|
/**
|
|
|
|
* Destroys a hash table, freeing all memory associated with it.
|
|
|
|
*
|
2006-10-20 16:13:28 +04:00
|
|
|
* \param ht Hash table to destroy. After the function returns, this
|
|
|
|
* will nolonger be valid.
|
2006-08-21 20:51:39 +04:00
|
|
|
*/
|
|
|
|
|
2006-08-20 16:25:41 +04:00
|
|
|
void hash_destroy(struct hash_table *ht)
|
|
|
|
{
|
2006-08-20 20:02:22 +04:00
|
|
|
unsigned int i;
|
2006-08-20 16:25:41 +04:00
|
|
|
|
2006-10-12 18:00:40 +04:00
|
|
|
if (ht == NULL)
|
|
|
|
return;
|
|
|
|
|
2006-08-20 16:25:41 +04:00
|
|
|
for (i = 0; i < ht->nchains; i++) {
|
|
|
|
if (ht->chain[i] != NULL) {
|
|
|
|
struct hash_entry *e = ht->chain[i];
|
|
|
|
while (e) {
|
2006-10-12 18:00:40 +04:00
|
|
|
struct hash_entry *n = e->next;
|
2006-10-20 16:13:28 +04:00
|
|
|
free(e->pairing);
|
2006-08-20 16:25:41 +04:00
|
|
|
free(e);
|
|
|
|
e = n;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
free(ht->chain);
|
|
|
|
free(ht);
|
|
|
|
}
|
|
|
|
|
2006-08-21 20:51:39 +04:00
|
|
|
/**
|
|
|
|
* Adds a key/value pair to a hash table. If the key you're adding is already
|
|
|
|
* in the hash table, it does not replace it, but it does take precedent over
|
|
|
|
* it. The old key/value pair will be inaccessable but still in memory until
|
|
|
|
* hash_destroy() is called on the hash table.
|
|
|
|
*
|
2006-10-20 16:13:28 +04:00
|
|
|
* \param ht The hash table context to add the key/value pair to.
|
|
|
|
* \param key The key to associate the value with. A copy is made.
|
2006-08-21 20:51:39 +04:00
|
|
|
* \param value The value to associate the key with. A copy is made.
|
|
|
|
* \return true if the add succeeded, false otherwise. (Failure most likely
|
2006-10-20 16:13:28 +04:00
|
|
|
* indicates insufficent memory to make copies of the key and value.
|
2006-08-21 20:51:39 +04:00
|
|
|
*/
|
|
|
|
|
2006-08-20 17:46:30 +04:00
|
|
|
bool hash_add(struct hash_table *ht, const char *key, const char *value)
|
2006-08-20 16:25:41 +04:00
|
|
|
{
|
2006-10-20 16:13:28 +04:00
|
|
|
unsigned int h, c, v;
|
|
|
|
struct hash_entry *e;
|
2006-10-12 18:00:40 +04:00
|
|
|
|
|
|
|
if (ht == NULL || key == NULL || value == NULL)
|
|
|
|
return false;
|
|
|
|
|
2006-10-20 16:13:28 +04:00
|
|
|
e = malloc(sizeof(struct hash_entry));
|
2006-08-20 20:02:22 +04:00
|
|
|
if (e == NULL) {
|
|
|
|
LOG(("Not enough memory for hash entry."));
|
|
|
|
return false;
|
|
|
|
}
|
2006-08-20 16:25:41 +04:00
|
|
|
|
2006-10-13 19:50:11 +04:00
|
|
|
h = hash_string_fnv(key, &(e->key_length));
|
2006-10-12 18:00:40 +04:00
|
|
|
c = h % ht->nchains;
|
2006-11-27 18:35:18 +03:00
|
|
|
|
2006-10-20 16:13:28 +04:00
|
|
|
v = strlen(value) ;
|
|
|
|
e->pairing = malloc(v + e->key_length + 2);
|
|
|
|
if (e->pairing == NULL) {
|
|
|
|
LOG(("Not enough memory for string duplication."));
|
2006-08-20 17:46:30 +04:00
|
|
|
free(e);
|
|
|
|
return false;
|
|
|
|
}
|
2006-10-20 16:13:28 +04:00
|
|
|
memcpy(e->pairing, key, e->key_length + 1);
|
|
|
|
memcpy(e->pairing + e->key_length + 1, value, v + 1);
|
2006-08-20 17:46:30 +04:00
|
|
|
|
2006-08-20 16:25:41 +04:00
|
|
|
e->next = ht->chain[c];
|
|
|
|
ht->chain[c] = e;
|
2006-08-20 17:46:30 +04:00
|
|
|
|
|
|
|
return true;
|
2006-08-20 16:25:41 +04:00
|
|
|
}
|
|
|
|
|
2006-08-21 20:51:39 +04:00
|
|
|
/**
|
|
|
|
* Looks up a the value associated with with a key from a specific hash table.
|
|
|
|
*
|
2006-10-20 16:13:28 +04:00
|
|
|
* \param ht The hash table context to look up the key in.
|
|
|
|
* \param key The key to search for.
|
2006-08-21 20:51:39 +04:00
|
|
|
* \return The value associated with the key, or NULL if it was not found.
|
|
|
|
*/
|
|
|
|
|
2006-08-20 16:25:41 +04:00
|
|
|
const char *hash_get(struct hash_table *ht, const char *key)
|
|
|
|
{
|
2006-10-20 16:13:28 +04:00
|
|
|
unsigned int h, c, key_length;
|
2006-10-12 18:00:40 +04:00
|
|
|
struct hash_entry *e;
|
|
|
|
|
|
|
|
if (ht == NULL || key == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
2006-10-13 19:50:11 +04:00
|
|
|
h = hash_string_fnv(key, &key_length);
|
2006-10-12 18:00:40 +04:00
|
|
|
c = h % ht->nchains;
|
|
|
|
|
2006-10-13 19:09:30 +04:00
|
|
|
for (e = ht->chain[c]; e; e = e->next)
|
2006-10-20 16:13:28 +04:00
|
|
|
if ((key_length == e->key_length) &&
|
|
|
|
(memcmp(key, e->pairing, key_length) == 0))
|
|
|
|
return e->pairing + key_length + 1;
|
2006-08-20 16:25:41 +04:00
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2006-10-20 16:13:28 +04:00
|
|
|
/**
|
|
|
|
* Iterate through all available hash keys.
|
|
|
|
*
|
|
|
|
* \param ht The hash table context to iterate.
|
|
|
|
* \param c1 Pointer to first context
|
|
|
|
* \param c2 Pointer to second context (set to 0 on first call)
|
|
|
|
* \return The next hash key, or NULL for no more keys
|
|
|
|
*/
|
|
|
|
|
|
|
|
const char *hash_iterate(struct hash_table *ht, unsigned int *c1, unsigned int **c2) {
|
|
|
|
struct hash_entry **he = (struct hash_entry **)c2;
|
|
|
|
|
|
|
|
if (ht == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (!*he)
|
|
|
|
*c1 = -1;
|
|
|
|
else
|
|
|
|
*he = (*he)->next;
|
|
|
|
|
|
|
|
if (*he)
|
|
|
|
return (*he)->pairing;
|
|
|
|
|
|
|
|
while (!*he) {
|
|
|
|
(*c1)++;
|
|
|
|
if (*c1 >= ht->nchains)
|
|
|
|
return NULL;
|
|
|
|
*he = ht->chain[*c1];
|
|
|
|
}
|
|
|
|
return (*he)->pairing;
|
|
|
|
}
|
|
|
|
|
2006-08-21 20:51:39 +04:00
|
|
|
/* A simple test rig. To compile, use:
|
|
|
|
* gcc -o hashtest -I../ -DTEST_RIG utils/hashtable.c
|
|
|
|
*
|
|
|
|
* If you make changes to this hash table implementation, please rerun this
|
|
|
|
* test, and if possible, through valgrind to make sure there are no memory
|
2006-10-13 19:50:11 +04:00
|
|
|
* leaks or invalid memory accesses. If you add new functionality, please
|
2006-08-21 20:51:39 +04:00
|
|
|
* include a test for it that has good coverage along side the other tests.
|
|
|
|
*/
|
|
|
|
|
2006-08-20 16:25:41 +04:00
|
|
|
#ifdef TEST_RIG
|
|
|
|
|
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
struct hash_table *a, *b;
|
|
|
|
FILE *dict;
|
|
|
|
char keybuf[BUFSIZ], valbuf[BUFSIZ];
|
2006-10-13 19:50:11 +04:00
|
|
|
int i;
|
2006-08-20 16:25:41 +04:00
|
|
|
|
|
|
|
a = hash_create(79);
|
|
|
|
assert(a != NULL);
|
|
|
|
|
|
|
|
b = hash_create(103);
|
|
|
|
assert(b != NULL);
|
|
|
|
|
|
|
|
hash_add(a, "cow", "moo");
|
|
|
|
hash_add(b, "moo", "cow");
|
|
|
|
|
|
|
|
hash_add(a, "pig", "oink");
|
|
|
|
hash_add(b, "oink", "pig");
|
|
|
|
|
|
|
|
hash_add(a, "chicken", "cluck");
|
|
|
|
hash_add(b, "cluck", "chicken");
|
|
|
|
|
|
|
|
hash_add(a, "dog", "woof");
|
|
|
|
hash_add(b, "woof", "dog");
|
|
|
|
|
|
|
|
hash_add(a, "cat", "meow");
|
|
|
|
hash_add(b, "meow", "cat");
|
|
|
|
|
|
|
|
#define MATCH(x,y) assert(!strcmp(hash_get(a, x), y)); assert(!strcmp(hash_get(b, y), x))
|
|
|
|
MATCH("cow", "moo");
|
|
|
|
MATCH("pig", "oink");
|
|
|
|
MATCH("chicken", "cluck");
|
|
|
|
MATCH("dog", "woof");
|
|
|
|
MATCH("cat", "meow");
|
|
|
|
|
|
|
|
hash_destroy(a);
|
|
|
|
hash_destroy(b);
|
|
|
|
|
|
|
|
/* this test requires /usr/share/dict/words - a large list of English
|
|
|
|
* words. We load the entire file - odd lines are used as keys, and
|
|
|
|
* even lines are used as the values for the previous line. we then
|
|
|
|
* work through it again making sure everything matches.
|
|
|
|
*
|
|
|
|
* We do this twice - once in a hash table with many chains, and once
|
|
|
|
* with a hash table with fewer chains.
|
|
|
|
*/
|
|
|
|
|
|
|
|
a = hash_create(1031);
|
|
|
|
b = hash_create(7919);
|
2006-10-12 18:00:40 +04:00
|
|
|
|
2006-08-20 16:25:41 +04:00
|
|
|
dict = fopen("/usr/share/dict/words", "r");
|
|
|
|
if (dict == NULL) {
|
|
|
|
fprintf(stderr, "Unable to open /usr/share/dict/words - extensive testing skipped.\n");
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
while (!feof(dict)) {
|
|
|
|
fscanf(dict, "%s", keybuf);
|
|
|
|
fscanf(dict, "%s", valbuf);
|
|
|
|
hash_add(a, keybuf, valbuf);
|
|
|
|
hash_add(b, keybuf, valbuf);
|
|
|
|
}
|
|
|
|
|
2006-10-13 19:50:11 +04:00
|
|
|
for (i = 0; i < 5; i++) {
|
|
|
|
fseek(dict, 0, SEEK_SET);
|
2006-08-20 16:25:41 +04:00
|
|
|
|
2006-10-13 19:50:11 +04:00
|
|
|
while (!feof(dict)) {
|
|
|
|
fscanf(dict, "%s", keybuf);
|
|
|
|
fscanf(dict, "%s", valbuf);
|
|
|
|
assert(strcmp(hash_get(a, keybuf), valbuf) == 0);
|
|
|
|
assert(strcmp(hash_get(b, keybuf), valbuf) == 0);
|
|
|
|
}
|
2006-08-20 16:25:41 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
hash_destroy(a);
|
|
|
|
hash_destroy(b);
|
|
|
|
|
|
|
|
fclose(dict);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|