xref: /aosp_15_r20/external/mesa3d/src/compiler/glsl/string_to_uint_map.h (revision 6104692788411f58d303aa86923a9ff6ecaded22)
1 /*
2  * Copyright © 2008 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  */
24 
25 #ifndef STRING_TO_UINT_MAP_H
26 #define STRING_TO_UINT_MAP_H
27 
28 #include <string.h>
29 #include <limits.h>
30 #include "util/hash_table.h"
31 
32 struct string_to_uint_map;
33 
34 #ifdef __cplusplus
35 extern "C" {
36 #endif
37 
38 struct string_to_uint_map *
39 string_to_uint_map_ctor();
40 
41 void
42 string_to_uint_map_dtor(struct string_to_uint_map *);
43 
44 void
45 string_to_uint_map_put(struct string_to_uint_map *map,
46                        unsigned value, const char *key);
47 
48 bool
49 string_to_uint_map_get(struct string_to_uint_map *map,
50                        unsigned *value, const char *key);
51 
52 #ifdef __cplusplus
53 }
54 
55 struct string_map_iterate_wrapper_closure {
56    void (*callback)(const char *key, unsigned value, void *closure);
57    void *closure;
58 };
59 
60 /**
61  * Map from a string (name) to an unsigned integer value
62  *
63  * \note
64  * Because of the way this class interacts with the \c hash_table
65  * implementation, values of \c UINT_MAX cannot be stored in the map.
66  */
67 struct string_to_uint_map {
68 public:
string_to_uint_mapstring_to_uint_map69    string_to_uint_map()
70    {
71       this->ht = _mesa_hash_table_create(NULL, _mesa_hash_string,
72                                          _mesa_key_string_equal);
73    }
74 
~string_to_uint_mapstring_to_uint_map75    ~string_to_uint_map()
76    {
77       hash_table_call_foreach(this->ht, delete_key, NULL);
78       _mesa_hash_table_destroy(this->ht, NULL);
79    }
80 
81    string_to_uint_map(const string_to_uint_map &) = delete;
82    string_to_uint_map & operator=(const string_to_uint_map &) = delete;
83 
84    /**
85     * Remove all mappings from this map.
86     */
clearstring_to_uint_map87    void clear()
88    {
89       hash_table_call_foreach(this->ht, delete_key, NULL);
90       _mesa_hash_table_clear(this->ht, NULL);
91    }
92 
93    /**
94     * Runs a passed callback for the hash
95     */
iteratestring_to_uint_map96    void iterate(void (*func)(const char *, unsigned, void *), void *closure)
97    {
98       struct string_map_iterate_wrapper_closure *wrapper;
99 
100       wrapper = (struct string_map_iterate_wrapper_closure *)
101          malloc(sizeof(struct string_map_iterate_wrapper_closure));
102       if (wrapper == NULL)
103          return;
104 
105       wrapper->callback = func;
106       wrapper->closure = closure;
107 
108       hash_table_call_foreach(this->ht, subtract_one_wrapper, wrapper);
109       free(wrapper);
110    }
111 
112    /**
113     * Get the value associated with a particular key
114     *
115     * \return
116     * If \c key is found in the map, \c true is returned.  Otherwise \c false
117     * is returned.
118     *
119     * \note
120     * If \c key is not found in the table, \c value is not modified.
121     */
getstring_to_uint_map122    bool get(unsigned &value, const char *key)
123    {
124       hash_entry *entry = _mesa_hash_table_search(this->ht,
125                                                   (const void *) key);
126 
127       if (!entry)
128          return false;
129 
130       const intptr_t v = (intptr_t) entry->data;
131       value = (unsigned)(v - 1);
132       return true;
133    }
134 
putstring_to_uint_map135    void put(unsigned value, const char *key)
136    {
137       /* The low-level hash table structure returns NULL if key is not in the
138        * hash table.  However, users of this map might want to store zero as a
139        * valid value in the table.  Bias the value by +1 so that a
140        * user-specified zero is stored as 1.  This enables ::get to tell the
141        * difference between a user-specified zero (returned as 1 by
142        * _mesa_hash_table_search) and the key not in the table (returned as 0 by
143        * _mesa_hash_table_search).
144        *
145        * The net effect is that we can't store UINT_MAX in the table.  This is
146        * because UINT_MAX+1 = 0.
147        */
148       assert(value != UINT_MAX);
149       char *dup_key = strdup(key);
150 
151       struct hash_entry *entry = _mesa_hash_table_search(this->ht, dup_key);
152       if (entry) {
153          entry->data = (void *) (intptr_t) (value + 1);
154       } else {
155          _mesa_hash_table_insert(this->ht, dup_key,
156                                  (void *) (intptr_t) (value + 1));
157       }
158 
159       if (entry)
160          free(dup_key);
161    }
162 
163 private:
delete_keystring_to_uint_map164    static void delete_key(const void *key, void *data, void *closure)
165    {
166       (void) data;
167       (void) closure;
168 
169       free((char *)key);
170    }
171 
subtract_one_wrapperstring_to_uint_map172    static void subtract_one_wrapper(const void *key, void *data, void *closure)
173    {
174       struct string_map_iterate_wrapper_closure *wrapper =
175          (struct string_map_iterate_wrapper_closure *) closure;
176       unsigned value = (intptr_t) data;
177 
178       value -= 1;
179 
180       wrapper->callback((const char *) key, value, wrapper->closure);
181    }
182 
183    struct hash_table *ht;
184 };
185 
186 #endif /* __cplusplus */
187 #endif /* STRING_TO_UINT_MAP_H */
188