st.c 12.5 KB
Newer Older
Alan Mishchenko committed
1 2 3 4 5 6 7 8 9 10
/*
 * Revision Control Information
 *
 * /projects/hsis/CVS/utilities/st/st.c,v
 * serdar
 * 1.1
 * 1993/07/29 01:00:13
 *
 */
#include <stdio.h>
Alan Mishchenko committed
11
#include <stdlib.h>
12

Alan Mishchenko committed
13
#include "st.h"
Alan Mishchenko committed
14

15 16 17
ABC_NAMESPACE_IMPL_START


Alan Mishchenko committed
18
#define ST_NUMCMP(x,y) ((x) != (y))
Alan Mishchenko committed
19 20 21
#define ST_NUMHASH(x,size) (ABC_ABS((long)x)%(size))
//#define ST_PTRHASH(x,size) ((int)((ABC_PTRUINT_T)(x)>>2)%size)  // 64-bit bug fix 9/17/2007
#define ST_PTRHASH(x,size) ((int)(((ABC_PTRUINT_T)(x)>>2)%size))
Alan Mishchenko committed
22 23 24 25 26 27 28 29 30 31
#define EQUAL(func, x, y) \
    ((((func) == st_numcmp) || ((func) == st_ptrcmp)) ?\
      (ST_NUMCMP((x),(y)) == 0) : ((*func)((x), (y)) == 0))


#define do_hash(key, table)\
    ((table->hash == st_ptrhash) ? ST_PTRHASH((key),(table)->num_bins) :\
     (table->hash == st_numhash) ? ST_NUMHASH((key), (table)->num_bins) :\
     (*table->hash)((key), (table)->num_bins))

32 33 34 35 36 37
static int rehash(st_table *table);

int st_numhash(const char*, int);
int st_ptrhash(const char*, int);
int st_numcmp(const char*, const char*);
int st_ptrcmp(const char*, const char*);
Alan Mishchenko committed
38 39

st_table *
40
st_init_table_with_params(st_compare_func_type compare, st_hash_func_type hash, int size, int density, double grow_factor, int reorder_flag)
Alan Mishchenko committed
41 42
{
    int i;
43
    st_table *newTable;
Alan Mishchenko committed
44

45 46
    newTable = ABC_ALLOC(st_table, 1);
    if (newTable == NULL) {
Alan Mishchenko committed
47
    return NULL;
Alan Mishchenko committed
48
    }
49 50 51 52 53 54
    newTable->compare = compare;
    newTable->hash = hash;
    newTable->num_entries = 0;
    newTable->max_density = density;
    newTable->grow_factor = grow_factor;
    newTable->reorder_flag = reorder_flag;
Alan Mishchenko committed
55 56 57
    if (size <= 0) {
    size = 1;
    }
58 59 60 61
    newTable->num_bins = size;
    newTable->bins = ABC_ALLOC(st_table_entry *, size);
    if (newTable->bins == NULL) {
    ABC_FREE(newTable);
Alan Mishchenko committed
62
    return NULL;
Alan Mishchenko committed
63 64
    }
    for(i = 0; i < size; i++) {
65
    newTable->bins[i] = 0;
Alan Mishchenko committed
66
    }
67
    return newTable;
Alan Mishchenko committed
68 69 70
}

st_table *
71
st_init_table(st_compare_func_type compare, st_hash_func_type hash)
Alan Mishchenko committed
72 73 74 75 76 77 78 79
{
    return st_init_table_with_params(compare, hash, ST_DEFAULT_INIT_TABLE_SIZE,
                     ST_DEFAULT_MAX_DENSITY,
                     ST_DEFAULT_GROW_FACTOR,
                     ST_DEFAULT_REORDER_FLAG);
}
                
void
80
st_free_table(st_table *table)
Alan Mishchenko committed
81 82 83 84 85 86
{
    register st_table_entry *ptr, *next;
    int i;

    for(i = 0; i < table->num_bins ; i++) {
    ptr = table->bins[i];
Alan Mishchenko committed
87
    while (ptr != NULL) {
Alan Mishchenko committed
88
        next = ptr->next;
Alan Mishchenko committed
89
        ABC_FREE(ptr);
Alan Mishchenko committed
90 91 92
        ptr = next;
    }
    }
Alan Mishchenko committed
93 94
    ABC_FREE(table->bins);
    ABC_FREE(table);
Alan Mishchenko committed
95 96 97
}

#define PTR_NOT_EQUAL(table, ptr, user_key)\
Alan Mishchenko committed
98
(ptr != NULL && !EQUAL(table->compare, user_key, (ptr)->key))
Alan Mishchenko committed
99 100 101 102 103 104 105

#define FIND_ENTRY(table, hash_val, key, ptr, last) \
    (last) = &(table)->bins[hash_val];\
    (ptr) = *(last);\
    while (PTR_NOT_EQUAL((table), (ptr), (key))) {\
    (last) = &(ptr)->next; (ptr) = *(last);\
    }\
Alan Mishchenko committed
106
    if ((ptr) != NULL && (table)->reorder_flag) {\
Alan Mishchenko committed
107 108 109 110 111 112
    *(last) = (ptr)->next;\
    (ptr)->next = (table)->bins[hash_val];\
    (table)->bins[hash_val] = (ptr);\
    }

int
113
st_lookup(st_table *table, register const char *key, char **value)
Alan Mishchenko committed
114 115 116 117 118 119 120 121
{
    int hash_val;
    register st_table_entry *ptr, **last;

    hash_val = do_hash(key, table);

    FIND_ENTRY(table, hash_val, key, ptr, last);
    
Alan Mishchenko committed
122
    if (ptr == NULL) {
Alan Mishchenko committed
123 124
    return 0;
    } else {
Alan Mishchenko committed
125
    if (value != NULL) {
Alan Mishchenko committed
126 127 128 129 130 131 132
        *value = ptr->record; 
    }
    return 1;
    }
}

int
133
st_lookup_int(st_table *table, register char *key, int *value)
Alan Mishchenko committed
134 135 136 137 138 139 140 141
{
    int hash_val;
    register st_table_entry *ptr, **last;

    hash_val = do_hash(key, table);

    FIND_ENTRY(table, hash_val, key, ptr, last);
    
Alan Mishchenko committed
142
    if (ptr == NULL) {
Alan Mishchenko committed
143 144
    return 0;
    } else {
Alan Mishchenko committed
145
    if (value != 0) {
Alan Mishchenko committed
146 147 148 149 150 151 152 153 154 155 156 157 158 159
        *value = (long) ptr->record;
    }
    return 1;
    }
}

/* This macro does not check if memory allocation fails. Use at you own risk */
#define ADD_DIRECT(table, key, value, hash_val, new)\
{\
    if (table->num_entries/table->num_bins >= table->max_density) {\
    rehash(table);\
    hash_val = do_hash(key,table);\
    }\
    \
Alan Mishchenko committed
160
    new = ABC_ALLOC(st_table_entry, 1);\
Alan Mishchenko committed
161 162 163 164 165 166 167 168 169
    \
    new->key = key;\
    new->record = value;\
    new->next = table->bins[hash_val];\
    table->bins[hash_val] = new;\
    table->num_entries++;\
}

int
170
st_insert(register st_table *table, register const char *key, char *value)
Alan Mishchenko committed
171 172
{
    int hash_val;
173
    st_table_entry *newEntry;
Alan Mishchenko committed
174 175 176 177 178 179
    register st_table_entry *ptr, **last;

    hash_val = do_hash(key, table);

    FIND_ENTRY(table, hash_val, key, ptr, last);

Alan Mishchenko committed
180
    if (ptr == NULL) {
Alan Mishchenko committed
181 182 183 184 185 186
    if (table->num_entries/table->num_bins >= table->max_density) {
        if (rehash(table) == ST_OUT_OF_MEM) {
        return ST_OUT_OF_MEM;
        }
        hash_val = do_hash(key, table);
    }
187 188
    newEntry = ABC_ALLOC(st_table_entry, 1);
    if (newEntry == NULL) {
Alan Mishchenko committed
189 190
        return ST_OUT_OF_MEM;
    }
191 192 193 194
    newEntry->key = key;
    newEntry->record = value;
    newEntry->next = table->bins[hash_val];
    table->bins[hash_val] = newEntry;
Alan Mishchenko committed
195 196 197 198 199 200 201 202 203
    table->num_entries++;
    return 0;
    } else {
    ptr->record = value;
    return 1;
    }
}

int
204
st_add_direct(st_table *table, char *key, char *value)
Alan Mishchenko committed
205 206
{
    int hash_val;
207
    st_table_entry *newEntry;
Alan Mishchenko committed
208 209 210 211 212 213 214 215
    
    hash_val = do_hash(key, table);
    if (table->num_entries / table->num_bins >= table->max_density) {
    if (rehash(table) == ST_OUT_OF_MEM) {
        return ST_OUT_OF_MEM;
    }
    }
    hash_val = do_hash(key, table);
216 217
    newEntry = ABC_ALLOC(st_table_entry, 1);
    if (newEntry == NULL) {
Alan Mishchenko committed
218 219
    return ST_OUT_OF_MEM;
    }
220 221 222 223
    newEntry->key = key;
    newEntry->record = value;
    newEntry->next = table->bins[hash_val];
    table->bins[hash_val] = newEntry;
Alan Mishchenko committed
224 225 226 227 228
    table->num_entries++;
    return 1;
}

int
229
st_find_or_add(st_table *table, char *key, char ***slot)
Alan Mishchenko committed
230 231
{
    int hash_val;
232
    st_table_entry *newEntry, *ptr, **last;
Alan Mishchenko committed
233 234 235 236 237

    hash_val = do_hash(key, table);

    FIND_ENTRY(table, hash_val, key, ptr, last);

Alan Mishchenko committed
238
    if (ptr == NULL) {
Alan Mishchenko committed
239 240 241 242 243 244
    if (table->num_entries / table->num_bins >= table->max_density) {
        if (rehash(table) == ST_OUT_OF_MEM) {
        return ST_OUT_OF_MEM;
        }
        hash_val = do_hash(key, table);
    }
245 246
    newEntry = ABC_ALLOC(st_table_entry, 1);
    if (newEntry == NULL) {
Alan Mishchenko committed
247 248
        return ST_OUT_OF_MEM;
    }
249 250 251 252
    newEntry->key = key;
    newEntry->record = (char *) 0;
    newEntry->next = table->bins[hash_val];
    table->bins[hash_val] = newEntry;
Alan Mishchenko committed
253
    table->num_entries++;
254
    if (slot != NULL) *slot = &newEntry->record;
Alan Mishchenko committed
255 256
    return 0;
    } else {
Alan Mishchenko committed
257
    if (slot != NULL) *slot = &ptr->record;
Alan Mishchenko committed
258 259 260 261 262
    return 1;
    }
}

int
263
st_find(st_table *table, char *key, char ***slot)
Alan Mishchenko committed
264 265 266 267 268 269 270 271
{
    int hash_val;
    st_table_entry *ptr, **last;

    hash_val = do_hash(key, table);

    FIND_ENTRY(table, hash_val, key, ptr, last);

Alan Mishchenko committed
272
    if (ptr == NULL) {
Alan Mishchenko committed
273 274
    return 0;
    } else {
Alan Mishchenko committed
275
    if (slot != NULL) {
Alan Mishchenko committed
276 277 278 279 280 281 282
        *slot = &ptr->record;
    }
    return 1;
    }
}

static int
283
rehash(register st_table *table)
Alan Mishchenko committed
284 285 286 287 288 289 290 291 292 293 294 295 296 297 298
{
    register st_table_entry *ptr, *next, **old_bins;
    int             i, old_num_bins, hash_val, old_num_entries;

    /* save old values */
    old_bins = table->bins;
    old_num_bins = table->num_bins;
    old_num_entries = table->num_entries;

    /* rehash */
    table->num_bins = (int)(table->grow_factor * old_num_bins);
    if (table->num_bins % 2 == 0) {
    table->num_bins += 1;
    }
    table->num_entries = 0;
Alan Mishchenko committed
299
    table->bins = ABC_ALLOC(st_table_entry *, table->num_bins);
Alan Mishchenko committed
300
    if (table->bins == NULL) {
Alan Mishchenko committed
301 302 303 304 305 306 307 308 309 310 311 312 313
    table->bins = old_bins;
    table->num_bins = old_num_bins;
    table->num_entries = old_num_entries;
    return ST_OUT_OF_MEM;
    }
    /* initialize */
    for (i = 0; i < table->num_bins; i++) {
    table->bins[i] = 0;
    }

    /* copy data over */
    for (i = 0; i < old_num_bins; i++) {
    ptr = old_bins[i];
Alan Mishchenko committed
314
    while (ptr != NULL) {
Alan Mishchenko committed
315 316 317 318 319 320 321 322
        next = ptr->next;
        hash_val = do_hash(ptr->key, table);
        ptr->next = table->bins[hash_val];
        table->bins[hash_val] = ptr;
        table->num_entries++;
        ptr = next;
    }
    }
Alan Mishchenko committed
323
    ABC_FREE(old_bins);
Alan Mishchenko committed
324 325 326 327 328

    return 1;
}

st_table *
329
st_copy(st_table *old_table)
Alan Mishchenko committed
330
{
331 332
    st_table *newEntry_table;
    st_table_entry *ptr, *newEntryptr, *next, *newEntry;
Alan Mishchenko committed
333 334
    int i, j, num_bins = old_table->num_bins;

335 336
    newEntry_table = ABC_ALLOC(st_table, 1);
    if (newEntry_table == NULL) {
Alan Mishchenko committed
337
    return NULL;
Alan Mishchenko committed
338 339
    }
    
340 341 342 343
    *newEntry_table = *old_table;
    newEntry_table->bins = ABC_ALLOC(st_table_entry *, num_bins);
    if (newEntry_table->bins == NULL) {
    ABC_FREE(newEntry_table);
Alan Mishchenko committed
344
    return NULL;
Alan Mishchenko committed
345 346
    }
    for(i = 0; i < num_bins ; i++) {
347
    newEntry_table->bins[i] = NULL;
Alan Mishchenko committed
348
    ptr = old_table->bins[i];
Alan Mishchenko committed
349
    while (ptr != NULL) {
350 351
        newEntry = ABC_ALLOC(st_table_entry, 1);
        if (newEntry == NULL) {
Alan Mishchenko committed
352
        for (j = 0; j <= i; j++) {
353 354 355 356 357
            newEntryptr = newEntry_table->bins[j];
            while (newEntryptr != NULL) {
            next = newEntryptr->next;
            ABC_FREE(newEntryptr);
            newEntryptr = next;
Alan Mishchenko committed
358 359
            }
        }
360 361
        ABC_FREE(newEntry_table->bins);
        ABC_FREE(newEntry_table);
Alan Mishchenko committed
362
        return NULL;
Alan Mishchenko committed
363
        }
364 365 366
        *newEntry = *ptr;
        newEntry->next = newEntry_table->bins[i];
        newEntry_table->bins[i] = newEntry;
Alan Mishchenko committed
367 368 369
        ptr = ptr->next;
    }
    }
370
    return newEntry_table;
Alan Mishchenko committed
371 372 373
}

int
374
st_delete(register st_table *table, register const char **keyp, char **value)
Alan Mishchenko committed
375 376
{
    int hash_val;
377
    const char *key = *keyp;
Alan Mishchenko committed
378 379 380 381 382 383
    register st_table_entry *ptr, **last;

    hash_val = do_hash(key, table);

    FIND_ENTRY(table, hash_val, key, ptr ,last);
    
Alan Mishchenko committed
384
    if (ptr == NULL) {
Alan Mishchenko committed
385 386 387 388
    return 0;
    }

    *last = ptr->next;
Alan Mishchenko committed
389
    if (value != NULL) *value = ptr->record;
Alan Mishchenko committed
390
    *keyp = ptr->key;
Alan Mishchenko committed
391
    ABC_FREE(ptr);
Alan Mishchenko committed
392 393 394 395 396
    table->num_entries--;
    return 1;
}

int
397
st_delete_int(register st_table *table, register long *keyp, char **value)
Alan Mishchenko committed
398 399 400 401 402 403 404 405 406
{
    int hash_val;
    char *key = (char *) *keyp;
    register st_table_entry *ptr, **last;

    hash_val = do_hash(key, table);

    FIND_ENTRY(table, hash_val, key, ptr ,last);

Alan Mishchenko committed
407
    if (ptr == NULL) {
Alan Mishchenko committed
408 409 410 411
        return 0;
    }

    *last = ptr->next;
Alan Mishchenko committed
412
    if (value != NULL) *value = ptr->record;
Alan Mishchenko committed
413
    *keyp = (long) ptr->key;
Alan Mishchenko committed
414
    ABC_FREE(ptr);
Alan Mishchenko committed
415 416 417 418 419
    table->num_entries--;
    return 1;
}

int
420
st_foreach(st_table *table, enum st_retval (*func)(const char *, char *, char *), char *arg)
Alan Mishchenko committed
421 422 423 424 425 426 427
{
    st_table_entry *ptr, **last;
    enum st_retval retval;
    int i;

    for(i = 0; i < table->num_bins; i++) {
    last = &table->bins[i]; ptr = *last;
Alan Mishchenko committed
428
    while (ptr != NULL) {
Alan Mishchenko committed
429 430 431 432 433 434 435 436 437 438
        retval = (*func)(ptr->key, ptr->record, arg);
        switch (retval) {
        case ST_CONTINUE:
        last = &ptr->next; ptr = *last;
        break;
        case ST_STOP:
        return 0;
        case ST_DELETE:
        *last = ptr->next;
        table->num_entries--;    /* cstevens@ic */
Alan Mishchenko committed
439
        ABC_FREE(ptr);
Alan Mishchenko committed
440 441 442 443 444 445 446 447
        ptr = *last;
        }
    }
    }
    return 1;
}

int
448
st_strhash(const char *string, int modulus)
Alan Mishchenko committed
449 450 451 452 453 454 455 456 457 458 459 460
{
    register int val = 0;
    register int c;
    
    while ((c = *string++) != '\0') {
    val = val*997 + c;
    }

    return ((val < 0) ? -val : val)%modulus;
}

int
461
st_numhash(const char *x, int size)
Alan Mishchenko committed
462 463 464 465 466
{
    return ST_NUMHASH(x, size);
}

int
467
st_ptrhash(const char *x, int size)
Alan Mishchenko committed
468 469 470 471 472
{
    return ST_PTRHASH(x, size);
}

int
473
st_numcmp(const char *x, const char *y)
Alan Mishchenko committed
474 475 476 477 478
{
    return ST_NUMCMP(x, y);
}

int
479
st_ptrcmp(const char *x, const char *y)
Alan Mishchenko committed
480 481 482
{
    return ST_NUMCMP(x, y);
}
483

Alan Mishchenko committed
484
st_generator *
485
st_init_gen(st_table *table)
Alan Mishchenko committed
486 487 488
{
    st_generator *gen;

Alan Mishchenko committed
489
    gen = ABC_ALLOC(st_generator, 1);
Alan Mishchenko committed
490 491
    if (gen == NULL) {
    return NULL;
Alan Mishchenko committed
492 493
    }
    gen->table = table;
Alan Mishchenko committed
494
    gen->entry = NULL;
Alan Mishchenko committed
495 496 497 498 499 500
    gen->index = 0;
    return gen;
}


int 
501
st_gen(st_generator *gen, const char **key_p, char **value_p)
Alan Mishchenko committed
502 503 504
{
    register int i;

Alan Mishchenko committed
505
    if (gen->entry == NULL) {
Alan Mishchenko committed
506 507
    /* try to find next entry */
    for(i = gen->index; i < gen->table->num_bins; i++) {
Alan Mishchenko committed
508
        if (gen->table->bins[i] != NULL) {
Alan Mishchenko committed
509 510 511 512 513
        gen->index = i+1;
        gen->entry = gen->table->bins[i];
        break;
        }
    }
Alan Mishchenko committed
514
    if (gen->entry == NULL) {
Alan Mishchenko committed
515 516 517 518 519 520 521 522 523 524 525 526 527
        return 0;        /* that's all folks ! */
    }
    }
    *key_p = gen->entry->key;
    if (value_p != 0) {
    *value_p = gen->entry->record;
    }
    gen->entry = gen->entry->next;
    return 1;
}


int 
528
st_gen_int(st_generator *gen, const char **key_p, long *value_p)
Alan Mishchenko committed
529 530 531
{
    register int i;

Alan Mishchenko committed
532
    if (gen->entry == NULL) {
Alan Mishchenko committed
533 534
    /* try to find next entry */
    for(i = gen->index; i < gen->table->num_bins; i++) {
Alan Mishchenko committed
535
        if (gen->table->bins[i] != NULL) {
Alan Mishchenko committed
536 537 538 539 540
        gen->index = i+1;
        gen->entry = gen->table->bins[i];
        break;
        }
    }
Alan Mishchenko committed
541
    if (gen->entry == NULL) {
Alan Mishchenko committed
542 543 544 545
        return 0;        /* that's all folks ! */
    }
    }
    *key_p = gen->entry->key;
Alan Mishchenko committed
546
    if (value_p != 0) {
Alan Mishchenko committed
547 548 549 550 551 552 553 554
       *value_p = (long) gen->entry->record;
    }
    gen->entry = gen->entry->next;
    return 1;
}


void
555
st_free_gen(st_generator *gen)
Alan Mishchenko committed
556
{
Alan Mishchenko committed
557
    ABC_FREE(gen);
Alan Mishchenko committed
558
}
559 560 561

ABC_NAMESPACE_IMPL_END