From cf5ff6f1405f8f079643d764530dbde4a51fb7eb Mon Sep 17 00:00:00 2001 From: fc_botelho Date: Mon, 24 Jan 2005 20:25:58 +0000 Subject: [PATCH] The file adpater was implemented. --- src/bmz.c | 2 +- src/bmz.h | 2 +- src/cmph.c | 82 +++++++++++++++++++++++++++++++++++++++++++++- src/cmph.h | 9 +++-- src/cmph_structs.c | 2 +- src/cmph_structs.h | 6 ++-- src/czech.c | 2 +- src/czech.h | 2 +- src/main.c | 75 +++++++----------------------------------- 9 files changed, 107 insertions(+), 75 deletions(-) diff --git a/src/bmz.c b/src/bmz.c index 7b29a86..bc75015 100644 --- a/src/bmz.c +++ b/src/bmz.c @@ -27,7 +27,7 @@ static cmph_uint8 bmz_traverse_critical_nodes(bmz_config_data_t *bmz, cmph_uint3 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); -bmz_config_data_t *bmz_config_new(cmph_key_source_t *key_source) +bmz_config_data_t *bmz_config_new(cmph_io_adapter_t *key_source) { bmz_config_data_t *bmz = NULL; bmz = (bmz_config_data_t *)malloc(sizeof(bmz_config_data_t)); diff --git a/src/bmz.h b/src/bmz.h index 7a81a02..13c0f87 100644 --- a/src/bmz.h +++ b/src/bmz.h @@ -6,7 +6,7 @@ typedef struct __bmz_data_t bmz_data_t; typedef struct __bmz_config_data_t bmz_config_data_t; -bmz_config_data_t *bmz_config_new(cmph_key_source_t *key_source); +bmz_config_data_t *bmz_config_new(cmph_io_adapter_t *key_source); void bmz_config_set_hashfuncs(cmph_config_t *mph, CMPH_HASH *hashfuncs); void bmz_config_destroy(cmph_config_t *mph); cmph_t *bmz_new(cmph_config_t *mph, float c); diff --git a/src/cmph.c b/src/cmph.c index bb52106..979b85f 100644 --- a/src/cmph.c +++ b/src/cmph.c @@ -12,7 +12,87 @@ const char *cmph_names[] = { "bmz", "czech", NULL }; /* included -- Fabiano */ -cmph_config_t *cmph_config_new(cmph_key_source_t *key_source) +static int key_nlfile_read(void *data, char **key, cmph_uint32 *keylen) +{ + FILE *fd = (FILE *)data; + *key = NULL; + *keylen = 0; + while(1) + { + char buf[BUFSIZ]; + char *c = fgets(buf, BUFSIZ, fd); + if (c == NULL) return -1; + if (feof(fd)) return -1; + *key = (char *)realloc(*key, *keylen + strlen(buf) + 1); + memcpy(*key + *keylen, buf, strlen(buf)); + *keylen += (cmph_uint32)strlen(buf); + if (buf[strlen(buf) - 1] != '\n') continue; + break; + } + if ((*keylen) && (*key)[*keylen - 1] == '\n') + { + (*key)[(*keylen) - 1] = 0; + --(*keylen); + } + return *keylen; +} + +static void key_nlfile_dispose(void *data, char *key, cmph_uint32 keylen) +{ + free(key); +} +static void key_nlfile_rewind(void *data) +{ + FILE *fd = (FILE *)data; + rewind(fd); +} + +static cmph_uint32 count_nlfile_keys(FILE *fd) +{ + cmph_uint32 count = 0; + rewind(fd); + while(1) + { + char buf[BUFSIZ]; + fgets(buf, BUFSIZ, fd); + if (feof(fd)) break; + if (buf[strlen(buf) - 1] != '\n') continue; + ++count; + } + rewind(fd); + return count; +} + +cmph_io_adapter_t *cmph_io_nlfile_adapter(FILE * keys_fd) +{ + cmph_io_adapter_t * key_source = malloc(sizeof(cmph_io_adapter_t)); + assert(key_source); + key_source->data = (void *)keys_fd; + key_source->nkeys = count_nlfile_keys(keys_fd); + key_source->read = key_nlfile_read; + key_source->dispose = key_nlfile_dispose; + key_source->rewind = key_nlfile_rewind; + return key_source; +} + +cmph_io_adapter_t *cmph_io_nlnkfile_adapter(FILE * keys_fd, cmph_uint32 nkeys) +{ + cmph_io_adapter_t * key_source = malloc(sizeof(cmph_io_adapter_t)); + assert(key_source); + key_source->data = (void *)keys_fd; + key_source->nkeys = nkeys; + key_source->read = key_nlfile_read; + key_source->dispose = key_nlfile_dispose; + key_source->rewind = key_nlfile_rewind; + return key_source; +} + +cmph_io_adapter_t *cmph_io_vector_adapter(const char ** vector, cmph_uint32 nkeys) +{ + return NULL; +} + +cmph_config_t *cmph_config_new(cmph_io_adapter_t *key_source) { cmph_config_t *mph = NULL; mph = __config_new(key_source); diff --git a/src/cmph.h b/src/cmph.h index 2d8d087..1e7da9f 100644 --- a/src/cmph.h +++ b/src/cmph.h @@ -21,10 +21,15 @@ typedef struct int (*read)(void *, char **, cmph_uint32 *); void (*dispose)(void *, char *, cmph_uint32); void (*rewind)(void *); -} cmph_key_source_t; +} cmph_io_adapter_t; + +/** Adapter pattern API **/ +cmph_io_adapter_t *cmph_io_nlfile_adapter(FILE * keys_fd); +cmph_io_adapter_t *cmph_io_nlnkfile_adapter(FILE * keys_fd, cmph_uint32 nkeys); +cmph_io_adapter_t *cmph_io_vector_adapter(const char ** vector, cmph_uint32 nkeys); /** Hash configuration API **/ -cmph_config_t *cmph_config_new(cmph_key_source_t *key_source); +cmph_config_t *cmph_config_new(cmph_io_adapter_t *key_source); void cmph_config_set_hashfuncs(cmph_config_t *mph, CMPH_HASH *hashfuncs); void cmph_config_set_verbosity(cmph_config_t *mph, cmph_uint32 verbosity); void cmph_config_set_graphsize(cmph_config_t *mph, float c); diff --git a/src/cmph_structs.c b/src/cmph_structs.c index 060fbd8..d6c4306 100644 --- a/src/cmph_structs.c +++ b/src/cmph_structs.c @@ -5,7 +5,7 @@ //#define DEBUG #include "debug.h" -cmph_config_t *__config_new(cmph_key_source_t *key_source) +cmph_config_t *__config_new(cmph_io_adapter_t *key_source) { cmph_config_t *mph = (cmph_config_t *)malloc(sizeof(cmph_config_t)); DEBUGP("Creating mph with algorithm %s\n", cmph_names[algo]); diff --git a/src/cmph_structs.h b/src/cmph_structs.h index 436e363..d150a60 100644 --- a/src/cmph_structs.h +++ b/src/cmph_structs.h @@ -8,7 +8,7 @@ struct __config_t { CMPH_ALGO algo; - cmph_key_source_t *key_source; + cmph_io_adapter_t *key_source; cmph_uint32 verbosity; float c; void *data; //algorithm dependent data @@ -20,11 +20,11 @@ struct __cmph_t { CMPH_ALGO algo; cmph_uint32 size; - cmph_key_source_t *key_source; + cmph_io_adapter_t *key_source; void *data; //algorithm dependent data }; -cmph_config_t *__config_new(cmph_key_source_t *key_source); +cmph_config_t *__config_new(cmph_io_adapter_t *key_source); void __config_destroy(); void __cmph_dump(cmph_t *mphf, FILE *); cmph_t *__cmph_load(FILE *f); diff --git a/src/czech.c b/src/czech.c index e7498e0..fe8c3bc 100644 --- a/src/czech.c +++ b/src/czech.c @@ -23,7 +23,7 @@ static int czech_gen_edges(cmph_config_t *mph); static void czech_traverse(czech_config_data_t *czech, cmph_uint8 *visited, cmph_uint32 v); -czech_config_data_t *czech_config_new(cmph_key_source_t *key_source) +czech_config_data_t *czech_config_new(cmph_io_adapter_t *key_source) { czech_config_data_t *czech = NULL; czech = (czech_config_data_t *)malloc(sizeof(czech_config_data_t)); diff --git a/src/czech.h b/src/czech.h index 9070e89..64af13c 100644 --- a/src/czech.h +++ b/src/czech.h @@ -6,7 +6,7 @@ typedef struct __czech_data_t czech_data_t; typedef struct __czech_config_data_t czech_config_data_t; -czech_config_data_t *czech_config_new(cmph_key_source_t *key_source); +czech_config_data_t *czech_config_new(cmph_io_adapter_t *key_source); void czech_config_set_hashfuncs(cmph_config_t *mph, CMPH_HASH *hashfuncs); void czech_config_destroy(cmph_config_t *mph); cmph_t *czech_new(cmph_config_t *mph, float c); diff --git a/src/main.c b/src/main.c index 97e73a6..4b9d0e5 100644 --- a/src/main.c +++ b/src/main.c @@ -40,56 +40,6 @@ void usage_long(const char *prg) fprintf(stderr, " keysfile\t line separated file with keys\n"); } -static int key_read(void *data, char **key, cmph_uint32 *keylen) -{ - FILE *fd = (FILE *)data; - *key = NULL; - *keylen = 0; - while(1) - { - char buf[BUFSIZ]; - char *c = fgets(buf, BUFSIZ, fd); - if (c == NULL) return -1; - if (feof(fd)) return -1; - *key = (char *)realloc(*key, *keylen + strlen(buf) + 1); - memcpy(*key + *keylen, buf, strlen(buf)); - *keylen += (cmph_uint32)strlen(buf); - if (buf[strlen(buf) - 1] != '\n') continue; - break; - } - if ((*keylen) && (*key)[*keylen - 1] == '\n') - { - (*key)[(*keylen) - 1] = 0; - --(*keylen); - } - return *keylen; -} - -static void key_dispose(void *data, char *key, cmph_uint32 keylen) -{ - free(key); -} -static void key_rewind(void *data) -{ - FILE *fd = (FILE *)data; - rewind(fd); -} - -static cmph_uint32 count_keys(FILE *fd) -{ - cmph_uint32 count = 0; - rewind(fd); - while(1) - { - char buf[BUFSIZ]; - fgets(buf, BUFSIZ, fd); - if (feof(fd)) break; - if (buf[strlen(buf) - 1] != '\n') continue; - ++count; - } - rewind(fd); - return count; -} int main(int argc, char **argv) { @@ -109,7 +59,7 @@ int main(int argc, char **argv) cmph_config_t *config = NULL; cmph_t *mphf = NULL; - cmph_key_source_t source; + cmph_io_adapter_t *source; while (1) { @@ -232,18 +182,14 @@ int main(int argc, char **argv) return -1; } - source.data = (void *)keys_fd; if (seed == UINT_MAX) seed = (cmph_uint32)time(NULL); - if(nkeys == UINT_MAX) source.nkeys = count_keys(keys_fd); - else source.nkeys = nkeys; - source.read = key_read; - source.dispose = key_dispose; - source.rewind = key_rewind; - + if(nkeys == UINT_MAX) source = cmph_io_nlfile_adapter(keys_fd); + else source = cmph_io_nlnkfile_adapter(keys_fd, nkeys); + if (generate) { //Create mphf - config = cmph_config_new(&source); + config = cmph_config_new(source); cmph_config_set_algo(config, mph_algo); if (nhashes) cmph_config_set_hashfuncs(config, hashes); cmph_config_set_verbosity(config, verbosity); @@ -288,15 +234,15 @@ int main(int argc, char **argv) free(mphf_file); return -1; } - hashtable = (cmph_uint8*)malloc(source.nkeys*sizeof(cmph_uint8)); - memset(hashtable, 0, source.nkeys); + hashtable = (cmph_uint8*)malloc(source->nkeys*sizeof(cmph_uint8)); + memset(hashtable, 0, source->nkeys); //check all keys - for (i = 0; i < source.nkeys; ++i) + for (i = 0; i < source->nkeys; ++i) { cmph_uint32 h; char *buf; cmph_uint32 buflen = 0; - source.read(source.data, &buf, &buflen); + source->read(source->data, &buf, &buflen); h = cmph_search(mphf, buf, buflen); if(hashtable[h])fprintf(stderr, "collision: %u\n",h); assert(hashtable[h]==0); @@ -305,12 +251,13 @@ int main(int argc, char **argv) { printf("%s -> %u\n", buf, h); } - source.dispose(source.data, buf, buflen); + source->dispose(source->data, buf, buflen); } cmph_destroy(mphf); free(hashtable); } fclose(keys_fd); free(mphf_file); + free(source); return 0; }