| 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 |
#ifndef QPRIORITY_QUEUE_H |
| 41 |
#define QPRIORITY_QUEUE_H |
| 42 |
|
| 43 |
#include <algorithm> |
| 44 |
#include <QList> |
| 45 |
|
| 46 |
template <class T> class QList; |
| 47 |
|
| 48 |
|
| 49 |
template <class T, typename LessThan = std::less < T* > > |
| 50 |
class QPriorityQueuePrivate { |
| 51 |
public: |
| 52 |
inline QPriorityQueuePrivate(LessThan l) : lessThan(l), d() {} |
| 53 |
inline ~QPriorityQueuePrivate() {} |
| 54 |
|
| 55 |
inline void clear() { qDeleteAll(d); return d.clear(); } |
| 56 |
inline int size() const { return d.size(); } |
| 57 |
inline bool empty() const { return d.empty(); } |
| 58 |
|
| 59 |
inline T *top() { return d.front(); } |
| 60 |
|
| 61 |
void pop(); |
| 62 |
void push(T *value); |
| 63 |
|
| 64 |
QList<T *> toList() {return d;} |
| 65 |
private: |
| 66 |
inline int parent(int i) { |
| 67 |
return (i - 1) / 2; |
| 68 |
} |
| 69 |
inline int leftChild(int i) { |
| 70 |
return 2 * i + 1; |
| 71 |
} |
| 72 |
inline int rightChild(int i) { |
| 73 |
return 2 * i + 2; |
| 74 |
} |
| 75 |
|
| 76 |
LessThan lessThan; |
| 77 |
QList < T *> d; |
| 78 |
}; |
| 79 |
|
| 80 |
template <class T, typename LessThan = std::less < T* > > |
| 81 |
class Q_CORE_EXPORT QPriorityQueue |
| 82 |
{ |
| 83 |
public: |
| 84 |
inline QPriorityQueue(LessThan l = std::less < T* >()) |
| 85 |
: lessThan(l), d(lessThan) { } |
| 86 |
inline QPriorityQueue(const QPriorityQueue<T> &q) |
| 87 |
: lessThan(q.lessThan), d(q.d) { } |
| 88 |
inline ~QPriorityQueue() { } |
| 89 |
|
| 90 |
QPriorityQueue<T> &operator=(const QPriorityQueue<T> &q) { |
| 91 |
d = q.d; return *this; |
| 92 |
} |
| 93 |
|
| 94 |
inline int size() const { return d.size(); } |
| 95 |
inline bool isEmpty() const { return empty(); } |
| 96 |
|
| 97 |
|
| 98 |
void clear(); |
| 99 |
void append(T *t) { return enqueue(t); } |
| 100 |
void append(const QList<T*> t); |
| 101 |
T* takeFirst() { return dequeue(); } |
| 102 |
inline int length() const { return size(); } |
| 103 |
inline T* first() { return head(); } |
| 104 |
inline const T* first() const { return head(); } |
| 105 |
inline void removeFirst() { pop(); } |
| 106 |
inline bool startsWith(const T &t) const |
| 107 |
{ return !isEmpty() && first() == t; } |
| 108 |
|
| 109 |
|
| 110 |
inline void enqueue(T* t) { push(t); } |
| 111 |
inline T* dequeue() { T* t = d.top(); d.pop(); return t; } |
| 112 |
inline T* head() { return top(); } |
| 113 |
inline const T* head() const { return top(); } |
| 114 |
|
| 115 |
|
| 116 |
typedef int size_type; |
| 117 |
typedef T* value_type; |
| 118 |
|
| 119 |
inline bool empty() const { return d.empty(); } |
| 120 |
inline value_type top() { Q_ASSERT(!isEmpty()); return d.top(); } |
| 121 |
inline void push(value_type x) { return d.push(x); } |
| 122 |
inline void pop() { Q_ASSERT(!isEmpty()); d.pop(); } |
| 123 |
inline QList<T*> toList() { return d.toList(); } |
| 124 |
|
| 125 |
|
| 126 |
inline QPriorityQueue<T> &operator+=(T* t) { |
| 127 |
enqueue(t); return *this; |
| 128 |
} |
| 129 |
inline QPriorityQueue<T> &operator<< (T* t) { |
| 130 |
enqueue(t); return *this; |
| 131 |
} |
| 132 |
inline QPriorityQueue<T> &operator>> (T* t) { |
| 133 |
t = d.top(); d.pop(); return *this; |
| 134 |
} |
| 135 |
|
| 136 |
|
| 137 |
private: |
| 138 |
LessThan lessThan; |
| 139 |
|
| 140 |
QPriorityQueuePrivate <T> d; |
| 141 |
}; |
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
template <typename T, typename LessThan> |
| 146 |
Q_INLINE_TEMPLATE void QPriorityQueue<T, LessThan>::clear() |
| 147 |
{ |
| 148 |
d.clear(); |
| 149 |
} |
| 150 |
|
| 151 |
|
| 152 |
template <typename T, typename LessThan> |
| 153 |
Q_OUTOFLINE_TEMPLATE void QPriorityQueue<T, LessThan>::append(const QList<T *> t) |
| 154 |
{ |
| 155 |
foreach (T * e, t) |
| 156 |
push(e); |
| 157 |
} |
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
template <typename T, typename LessThan> |
| 166 |
Q_OUTOFLINE_TEMPLATE void QPriorityQueuePrivate<T, LessThan>::pop() |
| 167 |
{ |
| 168 |
int i = 0; |
| 169 |
ssize_t size = d.size();; |
| 170 |
|
| 171 |
if(!size) |
| 172 |
return; |
| 173 |
if(size == 1) |
| 174 |
return d.clear(); |
| 175 |
|
| 176 |
d[0] = d.takeLast(); |
| 177 |
|
| 178 |
while(i < size - 1) { |
| 179 |
int left = leftChild(i); |
| 180 |
int right = rightChild(i); |
| 181 |
bool validLeft = left < size; |
| 182 |
bool validRight = right < size; |
| 183 |
|
| 184 |
if(validLeft && lessThan(d.at(i), d.at(left))) |
| 185 |
if(validRight && !lessThan(d.at(right), d.at(left))) { |
| 186 |
d.swap(i, right); |
| 187 |
i = right; |
| 188 |
} else { |
| 189 |
d.swap(i, left); |
| 190 |
i = left; |
| 191 |
} |
| 192 |
else if(validRight && lessThan(d.at(i), d.at(right))) { |
| 193 |
d.swap(i, right); |
| 194 |
i = right; |
| 195 |
} |
| 196 |
else |
| 197 |
break; |
| 198 |
} |
| 199 |
} |
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
template <typename T, typename LessThan> |
| 207 |
Q_OUTOFLINE_TEMPLATE void QPriorityQueuePrivate<T, LessThan>::push(T *value) |
| 208 |
{ |
| 209 |
int i = d.size(); |
| 210 |
d.append(value); |
| 211 |
while(i != 0 && !lessThan(d.at(i), d.at(parent(i)))) { |
| 212 |
d.swap(i, parent(i)); |
| 213 |
i = parent(i); |
| 214 |
} |
| 215 |
} |
| 216 |
|
| 217 |
|
| 218 |
#endif |