#include <assert.h>
#include <string.h>
+#include "hash_table.h"
#include "macros.h"
#include "ralloc.h"
#include "set.h"
return;
if (delete_function) {
- struct set_entry *entry;
-
set_foreach (ht, entry) {
delete_function(entry);
}
void
_mesa_set_clear(struct set *set, void (*delete_function)(struct set_entry *entry))
{
- struct set_entry *entry;
-
if (!set)
return;
}
static struct set_entry *
-set_add(struct set *ht, uint32_t hash, const void *key);
+set_add(struct set *ht, uint32_t hash, const void *key, bool *replaced);
static void
set_rehash(struct set *ht, unsigned new_size_index)
{
struct set old_ht;
- struct set_entry *table, *entry;
+ struct set_entry *table;
if (new_size_index >= ARRAY_SIZE(hash_sizes))
return;
ht->deleted_entries = 0;
set_foreach(&old_ht, entry) {
- set_add(ht, entry->hash, entry->key);
+ set_add(ht, entry->hash, entry->key, NULL);
}
ralloc_free(old_ht.table);
* so previously found hash_entries are no longer valid after this function.
*/
static struct set_entry *
-set_add(struct set *ht, uint32_t hash, const void *key)
+set_add(struct set *ht, uint32_t hash, const void *key, bool *replaced)
{
uint32_t hash_address;
struct set_entry *available_entry = NULL;
entry->hash == hash &&
ht->key_equals_function(key, entry->key)) {
entry->key = key;
+ if (replaced)
+ *replaced = true;
return entry;
}
available_entry->hash = hash;
available_entry->key = key;
ht->entries++;
+ if (replaced)
+ *replaced = false;
return available_entry;
}
_mesa_set_add(struct set *set, const void *key)
{
assert(set->key_hash_function);
- return set_add(set, set->key_hash_function(key), key);
+ return set_add(set, set->key_hash_function(key), key, NULL);
}
struct set_entry *
{
assert(set->key_hash_function == NULL ||
hash == set->key_hash_function(key));
- return set_add(set, hash, key);
+ return set_add(set, hash, key, NULL);
+}
+
+struct set_entry *
+_mesa_set_search_and_add(struct set *set, const void *key, bool *replaced)
+{
+ assert(set->key_hash_function);
+ return set_add(set, set->key_hash_function(key), key, replaced);
+}
+
+struct set_entry *
+_mesa_set_search_and_add_pre_hashed(struct set *set, uint32_t hash,
+ const void *key, bool *replaced)
+{
+ assert(set->key_hash_function == NULL ||
+ hash == set->key_hash_function(key));
+ return set_add(set, hash, key, replaced);
}
/**
return NULL;
}
+
+/**
+ * Helper to create a set with pointer keys.
+ */
+struct set *
+_mesa_pointer_set_create(void *mem_ctx)
+{
+ return _mesa_set_create(mem_ctx, _mesa_hash_pointer,
+ _mesa_key_pointer_equal);
+}