*** empty log message ***

This commit is contained in:
fc_botelho
2008-03-26 20:26:48 +00:00
parent 2665414b4e
commit c8a1c4fef9
25 changed files with 1875 additions and 88 deletions

101
src/chm.c
View File

@@ -292,3 +292,104 @@ void chm_destroy(cmph_t *mphf)
free(data);
free(mphf);
}
/** cmph_uint32 chm_search_fingerprint(cmph_t *mphf, const char *key, cmph_uint32 keylen, cmph_uint32 * fingerprint);
* \brief Computes the mphf value and a fingerprint of 12 bytes (i.e., figerprint should be a prealocated area to fit three 4-byte integers).
* \param mphf pointer to the resulting function
* \param key is the key to be hashed
* \param keylen is the key legth in bytes
* \return The mphf value
*
* Computes the mphf value and a fingerprint of 12 bytes. The figerprint pointer should be
* a prealocated area to fit three 4-byte integers. You don't need to use all the 12 bytes
* as fingerprint. According to the application, just few bits can be enough, once mphf does
* not allow collisions for the keys previously known.
*/
cmph_uint32 chm_search_fingerprint(cmph_t *mphf, const char *key, cmph_uint32 keylen, cmph_uint32 * fingerprint)
{
chm_data_t *chm = mphf->data;
cmph_uint32 h1, h2;
hash_vector(chm->hashes[0], key, keylen, fingerprint);
h1 = fingerprint[2] % chm->n;
hash_vector(chm->hashes[1], key, keylen, fingerprint);
h2 = fingerprint[2] % chm->n;
DEBUGP("key: %s h1: %u h2: %u\n", key, h1, h2);
if (h1 == h2 && ++h2 >= chm->n) h2 = 0;
DEBUGP("key: %s g[h1]: %u g[h2]: %u edges: %u\n", key, chm->g[h1], chm->g[h2], chm->m);
return (chm->g[h1] + chm->g[h2]) % chm->m;
}
/** \fn void chm_pack(cmph_t *mphf, void *packed_mphf);
* \brief Support the ability to pack a perfect hash function into a preallocated contiguous memory space pointed by packed_mphf.
* \param mphf pointer to the resulting mphf
* \param packed_mphf pointer to the contiguous memory area used to store the resulting mphf. The size of packed_mphf must be at least cmph_packed_size()
*/
void chm_pack(cmph_t *mphf, void *packed_mphf)
{
chm_data_t *data = (chm_data_t *)mphf->data;
cmph_uint32 * ptr = packed_mphf;
// packing h1
hash_state_pack(data->hashes[0], ptr);
ptr += (hash_state_packed_size(data->hashes[0]) >> 2); // (hash_state_packed_size(data->hashes[0]) / 4);
// packing h2
hash_state_pack(data->hashes[1], ptr);
ptr += (hash_state_packed_size(data->hashes[1]) >> 2); // (hash_state_packed_size(data->hashes[1]) / 4);
// packing n
*ptr++ = data->n;
// packing m
*ptr++ = data->m;
// packing g
memcpy(ptr, data->g, sizeof(cmph_uint32)*data->n);
}
/** \fn cmph_uint32 chm_packed_size(cmph_t *mphf);
* \brief Return the amount of space needed to pack mphf.
* \param mphf pointer to a mphf
* \return the size of the packed function or zero for failures
*/
cmph_uint32 chm_packed_size(cmph_t *mphf)
{
chm_data_t *data = (chm_data_t *)mphf->data;
return (sizeof(CMPH_ALGO) + 2*hash_state_packed_size(data->hashes[0]) + 2*sizeof(cmph_uint32) + sizeof(cmph_uint32)*data->n);
}
/** cmph_uint32 chm_search(void *packed_mphf, const char *key, cmph_uint32 keylen);
* \brief Use the packed mphf to do a search.
* \param packed_mphf pointer to the packed mphf
* \param key key to be hashed
* \param keylen key legth in bytes
* \return The mphf value
*/
cmph_uint32 chm_search_packed(void *packed_mphf, const char *key, cmph_uint32 keylen)
{
register cmph_uint32 *h1_ptr = (cmph_uint32 *)packed_mphf;
register cmph_uint32 h1_size = *h1_ptr;
// fprintf(stderr, "h1_size:%u\n", h1_size);
register cmph_uint32 *h2_ptr = h1_ptr + (h1_size >> 2); // h1_ptr + h1_size/4
register cmph_uint32 h2_size = *h2_ptr;
// fprintf(stderr, "h2_size:%u\n", h2_size);
register cmph_uint32 *g_ptr = h2_ptr + (h2_size >> 2); // h2_ptr + h2_size/4
register cmph_uint32 n = *g_ptr++;
register cmph_uint32 m = *g_ptr++;
register cmph_uint32 h1 = hash_packed(h1_ptr, key, keylen) % n;
register cmph_uint32 h2 = hash_packed(h2_ptr, key, keylen) % n;
DEBUGP("key: %s h1: %u h2: %u\n", key, h1, h2);
if (h1 == h2 && ++h2 >= n) h2 = 0;
DEBUGP("key: %s g[h1]: %u g[h2]: %u edges: %u\n", key, g_ptr[h1], g_ptr[h2], m);
return (g_ptr[h1] + g_ptr[h2]) % m;
}