blob: 783c3d41c4620564e22db30a7bbd4a04f253b0ac [file] [log] [blame]
Eric Anholt82c9d982012-12-04 01:03:57 -08001/*
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
35extern "C" {
36#endif
37
38struct set_entry {
39 uint32_t hash;
40 const void *key;
41};
42
43struct set {
44 void *mem_ctx;
45 struct set_entry *table;
Jason Ekstrand153b8b32015-01-15 09:31:18 -080046 uint32_t (*key_hash_function)(const void *key);
Eric Anholt82c9d982012-12-04 01:03:57 -080047 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
56struct set *
57_mesa_set_create(void *mem_ctx,
Jason Ekstrand153b8b32015-01-15 09:31:18 -080058 uint32_t (*key_hash_function)(const void *key),
Eric Anholt82c9d982012-12-04 01:03:57 -080059 bool (*key_equals_function)(const void *a,
60 const void *b));
Caio Marcelo de Oliveira Filhob034fac2018-06-25 09:51:20 -070061struct set *
62_mesa_set_clone(struct set *set, void *dst_mem_ctx);
63
Eric Anholt82c9d982012-12-04 01:03:57 -080064void
65_mesa_set_destroy(struct set *set,
66 void (*delete_function)(struct set_entry *entry));
Scott D Phillips5c075b02018-05-02 09:01:02 -070067void
Jason Ekstrandbab08c72019-05-10 13:50:56 -050068_mesa_set_resize(struct set *set, uint32_t entries);
69void
Scott D Phillips5c075b02018-05-02 09:01:02 -070070_mesa_set_clear(struct set *set,
71 void (*delete_function)(struct set_entry *entry));
Eric Anholt82c9d982012-12-04 01:03:57 -080072
73struct set_entry *
Jason Ekstrand153b8b32015-01-15 09:31:18 -080074_mesa_set_add(struct set *set, const void *key);
75struct set_entry *
76_mesa_set_add_pre_hashed(struct set *set, uint32_t hash, const void *key);
Eric Anholt82c9d982012-12-04 01:03:57 -080077
78struct set_entry *
Connor Abbott8a838e12019-03-27 12:00:54 +010079_mesa_set_search_or_add(struct set *set, const void *key);
80struct set_entry *
81_mesa_set_search_or_add_pre_hashed(struct set *set, uint32_t hash,
82 const void *key);
83
84struct set_entry *
Jason Ekstrand153b8b32015-01-15 09:31:18 -080085_mesa_set_search(const struct set *set, const void *key);
86struct set_entry *
87_mesa_set_search_pre_hashed(const struct set *set, uint32_t hash,
88 const void *key);
Eric Anholt82c9d982012-12-04 01:03:57 -080089
Jason Ekstrandabb45082019-05-10 13:37:42 -050090struct set_entry *
91_mesa_set_search_and_add(struct set *set, const void *key, bool *replaced);
92struct set_entry *
93_mesa_set_search_and_add_pre_hashed(struct set *set, uint32_t hash,
94 const void *key, bool *replaced);
95
Eric Anholt82c9d982012-12-04 01:03:57 -080096void
97_mesa_set_remove(struct set *set, struct set_entry *entry);
Caio Marcelo de Oliveira Filhofa0c19d2018-06-25 13:42:22 -070098void
99_mesa_set_remove_key(struct set *set, const void *key);
Eric Anholt82c9d982012-12-04 01:03:57 -0800100
101struct set_entry *
102_mesa_set_next_entry(const struct set *set, struct set_entry *entry);
103
104struct set_entry *
105_mesa_set_random_entry(struct set *set,
106 int (*predicate)(struct set_entry *entry));
107
Caio Marcelo de Oliveira Filhoee23e8b2018-09-11 16:37:33 -0700108struct set *
109_mesa_pointer_set_create(void *mem_ctx);
110
Eric Anholt82c9d982012-12-04 01:03:57 -0800111/**
112 * This foreach function is safe against deletion, but not against
113 * insertion (which may rehash the set, making entry a dangling
114 * pointer).
115 */
Eric Engestrome27902a2018-10-20 18:00:09 +0100116#define set_foreach(set, entry) \
117 for (struct set_entry *entry = _mesa_set_next_entry(set, NULL); \
118 entry != NULL; \
Eric Anholt82c9d982012-12-04 01:03:57 -0800119 entry = _mesa_set_next_entry(set, entry))
120
121#ifdef __cplusplus
122} /* extern C */
123#endif
124
125#endif /* _SET_H */