util/set: Add a helper to resize a set
authorJason Ekstrand <jason@jlekstrand.net>
Fri, 10 May 2019 18:50:56 +0000 (13:50 -0500)
committerJason Ekstrand <jason@jlekstrand.net>
Mon, 13 May 2019 14:43:47 +0000 (14:43 +0000)
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>
src/util/set.c
src/util/set.h

index 2fd54a71a6f04a7fcb9d7c472d6421f957a8014d..c2b97bdea08b1915c4cfa36e7be31510651c693d 100644 (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.
  *
index 7d277c59f8b94086f6edde73bbc9d0b2a22acf30..5742c311a77a4930e8e4e74e061264accfa33693 100644 (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));