<div dir="auto"></div><div class="gmail_quote">---------- Forwarded message ----------<br>From: "Thomas Helland" <<a href="mailto:thomashelland90@gmail.com">thomashelland90@gmail.com</a>><br>Date: Jan 2, 2017 1:16 PM<br>Subject: Re: [Mesa-dev] [PATCH 06/14] util: Make a copy of the existing hash table implementation<br>To: "Vladislav Egorov" <<a href="mailto:vegorov180@gmail.com">vegorov180@gmail.com</a>><br>Cc: <br><br type="attribution"><div dir="auto">Thanks!<div dir="auto"><br></div><div dir="auto">I was thinking that this data structure would have a name.</div><div dir="auto">I'll keep that in mind if I take the route of this series.</div><div dir="auto">If I follow Connor's suggestion a multimap would not be needed,</div><div dir="auto">so then I'll probably just stowe away the implementation and forget it.</div><div dir="auto"><div dir="auto"><br><div class="gmail_extra"><br><div class="gmail_quote">On Jan 2, 2017 11:49 AM, "Vladislav Egorov" <<a href="mailto:vegorov180@gmail.com" target="_blank">vegorov180@gmail.com</a>> wrote:<br type="attribution"><blockquote class="m_3244782390585305009quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">I think this data structure is known as multimap (in STL it's named std::unordered_multimap, in Scala it's MultiMap, in Apache Commons it's MultiValueMap and so on).<br>
<br>
<a href="https://en.wikipedia.org/wiki/Multimap" rel="noreferrer" target="_blank">https://en.wikipedia.org/wiki/<wbr>Multimap</a><br>
<br>
01.01.2017 21:37, Thomas Helland пишет:<div class="m_3244782390585305009elided-text"><br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
---<br>
src/util/Makefile.sources | 2 +<br>
src/util/non_replacing_hash_ta<wbr>ble.c | 504 ++++++++++++++++++++++++++++++<wbr>++++++<br>
src/util/non_replacing_hash_ta<wbr>ble.h | 159 ++++++++++++<br>
3 files changed, 665 insertions(+)<br>
create mode 100644 src/util/non_replacing_hash_ta<wbr>ble.c<br>
create mode 100644 src/util/non_replacing_hash_ta<wbr>ble.h<br>
<br>
diff --git a/src/util/Makefile.sources b/src/util/Makefile.sources<br>
index b24611a363..6d7f0884fb 100644<br>
--- a/src/util/Makefile.sources<br>
+++ b/src/util/Makefile.sources<br>
@@ -20,6 +20,8 @@ MESA_UTIL_FILES := \<br>
macros.h \<br>
mesa-sha1.c \<br>
mesa-sha1.h \<br>
+ non_replacing_hash_table.c \<br>
+ non_replacing_hash_table.h \<br>
ralloc.c \<br>
ralloc.h \<br>
register_allocate.c \<br>
diff --git a/src/util/non_replacing_hash_<wbr>table.c b/src/util/non_replacing_hash_<wbr>table.c<br>
new file mode 100644<br>
index 0000000000..1730b1ee0d<br>
--- /dev/null<br>
+++ b/src/util/non_replacing_hash_<wbr>table.c<br>
@@ -0,0 +1,504 @@<br>
+/*<br>
+ * Copyright © 2009,2012 Intel Corporation<br>
+ * Copyright © 1988-2004 Keith Packard and Bart Massey.<br>
+ *<br>
+ * Permission is hereby granted, free of charge, to any person obtaining a<br>
+ * copy of this software and associated documentation files (the "Software"),<br>
+ * to deal in the Software without restriction, including without limitation<br>
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,<br>
+ * and/or sell copies of the Software, and to permit persons to whom the<br>
+ * Software is furnished to do so, subject to the following conditions:<br>
+ *<br>
+ * The above copyright notice and this permission notice (including the next<br>
+ * paragraph) shall be included in all copies or substantial portions of the<br>
+ * Software.<br>
+ *<br>
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR<br>
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,<br>
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL<br>
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER<br>
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING<br>
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS<br>
+ * IN THE SOFTWARE.<br>
+ *<br>
+ * Except as contained in this notice, the names of the authors<br>
+ * or their institutions shall not be used in advertising or<br>
+ * otherwise to promote the sale, use or other dealings in this<br>
+ * Software without prior written authorization from the<br>
+ * authors.<br>
+ *<br>
+ * Authors:<br>
+ * Eric Anholt <<a href="mailto:eric@anholt.net" target="_blank">eric@anholt.net</a>><br>
+ * Keith Packard <<a href="mailto:keithp@keithp.com" target="_blank">keithp@keithp.com</a>><br>
+ */<br>
+<br>
+/**<br>
+ * Implements an open-addressing, linear-reprobing hash table.<br>
+ *<br>
+ * For more information, see:<br>
+ *<br>
+ * <a href="http://cgit.freedesktop.org/~anholt/hash_table/tree/README" rel="noreferrer" target="_blank">http://cgit.freedesktop.org/~a<wbr>nholt/hash_table/tree/README</a><br>
+ */<br>
+<br>
+#include <stdlib.h><br>
+#include <string.h><br>
+#include <assert.h><br>
+<br>
+#include "non_replacing_hash_table.h"<br>
+#include "ralloc.h"<br>
+#include "macros.h"<br>
+<br>
+static const uint32_t deleted_key_value;<br>
+<br>
+/**<br>
+ * From Knuth -- a good choice for hash/rehash values is p, p-2 where<br>
+ * p and p-2 are both prime. These tables are sized to have an extra 10%<br>
+ * free to avoid exponential performance degradation as the hash table fills<br>
+ */<br>
+static const struct {<br>
+ uint32_t max_entries, size, rehash;<br>
+} hash_sizes[] = {<br>
+ { 2, 5, 3 },<br>
+ { 4, 7, 5 },<br>
+ { 8, 13, 11 },<br>
+ { 16, 19, 17 },<br>
+ { 32, 43, 41 },<br>
+ { 64, 73, 71 },<br>
+ { 128, 151, 149 },<br>
+ { 256, 283, 281 },<br>
+ { 512, 571, 569 },<br>
+ { 1024, 1153, 1151 },<br>
+ { 2048, 2269, 2267 },<br>
+ { 4096, 4519, 4517 },<br>
+ { 8192, 9013, 9011 },<br>
+ { 16384, 18043, 18041 },<br>
+ { 32768, 36109, 36107 },<br>
+ { 65536, 72091, 72089 },<br>
+ { 131072, 144409, 144407 },<br>
+ { 262144, 288361, 288359 },<br>
+ { 524288, 576883, 576881 },<br>
+ { 1048576, 1153459, 1153457 },<br>
+ { 2097152, 2307163, 2307161 },<br>
+ { 4194304, 4613893, 4613891 },<br>
+ { 8388608, 9227641, 9227639 },<br>
+ { 16777216, 18455029, 18455027 },<br>
+ { 33554432, 36911011, 36911009 },<br>
+ { 67108864, 73819861, 73819859 },<br>
+ { 134217728, 147639589, 147639587 },<br>
+ { 268435456, 295279081, 295279079 },<br>
+ { 536870912, 590559793, 590559791 },<br>
+ { 1073741824, 1181116273, 1181116271},<br>
+ { 2147483648ul, 2362232233ul, 2362232231ul}<br>
+};<br>
+<br>
+static int<br>
+entry_is_free(const struct hash_entry *entry)<br>
+{<br>
+ return entry->key == NULL;<br>
+}<br>
+<br>
+static int<br>
+entry_is_deleted(const struct hash_table *ht, struct hash_entry *entry)<br>
+{<br>
+ return entry->key == ht->deleted_key;<br>
+}<br>
+<br>
+static int<br>
+entry_is_present(const struct hash_table *ht, struct hash_entry *entry)<br>
+{<br>
+ return entry->key != NULL && entry->key != ht->deleted_key;<br>
+}<br>
+<br>
+struct hash_table *<br>
+_mesa_hash_table_create(void *mem_ctx,<br>
+ uint32_t (*key_hash_function)(const void *key),<br>
+ bool (*key_equals_function)(const void *a,<br>
+ const void *b))<br>
+{<br>
+ struct hash_table *ht;<br>
+<br>
+ ht = ralloc(mem_ctx, struct hash_table);<br>
+ if (ht == NULL)<br>
+ return NULL;<br>
+<br>
+ ht->size_index = 0;<br>
+ ht->size = hash_sizes[ht->size_index].siz<wbr>e;<br>
+ ht->rehash = hash_sizes[ht->size_index].reh<wbr>ash;<br>
+ ht->max_entries = hash_sizes[ht->size_index].max<wbr>_entries;<br>
+ ht->key_hash_function = key_hash_function;<br>
+ ht->key_equals_function = key_equals_function;<br>
+ ht->table = rzalloc_array(ht, struct hash_entry, ht->size);<br>
+ ht->entries = 0;<br>
+ ht->deleted_entries = 0;<br>
+ ht->deleted_key = &deleted_key_value;<br>
+<br>
+ if (ht->table == NULL) {<br>
+ ralloc_free(ht);<br>
+ return NULL;<br>
+ }<br>
+<br>
+ return ht;<br>
+}<br>
+<br>
+/**<br>
+ * Frees the given hash table.<br>
+ *<br>
+ * If delete_function is passed, it gets called on each entry present before<br>
+ * freeing.<br>
+ */<br>
+void<br>
+_mesa_hash_table_destroy(stru<wbr>ct hash_table *ht,<br>
+ void (*delete_function)(struct hash_entry *entry))<br>
+{<br>
+ if (!ht)<br>
+ return;<br>
+<br>
+ if (delete_function) {<br>
+ struct hash_entry *entry;<br>
+<br>
+ hash_table_foreach(ht, entry) {<br>
+ delete_function(entry);<br>
+ }<br>
+ }<br>
+ ralloc_free(ht);<br>
+}<br>
+<br>
+/**<br>
+ * Deletes all entries of the given hash table without deleting the table<br>
+ * itself or changing its structure.<br>
+ *<br>
+ * If delete_function is passed, it gets called on each entry present.<br>
+ */<br>
+void<br>
+_mesa_hash_table_clear(struct hash_table *ht,<br>
+ void (*delete_function)(struct hash_entry *entry))<br>
+{<br>
+ struct hash_entry *entry;<br>
+<br>
+ for (entry = ht->table; entry != ht->table + ht->size; entry++) {<br>
+ if (entry->key == NULL)<br>
+ continue;<br>
+<br>
+ if (delete_function != NULL && entry->key != ht->deleted_key)<br>
+ delete_function(entry);<br>
+<br>
+ entry->key = NULL;<br>
+ }<br>
+<br>
+ ht->entries = 0;<br>
+ ht->deleted_entries = 0;<br>
+}<br>
+<br>
+/** Sets the value of the key pointer used for deleted entries in the table.<br>
+ *<br>
+ * The assumption is that usually keys are actual pointers, so we use a<br>
+ * default value of a pointer to an arbitrary piece of storage in the library.<br>
+ * But in some cases a consumer wants to store some other sort of value in the<br>
+ * table, like a uint32_t, in which case that pointer may conflict with one of<br>
+ * their valid keys. This lets that user select a safe value.<br>
+ *<br>
+ * This must be called before any keys are actually deleted from the table.<br>
+ */<br>
+void<br>
+_mesa_hash_table_set_deleted_<wbr>key(struct hash_table *ht, const void *deleted_key)<br>
+{<br>
+ ht->deleted_key = deleted_key;<br>
+}<br>
+<br>
+static struct hash_entry *<br>
+hash_table_search(struct hash_table *ht, uint32_t hash, const void *key)<br>
+{<br>
+ uint32_t start_hash_address = hash % ht->size;<br>
+ uint32_t hash_address = start_hash_address;<br>
+<br>
+ do {<br>
+ uint32_t double_hash;<br>
+<br>
+ struct hash_entry *entry = ht->table + hash_address;<br>
+<br>
+ if (entry_is_free(entry)) {<br>
+ return NULL;<br>
+ } else if (entry_is_present(ht, entry) && entry->hash == hash) {<br>
+ if (ht->key_equals_function(key, entry->key)) {<br>
+ return entry;<br>
+ }<br>
+ }<br>
+<br>
+ double_hash = 1 + hash % ht->rehash;<br>
+<br>
+ hash_address = (hash_address + double_hash) % ht->size;<br>
+ } while (hash_address != start_hash_address);<br>
+<br>
+ return NULL;<br>
+}<br>
+<br>
+/**<br>
+ * Finds a hash table entry with the given key and hash of that key.<br>
+ *<br>
+ * Returns NULL if no entry is found. Note that the data pointer may be<br>
+ * modified by the user.<br>
+ */<br>
+struct hash_entry *<br>
+_mesa_hash_table_search(struc<wbr>t hash_table *ht, const void *key)<br>
+{<br>
+ assert(ht->key_hash_function)<wbr>;<br>
+ return hash_table_search(ht, ht->key_hash_function(key), key);<br>
+}<br>
+<br>
+struct hash_entry *<br>
+_mesa_hash_table_search_pre_h<wbr>ashed(struct hash_table *ht, uint32_t hash,<br>
+ const void *key)<br>
+{<br>
+ assert(ht->key_hash_function == NULL || hash == ht->key_hash_function(key));<br>
+ return hash_table_search(ht, hash, key);<br>
+}<br>
+<br>
+static struct hash_entry *<br>
+hash_table_insert(struct hash_table *ht, uint32_t hash,<br>
+ const void *key, void *data);<br>
+<br>
+static void<br>
+_mesa_hash_table_rehash(struc<wbr>t hash_table *ht, unsigned new_size_index)<br>
+{<br>
+ struct hash_table old_ht;<br>
+ struct hash_entry *table, *entry;<br>
+<br>
+ if (new_size_index >= ARRAY_SIZE(hash_sizes))<br>
+ return;<br>
+<br>
+ table = rzalloc_array(ht, struct hash_entry,<br>
+ hash_sizes[new_size_index].si<wbr>ze);<br>
+ if (table == NULL)<br>
+ return;<br>
+<br>
+ old_ht = *ht;<br>
+<br>
+ ht->table = table;<br>
+ ht->size_index = new_size_index;<br>
+ ht->size = hash_sizes[ht->size_index].siz<wbr>e;<br>
+ ht->rehash = hash_sizes[ht->size_index].reh<wbr>ash;<br>
+ ht->max_entries = hash_sizes[ht->size_index].max<wbr>_entries;<br>
+ ht->entries = 0;<br>
+ ht->deleted_entries = 0;<br>
+<br>
+ hash_table_foreach(&old_ht, entry) {<br>
+ hash_table_insert(ht, entry->hash, entry->key, entry->data);<br>
+ }<br>
+<br>
+ ralloc_free(old_ht.table);<br>
+}<br>
+<br>
+static struct hash_entry *<br>
+hash_table_insert(struct hash_table *ht, uint32_t hash,<br>
+ const void *key, void *data)<br>
+{<br>
+ uint32_t start_hash_address, hash_address;<br>
+ struct hash_entry *available_entry = NULL;<br>
+<br>
+ assert(key != NULL);<br>
+<br>
+ if (ht->entries >= ht->max_entries) {<br>
+ _mesa_hash_table_rehash(ht, ht->size_index + 1);<br>
+ } else if (ht->deleted_entries + ht->entries >= ht->max_entries) {<br>
+ _mesa_hash_table_rehash(ht, ht->size_index);<br>
+ }<br>
+<br>
+ start_hash_address = hash % ht->size;<br>
+ hash_address = start_hash_address;<br>
+ do {<br>
+ struct hash_entry *entry = ht->table + hash_address;<br>
+ uint32_t double_hash;<br>
+<br>
+ if (!entry_is_present(ht, entry)) {<br>
+ /* Stash the first available entry we find */<br>
+ if (available_entry == NULL)<br>
+ available_entry = entry;<br>
+ if (entry_is_free(entry))<br>
+ break;<br>
+ }<br>
+<br>
+ /* Implement replacement when another insert happens<br>
+ * with a matching key. This is a relatively common<br>
+ * feature of hash tables, with the alternative<br>
+ * generally being "insert the new value as well, and<br>
+ * return it first when the key is searched for".<br>
+ *<br>
+ * Note that the hash table doesn't have a delete<br>
+ * callback. If freeing of old data pointers is<br>
+ * required to avoid memory leaks, perform a search<br>
+ * before inserting.<br>
+ */<br>
+ if (!entry_is_deleted(ht, entry) &&<br>
+ entry->hash == hash &&<br>
+ ht->key_equals_function(key, entry->key)) {<br>
+ entry->key = key;<br>
+ entry->data = data;<br>
+ return entry;<br>
+ }<br>
+<br>
+<br>
+ double_hash = 1 + hash % ht->rehash;<br>
+<br>
+ hash_address = (hash_address + double_hash) % ht->size;<br>
+ } while (hash_address != start_hash_address);<br>
+<br>
+ if (available_entry) {<br>
+ if (entry_is_deleted(ht, available_entry))<br>
+ ht->deleted_entries--;<br>
+ available_entry->hash = hash;<br>
+ available_entry->key = key;<br>
+ available_entry->data = data;<br>
+ ht->entries++;<br>
+ return available_entry;<br>
+ }<br>
+<br>
+ /* We could hit here if a required resize failed. An unchecked-malloc<br>
+ * application could ignore this result.<br>
+ */<br>
+ return NULL;<br>
+}<br>
+<br>
+/**<br>
+ * Inserts the key with the given hash into the table.<br>
+ *<br>
+ * Note that insertion may rearrange the table on a resize or rehash,<br>
+ * so previously found hash_entries are no longer valid after this function.<br>
+ */<br>
+struct hash_entry *<br>
+_mesa_hash_table_insert(struc<wbr>t hash_table *ht, const void *key, void *data)<br>
+{<br>
+ assert(ht->key_hash_function)<wbr>;<br>
+ return hash_table_insert(ht, ht->key_hash_function(key), key, data);<br>
+}<br>
+<br>
+struct hash_entry *<br>
+_mesa_hash_table_insert_pre_h<wbr>ashed(struct hash_table *ht, uint32_t hash,<br>
+ const void *key, void *data)<br>
+{<br>
+ assert(ht->key_hash_function == NULL || hash == ht->key_hash_function(key));<br>
+ return hash_table_insert(ht, hash, key, data);<br>
+}<br>
+<br>
+/**<br>
+ * This function deletes the given hash table entry.<br>
+ *<br>
+ * Note that deletion doesn't otherwise modify the table, so an iteration over<br>
+ * the table deleting entries is safe.<br>
+ */<br>
+void<br>
+_mesa_hash_table_remove(struc<wbr>t hash_table *ht,<br>
+ struct hash_entry *entry)<br>
+{<br>
+ if (!entry)<br>
+ return;<br>
+<br>
+ entry->key = ht->deleted_key;<br>
+ ht->entries--;<br>
+ ht->deleted_entries++;<br>
+}<br>
+<br>
+/**<br>
+ * This function is an iterator over the hash table.<br>
+ *<br>
+ * Pass in NULL for the first entry, as in the start of a for loop. Note that<br>
+ * an iteration over the table is O(table_size) not O(entries).<br>
+ */<br>
+struct hash_entry *<br>
+_mesa_hash_table_next_entry(s<wbr>truct hash_table *ht,<br>
+ struct hash_entry *entry)<br>
+{<br>
+ if (entry == NULL)<br>
+ entry = ht->table;<br>
+ else<br>
+ entry = entry + 1;<br>
+<br>
+ for (; entry != ht->table + ht->size; entry++) {<br>
+ if (entry_is_present(ht, entry)) {<br>
+ return entry;<br>
+ }<br>
+ }<br>
+<br>
+ return NULL;<br>
+}<br>
+<br>
+/**<br>
+ * Returns a random entry from the hash table.<br>
+ *<br>
+ * This may be useful in implementing random replacement (as opposed<br>
+ * to just removing everything) in caches based on this hash table<br>
+ * implementation. @predicate may be used to filter entries, or may<br>
+ * be set to NULL for no filtering.<br>
+ */<br>
+struct hash_entry *<br>
+_mesa_hash_table_random_entry<wbr>(struct hash_table *ht,<br>
+ bool (*predicate)(struct hash_entry *entry))<br>
+{<br>
+ struct hash_entry *entry;<br>
+ uint32_t i = rand() % ht->size;<br>
+<br>
+ if (ht->entries == 0)<br>
+ return NULL;<br>
+<br>
+ for (entry = ht->table + i; entry != ht->table + ht->size; entry++) {<br>
+ if (entry_is_present(ht, entry) &&<br>
+ (!predicate || predicate(entry))) {<br>
+ return entry;<br>
+ }<br>
+ }<br>
+<br>
+ for (entry = ht->table; entry != ht->table + i; entry++) {<br>
+ if (entry_is_present(ht, entry) &&<br>
+ (!predicate || predicate(entry))) {<br>
+ return entry;<br>
+ }<br>
+ }<br>
+<br>
+ return NULL;<br>
+}<br>
+<br>
+<br>
+/**<br>
+ * Quick FNV-1a hash implementation based on:<br>
+ * <a href="http://www.isthe.com/chongo/tech/comp/fnv/" rel="noreferrer" target="_blank">http://www.isthe.com/chongo/te<wbr>ch/comp/fnv/</a><br>
+ *<br>
+ * FNV-1a is not be the best hash out there -- Jenkins's lookup3 is supposed<br>
+ * to be quite good, and it probably beats FNV. But FNV has the advantage<br>
+ * that it involves almost no code. For an improvement on both, see Paul<br>
+ * Hsieh's <a href="http://www.azillionmonkeys.com/qed/hash.html" rel="noreferrer" target="_blank">http://www.azillionmonkeys.com<wbr>/qed/hash.html</a><br>
+ */<br>
+uint32_t<br>
+_mesa_hash_data(const void *data, size_t size)<br>
+{<br>
+ return _mesa_fnv32_1a_accumulate_bloc<wbr>k(_mesa_fnv32_1a_offset_bias,<br>
+ data, size);<br>
+}<br>
+<br>
+/** FNV-1a string hash implementation */<br>
+uint32_t<br>
+_mesa_hash_string(const char *key)<br>
+{<br>
+ uint32_t hash = _mesa_fnv32_1a_offset_bias;<br>
+<br>
+ while (*key != 0) {<br>
+ hash = _mesa_fnv32_1a_accumulate(hash<wbr>, *key);<br>
+ key++;<br>
+ }<br>
+<br>
+ return hash;<br>
+}<br>
+<br>
+/**<br>
+ * String compare function for use as the comparison callback in<br>
+ * _mesa_hash_table_create().<br>
+ */<br>
+bool<br>
+_mesa_key_string_equal(const void *a, const void *b)<br>
+{<br>
+ return strcmp(a, b) == 0;<br>
+}<br>
+<br>
+bool<br>
+_mesa_key_pointer_equal(const void *a, const void *b)<br>
+{<br>
+ return a == b;<br>
+}<br>
diff --git a/src/util/non_replacing_hash_<wbr>table.h b/src/util/non_replacing_hash_<wbr>table.h<br>
new file mode 100644<br>
index 0000000000..b35ee871bb<br>
--- /dev/null<br>
+++ b/src/util/non_replacing_hash_<wbr>table.h<br>
@@ -0,0 +1,159 @@<br>
+/*<br>
+ * Copyright © 2009,2012 Intel Corporation<br>
+ *<br>
+ * Permission is hereby granted, free of charge, to any person obtaining a<br>
+ * copy of this software and associated documentation files (the "Software"),<br>
+ * to deal in the Software without restriction, including without limitation<br>
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,<br>
+ * and/or sell copies of the Software, and to permit persons to whom the<br>
+ * Software is furnished to do so, subject to the following conditions:<br>
+ *<br>
+ * The above copyright notice and this permission notice (including the next<br>
+ * paragraph) shall be included in all copies or substantial portions of the<br>
+ * Software.<br>
+ *<br>
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR<br>
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,<br>
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL<br>
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER<br>
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING<br>
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS<br>
+ * IN THE SOFTWARE.<br>
+ *<br>
+ * Authors:<br>
+ * Eric Anholt <<a href="mailto:eric@anholt.net" target="_blank">eric@anholt.net</a>><br>
+ *<br>
+ */<br>
+<br>
+#ifndef _HASH_TABLE_H<br>
+#define _HASH_TABLE_H<br>
+<br>
+#include <stdlib.h><br>
+#include <inttypes.h><br>
+#include <stdbool.h><br>
+#include "c99_compat.h"<br>
+#include "macros.h"<br>
+<br>
+#ifdef __cplusplus<br>
+extern "C" {<br>
+#endif<br>
+<br>
+struct hash_entry {<br>
+ uint32_t hash;<br>
+ const void *key;<br>
+ void *data;<br>
+};<br>
+<br>
+struct hash_table {<br>
+ struct hash_entry *table;<br>
+ uint32_t (*key_hash_function)(const void *key);<br>
+ bool (*key_equals_function)(const void *a, const void *b);<br>
+ const void *deleted_key;<br>
+ uint32_t size;<br>
+ uint32_t rehash;<br>
+ uint32_t max_entries;<br>
+ uint32_t size_index;<br>
+ uint32_t entries;<br>
+ uint32_t deleted_entries;<br>
+};<br>
+<br>
+struct hash_table *<br>
+_mesa_hash_table_create(void *mem_ctx,<br>
+ uint32_t (*key_hash_function)(const void *key),<br>
+ bool (*key_equals_function)(const void *a,<br>
+ const void *b));<br>
+void _mesa_hash_table_destroy(struc<wbr>t hash_table *ht,<br>
+ void (*delete_function)(struct hash_entry *entry));<br>
+void _mesa_hash_table_clear(struct hash_table *ht,<br>
+ void (*delete_function)(struct hash_entry *entry));<br>
+void _mesa_hash_table_set_deleted_k<wbr>ey(struct hash_table *ht,<br>
+ const void *deleted_key);<br>
+<br>
+static inline uint32_t _mesa_hash_table_num_entries(s<wbr>truct hash_table *ht)<br>
+{<br>
+ return ht->entries;<br>
+}<br>
+<br>
+struct hash_entry *<br>
+_mesa_hash_table_insert(struc<wbr>t hash_table *ht, const void *key, void *data);<br>
+struct hash_entry *<br>
+_mesa_hash_table_insert_pre_h<wbr>ashed(struct hash_table *ht, uint32_t hash,<br>
+ const void *key, void *data);<br>
+struct hash_entry *<br>
+_mesa_hash_table_search(struc<wbr>t hash_table *ht, const void *key);<br>
+struct hash_entry *<br>
+_mesa_hash_table_search_pre_h<wbr>ashed(struct hash_table *ht, uint32_t hash,<br>
+ const void *key);<br>
+void _mesa_hash_table_remove(struct hash_table *ht,<br>
+ struct hash_entry *entry);<br>
+<br>
+struct hash_entry *_mesa_hash_table_next_entry(s<wbr>truct hash_table *ht,<br>
+ struct hash_entry *entry);<br>
+struct hash_entry *<br>
+_mesa_hash_table_random_entry<wbr>(struct hash_table *ht,<br>
+ bool (*predicate)(struct hash_entry *entry));<br>
+<br>
+uint32_t _mesa_hash_data(const void *data, size_t size);<br>
+uint32_t _mesa_hash_string(const char *key);<br>
+bool _mesa_key_string_equal(const void *a, const void *b);<br>
+bool _mesa_key_pointer_equal(const void *a, const void *b);<br>
+<br>
+static inline uint32_t _mesa_key_hash_string(const void *key)<br>
+{<br>
+ return _mesa_hash_string((const char *)key);<br>
+}<br>
+<br>
+static inline uint32_t _mesa_hash_pointer(const void *pointer)<br>
+{<br>
+ return _mesa_hash_data(&pointer, sizeof(pointer));<br>
+}<br>
+<br>
+enum {<br>
+ _mesa_fnv32_1a_offset_bias = 2166136261u,<br>
+};<br>
+<br>
+static inline uint32_t<br>
+_mesa_fnv32_1a_accumulate_blo<wbr>ck(uint32_t hash, const void *data, size_t size)<br>
+{<br>
+ const uint8_t *bytes = (const uint8_t *)data;<br>
+<br>
+ while (size-- != 0) {<br>
+ hash ^= *bytes;<br>
+ hash = hash * 0x01000193;<br>
+ bytes++;<br>
+ }<br>
+<br>
+ return hash;<br>
+}<br>
+<br>
+#define _mesa_fnv32_1a_accumulate(hash<wbr>, expr) \<br>
+ _mesa_fnv32_1a_accumulate_blo<wbr>ck(hash, &(expr), sizeof(expr))<br>
+<br>
+/**<br>
+ * This foreach function is safe against deletion (which just replaces<br>
+ * an entry's data with the deleted marker), but not against insertion<br>
+ * (which may rehash the table, making entry a dangling pointer).<br>
+ */<br>
+#define hash_table_foreach(ht, entry) \<br>
+ for (entry = _mesa_hash_table_next_entry(ht<wbr>, NULL); \<br>
+ entry != NULL; \<br>
+ entry = _mesa_hash_table_next_entry(ht<wbr>, entry))<br>
+<br>
+static inline void<br>
+hash_table_call_foreach(struc<wbr>t hash_table *ht,<br>
+ void (*callback)(const void *key,<br>
+ void *data,<br>
+ void *closure),<br>
+ void *closure)<br>
+{<br>
+ struct hash_entry *entry;<br>
+<br>
+ hash_table_foreach(ht, entry)<br>
+ callback(entry->key, entry->data, closure);<br>
+}<br>
+<br>
+#ifdef __cplusplus<br>
+} /* extern C */<br>
+#endif<br>
+<br>
+#endif /* _HASH_TABLE_H */<br>
</blockquote>
<br>
</div></blockquote></div><br></div></div></div></div>
</div>