1 | /* $Id: profcollection.h,v 1.1 2001-11-22 10:43:59 phaller 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 | ~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 | ~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 | ~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 | ~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 removeElement(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 getNumberOfElements() { return iElements; }
|
---|
166 |
|
---|
167 | protected:
|
---|
168 | void setSize0(int iNewSize);
|
---|
169 | unsigned long nextPrime(unsigned long x);
|
---|
170 |
|
---|
171 | // the array of linear lists
|
---|
172 | CLinearList** parrLists;
|
---|
173 |
|
---|
174 | // actual number of allocated elements
|
---|
175 | int iElements;
|
---|
176 | };
|
---|
177 |
|
---|
178 |
|
---|
179 | #endif /* _CCOLLECTION_H_ */
|
---|