1
Fork 0
turbonss/src/bmz.c

634 lines
20 KiB
C
Raw Normal View History

#include "graph.h"
2004-12-23 15:16:30 +02:00
#include "bmz.h"
#include "cmph_structs.h"
#include "bmz_structs.h"
#include "hash.h"
#include "vqueue.h"
#include "bitbool.h"
2004-12-23 15:16:30 +02:00
#include <math.h>
#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
#include <string.h>
//#define DEBUG
#include "debug.h"
static int bmz_gen_edges(cmph_config_t *mph);
static cmph_uint8 bmz_traverse_critical_nodes(bmz_config_data_t *bmz, cmph_uint32 v, cmph_uint32 * biggest_g_value, cmph_uint32 * biggest_edge_value, cmph_uint8 * used_edges, cmph_uint8 * visited);
static cmph_uint8 bmz_traverse_critical_nodes_heuristic(bmz_config_data_t *bmz, cmph_uint32 v, cmph_uint32 * biggest_g_value, cmph_uint32 * biggest_edge_value, cmph_uint8 * used_edges, cmph_uint8 * visited);
static void bmz_traverse_non_critical_nodes(bmz_config_data_t *bmz, cmph_uint8 * used_edges, cmph_uint8 * visited);
2004-12-23 15:16:30 +02:00
bmz_config_data_t *bmz_config_new()
2004-12-23 15:16:30 +02:00
{
bmz_config_data_t *bmz;
bmz = (bmz_config_data_t *)malloc(sizeof(bmz_config_data_t));
assert(bmz);
memset(bmz, 0, sizeof(bmz_config_data_t));
2005-01-18 23:06:08 +02:00
bmz->hashfuncs[0] = CMPH_HASH_JENKINS;
bmz->hashfuncs[1] = CMPH_HASH_JENKINS;
2004-12-23 15:16:30 +02:00
bmz->g = NULL;
bmz->graph = NULL;
bmz->hashes = NULL;
return bmz;
2004-12-23 15:16:30 +02:00
}
void bmz_config_destroy(cmph_config_t *mph)
2004-12-23 15:16:30 +02:00
{
bmz_config_data_t *data = (bmz_config_data_t *)mph->data;
2004-12-23 15:16:30 +02:00
DEBUGP("Destroying algorithm dependent data\n");
free(data);
}
void bmz_config_set_hashfuncs(cmph_config_t *mph, CMPH_HASH *hashfuncs)
2004-12-23 15:16:30 +02:00
{
bmz_config_data_t *bmz = (bmz_config_data_t *)mph->data;
2004-12-23 15:16:30 +02:00
CMPH_HASH *hashptr = hashfuncs;
2005-01-18 23:06:08 +02:00
cmph_uint32 i = 0;
while(*hashptr != CMPH_HASH_COUNT)
2004-12-23 15:16:30 +02:00
{
if (i >= 2) break; //bmz only uses two hash functions
bmz->hashfuncs[i] = *hashptr;
++i, ++hashptr;
}
}
cmph_t *bmz_new(cmph_config_t *mph, float c)
2004-12-23 15:16:30 +02:00
{
cmph_t *mphf = NULL;
bmz_data_t *bmzf = NULL;
2005-01-18 23:06:08 +02:00
cmph_uint32 i;
cmph_uint32 iterations;
cmph_uint32 iterations_map = 20;
cmph_uint8 *used_edges = NULL;
cmph_uint8 restart_mapping = 0;
cmph_uint8 * visited = NULL;
bmz_config_data_t *bmz = (bmz_config_data_t *)mph->data;
if (c == 0) c = 1.15; // validating restrictions over parameter c.
DEBUGP("c: %f\n", c);
2004-12-23 15:16:30 +02:00
bmz->m = mph->key_source->nkeys;
bmz->n = ceil(c * mph->key_source->nkeys);
DEBUGP("m (edges): %u n (vertices): %u c: %f\n", bmz->m, bmz->n, c);
bmz->graph = graph_new(bmz->n, bmz->m);
2004-12-23 15:16:30 +02:00
DEBUGP("Created graph\n");
bmz->hashes = (hash_state_t **)malloc(sizeof(hash_state_t *)*3);
2004-12-23 15:16:30 +02:00
for(i = 0; i < 3; ++i) bmz->hashes[i] = NULL;
2005-01-17 19:58:43 +02:00
do
2004-12-23 15:16:30 +02:00
{
2005-01-17 19:58:43 +02:00
// Mapping step
2005-01-18 23:06:08 +02:00
cmph_uint32 biggest_g_value = 0;
cmph_uint32 biggest_edge_value = 1;
2005-08-06 23:20:04 +03:00
iterations = 100;
2005-01-17 19:58:43 +02:00
if (mph->verbosity)
{
2004-12-23 15:16:30 +02:00
fprintf(stderr, "Entering mapping step for mph creation of %u keys with graph sized %u\n", bmz->m, bmz->n);
2005-01-17 19:58:43 +02:00
}
while(1)
{
2004-12-23 15:16:30 +02:00
int ok;
DEBUGP("hash function 1\n");
bmz->hashes[0] = hash_state_new(bmz->hashfuncs[0], bmz->n);
2004-12-23 15:16:30 +02:00
DEBUGP("hash function 2\n");
bmz->hashes[1] = hash_state_new(bmz->hashfuncs[1], bmz->n);
DEBUGP("Generating edges\n");
2004-12-23 15:16:30 +02:00
ok = bmz_gen_edges(mph);
if (!ok)
{
--iterations;
hash_state_destroy(bmz->hashes[0]);
2004-12-23 15:16:30 +02:00
bmz->hashes[0] = NULL;
hash_state_destroy(bmz->hashes[1]);
2004-12-23 15:16:30 +02:00
bmz->hashes[1] = NULL;
DEBUGP("%u iterations remaining\n", iterations);
if (mph->verbosity)
{
fprintf(stderr, "simple graph creation failure - %u iterations remaining\n", iterations);
}
if (iterations == 0) break;
}
else break;
2005-01-17 19:58:43 +02:00
}
if (iterations == 0)
{
graph_destroy(bmz->graph);
2004-12-23 15:16:30 +02:00
return NULL;
2005-01-17 19:58:43 +02:00
}
// Ordering step
if (mph->verbosity)
{
2004-12-23 15:16:30 +02:00
fprintf(stderr, "Starting ordering step\n");
2005-01-17 19:58:43 +02:00
}
graph_obtain_critical_nodes(bmz->graph);
2004-12-23 15:16:30 +02:00
2005-01-17 19:58:43 +02:00
// Searching step
if (mph->verbosity)
{
fprintf(stderr, "Starting Searching step.\n");
2004-12-23 15:16:30 +02:00
fprintf(stderr, "\tTraversing critical vertices.\n");
2005-01-17 19:58:43 +02:00
}
DEBUGP("Searching step\n");
visited = (cmph_uint8 *)malloc(bmz->n/8 + 1);
2005-01-17 19:58:43 +02:00
memset(visited, 0, bmz->n/8 + 1);
2005-01-18 23:06:08 +02:00
used_edges = (cmph_uint8 *)malloc(bmz->m/8 + 1);
2005-01-17 19:58:43 +02:00
memset(used_edges, 0, bmz->m/8 + 1);
free(bmz->g);
bmz->g = (cmph_uint32 *)calloc(bmz->n, sizeof(cmph_uint32));
2005-01-17 19:58:43 +02:00
assert(bmz->g);
for (i = 0; i < bmz->n; ++i) // critical nodes
{
if (graph_node_is_critical(bmz->graph, i) && (!GETBIT(visited,i)))
2004-12-23 15:16:30 +02:00
{
if(c > 1.14) restart_mapping = bmz_traverse_critical_nodes(bmz, i, &biggest_g_value, &biggest_edge_value, used_edges, visited);
2005-01-17 19:58:43 +02:00
else restart_mapping = bmz_traverse_critical_nodes_heuristic(bmz, i, &biggest_g_value, &biggest_edge_value, used_edges, visited);
if(restart_mapping) break;
2004-12-23 15:16:30 +02:00
}
2005-01-17 19:58:43 +02:00
}
if(!restart_mapping)
{
if (mph->verbosity)
{
fprintf(stderr, "\tTraversing non critical vertices.\n");
}
bmz_traverse_non_critical_nodes(bmz, used_edges, visited); // non_critical_nodes
}
else
{
iterations_map--;
if (mph->verbosity) fprintf(stderr, "Restarting mapping step. %u iterations remaining.\n", iterations_map);
}
free(used_edges);
free(visited);
}while(restart_mapping && iterations_map > 0);
graph_destroy(bmz->graph);
2004-12-23 15:16:30 +02:00
bmz->graph = NULL;
2005-08-06 23:20:04 +03:00
if (iterations_map == 0)
{
return NULL;
}
mphf = (cmph_t *)malloc(sizeof(cmph_t));
2004-12-23 15:16:30 +02:00
mphf->algo = mph->algo;
2005-07-28 00:13:02 +03:00
bmzf = (bmz_data_t *)malloc(sizeof(bmz_data_t));
2004-12-23 15:16:30 +02:00
bmzf->g = bmz->g;
bmz->g = NULL; //transfer memory ownership
bmzf->hashes = bmz->hashes;
bmz->hashes = NULL; //transfer memory ownership
bmzf->n = bmz->n;
bmzf->m = bmz->m;
mphf->data = bmzf;
mphf->size = bmz->m;
2004-12-23 15:16:30 +02:00
DEBUGP("Successfully generated minimal perfect hash\n");
if (mph->verbosity)
{
fprintf(stderr, "Successfully generated minimal perfect hash function\n");
}
return mphf;
}
static cmph_uint8 bmz_traverse_critical_nodes(bmz_config_data_t *bmz, cmph_uint32 v, cmph_uint32 * biggest_g_value, cmph_uint32 * biggest_edge_value, cmph_uint8 * used_edges, cmph_uint8 * visited)
2004-12-23 15:16:30 +02:00
{
2005-01-18 23:06:08 +02:00
cmph_uint32 next_g;
cmph_uint32 u; /* Auxiliary vertex */
cmph_uint32 lav; /* lookahead vertex */
cmph_uint8 collision;
2005-08-07 04:09:14 +03:00
vqueue_t * q = vqueue_new((cmph_uint32)(graph_ncritical_nodes(bmz->graph)) + 1);
graph_iterator_t it, it1;
2004-12-23 15:16:30 +02:00
DEBUGP("Labelling critical vertices\n");
2005-01-18 23:06:08 +02:00
bmz->g[v] = (cmph_uint32)ceil ((double)(*biggest_edge_value)/2) - 1;
2005-01-17 19:58:43 +02:00
SETBIT(visited, v);
2005-01-18 23:06:08 +02:00
next_g = (cmph_uint32)floor((double)(*biggest_edge_value/2)); /* next_g is incremented in the do..while statement*/
vqueue_insert(q, v);
while(!vqueue_is_empty(q))
2004-12-23 15:16:30 +02:00
{
v = vqueue_remove(q);
it = graph_neighbors_it(bmz->graph, v);
while ((u = graph_next_neighbor(bmz->graph, &it)) != GRAPH_NO_NEIGHBOR)
2004-12-23 15:16:30 +02:00
{
if (graph_node_is_critical(bmz->graph, u) && (!GETBIT(visited,u)))
2004-12-23 15:16:30 +02:00
{
collision = 1;
while(collision) // lookahead to resolve collisions
{
next_g = *biggest_g_value + 1;
it1 = graph_neighbors_it(bmz->graph, u);
2004-12-23 15:16:30 +02:00
collision = 0;
while((lav = graph_next_neighbor(bmz->graph, &it1)) != GRAPH_NO_NEIGHBOR)
2004-12-23 15:16:30 +02:00
{
if (graph_node_is_critical(bmz->graph, lav) && GETBIT(visited,lav))
2004-12-23 15:16:30 +02:00
{
2005-01-17 19:58:43 +02:00
if(next_g + bmz->g[lav] >= bmz->m)
{
vqueue_destroy(q);
2005-01-17 19:58:43 +02:00
return 1; // restart mapping step.
}
2008-03-23 02:46:34 +02:00
if (GETBIT(used_edges, (next_g + bmz->g[lav])))
2004-12-23 15:16:30 +02:00
{
collision = 1;
break;
}
}
}
if (next_g > *biggest_g_value) *biggest_g_value = next_g;
}
// Marking used edges...
it1 = graph_neighbors_it(bmz->graph, u);
while((lav = graph_next_neighbor(bmz->graph, &it1)) != GRAPH_NO_NEIGHBOR)
2004-12-23 15:16:30 +02:00
{
if (graph_node_is_critical(bmz->graph, lav) && GETBIT(visited, lav))
2004-12-23 15:16:30 +02:00
{
2008-03-23 02:46:34 +02:00
SETBIT(used_edges,(next_g + bmz->g[lav]));
2004-12-23 15:16:30 +02:00
if(next_g + bmz->g[lav] > *biggest_edge_value) *biggest_edge_value = next_g + bmz->g[lav];
}
}
bmz->g[u] = next_g; // Labelling vertex u.
2005-01-17 19:58:43 +02:00
SETBIT(visited,u);
vqueue_insert(q, u);
2004-12-23 15:16:30 +02:00
}
}
}
vqueue_destroy(q);
2005-01-17 19:58:43 +02:00
return 0;
}
static cmph_uint8 bmz_traverse_critical_nodes_heuristic(bmz_config_data_t *bmz, cmph_uint32 v, cmph_uint32 * biggest_g_value, cmph_uint32 * biggest_edge_value, cmph_uint8 * used_edges, cmph_uint8 * visited)
2005-01-17 19:58:43 +02:00
{
2005-01-18 23:06:08 +02:00
cmph_uint32 next_g;
cmph_uint32 u; /* Auxiliary vertex */
cmph_uint32 lav; /* lookahead vertex */
cmph_uint8 collision;
cmph_uint32 * unused_g_values = NULL;
cmph_uint32 unused_g_values_capacity = 0;
cmph_uint32 nunused_g_values = 0;
vqueue_t * q = vqueue_new((cmph_uint32)(0.5*graph_ncritical_nodes(bmz->graph))+1);
graph_iterator_t it, it1;
2005-01-17 19:58:43 +02:00
DEBUGP("Labelling critical vertices\n");
2005-01-18 23:06:08 +02:00
bmz->g[v] = (cmph_uint32)ceil ((double)(*biggest_edge_value)/2) - 1;
2005-01-17 19:58:43 +02:00
SETBIT(visited, v);
2005-01-18 23:06:08 +02:00
next_g = (cmph_uint32)floor((double)(*biggest_edge_value/2)); /* next_g is incremented in the do..while statement*/
vqueue_insert(q, v);
while(!vqueue_is_empty(q))
2005-01-17 19:58:43 +02:00
{
v = vqueue_remove(q);
it = graph_neighbors_it(bmz->graph, v);
while ((u = graph_next_neighbor(bmz->graph, &it)) != GRAPH_NO_NEIGHBOR)
2005-01-17 19:58:43 +02:00
{
if (graph_node_is_critical(bmz->graph, u) && (!GETBIT(visited,u)))
2005-01-17 19:58:43 +02:00
{
2005-01-18 23:06:08 +02:00
cmph_uint32 next_g_index = 0;
2005-01-17 19:58:43 +02:00
collision = 1;
while(collision) // lookahead to resolve collisions
{
if (next_g_index < nunused_g_values)
{
next_g = unused_g_values[next_g_index++];
}
else
{
next_g = *biggest_g_value + 1;
next_g_index = UINT_MAX;
}
it1 = graph_neighbors_it(bmz->graph, u);
2005-01-17 19:58:43 +02:00
collision = 0;
while((lav = graph_next_neighbor(bmz->graph, &it1)) != GRAPH_NO_NEIGHBOR)
2005-01-17 19:58:43 +02:00
{
if (graph_node_is_critical(bmz->graph, lav) && GETBIT(visited,lav))
2005-01-17 19:58:43 +02:00
{
if(next_g + bmz->g[lav] >= bmz->m)
{
vqueue_destroy(q);
2005-01-17 19:58:43 +02:00
free(unused_g_values);
return 1; // restart mapping step.
}
2008-03-23 02:46:34 +02:00
if (GETBIT(used_edges, (next_g + bmz->g[lav])))
2005-01-17 19:58:43 +02:00
{
collision = 1;
break;
}
}
}
if(collision && (next_g > *biggest_g_value)) // saving the current g value stored in next_g.
{
if(nunused_g_values == unused_g_values_capacity)
{
unused_g_values = (cmph_uint32 *)realloc(unused_g_values, (unused_g_values_capacity + BUFSIZ)*sizeof(cmph_uint32));
2005-01-17 19:58:43 +02:00
unused_g_values_capacity += BUFSIZ;
}
unused_g_values[nunused_g_values++] = next_g;
}
if (next_g > *biggest_g_value) *biggest_g_value = next_g;
}
next_g_index--;
if (next_g_index < nunused_g_values) unused_g_values[next_g_index] = unused_g_values[--nunused_g_values];
// Marking used edges...
it1 = graph_neighbors_it(bmz->graph, u);
while((lav = graph_next_neighbor(bmz->graph, &it1)) != GRAPH_NO_NEIGHBOR)
2005-01-17 19:58:43 +02:00
{
if (graph_node_is_critical(bmz->graph, lav) && GETBIT(visited, lav))
2005-01-17 19:58:43 +02:00
{
2008-03-23 02:46:34 +02:00
SETBIT(used_edges,(next_g + bmz->g[lav]));
2005-01-17 19:58:43 +02:00
if(next_g + bmz->g[lav] > *biggest_edge_value) *biggest_edge_value = next_g + bmz->g[lav];
}
}
bmz->g[u] = next_g; // Labelling vertex u.
SETBIT(visited, u);
vqueue_insert(q, u);
2005-01-17 19:58:43 +02:00
}
}
}
vqueue_destroy(q);
2005-01-17 19:58:43 +02:00
free(unused_g_values);
return 0;
2004-12-23 15:16:30 +02:00
}
static cmph_uint32 next_unused_edge(bmz_config_data_t *bmz, cmph_uint8 * used_edges, cmph_uint32 unused_edge_index)
2004-12-23 15:16:30 +02:00
{
while(1)
{
assert(unused_edge_index < bmz->m);
if(GETBIT(used_edges, unused_edge_index)) unused_edge_index ++;
2004-12-23 15:16:30 +02:00
else break;
}
return unused_edge_index;
}
static void bmz_traverse(bmz_config_data_t *bmz, cmph_uint8 * used_edges, cmph_uint32 v, cmph_uint32 * unused_edge_index, cmph_uint8 * visited)
2004-12-23 15:16:30 +02:00
{
graph_iterator_t it = graph_neighbors_it(bmz->graph, v);
2005-01-18 23:06:08 +02:00
cmph_uint32 neighbor = 0;
while((neighbor = graph_next_neighbor(bmz->graph, &it)) != GRAPH_NO_NEIGHBOR)
2004-12-23 15:16:30 +02:00
{
2005-01-17 19:58:43 +02:00
if(GETBIT(visited,neighbor)) continue;
2006-08-03 23:00:11 +03:00
//DEBUGP("Visiting neighbor %u\n", neighbor);
*unused_edge_index = next_unused_edge(bmz, used_edges, *unused_edge_index);
2005-08-06 23:20:04 +03:00
bmz->g[neighbor] = *unused_edge_index - bmz->g[v];
//if (bmz->g[neighbor] >= bmz->m) bmz->g[neighbor] += bmz->m;
2005-01-17 19:58:43 +02:00
SETBIT(visited, neighbor);
(*unused_edge_index)++;
2005-01-17 19:58:43 +02:00
bmz_traverse(bmz, used_edges, neighbor, unused_edge_index, visited);
2004-12-23 15:16:30 +02:00
}
}
static void bmz_traverse_non_critical_nodes(bmz_config_data_t *bmz, cmph_uint8 * used_edges, cmph_uint8 * visited)
2004-12-23 15:16:30 +02:00
{
2005-01-18 23:06:08 +02:00
cmph_uint32 i, v1, v2, unused_edge_index = 0;
2004-12-23 15:16:30 +02:00
DEBUGP("Labelling non critical vertices\n");
for(i = 0; i < bmz->m; i++)
{
v1 = graph_vertex_id(bmz->graph, i, 0);
v2 = graph_vertex_id(bmz->graph, i, 1);
2005-01-17 19:58:43 +02:00
if((GETBIT(visited,v1) && GETBIT(visited,v2)) || (!GETBIT(visited,v1) && !GETBIT(visited,v2))) continue;
if(GETBIT(visited,v1)) bmz_traverse(bmz, used_edges, v1, &unused_edge_index, visited);
else bmz_traverse(bmz, used_edges, v2, &unused_edge_index, visited);
2004-12-23 15:16:30 +02:00
}
for(i = 0; i < bmz->n; i++)
{
2005-01-17 19:58:43 +02:00
if(!GETBIT(visited,i))
2004-12-23 15:16:30 +02:00
{
bmz->g[i] = 0;
2005-01-17 19:58:43 +02:00
SETBIT(visited, i);
bmz_traverse(bmz, used_edges, i, &unused_edge_index, visited);
2004-12-23 15:16:30 +02:00
}
}
}
static int bmz_gen_edges(cmph_config_t *mph)
2004-12-23 15:16:30 +02:00
{
2005-01-18 23:06:08 +02:00
cmph_uint32 e;
bmz_config_data_t *bmz = (bmz_config_data_t *)mph->data;
2005-01-18 23:06:08 +02:00
cmph_uint8 multiple_edges = 0;
2004-12-23 15:16:30 +02:00
DEBUGP("Generating edges for %u vertices\n", bmz->n);
graph_clear_edges(bmz->graph);
2004-12-23 15:16:30 +02:00
mph->key_source->rewind(mph->key_source->data);
for (e = 0; e < mph->key_source->nkeys; ++e)
{
2005-01-18 23:06:08 +02:00
cmph_uint32 h1, h2;
cmph_uint32 keylen;
2005-07-29 03:00:01 +03:00
char *key = NULL;
2004-12-23 15:16:30 +02:00
mph->key_source->read(mph->key_source->data, &key, &keylen);
2005-07-29 03:00:01 +03:00
// if (key == NULL)fprintf(stderr, "key = %s -- read BMZ\n", key);
h1 = hash(bmz->hashes[0], key, keylen) % bmz->n;
h2 = hash(bmz->hashes[1], key, keylen) % bmz->n;
2004-12-23 15:16:30 +02:00
if (h1 == h2) if (++h2 >= bmz->n) h2 = 0;
if (h1 == h2)
{
if (mph->verbosity) fprintf(stderr, "Self loop for key %u\n", e);
2004-12-23 15:16:30 +02:00
mph->key_source->dispose(mph->key_source->data, key, keylen);
return 0;
}
2006-08-03 23:00:11 +03:00
//DEBUGP("Adding edge: %u -> %u for key %s\n", h1, h2, key);
2004-12-23 15:16:30 +02:00
mph->key_source->dispose(mph->key_source->data, key, keylen);
2005-07-29 03:00:01 +03:00
// fprintf(stderr, "key = %s -- dispose BMZ\n", key);
multiple_edges = graph_contains_edge(bmz->graph, h1, h2);
2004-12-23 15:16:30 +02:00
if (mph->verbosity && multiple_edges) fprintf(stderr, "A non simple graph was generated\n");
if (multiple_edges) return 0; // checking multiple edge restriction.
graph_add_edge(bmz->graph, h1, h2);
2004-12-23 15:16:30 +02:00
}
return !multiple_edges;
}
int bmz_dump(cmph_t *mphf, FILE *fd)
2004-12-23 15:16:30 +02:00
{
char *buf = NULL;
2005-01-18 23:06:08 +02:00
cmph_uint32 buflen;
cmph_uint32 two = 2; //number of hash functions
bmz_data_t *data = (bmz_data_t *)mphf->data;
__cmph_dump(mphf, fd);
2004-12-23 15:16:30 +02:00
2005-01-18 23:06:08 +02:00
fwrite(&two, sizeof(cmph_uint32), 1, fd);
2004-12-23 15:16:30 +02:00
hash_state_dump(data->hashes[0], &buf, &buflen);
2004-12-23 15:16:30 +02:00
DEBUGP("Dumping hash state with %u bytes to disk\n", buflen);
2005-01-18 23:06:08 +02:00
fwrite(&buflen, sizeof(cmph_uint32), 1, fd);
2004-12-23 15:16:30 +02:00
fwrite(buf, buflen, 1, fd);
free(buf);
hash_state_dump(data->hashes[1], &buf, &buflen);
2004-12-23 15:16:30 +02:00
DEBUGP("Dumping hash state with %u bytes to disk\n", buflen);
2005-01-18 23:06:08 +02:00
fwrite(&buflen, sizeof(cmph_uint32), 1, fd);
2004-12-23 15:16:30 +02:00
fwrite(buf, buflen, 1, fd);
free(buf);
2005-01-18 23:06:08 +02:00
fwrite(&(data->n), sizeof(cmph_uint32), 1, fd);
fwrite(&(data->m), sizeof(cmph_uint32), 1, fd);
2004-12-23 15:16:30 +02:00
2005-01-18 23:06:08 +02:00
fwrite(data->g, sizeof(cmph_uint32)*(data->n), 1, fd);
2004-12-23 15:16:30 +02:00
#ifdef DEBUG
2006-08-03 21:27:16 +03:00
cmph_uint32 i;
2004-12-23 15:16:30 +02:00
fprintf(stderr, "G: ");
for (i = 0; i < data->n; ++i) fprintf(stderr, "%u ", data->g[i]);
fprintf(stderr, "\n");
#endif
return 1;
}
void bmz_load(FILE *f, cmph_t *mphf)
2004-12-23 15:16:30 +02:00
{
2005-01-18 23:06:08 +02:00
cmph_uint32 nhashes;
2004-12-23 15:16:30 +02:00
char *buf = NULL;
2005-01-18 23:06:08 +02:00
cmph_uint32 buflen;
cmph_uint32 i;
bmz_data_t *bmz = (bmz_data_t *)malloc(sizeof(bmz_data_t));
2004-12-23 15:16:30 +02:00
DEBUGP("Loading bmz mphf\n");
mphf->data = bmz;
2005-01-18 23:06:08 +02:00
fread(&nhashes, sizeof(cmph_uint32), 1, f);
bmz->hashes = (hash_state_t **)malloc(sizeof(hash_state_t *)*(nhashes + 1));
2004-12-23 15:16:30 +02:00
bmz->hashes[nhashes] = NULL;
DEBUGP("Reading %u hashes\n", nhashes);
for (i = 0; i < nhashes; ++i)
{
hash_state_t *state = NULL;
2005-01-18 23:06:08 +02:00
fread(&buflen, sizeof(cmph_uint32), 1, f);
2004-12-23 15:16:30 +02:00
DEBUGP("Hash state has %u bytes\n", buflen);
buf = (char *)malloc(buflen);
fread(buf, buflen, 1, f);
state = hash_state_load(buf, buflen);
2004-12-23 15:16:30 +02:00
bmz->hashes[i] = state;
free(buf);
}
DEBUGP("Reading m and n\n");
2005-01-18 23:06:08 +02:00
fread(&(bmz->n), sizeof(cmph_uint32), 1, f);
fread(&(bmz->m), sizeof(cmph_uint32), 1, f);
2004-12-23 15:16:30 +02:00
2005-01-18 23:06:08 +02:00
bmz->g = (cmph_uint32 *)malloc(sizeof(cmph_uint32)*bmz->n);
fread(bmz->g, bmz->n*sizeof(cmph_uint32), 1, f);
2004-12-23 15:16:30 +02:00
#ifdef DEBUG
fprintf(stderr, "G: ");
for (i = 0; i < bmz->n; ++i) fprintf(stderr, "%u ", bmz->g[i]);
fprintf(stderr, "\n");
#endif
return;
}
cmph_uint32 bmz_search(cmph_t *mphf, const char *key, cmph_uint32 keylen)
2004-12-23 15:16:30 +02:00
{
bmz_data_t *bmz = mphf->data;
cmph_uint32 h1 = hash(bmz->hashes[0], key, keylen) % bmz->n;
cmph_uint32 h2 = hash(bmz->hashes[1], key, keylen) % bmz->n;
2004-12-23 15:16:30 +02:00
DEBUGP("key: %s h1: %u h2: %u\n", key, h1, h2);
if (h1 == h2 && ++h2 > bmz->n) h2 = 0;
DEBUGP("key: %s g[h1]: %u g[h2]: %u edges: %u\n", key, bmz->g[h1], bmz->g[h2], bmz->m);
2005-09-06 17:11:37 +03:00
return bmz->g[h1] + bmz->g[h2];
2004-12-23 15:16:30 +02:00
}
void bmz_destroy(cmph_t *mphf)
2004-12-23 15:16:30 +02:00
{
bmz_data_t *data = (bmz_data_t *)mphf->data;
2004-12-23 15:16:30 +02:00
free(data->g);
hash_state_destroy(data->hashes[0]);
hash_state_destroy(data->hashes[1]);
2004-12-23 15:16:30 +02:00
free(data->hashes);
free(data);
free(mphf);
}
2008-03-26 22:26:48 +02:00
/** cmph_uint32 bmz_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 bmz_search_fingerprint(cmph_t *mphf, const char *key, cmph_uint32 keylen, cmph_uint32 * fingerprint)
{
bmz_data_t *bmz = mphf->data;
cmph_uint32 h1, h2;
hash_vector(bmz->hashes[0], key, keylen, fingerprint);
h1 = fingerprint[2] % bmz->n;
hash_vector(bmz->hashes[1], key, keylen, fingerprint);
h2 = fingerprint[2] % bmz->n;
DEBUGP("key: %s h1: %u h2: %u\n", key, h1, h2);
if (h1 == h2 && ++h2 > bmz->n) h2 = 0;
DEBUGP("key: %s g[h1]: %u g[h2]: %u edges: %u\n", key, bmz->g[h1], bmz->g[h2], bmz->m);
return bmz->g[h1] + bmz->g[h2];
}
/** \fn void bmz_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 bmz_pack(cmph_t *mphf, void *packed_mphf)
{
bmz_data_t *data = (bmz_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 g
memcpy(ptr, data->g, sizeof(cmph_uint32)*data->n);
}
/** \fn cmph_uint32 bmz_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 bmz_packed_size(cmph_t *mphf)
{
bmz_data_t *data = (bmz_data_t *)mphf->data;
return (sizeof(CMPH_ALGO) + 2*hash_state_packed_size(data->hashes[0]) + sizeof(cmph_uint32) + sizeof(cmph_uint32)*data->n);
}
/** cmph_uint32 bmz_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 bmz_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;
register cmph_uint32 *h2_ptr = h1_ptr + (h1_size >> 2); // h1_ptr + h1_size/4
register cmph_uint32 h2_size = *h2_ptr;
register cmph_uint32 *g_ptr = h2_ptr + (h2_size >> 2); // h2_ptr + h2_size/4
register cmph_uint32 n = *g_ptr++;
register cmph_uint32 h1 = hash_packed(h1_ptr, key, keylen) % n;
register cmph_uint32 h2 = hash_packed(h2_ptr, key, keylen) % n;
if (h1 == h2 && ++h2 > n) h2 = 0;
return (g_ptr[h1] + g_ptr[h2]);
}