![]() |
VR-Forces 4.0.4 Class Documentation
|
00001 /********************************************************************* 00002 ** Copyright (c) 2002 MAK Technologies, Inc. 00003 ** All rights reserved. 00004 *********************************************************************/ 00005 /********************************************************************* 00006 ** $RCSfile: dictIter.h,v $ $Revision: 1.5 $ $State: Exp $ 00007 *********************************************************************/ 00008 00011 00012 00013 #ifndef DtDictIter_H_ 00014 #define DtDictIter_H_ 00015 00019 00020 #include <QHash> 00021 00022 template<typename Key, typename Type> 00023 class DtDictIterator : public QHash<Key, Type>::iterator 00024 { 00025 public: 00026 DtDictIterator(QHash<Key, Type> &d) : myPosition(0), myCurrentDict(NULL) { addDataDict(&d); toFirst(); }; 00027 00028 ~DtDictIterator() {}; 00029 00031 unsigned int count() { return myCurrentDict->count(); }; 00032 00034 bool isEmpty() const { return myCurrentDict->count() == 0; }; 00035 00038 Type toFirst() { if (myPosition >= (unsigned int)myList.count()) myPosition = 0; return reset(myPosition); }; 00039 00041 bool remove( const Key &k ) { return ((QHash<Key, Type>*)myCurrentDict)->remove(k); } 00042 00044 Type operator++() 00045 { 00046 QHash<Key, Type>::iterator::operator++(); 00047 00050 if (*this == myCurrentDict->end()) 00051 { 00052 return nextList(); 00053 } 00054 else 00055 { 00056 return QHash<Key, Type>::iterator::operator*(); 00057 } 00058 }; 00059 00061 Type operator+=(uint j) 00062 { 00063 Type result = NULL; 00064 00065 while (j > 0) 00066 { 00067 result = ++(*this); 00068 00069 if (!result && (myPosition == myList.count())) 00070 { 00071 return NULL; 00072 } 00073 00074 j--; 00075 } 00076 00077 return result; 00078 }; 00079 00080 void operator=(const typename QHash<Key, Type>::iterator& iter) 00081 { 00082 QHash<Key, Type>::iterator::operator=(iter); 00083 } 00084 00085 Type current() 00086 { 00087 if ((myCurrentDict == NULL) || 00088 (*this == myCurrentDict->end())) 00089 00090 { 00091 return NULL; 00092 } 00093 00094 return **this; 00095 } 00096 00098 Type reset(int toPosition = 0, bool findFirstValid = true) 00099 { 00100 myPosition = toPosition; 00101 00102 if ((myPosition >= (unsigned int)myList.count()) || (myPosition < 0)) 00103 { 00104 return NULL; 00105 } 00106 00107 myCurrentDict = myList.at(myPosition); 00108 00109 if (!findFirstValid) 00110 { 00111 *this = myCurrentDict->begin(); 00112 return *myCurrentDict->begin(); 00113 } 00114 00115 Type result = NULL; 00116 00117 do 00118 { 00119 *this = myCurrentDict->begin(); 00120 00121 if (myCurrentDict->begin() == myCurrentDict->end()) 00122 { 00123 result = NULL; 00124 } 00125 else 00126 { 00127 result = *myCurrentDict->begin(); 00128 } 00129 00130 if (!result) 00131 { 00132 if (myPosition == myList.count()) 00133 { 00134 return NULL; 00135 } 00136 else 00137 { 00138 if (++myPosition == myList.count()) 00139 { 00140 return NULL; 00141 } 00142 else 00143 { 00144 myCurrentDict = myList.at(myPosition); 00145 } 00146 } 00147 } 00148 } 00149 while (!result); 00150 00151 return result; 00152 }; 00153 00155 void addDataDict(QHash<Key, Type> *dict) 00156 { 00157 if (!myCurrentDict) 00158 { 00159 myCurrentDict = dict; 00160 } 00161 00162 myList.append(dict); 00163 }; 00164 00167 Type find(const Key &key) 00168 { 00169 Type result = NULL; 00170 int limit = myList.count(); 00171 00172 for (int i = 0; i < limit; i++) 00173 { 00174 if (result = myList.at(i)->value(key)) 00175 { 00176 return result; 00177 } 00178 } 00179 00180 return NULL; 00181 } 00182 00185 Type nextList() 00186 { 00187 if (++myPosition == myList.count()) 00188 { 00189 return NULL; 00190 } 00191 else 00192 { 00193 myCurrentDict = myList.at(myPosition); 00194 return toFirst(); 00195 } 00196 }; 00197 00198 protected: 00199 QHash<Key, Type>* myCurrentDict; 00200 QList<QHash<Key, Type>* > myList; 00201 unsigned int myPosition; 00202 }; 00203 00204 #endif 00205