EMAN2
priority_queue.h
Go to the documentation of this file.
1// Copyright (C) 2005-2008 Washington University in St Louis, Baylor College of Medicine. All rights reserved
2// Author: Tao Ju (taoju@cse.wustl.edu)
3// Description: A Priority queue implementation
4
5#ifndef PRIORITYQUEUE_H
6#define PRIORITYQUEUE_H
7
8#include <cstdlib>
9#include <cstdio>
10
11namespace EMAN {
12
16template < class ValueT, class KeyT >
18{
19public:
22
25
27 ValueT ** valueQueue ;
28
30 KeyT * keyQueue ;
31
32public:
33
37 PriorityQueue ( int max )
38 {
39 this->maxLength = max ;
40 this->queueLength = 0 ;
41 this->valueQueue = new ValueT* [ max ] ;
42 this->keyQueue = new KeyT [ max ] ;
43
44 };
45
50 {
51 delete [] keyQueue ;
52 for ( int i = 0 ; i < queueLength ; i ++ )
53 {
54 delete valueQueue [ i ] ;
55 }
56 delete [] valueQueue ;
57 };
58
62 int getLength ( )
63 {
64 return this->queueLength ;
65 };
66
70 bool isEmpty ( )
71 {
72 return ( this->queueLength == 0 ) ;
73 };
74
78 bool isFull ( )
79 {
80 return ( this->queueLength == this->maxLength ) ;
81 };
82
86 void add ( ValueT * v, KeyT k )
87 {
88 if ( this->isFull() )
89 {
90 printf("PRIORITY QUEUE FILLED UP !!! \n");
91 return ;
92 }
93
94 int ind = queueLength ;
95 int tind ;
96 queueLength ++ ;
97
98 while ( ind > 0 )
99 {
100 tind = ( ind + 1 ) / 2 - 1 ;
101 if ( k < keyQueue[tind] )
102 {
103 keyQueue[ ind ] = keyQueue [ tind ] ;
104 valueQueue [ ind ] = valueQueue [ tind ] ;
105 ind = tind ;
106 }
107 else
108 {
109 break;
110 }
111 }
112
113 valueQueue[ ind ] = v ;
114 keyQueue [ ind ] = k ;
115 };
116
120 void remove ( ValueT *& v, KeyT & k )
121 {
122 if ( this->isEmpty() )
123 {
124 v = NULL ;
125 k = 0 ;
126 return ;
127 }
128
129 v = valueQueue[0] ;
130 k = keyQueue[0] ;
131 queueLength -- ;
132
133 if ( queueLength == 0 )
134 {
135 valueQueue[0] = NULL ;
136 return ;
137 }
138
139 ValueT * vv = valueQueue [ queueLength ] ;
140 KeyT kk = keyQueue [ queueLength ], lowk ;
141 int ind = 0, tind, ind2, ind3 ;
142 while ( 1 )
143 {
144 ind2 = 2 * ( ind + 1 ) - 1 ;
145 ind3 = ind2 + 1 ;
146 tind = ind ;
147 lowk = kk ;
148
149 if ( ind2 >= queueLength )
150 {
151 break ;
152 }
153 else
154 {
155 if ( keyQueue [ ind2 ] < lowk )
156 {
157 tind = ind2 ;
158 lowk = keyQueue [ ind2 ] ;
159 }
160
161 if ( ind3 < queueLength )
162 {
163 if ( keyQueue [ ind3 ] < lowk )
164 {
165 tind = ind3 ;
166 }
167 }
168
169 if ( ind != tind )
170 {
171 valueQueue [ ind ] = valueQueue [ tind ] ;
172 keyQueue [ ind ] = keyQueue [ tind ] ;
173 ind = tind ;
174 }
175 else
176 {
177 break ;
178 }
179 }
180 }
181
182 valueQueue [ ind ] = vv ;
183 keyQueue [ ind ] = kk ;
184 valueQueue [ queueLength ] = NULL ;
185 keyQueue [ queueLength ] = NULL ;
186 };
187
188};
189
190
191}
192
193#endif
Template class for a priority queue.
int queueLength
Number of elements in queue.
~PriorityQueue()
Destructor.
void remove(ValueT *&v, KeyT &k)
Remove an element.
PriorityQueue(int max)
Constructor.
void add(ValueT *v, KeyT k)
Add an element.
ValueT ** valueQueue
Queue of elements.
bool isFull()
Test whether full.
int getLength()
Get current length.
KeyT * keyQueue
Queue of keys.
int maxLength
Maximum number of elements int he queue.
bool isEmpty()
Test whether empty.
E2Exception class.
Definition: aligner.h:40