ScummVM API documentation
PriorityQueue.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/*
23 * This code is based on the CRAB engine
24 *
25 * Copyright (c) Arvind Raja Yadav
26 *
27 * Licensed under MIT
28 *
29 */
30
31#ifndef CRAB_PRIORITYQUEUE_H
32#define CRAB_PRIORITYQUEUE_H
33
34
35namespace Crab {
36
41template<typename Node>
44 bool (*compare)(Node const *, Node const *);
45
46public:
49 explicit PriorityQueue(bool (*)(Node const *, Node const *));
50
53 bool empty() const;
54
59 void clear();
60
62 size_t size() const;
63
70 void push(Node *node);
71
76 Node *front() const;
77
84 void pop();
85
87 void remove(Node const *node);
88
98};
99
100template<typename Node>
101PriorityQueue<Node>::PriorityQueue(bool (*c)(Node const *, Node const *)) : _open(), compare(c) {
102}
103
104template<typename Node>
106 return _open.empty();
107}
108
109template<typename Node>
111 _open.clear();
112}
113
114template<typename Node>
116 return _open.size();
117}
118
119template<typename Node>
121 _open.insert(Common::upperBound(_open.begin(), _open.end(), node, compare), node);
122}
123
124template<typename Node>
126 return _open.back();
127}
128
129template<typename Node>
131 _open.pop_back();
132}
133
134template<typename Node>
136 _open.erase(Common::remove(_open.begin(), _open.end(), node), _open.end());
137}
138
139template<typename Node>
141 sorted.resize(_open.size());
142 Common::copy(_open.begin(), _open.end(), sorted.begin());
143}
144
145} // End of namespace Crab
146
147#endif // CRAB_PRIORITYQUEUE_H
iterator end()
Definition: array.h:380
iterator erase(iterator pos)
Definition: array.h:329
void insert(iterator pos, const T &element)
Definition: array.h:256
size_type size() const
Definition: array.h:316
T & back()
Definition: array.h:230
void clear()
Definition: array.h:321
void resize(size_type newSize)
Definition: array.h:412
bool empty() const
Definition: array.h:352
iterator begin()
Definition: array.h:375
void pop_back()
Definition: array.h:200
The open heap used by all cost-based search algorithms.
Definition: PriorityQueue.h:42
void remove(Node const *node)
Removes all instances of the specified node from the heap.
Definition: PriorityQueue.h:135
void clear()
Removes all nodes from the heap.
Definition: PriorityQueue.h:110
size_t size() const
Returns the number of nodes currently in the heap.
Definition: PriorityQueue.h:115
void pop()
Removes the least costly node from the heap.
Definition: PriorityQueue.h:130
bool empty() const
Returns true if the heap contains no nodes, false otherwise.
Definition: PriorityQueue.h:105
PriorityQueue(bool(*)(Node const *, Node const *))
Constructs a new PriorityQueue that heap-sorts nodes using the specified comparator.
Definition: PriorityQueue.h:101
void push(Node *node)
Pushes the specified node onto the heap.
Definition: PriorityQueue.h:120
Node * front() const
Returns the least costly node in the heap.
Definition: PriorityQueue.h:125
void enumerate(Common::Array< Node const * > &sorted) const
Enumerates all nodes in the heap so far.
Definition: PriorityQueue.h:140
It remove(It first, It last, const T &val)
Definition: algorithm.h:466
Out copy(In first, In last, Out dst)
Definition: algorithm.h:52
RandomIt upperBound(RandomIt first, RandomIt last, const V &val, Comp comp={})
Definition: algorithm.h:513
Definition: lobject.h:332