1
Fork 0
turbonss/src/hash.c

215 lines
5.6 KiB
C
Raw Normal View History

2004-12-23 15:16:30 +02:00
#include "hash_state.h"
#include <stdlib.h>
#include <assert.h>
#include <limits.h>
#include <string.h>
//#define DEBUG
#include "debug.h"
2008-03-23 02:46:34 +02:00
const char *cmph_hash_names[] = { "jenkins", NULL };
2004-12-23 15:16:30 +02:00
hash_state_t *hash_state_new(CMPH_HASH hashfunc, cmph_uint32 hashsize)
2004-12-23 15:16:30 +02:00
{
hash_state_t *state = NULL;
2004-12-23 15:16:30 +02:00
switch (hashfunc)
{
2005-01-18 23:06:08 +02:00
case CMPH_HASH_JENKINS:
2004-12-23 15:16:30 +02:00
DEBUGP("Jenkins function - %u\n", hashsize);
state = (hash_state_t *)jenkins_state_new(hashsize);
2004-12-23 15:16:30 +02:00
DEBUGP("Jenkins function created\n");
break;
default:
assert(0);
}
state->hashfunc = hashfunc;
return state;
}
cmph_uint32 hash(hash_state_t *state, const char *key, cmph_uint32 keylen)
2004-12-23 15:16:30 +02:00
{
switch (state->hashfunc)
{
2005-01-18 23:06:08 +02:00
case CMPH_HASH_JENKINS:
return jenkins_hash((jenkins_state_t *)state, key, keylen);
2004-12-23 15:16:30 +02:00
default:
assert(0);
}
assert(0);
return 0;
}
2008-03-23 02:46:34 +02:00
void hash_vector(hash_state_t *state, const char *key, cmph_uint32 keylen, cmph_uint32 * hashes)
{
switch (state->hashfunc)
{
case CMPH_HASH_JENKINS:
2008-03-26 22:26:48 +02:00
jenkins_hash_vector_((jenkins_state_t *)state, key, keylen, hashes);
2008-03-23 02:46:34 +02:00
break;
default:
assert(0);
}
}
void hash_state_dump(hash_state_t *state, char **buf, cmph_uint32 *buflen)
2004-12-23 15:16:30 +02:00
{
char *algobuf;
switch (state->hashfunc)
{
2005-01-18 23:06:08 +02:00
case CMPH_HASH_JENKINS:
jenkins_state_dump((jenkins_state_t *)state, &algobuf, buflen);
2004-12-23 15:16:30 +02:00
if (*buflen == UINT_MAX) return;
break;
default:
assert(0);
}
*buf = (char *)malloc(strlen(cmph_hash_names[state->hashfunc]) + 1 + *buflen);
2005-01-18 23:06:08 +02:00
memcpy(*buf, cmph_hash_names[state->hashfunc], strlen(cmph_hash_names[state->hashfunc]) + 1);
DEBUGP("Algobuf is %u\n", *(cmph_uint32 *)algobuf);
memcpy(*buf + strlen(cmph_hash_names[state->hashfunc]) + 1, algobuf, *buflen);
*buflen = (cmph_uint32)strlen(cmph_hash_names[state->hashfunc]) + 1 + *buflen;
2004-12-23 15:16:30 +02:00
free(algobuf);
return;
}
hash_state_t * hash_state_copy(hash_state_t *src_state)
{
hash_state_t *dest_state = NULL;
switch (src_state->hashfunc)
{
case CMPH_HASH_JENKINS:
dest_state = (hash_state_t *)jenkins_state_copy((jenkins_state_t *)src_state);
break;
default:
assert(0);
}
dest_state->hashfunc = src_state->hashfunc;
return dest_state;
}
hash_state_t *hash_state_load(const char *buf, cmph_uint32 buflen)
2004-12-23 15:16:30 +02:00
{
2005-01-18 23:06:08 +02:00
cmph_uint32 i;
cmph_uint32 offset;
CMPH_HASH hashfunc = CMPH_HASH_COUNT;
for (i = 0; i < CMPH_HASH_COUNT; ++i)
2004-12-23 15:16:30 +02:00
{
2005-01-18 23:06:08 +02:00
if (strcmp(buf, cmph_hash_names[i]) == 0)
2004-12-23 15:16:30 +02:00
{
hashfunc = i;
break;
}
}
2005-01-18 23:06:08 +02:00
if (hashfunc == CMPH_HASH_COUNT) return NULL;
offset = (cmph_uint32)strlen(cmph_hash_names[hashfunc]) + 1;
2004-12-23 15:16:30 +02:00
switch (hashfunc)
{
2005-01-18 23:06:08 +02:00
case CMPH_HASH_JENKINS:
return (hash_state_t *)jenkins_state_load(buf + offset, buflen - offset);
2004-12-23 15:16:30 +02:00
default:
return NULL;
}
return NULL;
}
void hash_state_destroy(hash_state_t *state)
2004-12-23 15:16:30 +02:00
{
switch (state->hashfunc)
{
2005-01-18 23:06:08 +02:00
case CMPH_HASH_JENKINS:
jenkins_state_destroy((jenkins_state_t *)state);
2004-12-23 15:16:30 +02:00
break;
default:
assert(0);
}
return;
}
2008-03-26 22:26:48 +02:00
2008-03-29 03:48:15 +02:00
/** \fn void hash_state_pack(hash_state_t *state, void *hash_packed)
2008-03-26 22:26:48 +02:00
* \brief Support the ability to pack a hash function into a preallocated contiguous memory space pointed by hash_packed.
* \param state points to the hash function
2008-03-29 03:48:15 +02:00
* \param hash_packed pointer to the contiguous memory area used to store the hash function. The size of hash_packed must be at least hash_state_packed_size()
*
* Support the ability to pack a hash function into a preallocated contiguous memory space pointed by hash_packed.
* However, the hash function type must be packed outside.
2008-03-26 22:26:48 +02:00
*/
void hash_state_pack(hash_state_t *state, void *hash_packed)
{
switch (state->hashfunc)
{
case CMPH_HASH_JENKINS:
// pack the jenkins hash function
2008-03-29 03:48:15 +02:00
jenkins_state_pack((jenkins_state_t *)state, hash_packed);
2008-03-26 22:26:48 +02:00
break;
default:
assert(0);
}
return;
}
2008-03-29 03:48:15 +02:00
/** \fn cmph_uint32 hash_state_packed_size(CMPH_HASH hashfunc)
2008-03-26 22:26:48 +02:00
* \brief Return the amount of space needed to pack a hash function.
2008-03-29 03:48:15 +02:00
* \param hashfunc function type
2008-03-26 22:26:48 +02:00
* \return the size of the packed function or zero for failures
*/
2008-03-29 03:48:15 +02:00
cmph_uint32 hash_state_packed_size(CMPH_HASH hashfunc)
2008-03-26 22:26:48 +02:00
{
2008-03-29 03:48:15 +02:00
cmph_uint32 size = 0;
switch (hashfunc)
2008-03-26 22:26:48 +02:00
{
case CMPH_HASH_JENKINS:
size += jenkins_state_packed_size();
break;
default:
assert(0);
}
return size;
}
2008-03-29 03:48:15 +02:00
/** \fn cmph_uint32 hash_packed(void *hash_packed, CMPH_HASH hashfunc, const char *k, cmph_uint32 keylen)
2008-03-26 22:26:48 +02:00
* \param hash_packed is a pointer to a contiguous memory area
2008-03-29 03:48:15 +02:00
* \param hashfunc is the type of the hash function packed in hash_packed
2008-03-26 22:26:48 +02:00
* \param key is a pointer to a key
* \param keylen is the key length
* \return an integer that represents a hash value of 32 bits.
*/
2008-03-29 03:48:15 +02:00
cmph_uint32 hash_packed(void *hash_packed, CMPH_HASH hashfunc, const char *k, cmph_uint32 keylen)
2008-03-26 22:26:48 +02:00
{
switch (hashfunc)
{
case CMPH_HASH_JENKINS:
2008-03-29 03:48:15 +02:00
return jenkins_hash_packed(hash_packed, k, keylen);
2008-03-26 22:26:48 +02:00
default:
assert(0);
}
assert(0);
return 0;
}
2008-03-29 03:48:15 +02:00
/** \fn hash_vector_packed(void *hash_packed, CMPH_HASH hashfunc, const char *k, cmph_uint32 keylen, cmph_uint32 * hashes)
2008-03-26 22:26:48 +02:00
* \param hash_packed is a pointer to a contiguous memory area
* \param key is a pointer to a key
* \param keylen is the key length
* \param hashes is a pointer to a memory large enough to fit three 32-bit integers.
*/
2008-03-29 03:48:15 +02:00
void hash_vector_packed(void *hash_packed, CMPH_HASH hashfunc, const char *k, cmph_uint32 keylen, cmph_uint32 * hashes)
{
2008-03-26 22:26:48 +02:00
switch (hashfunc)
{
case CMPH_HASH_JENKINS:
2008-03-29 03:48:15 +02:00
jenkins_hash_vector_packed(hash_packed, k, keylen, hashes);
2008-03-26 22:26:48 +02:00
break;
default:
assert(0);
}
}
2008-03-29 03:48:15 +02:00
/** \fn CMPH_HASH hash_get_type(hash_state_t *state);
* \param state is a pointer to a hash_state_t structure
* \return the hash function type pointed by state
*/
CMPH_HASH hash_get_type(hash_state_t *state)
{
return state->hashfunc;
}