IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Changeset 9083


Ignore:
Timestamp:
Sep 30, 2006, 2:45:34 PM (20 years ago)
Author:
drobbin
Message:

Fixed psHash's.

Location:
trunk/psLib/src/types
Files:
2 edited

Legend:

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

    r8973 r9083  
    1212*  @author GLG, MHPCC
    1313*
    14 *  @version $Revision: 1.32 $ $Name: not supported by cvs2svn $
    15 *  @date $Date: 2006-09-26 02:55:34 $
     14*  @version $Revision: 1.33 $ $Name: not supported by cvs2svn $
     15*  @date $Date: 2006-10-01 00:45:34 $
    1616*
    1717*  Copyright 2004-2005 Maui High Performance Computing Center, University of Hawaii
     
    4747psList* psHashKeyList(const psHash* hash)
    4848{
     49    PS_ASSERT_PTR_NON_NULL(hash, NULL);
    4950    psS32 i = 0;                  // Loop index variable
    5051    psList* myLinkList = NULL;  // The output data structure
    5152    psHashBucket* ptr = NULL;   // Used to step thru linked list.
    5253
    53     if (hash == NULL) {
    54         return NULL;
    55     }
    5654    // Create the linked list
    5755    myLinkList = psListAlloc(NULL);
     
    9896    bucket->key = psStringCopy(key);
    9997
    100     if (data == NULL) {
    101         // NOTE: Should we flag a warning message?
    102         bucket->data = NULL;
    103     } else {
    104         bucket->data = psMemIncrRefCounter(data);
    105     }
     98    //XXX:  Since this function is static and only called by doHashWork,
     99    //data can never be NULL here.
     100    //    if (data == NULL) {
     101    // NOTE: Should we flag a warning message?
     102    //        bucket->data = NULL;
     103    //    } else {
     104    bucket->data = psMemIncrRefCounter(data);
     105    //    }
    106106
    107107    bucket->next = next;
     
    120120static void hashBucketFree(psHashBucket* bucket)
    121121{
    122     if (bucket == NULL) {
    123         return;
    124     }
    125 
    126122    psFree(bucket->key);
    127 
    128123    psFree(bucket->data);
    129124}
     
    139134psHash* psHashAlloc(long nalloc)        // initial number of buckets
    140135{
     136    if (nalloc < 0)
     137    {
     138        psError(PS_ERR_BAD_PARAMETER_VALUE, true,
     139                "Can't allocate a psHash of negative size.");
     140        return NULL;
     141    }
    141142    psS32 i = 0;                  // loop index variable
    142143
     
    185186    psS32 i = 0;                  // Loop index variable.
    186187
    187     if (table == NULL) {
    188         return;
    189     }
    190188    // Loop through each bucket in the hash table.  If that bucket is not
    191189    // NULL, then free the bucket via a function call to hashBucketFree();
     
    225223static psPtr doHashWork(psHash* table,
    226224                        const char *key,
    227                         psPtr data, psBool remove
     225                        psPtr data,
     226                        psBool remove
    228227                           )
    229228{
     
    240239    // function, but I'm checking it anyway since future coders might change
    241240    // the way this procedure is called.
    242     if ((table == NULL) || (key == NULL)) {
     241    /*    if ((table == NULL) || (key == NULL)) {
    243242        psError(PS_ERR_BAD_PARAMETER_NULL, true,
    244                 _("Input key can not be NULL."));
     243        _("Input key can not be NULL."));
    245244        return NULL;
    246245    }
     246    */
    247247    // NOTE: This is the originally supplied hash function.
    248248    // for (psS32 i = 0, len = strlen(key); i < len; i++) {
     
    253253    // This hash algorithm is from Sedgewick.  NOTE: must reread to ensure that
    254254    // the size of the hash table is not required to be a prime number.
     255    if (table->n <= 0)
     256        return NULL;
    255257    tmpchar = (char *)key;
    256258    for (hash = 0; *tmpchar != '\0'; tmpchar++) {
     
    260262    // NOTE: This should not be necessary, but for now, I'm checking bounds
    261263    // anyway.
    262     if ((hash < 0) || (hash >= table->n)) {
     264    /*    if ((hash < 0) || (hash >= table->n)) {
    263265        psError(PS_ERR_UNKNOWN, true,
    264                 "Internal hash function out of range (%" PRId64 ")", hash);
    265     }
     266        "Internal hash function out of range (%" PRId64 ")", hash);
     267    }
     268    */
    266269    // ptr will have the correct hash bucket.
    267270    ptr = table->buckets[hash];
     
    397400                  const char *key)
    398401{
     402    PS_ASSERT_PTR_NON_NULL(hash, false);
     403    PS_ASSERT_PTR_NON_NULL(key, false);
     404
    399405    psPtr data = NULL;
    400406    psBool retVal = false;
    401 
    402     PS_ASSERT_PTR_NON_NULL(hash, false);
    403     PS_ASSERT_PTR_NON_NULL(key, false);
    404407
    405408    data = doHashWork(hash, key, NULL, true);
     
    421424    int nElements = 0;
    422425    int nbucket = hash->n;
     426    //XXX:  If we do psArrayAlloc(0) here and use psArrayAdd(result, 1, tmpBucket->data)
     427    //we can eliminate the 2nd for loop below.
    423428    for (int i = 0; i < nbucket; i++) {
    424429        psHashBucket* tmpBucket = hash->buckets[i];
  • trunk/psLib/src/types/psHash.h

    r8973 r9083  
    1111 *  @author GLG, MHPCC
    1212 *
    13  *  @version $Revision: 1.18 $ $Name: not supported by cvs2svn $
    14  *  @date $Date: 2006-09-26 02:55:34 $
     13 *  @version $Revision: 1.19 $ $Name: not supported by cvs2svn $
     14 *  @date $Date: 2006-10-01 00:45:34 $
    1515 *
    1616 *  Copyright 2004-2005 Maui High Performance Computing Center, University of Hawaii
Note: See TracChangeset for help on using the changeset viewer.