#ifndef HASH_TABLE_H
#define HASH_TABLE_H
-struct hash_table;
+#include <string.h>
+#include <stdbool.h>
+#include <stdlib.h>
+#include <stdint.h>
+#include <limits.h>
+#include <assert.h>
+#include "util/hash_table.h"
+
+struct string_to_uint_map;
+
+#ifdef __cplusplus
+extern "C" {
+#endif
typedef unsigned (*hash_func_t)(const void *key);
-typedef int (*hash_compare_func_t)(const void *key1, const void *key2);
+typedef bool (*hash_compare_func_t)(const void *key1, const void *key2);
/**
* Hash table constructor
* \param hash Function used to compute hash value of input keys.
* \param compare Function used to compare keys.
*/
-extern struct hash_table *hash_table_ctor(unsigned num_buckets,
- hash_func_t hash, hash_compare_func_t compare);
-
+static inline struct hash_table *hash_table_ctor(UNUSED unsigned num_buckets,
+ hash_func_t hash, hash_compare_func_t compare)
+{
+ return _mesa_hash_table_create(NULL, hash, compare);
+}
/**
* Release all memory associated with a hash table
*
* \warning
- * This function cannot release memory occupied either by keys or data.
+ * This function does not release memory occupied either by keys or data.
*/
-extern void hash_table_dtor(struct hash_table *ht);
-
+static inline void hash_table_dtor(struct hash_table *ht)
+{
+ return _mesa_hash_table_destroy(ht, NULL);
+}
/**
* Flush all entries from a hash table
*
* \param ht Table to be cleared of its entries.
*/
-extern void hash_table_clear(struct hash_table *ht);
-
+static inline void hash_table_clear(struct hash_table *ht)
+{
+ return _mesa_hash_table_clear(ht, NULL);
+}
/**
* Search a hash table for a specific element
* the matching key was added. If no matching key exists in the table,
* \c NULL is returned.
*/
-extern void *hash_table_find(struct hash_table *ht, const void *key);
-
+static inline void *hash_table_find(struct hash_table *ht, const void *key)
+{
+ struct hash_entry *entry = _mesa_hash_table_search(ht, key);
+ if (!entry)
+ return NULL;
+ return entry->data;
+}
/**
* Add an element to a hash table
+ *
+ * \warning
+ * The value passed by \c key is kept in the hash table and is used by later
+ * calls to \c hash_table_find.
+ */
+static inline void hash_table_insert(struct hash_table *ht, void *data,
+ const void *key)
+{
+ _mesa_hash_table_insert(ht, key, data);
+}
+
+/**
+ * Remove a specific element from a hash table.
*/
-extern void hash_table_insert(struct hash_table *ht, void *data,
- const void *key);
+static inline void hash_table_remove(struct hash_table *ht, const void *key)
+{
+ struct hash_entry *entry = _mesa_hash_table_search(ht, key);
+ if (entry)
+ _mesa_hash_table_remove(ht, entry);
+}
/**
* Compute hash value of a string
*
- * Computes the hash value of a string using the DJB2 algorithm developed by
- * Professor Daniel J. Bernstein. It was published on comp.lang.c once upon
- * a time. I was unable to find the original posting in the archives.
- *
* \param key Pointer to a NUL terminated string to be hashed.
*
* \sa hash_table_string_compare
*/
-extern unsigned hash_table_string_hash(const void *key);
-
+static unsigned
+hash_table_string_hash(const void *key)
+{
+ const char *str = (const char *) key;
+ uint32_t hash = _mesa_hash_string(str);
+ return hash;
+}
/**
* Compare two strings used as keys
*
- * This is just a macro wrapper around \c strcmp.
+ * This is just a wrapper around \c strcmp.
*
* \sa hash_table_string_hash
*/
-#define hash_table_string_compare ((hash_compare_func_t) strcmp)
+static bool
+hash_table_string_compare(const void *a, const void *b)
+{
+ return _mesa_key_string_equal(a, b);
+}
+
+/**
+ * Compute hash value of a pointer
+ *
+ * \param key Pointer to be used as a hash key
+ *
+ * \note
+ * The memory pointed to by \c key is \b never accessed. The value of \c key
+ * itself is used as the hash key
+ *
+ * \sa hash_table_pointer_compare
+ */
+static unsigned
+hash_table_pointer_hash(const void *key)
+{
+ return _mesa_hash_pointer(key);
+}
+
+/**
+ * Compare two pointers used as keys
+ *
+ * \sa hash_table_pointer_hash
+ */
+static bool
+hash_table_pointer_compare(const void *key1, const void *key2)
+{
+ return _mesa_key_pointer_equal(key1, key2);
+}
+
+struct string_to_uint_map *
+string_to_uint_map_ctor();
+
+void
+string_to_uint_map_dtor(struct string_to_uint_map *);
+
+
+#ifdef __cplusplus
+}
+
+struct string_map_iterate_wrapper_closure {
+ void (*callback)(const char *key, unsigned value, void *closure);
+ void *closure;
+};
+
+/**
+ * Map from a string (name) to an unsigned integer value
+ *
+ * \note
+ * Because of the way this class interacts with the \c hash_table
+ * implementation, values of \c UINT_MAX cannot be stored in the map.
+ */
+struct string_to_uint_map {
+public:
+ string_to_uint_map()
+ {
+ this->ht = _mesa_hash_table_create(NULL, _mesa_key_hash_string,
+ _mesa_key_string_equal);
+ }
+
+ ~string_to_uint_map()
+ {
+ hash_table_call_foreach(this->ht, delete_key, NULL);
+ _mesa_hash_table_destroy(this->ht, NULL);
+ }
+
+ /**
+ * Remove all mappings from this map.
+ */
+ void clear()
+ {
+ hash_table_call_foreach(this->ht, delete_key, NULL);
+ _mesa_hash_table_clear(this->ht, NULL);
+ }
+
+ /**
+ * Runs a passed callback for the hash
+ */
+ void iterate(void (*func)(const char *, unsigned, void *), void *closure)
+ {
+ struct string_map_iterate_wrapper_closure *wrapper;
+
+ wrapper = (struct string_map_iterate_wrapper_closure *)
+ malloc(sizeof(struct string_map_iterate_wrapper_closure));
+ if (wrapper == NULL)
+ return;
+
+ wrapper->callback = func;
+ wrapper->closure = closure;
+
+ hash_table_call_foreach(this->ht, subtract_one_wrapper, wrapper);
+ free(wrapper);
+ }
+
+ /**
+ * Get the value associated with a particular key
+ *
+ * \return
+ * If \c key is found in the map, \c true is returned. Otherwise \c false
+ * is returned.
+ *
+ * \note
+ * If \c key is not found in the table, \c value is not modified.
+ */
+ bool get(unsigned &value, const char *key)
+ {
+ hash_entry *entry = _mesa_hash_table_search(this->ht,
+ (const void *) key);
+
+ if (!entry)
+ return false;
+
+ const intptr_t v = (intptr_t) entry->data;
+ value = (unsigned)(v - 1);
+ return true;
+ }
+
+ void put(unsigned value, const char *key)
+ {
+ /* The low-level hash table structure returns NULL if key is not in the
+ * hash table. However, users of this map might want to store zero as a
+ * valid value in the table. Bias the value by +1 so that a
+ * user-specified zero is stored as 1. This enables ::get to tell the
+ * difference between a user-specified zero (returned as 1 by
+ * _mesa_hash_table_search) and the key not in the table (returned as 0 by
+ * _mesa_hash_table_search).
+ *
+ * The net effect is that we can't store UINT_MAX in the table. This is
+ * because UINT_MAX+1 = 0.
+ */
+ assert(value != UINT_MAX);
+ char *dup_key = strdup(key);
+
+ struct hash_entry *entry = _mesa_hash_table_search(this->ht, dup_key);
+ if (entry) {
+ entry->data = (void *) (intptr_t) (value + 1);
+ } else {
+ _mesa_hash_table_insert(this->ht, dup_key,
+ (void *) (intptr_t) (value + 1));
+ }
+
+ if (entry)
+ free(dup_key);
+ }
+
+private:
+ static void delete_key(const void *key, void *data, void *closure)
+ {
+ (void) data;
+ (void) closure;
+
+ free((char *)key);
+ }
+
+ static void subtract_one_wrapper(const void *key, void *data, void *closure)
+ {
+ struct string_map_iterate_wrapper_closure *wrapper =
+ (struct string_map_iterate_wrapper_closure *) closure;
+ unsigned value = (intptr_t) data;
+
+ value -= 1;
+
+ wrapper->callback((const char *) key, value, wrapper->closure);
+ }
+
+ struct hash_table *ht;
+};
+#endif /* __cplusplus */
#endif /* HASH_TABLE_H */