Event::AsyncEventPrioQueue< C, F > Class Template Reference

Priority queue template. More...

#include <AsyncEventRef.h>

Inheritance diagram for Event::AsyncEventPrioQueue< C, F >:

Public Member Functions

void Insert (AsyncEvent *event, C prio)
 
virtual void extract ()
 
AsyncEvent * First (const C prio)
 
AsyncEvent * First ()
 
bool HasEvents ()
 
const C * NextPrio ()
 

Detailed Description

template<class C, class F>
class Event::AsyncEventPrioQueue< C, F >

Priority queue template.

Definition at line 108 of file AsyncEventRef.h.

Constructor & Destructor Documentation

◆ AsyncEventPrioQueue()

template<class C , class F >
Event::AsyncEventPrioQueue< C, F >::AsyncEventPrioQueue ( )
inline

Definition at line 134 of file AsyncEventRef.h.

134 {
135 _level = 1;
136 _next[0] = _prev[0] = this;
137 memset (&_prio, 0, sizeof _prio);
138 _rand = 12345678;
139 }

◆ ~AsyncEventPrioQueue()

template<class C , class F >
virtual Event::AsyncEventPrioQueue< C, F >::~AsyncEventPrioQueue ( )
inlinevirtual

Definition at line 141 of file AsyncEventRef.h.

141{}

Member Function Documentation

◆ Insert()

template<class C , class F >
void Event::AsyncEventPrioQueue< C, F >::Insert ( AsyncEvent *  event,
prio 
)
inline

Definition at line 143 of file AsyncEventRef.h.

143 {
144 // Generate next random number
145 long int tmp = _rand/RAND_IQ;
146 _rand = tmp = RAND_IA*(_rand - tmp*RAND_IQ) - RAND_IR*tmp;
147
148 // Fold the upper bits into the lower bits
149 tmp ^= tmp >>_maxlevel*_shift;
150
151 int level = 1;
152
153 while ( level < _maxlevel && level <= _level && (tmp & _mask) == _mask ) {
154 level++;
155 tmp >>= _shift;
156 }
157
158 if ( level > _level ) {
159 // Need to fill in the head to head pointers
160 // As level cannot be more than one more than _level,
161 // there is no more than one pair to fill in
162 _next[_level] = _prev[_level] = this;
163 _level++;
164 }
165
166 AsyncEventPrioQueue *newq = _freelist;
167 if ( newq ) {
168 _freelist = newq->_next[0];
169 newq->Set (event);
170 newq->_prio = prio;
171 newq->_level = level;
172
173 } else {
174 newq = new AsyncEventPrioQueue (event, level, prio);
175 }
176
177 AsyncEventPrioQueue *succ = this;
178 AsyncEventPrioQueue *pred = this;
179
180 // Need to traverse all levels
181 level = _level;
182
183 F comp(prio);
184
185 while ( --level >= 0 ) {
186 // At the start of the loop we know that the new item
187 // Follows succ and preceeds pred.
188
189 AsyncEventPrioQueue *prev = succ->_prev[level];
190
191 // If the item preceeding the successor at this level
192 // is the same as the predecessor at the previous level
193 // there is nothing to do.
194 if ( prev != pred ) {
195
196 // Chain along at the current level, while the key of
197 // the predecessor follows the key of the new event
198
199 while ( comp.preceeds (prev->_prio) ) {
200 succ = prev;
201 prev = succ->_prev[level];
202 }
203
204 pred = prev;
205 }
206
207
208 // Insert the new item at this point in the level
209 // if the item has this level
210
211 if ( level < newq->_level ) {
212 newq->_next[level] = succ;
213 newq->_prev[level] = prev;
214 succ->_prev[level] = newq;
215 pred->_next[level] = newq;
216 }
217 }
218 }

◆ extract()

template<class C , class F >
virtual void Event::AsyncEventPrioQueue< C, F >::extract ( )
inlinevirtual

Definition at line 220 of file AsyncEventRef.h.

220 {
221 int level = _level;
222 while ( --level >= 0 ) {
223 _next[level]->_prev[level] = _prev[level];
224 _prev[level]->_next[level] = _next[level];
225 }
226 _next[0] = _freelist;
227 _freelist = this;
228 }

◆ First() [1/2]

template<class C , class F >
AsyncEvent * Event::AsyncEventPrioQueue< C, F >::First ( const C  prio)
inline

Definition at line 230 of file AsyncEventRef.h.

230 {
231 F comp (prio);
232
233 // Only return if there is something to return and
234 // The priority of the event is not lower than the given priority
235 if ( _next[0] == this || comp.preceeds (_next[0]->_prio) )
236 return 0;
237
238 return _next[0]->Extract();
239 }

◆ First() [2/2]

template<class C , class F >
AsyncEvent * Event::AsyncEventPrioQueue< C, F >::First ( )
inline

Definition at line 241 of file AsyncEventRef.h.

241 {
242 // Only return if there is something to return
243 // The priority of the event is not lower than the given priority
244 if ( _next[0] == this )
245 return 0;
246
247 return _next[0]->Extract();
248 }

◆ HasEvents()

template<class C , class F >
bool Event::AsyncEventPrioQueue< C, F >::HasEvents ( )
inline

Definition at line 250 of file AsyncEventRef.h.

250 {
251 return _next[0] != this;
252 }

◆ NextPrio()

template<class C , class F >
const C * Event::AsyncEventPrioQueue< C, F >::NextPrio ( )
inline

Definition at line 254 of file AsyncEventRef.h.

254 {
255 if ( _next[0] == this )
256 return 0;
257
258 return &_next[0]->_prio;
259 }

The documentation for this class was generated from the following file:

All rights reserved © 2002 - 2024 Isode Ltd.