1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326
|
/***************************************************************************
* Copyright (C) 2005-2013 by the FIFE team *
* http://www.fifengine.net *
* This file is part of FIFE. *
* *
* FIFE is free software; you can redistribute it and/or *
* modify it under the terms of the GNU Lesser General Public *
* License as published by the Free Software Foundation; either *
* version 2.1 of the License, or (at your option) any later version. *
* *
* This library is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
* Lesser General Public License for more details. *
* *
* You should have received a copy of the GNU Lesser General Public *
* License along with this library; if not, write to the *
* Free Software Foundation, Inc., *
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
***************************************************************************/
#ifndef FIFE_SOLVER_INDEXEDPQ_H
#define FIFE_SOLVER_INDEXEDPQ_H
#include <cassert>
#include <list>
namespace FIFE {
/** A pq which stores index-value pairs for elements.
*
* This acts as a normal PQ but stores some extra information about the
* elements that it's storing, namely a special unique index.
*/
template<typename index_type, typename priority_type>
class PriorityQueue {
public:
/** Used for element ordering.
*
*/
enum Ordering {
Ascending, //!< lowest priority first.
Descending //!< highest priority first.
};
typedef std::pair<index_type, priority_type> value_type;
/** Constructor
*
*/
PriorityQueue(void) : m_ordering(Ascending) {
}
/** Constructor
*
* @param ordering The ordering the priority queue should use.
*/
PriorityQueue(const Ordering ordering) : m_ordering(ordering) {
}
/** Pushes a new element onto the queue.
*
* The element is pushed onto the queue and then moved up the queue until it's
* in the correct position by priority.
*
* @param element Of type value_type which contains both the index and the priority of the element.
*/
void pushElement(const value_type& element);
/** Pops the element with the highest priority from the queue.
*
* Removes and deletes the highest priority element.
*/
void popElement(void);
/** Changes the priority of an element.
*
* Locates the element with the given index and changes it's priority to the given
* priority, it then re-orders the priority queue to take account of this new information.
*
* @param index The index of the element to change the priority of.
* @param newPriority The new priority of the element.
* @return True if the element could be found, false otherwise.
*/
bool changeElementPriority(const index_type& index, const priority_type& newPriority);
/** Removes all elements from the priority queue.
*
*/
void clear(void);
/** Retrieves the element with the highest priority.
*
* This function will generate an assertion error if the pq is
* empty.
*
* @return A const reference to the highest priority element.
*/
const value_type getPriorityElement(void) const {
assert(!empty());
return m_elements.front();
}
/** Determines whether the queue is currently empty.
*
* @return true if it is empty, false otherwise.
*/
bool empty(void) const {
return m_elements.empty();
}
/** Returns the current size of the queue.
*
*/
size_t size(void) const {
return m_elements.size();
}
private:
typedef std::list<value_type> ElementList;
typedef typename ElementList::iterator ElementListIt;
typedef typename ElementList::const_iterator ElementListConstIt;
//A list of valuetype pairs that represents the pq.
ElementList m_elements;
//The order to use when sorting the pq.
Ordering m_ordering;
/** Orders a PQ element up the list.
*
* @param i An iterator representing the element in the list to be sorted up.
*/
void orderUp(ElementListIt i);
/** Orders a PQ element up the list.
*
* @param entry A const reference to a value_type which represents the element to be added to the
* pq.
*/
void orderUp(const value_type& entry);
/** Orders a PQ element down the list.
*
* @param i An iterator representing the element in the PQ to order down.
*/
void orderDown(ElementListIt i);
/** Retrieves the iterator to the element with the given index.
*
* @param index A const reference to the index to find.
*/
ElementListIt getElementIterator(const index_type& index) {
for(ElementListIt i = m_elements.begin(); i != m_elements.end(); ++i) {
if(i->first == index) {
return i;
}
}
return m_elements.end();
}
/** The comparison function, used to compare two elements.
*
* @param a The l-operand of the comparison operation.
* @param b The r-operand of the comparison operation.
* @return An integer representing the result of the comparison operation. 1 being a is greather than b,
* -1 being a is less than b and 0 meaning that they're equal.
*/
int32_t compare(const value_type& a, const value_type& b);
};
}
template<typename index_type, typename priority_type>
void FIFE::PriorityQueue<index_type, priority_type>::pushElement(const value_type& element) {
if(empty()) {
m_elements.push_front(element);
}
else {
orderUp(element);
}
}
template<typename index_type, typename priority_type>
void FIFE::PriorityQueue<index_type, priority_type>::popElement(void) {
if(!empty()) {
m_elements.pop_front();
}
}
template<typename index_type, typename priority_type>
bool FIFE::PriorityQueue<index_type, priority_type>::changeElementPriority(const index_type& index, const priority_type& newPriority) {
ElementListIt i = getElementIterator(index);
if(i == m_elements.end()) {
return false;
}
int32_t compare_res = compare(value_type(index, newPriority), (*i));
i->second = newPriority;
if(compare_res > 0 && i != m_elements.begin()) {
orderDown(i);
} else if(compare_res < 0) {
orderUp(i);
}
return true;
}
template<typename index_type, typename priority_type>
void FIFE::PriorityQueue<index_type, priority_type>::clear(void) {
m_elements.clear();
}
template<typename index_type, typename priority_type>
void FIFE::PriorityQueue<index_type, priority_type>::orderUp(ElementListIt i) {
assert(i != m_elements.end() && L"Invalid iterator passed to function");
value_type vt = (*i);
i = m_elements.erase(i);
while(i != m_elements.end()) {
if(compare(vt, (*i)) > 0) {
m_elements.insert(i, vt);
return;
}
++i;
}
m_elements.push_back(vt);
}
template<class index_type, class priority_type>
void FIFE::PriorityQueue<index_type, priority_type>::orderUp(const value_type& val)
{
for(ElementListIt i = m_elements.begin(); i != m_elements.end(); ++i)
{
assert(val.first != i->first);
if(compare(val, (*i)) > 0)
{
assert(val.first != i->first);
m_elements.insert(i, val);
return;
}
}
m_elements.push_back(val);
}
template<typename index_type, typename priority_type>
void FIFE::PriorityQueue<index_type, priority_type>::orderDown(ElementListIt i) {
assert(i != m_elements.end());
value_type vt = (*i);
i = m_elements.erase(i);
if(i == m_elements.end()) {
--i;
}
ElementListIt j = i;
++j;
while(i != m_elements.begin()) {
if(compare(vt, (*i)) < 0) {
m_elements.insert(j, vt);
return;
}
--i;
--j;
}
m_elements.push_front(vt);
}
template<typename index_type, typename priority_type>
int32_t FIFE::PriorityQueue<index_type, priority_type>::compare(const value_type& a, const value_type& b) {
if(m_ordering == Descending) {
if(a.second > b.second) {
return 1;
} else if(b.second > a.second) {
return -1;
}
} else {
if(a.second < b.second) {
return 1;
} else if(b.second < a.second) {
return -1;
}
}
return 0;
}
#endif
|