1 /***************************************************************************/
5 /* Hashing functions (body). */
7 /***************************************************************************/
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 */
39 /*************************************************************************/
43 #include FT_INTERNAL_HASH_H
44 #include FT_INTERNAL_MEMORY_H
47 #define INITIAL_HT_SIZE 241
51 hash_str_lookup( FT_Hashkey
* key
)
53 const char* kp
= key
->str
;
57 /* Mocklisp hash function. */
59 res
= ( res
<< 5 ) - res
+ (FT_ULong
)*kp
++;
66 hash_num_lookup( FT_Hashkey
* key
)
68 FT_ULong num
= (FT_ULong
)key
->num
;
72 /* Mocklisp hash function. */
74 res
= ( res
<< 5 ) - res
+ ( ( num
>> 8 ) & 0xFF );
75 res
= ( res
<< 5 ) - res
+ ( ( num
>> 16 ) & 0xFF );
76 res
= ( res
<< 5 ) - res
+ ( ( num
>> 24 ) & 0xFF );
83 hash_str_compare( FT_Hashkey
* a
,
86 if ( a
->str
[0] == b
->str
[0] &&
87 ft_strcmp( a
->str
, b
->str
) == 0 )
95 hash_num_compare( FT_Hashkey
* a
,
98 if ( a
->num
== b
->num
)
106 hash_bucket( FT_Hashkey key
,
110 FT_Hashnode
* bp
= hash
->table
;
114 res
= (hash
->lookup
)( &key
);
116 ndp
= bp
+ ( res
% hash
->size
);
119 if ( (hash
->compare
)( &(*ndp
)->key
, &key
) )
124 ndp
= bp
+ ( hash
->size
- 1 );
132 hash_rehash( FT_Hash hash
,
135 FT_Hashnode
* obp
= hash
->table
;
139 FT_UInt i
, sz
= hash
->size
;
140 FT_Error error
= FT_Err_Ok
;
144 hash
->limit
= hash
->size
/ 3;
146 if ( FT_NEW_ARRAY( hash
->table
, hash
->size
) )
149 for ( i
= 0, bp
= obp
; i
< sz
; i
++, bp
++ )
153 nbp
= hash_bucket( (*bp
)->key
, hash
);
166 hash_init( FT_Hash hash
,
170 FT_UInt sz
= INITIAL_HT_SIZE
;
175 hash
->limit
= sz
/ 3;
180 hash
->lookup
= hash_num_lookup
;
181 hash
->compare
= hash_num_compare
;
185 hash
->lookup
= hash_str_lookup
;
186 hash
->compare
= hash_str_compare
;
189 FT_MEM_NEW_ARRAY( hash
->table
, sz
);
196 ft_hash_str_init( FT_Hash hash
,
199 return hash_init( hash
, 0, memory
);
204 ft_hash_num_init( FT_Hash hash
,
207 return hash_init( hash
, 1, memory
);
212 ft_hash_str_free( FT_Hash hash
,
217 FT_UInt sz
= hash
->size
;
218 FT_Hashnode
* bp
= hash
->table
;
222 for ( i
= 0; i
< sz
; i
++, bp
++ )
225 FT_FREE( hash
->table
);
230 /* `ft_hash_num_free' is the same as `ft_hash_str_free' */
234 hash_insert( FT_Hashkey key
,
240 FT_Hashnode
* bp
= hash_bucket( key
, hash
);
241 FT_Error error
= FT_Err_Ok
;
254 if ( hash
->used
>= hash
->limit
)
256 error
= hash_rehash( hash
, memory
);
272 ft_hash_str_insert( const char* key
,
282 return hash_insert( hk
, data
, hash
, memory
);
287 ft_hash_num_insert( FT_Int num
,
297 return hash_insert( hk
, data
, hash
, memory
);
302 hash_lookup( FT_Hashkey key
,
305 FT_Hashnode
* np
= hash_bucket( key
, hash
);
308 return (*np
) ? &(*np
)->data
314 ft_hash_str_lookup( const char* key
,
322 return hash_lookup( hk
, hash
);
327 ft_hash_num_lookup( FT_Int num
,
335 return hash_lookup( hk
, hash
);