1 /* $Id: hash.c,v 1.3 1999/10/13 18:42:50 brianp Exp $ */
4 * Mesa 3-D graphics library
7 * Copyright (C) 1999 Brian Paul All Rights Reserved.
9 * Permission is hereby granted, free of charge, to any person obtaining a
10 * copy of this software and associated documentation files (the "Software"),
11 * to deal in the Software without restriction, including without limitation
12 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
13 * and/or sell copies of the Software, and to permit persons to whom the
14 * Software is furnished to do so, subject to the following conditions:
16 * The above copyright notice and this permission notice shall be included
17 * in all copies or substantial portions of the Software.
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
22 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
23 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
24 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
37 #include "GL/xf86glx.h"
45 * Generic hash table. Only dependency is the GLuint datatype.
47 * This is used to implement display list and texture object lookup.
48 * NOTE: key=0 is illegal.
52 #define TABLE_SIZE 1024
57 struct HashEntry
*Next
;
61 struct HashEntry
*Table
[TABLE_SIZE
];
68 * Return pointer to a new, empty hash table.
70 struct HashTable
*NewHashTable(void)
72 return CALLOC_STRUCT(HashTable
);
78 * Delete a hash table.
80 void DeleteHashTable(struct HashTable
*table
)
84 for (i
=0;i
<TABLE_SIZE
;i
++) {
85 struct HashEntry
*entry
= table
->Table
[i
];
87 struct HashEntry
*next
= entry
->Next
;
98 * Lookup an entry in the hash table.
99 * Input: table - the hash table
101 * Return: user data pointer or NULL if key not in table
103 void *HashLookup(const struct HashTable
*table
, GLuint key
)
106 const struct HashEntry
*entry
;
111 pos
= key
& (TABLE_SIZE
-1);
112 entry
= table
->Table
[pos
];
114 if (entry
->Key
== key
) {
125 * Insert into the hash table. If an entry with this key already exists
126 * we'll replace the existing entry.
127 * Input: table - the hash table
128 * key - the key (not zero)
129 * data - pointer to user data
131 void HashInsert(struct HashTable
*table
, GLuint key
, void *data
)
133 /* search for existing entry with this key */
135 struct HashEntry
*entry
;
140 if (key
> table
->MaxKey
)
143 pos
= key
& (TABLE_SIZE
-1);
144 entry
= table
->Table
[pos
];
146 if (entry
->Key
== key
) {
147 /* replace entry's data */
154 /* alloc and insert new table entry */
155 entry
= MALLOC_STRUCT(HashEntry
);
158 entry
->Next
= table
->Table
[pos
];
159 table
->Table
[pos
] = entry
;
165 * Remove an entry from the hash table.
166 * Input: table - the hash table
167 * key - key of entry to remove
169 void HashRemove(struct HashTable
*table
, GLuint key
)
172 struct HashEntry
*entry
, *prev
;
177 pos
= key
& (TABLE_SIZE
-1);
179 entry
= table
->Table
[pos
];
181 if (entry
->Key
== key
) {
184 prev
->Next
= entry
->Next
;
187 table
->Table
[pos
] = entry
->Next
;
200 * Return the key of the "first" entry in the hash table.
201 * By calling this function until zero is returned we can get
202 * the keys of all entries in the table.
204 GLuint
HashFirstEntry(const struct HashTable
*table
)
208 for (pos
=0; pos
< TABLE_SIZE
; pos
++) {
209 if (table
->Table
[pos
])
210 return table
->Table
[pos
]->Key
;
218 * Dump contents of hash table for debugging.
220 void HashPrint(const struct HashTable
*table
)
224 for (i
=0;i
<TABLE_SIZE
;i
++) {
225 const struct HashEntry
*entry
= table
->Table
[i
];
227 printf("%u %p\n", entry
->Key
, entry
->Data
);
236 * Find a block of 'numKeys' adjacent unused hash keys.
237 * Input: table - the hash table
238 * numKeys - number of keys needed
239 * Return: startint key of free block or 0 if failure
241 GLuint
HashFindFreeKeyBlock(const struct HashTable
*table
, GLuint numKeys
)
243 GLuint maxKey
= ~((GLuint
) 0);
244 if (maxKey
- numKeys
> table
->MaxKey
) {
245 /* the quick solution */
246 return table
->MaxKey
+ 1;
249 /* the slow solution */
250 GLuint freeCount
= 0;
251 GLuint freeStart
= 0;
253 for (key
=0; key
!=maxKey
; key
++) {
254 if (HashLookup(table
, key
)) {
255 /* darn, this key is already in use */
260 /* this key not in use, check if we've found enough */
262 if (freeCount
== numKeys
) {
267 /* cannot allocate a block of numKeys consecutive keys */
274 #ifdef HASH_TEST_HARNESS
275 int main(int argc
, char *argv
[])
280 printf("&a = %p\n", &a
);
281 printf("&b = %p\n", &b
);
284 HashInsert(t
, 501, &a
);
285 HashInsert(t
, 10, &c
);
286 HashInsert(t
, 0xfffffff8, &b
);
288 printf("Find 501: %p\n", HashLookup(t
,501));
289 printf("Find 1313: %p\n", HashLookup(t
,1313));
290 printf("Find block of 100: %d\n", HashFindFreeKeyBlock(t
, 100));