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