The file adpater was implemented.
This commit is contained in:
parent
8ea43ca39f
commit
cf5ff6f140
@ -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 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);
|
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_config_data_t *bmz = NULL;
|
||||||
bmz = (bmz_config_data_t *)malloc(sizeof(bmz_config_data_t));
|
bmz = (bmz_config_data_t *)malloc(sizeof(bmz_config_data_t));
|
||||||
|
@ -6,7 +6,7 @@
|
|||||||
typedef struct __bmz_data_t bmz_data_t;
|
typedef struct __bmz_data_t bmz_data_t;
|
||||||
typedef struct __bmz_config_data_t bmz_config_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_set_hashfuncs(cmph_config_t *mph, CMPH_HASH *hashfuncs);
|
||||||
void bmz_config_destroy(cmph_config_t *mph);
|
void bmz_config_destroy(cmph_config_t *mph);
|
||||||
cmph_t *bmz_new(cmph_config_t *mph, float c);
|
cmph_t *bmz_new(cmph_config_t *mph, float c);
|
||||||
|
82
src/cmph.c
82
src/cmph.c
@ -12,7 +12,87 @@
|
|||||||
|
|
||||||
const char *cmph_names[] = { "bmz", "czech", NULL }; /* included -- Fabiano */
|
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;
|
cmph_config_t *mph = NULL;
|
||||||
mph = __config_new(key_source);
|
mph = __config_new(key_source);
|
||||||
|
@ -21,10 +21,15 @@ typedef struct
|
|||||||
int (*read)(void *, char **, cmph_uint32 *);
|
int (*read)(void *, char **, cmph_uint32 *);
|
||||||
void (*dispose)(void *, char *, cmph_uint32);
|
void (*dispose)(void *, char *, cmph_uint32);
|
||||||
void (*rewind)(void *);
|
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 **/
|
/** 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_hashfuncs(cmph_config_t *mph, CMPH_HASH *hashfuncs);
|
||||||
void cmph_config_set_verbosity(cmph_config_t *mph, cmph_uint32 verbosity);
|
void cmph_config_set_verbosity(cmph_config_t *mph, cmph_uint32 verbosity);
|
||||||
void cmph_config_set_graphsize(cmph_config_t *mph, float c);
|
void cmph_config_set_graphsize(cmph_config_t *mph, float c);
|
||||||
|
@ -5,7 +5,7 @@
|
|||||||
//#define DEBUG
|
//#define DEBUG
|
||||||
#include "debug.h"
|
#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));
|
cmph_config_t *mph = (cmph_config_t *)malloc(sizeof(cmph_config_t));
|
||||||
DEBUGP("Creating mph with algorithm %s\n", cmph_names[algo]);
|
DEBUGP("Creating mph with algorithm %s\n", cmph_names[algo]);
|
||||||
|
@ -8,7 +8,7 @@
|
|||||||
struct __config_t
|
struct __config_t
|
||||||
{
|
{
|
||||||
CMPH_ALGO algo;
|
CMPH_ALGO algo;
|
||||||
cmph_key_source_t *key_source;
|
cmph_io_adapter_t *key_source;
|
||||||
cmph_uint32 verbosity;
|
cmph_uint32 verbosity;
|
||||||
float c;
|
float c;
|
||||||
void *data; //algorithm dependent data
|
void *data; //algorithm dependent data
|
||||||
@ -20,11 +20,11 @@ struct __cmph_t
|
|||||||
{
|
{
|
||||||
CMPH_ALGO algo;
|
CMPH_ALGO algo;
|
||||||
cmph_uint32 size;
|
cmph_uint32 size;
|
||||||
cmph_key_source_t *key_source;
|
cmph_io_adapter_t *key_source;
|
||||||
void *data; //algorithm dependent data
|
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 __config_destroy();
|
||||||
void __cmph_dump(cmph_t *mphf, FILE *);
|
void __cmph_dump(cmph_t *mphf, FILE *);
|
||||||
cmph_t *__cmph_load(FILE *f);
|
cmph_t *__cmph_load(FILE *f);
|
||||||
|
@ -23,7 +23,7 @@
|
|||||||
static int czech_gen_edges(cmph_config_t *mph);
|
static int czech_gen_edges(cmph_config_t *mph);
|
||||||
static void czech_traverse(czech_config_data_t *czech, cmph_uint8 *visited, cmph_uint32 v);
|
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_config_data_t *czech = NULL;
|
||||||
czech = (czech_config_data_t *)malloc(sizeof(czech_config_data_t));
|
czech = (czech_config_data_t *)malloc(sizeof(czech_config_data_t));
|
||||||
|
@ -6,7 +6,7 @@
|
|||||||
typedef struct __czech_data_t czech_data_t;
|
typedef struct __czech_data_t czech_data_t;
|
||||||
typedef struct __czech_config_data_t czech_config_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_set_hashfuncs(cmph_config_t *mph, CMPH_HASH *hashfuncs);
|
||||||
void czech_config_destroy(cmph_config_t *mph);
|
void czech_config_destroy(cmph_config_t *mph);
|
||||||
cmph_t *czech_new(cmph_config_t *mph, float c);
|
cmph_t *czech_new(cmph_config_t *mph, float c);
|
||||||
|
75
src/main.c
75
src/main.c
@ -40,56 +40,6 @@ void usage_long(const char *prg)
|
|||||||
fprintf(stderr, " keysfile\t line separated file with keys\n");
|
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)
|
int main(int argc, char **argv)
|
||||||
{
|
{
|
||||||
@ -109,7 +59,7 @@ int main(int argc, char **argv)
|
|||||||
cmph_config_t *config = NULL;
|
cmph_config_t *config = NULL;
|
||||||
cmph_t *mphf = NULL;
|
cmph_t *mphf = NULL;
|
||||||
|
|
||||||
cmph_key_source_t source;
|
cmph_io_adapter_t *source;
|
||||||
|
|
||||||
while (1)
|
while (1)
|
||||||
{
|
{
|
||||||
@ -232,18 +182,14 @@ int main(int argc, char **argv)
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
source.data = (void *)keys_fd;
|
|
||||||
if (seed == UINT_MAX) seed = (cmph_uint32)time(NULL);
|
if (seed == UINT_MAX) seed = (cmph_uint32)time(NULL);
|
||||||
if(nkeys == UINT_MAX) source.nkeys = count_keys(keys_fd);
|
if(nkeys == UINT_MAX) source = cmph_io_nlfile_adapter(keys_fd);
|
||||||
else source.nkeys = nkeys;
|
else source = cmph_io_nlnkfile_adapter(keys_fd, nkeys);
|
||||||
source.read = key_read;
|
|
||||||
source.dispose = key_dispose;
|
|
||||||
source.rewind = key_rewind;
|
|
||||||
|
|
||||||
if (generate)
|
if (generate)
|
||||||
{
|
{
|
||||||
//Create mphf
|
//Create mphf
|
||||||
config = cmph_config_new(&source);
|
config = cmph_config_new(source);
|
||||||
cmph_config_set_algo(config, mph_algo);
|
cmph_config_set_algo(config, mph_algo);
|
||||||
if (nhashes) cmph_config_set_hashfuncs(config, hashes);
|
if (nhashes) cmph_config_set_hashfuncs(config, hashes);
|
||||||
cmph_config_set_verbosity(config, verbosity);
|
cmph_config_set_verbosity(config, verbosity);
|
||||||
@ -288,15 +234,15 @@ int main(int argc, char **argv)
|
|||||||
free(mphf_file);
|
free(mphf_file);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
hashtable = (cmph_uint8*)malloc(source.nkeys*sizeof(cmph_uint8));
|
hashtable = (cmph_uint8*)malloc(source->nkeys*sizeof(cmph_uint8));
|
||||||
memset(hashtable, 0, source.nkeys);
|
memset(hashtable, 0, source->nkeys);
|
||||||
//check all keys
|
//check all keys
|
||||||
for (i = 0; i < source.nkeys; ++i)
|
for (i = 0; i < source->nkeys; ++i)
|
||||||
{
|
{
|
||||||
cmph_uint32 h;
|
cmph_uint32 h;
|
||||||
char *buf;
|
char *buf;
|
||||||
cmph_uint32 buflen = 0;
|
cmph_uint32 buflen = 0;
|
||||||
source.read(source.data, &buf, &buflen);
|
source->read(source->data, &buf, &buflen);
|
||||||
h = cmph_search(mphf, buf, buflen);
|
h = cmph_search(mphf, buf, buflen);
|
||||||
if(hashtable[h])fprintf(stderr, "collision: %u\n",h);
|
if(hashtable[h])fprintf(stderr, "collision: %u\n",h);
|
||||||
assert(hashtable[h]==0);
|
assert(hashtable[h]==0);
|
||||||
@ -305,12 +251,13 @@ int main(int argc, char **argv)
|
|||||||
{
|
{
|
||||||
printf("%s -> %u\n", buf, h);
|
printf("%s -> %u\n", buf, h);
|
||||||
}
|
}
|
||||||
source.dispose(source.data, buf, buflen);
|
source->dispose(source->data, buf, buflen);
|
||||||
}
|
}
|
||||||
cmph_destroy(mphf);
|
cmph_destroy(mphf);
|
||||||
free(hashtable);
|
free(hashtable);
|
||||||
}
|
}
|
||||||
fclose(keys_fd);
|
fclose(keys_fd);
|
||||||
free(mphf_file);
|
free(mphf_file);
|
||||||
|
free(source);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user