00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #ifndef QDICT_H
00023 #define QDICT_H
00024
00025 #include "qdictbase.h"
00026
00027 namespace FX {
00028
00069 template<class type> class QDict : public QDictBase<FXString, type>
00070 {
00071 typedef QDictBase<FXString, type> Base;
00072 bool checkcase;
00073 FXuint hash(const FXString &str) const
00074 {
00075 return str.hash();
00076
00077
00078
00079
00080
00081
00082
00083
00084 }
00085 public:
00086 enum { HasSlowKeyCompare=true };
00088 explicit QDict(int size=13, bool caseSensitive=true, bool wantAutoDel=false)
00089 : checkcase(caseSensitive), Base(size, wantAutoDel)
00090 {
00091 }
00092 QDict(const QDict<type> &o) : checkcase(o.checkcase), Base(o) { }
00093 ~QDict() { Base::clear(); }
00094 FXADDMOVEBASECLASS(QDict, Base)
00096 bool caseSensitive() const throw() { return checkcase; }
00098 void setCaseSensitive(bool c) throw() { checkcase=c; }
00100 void insert(const FXString &k, const type *d)
00101 {
00102 if(checkcase)
00103 Base::insert(hash(k), k, const_cast<type *>(d));
00104 else
00105 {
00106 FXString key(k);
00107 key.lower();
00108 Base::insert(hash(key), key, const_cast<type *>(d));
00109 }
00110 }
00112 void replace(const FXString &k, const type *d)
00113 {
00114 if(checkcase)
00115 Base::replace(hash(k), k, const_cast<type *>(d));
00116 else
00117 {
00118 FXString key(k);
00119 key.lower();
00120 Base::replace(hash(key), key, const_cast<type *>(d));
00121 }
00122 }
00124 bool remove(const FXString &k)
00125 {
00126 if(checkcase)
00127 return Base::remove(hash(k), k);
00128 else
00129 {
00130 FXString key(k);
00131 key.lower();
00132 return Base::remove(hash(key), key);
00133 }
00134 }
00136 type *take(const FXString &k)
00137 {
00138 if(checkcase)
00139 return Base::take(hash(k), k);
00140 else
00141 {
00142 FXString key(k);
00143 key.lower();
00144 return Base::take(hash(key), key);
00145 }
00146 }
00148 type *find(const FXString &k) const
00149 {
00150 if(checkcase)
00151 return Base::find(hash(k), k);
00152 else
00153 {
00154 FXString key(k);
00155 key.lower();
00156 return Base::find(hash(key), key);
00157 }
00158 }
00160 type *operator[](const FXString &k) const { return find(k); }
00161 protected:
00162 virtual void deleteItem(type *d);
00163 };
00164
00165 template<class type> inline void QDict<type>::deleteItem(type *d)
00166 {
00167 if(Base::autoDelete())
00168 {
00169
00170 delete d;
00171 }
00172 }
00173
00174 template<> inline void QDict<void>::deleteItem(void *)
00175 {
00176 }
00177
00182 template<class type> class QDictIterator : public QDictBaseIterator<FXString, type>
00183 {
00184 public:
00185 QDictIterator() { }
00186 QDictIterator(const QDict<type> &d) : QDictBaseIterator<FXString, type>(d) { }
00187 };
00188
00190 template<class type> FXStream &operator<<(FXStream &s, const QDict<type> &i)
00191 {
00192 FXuint mysize=i.count();
00193 s << mysize;
00194 for(QDictIterator<type> it(i); it.current(); ++it)
00195 {
00196 s << it.currentKey();
00197 s << *it.current();
00198 }
00199 return s;
00200 }
00202 template<class type> FXStream &operator>>(FXStream &s, QDict<type> &i)
00203 {
00204 FXuint mysize;
00205 s >> mysize;
00206 i.clear();
00207 FXString key;
00208 for(FXuint n=0; n<mysize; n++)
00209 {
00210 type *item;
00211 FXERRHM(item=new type);
00212 s >> key;
00213 s >> *item;
00214 i.insert(key, item);
00215 }
00216 return s;
00217 }
00218
00219 }
00220
00221 #endif