qint64dict.h

Go to the documentation of this file.
00001 /********************************************************************************
00002 *                                                                               *
00003 *                       Q I n t 6 4 D i c t   T h u n k                         *
00004 *                                                                               *
00005 *********************************************************************************
00006 *        Copyright (C) 2004 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 QINT64DICT_H
00023 #define QINT64DICT_H
00024 
00025 #include "qdictbase.h"
00026 
00027 namespace FX {
00028 
00039 template<class type> class QInt64Dict : public QDictBase<FXlong, type>
00040 {
00041     typedef QDictBase<FXlong, type> Base;
00042     inline FXuint hash(FXlong k) const throw() { return (FXuint)((k>>32)^(k & 0xffffffff)); }
00043 public:
00045     explicit QInt64Dict(int size=13, bool wantAutoDel=false) : Base(size, wantAutoDel) { }
00046     ~QInt64Dict() { Base::clear(); }
00047     FXADDMOVEBASECLASS(QInt64Dict, Base)
00049     void insert(FXlong k, const type *d)
00050     {
00051         Base::insert(hash(k), k, const_cast<type *>(d));
00052     }
00054     void replace(FXlong k, const type *d)
00055     {
00056         Base::replace(hash(k), k, const_cast<type *>(d));
00057     }
00059     bool remove(FXlong k)
00060     {
00061         return Base::remove(hash(k), k);
00062     }
00064     type *take(FXlong k)
00065     {
00066         return Base::take(hash(k), k);
00067     }
00069     type *find(FXlong k) const
00070     {
00071         return Base::find(hash(k), k);
00072     }
00074     type *operator[](FXlong k) const { return find(k); }
00075 protected:
00076     virtual void deleteItem(type *d);
00077 };
00078 
00079 template<class type> inline void QInt64Dict<type>::deleteItem(type *d)
00080 {
00081     if(Base::autoDelete())
00082     {
00083         //fxmessage("QDB delete %p\n", d);
00084         delete d;
00085     }
00086 }
00087 // Don't delete void *
00088 template<> inline void QInt64Dict<void>::deleteItem(void *)
00089 {
00090 }
00091 
00096 template<class type> class QInt64DictIterator : public QDictBaseIterator<FXlong, type>
00097 {
00098 public:
00099     QInt64DictIterator() { }
00100     QInt64DictIterator(const QInt64Dict<type> &d) : QDictBaseIterator<FXlong, type>(d) { }
00101 };
00102 
00104 template<class type> FXStream &operator<<(FXStream &s, const QInt64Dict<type> &i)
00105 {
00106     FXuint mysize=i.count();
00107     s << mysize;
00108     for(QInt64DictIterator<type> it(i); it.current(); ++it)
00109     {
00110         s << it.currentKey();
00111         s << *it.current();
00112     }
00113     return s;
00114 }
00116 template<class type> FXStream &operator>>(FXStream &s, QInt64Dict<type> &i)
00117 {
00118     FXuint mysize;
00119     s >> mysize;
00120     i.clear();
00121     FXlong key;
00122     for(FXuint n=0; n<mysize; n++)
00123     {
00124         type *item;
00125         FXERRHM(item=new type);
00126         s >> key;
00127         s >> *item;
00128         i.insert(key, item);
00129     }
00130     return s;
00131 }
00132 
00133 } // namespace
00134 
00135 #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