hash_table.h revision 848b8605
1848b8605Smrg/*
2848b8605Smrg * Copyright © 2009,2012 Intel Corporation
3848b8605Smrg *
4848b8605Smrg * Permission is hereby granted, free of charge, to any person obtaining a
5848b8605Smrg * copy of this software and associated documentation files (the "Software"),
6848b8605Smrg * to deal in the Software without restriction, including without limitation
7848b8605Smrg * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8848b8605Smrg * and/or sell copies of the Software, and to permit persons to whom the
9848b8605Smrg * Software is furnished to do so, subject to the following conditions:
10848b8605Smrg *
11848b8605Smrg * The above copyright notice and this permission notice (including the next
12848b8605Smrg * paragraph) shall be included in all copies or substantial portions of the
13848b8605Smrg * Software.
14848b8605Smrg *
15848b8605Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16848b8605Smrg * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17848b8605Smrg * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18848b8605Smrg * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19848b8605Smrg * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20848b8605Smrg * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21848b8605Smrg * IN THE SOFTWARE.
22848b8605Smrg *
23848b8605Smrg * Authors:
24848b8605Smrg *    Eric Anholt <eric@anholt.net>
25848b8605Smrg *
26848b8605Smrg */
27848b8605Smrg
28848b8605Smrg#ifndef _HASH_TABLE_H
29848b8605Smrg#define _HASH_TABLE_H
30848b8605Smrg
31848b8605Smrg#include <stdlib.h>
32848b8605Smrg#include <inttypes.h>
33848b8605Smrg#include <stdbool.h>
34848b8605Smrg#include "c99_compat.h"
35848b8605Smrg#include "macros.h"
36848b8605Smrg
37848b8605Smrg#ifdef __cplusplus
38848b8605Smrgextern "C" {
39848b8605Smrg#endif
40848b8605Smrg
41848b8605Smrgstruct hash_entry {
42848b8605Smrg   uint32_t hash;
43848b8605Smrg   const void *key;
44848b8605Smrg   void *data;
45848b8605Smrg};
46848b8605Smrg
47848b8605Smrgstruct hash_table {
48848b8605Smrg   struct hash_entry *table;
49848b8605Smrg   bool (*key_equals_function)(const void *a, const void *b);
50848b8605Smrg   const void *deleted_key;
51848b8605Smrg   uint32_t size;
52848b8605Smrg   uint32_t rehash;
53848b8605Smrg   uint32_t max_entries;
54848b8605Smrg   uint32_t size_index;
55848b8605Smrg   uint32_t entries;
56848b8605Smrg   uint32_t deleted_entries;
57848b8605Smrg};
58848b8605Smrg
59848b8605Smrgstruct hash_table *
60848b8605Smrg_mesa_hash_table_create(void *mem_ctx,
61848b8605Smrg                        bool (*key_equals_function)(const void *a,
62848b8605Smrg                                                    const void *b));
63848b8605Smrgvoid _mesa_hash_table_destroy(struct hash_table *ht,
64848b8605Smrg                              void (*delete_function)(struct hash_entry *entry));
65848b8605Smrgvoid _mesa_hash_table_set_deleted_key(struct hash_table *ht,
66848b8605Smrg                                      const void *deleted_key);
67848b8605Smrg
68848b8605Smrgstruct hash_entry *
69848b8605Smrg_mesa_hash_table_insert(struct hash_table *ht, uint32_t hash,
70848b8605Smrg                        const void *key, void *data);
71848b8605Smrgstruct hash_entry *
72848b8605Smrg_mesa_hash_table_search(struct hash_table *ht, uint32_t hash,
73848b8605Smrg                        const void *key);
74848b8605Smrgvoid _mesa_hash_table_remove(struct hash_table *ht,
75848b8605Smrg                             struct hash_entry *entry);
76848b8605Smrg
77848b8605Smrgstruct hash_entry *_mesa_hash_table_next_entry(struct hash_table *ht,
78848b8605Smrg                                               struct hash_entry *entry);
79848b8605Smrgstruct hash_entry *
80848b8605Smrg_mesa_hash_table_random_entry(struct hash_table *ht,
81848b8605Smrg                              bool (*predicate)(struct hash_entry *entry));
82848b8605Smrg
83848b8605Smrguint32_t _mesa_hash_data(const void *data, size_t size);
84848b8605Smrguint32_t _mesa_hash_string(const char *key);
85848b8605Smrgbool _mesa_key_string_equal(const void *a, const void *b);
86848b8605Smrgbool _mesa_key_pointer_equal(const void *a, const void *b);
87848b8605Smrg
88848b8605Smrgstatic inline uint32_t _mesa_hash_pointer(const void *pointer)
89848b8605Smrg{
90848b8605Smrg   return _mesa_hash_data(&pointer, sizeof(pointer));
91848b8605Smrg}
92848b8605Smrg
93848b8605Smrg/**
94848b8605Smrg * This foreach function is safe against deletion (which just replaces
95848b8605Smrg * an entry's data with the deleted marker), but not against insertion
96848b8605Smrg * (which may rehash the table, making entry a dangling pointer).
97848b8605Smrg */
98848b8605Smrg#define hash_table_foreach(ht, entry)                   \
99848b8605Smrg   for (entry = _mesa_hash_table_next_entry(ht, NULL);  \
100848b8605Smrg        entry != NULL;                                  \
101848b8605Smrg        entry = _mesa_hash_table_next_entry(ht, entry))
102848b8605Smrg
103848b8605Smrg#ifdef __cplusplus
104848b8605Smrg} /* extern C */
105848b8605Smrg#endif
106848b8605Smrg
107848b8605Smrg#endif /* _HASH_TABLE_H */
108