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 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399
|
// -*- C++ -*-
/*!
\file ads/indexedPriorityQueue/IndexedPriorityQueueBinaryHeapPair.h
\brief Indexed priority queue that partitions the active and inactive elements.
*/
#if !defined(__ads_indexedPriorityQueue_IndexedPriorityQueueBinaryHeapPair_h__)
#define __ads_indexedPriorityQueue_IndexedPriorityQueueBinaryHeapPair_h__
#include "IndexedPriorityQueueBase.h"
#include <stddef.h>
#include <cstddef>
namespace ads {
//! Indexed priority queue that partitions the active and inactive elements.
/*!
\param Key is the key type.
*/
template < typename _Key = double >
class IndexedPriorityQueueBinaryHeapPair {
//
// Enumerations.
//
public:
enum {UsesPropensities = false};
//
// Public types.
//
public:
//! The key type.
typedef _Key Key;
//
// Private types.
//
private:
typedef std::pair<int, Key> Value;
typedef std::vector<Value> Queue;
typedef typename Queue::const_iterator ConstIterator;
typedef typename Queue::iterator Iterator;
//
// Nested classes.
//
private:
//! Compare two queue elements.
struct Compare {
bool
operator()(const Value& x, const Value& y) const {
return x.second < y.second;
}
};
//
// Member data.
//
private:
Queue _queue;
std::vector<Iterator> _pointers;
Iterator _heapEnd;
Compare _compare;
//--------------------------------------------------------------------------
//! \name Constructors etc.
//@{
public:
//! Construct from the size.
IndexedPriorityQueueBinaryHeapPair(const std::size_t size) :
_queue(size),
_pointers(size),
_heapEnd(_queue.begin()) {
clear();
}
//@}
//--------------------------------------------------------------------------
//! \name Accessors.
//@{
public:
//! Return the key of the specified element.
Key
get(const int index) const {
return _pointers[index]->second;
}
//! Return true if the binary heap data struture is valid.
bool
isValid() const {
const std::size_t size = _heapEnd - _queue.begin();
std::size_t parent = 0;
for (std::size_t child = 1; child < size; ++child) {
if (_compare(_queue[child], _queue[parent])) {
return false;
}
if ((child & 1) == 0) {
++parent;
}
}
return true;
}
private:
//! Return the beginning of the queue.
ConstIterator
getQueueBeginning() const {
return _queue.begin();
}
//! Return the end of the queue.
ConstIterator
getQueueEnd() const {
return _heapEnd;
}
//@}
//--------------------------------------------------------------------------
//! \name Manipulators.
//@{
public:
//! Return the index of the top element.
int
top() {
#ifdef DEBUG_stlib
assert(! _queue.empty());
#endif
return _queue.begin()->first;
}
//! Pop the top element off the queue.
void
popTop() {
#ifdef DEBUG_stlib
assert(_queue.begin()->second != std::numeric_limits<Key>::max());
#endif
_queue.begin()->second = std::numeric_limits<Key>::max();
remove(0);
#ifdef DEBUG_stlib
assert(isValid());
#endif
}
//! Pop the element off the queue.
void
pop(const int index) {
#ifdef DEBUG_stlib
assert(isValid());
#endif
#ifdef DEBUG_stlib
// It must be in the active queue.
assert(_pointers[index]->second != std::numeric_limits<Key>::max());
#endif
_pointers[index]->second = std::numeric_limits<Key>::max();
remove(_pointers[index] - _queue.begin());
#ifdef DEBUG_stlib
assert(isValid());
#endif
}
//! Push the value into the queue.
void
push(const int index, const Key key) {
#ifdef DEBUG_stlib
assert(_pointers[index]->second == std::numeric_limits<Key>::max() &&
key != std::numeric_limits<Key>::max());
#endif
_pointers[index]->second = key;
insert(index);
#ifdef DEBUG_stlib
assert(isValid());
#endif
}
//! Push the value at the top into the queue.
void
pushTop(const Key key) {
#ifdef DEBUG_stlib
assert(key != std::numeric_limits<Key>::max());
#endif
_queue[0].second = key;
#ifdef GIBSON_BRUCK_UPDATE
updateRecursive(0);
#else
pushDown(0);
#endif
#ifdef DEBUG_stlib
assert(isValid());
#endif
}
//! Change the value in the queue.
void
set(const int index, const Key key) {
#ifdef DEBUG_stlib
assert(_pointers[index]->second != std::numeric_limits<Key>::max() &&
key != std::numeric_limits<Key>::max());
#endif
// If we are using the Gibson and Bruck updating algorithm.
#ifdef GIBSON_BRUCK_UPDATE
_pointers[index]->second = key;
updateRecursive(_pointers[index] - getQueueBeginning());
#else
if (key < _pointers[index]->second) {
_pointers[index]->second = key;
pushUp(_pointers[index] - getQueueBeginning());
}
else {
_pointers[index]->second = key;
pushDown(_pointers[index] - getQueueBeginning());
}
#endif
#ifdef DEBUG_stlib
assert(isValid());
#endif
}
//! Clear the queue.
void
clear() {
for (std::size_t i = 0; i != _queue.size(); ++i) {
_queue[i].first = i;
_queue[i].second = std::numeric_limits<Key>::max();
}
for (std::size_t i = 0; i != _queue.size(); ++i) {
_pointers[i] = _queue.begin() + i;
}
_heapEnd = _queue.begin();
}
//! Shift the keys by the specified amount.
void
shift(const Key x) {
for (std::size_t i = 0; i != _queue.size(); ++i) {
_queue[i].second += x;
}
}
private:
//! Return the beginning of the queue.
Iterator
getQueueBeginning() {
return _queue.begin();
}
//! Return the end of the queue.
Iterator
getQueueEnd() {
return _heapEnd;
}
//! Remove the element from the heap.
/*!
\pre The element must be in the heap.
*/
void
remove(const int n) {
#ifdef DEBUG_stlib
assert(n < _heapEnd - getQueueBeginning());
#endif
--_heapEnd;
swap(_queue.begin() + n, _heapEnd);
#ifdef GIBSON_BRUCK_UPDATE
updateRecursive(n);
#else
pushUpOrDown(n);
#endif
}
//! Insert the element in the heap.
/*!
\pre The element must not be in the heap.
*/
void
insert(const int index) {
#ifdef DEBUG_stlib
assert(_pointers[index] >= _heapEnd);
#endif
swap(_pointers[index], _heapEnd);
#ifdef GIBSON_BRUCK_UPDATE
updateRecursive(_heapEnd - getQueueBeginning());
#else
pushUp(_heapEnd - getQueueBeginning());
#endif
++_heapEnd;
}
void
pushUp(ptrdiff_t child) {
ptrdiff_t parent = (child - 1) / 2;
while (child > 0 && _compare(_queue[child], _queue[parent])) {
swap(getQueueBeginning() + child, getQueueBeginning() + parent);
child = parent;
parent = (child - 1) / 2;
}
}
void
pushDown(ptrdiff_t parent) {
ptrdiff_t child = getSmallerChild(parent);
while (child != 0 && _compare(_queue[child], _queue[parent])) {
swap(getQueueBeginning() + child, getQueueBeginning() + parent);
parent = child;
child = getSmallerChild(parent);
}
}
//! The Gibson and Bruck updating algorithm.
void
updateRecursive(ptrdiff_t n) {
ptrdiff_t parent = (n - 1) / 2;
if (n > 0 && _compare(_queue[n], _queue[parent])) {
swap(getQueueBeginning() + n, getQueueBeginning() + parent);
updateRecursive(parent);
}
else {
ptrdiff_t child = getSmallerChild(n);
if (child != 0 && _compare(_queue[child], _queue[n])) {
swap(getQueueBeginning() + child, getQueueBeginning() + n);
updateRecursive(child);
}
}
}
//! Update the position.
void
update(ptrdiff_t n) {
while (true) {
ptrdiff_t parent = (n - 1) / 2;
if (n > 0 && _compare(_queue[n], _queue[parent])) {
swap(getQueueBeginning() + n, getQueueBeginning() + parent);
n = parent;
continue;
}
else {
ptrdiff_t child = getSmallerChild(n);
if (child != 0 && _compare(_queue[child], _queue[n])) {
swap(getQueueBeginning() + child, getQueueBeginning() + n);
n = child;
continue;
}
}
break;
}
}
//! Update the position. This is faster than update().
void
pushUpOrDown(ptrdiff_t n) {
ptrdiff_t parent = (n - 1) / 2;
if (n > 0 && _compare(_queue[n], _queue[parent])) {
pushUp(n);
}
else {
pushDown(n);
}
}
//! Return the index of the smaller child or 0 if there are no children.
ptrdiff_t
getSmallerChild(ptrdiff_t parent) const {
const ptrdiff_t size = getQueueEnd() - getQueueBeginning();
ptrdiff_t child = 2 * parent + 1;
// If there are no children.
if (child >= size) {
return 0;
}
// If the second child is smaller.
if (child + 1 < size && _compare(_queue[child + 1], _queue[child])) {
++child;
}
return child;
}
//! Swap the two elements' positions in the queue.
void
swap(const Iterator i, const Iterator j) {
std::swap(_pointers[i->first], _pointers[j->first]);
std::swap(*i, *j);
}
//@}
};
} // namespace ads
#endif
|