source: trunk/include/ccollection.h@ 5830

Last change on this file since 5830 was 5830, checked in by phaller, 24 years ago

.

File size: 4.4 KB
Line 
1/* $Id: ccollection.h,v 1.1 2001-05-30 01:30:56 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
26
27class 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
41typedef struct
42{
43 void* pObject;
44} INDEXLOOKUPENTRY, *PINDEXLOOKUPENTRY;
45
46
47class 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
79
80class CIndexLookupLimit : public CIndexLookup
81{
82 public:
83 CIndexLookupLimit(int iHardLimitLow = 0,
84 int iHardLimitHigh = 0);
85 ~CIndexLookupLimit();
86
87
88 protected:
89
90 /* ensure the specified index can be held in the table */
91 int ensureCapacity(int iIndex);
92
93 /* hard limits for the array */
94 int iLimitLow;
95 int iLimitHigh;
96};
97
98
99typedef struct tagLinearListEntry
100{
101 struct tagLinearListEntry *pNext;
102 struct tagLinearListEntry *pPrev;
103
104 void *pObject;
105} LINEARLISTENTRY, *PLINEARLISTENTRY;
106
107
108class CLinearList : public CCollection
109{
110 public:
111 CLinearList();
112 ~CLinearList();
113
114 PLINEARLISTENTRY addFirst (void *pObject);
115 PLINEARLISTENTRY addLast (void *pObject);
116 PLINEARLISTENTRY addBefore (PLINEARLISTENTRY pLLE, void *pObject);
117 PLINEARLISTENTRY addAfter (PLINEARLISTENTRY pLLE, void *pObject);
118 void removeElement(PLINEARLISTENTRY pLLE);
119 int removeElement(void *pObject);
120 PLINEARLISTENTRY findForward(void *pObject);
121 PLINEARLISTENTRY findForward(PLINEARLISTENTRY pLLECurrent,
122 void *pObject);
123 PLINEARLISTENTRY findBackward(void *pObject);
124 PLINEARLISTENTRY findBackward(PLINEARLISTENTRY pLLECurrent,
125 void *pObject);
126 PLINEARLISTENTRY getFirst(void) { return pFirst; }
127 PLINEARLISTENTRY getLast(void) { return pLast; }
128 PLINEARLISTENTRY getNext(PLINEARLISTENTRY pCurrent)
129 { return pCurrent->pNext; }
130 PLINEARLISTENTRY getPrevious(PLINEARLISTENTRY pCurrent)
131 { return pCurrent->pPrev; }
132 void clear();
133
134 protected:
135 PLINEARLISTENTRY add0 (void *pObject);
136
137 PLINEARLISTENTRY pFirst;
138 PLINEARLISTENTRY pLast;
139};
140
141
142typedef struct
143{
144 char* pszName;
145 void* pObject;
146} HASHTABLEENTRY, *PHASHTABLEENTRY;
147
148
149class CHashtableLookup : public CCollection
150{
151 public:
152 CHashtableLookup(int iInitialSize = 23);
153 ~CHashtableLookup();
154
155 PHASHTABLEENTRY addElement(char *pszName, void *pObject);
156 void* removeElement(char *pszName);
157 void* getElement(char *pszName);
158 void clear();
159 void rehash();
160
161 protected:
162 unsigned long nextPrime(unsigned long x);
163 friend unsigned long static _Optlink hashcode(int iRing, char *pszName);
164
165 // the array of linear lists
166 CLinearList** parrLists;
167
168 // actual number of allocated elements
169 int iElements;
170};
171
172
173#endif /* _CCOLLECTION_H_ */
Note: See TracBrowser for help on using the repository browser.