J
Jim Strathmeyer
So, I've been looking for a wrapper class for STL priority queues, and
haven't been able to find one. (Websearching for info about the STL sure
shows how much it's changed over the past few years.) I basically need
to be able to queue up an object with a time, and retrieve and/or pop
the element with the earliest time. So I coded a little something up. I
also have a push function to reinsert the topmost element. I'm not very
good with templates, or with stl or priority_queues, so if I've done
anything blindingly stupid please let me know.
#ifndef PRIORITYQUEUE_T
#define PRIORITYQUEUE_T
#include <queue>
template <typename T>
class PriorityQueue {
private:
template <typename U>
typedef struct PriorityElement {
U * element;
double time;
PriorityElement(U * element, double time) : t(t), time(time) { }
};
template <typename V>
struct LessPriorityElement {
bool operator()(PriorityElement<V> * x, PriorityElement<V> * y) const {
return (x->time > y->time);
}
};
std:
riority_queue<PriorityElement<T> *,
std::vector<PriorityElement<T> *>,
LessPriorityElement<T> > pq;
public:
void Add(T * e, double time) {
pq.push(new PriorityElement<T>(e,time));
}
void Push(double time) {
if( time = 0.0 ) return;
PriorityElement<T> * pe = pq.top();
pq.pop();
pe->time += time;
pq.push(pe);
}
void Pop() {
delete pq.top();
pq.pop();
}
T * Top() const {
if( pq.empty() ) return NULL;
return pq.top()->element;
}
double TopTime() const {
if( pq.empty() ) return 0.0;
return pq.top()->time;
}
bool Empty() const {
return pq.empty();
}
unsigned Size() const {
return pq.size();
}
};
#endif
haven't been able to find one. (Websearching for info about the STL sure
shows how much it's changed over the past few years.) I basically need
to be able to queue up an object with a time, and retrieve and/or pop
the element with the earliest time. So I coded a little something up. I
also have a push function to reinsert the topmost element. I'm not very
good with templates, or with stl or priority_queues, so if I've done
anything blindingly stupid please let me know.
#ifndef PRIORITYQUEUE_T
#define PRIORITYQUEUE_T
#include <queue>
template <typename T>
class PriorityQueue {
private:
template <typename U>
typedef struct PriorityElement {
U * element;
double time;
PriorityElement(U * element, double time) : t(t), time(time) { }
};
template <typename V>
struct LessPriorityElement {
bool operator()(PriorityElement<V> * x, PriorityElement<V> * y) const {
return (x->time > y->time);
}
};
std:
std::vector<PriorityElement<T> *>,
LessPriorityElement<T> > pq;
public:
void Add(T * e, double time) {
pq.push(new PriorityElement<T>(e,time));
}
void Push(double time) {
if( time = 0.0 ) return;
PriorityElement<T> * pe = pq.top();
pq.pop();
pe->time += time;
pq.push(pe);
}
void Pop() {
delete pq.top();
pq.pop();
}
T * Top() const {
if( pq.empty() ) return NULL;
return pq.top()->element;
}
double TopTime() const {
if( pq.empty() ) return 0.0;
return pq.top()->time;
}
bool Empty() const {
return pq.empty();
}
unsigned Size() const {
return pq.size();
}
};
#endif