aboutsummaryrefslogtreecommitdiffstats
path: root/src/util/hash_table.h
diff options
context:
space:
mode:
authorJason Ekstrand <[email protected]>2014-11-24 22:19:50 -0800
committerEric Anholt <[email protected]>2014-12-14 19:32:53 -0800
commit94303a0750f9eaae3fcf598b7bf1320e521898fb (patch)
tree34bd7bea6b61d60c94731333e31ffdca21205a29 /src/util/hash_table.h
parent0d7f4c8658e00d30a1b0c3f2d803378eaa0717c7 (diff)
util/hash_table: Rework the API to know about hashing
Previously, the hash_table API required the user to do all of the hashing of keys as it passed them in. Since the hashing function is intrinsically tied to the comparison function, it makes sense for the hash table to know about it. Also, it makes for a somewhat clumsy API as the user is constantly calling hashing functions many of which have long names. This is especially bad when the standard call looks something like _mesa_hash_table_insert(ht, _mesa_pointer_hash(key), key, data); In the above case, there is no reason why the hash table shouldn't do the hashing for you. We leave the option for you to do your own hashing if it's more efficient, but it's no longer needed. Also, if you do do your own hashing, the hash table will assert that your hash matches what it expects out of the hashing function. This should make it harder to mess up your hashing. v2: change to call the old entrypoint "pre_hashed" rather than "with_hash", like cworth's equivalent change upstream (change by anholt, acked-in-general by Jason). Signed-off-by: Jason Ekstrand <[email protected]> Signed-off-by: Eric Anholt <[email protected]> Reviewed-by: Eric Anholt <[email protected]>
Diffstat (limited to 'src/util/hash_table.h')
-rw-r--r--src/util/hash_table.h19
1 files changed, 15 insertions, 4 deletions
diff --git a/src/util/hash_table.h b/src/util/hash_table.h
index d6b6ebf4069..5561e158454 100644
--- a/src/util/hash_table.h
+++ b/src/util/hash_table.h
@@ -46,6 +46,7 @@ struct hash_entry {
struct hash_table {
struct hash_entry *table;
+ uint32_t (*key_hash_function)(const void *key);
bool (*key_equals_function)(const void *a, const void *b);
const void *deleted_key;
uint32_t size;
@@ -58,6 +59,7 @@ struct hash_table {
struct hash_table *
_mesa_hash_table_create(void *mem_ctx,
+ uint32_t (*key_hash_function)(const void *key),
bool (*key_equals_function)(const void *a,
const void *b));
void _mesa_hash_table_destroy(struct hash_table *ht,
@@ -66,11 +68,15 @@ void _mesa_hash_table_set_deleted_key(struct hash_table *ht,
const void *deleted_key);
struct hash_entry *
-_mesa_hash_table_insert(struct hash_table *ht, uint32_t hash,
- const void *key, void *data);
+_mesa_hash_table_insert(struct hash_table *ht, const void *key, void *data);
struct hash_entry *
-_mesa_hash_table_search(struct hash_table *ht, uint32_t hash,
- const void *key);
+_mesa_hash_table_insert_with_hash(struct hash_table *ht, uint32_t hash,
+ const void *key, void *data);
+struct hash_entry *
+_mesa_hash_table_search(struct hash_table *ht, const void *key);
+struct hash_entry *
+_mesa_hash_table_search_pre_hashed(struct hash_table *ht, uint32_t hash,
+ const void *key);
void _mesa_hash_table_remove(struct hash_table *ht,
struct hash_entry *entry);
@@ -85,6 +91,11 @@ uint32_t _mesa_hash_string(const char *key);
bool _mesa_key_string_equal(const void *a, const void *b);
bool _mesa_key_pointer_equal(const void *a, const void *b);
+static inline uint32_t _mesa_key_hash_string(const void *key)
+{
+ return _mesa_hash_string((const char *)key);
+}
+
static inline uint32_t _mesa_hash_pointer(const void *pointer)
{
return _mesa_hash_data(&pointer, sizeof(pointer));