ScummVM API documentation
priority_queue.h
1/* ScummVM - Graphic Adventure Engine
2 *
3 * ScummVM is the legal property of its developers, whose names
4 * are too numerous to list here. Please refer to the COPYRIGHT
5 * file distributed with this source distribution.
6 *
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 *
20 */
21
22#ifndef ULTIMA8_MISC_PRIORITY_QUEUE_H
23#define ULTIMA8_MISC_PRIORITY_QUEUE_H
24
25#include "common/algorithm.h"
26#include "common/util.h"
27
28namespace Ultima {
29namespace Ultima8 {
30
36template <class _Ty, class _Container, class _Pr>
38public:
39 PriorityQueue() : c(), comp() {}
40
41 explicit PriorityQueue(const _Pr &_Pred) : c(), comp(_Pred) {}
42
43 PriorityQueue(const _Pr &_Pred, const _Container &_Cont) : c(_Cont), comp(_Pred) {
44 make_heap(c.begin(), c.end(), comp);
45 }
46
47 template <class _InIt>
48 PriorityQueue(_InIt _First, _InIt _Last, const _Pr &_Pred, const _Container &_Cont) : c(_Cont), comp(_Pred) {
49 c.insert(c.end(), _First, _Last);
50 make_heap(c.begin(), c.end(), comp);
51 }
52
53 template <class _InIt>
54 PriorityQueue(_InIt _First, _InIt _Last) : c(_First, _Last), comp() {
55 make_heap(c.begin(), c.end(), comp);
56 }
57
58 template <class _InIt>
59 PriorityQueue(_InIt _First, _InIt _Last, const _Pr &_Pred) : c(_First, _Last), comp(_Pred) {
60 make_heap(c.begin(), c.end(), comp);
61 }
62
63 bool empty() const {
64 return c.empty();
65 }
66
67 size_t size() const {
68 return c.size();
69 }
70
71 typename _Container::const_reference top() const {
72 return c.back();
73 }
74
75 void push(const typename _Container::value_type &_Val) {
76 c.push_back(_Val);
77 Common::sort(c.begin(), c.end(), comp);
78 }
79
80 void pop() {
81 c.pop_back();
82 }
83
84 void swap(PriorityQueue &_Right) {
85 SWAP(c, _Right.c);
86 SWAP(comp, _Right.comp);
87 }
88
89protected:
90 _Container c;
91 _Pr comp;
92};
93
94} // End of namespace Ultima8
95} // End of namespace Ultima
96
97#endif
Definition: priority_queue.h:37
void sort(T first, T last, StrictWeakOrdering comp)
Definition: algorithm.h:349
void SWAP(T &a, T &b)
Definition: util.h:467