st.c 12.3 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>
Alan Mishchenko committed
12
#include "abc_global.h"
Alan Mishchenko committed
13
#include "st.h"
Alan Mishchenko committed
14

Alan Mishchenko committed
15
#define ST_NUMCMP(x,y) ((x) != (y))
Alan Mishchenko committed
16 17 18
#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
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
#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))

static int rehash();
int st_numhash(), st_ptrhash(), st_numcmp(), st_ptrcmp();

st_table *
st_init_table_with_params(compare, hash, size, density, grow_factor,
              reorder_flag)
int (*compare)();
int (*hash)();
int size;
int density;
double grow_factor;
int reorder_flag;
{
    int i;
    st_table *new;

Alan Mishchenko committed
45
    new = ABC_ALLOC(st_table, 1);
Alan Mishchenko committed
46 47
    if (new == NULL) {
    return NULL;
Alan Mishchenko committed
48 49 50 51 52 53 54 55 56 57 58
    }
    new->compare = compare;
    new->hash = hash;
    new->num_entries = 0;
    new->max_density = density;
    new->grow_factor = grow_factor;
    new->reorder_flag = reorder_flag;
    if (size <= 0) {
    size = 1;
    }
    new->num_bins = size;
Alan Mishchenko committed
59
    new->bins = ABC_ALLOC(st_table_entry *, size);
Alan Mishchenko committed
60
    if (new->bins == NULL) {
Alan Mishchenko committed
61
    ABC_FREE(new);
Alan Mishchenko committed
62
    return NULL;
Alan Mishchenko committed
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
    }
    for(i = 0; i < size; i++) {
    new->bins[i] = 0;
    }
    return new;
}

st_table *
st_init_table(compare, hash)
int (*compare)();
int (*hash)();
{
    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
st_free_table(table)
st_table *table;
{
    register st_table_entry *ptr, *next;
    int i;

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

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

#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
109
    if ((ptr) != NULL && (table)->reorder_flag) {\
Alan Mishchenko committed
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
    *(last) = (ptr)->next;\
    (ptr)->next = (table)->bins[hash_val];\
    (table)->bins[hash_val] = (ptr);\
    }

int
st_lookup(table, key, value)
st_table *table;
register char *key;
char **value;
{
    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
128
    if (ptr == NULL) {
Alan Mishchenko committed
129 130
    return 0;
    } else {
Alan Mishchenko committed
131
    if (value != NULL) {
Alan Mishchenko committed
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
        *value = ptr->record; 
    }
    return 1;
    }
}

int
st_lookup_int(table, key, value)
st_table *table;
register char *key;
int *value;
{
    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
151
    if (ptr == NULL) {
Alan Mishchenko committed
152 153
    return 0;
    } else {
Alan Mishchenko committed
154
    if (value != 0) {
Alan Mishchenko committed
155 156 157 158 159 160 161 162 163 164 165 166 167 168
        *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
169
    new = ABC_ALLOC(st_table_entry, 1);\
Alan Mishchenko committed
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
    \
    new->key = key;\
    new->record = value;\
    new->next = table->bins[hash_val];\
    table->bins[hash_val] = new;\
    table->num_entries++;\
}

int
st_insert(table, key, value)
register st_table *table;
register char *key;
char *value;
{
    int hash_val;
    st_table_entry *new;
    register st_table_entry *ptr, **last;

    hash_val = do_hash(key, table);

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

Alan Mishchenko committed
192
    if (ptr == NULL) {
Alan Mishchenko committed
193 194 195 196 197 198
    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);
    }
Alan Mishchenko committed
199
    new = ABC_ALLOC(st_table_entry, 1);
Alan Mishchenko committed
200
    if (new == NULL) {
Alan Mishchenko committed
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
        return ST_OUT_OF_MEM;
    }
    new->key = key;
    new->record = value;
    new->next = table->bins[hash_val];
    table->bins[hash_val] = new;
    table->num_entries++;
    return 0;
    } else {
    ptr->record = value;
    return 1;
    }
}

int
st_add_direct(table, key, value)
st_table *table;
char *key;
char *value;
{
    int hash_val;
    st_table_entry *new;
    
    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);
Alan Mishchenko committed
231
    new = ABC_ALLOC(st_table_entry, 1);
Alan Mishchenko committed
232
    if (new == NULL) {
Alan Mishchenko committed
233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255
    return ST_OUT_OF_MEM;
    }
    new->key = key;
    new->record = value;
    new->next = table->bins[hash_val];
    table->bins[hash_val] = new;
    table->num_entries++;
    return 1;
}

int
st_find_or_add(table, key, slot)
st_table *table;
char *key;
char ***slot;
{
    int hash_val;
    st_table_entry *new, *ptr, **last;

    hash_val = do_hash(key, table);

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

Alan Mishchenko committed
256
    if (ptr == NULL) {
Alan Mishchenko committed
257 258 259 260 261 262
    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);
    }
Alan Mishchenko committed
263
    new = ABC_ALLOC(st_table_entry, 1);
Alan Mishchenko committed
264
    if (new == NULL) {
Alan Mishchenko committed
265 266 267 268 269 270 271
        return ST_OUT_OF_MEM;
    }
    new->key = key;
    new->record = (char *) 0;
    new->next = table->bins[hash_val];
    table->bins[hash_val] = new;
    table->num_entries++;
Alan Mishchenko committed
272
    if (slot != NULL) *slot = &new->record;
Alan Mishchenko committed
273 274
    return 0;
    } else {
Alan Mishchenko committed
275
    if (slot != NULL) *slot = &ptr->record;
Alan Mishchenko committed
276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292
    return 1;
    }
}

int
st_find(table, key, slot)
st_table *table;
char *key;
char ***slot;
{
    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
293
    if (ptr == NULL) {
Alan Mishchenko committed
294 295
    return 0;
    } else {
Alan Mishchenko committed
296
    if (slot != NULL) {
Alan Mishchenko committed
297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320
        *slot = &ptr->record;
    }
    return 1;
    }
}

static int
rehash(table)
register st_table *table;
{
    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
321
    table->bins = ABC_ALLOC(st_table_entry *, table->num_bins);
Alan Mishchenko committed
322
    if (table->bins == NULL) {
Alan Mishchenko committed
323 324 325 326 327 328 329 330 331 332 333 334 335
    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
336
    while (ptr != NULL) {
Alan Mishchenko committed
337 338 339 340 341 342 343 344
        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
345
    ABC_FREE(old_bins);
Alan Mishchenko committed
346 347 348 349 350 351 352 353 354 355 356 357

    return 1;
}

st_table *
st_copy(old_table)
st_table *old_table;
{
    st_table *new_table;
    st_table_entry *ptr, *newptr, *next, *new;
    int i, j, num_bins = old_table->num_bins;

Alan Mishchenko committed
358
    new_table = ABC_ALLOC(st_table, 1);
Alan Mishchenko committed
359 360
    if (new_table == NULL) {
    return NULL;
Alan Mishchenko committed
361 362 363
    }
    
    *new_table = *old_table;
Alan Mishchenko committed
364
    new_table->bins = ABC_ALLOC(st_table_entry *, num_bins);
Alan Mishchenko committed
365
    if (new_table->bins == NULL) {
Alan Mishchenko committed
366
    ABC_FREE(new_table);
Alan Mishchenko committed
367
    return NULL;
Alan Mishchenko committed
368 369
    }
    for(i = 0; i < num_bins ; i++) {
Alan Mishchenko committed
370
    new_table->bins[i] = NULL;
Alan Mishchenko committed
371
    ptr = old_table->bins[i];
Alan Mishchenko committed
372
    while (ptr != NULL) {
Alan Mishchenko committed
373
        new = ABC_ALLOC(st_table_entry, 1);
Alan Mishchenko committed
374
        if (new == NULL) {
Alan Mishchenko committed
375 376
        for (j = 0; j <= i; j++) {
            newptr = new_table->bins[j];
Alan Mishchenko committed
377
            while (newptr != NULL) {
Alan Mishchenko committed
378
            next = newptr->next;
Alan Mishchenko committed
379
            ABC_FREE(newptr);
Alan Mishchenko committed
380 381 382
            newptr = next;
            }
        }
Alan Mishchenko committed
383 384
        ABC_FREE(new_table->bins);
        ABC_FREE(new_table);
Alan Mishchenko committed
385
        return NULL;
Alan Mishchenko committed
386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409
        }
        *new = *ptr;
        new->next = new_table->bins[i];
        new_table->bins[i] = new;
        ptr = ptr->next;
    }
    }
    return new_table;
}

int
st_delete(table, keyp, value)
register st_table *table;
register char **keyp;
char **value;
{
    int hash_val;
    char *key = *keyp;
    register st_table_entry *ptr, **last;

    hash_val = do_hash(key, table);

    FIND_ENTRY(table, hash_val, key, ptr ,last);
    
Alan Mishchenko committed
410
    if (ptr == NULL) {
Alan Mishchenko committed
411 412 413 414
    return 0;
    }

    *last = ptr->next;
Alan Mishchenko committed
415
    if (value != NULL) *value = ptr->record;
Alan Mishchenko committed
416
    *keyp = ptr->key;
Alan Mishchenko committed
417
    ABC_FREE(ptr);
Alan Mishchenko committed
418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435
    table->num_entries--;
    return 1;
}

int
st_delete_int(table, keyp, value)
register st_table *table;
register long *keyp;
char **value;
{
    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
436
    if (ptr == NULL) {
Alan Mishchenko committed
437 438 439 440
        return 0;
    }

    *last = ptr->next;
Alan Mishchenko committed
441
    if (value != NULL) *value = ptr->record;
Alan Mishchenko committed
442
    *keyp = (long) ptr->key;
Alan Mishchenko committed
443
    ABC_FREE(ptr);
Alan Mishchenko committed
444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459
    table->num_entries--;
    return 1;
}

int
st_foreach(table, func, arg)
st_table *table;
enum st_retval (*func)();
char *arg;
{
    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
460
    while (ptr != NULL) {
Alan Mishchenko committed
461 462 463 464 465 466 467 468 469 470
        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
471
        ABC_FREE(ptr);
Alan Mishchenko committed
472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531
        ptr = *last;
        }
    }
    }
    return 1;
}

int
st_strhash(string, modulus)
register char *string;
int modulus;
{
    register int val = 0;
    register int c;
    
    while ((c = *string++) != '\0') {
    val = val*997 + c;
    }

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

int
st_numhash(x, size)
char *x;
int size;
{
    return ST_NUMHASH(x, size);
}

int
st_ptrhash(x, size)
char *x;
int size;
{
    return ST_PTRHASH(x, size);
}

int
st_numcmp(x, y)
char *x;
char *y;
{
    return ST_NUMCMP(x, y);
}

int
st_ptrcmp(x, y)
char *x;
char *y;
{
    return ST_NUMCMP(x, y);
}

st_generator *
st_init_gen(table)
st_table *table;
{
    st_generator *gen;

Alan Mishchenko committed
532
    gen = ABC_ALLOC(st_generator, 1);
Alan Mishchenko committed
533 534
    if (gen == NULL) {
    return NULL;
Alan Mishchenko committed
535 536
    }
    gen->table = table;
Alan Mishchenko committed
537
    gen->entry = NULL;
Alan Mishchenko committed
538 539 540 541 542 543 544 545 546 547 548 549 550
    gen->index = 0;
    return gen;
}


int 
st_gen(gen, key_p, value_p)
st_generator *gen;
char **key_p;
char **value_p;
{
    register int i;

Alan Mishchenko committed
551
    if (gen->entry == NULL) {
Alan Mishchenko committed
552 553
    /* try to find next entry */
    for(i = gen->index; i < gen->table->num_bins; i++) {
Alan Mishchenko committed
554
        if (gen->table->bins[i] != NULL) {
Alan Mishchenko committed
555 556 557 558 559
        gen->index = i+1;
        gen->entry = gen->table->bins[i];
        break;
        }
    }
Alan Mishchenko committed
560
    if (gen->entry == NULL) {
Alan Mishchenko committed
561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580
        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 
st_gen_int(gen, key_p, value_p)
st_generator *gen;
char **key_p;
long *value_p;
{
    register int i;

Alan Mishchenko committed
581
    if (gen->entry == NULL) {
Alan Mishchenko committed
582 583
    /* try to find next entry */
    for(i = gen->index; i < gen->table->num_bins; i++) {
Alan Mishchenko committed
584
        if (gen->table->bins[i] != NULL) {
Alan Mishchenko committed
585 586 587 588 589
        gen->index = i+1;
        gen->entry = gen->table->bins[i];
        break;
        }
    }
Alan Mishchenko committed
590
    if (gen->entry == NULL) {
Alan Mishchenko committed
591 592 593 594
        return 0;        /* that's all folks ! */
    }
    }
    *key_p = gen->entry->key;
Alan Mishchenko committed
595
    if (value_p != 0) {
Alan Mishchenko committed
596 597 598 599 600 601 602 603 604 605 606
       *value_p = (long) gen->entry->record;
    }
    gen->entry = gen->entry->next;
    return 1;
}


void
st_free_gen(gen)
st_generator *gen;
{
Alan Mishchenko committed
607
    ABC_FREE(gen);
Alan Mishchenko committed
608
}