Go to the documentation of this file.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
00030
00031
00032 #ifndef PriorityQueueHPP
00033 #define PriorityQueueHPP
00034
00035 #include <string.h>
00036 #include <zeusbase/System/ZObject.h>
00037 #include <zeusbase/System/Queue.hpp>
00038 #include <zeusbase/System/SingleLinkedList.hpp>
00039 #include <zeusbase/System/Interfaces/IPriorityQueue.hpp>
00040
00041 #ifdef _MSC_VER
00042 #pragma warning(push, 1) // Avoid Visual Studio 6.0 level 4 and 3 compiler warnings.
00043 #endif
00044
00045 #include <stddef.h>
00046 #include <map>
00047
00048 #ifdef _MSC_VER
00049 #pragma warning(pop)
00050 #endif
00051
00052
00053 BEGIN_NAMESPACE_Zeus
00054
00055 using namespace std;
00056
00057
00061
00062 template <class TKeyType, class TValueType> class TPriorityQueue
00063 : public TZObject,
00064 public IPriorityQueue<TKeyType, TValueType>
00065 {
00066 public:
00067 typedef std::map<TKeyType, TQueue<TValueType> > TPriorityMap;
00068
00069
00075
00076 inline TPriorityQueue(bool bReplaceExisting = false)
00077 : m_EmptyObject(TValueType()),
00078 m_EmptyPriority(TKeyType())
00079 {
00080 m_iCount = 0;
00081 m_bReplaceExisting = bReplaceExisting;
00082 }
00083
00084
00091
00092 inline TPriorityQueue(const TValueType& rEmptyObject, bool bReplaceExisting = false)
00093 : m_EmptyObject(rEmptyObject),
00094 m_EmptyPriority(TKeyType())
00095 {
00096 m_iCount = 0;
00097 m_bReplaceExisting = bReplaceExisting;
00098 }
00099
00100
00103
00104 inline virtual ~TPriorityQueue()
00105 {
00106 flush();
00107 }
00108
00109
00112
00113 virtual void MQUALIFIER appendPriorityItem(const TKeyType& rKey, const TValueType& tData)
00114 {
00115 TQueue<TValueType>& rQueue = m_Map[rKey];
00116
00117 if (m_bReplaceExisting)
00118 {
00119 m_iCount -= rQueue.getCount();
00120 rQueue.flush();
00121 }
00122
00123 rQueue.appendItem(tData);
00124 ++m_iCount;
00125 }
00126
00127
00130
00131 virtual void MQUALIFIER copyToPriorityQueue(IPriorityQueue<TKeyType, TValueType>& rQueue) const
00132 {
00133 for (typename TPriorityMap::const_iterator Iterator = m_Map.begin();
00134 Iterator != m_Map.end();
00135 Iterator++)
00136 {
00137 TSingleLinkedList<TValueType> lstItems;
00138 (*Iterator).second.copyToList(lstItems);
00139
00140 IListIterator<TValueType>* pIt = lstItems.getIterator();
00141 while(pIt->hasNextItem())
00142 {
00143 rQueue.appendPriorityItem((*Iterator).first, pIt->getNextItem());
00144 }
00145 lstItems.releaseIterator(pIt);
00146 }
00147 }
00148
00149
00152
00153 inline virtual const TKeyType& MQUALIFIER peekPriority() const
00154 {
00155 typename TPriorityMap::const_iterator Iterator = m_Map.begin();
00156
00157 if (Iterator != m_Map.end())
00158 {
00159 return (*Iterator).first;
00160 }
00161 else
00162 {
00163 return m_EmptyPriority;
00164 }
00165 }
00166
00167
00170
00171 inline virtual void MQUALIFIER appendItem(const TValueType& tData)
00172 {
00173 this->appendPriorityItem(m_EmptyPriority, tData);
00174 }
00175
00176
00179
00180 inline virtual void MQUALIFIER copyToQueue(IQueue<TValueType>& rQueue) const
00181 {
00182 for (typename TPriorityMap::const_iterator Iterator = m_Map.begin();
00183 Iterator != m_Map.end();
00184 Iterator++)
00185 {
00186 (*Iterator).second.copyToQueue(rQueue);
00187 }
00188 }
00189
00190
00193
00194 inline virtual void MQUALIFIER copyToList(IList<TValueType>& rList) const
00195 {
00196 for (typename TPriorityMap::const_iterator Iterator = m_Map.begin();
00197 Iterator != m_Map.end();
00198 Iterator++)
00199 {
00200 (*Iterator).second.copyToList(rList);
00201 }
00202 }
00203
00204
00207
00208 virtual TValueType MQUALIFIER removeItem()
00209 {
00210 TValueType tRetval(m_EmptyObject);
00211 typename TPriorityMap::iterator it = m_Map.begin();
00212 if (it != m_Map.end())
00213 {
00214 tRetval = (*it).second.removeItem();
00215
00216
00217 if ((*it).second.isEmpty())
00218 {
00219 m_Map.erase((*it).first);
00220 }
00221 --m_iCount;
00222 }
00223 return tRetval;
00224 }
00225
00226
00229
00230 inline virtual Int MQUALIFIER getCount() const
00231 {
00232 return m_iCount;
00233 }
00234
00235
00238
00239 inline virtual void MQUALIFIER flush()
00240 {
00241 m_Map.clear();
00242 m_iCount = 0;
00243 }
00244
00245
00248
00249 inline virtual bool MQUALIFIER isEmpty() const
00250 {
00251 return m_Map.empty();
00252 }
00253
00254
00257
00258 inline virtual TValueType& MQUALIFIER peekItem()
00259 {
00260 typename TPriorityMap::iterator it = m_Map.begin();
00261 if (it != m_Map.end())
00262 {
00263 return (*it).second.peekItem();
00264 }
00265 else
00266 {
00267 return m_EmptyObject;
00268 }
00269 }
00270
00271
00274
00275 inline virtual const TValueType& MQUALIFIER peekItemConst() const
00276 {
00277 typename TPriorityMap::const_iterator it = m_Map.begin();
00278 if (it != m_Map.end())
00279 {
00280 return (*it).second.peekItemConst();
00281 }
00282 else
00283 {
00284 return m_EmptyObject;
00285 }
00286 }
00287
00288 protected:
00289
00290 MEMORY_MANAGER_INLINEIMPL()
00291 MEMORY_MANAGER_IMPL_END;
00292
00293 private:
00295 TValueType m_EmptyObject;
00297 TKeyType m_EmptyPriority;
00299 TPriorityMap m_Map;
00301 Int m_iCount;
00303 bool m_bReplaceExisting;
00304
00305 };
00306
00307 END_NAMESPACE_Zeus
00308
00309 #endif