[Mesa-dev] [PATCH 06/15] mesa: Add hash_table_replace

Ian Romanick idr at freedesktop.org
Thu Sep 29 10:51:56 PDT 2011


From: Ian Romanick <ian.d.romanick at intel.com>

Signed-off-by: Ian Romanick <ian.d.romanick at intel.com>
---
 src/mesa/program/hash_table.c |   25 +++++++++++++++++++++++++
 src/mesa/program/hash_table.h |   15 +++++++++++++++
 2 files changed, 40 insertions(+), 0 deletions(-)

diff --git a/src/mesa/program/hash_table.c b/src/mesa/program/hash_table.c
index 2b09462..dc8563a 100644
--- a/src/mesa/program/hash_table.c
+++ b/src/mesa/program/hash_table.c
@@ -150,6 +150,31 @@ hash_table_insert(struct hash_table *ht, void *data, const void *key)
 }
 
 void
+hash_table_replace(struct hash_table *ht, void *data, const void *key)
+{
+    const unsigned hash_value = (*ht->hash)(key);
+    const unsigned bucket = hash_value % ht->num_buckets;
+    struct node *node;
+    struct hash_node *hn;
+
+    foreach(node, & ht->buckets[bucket]) {
+       hn = (struct hash_node *) node;
+
+       if ((*ht->compare)(hn->key, key) == 0) {
+	  hn->data = data;
+	  return;
+       }
+    }
+
+    hn = calloc(1, sizeof(*hn));
+
+    hn->data = data;
+    hn->key = key;
+
+    insert_at_head(& ht->buckets[bucket], & hn->link);
+}
+
+void
 hash_table_remove(struct hash_table *ht, const void *key)
 {
    struct node *node = (struct node *) get_node(ht, key);
diff --git a/src/mesa/program/hash_table.h b/src/mesa/program/hash_table.h
index 746939c..e7ab067 100644
--- a/src/mesa/program/hash_table.h
+++ b/src/mesa/program/hash_table.h
@@ -93,11 +93,26 @@ extern void *hash_table_find(struct hash_table *ht, const void *key);
  * If \c key is already in the hash table, it will be added again.  Future
  * calls to \c hash_table_find and \c hash_table_remove will return or remove,
  * repsectively, the most recently added instance of \c key.
+ *
+ * \sa hash_table_replace
  */
 extern void hash_table_insert(struct hash_table *ht, void *data,
     const void *key);
 
 /**
+ * Add an element to a hash table with replacement
+ *
+ * \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
+ * that key.
+ *
+ * \sa hash_table_insert
+ */
+extern void hash_table_replace(struct hash_table *ht, void *data,
+    const void *key);
+
+/**
  * Remove a specific element from a hash table.
  */
 extern void hash_table_remove(struct hash_table *ht, const void *key);
-- 
1.7.6



More information about the mesa-dev mailing list