1 /****************************************************************************
5 * Hashing functions (body).
10 * Copyright 2000 Computing Research Labs, New Mexico State University
12 * Francesco Zappa Nardelli
14 * Permission is hereby granted, free of charge, to any person obtaining a
15 * copy of this software and associated documentation files (the "Software"),
16 * to deal in the Software without restriction, including without limitation
17 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
18 * and/or sell copies of the Software, and to permit persons to whom the
19 * Software is furnished to do so, subject to the following conditions:
21 * The above copyright notice and this permission notice shall be included in
22 * all copies or substantial portions of the Software.
24 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
25 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
26 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
27 * THE COMPUTING RESEARCH LAB OR NEW MEXICO STATE UNIVERSITY BE LIABLE FOR ANY
28 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
29 * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
30 * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
33 /**************************************************************************
35 * This file is based on code from bdf.c,v 1.22 2000/03/16 20:08:50
37 * taken from Mark Leisher's xmbdfed package
42 #include <freetype/internal/fthash.h>
43 #include <freetype/internal/ftmemory.h>
46 #define INITIAL_HT_SIZE 241
50 hash_str_lookup( FT_Hashkey* key )
52 const char* kp = key->str;
56 /* Mocklisp hash function. */
58 res = ( res << 5 ) - res + (FT_ULong)*kp++;
65 hash_num_lookup( FT_Hashkey* key )
67 FT_ULong num = (FT_ULong)key->num;
71 /* Mocklisp hash function. */
73 res = ( res << 5 ) - res + ( ( num >> 8 ) & 0xFF );
74 res = ( res << 5 ) - res + ( ( num >> 16 ) & 0xFF );
75 res = ( res << 5 ) - res + ( ( num >> 24 ) & 0xFF );
82 hash_str_compare( FT_Hashkey* a,
85 if ( a->str[0] == b->str[0] &&
86 ft_strcmp( a->str, b->str ) == 0 )
94 hash_num_compare( FT_Hashkey* a,
97 if ( a->num == b->num )
105 hash_bucket( FT_Hashkey key,
109 FT_Hashnode* bp = hash->table;
113 res = (hash->lookup)( &key );
115 ndp = bp + ( res % hash->size );
118 if ( (hash->compare)( &(*ndp)->key, &key ) )
123 ndp = bp + ( hash->size - 1 );
131 hash_rehash( FT_Hash hash,
134 FT_Hashnode* obp = hash->table;
138 FT_UInt i, sz = hash->size;
139 FT_Error error = FT_Err_Ok;
143 hash->limit = hash->size / 3;
145 if ( FT_NEW_ARRAY( hash->table, hash->size ) )
148 for ( i = 0, bp = obp; i < sz; i++, bp++ )
152 nbp = hash_bucket( (*bp)->key, hash );
165 hash_init( FT_Hash hash,
169 FT_UInt sz = INITIAL_HT_SIZE;
174 hash->limit = sz / 3;
179 hash->lookup = hash_num_lookup;
180 hash->compare = hash_num_compare;
184 hash->lookup = hash_str_lookup;
185 hash->compare = hash_str_compare;
188 FT_MEM_NEW_ARRAY( hash->table, sz );
195 ft_hash_str_init( FT_Hash hash,
198 return hash_init( hash, 0, memory );
203 ft_hash_num_init( FT_Hash hash,
206 return hash_init( hash, 1, memory );
211 ft_hash_str_free( FT_Hash hash,
216 FT_UInt sz = hash->size;
217 FT_Hashnode* bp = hash->table;
221 for ( i = 0; i < sz; i++, bp++ )
224 FT_FREE( hash->table );
229 /* `ft_hash_num_free' is the same as `ft_hash_str_free' */
233 hash_insert( FT_Hashkey key,
239 FT_Hashnode* bp = hash_bucket( key, hash );
240 FT_Error error = FT_Err_Ok;
253 if ( hash->used >= hash->limit )
255 error = hash_rehash( hash, memory );
271 ft_hash_str_insert( const char* key,
281 return hash_insert( hk, data, hash, memory );
286 ft_hash_num_insert( FT_Int num,
296 return hash_insert( hk, data, hash, memory );
301 hash_lookup( FT_Hashkey key,
304 FT_Hashnode* np = hash_bucket( key, hash );
307 return (*np) ? &(*np)->data
313 ft_hash_str_lookup( const char* key,
321 return hash_lookup( hk, hash );
326 ft_hash_num_lookup( FT_Int num,
334 return hash_lookup( hk, hash );