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 
35 namespace Crab {
36 
41 template<typename Node>
44  bool (*compare)(Node const *, Node const *);
45 
46 public:
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 
97  void enumerate(Common::Array<Node const *> &sorted) const;
98 };
99 
100 template<typename Node>
101 PriorityQueue<Node>::PriorityQueue(bool (*c)(Node const *, Node const *)) : _open(), compare(c) {
102 }
103 
104 template<typename Node>
106  return _open.empty();
107 }
108 
109 template<typename Node>
111  _open.clear();
112 }
113 
114 template<typename Node>
116  return _open.size();
117 }
118 
119 template<typename Node>
121  _open.insert(Common::upperBound(_open.begin(), _open.end(), node, compare), node);
122 }
123 
124 template<typename Node>
126  return _open.back();
127 }
128 
129 template<typename Node>
131  _open.pop_back();
132 }
133 
134 template<typename Node>
136  _open.erase(Common::remove(_open.begin(), _open.end(), node), _open.end());
137 }
138 
139 template<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
void enumerate(Common::Array< Node const *> &sorted) const
Enumerates all nodes in the heap so far.
Definition: PriorityQueue.h:140
PriorityQueue(bool(*)(Node const *, Node const *))
Constructs a new PriorityQueue that heap-sorts nodes using the specified comparator.
Definition: PriorityQueue.h:101
Definition: array.h:52
iterator begin()
Definition: array.h:374
The open heap used by all cost-based search algorithms.
Definition: PriorityQueue.h:42
void pop()
Removes the least costly node from the heap.
Definition: PriorityQueue.h:130
Out copy(In first, In last, Out dst)
Definition: algorithm.h:52
bool empty() const
Returns true if the heap contains no nodes, false otherwise.
Definition: PriorityQueue.h:105
RandomIt upperBound(RandomIt first, RandomIt last, const V &val, Comp comp={})
Definition: algorithm.h:513
void push(Node *node)
Pushes the specified node onto the heap.
Definition: PriorityQueue.h:120
Definition: moveeffect.h:37
void clear()
Removes all nodes from the heap.
Definition: PriorityQueue.h:110
It remove(It first, It last, const T &val)
Definition: algorithm.h:466
void resize(size_type newSize)
Definition: array.h:411
Definition: lobject.h:332
size_t size() const
Returns the number of nodes currently in the heap.
Definition: PriorityQueue.h:115
Node * front() const
Returns the least costly node in the heap.
Definition: PriorityQueue.h:125