hash_table.h revision 848b8605
146185892Smrg/* 246185892Smrg * Copyright © 2009,2012 Intel Corporation 346185892Smrg * 446185892Smrg * Permission is hereby granted, free of charge, to any person obtaining a 546185892Smrg * copy of this software and associated documentation files (the "Software"), 646185892Smrg * to deal in the Software without restriction, including without limitation 746185892Smrg * the rights to use, copy, modify, merge, publish, distribute, sublicense, 846185892Smrg * and/or sell copies of the Software, and to permit persons to whom the 946185892Smrg * Software is furnished to do so, subject to the following conditions: 1046185892Smrg * 1146185892Smrg * The above copyright notice and this permission notice (including the next 1246185892Smrg * paragraph) shall be included in all copies or substantial portions of the 1346185892Smrg * Software. 1446185892Smrg * 1546185892Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 1646185892Smrg * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 1746185892Smrg * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 1846185892Smrg * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 1946185892Smrg * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 2046185892Smrg * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 2146185892Smrg * IN THE SOFTWARE. 2246185892Smrg * 2346185892Smrg * Authors: 2446185892Smrg * Eric Anholt <eric@anholt.net> 2546185892Smrg * 2646185892Smrg */ 2746185892Smrg 2846185892Smrg#ifndef _HASH_TABLE_H 2946185892Smrg#define _HASH_TABLE_H 3046185892Smrg 3146185892Smrg#include <stdlib.h> 3246185892Smrg#include <inttypes.h> 3346185892Smrg#include <stdbool.h> 3446185892Smrg#include "c99_compat.h" 3546185892Smrg#include "macros.h" 3646185892Smrg 3746185892Smrg#ifdef __cplusplus 3846185892Smrgextern "C" { 3946185892Smrg#endif 4046185892Smrg 4146185892Smrgstruct hash_entry { 4246185892Smrg uint32_t hash; 4346185892Smrg const void *key; 4446185892Smrg void *data; 4546185892Smrg}; 4646185892Smrg 4746185892Smrgstruct hash_table { 4846185892Smrg struct hash_entry *table; 4946185892Smrg bool (*key_equals_function)(const void *a, const void *b); 5046185892Smrg const void *deleted_key; 5146185892Smrg uint32_t size; 5246185892Smrg uint32_t rehash; 53 uint32_t max_entries; 54 uint32_t size_index; 55 uint32_t entries; 56 uint32_t deleted_entries; 57}; 58 59struct hash_table * 60_mesa_hash_table_create(void *mem_ctx, 61 bool (*key_equals_function)(const void *a, 62 const void *b)); 63void _mesa_hash_table_destroy(struct hash_table *ht, 64 void (*delete_function)(struct hash_entry *entry)); 65void _mesa_hash_table_set_deleted_key(struct hash_table *ht, 66 const void *deleted_key); 67 68struct hash_entry * 69_mesa_hash_table_insert(struct hash_table *ht, uint32_t hash, 70 const void *key, void *data); 71struct hash_entry * 72_mesa_hash_table_search(struct hash_table *ht, uint32_t hash, 73 const void *key); 74void _mesa_hash_table_remove(struct hash_table *ht, 75 struct hash_entry *entry); 76 77struct hash_entry *_mesa_hash_table_next_entry(struct hash_table *ht, 78 struct hash_entry *entry); 79struct hash_entry * 80_mesa_hash_table_random_entry(struct hash_table *ht, 81 bool (*predicate)(struct hash_entry *entry)); 82 83uint32_t _mesa_hash_data(const void *data, size_t size); 84uint32_t _mesa_hash_string(const char *key); 85bool _mesa_key_string_equal(const void *a, const void *b); 86bool _mesa_key_pointer_equal(const void *a, const void *b); 87 88static inline uint32_t _mesa_hash_pointer(const void *pointer) 89{ 90 return _mesa_hash_data(&pointer, sizeof(pointer)); 91} 92 93/** 94 * This foreach function is safe against deletion (which just replaces 95 * an entry's data with the deleted marker), but not against insertion 96 * (which may rehash the table, making entry a dangling pointer). 97 */ 98#define hash_table_foreach(ht, entry) \ 99 for (entry = _mesa_hash_table_next_entry(ht, NULL); \ 100 entry != NULL; \ 101 entry = _mesa_hash_table_next_entry(ht, entry)) 102 103#ifdef __cplusplus 104} /* extern C */ 105#endif 106 107#endif /* _HASH_TABLE_H */ 108