1b8e80941Smrg/*
2b8e80941Smrg * Copyright © 2009-2012 Intel Corporation
3b8e80941Smrg *
4b8e80941Smrg * Permission is hereby granted, free of charge, to any person obtaining a
5b8e80941Smrg * copy of this software and associated documentation files (the "Software"),
6b8e80941Smrg * to deal in the Software without restriction, including without limitation
7b8e80941Smrg * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8b8e80941Smrg * and/or sell copies of the Software, and to permit persons to whom the
9b8e80941Smrg * Software is furnished to do so, subject to the following conditions:
10b8e80941Smrg *
11b8e80941Smrg * The above copyright notice and this permission notice (including the next
12b8e80941Smrg * paragraph) shall be included in all copies or substantial portions of the
13b8e80941Smrg * Software.
14b8e80941Smrg *
15b8e80941Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16b8e80941Smrg * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17b8e80941Smrg * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18b8e80941Smrg * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19b8e80941Smrg * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20b8e80941Smrg * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21b8e80941Smrg * IN THE SOFTWARE.
22b8e80941Smrg *
23b8e80941Smrg * Authors:
24b8e80941Smrg *    Eric Anholt <eric@anholt.net>
25b8e80941Smrg *
26b8e80941Smrg */
27b8e80941Smrg
28b8e80941Smrg#ifndef _SET_H
29b8e80941Smrg#define _SET_H
30b8e80941Smrg
31b8e80941Smrg#include <inttypes.h>
32b8e80941Smrg#include <stdbool.h>
33b8e80941Smrg
34b8e80941Smrg#ifdef __cplusplus
35b8e80941Smrgextern "C" {
36b8e80941Smrg#endif
37b8e80941Smrg
38b8e80941Smrgstruct set_entry {
39b8e80941Smrg   uint32_t hash;
40b8e80941Smrg   const void *key;
41b8e80941Smrg};
42b8e80941Smrg
43b8e80941Smrgstruct set {
44b8e80941Smrg   void *mem_ctx;
45b8e80941Smrg   struct set_entry *table;
46b8e80941Smrg   uint32_t (*key_hash_function)(const void *key);
47b8e80941Smrg   bool (*key_equals_function)(const void *a, const void *b);
48b8e80941Smrg   uint32_t size;
49b8e80941Smrg   uint32_t rehash;
50b8e80941Smrg   uint32_t max_entries;
51b8e80941Smrg   uint32_t size_index;
52b8e80941Smrg   uint32_t entries;
53b8e80941Smrg   uint32_t deleted_entries;
54b8e80941Smrg};
55b8e80941Smrg
56b8e80941Smrgstruct set *
57b8e80941Smrg_mesa_set_create(void *mem_ctx,
58b8e80941Smrg                 uint32_t (*key_hash_function)(const void *key),
59b8e80941Smrg                 bool (*key_equals_function)(const void *a,
60b8e80941Smrg                                             const void *b));
61b8e80941Smrgstruct set *
62b8e80941Smrg_mesa_set_clone(struct set *set, void *dst_mem_ctx);
63b8e80941Smrg
64b8e80941Smrgvoid
65b8e80941Smrg_mesa_set_destroy(struct set *set,
66b8e80941Smrg                  void (*delete_function)(struct set_entry *entry));
67b8e80941Smrgvoid
68b8e80941Smrg_mesa_set_clear(struct set *set,
69b8e80941Smrg                void (*delete_function)(struct set_entry *entry));
70b8e80941Smrg
71b8e80941Smrgstruct set_entry *
72b8e80941Smrg_mesa_set_add(struct set *set, const void *key);
73b8e80941Smrgstruct set_entry *
74b8e80941Smrg_mesa_set_add_pre_hashed(struct set *set, uint32_t hash, const void *key);
75b8e80941Smrg
76b8e80941Smrgstruct set_entry *
77b8e80941Smrg_mesa_set_search(const struct set *set, const void *key);
78b8e80941Smrgstruct set_entry *
79b8e80941Smrg_mesa_set_search_pre_hashed(const struct set *set, uint32_t hash,
80b8e80941Smrg                            const void *key);
81b8e80941Smrg
82b8e80941Smrgvoid
83b8e80941Smrg_mesa_set_remove(struct set *set, struct set_entry *entry);
84b8e80941Smrgvoid
85b8e80941Smrg_mesa_set_remove_key(struct set *set, const void *key);
86b8e80941Smrg
87b8e80941Smrgstruct set_entry *
88b8e80941Smrg_mesa_set_next_entry(const struct set *set, struct set_entry *entry);
89b8e80941Smrg
90b8e80941Smrgstruct set_entry *
91b8e80941Smrg_mesa_set_random_entry(struct set *set,
92b8e80941Smrg                       int (*predicate)(struct set_entry *entry));
93b8e80941Smrg
94b8e80941Smrgstruct set *
95b8e80941Smrg_mesa_pointer_set_create(void *mem_ctx);
96b8e80941Smrg
97b8e80941Smrg/**
98b8e80941Smrg * This foreach function is safe against deletion, but not against
99b8e80941Smrg * insertion (which may rehash the set, making entry a dangling
100b8e80941Smrg * pointer).
101b8e80941Smrg */
102b8e80941Smrg#define set_foreach(set, entry)                                     \
103b8e80941Smrg   for (struct set_entry *entry = _mesa_set_next_entry(set, NULL);  \
104b8e80941Smrg        entry != NULL;                                              \
105b8e80941Smrg        entry = _mesa_set_next_entry(set, entry))
106b8e80941Smrg
107b8e80941Smrg#ifdef __cplusplus
108b8e80941Smrg} /* extern C */
109b8e80941Smrg#endif
110b8e80941Smrg
111b8e80941Smrg#endif /* _SET_H */
112