Bug 47441 - apr_hash: Allow custom key comparison function
Summary: apr_hash: Allow custom key comparison function
Status: NEW
Alias: None
Product: APR
Classification: Unclassified
Component: APR (show other bugs)
Version: HEAD
Hardware: All All
: P2 enhancement (vote)
Target Milestone: ---
Assignee: Apache Portable Runtime bugs mailinglist
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2009-06-27 13:19 UTC by Neil Conway
Modified: 2009-07-29 16:24 UTC (History)
0 users



Attachments
Candidate patch, v1 (6.57 KB, patch)
2009-06-27 13:19 UTC, Neil Conway
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Neil Conway 2009-06-27 13:19:03 UTC
Created attachment 23892 [details]
Candidate patch, v1

Currently, apr_hash considers two keys to be equal iff memcmp(k1, k2, len) == 0. When hashing on a complex key type (e.g. a struct with a pointer field), that might not be sufficient; that is, the user might want structural equality, not the shallow equality provided by memcmp().

Attached is a straightforward patch against APR trunk that allows the user to supply a custom key comparison function when creating the hash table. I added a new constructor apr_hash_make_custom2() -- if breaking the API is acceptable, it might be cleaner to add another argument to apr_hash_make_custom() instead. The patch includes a test for the new functionality.

BTW, I notice that apr_hashfunc_t takes a key of type "const char *", whereas all the other hash API functions define the type of the key as "const void *". I've used the latter, which is more sensible anyway. This inconsistency might be worth fixing for APR2.

Comments welcome -- please consider this for inclusion in a future release of APR.
Comment 1 Neil Conway 2009-07-29 16:24:45 UTC
Any feedback on this patch? I think this is clearly useful functionality, provided there is any interest in allowing hash table keys to be non-trivial objects.