1
Fork 0

thread-safe vector adapter

This commit is contained in:
fc_botelho 2005-10-10 17:43:21 +00:00
parent a2af7dfa69
commit 26550aad88
5 changed files with 68 additions and 28 deletions

View File

@ -26,7 +26,7 @@ int main(int argc, char **argv)
fprintf(stderr, "Id:%u\n", id); fprintf(stderr, "Id:%u\n", id);
//Destroy hash //Destroy hash
cmph_destroy(hash); cmph_destroy(hash);
free(source); cmph_io_nlfile_adapter_destroy(source);
fclose(keys_fd); fclose(keys_fd);
return 0; return 0;
} }

View File

@ -21,6 +21,6 @@ int main(int argc, char **argv)
fprintf(stderr, "Id:%u\n", id); fprintf(stderr, "Id:%u\n", id);
//Destroy hash //Destroy hash
cmph_destroy(hash); cmph_destroy(hash);
free(source); cmph_io_vector_adapter_destroy(source);
return 0; return 0;
} }

View File

@ -13,7 +13,14 @@
const char *cmph_names[] = { "bmz", "bmz8", "chm", "brz", NULL }; /* included -- Fabiano */ const char *cmph_names[] = { "bmz", "bmz8", "chm", "brz", NULL }; /* included -- Fabiano */
static cmph_uint32 position; // access position when data is a vector typedef struct
{
void *vector;
cmph_uint32 position; // access position when data is a vector
} cmph_vector_t;
static cmph_io_adapter_t *cmph_io_vector_new(void * vector, cmph_uint32 nkeys);
static void cmph_io_vector_destroy(cmph_io_adapter_t * key_source);
static int key_nlfile_read(void *data, char **key, cmph_uint32 *keylen) static int key_nlfile_read(void *data, char **key, cmph_uint32 *keylen)
{ {
@ -42,19 +49,14 @@ static int key_nlfile_read(void *data, char **key, cmph_uint32 *keylen)
static int key_vector_read(void *data, char **key, cmph_uint32 *keylen) static int key_vector_read(void *data, char **key, cmph_uint32 *keylen)
{ {
char **keys_vd = (char **)data; cmph_vector_t *cmph_vector = (cmph_vector_t *)data;
if (keys_vd + position == NULL) return -1; char **keys_vd = (char **)cmph_vector->vector;
*keylen = strlen(*(keys_vd + position));
*key = (char *)malloc(*keylen + 1);
strcpy(*key, *(keys_vd + position));
position ++;
/* char **keys_vd = (char **)data; if (keys_vd + cmph_vector->position == NULL) return -1;
if (keys_vd[position] == NULL) return -1; *keylen = strlen(*(keys_vd + cmph_vector->position));
*keylen = strlen(keys_vd[position]);
*key = (char *)malloc(*keylen + 1); *key = (char *)malloc(*keylen + 1);
strcpy(*key, keys_vd[position]); strcpy(*key, *(keys_vd + cmph_vector->position));
position ++;*/ cmph_vector->position = cmph_vector->position + 1;
return *keylen; return *keylen;
} }
@ -66,7 +68,7 @@ static void key_nlfile_dispose(void *data, char *key, cmph_uint32 keylen)
static void key_vector_dispose(void *data, char *key, cmph_uint32 keylen) static void key_vector_dispose(void *data, char *key, cmph_uint32 keylen)
{ {
free(key); key_nlfile_dispose(data, key, keylen);
} }
static void key_nlfile_rewind(void *data) static void key_nlfile_rewind(void *data)
@ -77,10 +79,10 @@ static void key_nlfile_rewind(void *data)
static void key_vector_rewind(void *data) static void key_vector_rewind(void *data)
{ {
position = 0; cmph_vector_t *cmph_vector = (cmph_vector_t *)data;
cmph_vector->position = 0;
} }
static cmph_uint32 count_nlfile_keys(FILE *fd) static cmph_uint32 count_nlfile_keys(FILE *fd)
{ {
cmph_uint32 count = 0; cmph_uint32 count = 0;
@ -109,6 +111,11 @@ cmph_io_adapter_t *cmph_io_nlfile_adapter(FILE * keys_fd)
return key_source; return key_source;
} }
void cmph_io_nlfile_adapter_destroy(cmph_io_adapter_t * key_source)
{
free(key_source);
}
cmph_io_adapter_t *cmph_io_nlnkfile_adapter(FILE * keys_fd, cmph_uint32 nkeys) 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)); cmph_io_adapter_t * key_source = malloc(sizeof(cmph_io_adapter_t));
@ -121,18 +128,46 @@ cmph_io_adapter_t *cmph_io_nlnkfile_adapter(FILE * keys_fd, cmph_uint32 nkeys)
return key_source; return key_source;
} }
cmph_io_adapter_t *cmph_io_vector_adapter(char ** vector, cmph_uint32 nkeys) void cmph_io_nlnkfile_adapter_destroy(cmph_io_adapter_t * key_source)
{
free(key_source);
}
static cmph_io_adapter_t *cmph_io_vector_new(void * vector, cmph_uint32 nkeys)
{ {
cmph_io_adapter_t * key_source = malloc(sizeof(cmph_io_adapter_t)); cmph_io_adapter_t * key_source = malloc(sizeof(cmph_io_adapter_t));
cmph_vector_t * cmph_vector = malloc(sizeof(cmph_vector_t));
assert(key_source); assert(key_source);
key_source->data = (void *)vector; assert(cmph_vector);
cmph_vector->vector = vector;
cmph_vector->position = 0;
key_source->data = (void *)cmph_vector;
key_source->nkeys = nkeys; key_source->nkeys = nkeys;
return key_source;
}
static void cmph_io_vector_destroy(cmph_io_adapter_t * key_source)
{
cmph_vector_t *cmph_vector = (cmph_vector_t *)key_source->data;
cmph_vector->vector = NULL;
free(cmph_vector);
free(key_source);
}
cmph_io_adapter_t *cmph_io_vector_adapter(char ** vector, cmph_uint32 nkeys)
{
cmph_io_adapter_t * key_source = cmph_io_vector_new(vector, nkeys);
key_source->read = key_vector_read; key_source->read = key_vector_read;
key_source->dispose = key_vector_dispose; key_source->dispose = key_vector_dispose;
key_source->rewind = key_vector_rewind; key_source->rewind = key_vector_rewind;
return key_source; return key_source;
} }
void cmph_io_vector_adapter_destroy(cmph_io_adapter_t * key_source)
{
cmph_io_vector_destroy(key_source);
}
cmph_config_t *cmph_config_new(cmph_io_adapter_t *key_source) cmph_config_t *cmph_config_new(cmph_io_adapter_t *key_source)
{ {
cmph_config_t *mph = NULL; cmph_config_t *mph = NULL;

View File

@ -26,8 +26,13 @@ typedef struct
/** Adapter pattern API **/ /** Adapter pattern API **/
/* please call free() in the created adapters */ /* please call free() in the created adapters */
cmph_io_adapter_t *cmph_io_nlfile_adapter(FILE * keys_fd); cmph_io_adapter_t *cmph_io_nlfile_adapter(FILE * keys_fd);
void cmph_io_nlfile_adapter_destroy(cmph_io_adapter_t * key_source);
cmph_io_adapter_t *cmph_io_nlnkfile_adapter(FILE * keys_fd, cmph_uint32 nkeys); cmph_io_adapter_t *cmph_io_nlnkfile_adapter(FILE * keys_fd, cmph_uint32 nkeys);
void cmph_io_nlnkfile_adapter_destroy(cmph_io_adapter_t * key_source);
cmph_io_adapter_t *cmph_io_vector_adapter(char ** vector, cmph_uint32 nkeys); cmph_io_adapter_t *cmph_io_vector_adapter(char ** vector, cmph_uint32 nkeys);
void cmph_io_vector_adapter_destroy(cmph_io_adapter_t * key_source);
/** Hash configuration API **/ /** Hash configuration API **/
cmph_config_t *cmph_config_new(cmph_io_adapter_t *key_source); cmph_config_t *cmph_config_new(cmph_io_adapter_t *key_source);

View File

@ -7,7 +7,7 @@ int main(int argc, char **argv)
{ {
graph_iterator_t it; graph_iterator_t it;
cmph_uint32 i, neighbor; cmph_uint32 i, neighbor;
graph_t *g = graph_new(5, 10, 0); graph_t *g = graph_new(5, 10);
fprintf(stderr, "Building random graph\n"); fprintf(stderr, "Building random graph\n");
for (i = 0; i < 10; ++i) for (i = 0; i < 10; ++i)
@ -26,7 +26,7 @@ int main(int argc, char **argv)
graph_destroy(g); graph_destroy(g);
fprintf(stderr, "Building cyclic graph\n"); fprintf(stderr, "Building cyclic graph\n");
g = graph_new(4, 5, 0); g = graph_new(4, 5);
graph_add_edge(g, 0, 3); graph_add_edge(g, 0, 3);
graph_add_edge(g, 0, 1); graph_add_edge(g, 0, 1);
graph_add_edge(g, 1, 2); graph_add_edge(g, 1, 2);
@ -38,7 +38,7 @@ int main(int argc, char **argv)
graph_destroy(g); graph_destroy(g);
fprintf(stderr, "Building non-cyclic graph\n"); fprintf(stderr, "Building non-cyclic graph\n");
g = graph_new(5, 4, 0); g = graph_new(5, 4);
graph_add_edge(g, 0, 1); graph_add_edge(g, 0, 1);
graph_add_edge(g, 1, 2); graph_add_edge(g, 1, 2);
graph_add_edge(g, 2, 3); graph_add_edge(g, 2, 3);