|
Mixe for Privacy and Anonymity in the Internet
|
00001 00002 #ifndef HASHTABLE_H 00003 #define HASHTABLE_H 00004 00005 #include "CAMutex.hpp" 00006 00007 #define HASH_EMPTY_NONE (SINT8)0 00008 #define HASH_EMPTY_FREE (SINT8)1 00009 #define HASH_EMPTY_DELETE (SINT8)2 00010 00011 00012 00013 00014 class Hashtable 00015 { 00016 public: 00017 Hashtable(UINT32 (*func1)(void *), SINT32 (*func2)(void *,void *), SINT32 capacity = 1000,float loadFactor = 0.75); 00018 ~Hashtable(); 00019 00020 /************************** standard string hash functions **************************/ 00021 static UINT32 stringHash(UINT8* str) 00022 { 00023 // djb2 algorithm 00024 00025 UINT32 hash = 5381; 00026 SINT32 c; 00027 while ((c = *str++)) 00028 { 00029 hash = ((hash << 5) + hash) + c; /* hash * 33 + c */ 00030 } 00031 return hash; 00032 } 00033 00034 static SINT32 stringCompare(UINT8* a,UINT8* *b) 00035 { 00036 return strcmp((char*)a,(char*)b); 00037 } 00038 00039 static UINT32 hashUINT64(UINT64 *a_number) 00040 { 00041 return ((*a_number) % 4294967295u); 00042 } 00043 static SINT32 compareUINT64(UINT64 *a_numberA, UINT64 *a_numberB) 00044 { 00045 return (*a_numberA == *a_numberB)? 0 : ((*a_numberA > *a_numberB)? 1 : -1); 00046 /* 00047 if (*a_numberA == *a_numberB) 00048 { 00049 return 0; 00050 } 00051 else if (*a_numberA > *a_numberB) 00052 { 00053 return 1; 00054 } 00055 else 00056 { 00057 return -1; 00058 }*/ 00059 } 00060 static UINT32 hashUINT32(UINT32 *a_number) 00061 { 00062 return *a_number; 00063 } 00064 static SINT32 compareUINT32(UINT32 *a_numberA, UINT32 *a_numberB) 00065 { 00066 return (*a_numberA == *a_numberB)? 0 : ((*a_numberA > *a_numberB)? 1 : -1); 00067 } 00068 00069 CAMutex *getMutex(); 00070 00071 bool isEmpty(); 00072 bool containsKey(void *key); 00073 void *getValue(void *key); 00074 UINT32 getSize(); 00075 UINT32 getCapacity(); 00076 00077 void* put(void *key,void *value); 00078 void *remove(void *key); 00079 00080 void clear(SINT8 keyMode = HASH_EMPTY_NONE,SINT8 valueMode = HASH_EMPTY_NONE); 00081 00082 protected: 00083 bool rehash(); 00084 struct Entry *getHashEntry(void *key); 00085 00086 SINT32 m_capacity, m_count, m_threshold, m_modCount; 00087 float m_loadFactor; 00088 struct Entry **m_table; 00089 UINT32 (*m_hashFunc)(void *); 00090 SINT32 (*m_compareFunc)(void *,void *); 00091 00092 private: 00093 CAMutex *m_pMutex; 00094 }; 00095 00096 #endif // HASHTABLE_H
1.7.6.1