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