summaryrefslogtreecommitdiffstats
path: root/private/windows/diamond/glist.c
blob: fc9cfb35939fe2865206d01c489325d491a3db1e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
/***    glist.c - Generic List Manager
 *
 *      Microsoft Confidential
 *      Copyright (C) Microsoft Corporation 1994
 *      All Rights Reserved.
 *
 *  Author:
 *      Benjamin W. Slivka
 *
 *  History:
 *      06-Apr-1994 bens    Initial version
 *      22-Apr-1994 bens    Add GLFindNext, GLSetValue, GLGetValue
 *      26-Apr-1994 bens    Add GLFirstItem, GLNextItem, GLPreviousItem
 *      18-May-1994 bens    Add hashing for quick lookup
 *
 *  Notes:
 *  (1) To speed up name searches on large lists, we create a hash table
 *      the first time a name search -- GLFind() or GLFindAndGetValue()
 *      -- is performed on a a list with at least 50 items.  From then on,
 *      the hash table is updated as new items are added or removed.
 */

#include <string.h>
#include <stdlib.h>

#include "types.h"
#include "asrt.h"
#include "error.h"
#include "mem.h"
#include "message.h"
#include "glist.h"

#include "glist.msg"

/*
 ** GENERIC item definition
 */

//** Empty definition, to avoid "chicken and the egg" problem
typedef struct GENLIST_t GENLIST; /* gen */
typedef GENLIST *PGENLIST; /* glist */


typedef struct GENERIC_t {
#ifdef ASSERT
    SIGNATURE         sig;      // structure signature sigGENERIC
#endif
    char             *pszKey;   // key
    void             *pv;       // current value
    struct GENERIC_t *pgenHash; // next item in hash chain
    struct GENERIC_t *pgenNext; // next item in full list
    struct GENERIC_t *pgenPrev; // previous item in full list
    PGENLIST          pglist;   // List containing this item
} GENERIC;  /* gen */
typedef GENERIC *PGENERIC;  /* pgen */

#ifdef ASSERT
#define sigGENERIC MAKESIG('G','I','T','M')  // GENERIC signature
#define AssertGen(pgen) AssertStructure(pgen,sigGENERIC);
#else // !ASSERT
#define AssertGen(pgen)
#endif // !ASSERT


/*
 ** HASHTABLE definition
 *
 *  See hashString() function for use of the following constants.
 */

#define cHASH_TOP_BITS   3      // Hash index bits that get rotated
#define cHASH_BOT_BITS   8      // Hash index bits that get shifted left
#define cHASH_BITS       (cHASH_TOP_BITS+cHASH_BOT_BITS) // Total bits in hash
#define cHASH_ENTRIES    (1<<cHASH_BITS)   // Total number of hash entries
#define HASH_MASK        (cHASH_ENTRIES-1) // Mask for hash index
#define HASH_BOT_MASK    ((1 << cHASH_BOT_BITS)-1) // Mask for bottom bits

#define cHASH_THRESHOLD  50     // Number of elements in a generic list
                                //  that will trigger hashing

typedef struct {
#ifdef ASSERT
    SIGNATURE   sig;                    // structure signature sigGENLIST
#endif
    PGENERIC    apgen[cHASH_ENTRIES];   // Hash table
} HASHTABLE; /* ht */
typedef HASHTABLE *PHASHTABLE;  /* pht */
#ifdef ASSERT
#define sigHASHTABLE MAKESIG('H','A','S','H')  // HASHTABLE signature
#define AssertHT(pv) AssertStructure(pv,sigHASHTABLE);
#else // !ASSERT
#define AssertHT(pv)
#endif // !ASSERT


/*
 ** GENLIST definition
 */

typedef struct GENLIST_t {
#ifdef ASSERT
    SIGNATURE          sig;             // structure signature sigGENLIST
#endif
    PGENERIC           pgenHead;	// First item in list
    PGENERIC           pgenTail;        // Last item in list
    void              *pvDefault;       // Default value
    PFNGLDESTROYVALUE  pfngldv;		// Value destroying function
    int                cItems;          // Count of items on list
    PHASHTABLE         pht;             // Optional hash table
} GENLIST; /* glist */
//typedef GENLIST *PGENLIST; /* pglist */
#ifdef ASSERT
#define sigGENLIST MAKESIG('G','L','S','T')  // GENLIST signature
#define AssertGList(pv) AssertStructure(pv,sigGENLIST);
#else // !ASSERT
#define AssertGList(pv)
#endif // !ASSERT


#define HGENfromPGEN(h) ((PGENERIC)(h))
#define PGENfromHGEN(p) ((HGENERIC)(p))

#define HGLfromPGL(h) ((PGENLIST)(h))
#define PGLfromHGL(p) ((HGENLIST)(p))


//** Function Prototypes

PGENERIC findItem(PGENERIC pgen, char *pszKey);
int      hashString(char *psz);
void     hashItem(PGENLIST pglist, PGENERIC pgen);


//*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*
//
//  Exported Functions
//
//*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*


/***    GLAdd - Add an item to a list
 *
 *  NOTE: See glist.h for entry/exit conditions.
 */
HGENERIC GLAdd(HGENLIST  hglist,
               char     *pszKey,
               void     *pv,
               char     *pszDesc,
               BOOL      fUnique,
               PERROR    perr)
{
    PGENERIC   pgen;
    PGENLIST   pglist;

    pglist = PGLfromHGL(hglist);
    AssertGList(pglist);

    //** If requested, make sure item does not already exist
    if (fUnique && (pgen = findItem(pglist->pgenHead,pszKey))) {
        ErrSet(perr,pszGLERR_ALREADY_CREATED,"%s%s",pszDesc,pszKey);
        perr->code = ERRGLIST_NOT_UNIQUE; //** Indicate cause
        perr->pv   = HGENfromPGEN(pgen);  //** Return existing item
        return NULL;
    }

    //** Create item
    if (!(pgen = MemAlloc(sizeof(GENERIC)))) {
        goto error;
    }

    SetAssertSignature(pgen,sigGENERIC);
    pgen->pszKey = NULL;                // Make sure not to free garbage!
    pgen->pv     = NULL;                // Make sure not to free garbage!

    //** Make copy of name
    if (pszKey) {
        if (!(pgen->pszKey = MemStrDup(pszKey))) {
            goto error;
        }
    }
    else {
        pszKey = NULL;
    }

    //** Set value
    pgen->pv = pv;

    //** Link item into list
    pgen->pgenHash = NULL;              // Always last on hash list
    pgen->pgenNext = NULL;              // Always last on list
    pgen->pgenPrev = pglist->pgenTail;  // Always points to last item on list

    if (pglist->pgenHead == NULL) {
        pglist->pgenHead = pgen;
        pglist->pgenTail = pgen;
    }
    else {
        AssertGen(pglist->pgenTail);
        pglist->pgenTail->pgenNext = pgen;  // Add to end of list
        pglist->pgenTail = pgen;            // New tail
    }

    //** Remember which list we are on!
    pgen->pglist = pglist;

    //** Update count of item in list
    pglist->cItems++;
    Assert(pglist->cItems > 0);

    //** Add to hash table, if we are hashing
    if (pglist->pht != NULL) {
        hashItem(pglist,pgen);
    }

    //** Success
    return HGENfromPGEN(pgen);

error:
    if (!pgen) {
        if (!(pgen->pszKey)) {
            MemFree(pgen->pszKey);
        }
        MemFree(pgen);
    }
    if (!ErrIsError(perr)) {
        ErrSet(perr,pszGLERR_OUT_OF_MEMORY_ITEM,"%s%s",pszDesc,pszKey);
    }
    return NULL;
} /* GLAdd() */


/***    GLDelete - Delete item from a list
 *
 *  NOTE: See glist.h for entry/exit conditions.
 */
void GLDelete(HGENERIC hgen)
{
    int         i;
    PGENERIC    pgen;
    PGENERIC    pgenPred;
    PGENLIST    pglist;

    pgen = PGENfromHGEN(hgen);
    AssertGen(pgen);

    pglist = pgen->pglist;
    AssertGList(pglist);
    AssertGen(pglist->pgenHead);
    AssertGen(pglist->pgenTail);

    //** Free value, if function available
    if (pglist->pfngldv != NULL) {
        (*pglist->pfngldv)(pgen->pv);       // Destroy value
    }

    //** Free memory for item name
    if (pgen->pszKey) {
        MemFree(pgen->pszKey);
    }

    //** Adjust forward list pointers
    if (pgen->pgenPrev == NULL) {           // At head of list
        pglist->pgenHead = pgen->pgenNext;  // Remove from forward chain
    }
    else {                                  // At middle or end of list
        AssertGen(pgen->pgenPrev);
        pgen->pgenPrev->pgenNext = pgen->pgenNext; // Remove from forward chain
    }

    //** Adjust backward list pointers
    if (pgen->pgenNext == NULL) {           // At tail of list
        pglist->pgenTail = pgen->pgenPrev;  // Remove from backward chain
    }
    else {
        AssertGen(pgen->pgenNext);
        pgen->pgenNext->pgenPrev = pgen->pgenPrev; // Remove from backward chain
    }

    //** Adjust hash links, if present
    if (pglist->pht) {
        Assert(pgen->pszKey != NULL);       // No null keys for hash tables
        i = hashString(pgen->pszKey);
        AssertHT(pglist->pht);
        if (pgen == pglist->pht->apgen[i]) { // First item on hash list
            pglist->pht->apgen[i] = pgen->pgenHash; // Update head of list
        }
        else {
            //** Search for predecessor
            for (pgenPred=pglist->pht->apgen[i];
                 pgenPred && (pgenPred->pgenHash != pgen);
                 pgenPred=pgenPred->pgenHash) {
                ; //** Keep scanning until end of list or predecessor
            }

            //** Remove item from hash list
            if (pgenPred) {
                pgenPred->pgenHash = pgen->pgenHash;
            }
            else {
                Assert(0);                  // Oops, we weren't on the list!
            }
        }
    }

    //** Item is disentagled from all lists
    ClearAssertSignature(pgen);
    MemFree(pgen);

    //** Update count of item in list
    pglist->cItems--;
    Assert(pglist->cItems >= 0);
} /* GLDelete() */


/***    GLCreateList - Create a list of
 *
 *  NOTE: See glist.h for entry/exit conditions.
 */
HGENLIST GLCreateList(void              *pv,
                      PFNGLDESTROYVALUE  pfngldv,
                      char              *pszDesc,
                      PERROR             perr)
{
    PGENLIST    pglist;

    if (!(pglist = MemAlloc(sizeof(GENLIST)))) {
        ErrSet(perr,pszGLERR_OUT_OF_MEMORY_LIST,"%s",pszDesc);
        return NULL;
    }

    SetAssertSignature(pglist,sigGENLIST);
    pglist->pgenHead  = NULL;           // Empty list
    pglist->pgenTail  = NULL;           // Empty list
    pglist->pvDefault = pv;             // Default value
    pglist->pfngldv   = pfngldv;        // Value destroying function
    pglist->cItems    = 0;              // No items on list, yet
    pglist->pht       = NULL;           // No hash table, yet
//BUGBUG 11-Apr-1994 bens Add dummy node, then we can store the default
//          value there, and simplify the GLDelete code a little bit.

    return HGLfromPGL(pglist);
} /* GLCreateList() */


/***    GLCopyToList - Copy items from one list to another
 *
 *  NOTE: See glist.h for entry/exit conditions.
 */
BOOL GLCopyToList(HGENLIST            *phglistDst,
                  HGENLIST             hglistSrc,
                  PFNGLDUPLICATEVALUE  pfngldup,
                  char                *pszDesc,
                  PERROR               perr)
{
    PGENERIC    pgen;
    PGENLIST    pglistDst;
    PGENLIST    pglistSrc;
    void       *pvNew;

    pglistSrc = PGLfromHGL(hglistSrc);
    if (!pglistSrc) {                   // Source list is empty
        return TRUE;                    // Success
    }

    AssertGList(pglistSrc);             // Make sure source is valid
    pglistDst = PGLfromHGL(*phglistDst);
    if (pglistDst) {                    // Destination list exists
        AssertGList(pglistDst);         // Make sure destination is valid
        //** Value types must be compatible
        Assert(pglistDst->pfngldv == pglistSrc->pfngldv);
    }

    //** Copy items from source to destination list
    for (pgen=pglistSrc->pgenHead; pgen; pgen=pgen->pgenNext) {
        //** Create destination list if necessary
        if (!pglistDst) {               // Need to create destination
            pglistDst = GLCreateList(pglistSrc->pvDefault,
                                     pglistSrc->pfngldv,
                                     pszDesc,
                                     perr);
            if (!pglistDst) {
                return FALSE;           // Error already filled in
            }
            *phglistDst = HGLfromPGL(pglistDst); // Return to caller
        }

        //** Make sure item not already in destination list
        if (!findItem(pglistDst->pgenHead,pgen->pszKey)) {
            //** New item for destination list
            if (pfngldup != NULL) {
                if (!(pvNew = (*pfngldup)(pgen->pv))) {
                    ErrSet(perr,pszGLERR_OUT_OF_MEMORY_DUP,"%s",pszDesc);
                    return FALSE;
                }
            }
            else {
                pvNew = pgen->pv;       // Assume it is a scalar
            }
            //** Add item to destination list
            if (!GLAdd(*phglistDst,
                       pgen->pszKey,
                       pvNew,
                       pszDesc,
                       FALSE,
                       perr)) {
                if (pglistDst->pfngldv) {
                    (*pglistDst->pfngldv)(pvNew);   // Destroy value
                }
                return FALSE;
            }
        }
    }
    //** Success
    return TRUE;
} /* GLCopyToList() */


/***    GLDestroyList - Destroy a list
 *
 *  NOTE: See glist.h for entry/exit conditions.
 */
void GLDestroyList(HGENLIST hglist)
{
    PGENERIC    pgen;
    PGENERIC    pgenNext;
    PGENLIST    pglist;

    pglist = PGLfromHGL(hglist);
    AssertGList(pglist);

    //** Free the hash table, if present
    if (pglist->pht) {
        AssertHT(pglist->pht);
        MemFree(pglist->pht);
        ClearAssertSignature(pglist->pht);

        //** Tell GLDelete() not to bother updating the hash chains!
        pglist->pht = NULL;
    }

    //** Free all of the items
    for (pgen=pglist->pgenHead; pgen != NULL; ) {
        AssertGen(pgen);
        pgenNext = pgen->pgenNext;      // Save next pgen before we free pgen!
        GLDelete(HGENfromPGEN(pgen));
        pgen = pgenNext;                // Use it
    }
    ClearAssertSignature(pglist);

    //** Free the list itself
    MemFree(pglist);
} /* GLDestroyList() */


/***    GLFind - Search for item in list
 *
 *  NOTE: See glist.h for entry/exit conditions.
 */
HGENERIC GLFind(HGENLIST  hglist,
                char     *pszKey,
                char     *pszDesc,
                PERROR    perr)
{
    PGENLIST    pglist;
    PGENERIC    pgen;

    pglist = PGLfromHGL(hglist);
    AssertGList(pglist);

    //** Find item
    pgen = findItem(pglist->pgenHead, pszKey);
    if (pgen) {
        return HGENfromPGEN(pgen);
    }
    else {
        ErrSet(perr,pszGLERR_ITEM_NOT_FOUND,"%s%s",pszDesc,pszKey);
        return HGENfromPGEN(NULL);
    }
} /* GLFind() */


/***    GLFindNext - Search for next item in list
 *
 *  NOTE: See glist.h for entry/exit conditions.
 */
HGENERIC GLFindNext(HGENERIC  hgen,
                    char     *pszKey)
{
    PGENERIC    pgen;

    pgen = PGENfromHGEN(hgen);
    AssertGen(pgen);

    //** Find item, starting *after* passed in item
    return findItem(pgen->pgenNext, pszKey);
} /* GLFindNext() */


/***    GLFirstItem - Get first item from a list
 *
 *  NOTE: See glist.h for entry/exit conditions.
 */
HGENERIC GLFirstItem(HGENLIST hglist)
{
    PGENLIST    pglist;

    pglist = PGLfromHGL(hglist);
    AssertGList(pglist);
    return HGENfromPGEN(pglist->pgenHead);
} /* GLFirstItem() */


/***    GLNextItem - Get next item
 *
 *  NOTE: See glist.h for entry/exit conditions.
 */
HGENERIC GLNextItem(HGENERIC hgen)
{
    PGENERIC    pgen;

    pgen = PGENfromHGEN(hgen);
    AssertGen(pgen);
    return HGENfromPGEN(pgen->pgenNext);
} /* GLNextItem() */


/***    GLPreviousItem - Get previous item
 *
 *  NOTE: See glist.h for entry/exit conditions.
 */
HGENERIC GLPreviousItem(HGENERIC hgen)
{
    PGENERIC    pgen;

    pgen = PGENfromHGEN(hgen);
    AssertGen(pgen);
    return HGENfromPGEN(pgen->pgenPrev);
} /* GLPreviousItem() */


/***    GLGetKey - Get the item key
 *
 *  NOTE: See glist.h for entry/exit conditions.
 */
char *GLGetKey(HGENERIC hgen)
{
    PGENERIC    pgen;

    pgen = PGENfromHGEN(hgen);
    AssertGen(pgen);

    return pgen->pszKey;
} /* GLGetKey() */


/***    GLGetValue - Get the item value for a particular item
 *
 *  NOTE: See glist.h for entry/exit conditions.
 */
void *GLGetValue(HGENERIC hgen)
{
    PGENERIC    pgen;

    pgen = PGENfromHGEN(hgen);
    AssertGen(pgen);

    return pgen->pv;
} /* GLGetValue() */


/***    GLSetValue - Set the item value for a particular item
 *
 *  NOTE: See glist.h for entry/exit conditions.
 */
void GLSetValue(HGENERIC hgen, void *pv)
{
    PGENERIC    pgen;

    pgen = PGENfromHGEN(hgen);
    AssertGen(pgen);

    pgen->pv = pv;
} /* GLSetValue() */


/***    GLFindAndGetValue - Get the item value for a particular item
 *
 *  NOTE: See glist.h for entry/exit conditions.
 */
void *GLFindAndGetValue(HGENLIST  hglist,
                        char     *pszKey)
{
    PGENERIC    pgen;
    PGENLIST    pglist;

    pglist = PGLfromHGL(hglist);
    if (!pglist) {                      // No list
        return NULL;                    // Nothing to find
    }
    AssertGList(pglist);

    //** Find item
    pgen = findItem(pglist->pgenHead, pszKey);
    if (pgen) {
        AssertGen(pgen);
        return pgen->pv;
    }
    else {
        return pglist->pvDefault;
    }
} /* GLFindAndGetValue() */


//*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*
//
//  Private Functions
//
//*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*


/***    findItem - See if item exists
 *
 *  Entry:
 *      pgen    - Starting item for search
 *      pszKey  - Name to look for
 *
 *  Exit-Success:
 *      Returns PGENERIC, if item exists.
 *
 *  Exit-Failure:
 *      Returns NULL, item does not exist.
 *      ERROR structure filled in with details of error.
 */
PGENERIC findItem(PGENERIC pgenStart, char *pszKey)
{
    int         i;
    PGENERIC    pgen;
    PGENLIST    pglist;

    //** Quick out for empty list
    if (!pgenStart) {
        return NULL;
    }

    AssertGen(pgenStart);
    pglist = pgenStart->pglist;
    AssertGList(pglist);

    //** Use hash table, if present
    if (pglist->pht) {
        //** pgenStart must be pglist->pgenHead, because the hash search
        //   is undefined otherwise.
        Assert(pgenStart == pglist->pgenHead);
        Assert(pszKey != NULL);
        i = hashString(pszKey);
        AssertHT(pglist->pht);
        for (pgen=pglist->pht->apgen[i]; pgen; pgen=pgen->pgenHash) {
            if (!_stricmp(pgen->pszKey,pszKey)) {    // Got a match!
                return HGENfromPGEN(pgen);  // Return item handle
            }
        }
        //** Did not find item
        return NULL;
    }
    else if (pglist->cItems >= cHASH_THRESHOLD) {
        //** Create a hash table if keys are not NULL
        if (pszKey) {
            //** Create hash table, so *next* search is faster
            if (pglist->pht = MemAlloc(sizeof(HASHTABLE))) {
                SetAssertSignature(pglist->pht,sigHASHTABLE);
                //** Initialize table
                for (i=0; i<cHASH_ENTRIES; i++) {
                    pglist->pht->apgen[i] = NULL;   // Hash list is empty
                }
                //** Hash in all the items already in the list
                for (pgen=pglist->pgenHead; pgen; pgen=pgen->pgenNext) {
                    hashItem(pglist,pgen);
                }
            }
            else {
                //** Out of memory; No need to fail -- we can still do
                //   a linear search!
            }
        }
    }

    //** Do a linear search
    for (pgen=pgenStart; pgen != NULL; pgen = pgen->pgenNext) {
        AssertGen(pgen);
        if (pszKey && pgen->pszKey) {   // Both keys are strings
            if (!_stricmp(pgen->pszKey,pszKey)) {    // Got a match!
                return HGENfromPGEN(pgen);  // Return item handle
            }
        }
        else {                          // At least one key is NULL
            if (pszKey == pgen->pszKey) {   // Got a match
                return HGENfromPGEN(pgen);  // Return item handle
            }
        }
    }

    //** Did not find item
    return NULL;
} /* findItem() */


/***    hashString - compute hash table index from string
 *
 *  Entry:
 *      psz - String to compute hash function over
 *
 *  Exit:
 *      Returns hash table index (0..cHASH_ENTRIES-1).
 *
 *  Algorithm:
 *      We start out with a zero hash index.  For each character in
 *      the string, we rotate the previous 11 bit hash index left 3
 *      bits, and exclusive or in the new character:
 *	
 *          09876543210
 *          -----------
 *          aaabbbbbbbb     1st byte
 *          bbbbbbbbaaa     Rotate left 3 bits
 *          bbbxxxxxxxx     XOR with 2nd byte
 *          xxxxxxxxbbb     Rotate left 3 bits
 *          xxxyyyyyyyy     XOR with 3rd byte
 *          ...
 *
 *      This is designed to give us a good spread for ASCII strings.
 */
int hashString(char *psz) {
    unsigned index;                     // Avoid sign extension!

    index = (BYTE)*psz++;               // Skip rotate first time
    while (*psz) {
        //** Rotate 11-bit value left 3 bits
        index = ((index & ~HASH_BOT_MASK) >> cHASH_BOT_BITS) |
                ((index & HASH_BOT_MASK) << cHASH_TOP_BITS);
        index ^= (BYTE)*psz++;
    }

    //** Check range
    Assert((0<=index) && (index<cHASH_ENTRIES));
    return (int)index;                  // Match return type
} /* hashString() */


/***    hashItem - Hash item into hash table for generic list
 *
 *  Entry:
 *      pglist - Generic list
 *      pgen   - Item in list, needs to be hashed into hash table
 *
 *  Exit:
 *      None.  Item hashed into table
 */
void hashItem(PGENLIST pglist, PGENERIC pgen)
{
    int     i;

    AssertGList(pglist);
    AssertGen(pgen);

    //** Find the hash table entry
    if (pgen->pszKey) {
        i = hashString(pgen->pszKey);
    }
    else {
        // Doesn't make sense to do searching if some items have no key!
        Assert(0);
    }

    //** Add item to front of hash table list for this index
    AssertHT(pglist->pht);
    pgen->pgenHash = pglist->pht->apgen[i]; // Point to previous head
    pglist->pht->apgen[i] = pgen;       // New head of this hash list
} /* hashItem() */