| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
#include <stdio.h> |
| 22 |
#include <string.h> |
| 23 |
#include <stdlib.h> |
| 24 |
#include <assert.h> |
| 25 |
#include "dhash.h" |
| 26 |
|
| 27 |
struct my_data_t { |
| 28 |
int foo; |
| 29 |
char bar[128]; |
| 30 |
}; |
| 31 |
|
| 32 |
static void delete_callback(hash_entry_t *entry, |
| 33 |
hash_destroy_enum type, |
| 34 |
void *pvt) |
| 35 |
{ |
| 36 |
if (entry->value.type == HASH_VALUE_PTR) free(entry->value.ptr); |
| 37 |
} |
| 38 |
|
| 39 |
static bool visit_callback(hash_entry_t *entry, |
| 40 |
void *user_data) |
| 41 |
{ |
| 42 |
unsigned long *count = (unsigned long *)user_data; |
| 43 |
struct my_data_t *my_data = (struct my_data_t *) entry->value.ptr; |
| 44 |
|
| 45 |
(*count)++; |
| 46 |
|
| 47 |
printf("%s = [foo=%d bar=%s]\n", entry->key.str, my_data->foo, my_data->bar); |
| 48 |
return true; |
| 49 |
} |
| 50 |
|
| 51 |
static struct my_data_t *new_data(int foo, const char *bar) |
| 52 |
{ |
| 53 |
struct my_data_t *my_data = malloc(sizeof(struct my_data_t)); |
| 54 |
my_data->foo = foo; |
| 55 |
strncpy(my_data->bar, bar, sizeof(my_data->bar)); |
| 56 |
return my_data; |
| 57 |
} |
| 58 |
|
| 59 |
int main(int argc, char **argv) |
| 60 |
{ |
| 61 |
static hash_table_t *table = NULL; |
| 62 |
hash_key_t key, *keys; |
| 63 |
hash_value_t value; |
| 64 |
struct hash_iter_context_t *iter; |
| 65 |
hash_entry_t *entry; |
| 66 |
unsigned long i, n_entries; |
| 67 |
int error; |
| 68 |
struct my_data_t *my_data = new_data(1024, "Hello World!"); |
| 69 |
unsigned long count; |
| 70 |
|
| 71 |
|
| 72 |
error = hash_create(10, &table, delete_callback, NULL); |
| 73 |
if (error != HASH_SUCCESS) { |
| 74 |
fprintf(stderr, "cannot create hash table (%s)\n", hash_error_string(error)); |
| 75 |
return error; |
| 76 |
} |
| 77 |
|
| 78 |
|
| 79 |
key.type = HASH_KEY_STRING; |
| 80 |
key.str = strdup("My Data"); |
| 81 |
value.type = HASH_VALUE_PTR; |
| 82 |
value.ptr = my_data; |
| 83 |
|
| 84 |
if ((error = hash_enter(table, &key, &value)) != HASH_SUCCESS) { |
| 85 |
fprintf(stderr, "cannot add to table \"%s\" (%s)\n", key.str, hash_error_string(error)); |
| 86 |
return error; |
| 87 |
} |
| 88 |
free(key.str); |
| 89 |
|
| 90 |
|
| 91 |
if ((error = hash_keys(table, &count, &keys)) != HASH_SUCCESS) { |
| 92 |
fprintf(stderr, "cannot get key list (%s)\n", hash_error_string(error)); |
| 93 |
return error; |
| 94 |
} |
| 95 |
|
| 96 |
for (i = 0; i < count; i++) |
| 97 |
printf("key: %s\n", keys[i].str); |
| 98 |
free(keys); |
| 99 |
|
| 100 |
|
| 101 |
key.type = HASH_KEY_STRING; |
| 102 |
key.str = strdup("My Data"); |
| 103 |
if ((error = hash_lookup(table, &key, &value)) != HASH_SUCCESS) { |
| 104 |
fprintf(stderr, "cannot find key \"%s\" (%s)\n", key.str, hash_error_string(error)); |
| 105 |
} |
| 106 |
free(key.str); |
| 107 |
|
| 108 |
|
| 109 |
printf("Iterate using callback\n"); |
| 110 |
count = 0; |
| 111 |
hash_iterate(table, visit_callback, &count); |
| 112 |
|
| 113 |
|
| 114 |
assert(count == hash_count(table)); |
| 115 |
|
| 116 |
|
| 117 |
printf("Iterate using iterator\n"); |
| 118 |
n_entries = 0; |
| 119 |
iter = new_hash_iter_context(table); |
| 120 |
while ((entry = iter->next(iter)) != NULL) { |
| 121 |
struct my_data_t *data = (struct my_data_t *) entry->value.ptr; |
| 122 |
|
| 123 |
printf("%s = [foo=%d bar=%s]\n", entry->key.str, data->foo, data->bar); |
| 124 |
n_entries++; |
| 125 |
} |
| 126 |
free(iter); |
| 127 |
|
| 128 |
assert(n_entries == hash_count(table)); |
| 129 |
|
| 130 |
|
| 131 |
key.type = HASH_KEY_STRING; |
| 132 |
key.str = strdup("My Data"); |
| 133 |
if ((error = hash_delete(table, &key)) != HASH_SUCCESS) { |
| 134 |
fprintf(stderr, "cannot delete from table (%s)\n", hash_error_string(error)); |
| 135 |
} |
| 136 |
|
| 137 |
|
| 138 |
assert (!hash_has_key(table, &key)); |
| 139 |
free(key.str); |
| 140 |
|
| 141 |
|
| 142 |
hash_destroy(table); |
| 143 |
|
| 144 |
return 0; |
| 145 |
} |