Branch
Hash :
81b81feb
Author :
Date :
2022-01-14T18:22:23
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338
/****************************************************************************
*
* fthash.c
*
* Hashing functions (body).
*
*/
/*
* Copyright 2000 Computing Research Labs, New Mexico State University
* Copyright 2001-2015
* Francesco Zappa Nardelli
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE COMPUTING RESEARCH LAB OR NEW MEXICO STATE UNIVERSITY BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
* OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
* THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
/**************************************************************************
*
* This file is based on code from bdf.c,v 1.22 2000/03/16 20:08:50
*
* taken from Mark Leisher's xmbdfed package
*
*/
#include <freetype/internal/fthash.h>
#include <freetype/internal/ftmemory.h>
#define INITIAL_HT_SIZE 241
static FT_ULong
hash_str_lookup( FT_Hashkey* key )
{
const char* kp = key->str;
FT_ULong res = 0;
/* Mocklisp hash function. */
while ( *kp )
res = ( res << 5 ) - res + (FT_ULong)*kp++;
return res;
}
static FT_ULong
hash_num_lookup( FT_Hashkey* key )
{
FT_ULong num = (FT_ULong)key->num;
FT_ULong res;
/* Mocklisp hash function. */
res = num & 0xFF;
res = ( res << 5 ) - res + ( ( num >> 8 ) & 0xFF );
res = ( res << 5 ) - res + ( ( num >> 16 ) & 0xFF );
res = ( res << 5 ) - res + ( ( num >> 24 ) & 0xFF );
return res;
}
static FT_Bool
hash_str_compare( FT_Hashkey* a,
FT_Hashkey* b )
{
if ( a->str[0] == b->str[0] &&
ft_strcmp( a->str, b->str ) == 0 )
return 1;
return 0;
}
static FT_Bool
hash_num_compare( FT_Hashkey* a,
FT_Hashkey* b )
{
if ( a->num == b->num )
return 1;
return 0;
}
static FT_Hashnode*
hash_bucket( FT_Hashkey key,
FT_Hash hash )
{
FT_ULong res = 0;
FT_Hashnode* bp = hash->table;
FT_Hashnode* ndp;
res = (hash->lookup)( &key );
ndp = bp + ( res % hash->size );
while ( *ndp )
{
if ( (hash->compare)( &(*ndp)->key, &key ) )
break;
ndp--;
if ( ndp < bp )
ndp = bp + ( hash->size - 1 );
}
return ndp;
}
static FT_Error
hash_rehash( FT_Hash hash,
FT_Memory memory )
{
FT_Hashnode* obp = hash->table;
FT_Hashnode* bp;
FT_Hashnode* nbp;
FT_UInt i, sz = hash->size;
FT_Error error = FT_Err_Ok;
hash->size <<= 1;
hash->limit = hash->size / 3;
if ( FT_NEW_ARRAY( hash->table, hash->size ) )
goto Exit;
for ( i = 0, bp = obp; i < sz; i++, bp++ )
{
if ( *bp )
{
nbp = hash_bucket( (*bp)->key, hash );
*nbp = *bp;
}
}
FT_FREE( obp );
Exit:
return error;
}
static FT_Error
hash_init( FT_Hash hash,
FT_Bool is_num,
FT_Memory memory )
{
FT_UInt sz = INITIAL_HT_SIZE;
FT_Error error;
hash->size = sz;
hash->limit = sz / 3;
hash->used = 0;
if ( is_num )
{
hash->lookup = hash_num_lookup;
hash->compare = hash_num_compare;
}
else
{
hash->lookup = hash_str_lookup;
hash->compare = hash_str_compare;
}
FT_MEM_NEW_ARRAY( hash->table, sz );
return error;
}
FT_Error
ft_hash_str_init( FT_Hash hash,
FT_Memory memory )
{
return hash_init( hash, 0, memory );
}
FT_Error
ft_hash_num_init( FT_Hash hash,
FT_Memory memory )
{
return hash_init( hash, 1, memory );
}
void
ft_hash_str_free( FT_Hash hash,
FT_Memory memory )
{
if ( hash )
{
FT_UInt sz = hash->size;
FT_Hashnode* bp = hash->table;
FT_UInt i;
for ( i = 0; i < sz; i++, bp++ )
FT_FREE( *bp );
FT_FREE( hash->table );
}
}
/* `ft_hash_num_free' is the same as `ft_hash_str_free' */
static FT_Error
hash_insert( FT_Hashkey key,
size_t data,
FT_Hash hash,
FT_Memory memory )
{
FT_Hashnode nn;
FT_Hashnode* bp = hash_bucket( key, hash );
FT_Error error = FT_Err_Ok;
nn = *bp;
if ( !nn )
{
if ( FT_QNEW( nn ) )
goto Exit;
*bp = nn;
nn->key = key;
nn->data = data;
if ( hash->used >= hash->limit )
{
error = hash_rehash( hash, memory );
if ( error )
goto Exit;
}
hash->used++;
}
else
nn->data = data;
Exit:
return error;
}
FT_Error
ft_hash_str_insert( const char* key,
size_t data,
FT_Hash hash,
FT_Memory memory )
{
FT_Hashkey hk;
hk.str = key;
return hash_insert( hk, data, hash, memory );
}
FT_Error
ft_hash_num_insert( FT_Int num,
size_t data,
FT_Hash hash,
FT_Memory memory )
{
FT_Hashkey hk;
hk.num = num;
return hash_insert( hk, data, hash, memory );
}
static size_t*
hash_lookup( FT_Hashkey key,
FT_Hash hash )
{
FT_Hashnode* np = hash_bucket( key, hash );
return (*np) ? &(*np)->data
: NULL;
}
size_t*
ft_hash_str_lookup( const char* key,
FT_Hash hash )
{
FT_Hashkey hk;
hk.str = key;
return hash_lookup( hk, hash );
}
size_t*
ft_hash_num_lookup( FT_Int num,
FT_Hash hash )
{
FT_Hashkey hk;
hk.num = num;
return hash_lookup( hk, hash );
}
/* END */