-
Notifications
You must be signed in to change notification settings - Fork 222
/
Copy pathrtphashtable.h
345 lines (292 loc) · 8.43 KB
/
rtphashtable.h
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
/*
This file is a part of JRTPLIB
Copyright (c) 1999-2017 Jori Liesenborgs
Contact: [email protected]
This library was developed at the Expertise Centre for Digital Media
(http://www.edm.uhasselt.be), a research center of the Hasselt University
(http://www.uhasselt.be). The library is based upon work done for
my thesis at the School for Knowledge Technology (Belgium/The Netherlands).
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
IN THE SOFTWARE.
*/
#ifndef RTPHASHTABLE_H
#define RTPHASHTABLE_H
/**
* \file rtphashtable.h
*/
#include "rtperrors.h"
#include "rtpmemoryobject.h"
#ifdef RTPDEBUG
#include <iostream>
#endif // RTPDEBUG
namespace jrtplib
{
//template<class Element,int GetIndex(const Element &k),int hashsize>
template<class Element,class GetIndex,int hashsize>
class RTPHashTable : public RTPMemoryObject
{
JRTPLIB_NO_COPY(RTPHashTable)
public:
RTPHashTable(RTPMemoryManager *mgr = 0, int memtype = RTPMEM_TYPE_OTHER);
~RTPHashTable() { Clear(); }
void GotoFirstElement() { curhashelem = firsthashelem; }
void GotoLastElement() { curhashelem = lasthashelem; }
bool HasCurrentElement() { return (curhashelem == 0)?false:true; }
int DeleteCurrentElement();
Element &GetCurrentElement() { return curhashelem->GetElement(); }
int GotoElement(const Element &e);
bool HasElement(const Element &e);
void GotoNextElement();
void GotoPreviousElement();
void Clear();
int AddElement(const Element &elem);
int DeleteElement(const Element &elem);
#ifdef RTPDEBUG
void Dump();
#endif // RTPDEBUG
private:
class HashElement
{
public:
HashElement(const Element &e,int index):element(e) { hashprev = 0; hashnext = 0; listnext = 0; listprev = 0; hashindex = index; }
int GetHashIndex() { return hashindex; }
Element &GetElement() { return element; }
#ifdef RTPDEBUG
void Dump() { std::cout << "\tHash index " << hashindex << " | Element " << element << std::endl; }
#endif // RTPDEBUG
private:
int hashindex;
Element element;
public:
HashElement *hashprev,*hashnext;
HashElement *listprev,*listnext;
};
HashElement *table[hashsize];
HashElement *firsthashelem,*lasthashelem;
HashElement *curhashelem;
#ifdef RTP_SUPPORT_MEMORYMANAGEMENT
int memorytype;
#endif // RTP_SUPPORT_MEMORYMANAGEMENT
};
template<class Element,class GetIndex,int hashsize>
inline RTPHashTable<Element,GetIndex,hashsize>::RTPHashTable(RTPMemoryManager *mgr,int memtype) : RTPMemoryObject(mgr)
{
JRTPLIB_UNUSED(memtype); // possibly unused
for (int i = 0 ; i < hashsize ; i++)
table[i] = 0;
firsthashelem = 0;
lasthashelem = 0;
#ifdef RTP_SUPPORT_MEMORYMANAGEMENT
memorytype = memtype;
#endif // RTP_SUPPORT_MEMORYMANAGEMENT
}
template<class Element,class GetIndex,int hashsize>
inline int RTPHashTable<Element,GetIndex,hashsize>::DeleteCurrentElement()
{
if (curhashelem)
{
HashElement *tmp1,*tmp2;
int index;
// First, relink elements in current hash bucket
index = curhashelem->GetHashIndex();
tmp1 = curhashelem->hashprev;
tmp2 = curhashelem->hashnext;
if (tmp1 == 0) // no previous element in hash bucket
{
table[index] = tmp2;
if (tmp2 != 0)
tmp2->hashprev = 0;
}
else // there is a previous element in the hash bucket
{
tmp1->hashnext = tmp2;
if (tmp2 != 0)
tmp2->hashprev = tmp1;
}
// Relink elements in list
tmp1 = curhashelem->listprev;
tmp2 = curhashelem->listnext;
if (tmp1 == 0) // curhashelem is first in list
{
firsthashelem = tmp2;
if (tmp2 != 0)
tmp2->listprev = 0;
else // curhashelem is also last in list
lasthashelem = 0;
}
else
{
tmp1->listnext = tmp2;
if (tmp2 != 0)
tmp2->listprev = tmp1;
else // curhashelem is last in list
lasthashelem = tmp1;
}
// finally, with everything being relinked, we can delete curhashelem
RTPDelete(curhashelem,GetMemoryManager());
curhashelem = tmp2; // Set to next element in the list
}
else
return ERR_RTP_HASHTABLE_NOCURRENTELEMENT;
return 0;
}
template<class Element,class GetIndex,int hashsize>
inline int RTPHashTable<Element,GetIndex,hashsize>::GotoElement(const Element &e)
{
int index;
bool found;
index = GetIndex::GetIndex(e);
if (index >= hashsize)
return ERR_RTP_HASHTABLE_FUNCTIONRETURNEDINVALIDHASHINDEX;
curhashelem = table[index];
found = false;
while(!found && curhashelem != 0)
{
if (curhashelem->GetElement() == e)
found = true;
else
curhashelem = curhashelem->hashnext;
}
if (!found)
return ERR_RTP_HASHTABLE_ELEMENTNOTFOUND;
return 0;
}
template<class Element,class GetIndex,int hashsize>
inline bool RTPHashTable<Element,GetIndex,hashsize>::HasElement(const Element &e)
{
int index;
bool found;
HashElement *tmp;
index = GetIndex::GetIndex(e);
if (index >= hashsize)
return false;
tmp = table[index];
found = false;
while(!found && tmp != 0)
{
if (tmp->GetElement() == e)
found = true;
else
tmp = tmp->hashnext;
}
return found;
}
template<class Element,class GetIndex,int hashsize>
inline void RTPHashTable<Element,GetIndex,hashsize>::GotoNextElement()
{
if (curhashelem)
curhashelem = curhashelem->listnext;
}
template<class Element,class GetIndex,int hashsize>
inline void RTPHashTable<Element,GetIndex,hashsize>::GotoPreviousElement()
{
if (curhashelem)
curhashelem = curhashelem->listprev;
}
template<class Element,class GetIndex,int hashsize>
inline void RTPHashTable<Element,GetIndex,hashsize>::Clear()
{
HashElement *tmp1,*tmp2;
for (int i = 0 ; i < hashsize ; i++)
table[i] = 0;
tmp1 = firsthashelem;
while (tmp1 != 0)
{
tmp2 = tmp1->listnext;
RTPDelete(tmp1,GetMemoryManager());
tmp1 = tmp2;
}
firsthashelem = 0;
lasthashelem = 0;
}
template<class Element,class GetIndex,int hashsize>
inline int RTPHashTable<Element,GetIndex,hashsize>::AddElement(const Element &elem)
{
int index;
bool found;
HashElement *e,*newelem;
index = GetIndex::GetIndex(elem);
if (index >= hashsize)
return ERR_RTP_HASHTABLE_FUNCTIONRETURNEDINVALIDHASHINDEX;
e = table[index];
found = false;
while(!found && e != 0)
{
if (e->GetElement() == elem)
found = true;
else
e = e->hashnext;
}
if (found)
return ERR_RTP_HASHTABLE_ELEMENTALREADYEXISTS;
// Okay, the key doesn't exist, so we can add the new element in the hash table
newelem = RTPNew(GetMemoryManager(),memorytype) HashElement(elem,index);
if (newelem == 0)
return ERR_RTP_OUTOFMEM;
e = table[index];
table[index] = newelem;
newelem->hashnext = e;
if (e != 0)
e->hashprev = newelem;
// Now, we still got to add it to the linked list
if (firsthashelem == 0)
{
firsthashelem = newelem;
lasthashelem = newelem;
}
else // there already are some elements in the list
{
lasthashelem->listnext = newelem;
newelem->listprev = lasthashelem;
lasthashelem = newelem;
}
return 0;
}
template<class Element,class GetIndex,int hashsize>
inline int RTPHashTable<Element,GetIndex,hashsize>::DeleteElement(const Element &elem)
{
int status;
status = GotoElement(elem);
if (status < 0)
return status;
return DeleteCurrentElement();
}
#ifdef RTPDEBUG
template<class Element,class GetIndex,int hashsize>
inline void RTPHashTable<Element,GetIndex,hashsize>::Dump()
{
HashElement *e;
std::cout << "DUMPING TABLE CONTENTS:" << std::endl;
for (int i = 0 ; i < hashsize ; i++)
{
e = table[i];
while (e != 0)
{
e->Dump();
e = e->hashnext;
}
}
std::cout << "DUMPING LIST CONTENTS:" << std::endl;
e = firsthashelem;
while (e != 0)
{
e->Dump();
e = e->listnext;
}
}
#endif // RTPDEBUG
} // end namespace
#endif // RTPHASHTABLE_H