[ccan] Help using htable
Zoltán Lajos Kis
zoltan.lajos.kis at ericsson.com
Wed Mar 7 22:05:39 EST 2012
Hi,
I'm trying to use the htable module, but experiencing some strange behaviors.
When I add an element with hash key 0 or 1 to the table, it does not get inserted to the table; only the element counter is increased.
Any idea what is going wrong? Test code below...
Thanks.
Kind Regards,
Zoltan.
=====================================
#include <stdio.h>
#include <stdbool.h>
#include "ccan/htable/htable.h"
struct data {
size_t key;
};
static size_t hash(const void *e, void *unused) {
struct data *d = (struct data *)e;
printf("Hash called.\n");
return d->key;
}
static bool eq(const void *e, void *k) {
struct data *d = (struct data *)e;
size_t *key = (size_t *)k;
printf("Eq called.\n");
return (d->key == *key);
}
int main(void) {
struct htable *table;
size_t key;
struct data *d0 = malloc(sizeof(struct data)); d0->key = 0;
struct data *d1 = malloc(sizeof(struct data)); d1->key = 1;
struct data *d2 = malloc(sizeof(struct data)); d2->key = 2;
struct data *d3 = malloc(sizeof(struct data)); d3->key = 3;
printf("\nTest1\n");
table = malloc(sizeof(struct htable));
htable_init(table, hash, NULL);
htable_add(table, d0->key, d0);
htable_add(table, d1->key, d1);
printf("Elems: %d\n", table->elems);
key = 0;
printf("Get %d: %zu\n", key, (size_t)htable_get(table, key, eq, &key));
key = 1;
printf("Get %d: %zu\n", key, (size_t)htable_get(table, key, eq, &key));
printf("\nTest2\n");
table = malloc(sizeof(struct htable));
htable_init(table, hash, NULL);
htable_add(table, d1->key, d1);
htable_add(table, d0->key, d0);
htable_add(table, d2->key, d2);
printf("Elems: %d\n", table->elems);
key = 0;
printf("Get %d: %zu\n", key, (size_t)htable_get(table, key, eq, &key));
key = 1;
printf("Get %d: %zu\n", key, (size_t)htable_get(table, key, eq, &key));
key = 2;
printf("Get %d: %zu\n", key, (size_t)htable_get(table, key, eq, &key));
printf("\nTest3\n");
table = malloc(sizeof(struct htable));
htable_init(table, hash, NULL);
htable_add(table, d2->key, d2);
htable_add(table, d3->key, d3);
htable_add(table, d0->key, d0);
printf("Elems: %d\n", table->elems);
key = 2;
printf("Get %d: %zu\n", key, (size_t)htable_get(table, key, eq, &key));
key = 3;
printf("Get %d: %zu\n", key, (size_t)htable_get(table, key, eq, &key));
key = 0;
printf("Get %d: %zu\n", key, (size_t)htable_get(table, key, eq, &key));
return 0;
}
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.ozlabs.org/pipermail/ccan/attachments/20120307/be00b1c1/attachment.html>
More information about the ccan
mailing list