IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
Jun 6, 2004, 2:32:53 PM (22 years ago)
Author:
gusciora
Message:

Lots of bug fixes and such.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/psLib/src/sysUtils/psHash.c

    r885 r889  
    1 /******************************************************************************
    2     An interface to hash tables for Pan-STARRS
    3  
    4     Collisions are handled by simple linked lists
    5  *****************************************************************************/
     1/** @file  psHash.c
     2 *
     3 *  @brief Contains support for basic hashing functions.
     4 *
     5 *  This file will hold the functions for defining a hash table with arbitrary
     6 *  data types, allocating/deallocating that has table, adding and removing
     7 *  data from that hash table, and listing all keys defined in the hash table.
     8 *
     9 *  @author Robert Lupton, Princeton University
     10 *  @author George Gusciora, MHPCC
     11 *   
     12 *  @version $Revision: 1.6 $ $Name: not supported by cvs2svn $
     13 *  @date $Date: 2004-06-07 00:32:53 $
     14 *
     15 *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
     16 */
    617#include <stdlib.h>
    718#include <stdio.h>
     
    1223#include "psString.h"
    1324#include "psTrace.h"
     25#include "psAbort.h"
    1426
    1527psDlist *psHashKeyList(psHash *table)
     
    142154    // bucket.
    143155    psHashBucket *optr = NULL;
     156    char *tmpchar = NULL;
    144157
    145158    // NOTE: this is NOT a good hash function!  See Knuth.
    146159    //
    147     for (int i = 0, len = strlen(key); i < len; i++) {
    148         hash = (hash << 1) ^ key[i];
    149     }
    150     hash &= (table->nbucket - 1);
     160    //    for (int i = 0, len = strlen(key); i < len; i++) {
     161    //        hash = (hash << 1) ^ key[i];
     162    //    }
     163    //    hash &= (table->nbucket - 1);
     164
     165    // This hash algorithm is from Sedgewick.  NOTE: must reread to ensure that
     166    // the size of the hash table is not required to be a prime number.
     167    tmpchar = (char *) key;
     168    for (hash = 0; *tmpchar != '\0'; tmpchar++) {
     169        hash = (64 * hash + *tmpchar) % (table->nbucket);
     170    }
     171
     172    // NOTE: This should not be necessary, but for now, I'm checking bounds
     173    // anyway.
     174    if ((hash < 0) ||
     175            (hash >= table->nbucket)) {
     176        psAbort(__func__,"Internal hash function out of range (%d)",
     177                hash);
     178    }
    151179
    152180    ptr = table->buckets[hash];
Note: See TracChangeset for help on using the changeset viewer.