00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00031
00032 #ifndef __HASH_DB_HASH_H__
00033 #define __HASH_DB_HASH_H__
00034
00035 #include "direct_hash_base.h"
00036
00038 template <typename KT>
00039 class DBHash : public DHashBase<KT, KT>
00040 {
00041 protected:
00042 KT emptyKey;
00043 KT deletedKey;
00044 public:
00045 DBHash(int capacity, KT emptyKey, KT deletedKey) : DHashBase<KT, KT>(capacity)
00046 {
00047
00048 assert(emptyKey != deletedKey);
00049 this->emptyKey = emptyKey;
00050 this->deletedKey = deletedKey;
00051 clear();
00052 }
00053
00054
00055 virtual int hashCode(KT key)
00056 {
00057 return this->baseTypeHashCode((unsigned int)key);
00058 }
00059 virtual bool match(KT key, KT *slot)
00060 {
00061 return (key == *slot);
00062 }
00063 virtual bool isEmptySlot(KT *slot)
00064 {
00065 return (*slot == emptyKey);
00066 }
00067
00068 virtual bool isDeletedSlot(KT *slot)
00069 {
00070 return (*slot == deletedKey);
00071 }
00072
00073 void clear()
00074 {
00075 if (! emptyKey)
00076 this->zero();
00077 else
00078 {
00079 for (int i = 0; i < this->capacity; i++)
00080 this->data[i] = emptyKey;
00081 this->count = 0;
00082 }
00083 }
00084
00085 inline bool remove(KT key)
00086 {
00087 KT *slot;
00088 if (! search(key, &slot))
00089 return false;
00090 *slot = deletedKey;
00091 this->count--;
00092 return true;
00093 }
00094
00095 inline void put(KT key)
00096 {
00097 KT *slot;
00098 if (! search(key, &slot))
00099 {
00100 *slot = key;
00101 this->count++;
00102 }
00103 }
00104 inline void put(KT key, hash_slot_t destSlot)
00105 {
00106 KT *slot = (KT *)destSlot;
00107 if (*slot == emptyKey)
00108 {
00109 *slot = key;
00110 this->count++;
00111 }
00112 }
00113
00114 inline bool exists(KT key)
00115 {
00116 KT *slot;
00117 return search(key, &slot);
00118 }
00119 inline bool exists(KT key, hash_slot_t *destSlot)
00120 {
00121 return search(key, (KT **)destSlot);
00122 }
00123 };
00124
00125 #endif // __HASH_DS_HASH_H__