st.c 12.4 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


Alan Mishchenko committed
19
#define ST_NUMCMP(x,y) ((x) != (y))
20
#define ST_NUMHASH(x,size) (Abc_AbsInt((long)x)%(size))
Alan Mishchenko committed
21 22
//#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 24 25 26 27 28 29 30 31 32
#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))

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

st_table *
41
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 47
    newTable = ABC_ALLOC(st_table, 1);
    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 60 61 62
    newTable->num_bins = size;
    newTable->bins = ABC_ALLOC(st_table_entry *, size);
    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
}

st_table *
72
st_init_table(st_compare_func_type compare, st_hash_func_type hash)
Alan Mishchenko committed
73 74 75 76 77 78 79 80
{
    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
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);\
    }\
    \
Alan Mishchenko committed
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
    st_table_entry *newEntry;
175
    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 183 184 185 186 187
    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);
    }
188 189
    newEntry = ABC_ALLOC(st_table_entry, 1);
    if (newEntry == NULL) {
Alan Mishchenko committed
190 191
        return ST_OUT_OF_MEM;
    }
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 212 213 214 215 216
    
    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);
217 218
    newEntry = ABC_ALLOC(st_table_entry, 1);
    if (newEntry == NULL) {
Alan Mishchenko committed
219 220
    return ST_OUT_OF_MEM;
    }
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 241 242 243 244 245
    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);
    }
246 247
    newEntry = ABC_ALLOC(st_table_entry, 1);
    if (newEntry == NULL) {
Alan Mishchenko committed
248 249
        return ST_OUT_OF_MEM;
    }
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 267 268 269 270 271 272
{
    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
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;
Alan Mishchenko committed
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 305 306 307 308 309 310 311 312 313 314
    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
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 329

    return 1;
}

st_table *
330
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 337
    newEntry_table = ABC_ALLOC(st_table, 1);
    if (newEntry_table == NULL) {
Alan Mishchenko committed
338
    return NULL;
Alan Mishchenko committed
339 340
    }
    
341 342 343 344
    *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
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 352
        newEntry = ABC_ALLOC(st_table_entry, 1);
        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 425 426 427 428
{
    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
429
    while (ptr != NULL) {
Alan Mishchenko committed
430 431 432 433 434 435 436 437 438 439
        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
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
    int val = 0;
    int c;
Alan Mishchenko committed
453 454 455 456 457 458 459 460 461
    
    while ((c = *string++) != '\0') {
    val = val*997 + c;
    }

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

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

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

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

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

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

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


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

Alan Mishchenko committed
506
    if (gen->entry == NULL) {
Alan Mishchenko committed
507 508
    /* try to find next entry */
    for(i = gen->index; i < gen->table->num_bins; i++) {
Alan Mishchenko committed
509
        if (gen->table->bins[i] != NULL) {
Alan Mishchenko committed
510 511 512 513 514
        gen->index = i+1;
        gen->entry = gen->table->bins[i];
        break;
        }
    }
Alan Mishchenko committed
515
    if (gen->entry == NULL) {
Alan Mishchenko committed
516 517 518 519 520 521 522 523 524 525 526 527 528
        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 
529
st_gen_int(st_generator *gen, const char **key_p, long *value_p)
Alan Mishchenko committed
530
{
531
    int i;
Alan Mishchenko committed
532

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


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

ABC_NAMESPACE_IMPL_END