summaryrefslogtreecommitdiff
path: root/common/dhash/dhash_example.c
blob: b7de62356ec3bb1f94284f3be0d15d38e1f3e67e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include "dhash.h"

struct my_data_t {
    int foo;
    char bar[128];
};

void delete_callback(hash_entry_t *entry)
{
    if (entry->value.type == HASH_VALUE_PTR) free(entry->value.ptr);
}

bool visit_callback(hash_entry_t *entry, void *user_data)
{
    unsigned long *count = (unsigned long *)user_data;
    struct my_data_t *my_data = (struct my_data_t *) entry->value.ptr;

    (*count)++;

    printf("%s = [foo=%d bar=%s]\n", entry->key.str, my_data->foo, my_data->bar);
    return true;
}

struct my_data_t *new_data(int foo, const char *bar)
{
    struct my_data_t *my_data = malloc(sizeof(struct my_data_t));
    my_data->foo = foo;
    strncpy(my_data->bar, bar, sizeof(my_data->bar));
    return my_data;
}
int main(int argc, char **argv)
{
    static hash_table_t *table = NULL;
    hash_key_t key, *keys;
    hash_value_t value;
    struct hash_iter_context_t *iter;
    hash_entry_t *entry;
    unsigned long i, n_entries;
    int error;
    struct my_data_t *my_data = new_data(1024, "Hello World!");
    unsigned long count;

    /* Create a hash table */
    if ((error = hash_create(10, &table, delete_callback)) != HASH_SUCCESS) {
        fprintf(stderr, "cannot create hash table (%s)\n", hash_error_string(error));
        return error;
    }

    /* Enter a key named "My Data" and specify it's value as a pointer to my_data */
    key.type = HASH_KEY_STRING;
    key.str = strdup("My Data");
    value.type = HASH_VALUE_PTR;
    value.ptr = my_data;

    if ((error = hash_enter(table, &key, &value)) != HASH_SUCCESS) {
        fprintf(stderr, "cannot add to table \"%s\" (%s)\n", key.str, hash_error_string(error));
        return error;
    }

    /* Get a list of keys and print them out, free the list when we're done */
    if ((error = hash_keys(table, &count, &keys)) != HASH_SUCCESS) {
        fprintf(stderr, "cannot get key list (%s)\n", hash_error_string(error));
        return error;
    }

    for (i = 0; i < count; i++)
        printf("key: %s\n", keys[i].str);
    free(keys);

    /* Lookup the key named "My Data" */
    key.type = HASH_KEY_STRING;
    key.str = strdup("My Data");
    if ((error = hash_lookup(table, &key, &value)) != HASH_SUCCESS) {
        fprintf(stderr, "cannot find key \"%s\" (%s)\n", key.str, hash_error_string(error));
    }

    /* Visit each entry in the table, callback will increment count on each visit */
    printf("Iterate using callback\n");
    count = 0;
    hash_iterate(table, visit_callback, &count);

    /* Assure number of visits equal the table size */
    assert(count == hash_count(table));

    /* Visit each entry using iterator object */
    printf("Iterate using iterator\n");
    n_entries = 0;
    iter = new_hash_iter_context(table);
    while ((entry = iter->next(iter)) != NULL) {
        struct my_data_t *data = (struct my_data_t *) entry->value.ptr;

        printf("%s = [foo=%d bar=%s]\n", entry->key.str, data->foo, data->bar);
        n_entries++;
    }
    free(iter);
    /* Assure number of visits equal the table size */
    assert(n_entries == hash_count(table));

    /* Remove the entry, deletion callback will be invoked */
    key.type = HASH_KEY_STRING;
    key.str = strdup("My Data");
    if ((error = hash_delete(table, &key)) != HASH_SUCCESS) {
        fprintf(stderr, "cannot delete from table (%s)\n", hash_error_string(error));
    }

    /* Assure key is no longer in table */
    assert (!hash_has_key(table, &key));

    /* Free the table */
    hash_destroy(table);

    return 0;
}