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 #ifndef __SEAL_PRTQUEUE_H__
00030 #define __SEAL_PRTQUEUE_H__
00031
00032 #include "dataptr.h"
00033 #include "node.h"
00034 #include "collection.h"
00035
00036 namespace seal
00037 {
00038
00045 template <typename PrtType, typename DataType>
00046 class PriorityQueue : public Collection<DataType>
00047 {
00048 public:
00049
00052 class Iterator : public AbstractIterator<DataType>
00053 {
00054
00055 friend class PriorityQueue<PrtType, DataType>;
00056
00057 public:
00058
00061 Iterator() : AbstractIterator<DataType>(), _firstPtr(NULL), _currPtr(NULL) {}
00062
00065 virtual ~Iterator() {}
00066
00069 virtual bool hasNext(void) { if (_currPtr != NULL) return true; else return false; }
00070
00073 virtual DataType next(void) throw (OutOfBoundsException);
00074
00077 virtual void reset(void) { _currPtr = _firstPtr; }
00078
00079 private:
00080
00083 Iterator(PrtQueueNode<PrtType, DataType> *fPtr)
00084 : AbstractIterator<DataType>(), _firstPtr(fPtr), _currPtr(fPtr)
00085 {}
00086
00087 PrtQueueNode<PrtType, DataType> *_firstPtr, *_currPtr;
00088 };
00089
00090 public:
00093 PriorityQueue(void);
00094
00097 PriorityQueue(const PriorityQueue<PrtType, DataType> &q);
00098
00101 ~PriorityQueue();
00102
00105 virtual DataType& first(void) throw (EmptyCollectionException);
00106
00109 virtual const DataType& first(void) const throw (EmptyCollectionException);
00110
00113 virtual DataType& last(void) throw (EmptyCollectionException);
00114
00117 virtual const DataType& last(void) const throw (EmptyCollectionException);
00118
00122 DataType& first(const PrtType &p) throw (MissingPriorityException);
00123
00127 const DataType& first(const PrtType &p) const throw (MissingPriorityException);
00128
00132 DataType& last(const PrtType &p) throw (MissingPriorityException);
00133
00137 const DataType& last(const PrtType &p) const throw (MissingPriorityException);
00138
00141 bool hasPriority(const PrtType &prt);
00142
00145 virtual bool isEmpty(void) const { if (*_sizePtr == 0) return true; else return false; }
00146
00149 virtual unsigned int size(void) const { return *_sizePtr; }
00150
00154 virtual AbstractIterator<DataType>* iteratorPtr(void) const
00155 {
00156 return new typename PriorityQueue<PrtType, DataType>::Iterator(_firstNodePtr->data);
00157 }
00158
00161 typename PriorityQueue<PrtType, DataType>::Iterator iterator(void) const
00162 {
00163 return typename PriorityQueue<PrtType, DataType>::Iterator(_firstNodePtr->data);
00164 }
00165
00172 void enque(const PrtType &prt, const DataType &data);
00173
00176 inline void deque(void);
00177
00180 void purge(void);
00181
00184 void operator=(const PriorityQueue<PrtType, DataType> &map);
00185
00186 private:
00187 unsigned int *_sizePtr, *_refCountPtr;
00188 DataPtr< PrtQueueNode<PrtType, DataType> > *_firstNodePtr, *_lastNodePtr;
00189 };
00190
00191 template <typename PrtType, typename DataType>
00192 PriorityQueue<PrtType, DataType>::PriorityQueue(void)
00193 : Collection<DataType>()
00194 {
00195 _sizePtr = new unsigned int; *_sizePtr = 0;
00196 _refCountPtr = new unsigned int; *_refCountPtr = 1;
00197
00198 _firstNodePtr = new DataPtr< PrtQueueNode<PrtType, DataType> >();
00199 _firstNodePtr->data = NULL;
00200
00201 _lastNodePtr = new DataPtr< PrtQueueNode<PrtType, DataType> >();
00202 _lastNodePtr->data = NULL;
00203 }
00204
00205 template <typename PrtType, typename DataType>
00206 PriorityQueue<PrtType, DataType>::PriorityQueue(const PriorityQueue<PrtType, DataType> &q)
00207 : Collection<DataType>(),
00208 _sizePtr(q._sizePtr), _refCountPtr(q._refCountPtr),
00209 _firstNodePtr(q._firstNodePtr), _lastNodePtr(q._lastNodePtr)
00210 {
00211 (*_refCountPtr)++;
00212 }
00213
00214 template <typename PrtType, typename DataType>
00215 PriorityQueue<PrtType, DataType>::~PriorityQueue()
00216 {
00217 if (*_refCountPtr > 1)
00218 (*_refCountPtr)--;
00219 else
00220 {
00221 purge();
00222 delete _sizePtr; delete _refCountPtr;
00223 delete _firstNodePtr; delete _lastNodePtr;
00224 }
00225 }
00226
00227 template <typename PrtType, typename DataType>
00228 void PriorityQueue<PrtType, DataType>::operator=(const PriorityQueue<PrtType, DataType> &q)
00229 {
00230 if (_refCountPtr == q._refCountPtr)
00231 return;
00232
00233 if (*_refCountPtr > 1)
00234 (*_refCountPtr)--;
00235 else
00236 {
00237 purge();
00238 delete _sizePtr; delete _refCountPtr;
00239 delete _firstNodePtr; delete _lastNodePtr;
00240 }
00241
00242 _sizePtr = q._sizePtr;
00243 _refCountPtr = q._refCountPtr;
00244 _firstNodePtr = q._firstNodePtr;
00245 _lastNodePtr = q._lastNodePtr;
00246
00247 (*_refCountPtr)++;
00248 }
00249
00250 template <typename PrtType, typename DataType>
00251 DataType& PriorityQueue<PrtType, DataType>::first(void) throw (EmptyCollectionException)
00252 {
00253 if (*_sizePtr == 0)
00254 throw EmptyCollectionException();
00255
00256 return _firstNodePtr->data->data;
00257 }
00258
00259 template <typename PrtType, typename DataType>
00260 DataType& PriorityQueue<PrtType, DataType>::last(void) throw (EmptyCollectionException)
00261 {
00262 if (*_sizePtr == 0)
00263 throw EmptyCollectionException();
00264
00265 return _lastNodePtr->data->data;
00266 }
00267
00268 template <typename PrtType, typename DataType>
00269 const DataType& PriorityQueue<PrtType, DataType>::first(void) const throw (EmptyCollectionException)
00270 {
00271 if (*_sizePtr == 0)
00272 throw EmptyCollectionException();
00273
00274 return _firstNodePtr->data->data;
00275 }
00276
00277 template <typename PrtType, typename DataType>
00278 const DataType& PriorityQueue<PrtType, DataType>::last(void) const throw (EmptyCollectionException)
00279 {
00280 if (*_sizePtr == 0)
00281 throw EmptyCollectionException();
00282
00283 return _lastNodePtr->data->data;
00284 }
00285
00286 template <typename PrtType, typename DataType>
00287 DataType& PriorityQueue<PrtType, DataType>::first(const PrtType &p) throw (MissingPriorityException)
00288 {
00289 if (*_sizePtr == 0)
00290 throw MissingPriorityException();
00291
00292 PrtQueueNode<PrtType, DataType> *nodePtr = _firstNodePtr->data;
00293 while (nodePtr->prt != p && nodePtr != NULL)
00294 {
00295 nodePtr = nodePtr->nextPtr;
00296 if (nodePtr == NULL)
00297 break;
00298 }
00299
00300 if (nodePtr == NULL)
00301 throw MissingPriorityException();
00302
00303 return nodePtr->data;
00304 }
00305
00306 template <typename PrtType, typename DataType>
00307 const DataType& PriorityQueue<PrtType, DataType>::first(const PrtType &p) const throw (MissingPriorityException)
00308 {
00309 if (*_sizePtr == 0)
00310 throw MissingPriorityException();
00311
00312 PrtQueueNode<PrtType, DataType> *nodePtr = _firstNodePtr->data;
00313 while (nodePtr->prt != p && nodePtr != NULL)
00314 {
00315 nodePtr = nodePtr->nextPtr;
00316 if (nodePtr == NULL)
00317 break;
00318 }
00319
00320 if (nodePtr == NULL)
00321 throw MissingPriorityException();
00322
00323 return nodePtr->data;
00324 }
00325
00326 template <typename PrtType, typename DataType>
00327 DataType& PriorityQueue<PrtType, DataType>::last(const PrtType &p) throw (MissingPriorityException)
00328 {
00329 if (*_sizePtr == 0)
00330 throw MissingPriorityException();
00331
00332 PrtQueueNode<PrtType, DataType> *nodePtr = _firstNodePtr->data;
00333 while (nodePtr->prt != p && nodePtr != NULL)
00334 {
00335 nodePtr = nodePtr->nextPtr;
00336 if (nodePtr == NULL)
00337 break;
00338 }
00339
00340 if (nodePtr == NULL)
00341 throw MissingPriorityException();
00342
00343 while (nodePtr->prt == p && nodePtr != NULL)
00344 {
00345 nodePtr = nodePtr->nextPtr;
00346 if (nodePtr == NULL)
00347 break;
00348 }
00349
00350 if (nodePtr == NULL)
00351 return _lastNodePtr->data->data;
00352 else
00353 return nodePtr->prevPtr->data;
00354 }
00355
00356 template <typename PrtType, typename DataType>
00357 const DataType& PriorityQueue<PrtType, DataType>::last(const PrtType &p) const throw (MissingPriorityException)
00358 {
00359 if (*_sizePtr == 0)
00360 throw MissingPriorityException();
00361
00362 PrtQueueNode<PrtType, DataType> *nodePtr = _firstNodePtr->data;
00363 while (nodePtr->prt != p)
00364 {
00365 nodePtr = nodePtr->nextPtr;
00366 if (nodePtr == NULL)
00367 break;
00368 }
00369
00370 if (nodePtr == NULL)
00371 throw MissingPriorityException();
00372
00373 while (nodePtr->prt == p)
00374 {
00375 nodePtr = nodePtr->nextPtr;
00376 if (nodePtr == NULL)
00377 break;
00378 }
00379
00380 if (nodePtr == NULL)
00381 return _lastNodePtr->data->data;
00382 else
00383 return nodePtr->prevPtr->data;
00384 }
00385
00386 template <typename PrtType, typename DataType>
00387 bool PriorityQueue<PrtType, DataType>::hasPriority(const PrtType &p)
00388 {
00389 if (*_sizePtr == 0)
00390 return false;
00391
00392 PrtQueueNode<PrtType, DataType> *nodePtr = _firstNodePtr->data;
00393 while (nodePtr->prt != p)
00394 {
00395 nodePtr = nodePtr->nextPtr;
00396 if (nodePtr == NULL)
00397 break;
00398 }
00399
00400 if (nodePtr == NULL)
00401 return false;
00402 else
00403 return true;
00404 }
00405
00406 template <typename PrtType, typename DataType>
00407 void PriorityQueue<PrtType, DataType>::deque(void)
00408 {
00409 if (*_sizePtr == 0)
00410 return;
00411
00412 if (*_sizePtr == 1)
00413 {
00414 delete _firstNodePtr->data;
00415 _lastNodePtr->data = _firstNodePtr->data = NULL;
00416 }
00417 else
00418 {
00419 PrtQueueNode<PrtType, DataType> *nodePtr = _firstNodePtr->data;
00420 _firstNodePtr->data = nodePtr->nextPtr;
00421 _firstNodePtr->data->prevPtr = NULL;
00422
00423 delete nodePtr;
00424 }
00425
00426 (*_sizePtr)--;
00427 }
00428
00429 template <typename PrtType, typename DataType>
00430 void PriorityQueue<PrtType, DataType>::purge(void)
00431 {
00432 while (*_sizePtr != 0)
00433 {
00434 if (*_sizePtr == 1)
00435 {
00436 delete _firstNodePtr->data;
00437 _lastNodePtr->data = _firstNodePtr->data = NULL;
00438 }
00439 else
00440 {
00441 PrtQueueNode<PrtType, DataType> *nodePtr = _firstNodePtr->data;
00442 _firstNodePtr->data = nodePtr->nextPtr;
00443 _firstNodePtr->data->prevPtr = NULL;
00444
00445 delete nodePtr;
00446 }
00447
00448 (*_sizePtr)--;
00449 }
00450 }
00451
00452 template <typename PrtType, typename DataType>
00453 void PriorityQueue<PrtType, DataType>::enque(const PrtType &prt, const DataType &data)
00454 {
00455 PrtQueueNode<PrtType, DataType> *newNodePtr = new PrtQueueNode<PrtType, DataType>();
00456 newNodePtr->data = data;
00457 newNodePtr->prt = prt;
00458
00459 if ((*_sizePtr) == 0)
00460 {
00461 _firstNodePtr->data = newNodePtr;
00462 _lastNodePtr->data = newNodePtr;
00463 }
00464 else
00465 {
00466 PrtQueueNode<PrtType, DataType> *nodePtr = _firstNodePtr->data;
00467 while (nodePtr != NULL)
00468 {
00469 if (nodePtr->prt > prt)
00470 break;
00471
00472 nodePtr = nodePtr->nextPtr;
00473 }
00474
00475 if (nodePtr == NULL)
00476 {
00477
00478 _lastNodePtr->data->nextPtr = newNodePtr;
00479 newNodePtr->prevPtr = _lastNodePtr->data;
00480
00481 _lastNodePtr->data = newNodePtr;
00482 }
00483 else
00484 {
00485
00486 if (nodePtr->prevPtr != NULL)
00487 {
00488
00489 newNodePtr->prevPtr = nodePtr->prevPtr;
00490 newNodePtr->nextPtr = nodePtr;
00491
00492 nodePtr->prevPtr = newNodePtr;
00493 newNodePtr->prevPtr->nextPtr = newNodePtr;
00494 }
00495 else
00496 {
00497
00498 _firstNodePtr->data->prevPtr = newNodePtr;
00499 newNodePtr->nextPtr = _firstNodePtr->data;
00500
00501 _firstNodePtr->data = newNodePtr;
00502 }
00503 }
00504 }
00505
00506 (*_sizePtr)++;
00507 }
00508
00509
00510
00511
00512
00513 template <typename PrtType, typename DataType>
00514 DataType PriorityQueue<PrtType, DataType>::Iterator::next(void) throw (OutOfBoundsException)
00515 {
00516 if (_currPtr == NULL)
00517 throw OutOfBoundsException();
00518
00519 PrtQueueNode<PrtType, DataType> *node = _currPtr;
00520 _currPtr = _currPtr->nextPtr;
00521
00522 return node->data;
00523 }
00524
00525 }
00526
00527 #endif // __SEAL_PRTQUEUE_H__
00528