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 /** 82 * Remove all mappings from this map. 83 */ clearstring_to_uint_map84 void clear() 85 { 86 hash_table_call_foreach(this->ht, delete_key, NULL); 87 _mesa_hash_table_clear(this->ht, NULL); 88 } 89 90 /** 91 * Runs a passed callback for the hash 92 */ iteratestring_to_uint_map93 void iterate(void (*func)(const char *, unsigned, void *), void *closure) 94 { 95 struct string_map_iterate_wrapper_closure *wrapper; 96 97 wrapper = (struct string_map_iterate_wrapper_closure *) 98 malloc(sizeof(struct string_map_iterate_wrapper_closure)); 99 if (wrapper == NULL) 100 return; 101 102 wrapper->callback = func; 103 wrapper->closure = closure; 104 105 hash_table_call_foreach(this->ht, subtract_one_wrapper, wrapper); 106 free(wrapper); 107 } 108 109 /** 110 * Get the value associated with a particular key 111 * 112 * \return 113 * If \c key is found in the map, \c true is returned. Otherwise \c false 114 * is returned. 115 * 116 * \note 117 * If \c key is not found in the table, \c value is not modified. 118 */ getstring_to_uint_map119 bool get(unsigned &value, const char *key) 120 { 121 hash_entry *entry = _mesa_hash_table_search(this->ht, 122 (const void *) key); 123 124 if (!entry) 125 return false; 126 127 const intptr_t v = (intptr_t) entry->data; 128 value = (unsigned)(v - 1); 129 return true; 130 } 131 putstring_to_uint_map132 void put(unsigned value, const char *key) 133 { 134 /* The low-level hash table structure returns NULL if key is not in the 135 * hash table. However, users of this map might want to store zero as a 136 * valid value in the table. Bias the value by +1 so that a 137 * user-specified zero is stored as 1. This enables ::get to tell the 138 * difference between a user-specified zero (returned as 1 by 139 * _mesa_hash_table_search) and the key not in the table (returned as 0 by 140 * _mesa_hash_table_search). 141 * 142 * The net effect is that we can't store UINT_MAX in the table. This is 143 * because UINT_MAX+1 = 0. 144 */ 145 assert(value != UINT_MAX); 146 char *dup_key = strdup(key); 147 148 struct hash_entry *entry = _mesa_hash_table_search(this->ht, dup_key); 149 if (entry) { 150 entry->data = (void *) (intptr_t) (value + 1); 151 } else { 152 _mesa_hash_table_insert(this->ht, dup_key, 153 (void *) (intptr_t) (value + 1)); 154 } 155 156 if (entry) 157 free(dup_key); 158 } 159 160 private: delete_keystring_to_uint_map161 static void delete_key(const void *key, void *data, void *closure) 162 { 163 (void) data; 164 (void) closure; 165 166 free((char *)key); 167 } 168 subtract_one_wrapperstring_to_uint_map169 static void subtract_one_wrapper(const void *key, void *data, void *closure) 170 { 171 struct string_map_iterate_wrapper_closure *wrapper = 172 (struct string_map_iterate_wrapper_closure *) closure; 173 unsigned value = (intptr_t) data; 174 175 value -= 1; 176 177 wrapper->callback((const char *) key, value, wrapper->closure); 178 } 179 180 struct hash_table *ht; 181 }; 182 183 #endif /* __cplusplus */ 184 #endif /* STRING_TO_UINT_MAP_H */ 185