2012-09-05 10:44:41 +00:00
|
|
|
/*
|
|
|
|
hash.c -- hash table management
|
2013-08-13 18:38:57 +00:00
|
|
|
Copyright (C) 2012-2013 Guus Sliepen <guus@tinc-vpn.org>
|
2012-09-05 10:44:41 +00: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 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
|
|
|
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, MA 02110-1301 USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "system.h"
|
|
|
|
|
|
|
|
#include "hash.h"
|
|
|
|
#include "xalloc.h"
|
|
|
|
|
|
|
|
/* Generic hash function */
|
|
|
|
|
|
|
|
static uint32_t hash_function(const void *p, size_t len) {
|
|
|
|
const uint8_t *q = p;
|
|
|
|
uint32_t hash = 0;
|
2012-10-09 15:49:09 +00:00
|
|
|
while(true) {
|
2012-09-05 10:44:41 +00:00
|
|
|
for(int i = len > 4 ? 4 : len; --i;)
|
2013-07-20 13:50:28 +00:00
|
|
|
hash += q[len - i] << (8 * i);
|
2012-09-05 10:44:41 +00:00
|
|
|
hash *= 0x9e370001UL; // Golden ratio prime.
|
2012-10-09 15:49:09 +00:00
|
|
|
if(len <= 4)
|
|
|
|
break;
|
2012-09-05 10:44:41 +00:00
|
|
|
len -= 4;
|
|
|
|
}
|
|
|
|
return hash;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Map 32 bits int onto 0..n-1, without throwing away too many bits if n is 2^8 or 2^16 */
|
|
|
|
|
|
|
|
static uint32_t modulo(uint32_t hash, size_t n) {
|
|
|
|
if(n == 0x100)
|
|
|
|
return (hash >> 24) ^ ((hash >> 16) & 0xff) ^ ((hash >> 8) & 0xff) ^ (hash & 0xff);
|
|
|
|
else if(n == 0x10000)
|
|
|
|
return (hash >> 16) ^ (hash & 0xffff);
|
|
|
|
else
|
|
|
|
return hash % n;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* (De)allocation */
|
|
|
|
|
|
|
|
hash_t *hash_alloc(size_t n, size_t size) {
|
2013-05-01 15:31:33 +00:00
|
|
|
hash_t *hash = xzalloc(sizeof *hash);
|
2012-09-05 10:44:41 +00:00
|
|
|
hash->n = n;
|
|
|
|
hash->size = size;
|
2013-05-01 15:31:33 +00:00
|
|
|
hash->keys = xzalloc(hash->n * hash->size);
|
|
|
|
hash->values = xzalloc(hash->n * sizeof *hash->values);
|
2012-09-05 10:44:41 +00:00
|
|
|
return hash;
|
|
|
|
}
|
|
|
|
|
|
|
|
void hash_free(hash_t *hash) {
|
|
|
|
free(hash->keys);
|
|
|
|
free(hash->values);
|
|
|
|
free(hash);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Searching and inserting */
|
|
|
|
|
|
|
|
void hash_insert(hash_t *hash, const void *key, const void *value) {
|
|
|
|
uint32_t i = modulo(hash_function(key, hash->size), hash->n);
|
|
|
|
memcpy(hash->keys + i * hash->size, key, hash->size);
|
|
|
|
hash->values[i] = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
void *hash_search(const hash_t *hash, const void *key) {
|
|
|
|
uint32_t i = modulo(hash_function(key, hash->size), hash->n);
|
|
|
|
if(hash->values[i] && !memcmp(key, hash->keys + i * hash->size, hash->size)) {
|
|
|
|
return (void *)hash->values[i];
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
void *hash_search_or_insert(hash_t *hash, const void *key, const void *value) {
|
|
|
|
uint32_t i = modulo(hash_function(key, hash->size), hash->n);
|
|
|
|
if(hash->values[i] && !memcmp(key, hash->keys + i * hash->size, hash->size))
|
|
|
|
return (void *)hash->values[i];
|
|
|
|
memcpy(hash->keys + i * hash->size, key, hash->size);
|
|
|
|
hash->values[i] = value;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Utility functions */
|
|
|
|
|
|
|
|
void hash_clear(hash_t *hash) {
|
|
|
|
memset(hash->values, 0, hash->n * sizeof *hash->values);
|
|
|
|
}
|
|
|
|
|
|
|
|
void hash_resize(hash_t *hash, size_t n) {
|
|
|
|
hash->keys = xrealloc(hash->keys, n * hash->size);
|
|
|
|
hash->values = xrealloc(hash->values, n * sizeof *hash->values);
|
2012-11-29 13:35:08 +00:00
|
|
|
if(n > hash->n) {
|
|
|
|
memset(hash->keys + hash->n * hash->size, 0, (n - hash->n) * hash->size);
|
2012-09-05 10:44:41 +00:00
|
|
|
memset(hash->values + hash->n, 0, (n - hash->n) * sizeof *hash->values);
|
2012-11-29 13:35:08 +00:00
|
|
|
}
|
2012-09-05 10:44:41 +00:00
|
|
|
}
|