mesa: Add a new function for getting the nonconst sampler array index
[mesa.git] / src / mesa / program / hash_table.h
index 941d28a4ce9dd443d352068b0ef392da53d69efa..e95fc4982ec861b404b02218c96df78f1277b42b 100644 (file)
 #define HASH_TABLE_H
 
 #include <string.h>
+#include <stdbool.h>
 #include <stdlib.h>
 #include <stdint.h>
 #include <limits.h>
 #include <assert.h>
 
-struct hash_table;
 struct string_to_uint_map;
 
-typedef unsigned (*hash_func_t)(const void *key);
-typedef int (*hash_compare_func_t)(const void *key1, const void *key2);
-
 #ifdef __cplusplus
 extern "C" {
 #endif
 
+struct hash_table;
+
+typedef unsigned (*hash_func_t)(const void *key);
+typedef int (*hash_compare_func_t)(const void *key1, const void *key2);
+
 /**
  * Hash table constructor
  *
@@ -113,6 +115,10 @@ extern void hash_table_insert(struct hash_table *ht, void *data,
 /**
  * Add an element to a hash table with replacement
  *
+ * \return
+ * 1 if it did replace the the value (in which case the old key is kept), 0 if
+ * it did not replace the value (in which case the new key is kept).
+ *
  * \warning
  * If \c key is already in the hash table, \c data will \b replace the most
  * recently inserted \c data (see the warning in \c hash_table_insert) for
@@ -120,7 +126,7 @@ extern void hash_table_insert(struct hash_table *ht, void *data,
  *
  * \sa hash_table_insert
  */
-extern void hash_table_replace(struct hash_table *ht, void *data,
+extern bool hash_table_replace(struct hash_table *ht, void *data,
     const void *key);
 
 /**
@@ -213,6 +219,15 @@ public:
       hash_table_dtor(this->ht);
    }
 
+   /**
+    * Remove all mappings from this map.
+    */
+   void clear()
+   {
+      hash_table_call_foreach(this->ht, delete_key, NULL);
+      hash_table_clear(this->ht);
+   }
+
    /**
     * Get the value associated with a particular key
     *
@@ -249,9 +264,12 @@ public:
        * because UINT_MAX+1 = 0.
        */
       assert(value != UINT_MAX);
-      hash_table_replace(this->ht,
-                        (void *) (intptr_t) (value + 1),
-                        strdup(key));
+      char *dup_key = strdup(key);
+      bool result = hash_table_replace(this->ht,
+                                      (void *) (intptr_t) (value + 1),
+                                      dup_key);
+      if (result)
+        free(dup_key);
    }
 
 private: