"Fossies" - the Fresh Open Source Software Archive

Member "ical-tcl/types/hashfuncs.C" (15 Apr 2019, 1224 Bytes) of package /linux/privat/ical-3.0.4.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 /* Copyright (c) 1995 by Sanjay Ghemawat */
    2 #include <ctype.h>
    3 #include "hashfuncs.h"
    4 
    5 /*
    6  * Multiplicative hash function.
    7  *
    8  * The value "1737350766" is the bottom 32 bits of int(PHI * 2^30),
    9  * suggested in CLR, Knuth.  The hash value should be top "k" of the
   10  * bottom 30 bits of the number multipled by this value.
   11  */
   12 
   13 int hash_int(int x) {
   14     /*
   15      * Multiply by a constant and take the bottom bits of the
   16      * result.
   17      */
   18 
   19     return (int) ((((unsigned int) x) * 1737350766) & 0x7fffffff);
   20 }
   21 
   22 int hash_ptr(void* x) {
   23     return hash_int((int) ((unsigned long) x));
   24 }
   25 
   26 /* String hashing out of The Dragon Book. */
   27 int hash_string(char const* x) {
   28     char const* p;
   29     unsigned int h = 0;
   30     unsigned int g;
   31 
   32     for (p = x; *p != '\0'; p++) {
   33         h = (h << 4) + *p;
   34         if ((g = h & 0xf0000000)) {
   35             h = h ^ (g >> 24);
   36             h = h ^ g;
   37         }
   38     }
   39     return (int) h;
   40 }
   41 
   42 int hash_string_lowercase(char const* x) {
   43     char const* p;
   44     unsigned int h = 0;
   45     unsigned int g;
   46 
   47     for (p = x; *p != '\0'; p++) {
   48         h = (h << 4) + tolower(*p);
   49         if ((g = h & 0xf0000000)) {
   50             h = h ^ (g >> 24);
   51             h = h ^ g;
   52         }
   53     }
   54     return (int) h;
   55 }