From 38f66219782db1c96db16439db08f7a2cab3fc25 Mon Sep 17 00:00:00 2001 From: fc_botelho Date: Fri, 29 Jul 2005 00:00:01 +0000 Subject: [PATCH] fixed some mistakes in BRZ algorithm --- src/bmz.c | 8 ++++--- src/brz.c | 69 +++++++++++++++++++++++++++++++++++++----------------- src/cmph.c | 31 +++++++++++++++++------- 3 files changed, 75 insertions(+), 33 deletions(-) diff --git a/src/bmz.c b/src/bmz.c index 2861085..efa42f6 100644 --- a/src/bmz.c +++ b/src/bmz.c @@ -99,7 +99,7 @@ cmph_t *bmz_new(cmph_config_t *mph, float c) bmz->hashes[0] = hash_state_new(bmz->hashfuncs[0], bmz->n); DEBUGP("hash function 2\n"); bmz->hashes[1] = hash_state_new(bmz->hashfuncs[1], bmz->n); - DEBUGP("Generating edges\n"); + DEBUGP("Generating edges\n"); ok = bmz_gen_edges(mph); if (!ok) { @@ -413,7 +413,6 @@ static int bmz_gen_edges(cmph_config_t *mph) cmph_uint32 e; bmz_config_data_t *bmz = (bmz_config_data_t *)mph->data; cmph_uint8 multiple_edges = 0; - DEBUGP("Generating edges for %u vertices\n", bmz->n); graph_clear_edges(bmz->graph); mph->key_source->rewind(mph->key_source->data); @@ -421,8 +420,10 @@ static int bmz_gen_edges(cmph_config_t *mph) { cmph_uint32 h1, h2; cmph_uint32 keylen; - char *key; + char *key = NULL; mph->key_source->read(mph->key_source->data, &key, &keylen); + +// 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; if (h1 == h2) if (++h2 >= bmz->n) h2 = 0; @@ -434,6 +435,7 @@ static int bmz_gen_edges(cmph_config_t *mph) } DEBUGP("Adding edge: %u -> %u for key %s\n", h1, h2, key); mph->key_source->dispose(mph->key_source->data, key, keylen); +// fprintf(stderr, "key = %s -- dispose BMZ\n", key); multiple_edges = graph_contains_edge(bmz->graph, h1, h2); if (mph->verbosity && multiple_edges) fprintf(stderr, "A non simple graph was generated\n"); if (multiple_edges) return 0; // checking multiple edge restriction. diff --git a/src/brz.c b/src/brz.c index 4ef8bea..4c0756d 100755 --- a/src/brz.c +++ b/src/brz.c @@ -19,7 +19,7 @@ static int brz_before_gen_graphs(cmph_config_t *mph, cmph_uint32 * disksize, cmph_uint32 * diskoffset); static void brz_gen_graphs(cmph_config_t *mph, cmph_uint32 * disksize, cmph_uint32 * diskoffset, FILE * graphs_fd); -static char ** brz_read_keys_vd(FILE * graphs_fd, cmph_uint8 nkeys); +static char ** brz_read_keys_vd(FILE * graphs_fd, cmph_uint8 nkeys, cmph_uint32 h3); static void brz_destroy_keys_vd(char ** keys_vd, cmph_uint8 nkeys); static void brz_copy_partial_mphf(brz_config_data_t *brz, bmz_data_t * bmzf, cmph_uint32 index); @@ -78,9 +78,9 @@ cmph_t *brz_new(cmph_config_t *mph, float c) DEBUGP("c: %f\n", c); brz_config_data_t *brz = (brz_config_data_t *)mph->data; brz->m = mph->key_source->nkeys; - DEBUGP("m: %f\n", brz->m); + DEBUGP("m: %u\n", brz->m); brz->k = ceil(brz->m/128); - DEBUGP("k: %f\n", brz->k); + DEBUGP("k: %u\n", brz->k); brz->size = (cmph_uint8 *) malloc(sizeof(cmph_uint8)*brz->k); brz->offset = (cmph_uint32 *)malloc(sizeof(cmph_uint32)*brz->k); @@ -128,7 +128,7 @@ cmph_t *brz_new(cmph_config_t *mph, float c) return NULL; } - graphs_fd = fopen("/colecao/fbotelho/cmph.tmp", "wb+"); + graphs_fd = fopen("/colecao/fbotelho/cmph.tmp", "wb"); if (graphs_fd == NULL) { free(brz->size); @@ -138,12 +138,13 @@ cmph_t *brz_new(cmph_config_t *mph, float c) fprintf(stderr, "Unable to open file %s\n", "/colecao/fbotelho/cmph.tmp"); return NULL; } - // Clustering the keys by graph id. brz_gen_graphs(mph, disksize, diskoffset, graphs_fd); free(disksize); free(diskoffset); DEBUGP("Graphs generated\n"); + fclose(graphs_fd); + graphs_fd = fopen("/colecao/fbotelho/cmph.tmp", "rb"); // codigo do algoritmo... brz->h1 = (hash_state_t **)malloc(sizeof(hash_state_t *)*brz->k); brz->h2 = (hash_state_t **)malloc(sizeof(hash_state_t *)*brz->k); @@ -151,29 +152,26 @@ cmph_t *brz_new(cmph_config_t *mph, float c) for(i = 0; i < brz->k; i++) { + cmph_uint32 j; bmz_data_t * bmzf = NULL; if (brz->size[i] == 0) continue; - keys_vd = brz_read_keys_vd(graphs_fd, brz->size[i]); + keys_vd = brz_read_keys_vd(graphs_fd, brz->size[i], i); // Source of keys source = cmph_io_vector_adapter(keys_vd, (cmph_uint32)brz->size[i]); - config = cmph_config_new(source); cmph_config_set_algo(config, CMPH_BMZ); cmph_config_set_graphsize(config, c); mphf_tmp = cmph_new(config); - bmzf = (bmz_data_t *)mphf_tmp->data; - brz_copy_partial_mphf(brz, bmzf, i); // implementar - + brz_copy_partial_mphf(brz, bmzf, i); // implementar cmph_config_destroy(config); - brz_destroy_keys_vd(keys_vd, brz->size[i]); + brz_destroy_keys_vd(keys_vd, brz->size[i]); free(keys_vd); cmph_destroy(mphf_tmp); free(source); } - fclose(graphs_fd); - + fclose(graphs_fd); // Generating a mphf mphf = (cmph_t *)malloc(sizeof(cmph_t)); @@ -216,15 +214,29 @@ static int brz_before_gen_graphs(cmph_config_t *mph, cmph_uint32 * disksize, cmp char *key; mph->key_source->read(mph->key_source->data, &key, &keylen); h3 = hash(brz->h3, key, keylen) % brz->k; + if(h3 == 6) + { + DEBUGP("key = %s\n", key); + DEBUGP("keylen = %u\n", keylen + 1); + } + mph->key_source->dispose(mph->key_source->data, key, keylen); if (brz->size[h3] == 255) return 0; brz->size[h3] = brz->size[h3] + 1; disksize[h3] = disksize[h3] + keylen + 1; +// if(h3 == 6) +// { +// DEBUGP("disksize[%u]=%u \n", h3, disksize[h3]); +// } + } + fprintf(stderr, "size:%u offset: %u\n", brz->size[0], brz->offset[0]); for (e = 1; e < brz->k; ++e) { - brz->offset[e] = brz->size[e-1] + brz->size[e]; - diskoffset[e] = disksize[e-1] + disksize[e]; + brz->offset[e] = brz->size[e-1] + brz->offset[e-1]; + diskoffset[e] = disksize[e-1] + diskoffset[e-1]; + DEBUGP("disksize[%u]=%u diskoffset[%u]: %u\n", e, disksize[e], e, diskoffset[e]); + DEBUGP("size[%u]=%u offset[%u]: %u\n", e, brz->size[e], e, brz->offset[e]); } return 1; } @@ -243,14 +255,23 @@ static void brz_gen_graphs(cmph_config_t *mph, cmph_uint32 * disksize, cmph_uint char *key; mph->key_source->read(mph->key_source->data, &key, &keylen); h3 = hash(brz->h3, key, keylen) % brz->k; - mph->key_source->dispose(mph->key_source->data, key, keylen); + if(h3 == 6) + { + DEBUGP("key = %s\n", key); + DEBUGP("keylen = %u\n", keylen + 1); + } fseek(graphs_fd, diskoffset[h3], SEEK_SET); fwrite(key, sizeof(char), keylen + 1, graphs_fd); + if(h3 == 6) + { + DEBUGP("diskoffset[%u]=%u \n", h3, diskoffset[h3]); + } diskoffset[h3] = diskoffset[h3] + keylen + 1; + mph->key_source->dispose(mph->key_source->data, key, keylen); } } -static char ** brz_read_keys_vd(FILE * graphs_fd, cmph_uint8 nkeys) +static char ** brz_read_keys_vd(FILE * graphs_fd, cmph_uint8 nkeys, cmph_uint32 h3) { char ** keys_vd = (char **)malloc(sizeof(char *)*nkeys); cmph_uint8 i; @@ -270,8 +291,9 @@ static char ** brz_read_keys_vd(FILE * graphs_fd, cmph_uint8 nkeys) } keys_vd[i] = (char *)malloc(strlen(buf) + 1); strcpy(keys_vd[i], buf); - free(buf); - } + if(h3 == 6) DEBUGP("key = %s\n", keys_vd[i]); + free(buf); + } return keys_vd; } @@ -389,14 +411,19 @@ void brz_destroy(cmph_t *mphf) { cmph_uint32 i; brz_data_t *data = (brz_data_t *)mphf->data; + fprintf(stderr, "MERDAAAAAA %u\n", data->h3->hashfunc); for(i = 0; i < data->k; i++) { free(data->g[i]); + fprintf(stderr, "MERDAAAAAA1 %u\n", data->h3->hashfunc); hash_state_destroy(data->h1[i]); + fprintf(stderr, "MERDAAAAAA2 %u\n", data->h3->hashfunc); hash_state_destroy(data->h2[i]); + fprintf(stderr, "MERDAAAAAA3 %u\n", data->h3->hashfunc); } - - hash_state_destroy(data->h3); + fprintf(stderr, "MERDAAAAAA %u\n", data->h3->hashfunc); + hash_state_destroy(data->h3); + fprintf(stderr, "MERDAAAAAA1FAB\n"); free(data->g); free(data->h1); free(data->h2); diff --git a/src/cmph.c b/src/cmph.c index 32c1790..f7f4acb 100644 --- a/src/cmph.c +++ b/src/cmph.c @@ -41,12 +41,19 @@ static int key_nlfile_read(void *data, char **key, cmph_uint32 *keylen) static int key_vector_read(void *data, char **key, cmph_uint32 *keylen) { - char **keys = (char **)data; - if (keys + position == NULL) return -1; - *keylen = strlen(*(keys + position)); - *key = (char *)malloc(*keylen); - strcpy(*key, *(keys + position)); + char **keys_vd = (char **)data; + if (keys_vd + position == NULL) return -1; + *keylen = strlen(*(keys_vd + position)); + *key = (char *)malloc(*keylen + 1); + strcpy(*key, *(keys_vd + position)); position ++; + +/* char **keys_vd = (char **)data; + if (keys_vd[position] == NULL) return -1; + *keylen = strlen(keys_vd[position]); + *key = (char *)malloc(*keylen + 1); + strcpy(*key, keys_vd[position]); + position ++;*/ return *keylen; } @@ -56,6 +63,11 @@ static void key_nlfile_dispose(void *data, char *key, cmph_uint32 keylen) free(key); } +static void key_vector_dispose(void *data, char *key, cmph_uint32 keylen) +{ + free(key); +} + static void key_nlfile_rewind(void *data) { FILE *fd = (FILE *)data; @@ -67,6 +79,7 @@ static void key_vector_rewind(void *data) position = 0; } + static cmph_uint32 count_nlfile_keys(FILE *fd) { cmph_uint32 count = 0; @@ -114,7 +127,7 @@ cmph_io_adapter_t *cmph_io_vector_adapter(char ** vector, cmph_uint32 nkeys) key_source->data = (void *)vector; key_source->nkeys = nkeys; key_source->read = key_vector_read; - key_source->dispose = key_nlfile_dispose; + key_source->dispose = key_vector_dispose; key_source->rewind = key_vector_rewind; return key_source; } @@ -136,13 +149,13 @@ void cmph_config_set_algo(cmph_config_t *mph, CMPH_ALGO algo) switch (mph->algo) { case CMPH_CHM: - chm_config_destroy(mph->data); + chm_config_destroy(mph); break; case CMPH_BMZ: - bmz_config_destroy(mph->data); + bmz_config_destroy(mph); break; case CMPH_BRZ: - brz_config_destroy(mph->data); + brz_config_destroy(mph); break; default: assert(0);