JS8Call-Improved master
Loading...
Searching...
No Matches
qpriorityqueue.h
1/****************************************************************************
2**
3** Copyright (C) 2015 Corentin Chary <corentin.chary@gmail.cm>
4**
5** This file could be part of the QtCore module of the Qt Toolkit.
6**
7** $QT_BEGIN_LICENSE:LGPL$
8** No Commercial Usage
9** This file contains pre-release code and may not be distributed.
10** You may use this file in accordance with the terms and conditions
11** contained in the Technology Preview License Agreement accompanying
12** this package.
13**
14** GNU Lesser General Public License Usage
15** Alternatively, this file may be used under the terms of the GNU Lesser
16** General Public License version 2.1 as published by the Free Software
17** Foundation and appearing in the file LICENSE.LGPL included in the
18** packaging of this file. Please review the following information to
19** ensure the GNU Lesser General Public License version 2.1 requirements
20** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
21**
22** In addition, as a special exception, Nokia gives you certain additional
23** rights. These rights are described in the Nokia Qt LGPL Exception
24** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
25**
26** If you have questions regarding the use of this file, please contact
27** Nokia at qt-info@nokia.com.
28** $QT_END_LICENSE$
29**
30****************************************************************************/
31
32#ifndef QPRIORITY_QUEUE_H
33#define QPRIORITY_QUEUE_H
34
35#include <QtCore/qalgorithms.h>
36
37QT_BEGIN_NAMESPACE
38
39template <class T> class QList;
40
41#ifndef QT_NO_STL
42#include <queue>
43#include <vector>
44#else
45/* Fallback class used when stl is not available */
46template <class T, typename LessThan = std::less<T>>
47class QPriorityQueuePrivate {
48 public:
49 inline QPriorityQueuePrivate(LessThan l) : lessThan(l), d() {}
50 inline ~QPriorityQueuePrivate() {}
51
52 inline void clear() { return d.clear(); }
53 inline int size() const { return d.size(); }
54 inline bool empty() const { return d.empty(); }
55
56 inline T &top() { return d.front(); }
57
58 void pop();
59 void push(const T &value);
60
61 private:
62 inline int parent(int i) { return (i - 1) / 2; }
63 inline int leftChild(int i) { return 2 * i + 1; }
64 inline int rightChild(int i) { return 2 * i + 2; }
65
66 LessThan lessThan;
67 QList<T> d;
68};
69#endif
70
71template <class T, typename LessThan = std::less<T>>
72class Q_CORE_EXPORT QPriorityQueue {
73 public:
74 inline QPriorityQueue(LessThan l = std::less<T>())
75 : lessThan(l), d(lessThan) {}
76 inline QPriorityQueue(const QPriorityQueue<T> &q)
77 : lessThan(q.lessThan), d(q.d) {}
78 inline ~QPriorityQueue() {}
79
80 QPriorityQueue<T> &operator=(const QPriorityQueue<T> &q) {
81 d = q.d;
82 return *this;
83 }
84
85 inline int size() const { return d.size(); }
86 inline bool isEmpty() const { return empty(); }
87
88 // like qlist
89 void clear();
90 void append(const T &t) { return enqueue(t); }
91 void append(const QList<T> &t);
92 T takeFirst() { return dequeue(); }
93 inline int length() const { return size(); } // Same as count()
94 inline T &first() { return head(); }
95 inline const T &first() const { return head(); }
96 inline void removeFirst() { pop(); }
97 inline bool startsWith(const T &t) const {
98 return !isEmpty() && first() == t;
99 }
100
101 // like qqueue
102 inline void enqueue(const T &t) { push(t); }
103 inline T dequeue() {
104 T t = d.top();
105 d.pop();
106 return t;
107 }
108 inline T &head() { return const_cast<T &>(top()); }
109 inline const T &head() const { return top(); }
110
111 // stl compatibility
112 typedef int size_type;
113 typedef T value_type;
114
115 inline bool empty() const { return d.empty(); }
116 inline const value_type &top() {
117 Q_ASSERT(!isEmpty());
118 return d.top();
119 }
120 inline void push(const value_type &x) { return d.push(x); }
121 inline void pop() {
122 Q_ASSERT(!isEmpty());
123 d.pop();
124 }
125
126 // comfort
127 inline QPriorityQueue<T> &operator+=(const T &t) {
128 enqueue(t);
129 return *this;
130 }
131 inline QPriorityQueue<T> &operator<<(const T &t) {
132 enqueue(t);
133 return *this;
134 }
135 inline QPriorityQueue<T> &operator>>(T &t) {
136 t = d.top();
137 d.pop();
138 return *this;
139 }
140
141#ifndef QT_NO_STL
142 static inline QPriorityQueue<T>
143 fromStdPriorityQueue(const std::priority_queue<T> &q) {
144 QPriorityQueue<T> tmp;
145 tmp.d = q;
146 return tmp;
147 }
148 inline std::priority_queue<T> toStdPriorityQueue() const { return d; }
149#endif
150
151 private:
152 LessThan lessThan;
153#ifndef QT_NO_STL
154 std::priority_queue<T, std::vector<T>, LessThan> d;
155#else
156 QPriorityQueuePrivate<T> d;
157#endif
158};
159
160#ifndef QT_NO_STL
161
162template <typename T, typename LessThan>
163Q_INLINE_TEMPLATE void QPriorityQueue<T, LessThan>::clear() {
164 d = std::priority_queue<T, std::vector<T>, LessThan>(lessThan);
165 // d = std::priority_queue<T>(lessThan);
166}
167
168#else
169
170template <typename T, typename LessThan>
171Q_INLINE_TEMPLATE void QPriorityQueue<T, LessThan>::clear() {
172 d.clear();
173}
174
175#endif
176
177template <typename T, typename LessThan>
178Q_OUTOFLINE_TEMPLATE void
179QPriorityQueue<T, LessThan>::append(const QList<T> &t) {
180 foreach (T &e, t)
181 push(e);
182}
183
184// Re-implement std::priority_queue if STL not available, probably
185// less efficient.
186#ifdef QT_NO_STL
193template <typename T, typename LessThan>
194Q_OUTOFLINE_TEMPLATE void QPriorityQueuePrivate<T, LessThan>::pop() {
195 int i = 0;
196 ssize_t size = d.size();
197 ;
198
199 if (!size)
200 return;
201 if (size == 1)
202 return d.clear();
203
204 d[0] = d.takeLast();
205
206 while (i < size - 1) {
207 int left = leftChild(i);
208 int right = rightChild(i);
209 bool validLeft = left < size;
210 bool validRight = right < size;
211
212 if (validLeft && lessThan(d.at(i), d.at(left)))
213 if (validRight && !lessThan(d.at(right), d.at(left))) {
214 d.swap(i, right);
215 i = right;
216 } else {
217 d.swap(i, left);
218 i = left;
219 }
220 else if (validRight && lessThan(d.at(i), d.at(right))) {
221 d.swap(i, right);
222 i = right;
223 } else
224 break;
225 }
226}
227
233template <typename T, typename LessThan>
234Q_OUTOFLINE_TEMPLATE void
235QPriorityQueuePrivate<T, LessThan>::push(const T &value) {
236 int i = d.size();
237 d.append(value);
238 while (i != 0 && !lessThan(d.at(i), d.at(parent(i)))) {
239 d.swap(i, parent(i));
240 i = parent(i);
241 }
242}
243#endif
244
245QT_END_NAMESPACE
246
247#endif // QPRIORITY_QUEUE_H
Definition qpriorityqueue.h:39