Debian Codegraph @

dhash/examples/dhash_example.c

raw symbols SCIP
1 /*
2 Authors:
3 John Dennis <jdennis.redhat.com>
4
5 Copyright (C) 2009 Red Hat
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU Lesser General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
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 /* Create a hash table */
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 /* Enter a key named "My Data" and specify it's value as a pointer to my_data */
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 /* Get a list of keys and print them out, free the list when we're done */
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 /* Lookup the key named "My Data" */
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 /* Visit each entry in the table, callback will increment count on each visit */
109 printf("Iterate using callback\n");
110 count = 0;
111 hash_iterate(table, visit_callback, &count);
112
113 /* Assure number of visits equal the table size */
114 assert(count == hash_count(table));
115
116 /* Visit each entry using iterator object */
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 /* Assure number of visits equal the table size */
128 assert(n_entries == hash_count(table));
129
130 /* Remove the entry, deletion callback will be invoked */
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 /* Assure key is no longer in table */
138 assert (!hash_has_key(table, &key));
139 free(key.str);
140
141 /* Free the table */
142 hash_destroy(table);
143
144 return 0;
145 }