| 1 | /* $Id: ccollection.h,v 1.10 2002-05-17 10:13:20 sandervl Exp $ */
 | 
|---|
| 2 | 
 | 
|---|
| 3 | /*
 | 
|---|
| 4 |  * Collection class:
 | 
|---|
| 5 |  *   provides very fast object lookup by index number
 | 
|---|
| 6 |  *
 | 
|---|
| 7 |  * Copyright 2001 Patrick Haller <patrick.haller@innotek.de>
 | 
|---|
| 8 |  *
 | 
|---|
| 9 |  *
 | 
|---|
| 10 |  * Project Odin Software License can be found in LICENSE.TXT
 | 
|---|
| 11 |  *
 | 
|---|
| 12 |  *
 | 
|---|
| 13 |  */
 | 
|---|
| 14 | 
 | 
|---|
| 15 | 
 | 
|---|
| 16 | #ifndef _CCOLLECTION_H_
 | 
|---|
| 17 | #define _CCOLLECTION_H_
 | 
|---|
| 18 | 
 | 
|---|
| 19 | 
 | 
|---|
| 20 | #include <string.h>
 | 
|---|
| 21 | #include <malloc.h>
 | 
|---|
| 22 | #include <stdlib.h>
 | 
|---|
| 23 | #include <limits.h>
 | 
|---|
| 24 | #include <math.h>
 | 
|---|
| 25 | #include <odin.h>                       /* Watcom needs max/min... */
 | 
|---|
| 26 | 
 | 
|---|
| 27 | class CCollection
 | 
|---|
| 28 | {
 | 
|---|
| 29 |     protected:
 | 
|---|
| 30 |         int iSize; /* current size of the structure in elements */
 | 
|---|
| 31 | 
 | 
|---|
| 32 |     public:
 | 
|---|
| 33 |         CCollection(int iInitialSize = 0);
 | 
|---|
| 34 |         virtual ~CCollection();
 | 
|---|
| 35 | 
 | 
|---|
| 36 |         virtual void clear() = 0; // remove all elements from the table
 | 
|---|
| 37 |                 int  getSize() { return iSize; } // query size of structure
 | 
|---|
| 38 | };
 | 
|---|
| 39 | 
 | 
|---|
| 40 | 
 | 
|---|
| 41 | typedef struct
 | 
|---|
| 42 | {
 | 
|---|
| 43 |     void* pObject;
 | 
|---|
| 44 | } INDEXLOOKUPENTRY, *PINDEXLOOKUPENTRY;
 | 
|---|
| 45 | 
 | 
|---|
| 46 | 
 | 
|---|
| 47 | class CIndexLookup : public CCollection
 | 
|---|
| 48 | {
 | 
|---|
| 49 |     public:
 | 
|---|
| 50 |         CIndexLookup(int iInitialSize = 0);
 | 
|---|
| 51 |         virtual ~CIndexLookup();
 | 
|---|
| 52 | 
 | 
|---|
| 53 |         void* addElement(int iIndex, void *pObject);
 | 
|---|
| 54 |         void* removeElement(int iIndex);
 | 
|---|
| 55 |         void* getElement(int iIndex);
 | 
|---|
| 56 |         int   isValidIndex(int iIndex);
 | 
|---|
| 57 |         void  clear();
 | 
|---|
| 58 |         int   shrink();
 | 
|---|
| 59 | 
 | 
|---|
| 60 |     protected:
 | 
|---|
| 61 | 
 | 
|---|
| 62 |         /* ensure the specified index can be held in the table */
 | 
|---|
| 63 |         virtual int ensureCapacity(int iIndex);
 | 
|---|
| 64 |         PINDEXLOOKUPENTRY reallocateData(int iShift, int iRequiredSize);
 | 
|---|
| 65 | 
 | 
|---|
| 66 |         /* pointer to array with data entries */
 | 
|---|
| 67 |         PINDEXLOOKUPENTRY pEntries;
 | 
|---|
| 68 | 
 | 
|---|
| 69 |         /* offset is subtracted from any index into the table */
 | 
|---|
| 70 |         /* this also is the lowest valid index in the table */
 | 
|---|
| 71 |         int iOffset;
 | 
|---|
| 72 |         int iUsedOffset;
 | 
|---|
| 73 | 
 | 
|---|
| 74 |         /* this is the highest valid index in the table */
 | 
|---|
| 75 |         int iHigh;
 | 
|---|
| 76 |         int iUsedHigh;
 | 
|---|
| 77 | 
 | 
|---|
| 78 |         /* are the iUsedOffset and iUserHigh initialized? */
 | 
|---|
| 79 |         int iInitialized;
 | 
|---|
| 80 | };
 | 
|---|
| 81 | 
 | 
|---|
| 82 | 
 | 
|---|
| 83 | class CIndexLookupLimit : public CIndexLookup
 | 
|---|
| 84 | {
 | 
|---|
| 85 |     public:
 | 
|---|
| 86 |         CIndexLookupLimit(int iHardLimitLow = 0,
 | 
|---|
| 87 |                           int iHardLimitHigh = 0);
 | 
|---|
| 88 |         virtual ~CIndexLookupLimit();
 | 
|---|
| 89 | 
 | 
|---|
| 90 | 
 | 
|---|
| 91 |     protected:
 | 
|---|
| 92 | 
 | 
|---|
| 93 |         /* ensure the specified index can be held in the table */
 | 
|---|
| 94 |         int ensureCapacity(int iIndex);
 | 
|---|
| 95 | 
 | 
|---|
| 96 |         /* hard limits for the array */
 | 
|---|
| 97 |         int iLimitLow;
 | 
|---|
| 98 |         int iLimitHigh;
 | 
|---|
| 99 | };
 | 
|---|
| 100 | 
 | 
|---|
| 101 | 
 | 
|---|
| 102 | typedef struct tagLinearListEntry
 | 
|---|
| 103 | {
 | 
|---|
| 104 |     struct tagLinearListEntry *pNext;
 | 
|---|
| 105 |     struct tagLinearListEntry *pPrev;
 | 
|---|
| 106 | 
 | 
|---|
| 107 |     void *pObject;
 | 
|---|
| 108 | } LINEARLISTENTRY, *PLINEARLISTENTRY;
 | 
|---|
| 109 | 
 | 
|---|
| 110 | 
 | 
|---|
| 111 | class CLinearList : public CCollection
 | 
|---|
| 112 | {
 | 
|---|
| 113 |     public:
 | 
|---|
| 114 |         CLinearList();
 | 
|---|
| 115 |         virtual ~CLinearList();
 | 
|---|
| 116 | 
 | 
|---|
| 117 |         PLINEARLISTENTRY addFirst  (void *pObject);
 | 
|---|
| 118 |         PLINEARLISTENTRY addLast   (void *pObject);
 | 
|---|
| 119 |         PLINEARLISTENTRY addBefore (PLINEARLISTENTRY pLLE, void *pObject);
 | 
|---|
| 120 |         PLINEARLISTENTRY addAfter  (PLINEARLISTENTRY pLLE, void *pObject);
 | 
|---|
| 121 |         void             removeElement(PLINEARLISTENTRY pLLE);
 | 
|---|
| 122 |         int              removeObject(void *pObject);
 | 
|---|
| 123 |         PLINEARLISTENTRY findForward(void *pObject);
 | 
|---|
| 124 |         PLINEARLISTENTRY findForward(PLINEARLISTENTRY pLLECurrent,
 | 
|---|
| 125 |                                      void *pObject);
 | 
|---|
| 126 |         PLINEARLISTENTRY findBackward(void *pObject);
 | 
|---|
| 127 |         PLINEARLISTENTRY findBackward(PLINEARLISTENTRY pLLECurrent,
 | 
|---|
| 128 |                                       void *pObject);
 | 
|---|
| 129 |         PLINEARLISTENTRY getFirst(void) { return pFirst; }
 | 
|---|
| 130 |         PLINEARLISTENTRY getLast(void)  { return pLast;  }
 | 
|---|
| 131 |         PLINEARLISTENTRY getNext(PLINEARLISTENTRY pCurrent)
 | 
|---|
| 132 |                                         { return pCurrent->pNext; }
 | 
|---|
| 133 |         PLINEARLISTENTRY getPrevious(PLINEARLISTENTRY pCurrent)
 | 
|---|
| 134 |                                         { return pCurrent->pPrev; }
 | 
|---|
| 135 |         void             clear();
 | 
|---|
| 136 | 
 | 
|---|
| 137 |     protected:
 | 
|---|
| 138 |         PLINEARLISTENTRY add0      (void *pObject);
 | 
|---|
| 139 | 
 | 
|---|
| 140 |         PLINEARLISTENTRY pFirst;
 | 
|---|
| 141 |         PLINEARLISTENTRY pLast;
 | 
|---|
| 142 | };
 | 
|---|
| 143 | 
 | 
|---|
| 144 | 
 | 
|---|
| 145 | typedef struct
 | 
|---|
| 146 | {
 | 
|---|
| 147 |     char* pszName;
 | 
|---|
| 148 |     void* pObject;
 | 
|---|
| 149 | } HASHTABLEENTRY, *PHASHTABLEENTRY;
 | 
|---|
| 150 | 
 | 
|---|
| 151 | 
 | 
|---|
| 152 | class CHashtableLookup : public CCollection
 | 
|---|
| 153 | {
 | 
|---|
| 154 |     public:
 | 
|---|
| 155 |         CHashtableLookup(int iInitialSize = 23);
 | 
|---|
| 156 |         ~CHashtableLookup();
 | 
|---|
| 157 | 
 | 
|---|
| 158 |         PHASHTABLEENTRY addElement(char *pszName, void *pObject);
 | 
|---|
| 159 |         void* removeElement(char *pszName);
 | 
|---|
| 160 |         void* getElement(char *pszName);
 | 
|---|
| 161 |         void  clear();
 | 
|---|
| 162 |         void  rehash();
 | 
|---|
| 163 |         void  setSize(int iNewSize);
 | 
|---|
| 164 |         int   getElementMap(PHASHTABLEENTRY pBuffer);
 | 
|---|
| 165 |         int   getSize() { return iSize; }
 | 
|---|
| 166 |         int   getNumberOfElements() { return iElements; }
 | 
|---|
| 167 |   
 | 
|---|
| 168 |     protected:
 | 
|---|
| 169 |         void          setSize0(int iNewSize);
 | 
|---|
| 170 |         unsigned long nextPrime(unsigned long x);
 | 
|---|
| 171 | 
 | 
|---|
| 172 |         // the array of linear lists
 | 
|---|
| 173 |         CLinearList** parrLists;
 | 
|---|
| 174 | 
 | 
|---|
| 175 |         // actual number of allocated elements
 | 
|---|
| 176 |         int iElements;
 | 
|---|
| 177 | };
 | 
|---|
| 178 | 
 | 
|---|
| 179 | 
 | 
|---|
| 180 | #endif /* _CCOLLECTION_H_ */
 | 
|---|