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