M
Michael B Allen
I would like to know your preferences regarding a C API for a hash
map. Specifically, when inserting a data item into a hash map there are
two special cases that require consideration.
1) If two keys generate the same hash value should a comparison function
be used to compare keys or should data items be compared? If data items
are compared it is not necessary to store the keys with each entry
which has an impact on question 2.
2) If two keys generate the same hash value and the comparison function
finds the entries to be equal, which of the following resolutions is
preferred?
A) Return an error and otherwise do nothing. The user will be required
to first remove the old item.
B) Provide void **replaced parameters to accept the displaced data item
(and key if they are being stored) and then insert the new data item
in place of the displaced data item.
int hashmap_put(struct hashmap *h, void *key, void *data, void **replaced);
OR
int hashmap_put(struct hashmap *h,
void *key, void *data,
void **replaced_key, void **replaced_data);
C) Delete the data item (and key if they are being stored) using
function pointers provided when the hash map was created and then
insert the new data item in place of the deleted data item.
typedef int (*del_fn)(void *context, void *object);
int hashmap_create(struct hashmap *h,
unsigned int size,
hash_fn hash,
compare_fn cmp,
del_fn key_del,
del_fn data_del,
void *context);
This code will be part of libmba.
Mike
map. Specifically, when inserting a data item into a hash map there are
two special cases that require consideration.
1) If two keys generate the same hash value should a comparison function
be used to compare keys or should data items be compared? If data items
are compared it is not necessary to store the keys with each entry
which has an impact on question 2.
2) If two keys generate the same hash value and the comparison function
finds the entries to be equal, which of the following resolutions is
preferred?
A) Return an error and otherwise do nothing. The user will be required
to first remove the old item.
B) Provide void **replaced parameters to accept the displaced data item
(and key if they are being stored) and then insert the new data item
in place of the displaced data item.
int hashmap_put(struct hashmap *h, void *key, void *data, void **replaced);
OR
int hashmap_put(struct hashmap *h,
void *key, void *data,
void **replaced_key, void **replaced_data);
C) Delete the data item (and key if they are being stored) using
function pointers provided when the hash map was created and then
insert the new data item in place of the deleted data item.
typedef int (*del_fn)(void *context, void *object);
int hashmap_create(struct hashmap *h,
unsigned int size,
hash_fn hash,
compare_fn cmp,
del_fn key_del,
del_fn data_del,
void *context);
This code will be part of libmba.
Mike