st.c 12.6 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
#include <string.h>
13

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

16 17 18
ABC_NAMESPACE_IMPL_START


19 20 21 22
#define st__NUMCMP(x,y) ((x) != (y))
#define st__NUMHASH(x,size) (Abc_AbsInt((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
23
#define EQUAL(func, x, y) \
24 25
    ((((func) == st__numcmp) || ((func) == st__ptrcmp)) ?\
      (st__NUMCMP((x),(y)) == 0) : ((*func)((x), (y)) == 0))
Alan Mishchenko committed
26 27 28


#define do_hash(key, table)\
29 30
    ((table->hash == st__ptrhash) ? st__PTRHASH((key),(table)->num_bins) :\
     (table->hash == st__numhash) ? st__NUMHASH((key), (table)->num_bins) :\
Alan Mishchenko committed
31 32
     (*table->hash)((key), (table)->num_bins))

33
static int rehash( st__table *table);
34

35 36 37 38
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
39

40 41
 st__table *
 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
42 43
{
    int i;
44
    st__table *newTable;
Alan Mishchenko committed
45

46
    newTable = ABC_ALLOC( st__table, 1);
47
    if (newTable == NULL) {
Alan Mishchenko committed
48
    return NULL;
Alan Mishchenko committed
49
    }
50 51 52 53 54 55
    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
56 57 58
    if (size <= 0) {
    size = 1;
    }
59
    newTable->num_bins = size;
60
    newTable->bins = ABC_ALLOC( st__table_entry *, size);
61 62
    if (newTable->bins == NULL) {
    ABC_FREE(newTable);
Alan Mishchenko committed
63
    return NULL;
Alan Mishchenko committed
64 65
    }
    for(i = 0; i < size; i++) {
66
    newTable->bins[i] = 0;
Alan Mishchenko committed
67
    }
68
    return newTable;
Alan Mishchenko committed
69 70
}

71 72
 st__table *
 st__init_table( st__compare_func_type compare, st__hash_func_type hash)
Alan Mishchenko committed
73
{
74 75 76 77
    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);
Alan Mishchenko committed
78 79 80
}
                
void
81
 st__free_table( st__table *table)
Alan Mishchenko committed
82
{
83
    st__table_entry *ptr, *next;
Alan Mishchenko committed
84 85 86 87
    int i;

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

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

#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
107
    if ((ptr) != NULL && (table)->reorder_flag) {\
Alan Mishchenko committed
108 109 110 111 112 113
    *(last) = (ptr)->next;\
    (ptr)->next = (table)->bins[hash_val];\
    (table)->bins[hash_val] = (ptr);\
    }

int
114
 st__lookup( st__table *table, const char *key, char **value)
Alan Mishchenko committed
115 116
{
    int hash_val;
117
    st__table_entry *ptr, **last;
Alan Mishchenko committed
118 119 120 121 122

    hash_val = do_hash(key, table);

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

int
134
 st__lookup_int( st__table *table, char *key, int *value)
Alan Mishchenko committed
135 136
{
    int hash_val;
137
    st__table_entry *ptr, **last;
Alan Mishchenko committed
138 139 140 141 142

    hash_val = do_hash(key, table);

    FIND_ENTRY(table, hash_val, key, ptr, last);
    
Alan Mishchenko committed
143
    if (ptr == NULL) {
Alan Mishchenko committed
144 145
    return 0;
    } else {
Alan Mishchenko committed
146
    if (value != 0) {
Alan Mishchenko committed
147 148 149 150 151 152 153 154 155 156 157 158 159 160
        *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);\
    }\
    \
161
    new = ABC_ALLOC( st__table_entry, 1);\
Alan Mishchenko committed
162 163 164 165 166 167 168 169 170
    \
    new->key = key;\
    new->record = value;\
    new->next = table->bins[hash_val];\
    table->bins[hash_val] = new;\
    table->num_entries++;\
}

int
171
 st__insert( st__table *table, const char *key, char *value)
Alan Mishchenko committed
172 173
{
    int hash_val;
174 175
    st__table_entry *newEntry;
    st__table_entry *ptr, **last;
Alan Mishchenko committed
176 177 178 179 180

    hash_val = do_hash(key, table);

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

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

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

int
230
 st__find_or_add( st__table *table, char *key, char ***slot)
Alan Mishchenko committed
231 232
{
    int hash_val;
233
    st__table_entry *newEntry, *ptr, **last;
Alan Mishchenko committed
234 235 236 237 238

    hash_val = do_hash(key, table);

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

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

int
264
 st__find( st__table *table, char *key, char ***slot)
Alan Mishchenko committed
265 266
{
    int hash_val;
267
    st__table_entry *ptr, **last;
Alan Mishchenko committed
268 269 270 271 272

    hash_val = do_hash(key, table);

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

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

static int
284
rehash( st__table *table)
Alan Mishchenko committed
285
{
286
    st__table_entry *ptr, *next, **old_bins;
Alan Mishchenko committed
287 288 289 290 291 292 293 294 295 296 297 298 299
    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;
300
    table->bins = ABC_ALLOC( st__table_entry *, table->num_bins);
Alan Mishchenko committed
301
    if (table->bins == NULL) {
Alan Mishchenko committed
302 303 304
    table->bins = old_bins;
    table->num_bins = old_num_bins;
    table->num_entries = old_num_entries;
305
    return st__OUT_OF_MEM;
Alan Mishchenko committed
306 307 308 309 310 311 312 313 314
    }
    /* 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
315
    while (ptr != NULL) {
Alan Mishchenko committed
316 317 318 319 320 321 322 323
        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
324
    ABC_FREE(old_bins);
Alan Mishchenko committed
325 326 327 328

    return 1;
}

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

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

int
375
 st__delete( st__table *table, const char **keyp, char **value)
Alan Mishchenko committed
376 377
{
    int hash_val;
378
    const char *key = *keyp;
379
    st__table_entry *ptr, **last;
Alan Mishchenko committed
380 381 382 383 384

    hash_val = do_hash(key, table);

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

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

int
398
 st__delete_int( st__table *table, long *keyp, char **value)
Alan Mishchenko committed
399 400 401
{
    int hash_val;
    char *key = (char *) *keyp;
402
    st__table_entry *ptr, **last;
Alan Mishchenko committed
403 404 405 406 407

    hash_val = do_hash(key, table);

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

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

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

int
421
 st__foreach( st__table *table, enum st__retval (*func)(char *, char *, char *), char *arg)
Alan Mishchenko committed
422
{
423 424
    st__table_entry *ptr, **last;
    enum st__retval retval;
Alan Mishchenko committed
425 426 427 428
    int i;

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

int
449
 st__strhash(const char *string, int modulus)
Alan Mishchenko committed
450
{
451 452 453 454 455
    unsigned char * ustring = (unsigned char *)string;
    unsigned c, val = 0;
    assert( modulus > 0 );    
    while ((c = *ustring++) != '\0') {
        val = val*997 + c;
Alan Mishchenko committed
456
    }
457
    return (int)(val%modulus);
Alan Mishchenko committed
458 459 460
}

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

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

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

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

484 485
 st__generator *
 st__init_gen( st__table *table)
Alan Mishchenko committed
486
{
487
    st__generator *gen;
Alan Mishchenko committed
488

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
    int i;
Alan Mishchenko committed
504

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) {
515
        return 0;       /* that's all folks ! */
Alan Mishchenko committed
516 517 518 519 520 521 522 523 524 525 526 527
    }
    }
    *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
    int i;
Alan Mishchenko committed
531

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) {
542
        return 0;       /* that's all folks ! */
Alan Mishchenko committed
543 544 545
    }
    }
    *key_p = gen->entry->key;
Alan Mishchenko committed
546
    if (value_p != 0) {
547
    *value_p = (long) gen->entry->record;
Alan Mishchenko committed
548 549 550 551 552 553 554
    }
    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