10 struct HashNode
*next
;
20 struct HashNode
*table
[];
23 struct HashTable
*hashtable_new(HashFunc func
, HashValueCmpFunc cmp
, int size
,
26 struct HashTable
*ht
= malloc(sizeof(*ht
) + size
* sizeof(ht
->table
[0]));
31 ht
->elemSize
= elemSize
;
32 memset(ht
->table
, 0, ht
->size
* sizeof(ht
->table
[0]));
36 void hashtable_free(struct HashTable
*ht
)
40 for (i
= 0; i
< ht
->size
; i
++)
42 struct HashNode
*node
= ht
->table
[i
];
46 struct HashNode
*next
= node
->next
;
55 void hashtable_insert(struct HashTable
*ht
, const void *value
)
57 unsigned int key
= ht
->func(value
) % ht
->size
;
58 struct HashNode
*node
= malloc(sizeof(*node
) + ht
->elemSize
);
61 memcpy(node
->value
, value
, ht
->elemSize
);
63 if (ht
->table
[key
] == NULL
)
65 ht
->table
[key
] = node
;
69 struct HashNode
*parent
= ht
->table
[key
];
71 while (parent
->next
!= NULL
)
72 parent
= parent
->next
;
77 void *hashtable_query(struct HashTable
*ht
, const void *value
)
79 unsigned int key
= ht
->func(value
) % ht
->size
;
80 struct HashNode
*node
= ht
->table
[key
];
84 if (ht
->cmp(node
->value
, value
))