Mixe for Privacy and Anonymity in the Internet
Hashtable.hpp
Go to the documentation of this file.
1 /*
2 Copyright (c) 2000, The JAP-Team
3 All rights reserved.
4 Redistribution and use in source and binary forms, with or without modification,
5 are permitted provided that the following conditions are met:
6 
7  - Redistributions of source code must retain the above copyright notice,
8  this list of conditions and the following disclaimer.
9 
10  - Redistributions in binary form must reproduce the above copyright notice,
11  this list of conditions and the following disclaimer in the documentation and/or
12  other materials provided with the distribution.
13 
14  - Neither the name of the University of Technology Dresden, Germany nor the names of its contributors
15  may be used to endorse or promote products derived from this software without specific
16  prior written permission.
17 
18 
19 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS
20 OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
21 AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS
22 BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
24 OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25 IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE
27 */
28 
29 #ifndef HASHTABLE_H
30 #define HASHTABLE_H
31 #ifdef PAYMENT
32 #include "CAMutex.hpp"
33 
34 #define HASH_EMPTY_NONE (SINT8)0
35 #define HASH_EMPTY_FREE (SINT8)1
36 #define HASH_EMPTY_DELETE (SINT8)2
37 
38 
39 
40 
41 class Hashtable
42 {
43  public:
44  Hashtable(UINT32 (*func1)(void *), SINT32 (*func2)(void *,void *), SINT32 capacity = 1000,float loadFactor = 0.75);
45  ~Hashtable();
46 
47  /************************** standard string hash functions **************************/
48  static UINT32 stringHash(UINT8* str)
49  {
50  // djb2 algorithm
51 
52  UINT32 hash = 5381;
53  SINT32 c;
54  while ((c = *str++))
55  {
56  hash = ((hash << 5) + hash) + c; /* hash * 33 + c */
57  }
58  return hash;
59  }
60 
61  static SINT32 stringCompare(UINT8* a,UINT8* *b)
62  {
63  return strcmp((char*)a,(char*)b);
64  }
65 
66  static UINT32 hashUINT64(UINT64 *a_number)
67  {
68  return ((*a_number) % 4294967295u);
69  }
70  static SINT32 compareUINT64(UINT64 *a_numberA, UINT64 *a_numberB)
71  {
72  return (*a_numberA == *a_numberB)? 0 : ((*a_numberA > *a_numberB)? 1 : -1);
73  /*
74  if (*a_numberA == *a_numberB)
75  {
76  return 0;
77  }
78  else if (*a_numberA > *a_numberB)
79  {
80  return 1;
81  }
82  else
83  {
84  return -1;
85  }*/
86  }
87  static UINT32 hashUINT32(UINT32 *a_number)
88  {
89  return *a_number;
90  }
91  static SINT32 compareUINT32(UINT32 *a_numberA, UINT32 *a_numberB)
92  {
93  return (*a_numberA == *a_numberB)? 0 : ((*a_numberA > *a_numberB)? 1 : -1);
94  }
95 
96  CAMutex *getMutex();
97 
98  bool isEmpty();
99  bool containsKey(void *key);
100  void *getValue(void *key);
101  UINT32 getSize();
102  UINT32 getCapacity();
103 
104  void* put(void *key,void *value);
105  void *remove(void *key);
106 
107  void clear(SINT8 keyMode = HASH_EMPTY_NONE,SINT8 valueMode = HASH_EMPTY_NONE);
108 
109  protected:
110  bool rehash();
111  struct Entry *getHashEntry(void *key);
112 
113  SINT32 m_capacity, m_count, m_threshold, m_modCount;
114  float m_loadFactor;
115  struct Entry **m_table;
116  UINT32 (*m_hashFunc)(void *);
117  SINT32 (*m_compareFunc)(void *,void *);
118 
119  private:
120  CAMutex *m_pMutex;
121 };
122 #endif
123 #endif // HASHTABLE_H
signed int SINT32
Definition: basetypedefs.h:132
signed char SINT8
Definition: basetypedefs.h:136
unsigned char UINT8
Definition: basetypedefs.h:135
unsigned int UINT32
Definition: basetypedefs.h:131
Definition: Hashtable.cpp:43