thread-safe vector adapter
This commit is contained in:
parent
da288dc849
commit
312947b34f
|
@ -26,7 +26,7 @@ int main(int argc, char **argv)
|
|||
fprintf(stderr, "Id:%u\n", id);
|
||||
//Destroy hash
|
||||
cmph_destroy(hash);
|
||||
free(source);
|
||||
cmph_io_nlfile_adapter_destroy(source);
|
||||
fclose(keys_fd);
|
||||
return 0;
|
||||
}
|
||||
|
|
|
@ -21,6 +21,6 @@ int main(int argc, char **argv)
|
|||
fprintf(stderr, "Id:%u\n", id);
|
||||
//Destroy hash
|
||||
cmph_destroy(hash);
|
||||
free(source);
|
||||
cmph_io_vector_adapter_destroy(source);
|
||||
return 0;
|
||||
}
|
||||
|
|
69
src/cmph.c
69
src/cmph.c
|
@ -13,7 +13,14 @@
|
|||
|
||||
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)
|
||||
{
|
||||
|
@ -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)
|
||||
{
|
||||
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 ++;
|
||||
cmph_vector_t *cmph_vector = (cmph_vector_t *)data;
|
||||
char **keys_vd = (char **)cmph_vector->vector;
|
||||
|
||||
/* char **keys_vd = (char **)data;
|
||||
if (keys_vd[position] == NULL) return -1;
|
||||
*keylen = strlen(keys_vd[position]);
|
||||
if (keys_vd + cmph_vector->position == NULL) return -1;
|
||||
*keylen = strlen(*(keys_vd + cmph_vector->position));
|
||||
*key = (char *)malloc(*keylen + 1);
|
||||
strcpy(*key, keys_vd[position]);
|
||||
position ++;*/
|
||||
strcpy(*key, *(keys_vd + cmph_vector->position));
|
||||
cmph_vector->position = cmph_vector->position + 1;
|
||||
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)
|
||||
{
|
||||
free(key);
|
||||
key_nlfile_dispose(data, key, keylen);
|
||||
}
|
||||
|
||||
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)
|
||||
{
|
||||
position = 0;
|
||||
cmph_vector_t *cmph_vector = (cmph_vector_t *)data;
|
||||
cmph_vector->position = 0;
|
||||
}
|
||||
|
||||
|
||||
static cmph_uint32 count_nlfile_keys(FILE *fd)
|
||||
{
|
||||
cmph_uint32 count = 0;
|
||||
|
@ -109,6 +111,11 @@ cmph_io_adapter_t *cmph_io_nlfile_adapter(FILE * keys_fd)
|
|||
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 * 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;
|
||||
}
|
||||
|
||||
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_vector_t * cmph_vector = malloc(sizeof(cmph_vector_t));
|
||||
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;
|
||||
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->dispose = key_vector_dispose;
|
||||
key_source->rewind = key_vector_rewind;
|
||||
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 *mph = NULL;
|
||||
|
|
|
@ -26,8 +26,13 @@ typedef struct
|
|||
/** Adapter pattern API **/
|
||||
/* please call free() in the created adapters */
|
||||
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);
|
||||
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);
|
||||
void cmph_io_vector_adapter_destroy(cmph_io_adapter_t * key_source);
|
||||
|
||||
/** Hash configuration API **/
|
||||
cmph_config_t *cmph_config_new(cmph_io_adapter_t *key_source);
|
||||
|
|
|
@ -7,7 +7,7 @@ int main(int argc, char **argv)
|
|||
{
|
||||
graph_iterator_t it;
|
||||
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");
|
||||
for (i = 0; i < 10; ++i)
|
||||
|
@ -26,7 +26,7 @@ int main(int argc, char **argv)
|
|||
graph_destroy(g);
|
||||
|
||||
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, 1);
|
||||
graph_add_edge(g, 1, 2);
|
||||
|
@ -38,7 +38,7 @@ int main(int argc, char **argv)
|
|||
graph_destroy(g);
|
||||
|
||||
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, 1, 2);
|
||||
graph_add_edge(g, 2, 3);
|
||||
|
|
Loading…
Reference in New Issue