IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
May 1, 2004, 1:04:35 PM (22 years ago)
Author:
gusciora
Message:

This is a fairly large checkin. I had been sitting on a lot of test code
that was not quite in sync with the current test harness. I modified it
somewhat so it mostly works with the test harness, though there are still
problems with the printFooter() function not having a status for "status
unknown". Also, the runTest is not correctly parsing my date strings in
psLogMsg calls. Also, I added an individual entry for each test in the
Makefile in the test directory. I did this because the generic rules were
failing, and I did not want to break the tests for those people who were
successfully using the generic rules.

File:
1 edited

Legend:

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

    r494 r560  
    1313#include "psTrace.h"
    1414
    15 // A bucket that holds an item of data
    16 typedef struct HashBucket
    17 {
    18     char *key;    // key for this item of data
    19     void *data;    // the data itself
    20     struct HashBucket *next;  // list of other possible keys
    21 }
    22 HashBucket;
    23 
    24 // An entire hash table
    25 struct HashTable
    26 {
    27     int nbucket;   // number of buckets
    28     HashBucket **buckets;  // the buckets themselves
    29 };
    30 
    31 /******************************************************************************
    32     Con/Destruct buckets
    33  *****************************************************************************/
    34 static HashBucket *hashBucketAlloc(const char *key,
    35                                    void *data,
    36                                    HashBucket *next)
    37 {
    38     HashBucket *bucket = psAlloc(sizeof(HashBucket));
     15/******************************************************************************
     16    Construct buckets
     17 *****************************************************************************/
     18static psHashBucket *hashBucketAlloc(const char *key,
     19                                     void *data,
     20                                     psHashBucket *next)
     21{
     22    psHashBucket *bucket = psAlloc(sizeof(psHashBucket));
    3923
    4024    bucket->key = psStringCopy(key);
     
    4630
    4731/******************************************************************************
    48  
    49  *****************************************************************************/
    50 static void hashBucketFree(
    51     HashBucket *bucket,   // bucket to free
    52     void (*itemFree)(void *item)) // how to free hashed data;
    53 // or NULL
     32    Destruct buckets
     33 *****************************************************************************/
     34static void hashBucketFree(psHashBucket *bucket,   // bucket to free
     35                           void (*itemFree)(void *item)) // how to free data;
    5436{
    5537    if (bucket == NULL)
     
    7557{
    7658    psHash *table = psAlloc(sizeof(psHash));
    77     table->buckets = psAlloc(nbucket*sizeof(HashBucket *));
     59    table->buckets = psAlloc(nbucket*sizeof(psHashBucket *));
    7860    table->nbucket = nbucket;
    7961
     
    10486    {
    10587        if (table->buckets[i] != NULL) {
    106             HashBucket *ptr = table->buckets[i];
     88            psHashBucket *ptr = table->buckets[i];
    10789            while (ptr != NULL) {
    108                 HashBucket *tmp = ptr->next;
     90                psHashBucket *tmp = ptr->next;
    10991                hashBucketFree(ptr, itemFree);
    11092                ptr = tmp;
     
    124106    N.b. this is NOT a good hash function! See Knuth for some better ones
    125107 *****************************************************************************/
    126 static void *doHashWork(
    127     psHash *table,   // table to insert in
    128     const char *key,   // key to use
    129     void *data,    // data to insert,
    130     // or (if NULL) retrieve/remove
    131     int remove
    132         ,    // remove the item from the list?
    133         void (*itemFree)(void *item)) // how to free hashed data
    134     // or NULL
     108static void *doHashWork(psHash     *table,   // table to insert in
     109                        const char *key,     // key to use
     110                        void       *data,    // data to insert,
     111                        // or (if NULL) retrieve/remove
     112                        int remove
     113                            ,          // remove the item from the list?
     114                            void (*itemFree)(void *item))
     115    // how to free hashed data
    135116{
    136117    long int hash = 1;
     
    142123    hash &= (table->nbucket - 1);
    143124
    144     HashBucket *ptr = table->buckets[hash];
    145     /*
    146      * We've found the correct hash bucket, now we need to know what to do
    147      */
     125    psHashBucket *ptr = table->buckets[hash];
     126
     127    // We've found the correct hash bucket, now we need to know what to do
    148128    if (data == NULL) {   // retrieve/remove
    149129        if (remove
    150130           ) {
    151             HashBucket *optr = ptr;
     131            psHashBucket *optr = ptr;
    152132            while (ptr != NULL) {
    153133                if (strcmp(key, ptr->key) == 0) { // found it!
     
    196176            }
    197177        }
    198         /*
    199          * Not found, so insert at the front of the list
    200          */
     178
     179        // Not found, so insert at the front of the list
    201180        table->buckets[hash] = hashBucketAlloc(key, data, table->buckets[hash]);
    202181
     
    208187    Insert a value into a hash table
    209188 *****************************************************************************/
    210 void *psHashInsert(psHash *table, // table to insert in
     189void *psHashInsert(psHash *table,   // table to insert in
    211190                   const char *key, // key to use
    212                    void *data,  // data to insert
     191                   void *data,      // data to insert
    213192                   void (*itemFree)(void *item)) // how to free hashed data;
    214 // or NULL
    215193{
    216194    return doHashWork(table, key, data, 0, itemFree);
     
    220198    Lookup a value in a hash table
    221199 *****************************************************************************/
    222 void *psHashLookup(psHash *table, // table to lookup key in
     200void *psHashLookup(psHash *table,   // table to lookup key in
    223201                   const char *key) // key to lookup
    224202{
     
    229207    Remove and return a value from a hash table
    230208 *****************************************************************************/
    231 void *psHashRemove(psHash *table, // table to lookup key in
     209void *psHashRemove(psHash *table,   // table to lookup key in
    232210                   const char *key) // key to lookup
    233211{
Note: See TracChangeset for help on using the changeset viewer.