Annotation of parser3/src/lib/sdbm/sdbm_hash.c, revision 1.3

1.3     ! moko        1: /* Copyright 2000-2005 The Apache Software Foundation or its licensors, as
        !             2:  * applicable.
1.1       paf         3:  *
1.3     ! moko        4:  * Licensed under the Apache License, Version 2.0 (the "License");
        !             5:  * you may not use this file except in compliance with the License.
        !             6:  * You may obtain a copy of the License at
        !             7:  *
        !             8:  *     http://www.apache.org/licenses/LICENSE-2.0
        !             9:  *
        !            10:  * Unless required by applicable law or agreed to in writing, software
        !            11:  * distributed under the License is distributed on an "AS IS" BASIS,
        !            12:  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
        !            13:  * See the License for the specific language governing permissions and
        !            14:  * limitations under the License.
1.1       paf        15:  */
                     16: 
                     17: /*
                     18:  * sdbm - ndbm work-alike hashed database library
                     19:  * based on Per-Aake Larson's Dynamic Hashing algorithms. BIT 18 (1978).
                     20:  * author: oz@nexus.yorku.ca
                     21:  * status: ex-public domain. keep it that way.
                     22:  *
                     23:  * hashing routine
                     24:  */
                     25: 
1.2       moko       26: #include "pa_sdbm.h"
1.1       paf        27: #include "sdbm_private.h"
                     28: 
                     29: /*
                     30:  * polynomial conversion ignoring overflows
                     31:  * [this seems to work remarkably well, in fact better
                     32:  * then the ndbm hash function. Replace at your own risk]
                     33:  * use: 65599  nice.
                     34:  *      65587   even better. 
                     35:  */
                     36: long sdbm_hash(const char *str, int len)
                     37: {
                     38:        register unsigned long n = 0;
                     39: 
                     40: #define DUFF   /* go ahead and use the loop-unrolled version */
                     41: #ifdef DUFF
                     42: 
                     43: #define HASHC  n = *str++ + 65599 * n
                     44: 
                     45:        if (len > 0) {
                     46:                register int loop = (len + 8 - 1) >> 3;
                     47: 
                     48:                switch(len & (8 - 1)) {
                     49:                case 0: do {
                     50:                        HASHC;  case 7: HASHC;
                     51:                case 6: HASHC;  case 5: HASHC;
                     52:                case 4: HASHC;  case 3: HASHC;
                     53:                case 2: HASHC;  case 1: HASHC;
                     54:                        } while (--loop);
                     55:                }
                     56: 
                     57:        }
                     58: #else
                     59:        while (len--)
                     60:                n = *str++ + 65599 * n;
                     61: #endif
                     62:        return n;
                     63: }

E-mail: