static fch_buckets_t * mapping(cmph_config_t *mph) { cmph_uint32 i = 0; fch_buckets_t *buckets = NULL; fch_config_data_t *fch = (fch_config_data_t *)mph->data; if (fch->h1) hash_state_destroy(fch->h1); fch->h1 = hash_state_new(fch->hashfuncs[0], fch->m); fch->b = fch_calc_b(fch->c, fch->m); fch->p1 = fch_calc_p1(fch->m); fch->p2 = fch_calc_p2(fch->b); //DEBUGP("b:%u p1:%f p2:%f\n", fch->b, fch->p1, fch->p2); buckets = fch_buckets_new(fch->b); mph->key_source->rewind(mph->key_source->data); for(i = 0; i < fch->m; i++) { cmph_uint32 h1, keylen; char *key = NULL; mph->key_source->read(mph->key_source->data, &key, &keylen); h1 = hash(fch->h1, key, keylen) % fch->m; h1 = mixh10h11h12 (fch->b, fch->p1, fch->p2, h1); fch_buckets_insert(buckets, h1, key, keylen); key = NULL; // transger memory ownership } return buckets; }
static cmph_uint32 brz_fch_search_packed(cmph_uint32 *packed_mphf, const char *key, cmph_uint32 keylen, cmph_uint32 * fingerprint) { register CMPH_HASH h0_type = *packed_mphf++; register cmph_uint32 *h0_ptr = packed_mphf; packed_mphf = (cmph_uint32 *)(((cmph_uint8 *)packed_mphf) + hash_state_packed_size(h0_type)); register cmph_uint32 k = *packed_mphf++; register double c = (double)(*((cmph_uint64*)packed_mphf)); packed_mphf += 2; register CMPH_HASH h1_type = *packed_mphf++; register CMPH_HASH h2_type = *packed_mphf++; register cmph_uint8 * size = (cmph_uint8 *) packed_mphf; packed_mphf = (cmph_uint32 *)(size + k); register cmph_uint32 * offset = packed_mphf; packed_mphf += k; register cmph_uint32 h0; hash_vector_packed(h0_ptr, h0_type, key, keylen, fingerprint); h0 = fingerprint[2] % k; register cmph_uint32 m = size[h0]; register cmph_uint32 b = fch_calc_b(c, m); register double p1 = fch_calc_p1(m); register double p2 = fch_calc_p2(b); #if defined (__ia64) || defined (__x86_64__) register cmph_uint64 * g_is_ptr = (cmph_uint64 *)packed_mphf; #else register cmph_uint32 * g_is_ptr = packed_mphf; #endif register cmph_uint8 * h1_ptr = (cmph_uint8 *) g_is_ptr[h0]; register cmph_uint8 * h2_ptr = h1_ptr + hash_state_packed_size(h1_type); register cmph_uint8 * g = h2_ptr + hash_state_packed_size(h2_type); register cmph_uint32 h1 = hash_packed(h1_ptr, h1_type, key, keylen) % m; register cmph_uint32 h2 = hash_packed(h2_ptr, h2_type, key, keylen) % m; register cmph_uint8 mphf_bucket = 0; h1 = mixh10h11h12(b, p1, p2, h1); mphf_bucket = (cmph_uint8)((h2 + g[h1]) % m); return (mphf_bucket + offset[h0]); }
static cmph_uint32 brz_fch_search(brz_data_t *brz, const char *key, cmph_uint32 keylen, cmph_uint32 * fingerprint) { register cmph_uint32 h0; hash_vector(brz->h0, key, keylen, fingerprint); h0 = fingerprint[2] % brz->k; register cmph_uint32 m = brz->size[h0]; register cmph_uint32 b = fch_calc_b(brz->c, m); register double p1 = fch_calc_p1(m); register double p2 = fch_calc_p2(b); register cmph_uint32 h1 = hash(brz->h1[h0], key, keylen) % m; register cmph_uint32 h2 = hash(brz->h2[h0], key, keylen) % m; register cmph_uint8 mphf_bucket = 0; h1 = mixh10h11h12(b, p1, p2, h1); mphf_bucket = (cmph_uint8)((h2 + brz->g[h0][h1]) % m); return (mphf_bucket + brz->offset[h0]); }
/** \fn cmph_uint32 brz_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 brz_packed_size(cmph_t *mphf) { cmph_uint32 i; cmph_uint32 size = 0; brz_data_t *data = (brz_data_t *)mphf->data; CMPH_HASH h0_type = hash_get_type(data->h0); CMPH_HASH h1_type = hash_get_type(data->h1[0]); CMPH_HASH h2_type = hash_get_type(data->h2[0]); size = (cmph_uint32)(2*sizeof(CMPH_ALGO) + 3*sizeof(CMPH_HASH) + hash_state_packed_size(h0_type) + sizeof(cmph_uint32) + sizeof(double) + sizeof(cmph_uint8)*data->k + sizeof(cmph_uint32)*data->k); // pointers to g_is #if defined (__ia64) || defined (__x86_64__) size += (cmph_uint32) sizeof(cmph_uint64)*data->k; #else size += (cmph_uint32) sizeof(cmph_uint32)*data->k; #endif size += hash_state_packed_size(h1_type) * data->k; size += hash_state_packed_size(h2_type) * data->k; cmph_uint32 n = 0; for(i = 0; i < data->k; i++) { switch(data->algo) { case CMPH_FCH: n = fch_calc_b(data->c, data->size[i]); break; case CMPH_BMZ8: n = (cmph_uint32)ceil(data->c * data->size[i]); break; default: assert(0); } size += n; } return size; }
/** \fn void brz_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 brz_pack(cmph_t *mphf, void *packed_mphf) { brz_data_t *data = (brz_data_t *)mphf->data; cmph_uint8 * ptr = packed_mphf; cmph_uint32 i,n; // packing internal algo type memcpy(ptr, &(data->algo), sizeof(data->algo)); ptr += sizeof(data->algo); // packing h0 type CMPH_HASH h0_type = hash_get_type(data->h0); memcpy(ptr, &h0_type, sizeof(h0_type)); ptr += sizeof(h0_type); // packing h0 hash_state_pack(data->h0, ptr); ptr += hash_state_packed_size(h0_type); // packing k memcpy(ptr, &(data->k), sizeof(data->k)); ptr += sizeof(data->k); // packing c *((cmph_uint64 *)ptr) = (cmph_uint64)data->c; ptr += sizeof(data->c); // packing h1 type CMPH_HASH h1_type = hash_get_type(data->h1[0]); memcpy(ptr, &h1_type, sizeof(h1_type)); ptr += sizeof(h1_type); // packing h2 type CMPH_HASH h2_type = hash_get_type(data->h2[0]); memcpy(ptr, &h2_type, sizeof(h2_type)); ptr += sizeof(h2_type); // packing size memcpy(ptr, data->size, sizeof(cmph_uint8)*data->k); ptr += data->k; // packing offset memcpy(ptr, data->offset, sizeof(cmph_uint32)*data->k); ptr += sizeof(cmph_uint32)*data->k; #if defined (__ia64) || defined (__x86_64__) cmph_uint64 * g_is_ptr = (cmph_uint64 *)ptr; #else cmph_uint32 * g_is_ptr = (cmph_uint32 *)ptr; #endif cmph_uint8 * g_i = (cmph_uint8 *) (g_is_ptr + data->k); for(i = 0; i < data->k; i++) { #if defined (__ia64) || defined (__x86_64__) *g_is_ptr++ = (cmph_uint64)g_i; #else *g_is_ptr++ = (cmph_uint32)g_i; #endif // packing h1[i] hash_state_pack(data->h1[i], g_i); g_i += hash_state_packed_size(h1_type); // packing h2[i] hash_state_pack(data->h2[i], g_i); g_i += hash_state_packed_size(h2_type); // packing g_i switch(data->algo) { case CMPH_FCH: n = fch_calc_b(data->c, data->size[i]); break; case CMPH_BMZ8: n = (cmph_uint32)ceil(data->c * data->size[i]); break; default: assert(0); } memcpy(g_i, data->g[i], sizeof(cmph_uint8)*n); g_i += n; } }
void brz_load(FILE *f, cmph_t *mphf) { char *buf = NULL; cmph_uint32 buflen; register size_t nbytes; cmph_uint32 i, n; brz_data_t *brz = (brz_data_t *)malloc(sizeof(brz_data_t)); DEBUGP("Loading brz mphf\n"); mphf->data = brz; nbytes = fread(&(brz->c), sizeof(double), (size_t)1, f); nbytes = fread(&(brz->algo), sizeof(brz->algo), (size_t)1, f); // Reading algo. nbytes = fread(&(brz->k), sizeof(cmph_uint32), (size_t)1, f); brz->size = (cmph_uint8 *) malloc(sizeof(cmph_uint8)*brz->k); nbytes = fread(brz->size, sizeof(cmph_uint8)*(brz->k), (size_t)1, f); brz->h1 = (hash_state_t **)malloc(sizeof(hash_state_t *)*brz->k); brz->h2 = (hash_state_t **)malloc(sizeof(hash_state_t *)*brz->k); brz->g = (cmph_uint8 **) calloc((size_t)brz->k, sizeof(cmph_uint8 *)); DEBUGP("Reading c = %f k = %u algo = %u \n", brz->c, brz->k, brz->algo); //loading h_i1, h_i2 and g_i. for(i = 0; i < brz->k; i++) { // h1 nbytes = fread(&buflen, sizeof(cmph_uint32), (size_t)1, f); DEBUGP("Hash state 1 has %u bytes\n", buflen); buf = (char *)malloc((size_t)buflen); nbytes = fread(buf, (size_t)buflen, (size_t)1, f); brz->h1[i] = hash_state_load(buf, buflen); free(buf); //h2 nbytes = fread(&buflen, sizeof(cmph_uint32), (size_t)1, f); DEBUGP("Hash state 2 has %u bytes\n", buflen); buf = (char *)malloc((size_t)buflen); nbytes = fread(buf, (size_t)buflen, (size_t)1, f); brz->h2[i] = hash_state_load(buf, buflen); free(buf); switch(brz->algo) { case CMPH_FCH: n = fch_calc_b(brz->c, brz->size[i]); break; case CMPH_BMZ8: n = (cmph_uint32)ceil(brz->c * brz->size[i]); break; default: assert(0); } DEBUGP("g_i has %u bytes\n", n); brz->g[i] = (cmph_uint8 *)calloc((size_t)n, sizeof(cmph_uint8)); nbytes = fread(brz->g[i], sizeof(cmph_uint8)*n, (size_t)1, f); } //loading h0 nbytes = fread(&buflen, sizeof(cmph_uint32), (size_t)1, f); DEBUGP("Hash state has %u bytes\n", buflen); buf = (char *)malloc((size_t)buflen); nbytes = fread(buf, (size_t)buflen, (size_t)1, f); brz->h0 = hash_state_load(buf, buflen); free(buf); //loading c, m, and the vector offset. nbytes = fread(&(brz->m), sizeof(cmph_uint32), (size_t)1, f); brz->offset = (cmph_uint32 *)malloc(sizeof(cmph_uint32)*brz->k); nbytes = fread(brz->offset, sizeof(cmph_uint32)*(brz->k), (size_t)1, f); return; }