1 /* $Id: hash.c,v 1.5 2000/01/04 08:14:36 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.
40 * This is used to implement display list and texture object lookup.
41 * NOTE: key=0 is illegal.
45 #define TABLE_SIZE 1024
50 struct HashEntry
*Next
;
54 struct HashEntry
*Table
[TABLE_SIZE
];
61 * Return pointer to a new, empty hash table.
63 struct HashTable
*NewHashTable(void)
65 return CALLOC_STRUCT(HashTable
);
71 * Delete a hash table.
73 void DeleteHashTable(struct HashTable
*table
)
77 for (i
=0;i
<TABLE_SIZE
;i
++) {
78 struct HashEntry
*entry
= table
->Table
[i
];
80 struct HashEntry
*next
= entry
->Next
;
91 * Lookup an entry in the hash table.
92 * Input: table - the hash table
94 * Return: user data pointer or NULL if key not in table
96 void *HashLookup(const struct HashTable
*table
, GLuint key
)
99 const struct HashEntry
*entry
;
104 pos
= key
& (TABLE_SIZE
-1);
105 entry
= table
->Table
[pos
];
107 if (entry
->Key
== key
) {
118 * Insert into the hash table. If an entry with this key already exists
119 * we'll replace the existing entry.
120 * Input: table - the hash table
121 * key - the key (not zero)
122 * data - pointer to user data
124 void HashInsert(struct HashTable
*table
, GLuint key
, void *data
)
126 /* search for existing entry with this key */
128 struct HashEntry
*entry
;
133 if (key
> table
->MaxKey
)
136 pos
= key
& (TABLE_SIZE
-1);
137 entry
= table
->Table
[pos
];
139 if (entry
->Key
== key
) {
140 /* replace entry's data */
147 /* alloc and insert new table entry */
148 entry
= MALLOC_STRUCT(HashEntry
);
151 entry
->Next
= table
->Table
[pos
];
152 table
->Table
[pos
] = entry
;
158 * Remove an entry from the hash table.
159 * Input: table - the hash table
160 * key - key of entry to remove
162 void HashRemove(struct HashTable
*table
, GLuint key
)
165 struct HashEntry
*entry
, *prev
;
170 pos
= key
& (TABLE_SIZE
-1);
172 entry
= table
->Table
[pos
];
174 if (entry
->Key
== key
) {
177 prev
->Next
= entry
->Next
;
180 table
->Table
[pos
] = entry
->Next
;
193 * Return the key of the "first" entry in the hash table.
194 * By calling this function until zero is returned we can get
195 * the keys of all entries in the table.
197 GLuint
HashFirstEntry(const struct HashTable
*table
)
201 for (pos
=0; pos
< TABLE_SIZE
; pos
++) {
202 if (table
->Table
[pos
])
203 return table
->Table
[pos
]->Key
;
211 * Dump contents of hash table for debugging.
213 void HashPrint(const struct HashTable
*table
)
217 for (i
=0;i
<TABLE_SIZE
;i
++) {
218 const struct HashEntry
*entry
= table
->Table
[i
];
220 printf("%u %p\n", entry
->Key
, entry
->Data
);
229 * Find a block of 'numKeys' adjacent unused hash keys.
230 * Input: table - the hash table
231 * numKeys - number of keys needed
232 * Return: starting key of free block or 0 if failure
234 GLuint
HashFindFreeKeyBlock(const struct HashTable
*table
, GLuint numKeys
)
236 GLuint maxKey
= ~((GLuint
) 0);
237 if (maxKey
- numKeys
> table
->MaxKey
) {
238 /* the quick solution */
239 return table
->MaxKey
+ 1;
242 /* the slow solution */
243 GLuint freeCount
= 0;
244 GLuint freeStart
= 1;
246 for (key
=1; key
!=maxKey
; key
++) {
247 if (HashLookup(table
, key
)) {
248 /* darn, this key is already in use */
253 /* this key not in use, check if we've found enough */
255 if (freeCount
== numKeys
) {
260 /* cannot allocate a block of numKeys consecutive keys */
267 #ifdef HASH_TEST_HARNESS
268 int main(int argc
, char *argv
[])
273 printf("&a = %p\n", &a
);
274 printf("&b = %p\n", &b
);
277 HashInsert(t
, 501, &a
);
278 HashInsert(t
, 10, &c
);
279 HashInsert(t
, 0xfffffff8, &b
);
281 printf("Find 501: %p\n", HashLookup(t
,501));
282 printf("Find 1313: %p\n", HashLookup(t
,1313));
283 printf("Find block of 100: %d\n", HashFindFreeKeyBlock(t
, 100));