1bf215546Sopenharmony_ci/*
2bf215546Sopenharmony_ci * Copyright © 2009-2012 Intel Corporation
3bf215546Sopenharmony_ci *
4bf215546Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a
5bf215546Sopenharmony_ci * copy of this software and associated documentation files (the "Software"),
6bf215546Sopenharmony_ci * to deal in the Software without restriction, including without limitation
7bf215546Sopenharmony_ci * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8bf215546Sopenharmony_ci * and/or sell copies of the Software, and to permit persons to whom the
9bf215546Sopenharmony_ci * Software is furnished to do so, subject to the following conditions:
10bf215546Sopenharmony_ci *
11bf215546Sopenharmony_ci * The above copyright notice and this permission notice (including the next
12bf215546Sopenharmony_ci * paragraph) shall be included in all copies or substantial portions of the
13bf215546Sopenharmony_ci * Software.
14bf215546Sopenharmony_ci *
15bf215546Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16bf215546Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17bf215546Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18bf215546Sopenharmony_ci * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19bf215546Sopenharmony_ci * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20bf215546Sopenharmony_ci * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21bf215546Sopenharmony_ci * IN THE SOFTWARE.
22bf215546Sopenharmony_ci *
23bf215546Sopenharmony_ci * Authors:
24bf215546Sopenharmony_ci *    Eric Anholt <eric@anholt.net>
25bf215546Sopenharmony_ci *
26bf215546Sopenharmony_ci */
27bf215546Sopenharmony_ci
28bf215546Sopenharmony_ci#ifndef _SET_H
29bf215546Sopenharmony_ci#define _SET_H
30bf215546Sopenharmony_ci
31bf215546Sopenharmony_ci#include <inttypes.h>
32bf215546Sopenharmony_ci#include <stdbool.h>
33bf215546Sopenharmony_ci
34bf215546Sopenharmony_ci#ifdef __cplusplus
35bf215546Sopenharmony_ciextern "C" {
36bf215546Sopenharmony_ci#endif
37bf215546Sopenharmony_ci
38bf215546Sopenharmony_cistruct set_entry {
39bf215546Sopenharmony_ci   uint32_t hash;
40bf215546Sopenharmony_ci   const void *key;
41bf215546Sopenharmony_ci};
42bf215546Sopenharmony_ci
43bf215546Sopenharmony_cistruct set {
44bf215546Sopenharmony_ci   void *mem_ctx;
45bf215546Sopenharmony_ci   struct set_entry *table;
46bf215546Sopenharmony_ci   uint32_t (*key_hash_function)(const void *key);
47bf215546Sopenharmony_ci   bool (*key_equals_function)(const void *a, const void *b);
48bf215546Sopenharmony_ci   uint32_t size;
49bf215546Sopenharmony_ci   uint32_t rehash;
50bf215546Sopenharmony_ci   uint64_t size_magic;
51bf215546Sopenharmony_ci   uint64_t rehash_magic;
52bf215546Sopenharmony_ci   uint32_t max_entries;
53bf215546Sopenharmony_ci   uint32_t size_index;
54bf215546Sopenharmony_ci   uint32_t entries;
55bf215546Sopenharmony_ci   uint32_t deleted_entries;
56bf215546Sopenharmony_ci};
57bf215546Sopenharmony_ci
58bf215546Sopenharmony_cibool
59bf215546Sopenharmony_ci_mesa_set_init(struct set *ht, void *mem_ctx,
60bf215546Sopenharmony_ci                 uint32_t (*key_hash_function)(const void *key),
61bf215546Sopenharmony_ci                 bool (*key_equals_function)(const void *a,
62bf215546Sopenharmony_ci                                             const void *b));
63bf215546Sopenharmony_ci
64bf215546Sopenharmony_cistruct set *
65bf215546Sopenharmony_ci_mesa_set_create(void *mem_ctx,
66bf215546Sopenharmony_ci                 uint32_t (*key_hash_function)(const void *key),
67bf215546Sopenharmony_ci                 bool (*key_equals_function)(const void *a,
68bf215546Sopenharmony_ci                                             const void *b));
69bf215546Sopenharmony_cistruct set *
70bf215546Sopenharmony_ci_mesa_set_create_u32_keys(void *mem_ctx);
71bf215546Sopenharmony_ci
72bf215546Sopenharmony_cistruct set *
73bf215546Sopenharmony_ci_mesa_set_clone(struct set *set, void *dst_mem_ctx);
74bf215546Sopenharmony_ci
75bf215546Sopenharmony_civoid
76bf215546Sopenharmony_ci_mesa_set_destroy(struct set *set,
77bf215546Sopenharmony_ci                  void (*delete_function)(struct set_entry *entry));
78bf215546Sopenharmony_civoid
79bf215546Sopenharmony_ci_mesa_set_resize(struct set *set, uint32_t entries);
80bf215546Sopenharmony_civoid
81bf215546Sopenharmony_ci_mesa_set_clear(struct set *set,
82bf215546Sopenharmony_ci                void (*delete_function)(struct set_entry *entry));
83bf215546Sopenharmony_ci
84bf215546Sopenharmony_cistruct set_entry *
85bf215546Sopenharmony_ci_mesa_set_add(struct set *set, const void *key);
86bf215546Sopenharmony_cistruct set_entry *
87bf215546Sopenharmony_ci_mesa_set_add_pre_hashed(struct set *set, uint32_t hash, const void *key);
88bf215546Sopenharmony_ci
89bf215546Sopenharmony_cistruct set_entry *
90bf215546Sopenharmony_ci_mesa_set_search_or_add(struct set *set, const void *key, bool *found);
91bf215546Sopenharmony_cistruct set_entry *
92bf215546Sopenharmony_ci_mesa_set_search_or_add_pre_hashed(struct set *set, uint32_t hash,
93bf215546Sopenharmony_ci                                   const void *key, bool *found);
94bf215546Sopenharmony_ci
95bf215546Sopenharmony_cistruct set_entry *
96bf215546Sopenharmony_ci_mesa_set_search(const struct set *set, const void *key);
97bf215546Sopenharmony_cistruct set_entry *
98bf215546Sopenharmony_ci_mesa_set_search_pre_hashed(const struct set *set, uint32_t hash,
99bf215546Sopenharmony_ci                            const void *key);
100bf215546Sopenharmony_ci
101bf215546Sopenharmony_cistruct set_entry *
102bf215546Sopenharmony_ci_mesa_set_search_and_add(struct set *set, const void *key, bool *replaced);
103bf215546Sopenharmony_cistruct set_entry *
104bf215546Sopenharmony_ci_mesa_set_search_and_add_pre_hashed(struct set *set, uint32_t hash,
105bf215546Sopenharmony_ci                                    const void *key, bool *replaced);
106bf215546Sopenharmony_ci
107bf215546Sopenharmony_civoid
108bf215546Sopenharmony_ci_mesa_set_remove(struct set *set, struct set_entry *entry);
109bf215546Sopenharmony_civoid
110bf215546Sopenharmony_ci_mesa_set_remove_key(struct set *set, const void *key);
111bf215546Sopenharmony_ci
112bf215546Sopenharmony_cistruct set_entry *
113bf215546Sopenharmony_ci_mesa_set_next_entry(const struct set *set, struct set_entry *entry);
114bf215546Sopenharmony_cistruct set_entry *
115bf215546Sopenharmony_ci_mesa_set_next_entry_unsafe(const struct set *set, struct set_entry *entry);
116bf215546Sopenharmony_ci
117bf215546Sopenharmony_cistruct set_entry *
118bf215546Sopenharmony_ci_mesa_set_random_entry(struct set *set,
119bf215546Sopenharmony_ci                       int (*predicate)(struct set_entry *entry));
120bf215546Sopenharmony_ci
121bf215546Sopenharmony_cistruct set *
122bf215546Sopenharmony_ci_mesa_pointer_set_create(void *mem_ctx);
123bf215546Sopenharmony_ci
124bf215546Sopenharmony_cibool
125bf215546Sopenharmony_ci_mesa_set_intersects(struct set *a, struct set *b);
126bf215546Sopenharmony_ci
127bf215546Sopenharmony_ci/**
128bf215546Sopenharmony_ci * This foreach function is safe against deletion, but not against
129bf215546Sopenharmony_ci * insertion (which may rehash the set, making entry a dangling
130bf215546Sopenharmony_ci * pointer).
131bf215546Sopenharmony_ci */
132bf215546Sopenharmony_ci#define set_foreach(set, entry)                                     \
133bf215546Sopenharmony_ci   for (struct set_entry *entry = _mesa_set_next_entry(set, NULL);  \
134bf215546Sopenharmony_ci        entry != NULL;                                              \
135bf215546Sopenharmony_ci        entry = _mesa_set_next_entry(set, entry))
136bf215546Sopenharmony_ci
137bf215546Sopenharmony_ci/**
138bf215546Sopenharmony_ci * This foreach function destroys the table as it iterates.
139bf215546Sopenharmony_ci * It is not safe to use when inserting or removing entries.
140bf215546Sopenharmony_ci */
141bf215546Sopenharmony_ci#define set_foreach_remove(set, entry)                              \
142bf215546Sopenharmony_ci   for (struct set_entry *entry = _mesa_set_next_entry_unsafe(set, NULL);  \
143bf215546Sopenharmony_ci        (set)->entries;                                              \
144bf215546Sopenharmony_ci        entry->hash = 0, entry->key = (void*)NULL, (set)->entries--, entry = _mesa_set_next_entry_unsafe(set, entry))
145bf215546Sopenharmony_ci
146bf215546Sopenharmony_ci#ifdef __cplusplus
147bf215546Sopenharmony_ci} /* extern C */
148bf215546Sopenharmony_ci#endif
149bf215546Sopenharmony_ci
150bf215546Sopenharmony_ci#endif /* _SET_H */
151