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_DBB_HASH_H__
00033 #define __HASH_DBB_HASH_H__
00034
00035 #include "direct_hash_base.h"
00036
00037 template <typename KT, typename VT> struct dbb_hash_slot
00038 {
00039 KT key;
00040 VT value;
00041 };
00042
00044 template <typename KT, typename VT>
00045 class DBBHash : public DHashBase<KT, struct dbb_hash_slot<KT, VT> >
00046 {
00047 protected:
00048 KT emptyKey;
00049 public:
00050 DBBHash(int capacity, KT emptyKey) : DHashBase<KT, struct dbb_hash_slot<KT, VT> >(capacity)
00051 {
00052 this->emptyKey = emptyKey;
00053 clear();
00054 }
00055
00056
00057 virtual int hashCode(KT key)
00058 {
00059 return this->baseTypeHashCode((unsigned int)key);
00060 }
00061 virtual bool match(KT key, struct dbb_hash_slot<KT, VT> *slot)
00062 {
00063 return (key == slot->key);
00064 }
00065 virtual bool isEmptySlot(struct dbb_hash_slot<KT, VT> *slot)
00066 {
00067 return (slot->key == emptyKey);
00068 }
00069
00070 void clear()
00071 {
00072 if (! emptyKey)
00073 this->zero();
00074 else
00075 {
00076 for (int i = 0; i < this->capacity; i++)
00077 this->data[i].key = emptyKey;
00078 this->count = 0;
00079 }
00080 }
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094 inline void put(KT key, VT value)
00095 {
00096 struct dbb_hash_slot<KT, VT> *slot;
00097 bool found = search(key, &slot);
00098 if (! found)
00099 {
00100 slot->key = key;
00101 this->count++;
00102 }
00103 slot->value = value;
00104 }
00105 inline void put(KT key, hash_slot_t destSlot, VT value)
00106 {
00107 struct dbb_hash_slot<KT, VT> *slot = (struct dbb_hash_slot<KT, VT> *)destSlot;
00108 if (slot->key == emptyKey)
00109 {
00110 slot->key = key;
00111 this->count++;
00112 }
00113 slot->value = value;
00114 }
00115
00116 inline bool get(KT key, VT *value)
00117 {
00118 struct dbb_hash_slot<KT, VT> *slot;
00119 bool found = search(key, &slot);
00120 if (found)
00121 *value = slot->value;
00122 return found;
00123 }
00124 bool get(KT key, hash_slot_t *destSlot, VT *value)
00125 {
00126 struct dbb_hash_slot<KT, VT> **slot = (struct dbb_hash_slot<KT, VT> **)destSlot;
00127 bool found = search(key, slot);
00128 if (found)
00129 *value = (*slot)->value;
00130 return found;
00131 }
00132 inline bool exists(KT key)
00133 {
00134 struct dbb_hash_slot<KT, VT> *slot;
00135 return search(key, &slot);
00136 }
00137 inline bool exists(KT key, hash_slot_t *destSlot)
00138 {
00139 return search(key, (struct dbb_hash_slot<KT, VT> **)destSlot);
00140 }
00141 };
00142
00143 #endif // __HASH_DBB_HASH_H__