qdict.h

Go to the documentation of this file.
00001 /********************************************************************************
00002 *                                                                               *
00003 *                            Q D i c t   T h u n k                              *
00004 *                                                                               *
00005 *********************************************************************************
00006 *        Copyright (C) 2003 by Niall Douglas.   All Rights Reserved.            *
00007 *       NOTE THAT I DO NOT PERMIT ANY OF MY CODE TO BE PROMOTED TO THE GPL      *
00008 *********************************************************************************
00009 * This code is free software; you can redistribute it and/or modify it under    *
00010 * the terms of the GNU Library General Public License v2.1 as published by the  *
00011 * Free Software Foundation EXCEPT that clause 3 does not apply ie; you may not  *
00012 * "upgrade" this code to the GPL without my prior written permission.           *
00013 * Please consult the file "License_Addendum2.txt" accompanying this file.       *
00014 *                                                                               *
00015 * This code is distributed in the hope that it will be useful,                  *
00016 * but WITHOUT ANY WARRANTY; without even the implied warranty of                *
00017 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.                          *
00018 *********************************************************************************
00019 * $Id:                                                                          *
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     {   // Fast hash as QDictBase does its own hashing
00075         return str.hash();
00076         //const FXchar *txt=str.text();
00077         //const FXchar *end=txt+str.length();
00078         //FXuint h=0;
00079         //for(int n=0; end>=txt && n<6; n++, end--)
00080         //{
00081         //  h=(h & 0xffffffc0)^(h<<5)^((*end)-32);
00082         //}
00083         //return h;
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         //fxmessage("QDB delete %p\n", d);
00170         delete d;
00171     }
00172 }
00173 // Don't delete void *
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 } // namespace
00220 
00221 #endif

(C) 2002-2009 Niall Douglas. Some parts (C) to assorted authors.
Generated on Fri Nov 20 18:31:25 2009 for TnFOX by doxygen v1.4.7