[view source]
cp_hashlist *cp_hashlist_create_by_mode(int mode,
long initial_size,
cp_hashfunction hash_fn,
cp_compare_fn compare_fn);
cp_hashlist *cp_hashlist_create_by_option(int mode, long initial_size,
cp_hashfunction cp_hash_fn,
cp_compare_fn compare_fn,
cp_copy_fn copy_key,
cp_destructor_fn key_free,
cp_copy_fn copy_value
cp_destructor_fn free_value);
void cp_hashlist_destroy_by_option(cp_hashlist *list, int mode);
void cp_hashlist_destroy_custom(cp_hashlist *list,
cp_destructor_fn dk,
cp_destructor_fn dv);
#include <cprops/hashlist.h>
#include <stdio.h>
#include <string.h> /* strcmp and strdup */
#include <stdlib.h> /* free */
#include <errno.h> /* perror */
int main(int argc, char *argv[])
{
char key[32];
char value[32];
char *res;
cp_hashlist *t;
cp_hashlist_iterator *i;
int j;
t = cp_hashlist_create_by_option(COLLECTION_MODE_NOSYNC |
COLLECTION_MODE_DEEP |
COLLECTION_MODE_COPY,
10,
cp_hash_string,
(cp_compare_fn) strcmp,
(cp_copy_fn) strdup,
(cp_destructor_fn) free,
(cp_copy_fn) strdup,
(cp_destructor_fn) free);
if (t == NULL)
{
perror(argv[0]);
exit(1);
}
for (j = 0; j < 10; j++)
{
sprintf(key, "ENTRY (%d)", j);
sprintf(value, "VALUE (%d)", j);
if (cp_hashlist_insert(t, key, value) == NULL)
{
perror(argv[0]);
exit(1);
}
}
res = cp_hashlist_get(t, "ENTRY (5)");
printf("looking up entry #5: %s\n", res ? res : "not found");
printf("iterating over list values:\n");
i = cp_hashlist_create_iterator(t, COLLECTION_LOCK_NONE);
while ((res = cp_hashlist_iterator_next_value(i)))
printf("\to\t%s\n", res);
cp_hashlist_iterator_destroy(i);
cp_hashlist_destroy(t);
return 0;
}
for notes on compiling and linking see cprops(3).