File: PriorityQueue_STLPQ.h

package info (click to toggle)
libassa 3.5.1-2
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 3,268 kB
  • sloc: cpp: 15,703; sh: 12,083; makefile: 379; perl: 51
file content (169 lines) | stat: -rw-r--r-- 3,876 bytes parent folder | download | duplicates (9)
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
// -*- c++ -*-
//------------------------------------------------------------------------------
//                          PriorityQueue_STLPQ.h
//------------------------------------------------------------------------------
//  Copyright (c) 1999 by Vladislav Grinchenko
//
//  This library is free software; you can redistribute it and/or
//  modify it under the terms of the GNU Library General Public
//  License as published by the Free Software Foundation; either
//  version 2 of the License, or (at your option) any later version.
//------------------------------------------------------------------------------
//  Created: 09/29/1999
//------------------------------------------------------------------------------
#ifndef PRIORITY_QUEUE_STLPQ_H
#define PRIORITY_QUEUE_STLPQ_H

#include <stack>
#include <deque>
#include <list>
#include <queue>
using namespace std;

#include "assa/Timer.h"

namespace ASSA {

/** @file PriorityQueue_STLPQ.h  

    Priority Queue implementation based on STL priority_queue. 
*/

template< class T, class Compare >
class PriorityQueue_STLPQ : 
	public PriorityQueue_Impl< T, Compare >
{
public:
        ~PriorityQueue_STLPQ ();

	void     insert (const T&);
	T        pop ();
	const T& top () const;
	bool     remove (const int);
	size_t   size ();

	void     dump ();

private:
	priority_queue<T*, deque<T*>, Compare>  m_queue;
};

template< class T, class Compare>
inline
PriorityQueue_STLPQ<T, Compare>::
~PriorityQueue_STLPQ ()
{
	trace("PriorityQueue_STLPQ::~PriorityQueue_STLPQ"); 

	while ( m_queue.size () ) {
		delete m_queue.top ();
		m_queue.pop ();
	}
}

template< class T, class Compare>
inline void
PriorityQueue_STLPQ<T, Compare>::
insert (const T& t_)
{
	trace("PriorityQueue_STLPQ::insert");
	m_queue.push (t_);
}

template< class T, class Compare>
inline T
PriorityQueue_STLPQ<T, Compare>::
pop ()
{
	trace("PriorityQueue_STLPQ::pop");

	T t = m_queue.top ();
	m_queue.pop ();
	return t;
}

template< class T, class Compare>
inline const T&
PriorityQueue_STLPQ<T, Compare>::
top () const
{
	trace("PriorityQueue_STLPQ::top");
	return (const T&) m_queue.top ();
}

/*******************************************************************************
   STL priority queue doesn't allow to remove arbitrary
   element from the queue. Only top element can be removed.
   To search for the element, I extract top one, and if it 
   doesn't match my search, put it into list<>. When either
   found or reached end of queue, I restore all elements
   in the list<> back to the priority queue.
   This needs rethinking!
*******************************************************************************/
template< class T, class Compare>
bool
PriorityQueue_STLPQ<T, Compare>::
remove (const int id_)
{
	trace("PriorityQueue_STLPQ::remove");

	list<Timer*> t_list;
	register Timer* t_ptr = 0;
	register int cnt = 0;

	while (m_queue.size () > 0) {
		t_ptr = m_queue.top ();
		if (t_ptr->getHandler ()-> id() == id_) {
			delete t_ptr;
			cnt++;
		}
		else {
			t_list.push_back (t_ptr);
		}
		m_queue.pop ();
	}
	// Restore queue
	
	list<Timer*>::iterator i;

	for (i = t_list.begin (); i != t_list.end (); i++) {
		m_queue.push (*i);
	}

	return cnt;
}

template< class T, class Compare>
inline size_t
PriorityQueue_STLPQ<T, Compare>::
size ()
{
	return m_queue.size ();
}

template< class T, class Compare>
inline void
PriorityQueue_STLPQ<T, Compare>::
dump () 
{
	trace("PriorityQueue_STLPQ::dump");

	list<Timer*> t_list;
	register Timer* t_ptr = 0;
	DL((TRACE,"======TimerQueue start=======\n"));
	while (m_queue.size () > 0) {
		t_ptr = m_queue.top ();
		t_ptr->dump ();
		t_list.push_back (t_ptr);
	}
	DL((TRACE,"======TimerQueue end=========\n"));
	list<Timer*>::iterator i;

	for (i = t_list.begin (); i != t_list.end (); i++) {
		m_queue.push (*i);
	}
}

} // end namespace ASSA

#endif /* PRIORITY_QUEUE_STLPQ_H */