[BACK]Return to hash.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / asn1_compile

Annotation of src/usr.bin/asn1_compile/hash.c, Revision 1.3

1.1       hin         1: /*
                      2:  * Copyright (c) 1997 Kungliga Tekniska Högskolan
                      3:  * (Royal Institute of Technology, Stockholm, Sweden).
                      4:  * All rights reserved.
                      5:  *
                      6:  * Redistribution and use in source and binary forms, with or without
                      7:  * modification, are permitted provided that the following conditions
                      8:  * are met:
                      9:  *
                     10:  * 1. Redistributions of source code must retain the above copyright
                     11:  *    notice, this list of conditions and the following disclaimer.
                     12:  *
                     13:  * 2. Redistributions in binary form must reproduce the above copyright
                     14:  *    notice, this list of conditions and the following disclaimer in the
                     15:  *    documentation and/or other materials provided with the distribution.
                     16:  *
                     17:  * 3. Neither the name of the Institute nor the names of its contributors
                     18:  *    may be used to endorse or promote products derived from this software
                     19:  *    without specific prior written permission.
                     20:  *
                     21:  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
                     22:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     23:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     24:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
                     25:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     26:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     27:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     28:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     29:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     30:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     31:  * SUCH DAMAGE.
                     32:  */
                     33:
                     34: /*
                     35:  * Hash table functions
                     36:  */
                     37:
                     38: #include "gen_locl.h"
                     39:
                     40: /*
                     41: RCSID("$KTH: hash.c,v 1.8 1999/12/02 17:05:02 joda Exp $");
                     42: */
                     43:
                     44: static Hashentry *_search(Hashtab * htab,      /* The hash table */
                     45:                          void *ptr);   /* And key */
                     46:
                     47: Hashtab *
                     48: hashtabnew(int sz,
                     49:           int (*cmp) (void *, void *),
                     50:           unsigned (*hash) (void *))
                     51: {
                     52:     Hashtab *htab;
                     53:     int i;
                     54:
                     55:     assert(sz > 0);
                     56:
                     57:     htab = (Hashtab *) malloc(sizeof(Hashtab) + (sz - 1) * sizeof(Hashentry *));
                     58:
                     59:     if (htab == NULL) {
                     60:        return NULL;
                     61:     } else {
1.3     ! jaredy     62:        for (i = 0; i < sz; ++i)
        !            63:                htab->tab[i] = NULL;
1.1       hin        64:        htab->cmp = cmp;
                     65:        htab->hash = hash;
                     66:        htab->sz = sz;
                     67:        return htab;
                     68:     }
                     69: }
                     70:
                     71: /* Intern search function */
                     72:
                     73: static Hashentry *
                     74: _search(Hashtab * htab, void *ptr)
                     75: {
                     76:     Hashentry *hptr;
                     77:
                     78:     assert(htab && ptr);
                     79:
                     80:     for (hptr = htab->tab[(*htab->hash) (ptr) % htab->sz];
                     81:         hptr;
                     82:         hptr = hptr->next)
                     83:        if ((*htab->cmp) (ptr, hptr->ptr) == 0)
                     84:            break;
                     85:     return hptr;
                     86: }
                     87:
                     88: /* Search for element in hash table */
                     89:
                     90: void *
                     91: hashtabsearch(Hashtab * htab, void *ptr)
                     92: {
                     93:     Hashentry *tmp;
                     94:
                     95:     tmp = _search(htab, ptr);
                     96:     return tmp ? tmp->ptr : tmp;
                     97: }
                     98:
                     99: /* add element to hash table */
                    100: /* if already there, set new value */
1.2       jmc       101: /* !NULL if successful */
1.1       hin       102:
                    103: void *
                    104: hashtabadd(Hashtab * htab, void *ptr)
                    105: {
                    106:     Hashentry *h = _search(htab, ptr);
                    107:     Hashentry **tabptr;
                    108:
                    109:     assert(htab && ptr);
                    110:
                    111:     if (h)
                    112:        free((void *) h->ptr);
                    113:     else {
                    114:        h = (Hashentry *) malloc(sizeof(Hashentry));
                    115:        if (h == NULL) {
                    116:            return NULL;
                    117:        }
                    118:        tabptr = &htab->tab[(*htab->hash) (ptr) % htab->sz];
                    119:        h->next = *tabptr;
                    120:        *tabptr = h;
                    121:        h->prev = tabptr;
                    122:        if (h->next)
                    123:            h->next->prev = &h->next;
                    124:     }
                    125:     h->ptr = ptr;
                    126:     return h;
                    127: }
                    128:
                    129: /* delete element with key key. Iff freep, free Hashentry->ptr */
                    130:
                    131: int
                    132: _hashtabdel(Hashtab * htab, void *ptr, int freep)
                    133: {
                    134:     Hashentry *h;
                    135:
                    136:     assert(htab && ptr);
                    137:
                    138:     h = _search(htab, ptr);
                    139:     if (h) {
                    140:        if (freep)
                    141:            free(h->ptr);
                    142:        if ((*(h->prev) = h->next))
                    143:            h->next->prev = h->prev;
                    144:        free(h);
                    145:        return 0;
                    146:     } else
                    147:        return -1;
                    148: }
                    149:
                    150: /* Do something for each element */
                    151:
                    152: void
                    153: hashtabforeach(Hashtab * htab, int (*func) (void *ptr, void *arg),
                    154:               void *arg)
                    155: {
                    156:     Hashentry **h, *g;
                    157:
                    158:     assert(htab);
                    159:
                    160:     for (h = htab->tab; h < &htab->tab[htab->sz]; ++h)
                    161:        for (g = *h; g; g = g->next)
                    162:            if ((*func) (g->ptr, arg))
                    163:                return;
                    164: }
                    165:
                    166: /* standard hash-functions for strings */
                    167:
                    168: unsigned
                    169: hashadd(const char *s)
                    170: {                              /* Standard hash function */
                    171:     unsigned i;
                    172:
                    173:     assert(s);
                    174:
                    175:     for (i = 0; *s; ++s)
                    176:        i += *s;
                    177:     return i;
                    178: }
                    179:
                    180: unsigned
                    181: hashcaseadd(const char *s)
                    182: {                              /* Standard hash function */
                    183:     unsigned i;
                    184:
                    185:     assert(s);
                    186:
                    187:     for (i = 0; *s; ++s)
                    188:        i += toupper(*s);
                    189:     return i;
                    190: }
                    191:
                    192: #define TWELVE (sizeof(unsigned))
                    193: #define SEVENTYFIVE (6*sizeof(unsigned))
                    194: #define HIGH_BITS (~((unsigned)(~0) >> TWELVE))
                    195:
                    196: unsigned
                    197: hashjpw(const char *ss)
                    198: {                              /* another hash function */
                    199:     unsigned h = 0;
                    200:     unsigned g;
                    201:     const unsigned char *s = (const unsigned char *)ss;
                    202:
                    203:     for (; *s; ++s) {
                    204:        h = (h << TWELVE) + *s;
                    205:        if ((g = h & HIGH_BITS))
                    206:            h = (h ^ (g >> SEVENTYFIVE)) & ~HIGH_BITS;
                    207:     }
                    208:     return h;
                    209: }