1
// rak - Rakshasa's toolbox
2
// Copyright (C) 2005-2007, Jari Sundell
4
// This program is free software; you can redistribute it and/or modify
5
// it under the terms of the GNU General Public License as published by
6
// the Free Software Foundation; either version 2 of the License, or
7
// (at your option) any later version.
9
// This program is distributed in the hope that it will be useful,
10
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
// GNU General Public License for more details.
14
// You should have received a copy of the GNU General Public License
15
// along with this program; if not, write to the Free Software
16
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18
// In addition, as a special exception, the copyright holders give
19
// permission to link the code of portions of this program with the
20
// OpenSSL library under certain conditions as described in each
21
// individual source file, and distribute linked combinations
24
// You must obey the GNU General Public License in all respects for
25
// all of the code used other than OpenSSL. If you modify file(s)
26
// with this exception, you may extend this exception to your version
27
// of the file(s), but you are not obligated to do so. If you do not
28
// wish to do so, delete this exception statement from your version.
29
// If you delete this exception statement from all source files in the
30
// program, then also delete it here.
32
// Contact: Jari Sundell <jaris@ifi.uio.no>
35
// 3185 Skoppum, NORWAY
37
// priority_queue is a priority queue implemented using a binary
38
// heap. It can contain multiple instances of a value.
40
#ifndef RAK_PRIORITY_QUEUE_H
41
#define RAK_PRIORITY_QUEUE_H
49
template <typename Value, typename Compare, typename Equal, typename Alloc = std::allocator<Value> >
50
class priority_queue : public std::vector<Value, Alloc> {
52
typedef std::vector<Value, Alloc> base_type;
53
typedef typename base_type::reference reference;
54
typedef typename base_type::const_reference const_reference;
55
typedef typename base_type::iterator iterator;
56
typedef typename base_type::const_iterator const_iterator;
57
typedef typename base_type::value_type value_type;
59
using base_type::begin;
61
using base_type::size;
62
using base_type::empty;
64
priority_queue(Compare l = Compare(), Equal e = Equal())
65
: m_compare(l), m_equal(e) {}
67
const_reference top() const {
68
return base_type::front();
72
std::pop_heap(begin(), end(), m_compare);
73
base_type::pop_back();
76
void push(const value_type& value) {
77
base_type::push_back(value);
78
std::push_heap(begin(), end(), m_compare);
81
template <typename Key>
82
iterator find(const Key& key) {
83
return std::find_if(begin(), end(), std::bind2nd(m_equal, key));
86
template <typename Key>
87
bool erase(const Key& key) {
88
iterator itr = find(key);
97
// Removes 'itr' from the queue. This assumes 'itr' has been
98
// modified such that it has a higher priority than any other
99
// element in the queue.
100
void erase(iterator itr) {
101
// std::push_heap(begin(), ++itr, m_compare);
103
base_type::erase(itr);
104
std::make_heap(begin(), end(), m_compare);
112
// Iterate while the top node has higher priority, as 'Compare'
114
template <typename Queue, typename Compare>
115
class queue_pop_iterator
116
: public std::iterator<std::forward_iterator_tag, void, void, void, void> {
118
typedef Queue container_type;
120
queue_pop_iterator() : m_queue(NULL) {}
121
queue_pop_iterator(Queue* q, Compare c) : m_queue(q), m_compare(c) {}
123
queue_pop_iterator& operator ++ () { m_queue->pop(); return *this; }
124
queue_pop_iterator& operator ++ (int) { m_queue->pop(); return *this; }
126
typename container_type::const_reference operator * () { return m_queue->top(); }
128
bool operator != (const queue_pop_iterator& itr) { return !m_queue->empty() && !m_compare(m_queue->top()); }
129
bool operator == (const queue_pop_iterator& itr) { return m_queue->empty() || m_compare(m_queue->top()); }
136
template <typename Queue, typename Compare>
137
inline queue_pop_iterator<Queue, Compare>
138
queue_popper(Queue& queue, Compare comp) {
139
return queue_pop_iterator<Queue, Compare>(&queue, comp);