-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnew.cc
355 lines (312 loc) · 9.09 KB
/
new.cc
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
#include <string.h>
#include <errno.h>
#include <sys/ipc.h>
#include <sys/sem.h>
#include <algorithm>
#include <unordered_map>
#include <iostream>
#include <expat.h>
#include <cstring>
// Define UINT32 if it's not already defined
#ifndef UINT32
#define UINT32 unsigned int
#endif
#define EQUAL 0
#define SHT_NUM_OF_SEM 2
#define SHT_SEM_FLG SEM_UNDO
#define SHT_SEM_SET -1
#define SHT_SEM_RESET 1
#ifdef XML_LARGE_SIZE
#if defined(XML_USE_MSC_EXTENSIONS) && _MSC_VER < 1400
#define XML_FMT_INT_MOD "I64"
#else
#define XML_FMT_INT_MOD "ll"
#endif
#else
#define XML_FMT_INT_MOD "l"
#endif
#ifndef XMLCALL
#if defined(_MSC_EXTENSIONS) && !defined(__BEOS__) && !defined(__CYGWIN__)
#define XMLCALL __cdecl
#elif defined(__GNUC__)
#define XMLCALL __attribute__((cdecl))
#else
#define XMLCALL
#endif
#endif
using namespace std;
class KeyHash
{
public:
size_t operator()(const char *s) const
{
unsigned int h = 0;
for (; *s; ++s)
h = 5 * h + *s;
return size_t(h);
}
};
class KeyEqual
{
public:
bool operator()(const char *s1, const char *s2) const
{
return (strcmp(s1, s2) == EQUAL);
}
};
enum HashTableError
{
SHT_FAILURE = 0,
SHT_SUCCESS,
SHT_OVERFLOW,
SHT_KEY_ALREADY_EXIST,
SHT_NO_DATA_FOUND
};
class HashTable
{
protected:
typedef struct
{
char mKey[256]; // Adjust the key size as needed
char mValue[256]; // Adjust the value size as needed
int mNextIdx;
} DataNode;
typedef struct
{
int mNextFreeIdx;
int mLastFreeIdx;
int mNumOfRecords;
} NodeMgmtQueueHeader;
typedef struct
{
NodeMgmtQueueHeader *HeaderPtr;
int *mpFreeNodeArray;
} NodeMgmtQueue;
typedef KeyHash hasher;
typedef KeyEqual keyEqual;
int *mpIndexTablePtr;
DataNode *mpDataPoolPtr;
NodeMgmtQueue *mpNodeMgmtQueuePtr;
NodeMgmtQueueHeader *mpNodeMgmtQueueHeaderPtr;
hasher hash;
keyEqual equal;
int mMaxPoolSize;
int mMaxIndex;
float mLoadFactor;
int mSemId;
struct sembuf mSet;
struct sembuf mReset;
public:
HashTable(int bucketCount, float loadFactor, int semId)
{
mMaxPoolSize = bucketCount + 2;
mLoadFactor = loadFactor;
mMaxIndex = (int)(mMaxPoolSize * mLoadFactor);
mpNodeMgmtQueuePtr = new NodeMgmtQueue();
}
int SetSemaphoreId(int semId)
{
mSemId = semget(semId, SHT_NUM_OF_SEM, 0666 | IPC_CREAT);
if (-1 == mSemId)
{
return SHT_FAILURE;
}
return SHT_SUCCESS;
}
int SetBucketCount(int bucketCount)
{
mMaxPoolSize = bucketCount + 2;
mMaxIndex = (int)(mMaxPoolSize * mLoadFactor);
return SHT_SUCCESS;
}
int SetLoadFactor(float loadFactor)
{
mLoadFactor = loadFactor;
mMaxIndex = (int)(mMaxPoolSize * mLoadFactor);
return SHT_SUCCESS;
}
int GetHashTableSize()
{
int lIndexTableSize;
int lDataPoolSize;
int lNodeMgmtQueueSize;
int lTotalSize;
lIndexTableSize = mMaxIndex * sizeof(int);
lDataPoolSize = sizeof(DataNode) * (mMaxPoolSize + 1);
lNodeMgmtQueueSize = sizeof(NodeMgmtQueueHeader) + (sizeof(int) * (mMaxPoolSize + 1));
lTotalSize = lIndexTableSize + lDataPoolSize + lNodeMgmtQueueSize;
return lTotalSize;
}
int SetHashMemoryPtr(void *PoolPtr, bool init)
{
int i;
if (PoolPtr == NULL)
{
return SHT_FAILURE;
}
mpIndexTablePtr = (int *)PoolPtr;
mpDataPoolPtr = (DataNode *)(mpIndexTablePtr + mMaxIndex);
mpNodeMgmtQueueHeaderPtr = (NodeMgmtQueueHeader *)(mpDataPoolPtr + (mMaxPoolSize + 1));
mpNodeMgmtQueuePtr->HeaderPtr = mpNodeMgmtQueueHeaderPtr;
mpNodeMgmtQueuePtr->mpFreeNodeArray = (int *)(mpNodeMgmtQueueHeaderPtr + 1);
if (init)
{
for (i = 0; i < mMaxIndex; i++)
{
mpIndexTablePtr[i] = 0;
}
for (i = 0; i <= mMaxPoolSize; i++)
{
mpDataPoolPtr[i].mNextIdx = 0;
memset(&(mpDataPoolPtr[i].mKey), 0, sizeof(mpDataPoolPtr[i].mKey));
memset(&(mpDataPoolPtr[i].mValue), 0, sizeof(mpDataPoolPtr[i].mValue));
mpNodeMgmtQueuePtr->mpFreeNodeArray[i] = i;
}
mpNodeMgmtQueuePtr->HeaderPtr->mNextFreeIdx = 1;
mpNodeMgmtQueuePtr->HeaderPtr->mLastFreeIdx = 1;
mpNodeMgmtQueuePtr->HeaderPtr->mNumOfRecords = 0;
}
return SHT_SUCCESS;
}
int Insert(const char *key, const char *value)
{
int lHashValue = 0;
int lFreeIdx = 0;
int lIndexValue = 0;
int lLastOccupiedIdx = 0;
SetSem();
lHashValue = hash(key);
lIndexValue = lHashValue % mMaxIndex;
if (mpNodeMgmtQueuePtr->mpFreeNodeArray[mpNodeMgmtQueuePtr->HeaderPtr->mNextFreeIdx] == 0)
{
UINT32 lTempIndex = mpNodeMgmtQueuePtr->HeaderPtr->mNextFreeIdx;
if (mpNodeMgmtQueuePtr->HeaderPtr->mNumOfRecords == mMaxPoolSize)
{
ResetSem();
return SHT_OVERFLOW;
}
UpdateNodeMgmtQueueIdx(&(mpNodeMgmtQueuePtr->HeaderPtr->mNextFreeIdx));
while (mpNodeMgmtQueuePtr->mpFreeNodeArray[mpNodeMgmtQueuePtr->HeaderPtr->mNextFreeIdx] == 0)
{
if (lTempIndex == mpNodeMgmtQueuePtr->HeaderPtr->mNextFreeIdx)
{
ResetSem();
return SHT_OVERFLOW;
}
UpdateNodeMgmtQueueIdx(&(mpNodeMgmtQueuePtr->HeaderPtr->mNextFreeIdx));
}
}
else
{
lFreeIdx = mpNodeMgmtQueuePtr->mpFreeNodeArray[mpNodeMgmtQueuePtr->HeaderPtr->mNextFreeIdx];
mpNodeMgmtQueuePtr->mpFreeNodeArray[mpNodeMgmtQueuePtr->HeaderPtr->mNextFreeIdx] = 0;
UpdateNodeMgmtQueueIdx(&(mpNodeMgmtQueuePtr->HeaderPtr->mNextFreeIdx));
mpNodeMgmtQueuePtr->HeaderPtr->mNumOfRecords++;
}
if (mpIndexTablePtr[lIndexValue] == 0)
{
mpIndexTablePtr[lIndexValue] = lFreeIdx;
strcpy(mpDataPoolPtr[lFreeIdx].mKey, key);
strcpy(mpDataPoolPtr[lFreeIdx].mValue, value);
mpDataPoolPtr[lFreeIdx].mNextIdx = 0;
}
else
{
lLastOccupiedIdx = mpIndexTablePtr[lIndexValue];
while (equal(mpDataPoolPtr[lLastOccupiedIdx].mKey, key) || mpDataPoolPtr[lLastOccupiedIdx].mNextIdx != 0)
{
if (equal(mpDataPoolPtr[lLastOccupiedIdx].mKey, key))
{
mpNodeMgmtQueuePtr->mpFreeNodeArray[mpNodeMgmtQueuePtr->HeaderPtr->mLastFreeIdx] =
lFreeIdx;
UpdateNodeMgmtQueueIdx(&(mpNodeMgmtQueuePtr->HeaderPtr->mLastFreeIdx));
mpNodeMgmtQueuePtr->HeaderPtr->mNumOfRecords--;
ResetSem();
return SHT_KEY_ALREADY_EXIST;
}
else
{
lLastOccupiedIdx = mpDataPoolPtr[lLastOccupiedIdx].mNextIdx;
}
}
strcpy(mpDataPoolPtr[lFreeIdx].mKey, key);
strcpy(mpDataPoolPtr[lFreeIdx].mValue, value);
mpDataPoolPtr[lLastOccupiedIdx].mNextIdx = lFreeIdx;
mpDataPoolPtr[lFreeIdx].mNextIdx = 0;
}
ResetSem();
return SHT_SUCCESS;
}
// Define other member functions here
};
class XmlParser
{
// Define XML parsing functions here
};
int main()
{
// Define your main program logic here
return 0;
}
for (UINT8 lByteIndex = 0; lByteIndex < lAddrLen; lByteIndex += 2, lWriteIdx++)
{
param[lWriteIdx] = BYTE_RESET_VAL;
if ('\0' == address[lByteIndex + 1])
{
param[lWriteIdx] = NIBBLE_FILLER;
}
else
{
switch (address[lByteIndex + 1])
{
case '*':
param[lWriteIdx] = 0xA0;
break;
case '#':
param[lWriteIdx] = 0xB0;
break;
case 'a':
param[lWriteIdx] = 0xC0;
break;
case 'b':
param[lWriteIdx] = 0xD0;
break;
case 'c':
param[lWriteIdx] = 0xE0;
break;
case 'f':
param[lWriteIdx] = 0xF0;
break;
default:
param[lWriteIdx] = ((address[lByteIndex + 1] - ASCII_OFFSET) &
LOWER_NIBBLE_MASK)
<< ONE_NIBBLE_SHIFT;
break;
}
}
switch (address[lByteIndex])
{
case '*':
param[lWriteIdx] |= 0x0A;
break;
case '#':
param[lWriteIdx] |= 0x0B;
break;
case 'a':
param[lWriteIdx] |= 0x0C;
break;
case 'b':
param[lWriteIdx] |= 0x0D;
break;
case 'c':
param[lWriteIdx] |= 0x0E;
break;
case 'f':
param[lWriteIdx] |= 0x0F;
break;
default:
param[lWriteIdx] |= ((address[lByteIndex] - ASCII_OFFSET) &
LOWER_NIBBLE_MASK);
break;
}
}