1
/* PeTe - Petri Engine exTremE
2
* Copyright (C) 2011 Jonas Finnemann Jensen <jopsen@gmail.com>,
3
* Thomas Søndersø Nielsen <primogens@gmail.com>,
4
* Lars Kærlund Østergaard <larsko@gmail.com>,
6
* This program is free software: you can redistribute it and/or modify
7
* it under the terms of the GNU General Public License as published by
8
* the Free Software Foundation, either version 3 of the License, or
9
* (at your option) any later version.
11
* This program is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
* GNU General Public License for more details.
16
* You should have received a copy of the GNU General Public License
17
* along with this program. If not, see <http://www.gnu.org/licenses/>.
19
#ifndef ENHANCEDPRIORITYQUEUE_H
20
#define ENHANCEDPRIORITYQUEUE_H
26
namespace PetriEngine{
30
/** Implementation of simple priority queue, that uses double as priority
31
* @remarks Lower priority is better, e.g. lowest priority comes out first!
32
* Note this is the same as PriorityQueue except this one uses a map and list
33
* internally, also it returns the newest item if two items have the same priority.
36
class EnhancedPriorityQueue{
38
typedef std::list<Item> ItemList;
39
typedef std::map<double, ItemList> Queue;
40
typedef typename Queue::iterator Iter;
41
typedef typename Queue::const_iterator ConstIter;
42
/** Underlying queue */
46
EnhancedPriorityQueue(){
50
/** Test if queue is empty */
52
return _queue.empty();
55
/** Get the size of the queue */
60
/** Delete everything in the queue */
66
/** Put a new item in the queue */
67
void push(double priority, const Item& item){
69
Iter it = _queue.find(priority);
70
if(it != _queue.end())
71
it->second.push_back(item);
73
_queue.insert(std::pair<double, ItemList>(priority, ItemList(1, item)));
76
/** Pop item with lowest priority */
77
Item pop(bool preferNewest = true){
80
assert(_queue.begin() != _queue.end());
81
Iter it = _queue.begin();
82
assert(!it->second.empty());
83
Item retval = preferNewest ? it->second.back() : it->second.front();
85
it->second.pop_back();
87
it->second.pop_front();
88
if(it->second.empty())
93
/** Get the top most item */
94
Item& top(bool preferNewest = true){
96
assert(_queue.begin() != _queue.end());
97
Iter it = _queue.begin();
98
assert(!it->second.empty());
99
return preferNewest ? it->second.back() : it->second.front();
102
/** Get priority of top most item in queue */
103
double topPriority() const {
105
assert(_queue.begin() != _queue.end());
106
ConstIter it = _queue.begin();
107
assert(!it->second.empty());
116
#endif // ENHANCEDPRIORITYQUEUE_H