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 45 #ifdef __cplusplus 46 } 47 48 struct string_map_iterate_wrapper_closure { 49 void (*callback)(const char *key, unsigned value, void *closure); 50 void *closure; 51 }; 52 53 /** 54 * Map from a string (name) to an unsigned integer value 55 * 56 * \note 57 * Because of the way this class interacts with the \c hash_table 58 * implementation, values of \c UINT_MAX cannot be stored in the map. 59 */ 60 struct string_to_uint_map { 61 public: string_to_uint_mapstring_to_uint_map62 string_to_uint_map() 63 { 64 this->ht = _mesa_hash_table_create(NULL, _mesa_key_hash_string, 65 _mesa_key_string_equal); 66 } 67 ~string_to_uint_mapstring_to_uint_map68 ~string_to_uint_map() 69 { 70 hash_table_call_foreach(this->ht, delete_key, NULL); 71 _mesa_hash_table_destroy(this->ht, NULL); 72 } 73 74 /** 75 * Remove all mappings from this map. 76 */ clearstring_to_uint_map77 void clear() 78 { 79 hash_table_call_foreach(this->ht, delete_key, NULL); 80 _mesa_hash_table_clear(this->ht, NULL); 81 } 82 83 /** 84 * Runs a passed callback for the hash 85 */ iteratestring_to_uint_map86 void iterate(void (*func)(const char *, unsigned, void *), void *closure) 87 { 88 struct string_map_iterate_wrapper_closure *wrapper; 89 90 wrapper = (struct string_map_iterate_wrapper_closure *) 91 malloc(sizeof(struct string_map_iterate_wrapper_closure)); 92 if (wrapper == NULL) 93 return; 94 95 wrapper->callback = func; 96 wrapper->closure = closure; 97 98 hash_table_call_foreach(this->ht, subtract_one_wrapper, wrapper); 99 free(wrapper); 100 } 101 102 /** 103 * Get the value associated with a particular key 104 * 105 * \return 106 * If \c key is found in the map, \c true is returned. Otherwise \c false 107 * is returned. 108 * 109 * \note 110 * If \c key is not found in the table, \c value is not modified. 111 */ getstring_to_uint_map112 bool get(unsigned &value, const char *key) 113 { 114 hash_entry *entry = _mesa_hash_table_search(this->ht, 115 (const void *) key); 116 117 if (!entry) 118 return false; 119 120 const intptr_t v = (intptr_t) entry->data; 121 value = (unsigned)(v - 1); 122 return true; 123 } 124 putstring_to_uint_map125 void put(unsigned value, const char *key) 126 { 127 /* The low-level hash table structure returns NULL if key is not in the 128 * hash table. However, users of this map might want to store zero as a 129 * valid value in the table. Bias the value by +1 so that a 130 * user-specified zero is stored as 1. This enables ::get to tell the 131 * difference between a user-specified zero (returned as 1 by 132 * _mesa_hash_table_search) and the key not in the table (returned as 0 by 133 * _mesa_hash_table_search). 134 * 135 * The net effect is that we can't store UINT_MAX in the table. This is 136 * because UINT_MAX+1 = 0. 137 */ 138 assert(value != UINT_MAX); 139 char *dup_key = strdup(key); 140 141 struct hash_entry *entry = _mesa_hash_table_search(this->ht, dup_key); 142 if (entry) { 143 entry->data = (void *) (intptr_t) (value + 1); 144 } else { 145 _mesa_hash_table_insert(this->ht, dup_key, 146 (void *) (intptr_t) (value + 1)); 147 } 148 149 if (entry) 150 free(dup_key); 151 } 152 153 private: delete_keystring_to_uint_map154 static void delete_key(const void *key, void *data, void *closure) 155 { 156 (void) data; 157 (void) closure; 158 159 free((char *)key); 160 } 161 subtract_one_wrapperstring_to_uint_map162 static void subtract_one_wrapper(const void *key, void *data, void *closure) 163 { 164 struct string_map_iterate_wrapper_closure *wrapper = 165 (struct string_map_iterate_wrapper_closure *) closure; 166 unsigned value = (intptr_t) data; 167 168 value -= 1; 169 170 wrapper->callback((const char *) key, value, wrapper->closure); 171 } 172 173 struct hash_table *ht; 174 }; 175 176 #endif /* __cplusplus */ 177 #endif /* STRING_TO_UINT_MAP_H */ 178