JRTPLIB  3.11.1
rtphashtable.h
Go to the documentation of this file.
1 /*
2 
3  This file is a part of JRTPLIB
4  Copyright (c) 1999-2017 Jori Liesenborgs
5 
6  Contact: jori.liesenborgs@gmail.com
7 
8  This library was developed at the Expertise Centre for Digital Media
9  (http://www.edm.uhasselt.be), a research center of the Hasselt University
10  (http://www.uhasselt.be). The library is based upon work done for
11  my thesis at the School for Knowledge Technology (Belgium/The Netherlands).
12 
13  Permission is hereby granted, free of charge, to any person obtaining a
14  copy of this software and associated documentation files (the "Software"),
15  to deal in the Software without restriction, including without limitation
16  the rights to use, copy, modify, merge, publish, distribute, sublicense,
17  and/or sell copies of the Software, and to permit persons to whom the
18  Software is furnished to do so, subject to the following conditions:
19 
20  The above copyright notice and this permission notice shall be included
21  in all copies or substantial portions of the Software.
22 
23  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
24  OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
26  THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
28  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
29  IN THE SOFTWARE.
30 
31 */
32 
33 #ifndef RTPHASHTABLE_H
34 
35 #define RTPHASHTABLE_H
36 
41 #include "rtperrors.h"
42 #include "rtpmemoryobject.h"
43 
44 #ifdef RTPDEBUG
45 #include <iostream>
46 #endif // RTPDEBUG
47 
48 namespace jrtplib
49 {
50 
51 //template<class Element,int GetIndex(const Element &k),int hashsize>
52 template<class Element,class GetIndex,int hashsize>
53 class RTPHashTable : public RTPMemoryObject
54 {
55 public:
56  RTPHashTable(RTPMemoryManager *mgr = 0, int memtype = RTPMEM_TYPE_OTHER);
57  ~RTPHashTable() { Clear(); }
58 
59  void GotoFirstElement() { curhashelem = firsthashelem; }
60  void GotoLastElement() { curhashelem = lasthashelem; }
61  bool HasCurrentElement() { return (curhashelem == 0)?false:true; }
62  int DeleteCurrentElement();
63  Element &GetCurrentElement() { return curhashelem->GetElement(); }
64  int GotoElement(const Element &e);
65  bool HasElement(const Element &e);
66  void GotoNextElement();
67  void GotoPreviousElement();
68  void Clear();
69 
70  int AddElement(const Element &elem);
71  int DeleteElement(const Element &elem);
72 
73 #ifdef RTPDEBUG
74  void Dump();
75 #endif // RTPDEBUG
76 private:
77  class HashElement
78  {
79  public:
80  HashElement(const Element &e,int index):element(e) { hashprev = 0; hashnext = 0; listnext = 0; listprev = 0; hashindex = index; }
81  int GetHashIndex() { return hashindex; }
82  Element &GetElement() { return element; }
83 #ifdef RTPDEBUG
84  void Dump() { std::cout << "\tHash index " << hashindex << " | Element " << element << std::endl; }
85 #endif // RTPDEBUG
86  private:
87  int hashindex;
88  Element element;
89  public:
90  HashElement *hashprev,*hashnext;
91  HashElement *listprev,*listnext;
92  };
93 
94  HashElement *table[hashsize];
95  HashElement *firsthashelem,*lasthashelem;
96  HashElement *curhashelem;
97 #ifdef RTP_SUPPORT_MEMORYMANAGEMENT
98  int memorytype;
99 #endif // RTP_SUPPORT_MEMORYMANAGEMENT
100 };
101 
102 template<class Element,class GetIndex,int hashsize>
103 inline RTPHashTable<Element,GetIndex,hashsize>::RTPHashTable(RTPMemoryManager *mgr,int memtype) : RTPMemoryObject(mgr)
104 {
105  JRTPLIB_UNUSED(memtype); // possibly unused
106 
107  for (int i = 0 ; i < hashsize ; i++)
108  table[i] = 0;
109  firsthashelem = 0;
110  lasthashelem = 0;
111 #ifdef RTP_SUPPORT_MEMORYMANAGEMENT
112  memorytype = memtype;
113 #endif // RTP_SUPPORT_MEMORYMANAGEMENT
114 }
115 
116 template<class Element,class GetIndex,int hashsize>
117 inline int RTPHashTable<Element,GetIndex,hashsize>::DeleteCurrentElement()
118 {
119  if (curhashelem)
120  {
121  HashElement *tmp1,*tmp2;
122  int index;
123 
124  // First, relink elements in current hash bucket
125 
126  index = curhashelem->GetHashIndex();
127  tmp1 = curhashelem->hashprev;
128  tmp2 = curhashelem->hashnext;
129  if (tmp1 == 0) // no previous element in hash bucket
130  {
131  table[index] = tmp2;
132  if (tmp2 != 0)
133  tmp2->hashprev = 0;
134  }
135  else // there is a previous element in the hash bucket
136  {
137  tmp1->hashnext = tmp2;
138  if (tmp2 != 0)
139  tmp2->hashprev = tmp1;
140  }
141 
142  // Relink elements in list
143 
144  tmp1 = curhashelem->listprev;
145  tmp2 = curhashelem->listnext;
146  if (tmp1 == 0) // curhashelem is first in list
147  {
148  firsthashelem = tmp2;
149  if (tmp2 != 0)
150  tmp2->listprev = 0;
151  else // curhashelem is also last in list
152  lasthashelem = 0;
153  }
154  else
155  {
156  tmp1->listnext = tmp2;
157  if (tmp2 != 0)
158  tmp2->listprev = tmp1;
159  else // curhashelem is last in list
160  lasthashelem = tmp1;
161  }
162 
163  // finally, with everything being relinked, we can delete curhashelem
164  RTPDelete(curhashelem,GetMemoryManager());
165  curhashelem = tmp2; // Set to next element in the list
166  }
167  else
168  return ERR_RTP_HASHTABLE_NOCURRENTELEMENT;
169  return 0;
170 }
171 
172 template<class Element,class GetIndex,int hashsize>
173 inline int RTPHashTable<Element,GetIndex,hashsize>::GotoElement(const Element &e)
174 {
175  int index;
176  bool found;
177 
178  index = GetIndex::GetIndex(e);
179  if (index >= hashsize)
180  return ERR_RTP_HASHTABLE_FUNCTIONRETURNEDINVALIDHASHINDEX;
181 
182  curhashelem = table[index];
183  found = false;
184  while(!found && curhashelem != 0)
185  {
186  if (curhashelem->GetElement() == e)
187  found = true;
188  else
189  curhashelem = curhashelem->hashnext;
190  }
191  if (!found)
192  return ERR_RTP_HASHTABLE_ELEMENTNOTFOUND;
193  return 0;
194 }
195 
196 template<class Element,class GetIndex,int hashsize>
197 inline bool RTPHashTable<Element,GetIndex,hashsize>::HasElement(const Element &e)
198 {
199  int index;
200  bool found;
201  HashElement *tmp;
202 
203  index = GetIndex::GetIndex(e);
204  if (index >= hashsize)
205  return false;
206 
207  tmp = table[index];
208  found = false;
209  while(!found && tmp != 0)
210  {
211  if (tmp->GetElement() == e)
212  found = true;
213  else
214  tmp = tmp->hashnext;
215  }
216  return found;
217 }
218 
219 template<class Element,class GetIndex,int hashsize>
220 inline void RTPHashTable<Element,GetIndex,hashsize>::GotoNextElement()
221 {
222  if (curhashelem)
223  curhashelem = curhashelem->listnext;
224 }
225 
226 template<class Element,class GetIndex,int hashsize>
227 inline void RTPHashTable<Element,GetIndex,hashsize>::GotoPreviousElement()
228 {
229  if (curhashelem)
230  curhashelem = curhashelem->listprev;
231 }
232 
233 template<class Element,class GetIndex,int hashsize>
234 inline void RTPHashTable<Element,GetIndex,hashsize>::Clear()
235 {
236  HashElement *tmp1,*tmp2;
237 
238  for (int i = 0 ; i < hashsize ; i++)
239  table[i] = 0;
240 
241  tmp1 = firsthashelem;
242  while (tmp1 != 0)
243  {
244  tmp2 = tmp1->listnext;
245  RTPDelete(tmp1,GetMemoryManager());
246  tmp1 = tmp2;
247  }
248  firsthashelem = 0;
249  lasthashelem = 0;
250 }
251 
252 template<class Element,class GetIndex,int hashsize>
253 inline int RTPHashTable<Element,GetIndex,hashsize>::AddElement(const Element &elem)
254 {
255  int index;
256  bool found;
257  HashElement *e,*newelem;
258 
259  index = GetIndex::GetIndex(elem);
260  if (index >= hashsize)
261  return ERR_RTP_HASHTABLE_FUNCTIONRETURNEDINVALIDHASHINDEX;
262 
263  e = table[index];
264  found = false;
265  while(!found && e != 0)
266  {
267  if (e->GetElement() == elem)
268  found = true;
269  else
270  e = e->hashnext;
271  }
272  if (found)
273  return ERR_RTP_HASHTABLE_ELEMENTALREADYEXISTS;
274 
275  // Okay, the key doesn't exist, so we can add the new element in the hash table
276 
277  newelem = RTPNew(GetMemoryManager(),memorytype) HashElement(elem,index);
278  if (newelem == 0)
279  return ERR_RTP_OUTOFMEM;
280 
281  e = table[index];
282  table[index] = newelem;
283  newelem->hashnext = e;
284  if (e != 0)
285  e->hashprev = newelem;
286 
287  // Now, we still got to add it to the linked list
288 
289  if (firsthashelem == 0)
290  {
291  firsthashelem = newelem;
292  lasthashelem = newelem;
293  }
294  else // there already are some elements in the list
295  {
296  lasthashelem->listnext = newelem;
297  newelem->listprev = lasthashelem;
298  lasthashelem = newelem;
299  }
300  return 0;
301 }
302 
303 template<class Element,class GetIndex,int hashsize>
304 inline int RTPHashTable<Element,GetIndex,hashsize>::DeleteElement(const Element &elem)
305 {
306  int status;
307 
308  status = GotoElement(elem);
309  if (status < 0)
310  return status;
311  return DeleteCurrentElement();
312 }
313 
314 #ifdef RTPDEBUG
315 template<class Element,class GetIndex,int hashsize>
316 inline void RTPHashTable<Element,GetIndex,hashsize>::Dump()
317 {
318  HashElement *e;
319 
320  std::cout << "DUMPING TABLE CONTENTS:" << std::endl;
321  for (int i = 0 ; i < hashsize ; i++)
322  {
323  e = table[i];
324  while (e != 0)
325  {
326  e->Dump();
327  e = e->hashnext;
328  }
329  }
330 
331  std::cout << "DUMPING LIST CONTENTS:" << std::endl;
332  e = firsthashelem;
333  while (e != 0)
334  {
335  e->Dump();
336  e = e->listnext;
337  }
338 }
339 #endif // RTPDEBUG
340 
341 } // end namespace
342 
343 #endif // RTPHASHTABLE_H
344 
Definition: rtpfaketransmitter.h:64
#define RTPMEM_TYPE_OTHER
Used to indicate a general kind of memory block.
Definition: rtpmemorymanager.h:45