diff options
author | Adam Jackson <ajax@redhat.com> | 2017-05-11 11:14:16 -0400 |
---|---|---|
committer | Adam Jackson <ajax@redhat.com> | 2017-06-16 15:47:57 -0400 |
commit | 5d49af3c0306fe2f9141c7970b316fc2adc572a9 (patch) | |
tree | 7822ab3487347a3b9a2fe3320b86f1d31b0164ac | |
parent | 0cdb8437663a9b447f6ff8fcc7eccc975375b9c3 (diff) |
hashtable: Add ht_walk
This is obviously linear, but at least there's a bailout. And we expect
the hash tables this will be called on will be small.
Signed-off-by: Adam Jackson <ajax@redhat.com>
-rw-r--r-- | Xext/hashtable.c | 14 | ||||
-rw-r--r-- | Xext/hashtable.h | 4 |
2 files changed, 18 insertions, 0 deletions
diff --git a/Xext/hashtable.c b/Xext/hashtable.c index 41b2e0013..e8477a11b 100644 --- a/Xext/hashtable.c +++ b/Xext/hashtable.c @@ -83,6 +83,20 @@ ht_destroy(HashTable ht) free(ht->buckets); } +void +ht_walk(HashTable ht, int (*callback)(void *key, void *data, void *c), void *c) +{ + int i; + BucketPtr it, tmp; + int numBuckets = 1 << ht->bucketBits; + for (i = 0; i < numBuckets; ++i) { + xorg_list_for_each_entry_safe(it, tmp, &ht->buckets[i], l) { + if (callback(it->key, it->data, c)) + return; + } + } +} + static Bool double_size(HashTable ht) { diff --git a/Xext/hashtable.h b/Xext/hashtable.h index a988af32c..db0b63935 100644 --- a/Xext/hashtable.h +++ b/Xext/hashtable.h @@ -134,4 +134,8 @@ extern _X_EXPORT int ht_resourceid_compare(void *cdata, const void *a, const void *b); +/** @brief Calls the callback function for every element of the hash table. */ +extern _X_EXPORT void +ht_walk(HashTable ht, int (*callback)(void *key, void *data, void *c), void *c); + #endif // HASHTABLE_H |