util/set: Add a helper to resize a set

Often times you don't know how big a set will be and you want the code
to just grow it as needed.  However, sometimes you do know and you can
avoid a lot of rehashing if you just specify a size up-front.

Reviewed-by: Eric Anholt <eric@anholt.net>
Reviewed-by: Thomas Helland <thomashelland90@gmail.com>
This commit is contained in:
Jason Ekstrand
2019-05-10 13:50:56 -05:00
parent abb450870e
commit bab08c791d
2 changed files with 16 additions and 0 deletions
+14
View File
@@ -280,6 +280,20 @@ set_rehash(struct set *ht, unsigned new_size_index)
ralloc_free(old_ht.table);
}
void
_mesa_set_resize(struct set *set, uint32_t entries)
{
/* You can't shrink a set below its number of entries */
if (set->entries > entries)
entries = set->entries;
unsigned size_index = 0;
while (hash_sizes[size_index].max_entries < entries)
size_index++;
set_rehash(set, size_index);
}
/**
* Inserts the key with the given hash into the table.
*
+2
View File
@@ -65,6 +65,8 @@ void
_mesa_set_destroy(struct set *set,
void (*delete_function)(struct set_entry *entry));
void
_mesa_set_resize(struct set *set, uint32_t entries);
void
_mesa_set_clear(struct set *set,
void (*delete_function)(struct set_entry *entry));