r600 : fix draw_prim bug: vertex fetcher setting.
[mesa.git] / src / mesa / shader / hash_table.c
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
21 * DEALINGS IN THE SOFTWARE.
22 */
23
24 /**
25 * \file hash_table.c
26 * \brief Implementation of a generic, opaque hash table data type.
27 *
28 * \author Ian Romanick <ian.d.romanick@intel.com>
29 */
30 #include <stdlib.h>
31 #include <string.h>
32
33 #include <assert.h>
34
35 #include "main/imports.h"
36 #include "main/simple_list.h"
37 #include "hash_table.h"
38
39 struct node {
40 struct node *next;
41 struct node *prev;
42 };
43
44 struct hash_table {
45 hash_func_t hash;
46 hash_compare_func_t compare;
47
48 unsigned num_buckets;
49 struct node buckets[1];
50 };
51
52
53 struct hash_node {
54 struct node link;
55 const void *key;
56 void *data;
57 };
58
59
60 struct hash_table *
61 hash_table_ctor(unsigned num_buckets, hash_func_t hash,
62 hash_compare_func_t compare)
63 {
64 struct hash_table *ht;
65 unsigned i;
66
67
68 if (num_buckets < 16) {
69 num_buckets = 16;
70 }
71
72 ht = _mesa_malloc(sizeof(*ht) + ((num_buckets - 1)
73 * sizeof(ht->buckets[0])));
74 if (ht != NULL) {
75 ht->hash = hash;
76 ht->compare = compare;
77 ht->num_buckets = num_buckets;
78
79 for (i = 0; i < num_buckets; i++) {
80 make_empty_list(& ht->buckets[i]);
81 }
82 }
83
84 return ht;
85 }
86
87
88 void
89 hash_table_dtor(struct hash_table *ht)
90 {
91 hash_table_clear(ht);
92 _mesa_free(ht);
93 }
94
95
96 void
97 hash_table_clear(struct hash_table *ht)
98 {
99 struct node *node;
100 struct node *temp;
101 unsigned i;
102
103
104 for (i = 0; i < ht->num_buckets; i++) {
105 foreach_s(node, temp, & ht->buckets[i]) {
106 remove_from_list(node);
107 _mesa_free(node);
108 }
109
110 assert(is_empty_list(& ht->buckets[i]));
111 }
112 }
113
114
115 void *
116 hash_table_find(struct hash_table *ht, const void *key)
117 {
118 const unsigned hash_value = (*ht->hash)(key);
119 const unsigned bucket = hash_value % ht->num_buckets;
120 struct node *node;
121
122 foreach(node, & ht->buckets[bucket]) {
123 struct hash_node *hn = (struct hash_node *) node;
124
125 if ((*ht->compare)(hn->key, key) == 0) {
126 return hn->data;
127 }
128 }
129
130 return NULL;
131 }
132
133
134 void
135 hash_table_insert(struct hash_table *ht, void *data, const void *key)
136 {
137 const unsigned hash_value = (*ht->hash)(key);
138 const unsigned bucket = hash_value % ht->num_buckets;
139 struct hash_node *node;
140
141 node = _mesa_calloc(sizeof(*node));
142
143 node->data = data;
144 node->key = key;
145
146 insert_at_head(& ht->buckets[bucket], & node->link);
147 }
148
149
150 unsigned
151 hash_table_string_hash(const void *key)
152 {
153 const char *str = (const char *) key;
154 unsigned hash = 5381;
155
156
157 while (*str != '\0') {
158 hash = (hash * 33) + *str;
159 str++;
160 }
161
162 return hash;
163 }