2
* Copyright © 2008 Intel Corporation
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:
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
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
25
#ifndef STRING_TO_UINT_MAP_H
26
#define STRING_TO_UINT_MAP_H
30
#include "util/hash_table.h"
32
struct string_to_uint_map;
38
struct string_to_uint_map *
39
string_to_uint_map_ctor();
42
string_to_uint_map_dtor(struct string_to_uint_map *);
48
struct string_map_iterate_wrapper_closure {
49
void (*callback)(const char *key, unsigned value, void *closure);
54
* Map from a string (name) to an unsigned integer value
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.
60
struct string_to_uint_map {
64
this->ht = _mesa_hash_table_create(NULL, _mesa_hash_string,
65
_mesa_key_string_equal);
70
hash_table_call_foreach(this->ht, delete_key, NULL);
71
_mesa_hash_table_destroy(this->ht, NULL);
75
* Remove all mappings from this map.
79
hash_table_call_foreach(this->ht, delete_key, NULL);
80
_mesa_hash_table_clear(this->ht, NULL);
84
* Runs a passed callback for the hash
86
void iterate(void (*func)(const char *, unsigned, void *), void *closure)
88
struct string_map_iterate_wrapper_closure *wrapper;
90
wrapper = (struct string_map_iterate_wrapper_closure *)
91
malloc(sizeof(struct string_map_iterate_wrapper_closure));
95
wrapper->callback = func;
96
wrapper->closure = closure;
98
hash_table_call_foreach(this->ht, subtract_one_wrapper, wrapper);
103
* Get the value associated with a particular key
106
* If \c key is found in the map, \c true is returned. Otherwise \c false
110
* If \c key is not found in the table, \c value is not modified.
112
bool get(unsigned &value, const char *key)
114
hash_entry *entry = _mesa_hash_table_search(this->ht,
120
const intptr_t v = (intptr_t) entry->data;
121
value = (unsigned)(v - 1);
125
void put(unsigned value, const char *key)
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).
135
* The net effect is that we can't store UINT_MAX in the table. This is
136
* because UINT_MAX+1 = 0.
138
assert(value != UINT_MAX);
139
char *dup_key = strdup(key);
141
struct hash_entry *entry = _mesa_hash_table_search(this->ht, dup_key);
143
entry->data = (void *) (intptr_t) (value + 1);
145
_mesa_hash_table_insert(this->ht, dup_key,
146
(void *) (intptr_t) (value + 1));
154
static void delete_key(const void *key, void *data, void *closure)
162
static void subtract_one_wrapper(const void *key, void *data, void *closure)
164
struct string_map_iterate_wrapper_closure *wrapper =
165
(struct string_map_iterate_wrapper_closure *) closure;
166
unsigned value = (intptr_t) data;
170
wrapper->callback((const char *) key, value, wrapper->closure);
173
struct hash_table *ht;
176
#endif /* __cplusplus */
177
#endif /* STRING_TO_UINT_MAP_H */